Files
go-toml/internal/unmarshaler/parser_test.go
T
Thomas Pelletier 04925e4882 Handle bools
2021-03-14 15:52:22 -04:00

169 lines
3.0 KiB
Go

package unmarshaler
import (
"testing"
"github.com/pelletier/go-toml/v2/internal/ast"
"github.com/stretchr/testify/require"
)
func TestParser_AST(t *testing.T) {
examples := []struct {
desc string
input string
ast ast.Root
err bool
}{
{
desc: "simple string assignment",
input: `A = "hello"`,
ast: ast.Root{
ast.Node{
Kind: ast.KeyValue,
Children: []ast.Node{
{
Kind: ast.Key,
Data: []byte(`A`),
},
{
Kind: ast.String,
Data: []byte(`hello`),
},
},
},
},
},
{
desc: "simple bool assignment",
input: `A = true`,
ast: ast.Root{
ast.Node{
Kind: ast.KeyValue,
Children: []ast.Node{
{
Kind: ast.Key,
Data: []byte(`A`),
},
{
Kind: ast.Bool,
Data: []byte(`true`),
},
},
},
},
},
{
desc: "array of strings",
input: `A = ["hello", ["world", "again"]]`,
ast: ast.Root{
ast.Node{
Kind: ast.KeyValue,
Children: []ast.Node{
{
Kind: ast.Key,
Data: []byte(`A`),
},
{
Kind: ast.Array,
Children: []ast.Node{
{
Kind: ast.String,
Data: []byte(`hello`),
},
{
Kind: ast.Array,
Children: []ast.Node{
{
Kind: ast.String,
Data: []byte(`world`),
},
{
Kind: ast.String,
Data: []byte(`again`),
},
},
},
},
},
},
},
},
},
{
desc: "array of arrays of strings",
input: `A = ["hello", "world"]`,
ast: ast.Root{
ast.Node{
Kind: ast.KeyValue,
Children: []ast.Node{
{
Kind: ast.Key,
Data: []byte(`A`),
},
{
Kind: ast.Array,
Children: []ast.Node{
{
Kind: ast.String,
Data: []byte(`hello`),
},
{
Kind: ast.String,
Data: []byte(`world`),
},
},
},
},
},
},
},
{
desc: "inline table",
input: `name = { first = "Tom", last = "Preston-Werner" }`,
ast: ast.Root{
ast.Node{
Kind: ast.KeyValue,
Children: []ast.Node{
{
Kind: ast.Key,
Data: []byte(`name`),
},
{
Kind: ast.InlineTable,
Children: []ast.Node{
{
Kind: ast.KeyValue,
Children: []ast.Node{
{Kind: ast.Key, Data: []byte(`first`)},
{Kind: ast.String, Data: []byte(`Tom`)},
},
},
{
Kind: ast.KeyValue,
Children: []ast.Node{
{Kind: ast.Key, Data: []byte(`last`)},
{Kind: ast.String, Data: []byte(`Preston-Werner`)},
},
},
},
},
},
},
},
},
}
for _, e := range examples {
t.Run(e.desc, func(t *testing.T) {
p := parser{}
err := p.parse([]byte(e.input))
if e.err {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, e.ast, p.tree)
}
})
}
}