1package toml
2
3import (
4	"fmt"
5	"testing"
6)
7
8func 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
24func 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
34func TestBareKeyBasic(t *testing.T) {
35	testResult(t, "test", []string{"test"})
36}
37
38func TestBareKeyDotted(t *testing.T) {
39	testResult(t, "this.is.a.key", []string{"this", "is", "a", "key"})
40}
41
42func TestDottedKeyBasic(t *testing.T) {
43	testResult(t, "\"a.dotted.key\"", []string{"a.dotted.key"})
44}
45
46func TestBaseKeyPound(t *testing.T) {
47	testError(t, "hello#world", "invalid bare character: #")
48}
49
50func TestQuotedKeys(t *testing.T) {
51	testResult(t, `hello."foo".bar`, []string{"hello", "foo", "bar"})
52	testResult(t, `"hello!"`, []string{"hello!"})
53	testResult(t, `foo."ba.r".baz`, []string{"foo", "ba.r", "baz"})
54
55	// escape sequences must not be converted
56	testResult(t, `"hello\tworld"`, []string{`hello\tworld`})
57}
58
59func TestEmptyKey(t *testing.T) {
60	testError(t, "", "empty key")
61	testError(t, " ", "empty key")
62	testResult(t, `""`, []string{""})
63}
64