package toml import ( "testing" ) func TestTableKey(t *testing.T) { for _, test := range []parseToASTTest{ {"[", `{}`, `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() testParseToAST(t, p, p.startTable, test) } } func TestTable(t *testing.T) { for _, test := range []parseToASTTest{ {"[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}}`, ``}, } { p := newParser() testParseToAST(t, p, p.startTable, test) } } func TestArrayOfTableKey(t *testing.T) { for _, test := range []parseToASTTest{ {"[[", `{}`, `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() testParseToAST(t, p, p.startTable, test) } } func TestArrayOfTables(t *testing.T) { for _, test := range []parseToASTTest{ {"[[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}}}`, ``}, } { p := newParser() testParseToAST(t, p, p.startTable, test) } }