8 func testResult(t *testing.T, key string, expected []string) {
9 parsed, err := parseKey(key)
10 t.Logf("key=%s expected=%s parsed=%s", key, expected, parsed)
12 t.Fatal("Unexpected error:", err)
14 if len(expected) != len(parsed) {
15 t.Fatal("Expected length", len(expected), "but", len(parsed), "parsed")
17 for index, expectedKey := range expected {
18 if expectedKey != parsed[index] {
19 t.Fatal("Expected", expectedKey, "at index", index, "but found", parsed[index])
24 func testError(t *testing.T, key string, expectedError string) {
25 res, err := parseKey(key)
27 t.Fatalf("Expected error, but succesfully parsed key %s", res)
29 if fmt.Sprintf("%s", err) != expectedError {
30 t.Fatalf("Expected error \"%s\", but got \"%s\".", expectedError, err)
34 func TestBareKeyBasic(t *testing.T) {
35 testResult(t, "test", []string{"test"})
38 func TestBareKeyDotted(t *testing.T) {
39 testResult(t, "this.is.a.key", []string{"this", "is", "a", "key"})
42 func TestDottedKeyBasic(t *testing.T) {
43 testResult(t, "\"a.dotted.key\"", []string{"a.dotted.key"})
46 func TestBaseKeyPound(t *testing.T) {
47 testError(t, "hello#world", "invalid bare character: #")
50 func TestQuotedKeys(t *testing.T) {
51 testResult(t, `hello."foo".bar`, []string{"hello", "foo", "bar"})
52 testResult(t, `"hello!"`, []string{"hello!"})
53 testResult(t, `"hello\tworld"`, []string{"hello\tworld"})
54 testResult(t, `"\U0001F914"`, []string{"\U0001F914"})
55 testResult(t, `"\u2764"`, []string{"\u2764"})
57 testResult(t, `hello.'foo'.bar`, []string{"hello", "foo", "bar"})
58 testResult(t, `'hello!'`, []string{"hello!"})
59 testResult(t, `'hello\tworld'`, []string{`hello\tworld`})
61 testError(t, `"\w"`, `invalid escape sequence \w`)
62 testError(t, `"\`, `unfinished escape sequence`)
63 testError(t, `"\t`, `mismatched quotes`)
66 func TestEmptyKey(t *testing.T) {
67 testError(t, "", "empty key")
68 testError(t, " ", "empty key")
69 testResult(t, `""`, []string{""})