golangci-lint: decoder/unmarshal (#518)

This commit is contained in:
Vincent Serpoul
2021-04-22 21:29:23 +08:00
committed by GitHub
parent 21445f5170
commit 2b1c52dddd
6 changed files with 253 additions and 177 deletions
+10 -54
View File
@@ -7,7 +7,10 @@ import (
"github.com/stretchr/testify/require"
)
//nolint:funlen
func TestParser_AST_Numbers(t *testing.T) {
t.Parallel()
examples := []struct {
desc string
input string
@@ -132,7 +135,9 @@ func TestParser_AST_Numbers(t *testing.T) {
}
for _, e := range examples {
e := e
t.Run(e.desc, func(t *testing.T) {
t.Parallel()
p := parser{}
p.Reset([]byte(`A = ` + e.input))
p.NextExpression()
@@ -156,7 +161,6 @@ func TestParser_AST_Numbers(t *testing.T) {
}
type (
astRoot []astNode
astNode struct {
Kind ast.Kind
Data []byte
@@ -164,11 +168,6 @@ type (
}
)
func compareAST(t *testing.T, expected astRoot, actual *ast.Root) {
it := actual.Iterator()
compareIterator(t, expected, it)
}
func compareNode(t *testing.T, e astNode, n ast.Node) {
t.Helper()
require.Equal(t, e.Kind, n.Kind)
@@ -199,55 +198,10 @@ func compareIterator(t *testing.T, expected []astNode, actual ast.Iterator) {
}
}
func (r astRoot) toOrig() *ast.Root {
builder := &ast.Builder{}
var last ast.Reference
for i, n := range r {
ref := builder.Push(ast.Node{
Kind: n.Kind,
Data: n.Data,
})
if i > 0 {
builder.Chain(last, ref)
}
last = ref
if len(n.Children) > 0 {
c := childrenToOrig(builder, n.Children)
builder.AttachChild(ref, c)
}
}
return builder.Tree()
}
func childrenToOrig(b *ast.Builder, nodes []astNode) ast.Reference {
var first ast.Reference
var last ast.Reference
for i, n := range nodes {
ref := b.Push(ast.Node{
Kind: n.Kind,
Data: n.Data,
})
if i == 0 {
first = ref
} else {
b.Chain(last, ref)
}
last = ref
if len(n.Children) > 0 {
c := childrenToOrig(b, n.Children)
b.AttachChild(ref, c)
}
}
return first
}
//nolint:funlen
func TestParser_AST(t *testing.T) {
t.Parallel()
examples := []struct {
desc string
input string
@@ -384,7 +338,9 @@ func TestParser_AST(t *testing.T) {
}
for _, e := range examples {
e := e
t.Run(e.desc, func(t *testing.T) {
t.Parallel()
p := parser{}
p.Reset([]byte(e.input))
p.NextExpression()