116 lines
5.6 KiB
Go
116 lines
5.6 KiB
Go
package toml
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.makaay.nl/mauricem/go-parsekit/parse"
|
|
)
|
|
|
|
func TestStartTable(t *testing.T) {
|
|
parser := newParser()
|
|
wrapper := func(p *parse.API) { parser.startTable(p) }
|
|
testParse(t, parser, wrapper, parseTest{"(not a table)", "{}", "unexpected input (expected a table) at start of file"})
|
|
}
|
|
|
|
func TestTableKey(t *testing.T) {
|
|
for _, test := range []parseTest{
|
|
{"[", `{}`, `unexpected end of file (expected a key name) at line 1, column 2`},
|
|
{" \t [", `{}`, `unexpected end of file (expected a key name) at line 1, column 5`},
|
|
{" \t [key", `{}`, `unexpected end of file (expected closing ']' for table name) at line 1, column 8`},
|
|
{" \t [key.", `{}`, `unexpected end of file (expected a key name) at line 1, column 9`},
|
|
{" \t [key.'sub key'", `{}`, `unexpected end of file (expected closing ']' for table name) at line 1, column 18`},
|
|
{" \t [key.'sub key' \t]", `{"key": {"sub key": {}}}`, ``},
|
|
{" \t [key.'sub key' \t] \t ", `{"key": {"sub key": {}}}`, ``},
|
|
{" \t [key.'sub key' \t] \t \n", `{"key": {"sub key": {}}}`, ``},
|
|
{" \t [key.'sub key' \t]# with comment\n", `{"key": {"sub key": {}}}`, ``},
|
|
{" \t [key.'sub key' \t] \t # with comment\n", `{"key": {"sub key": {}}}`, ``},
|
|
} {
|
|
p := newParser()
|
|
testParse(t, p, p.startTable, test)
|
|
}
|
|
}
|
|
|
|
func TestTable(t *testing.T) {
|
|
for _, test := range []parseTest{
|
|
{"[a]", `{"a": {}}`, ``},
|
|
{"['a key']", `{"a key": {}}`, ``},
|
|
{"[\"a key\"]", `{"a key": {}}`, ``},
|
|
{"[\"a key\".'sub'.key]", `{"a key": {"sub": {"key": {}}}}`, ``},
|
|
{"[a]\nx=1234", `{"a": {"x": 1234}}`, ``},
|
|
{"[a]\nx=1234\ny='string'", `{"a": {"x": 1234, "y": "string"}}`, ``},
|
|
{"[a]\n[b]", `{"a": {}, "b": {}}`, ``},
|
|
{"[a]\n[a.b]\n[a.b.c]\n[a.d]\nx=1", `{"a": {"b": {"c": {}}, "d": {"x": 1}}}`, ``},
|
|
{"[a]\n[b] #another table \na=1\n", `{"a": {}, "b": {"a": 1}}`, ``},
|
|
{"[a]\nx=1\ny=2\n[b] #another table \nx=1\ny=2021-01-01", `{"a": {"x": 1, "y": 2}, "b": {"x": 1, "y": 2021-01-01}}`, ``},
|
|
{"[a]\nx=1\ny=2\n[a.b] #subtable \nx=1\ny=2021-01-01", `{"a": {"b": {"x": 1, "y": 2021-01-01}, "x": 1, "y": 2}}`, ``},
|
|
{"[a]\n[a]", `{"a": {}}`, `invalid table: table item already exists at key [a] at line 2, column 4`},
|
|
} {
|
|
p := newParser()
|
|
testParse(t, p, p.startTable, test)
|
|
}
|
|
}
|
|
|
|
func TestArrayOfTableKey(t *testing.T) {
|
|
for _, test := range []parseTest{
|
|
{"[[", `{}`, `unexpected end of file (expected a key name) at line 1, column 3`},
|
|
{" \t [[", `{}`, `unexpected end of file (expected a key name) at line 1, column 6`},
|
|
{" \t [[key", `{}`, `unexpected end of file (expected closing ']]' for array of tables name) at line 1, column 9`},
|
|
{" \t [[key.", `{}`, `unexpected end of file (expected a key name) at line 1, column 10`},
|
|
{" \t [[key.'sub key'", `{}`, `unexpected end of file (expected closing ']]' for array of tables name) at line 1, column 19`},
|
|
{" \t [[key.'sub key' \t]]", `{"key": {"sub key": [{}]}}`, ``},
|
|
{" \t [[key.'sub key' \t]] \t ", `{"key": {"sub key": [{}]}}`, ``},
|
|
{" \t [[key.'sub key' \t]] \t \n", `{"key": {"sub key": [{}]}}`, ``},
|
|
{" \t [[key.'sub key' \t]]# with comment\n", `{"key": {"sub key": [{}]}}`, ``},
|
|
{" \t [[key.'sub key' \t]] \t # with comment\n", `{"key": {"sub key": [{}]}}`, ``},
|
|
} {
|
|
p := newParser()
|
|
testParse(t, p, p.startTable, test)
|
|
}
|
|
}
|
|
|
|
func TestArrayOfTables(t *testing.T) {
|
|
for _, test := range []parseTest{
|
|
{"[[a]]", `{"a": [{}]}`, ``},
|
|
{"[[a]]\n[['a']]\n[[\"a\"]]", `{"a": [{}, {}, {}]}`, ``},
|
|
{"[[a]]\n[['a']]\n[b]\n[[\"a\"]]", `{"a": [{}, {}, {}], "b": {}}`, ``},
|
|
{"[[a]]\nx=1\n[['a']]\nx=2\ny=3\n[[\"a\"]]", `{"a": [{"x": 1}, {"x": 2, "y": 3}, {}]}`, ``},
|
|
{"[a]\n[[a.b]]\nx=1\n[[a.b]]\nx=2\n[a.c]\ny=1234", `{"a": {"b": [{"x": 1}, {"x": 2}], "c": {"y": 1234}}}`, ``},
|
|
{"[a]\n[[a]]", `{"a": {}}`, `invalid table array: table item already exists at key [a] at line 2, column 6`},
|
|
} {
|
|
p := newParser()
|
|
testParse(t, p, p.startTable, test)
|
|
}
|
|
}
|
|
|
|
func TestStartInlineTable(t *testing.T) {
|
|
parser := newParser()
|
|
wrapper := func(p *parse.API) { parser.parseInlineTable(p) }
|
|
testParse(t, parser, wrapper, parseTest{"(not an inline table)", "{}", "unexpected input (expected an inline table) at start of file"})
|
|
}
|
|
|
|
func TestInlineTable(t *testing.T) {
|
|
for _, test := range []parseTest{
|
|
{"x={}", `{"x": {}}`, ``},
|
|
{"x={} # comments", `{"x": {}}`, ``},
|
|
{"x={ # comments }", `{}`, `unexpected input (expected a key name) at line 1, column 5`},
|
|
{"x={a = 1, b\t=2}", `{"x": {"a": 1, "b": 2}}`, ``},
|
|
{"x={a='string', b=\"values\"}", `{"x": {"a": "string", "b": "values"}}`, ``},
|
|
{"x={a={}}", `{"x": {"a": {}}}`, ``},
|
|
{"x={a=[{}]}", `{"x": {"a": [{}]}}`, ``},
|
|
{"x=[{b=1},{b=2},{b=3}]", `{"x": [{"b": 1}, {"b": 2}, {"b": 3}]}`, ``},
|
|
{"x={a=[{b=1},{b=2},{b=3}]}", `{"x": {"a": [{"b": 1}, {"b": 2}, {"b": 3}]}}`, ``},
|
|
{"x={", `{}`, `unexpected end of file (expected a key name) at line 1, column 4`},
|
|
{"x={a", `{}`, `unexpected end of file (expected a value assignment) at line 1, column 5`},
|
|
{"x={a=", `{}`, `unexpected end of file (expected a value) at line 1, column 6`},
|
|
{"x={a=,", `{}`, `unexpected input (expected a value) at line 1, column 6`},
|
|
{"x={a=1", `{}`, `unexpected end of file (expected an array separator) at line 1, column 7`},
|
|
{"x={a=1,", `{}`, `unexpected end of file (expected a key name) at line 1, column 8`},
|
|
{"x={a=1,}", `{}`, `unexpected input (expected a key name) at line 1, column 8`},
|
|
{"x={a=1,a=2}", `{}`, `invalid key/value pair: integer item already exists at key [a] at line 1, column 11`},
|
|
{"x={a={b=1,b=2}}", `{}`, `invalid key/value pair: integer item already exists at key [b] at line 1, column 14`},
|
|
} {
|
|
p := newParser()
|
|
testParse(t, p, p.startKeyValuePair, test)
|
|
}
|
|
}
|