go-toml/keyvaluepair_test.go

78 lines
3.4 KiB
Go

package parser
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")`, `keydot()`, `Error: unexpected end of file (expected a key name) at line 1, column 6`}},
{"key1.key2", []string{`key("key1")`, `keydot()`, `key("key2")`}},
{"key . with . spaces", []string{`key("key")`, `keydot()`, `key("with")`, `keydot()`, `key("spaces")`}},
{"key \t . \twithtabs\t . \tandspaces", []string{`key("key")`, `keydot()`, `key("withtabs")`, `keydot()`, `key("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")`, `keydot()`, `key("i\\s")`, `keydot()`, `key("madness\t")`, `keydot()`, `key("")`}},
} {
p := &parser{}
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{`assign()`}},
{" \t = \t ", []string{`assign()`}},
{" \n = \n ", []string{`Error: unexpected input (expected a value assignment) at start of file`}},
} {
p := &parser{}
testParseHandler(t, p, p.startAssignment, test)
}
}
func TestValue(t *testing.T) {
for _, test := range []parseTest{
{``, []string{`Error: unexpected end of file (expected a value) at start of file`}},
{`"basic string value"`, []string{`string("basic string value")`}},
} {
p := &parser{}
testParseHandler(t, p, p.startValue, 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")`, `assign()`, `Error: unexpected end of file (expected a value) at line 1, column 8`}},
{" key \t =\t \"The Value\" \r\n", []string{`key("key")`, `assign()`, `string("The Value")`}},
{"key1=\"value1\"key2=\"value2\"\r\nkey3=\"value3\"", []string{
`key("key1")`, `assign()`, `string("value1")`,
`key("key2")`, `assign()`, `string("value2")`,
`key("key3")`, `assign()`, `string("value3")`}},
{"with=\"comments\"# boring \nanother.cool =\"one\" \t # to the end\r\n", []string{
`key("with")`, `assign()`, `string("comments")`, `comment("# boring ")`,
`key("another")`, `keydot()`, `key("cool")`, `assign()`, `string("one")`, `comment("# to the end")`}},
} {
p := &parser{}
testParseHandler(t, p, p.startKeyValuePair, test)
}
}