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