package toml import "testing" func TestKey(t *testing.T) { for _, test := range []parseToASTTest{ // Bare key tests {"barekey=0", `{"barekey": 0}`, ``}, {"1234567=0", `{"1234567": 0}`, ``}, {"mix-12_34=0", `{"mix-12_34": 0}`, ``}, {"-hey_good_Lookin123-=0", `{"-hey_good_Lookin123-": 0}`, ``}, {"wrong!=0", `{}`, `unexpected input (expected a value assignment) at line 1, column 6`}, {"key1.=0", `{}`, `unexpected input (expected a key name) at line 1, column 6`}, {"key1.key2=0", `{"key1": {"key2": 0}}`, ``}, {"key . with . spaces=0", `{"key": {"with": {"spaces": 0}}}`, ``}, {"key \t . \twithtabs\t . \tandspaces=0", `{"key": {"withtabs": {"andspaces": 0}}}`, ``}, // Single quoted key tests {"''=0", `{"": 0}`, ``}, {"'single quoted'=0", `{"single quoted": 0}`, ``}, {`'escape\s are literal'=0`, `{"escape\\s are literal": 0}`, ``}, {`'"using inner quotes"'=0`, `{"\"using inner quotes\"": 0}`, ``}, // Double quoted key tests {`""=0`, `{"": 0}`, ``}, {`"double quoted"=0`, `{"double quoted": 0}`, ``}, {`"escapes are in\terpreted"=0`, `{"escapes are in\terpreted": 0}`, ``}, {`"using 'inner' \"quotes\""=0`, `{"using 'inner' \"quotes\"": 0}`, ``}, // Mixed key types {`this.'i\s'."madness\t".''=0`, `{"this": {"i\\s": {"madness\t": {"": 0}}}}`, ``}, } { p := newParser() testParseToAST(t, p, p.startKeyValuePair, test) } } func TestKeyValuePair(t *testing.T) { for _, test := range []parseToASTTest{ {``, `{}`, ``}, {` `, `{}`, ``}, {" \t ", `{}`, ``}, {" key ", `{}`, `unexpected input (expected a value assignment) at line 1, column 5`}, {" key \t=", `{}`, `unexpected end of file (expected a value) at line 1, column 8`}, {"key = # INVALID", `{}`, `unexpected input (expected a value) at line 1, column 7`}, {" key \t =\t \"The Value\" \r\n", `{"key": "The Value"}`, ``}, {`3.14159 = "pi"`, `{"3": {"14159": "pi"}}`, ``}, {`"ʎǝʞ" = "value"`, `{"ʎǝʞ": "value"}`, ``}, {`key = "value" # This is a comment at the end of a line`, `{"key": "value"}`, ``}, {`another = "# This is not a comment"`, `{"another": "# This is not a comment"}`, ``}, {"key1=\"value1\"key2=\"value2\"\r\nkey3a.key3b=\"value3\"", `{"key1": "value1", "key2": "value2", "key3a": {"key3b": "value3"}}`, ``}, {"with=\"comments\"# boring \nanother.cool =\"one\" \t # to the end\r\n", `{"another": {"cool": "one"}, "with": "comments"}`, ``}, } { p := newParser() testParseToAST(t, p, p.startKeyValuePair, test) } } func TestKeyValuePair_ForAllTypes(t *testing.T) { for _, test := range []parseToASTTest{ {"string='literal'", `{"string": "literal"}`, ``}, {"string='''literal\nmulti-line'''", `{"string": "literal\nmulti-line"}`, ``}, {`string="basic"`, `{"string": "basic"}`, ``}, {"string=\"\"\"basic\nmulti-line\"\"\"", `{"string": "basic\nmulti-line"}`, ``}, {"integer=1_234_567", `{"integer": 1234567}`, ``}, {"integer=42", `{"integer": 42}`, ``}, {"integer=0x42", `{"integer": 66}`, ``}, {"integer=0o42", `{"integer": 34}`, ``}, {"integer=0b101010", `{"integer": 42}`, ``}, {"float=42.37", `{"float": 42.37}`, ``}, {"float=42e+37", `{"float": 4.2e+38}`, ``}, {"float=42.37e-11", `{"float": 4.237e-10}`, ``}, {"boolean=true", `{"boolean": true}`, ``}, {"boolean=false", `{"boolean": false}`, ``}, {"date=2019-01-01", `{"date": 2019-01-01}`, ``}, {"time=15:03:11", `{"time": 15:03:11}`, ``}, {"datetime=2021-02-01 15:03:11.123", `{"datetime": 2021-02-01 15:03:11.123}`, ``}, {"offset_datetime=1111-11-11 11:11:11.111111111+11:11", `{"offset_datetime": 1111-11-11T11:11:11.111111111+11:11}`, ``}, {"static_array=['a', 'static', 'array']", `{"static_array": ["a", "static", "array"]}`, ``}, } { p := newParser() testParseToAST(t, p, p.startKeyValuePair, test) } } func TestKeyValuePair_ExamplesFromSpecification(t *testing.T) { for _, test := range []parseToASTTest{ {"int1 = +99", `{"int1": 99}`, ``}, {"int2 = 42", `{"int2": 42}`, ``}, {"int3 = 0", `{"int3": 0}`, ``}, {"int4 = -17", `{"int4": -17}`, ``}, {"int5 = 1_000", `{"int5": 1000}`, ``}, {"int6 = 5_349_221", `{"int6": 5349221}`, ``}, {"int7 = 1_2_3_4_5 # VALID but discouraged", `{"int7": 12345}`, ``}, {"hex1 = 0xDEADBEEF", `{"hex1": 3735928559}`, ``}, {"hex2 = 0xdeadbeef", `{"hex2": 3735928559}`, ``}, {"hex3 = 0xdead_beef", `{"hex3": 3735928559}`, ``}, {"oct1 = 0o01234567", `{"oct1": 342391}`, ``}, {"oct2 = 0o755", `{"oct2": 493}`, ``}, {"bin1 = 0b11010110", `{"bin1": 214}`, ``}, {"flt1 = +1.0", `{"flt1": 1}`, ``}, {"flt2 = 3.1415", `{"flt2": 3.1415}`, ``}, {"flt3 = -0.01", `{"flt3": -0.01}`, ``}, {"flt4 = 5e+22", `{"flt4": 5e+22}`, ``}, {"flt5 = 1e6", `{"flt5": 1e+06}`, ``}, {"flt6 = -2E-2", `{"flt6": -0.02}`, ``}, {"flt7 = 6.626e-34", `{"flt7": 6.626e-34}`, ``}, {"flt8 = 224_617.445_991_228", `{"flt8": 224617.445991228}`, ``}, {"sf1 = inf # positive infinity", `{"sf1": +Inf}`, ``}, {"sf2 = +inf # positive infinity", `{"sf2": +Inf}`, ``}, {"sf3 = -inf # negative infinity", `{"sf3": -Inf}`, ``}, {"sf4 = nan # actual sNaN/qNaN encoding is implementation-specific", `{"sf4": NaN}`, ``}, {"sf5 = +nan # same as `nan`", `{"sf5": NaN}`, ``}, {"sf6 = -nan # valid, actual encoding is implementation-specific", `{"sf6": NaN}`, ``}, {"bool1 = true", `{"bool1": true}`, ``}, {"bool2 = false", `{"bool2": false}`, ``}, {"odt1 = 1979-05-27T07:32:00Z", `{"odt1": 1979-05-27T07:32:00Z}`, ``}, {"odt2 = 1979-05-27T00:32:00-07:00", `{"odt2": 1979-05-27T00:32:00-07:00}`, ``}, {"odt3 = 1979-05-27T00:32:00.999999-07:00", `{"odt3": 1979-05-27T00:32:00.999999-07:00}`, ``}, {"odt4 = 1979-05-27 07:32:00Z", `{"odt4": 1979-05-27T07:32:00Z}`, ``}, {"ldt1 = 1979-05-27T07:32:00", `{"ldt1": 1979-05-27 07:32:00}`, ``}, {"ldt2 = 1979-05-27T00:32:00.999999", `{"ldt2": 1979-05-27 00:32:00.999999}`, ``}, {"ld1 = 1979-05-27", `{"ld1": 1979-05-27}`, ``}, {"lt1 = 07:32:00", `{"lt1": 07:32:00}`, ``}, {"lt2 = 00:32:00.999999", `{"lt2": 00:32:00.999999}`, ``}, {"arr1 = [ 1, 2, 3 ]", `{"arr1": [1, 2, 3]}`, ``}, {`arr2 = [ "red", "yellow", "green" ]`, `{"arr2": ["red", "yellow", "green"]}`, ``}, {`arr3 = [ [ 1, 2 ], [3, 4, 5] ]`, `{"arr3": [[1, 2], [3, 4, 5]]}`, ``}, {`arr4 = [ "all", 'strings', """are the same""", '''type''']`, `{"arr4": ["all", "strings", "are the same", "type"]}`, ``}, {`arr5 = [ [ 1, 2 ], ["a", "b", "c"] ]`, `{"arr5": [[1, 2], ["a", "b", "c"]]}`, ``}, {`arr6 = [ 1, 2.0 ] # INVALID`, `{}`, `type mismatch in array of integers: found an item of type float at line 1, column 16`}, {"arr7 = [\n 1, 2, 3\n]", `{"arr7": [1, 2, 3]}`, ``}, {"arr8 = [\n 1,\n 2, # this is ok\n]", `{"arr8": [1, 2]}`, ``}, } { p := newParser() testParseToAST(t, p, p.startKeyValuePair, test) } }