OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / pelletier / go-toml / keysparsing_test.go
1 package toml
2
3 import (
4         "fmt"
5         "testing"
6 )
7
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)
11         if err != nil {
12                 t.Fatal("Unexpected error:", err)
13         }
14         if len(expected) != len(parsed) {
15                 t.Fatal("Expected length", len(expected), "but", len(parsed), "parsed")
16         }
17         for index, expectedKey := range expected {
18                 if expectedKey != parsed[index] {
19                         t.Fatal("Expected", expectedKey, "at index", index, "but found", parsed[index])
20                 }
21         }
22 }
23
24 func testError(t *testing.T, key string, expectedError string) {
25         res, err := parseKey(key)
26         if err == nil {
27                 t.Fatalf("Expected error, but succesfully parsed key %s", res)
28         }
29         if fmt.Sprintf("%s", err) != expectedError {
30                 t.Fatalf("Expected error \"%s\", but got \"%s\".", expectedError, err)
31         }
32 }
33
34 func TestBareKeyBasic(t *testing.T) {
35         testResult(t, "test", []string{"test"})
36 }
37
38 func TestBareKeyDotted(t *testing.T) {
39         testResult(t, "this.is.a.key", []string{"this", "is", "a", "key"})
40 }
41
42 func TestDottedKeyBasic(t *testing.T) {
43         testResult(t, "\"a.dotted.key\"", []string{"a.dotted.key"})
44 }
45
46 func TestBaseKeyPound(t *testing.T) {
47         testError(t, "hello#world", "invalid bare character: #")
48 }
49
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"})
56
57         testResult(t, `hello.'foo'.bar`, []string{"hello", "foo", "bar"})
58         testResult(t, `'hello!'`, []string{"hello!"})
59         testResult(t, `'hello\tworld'`, []string{`hello\tworld`})
60
61         testError(t, `"\w"`, `invalid escape sequence \w`)
62         testError(t, `"\`, `unfinished escape sequence`)
63         testError(t, `"\t`, `mismatched quotes`)
64 }
65
66 func TestEmptyKey(t *testing.T) {
67         testError(t, "", "empty key")
68         testError(t, " ", "empty key")
69         testResult(t, `""`, []string{""})
70 }