9 type customString string
11 type stringer struct{}
13 func (s stringer) String() string {
17 func validate(t *testing.T, path string, object interface{}) {
18 switch o := object.(type) {
20 for key, tree := range o.values {
21 validate(t, path+"."+key, tree)
24 for index, tree := range o {
25 validate(t, path+"."+strconv.Itoa(index), tree)
28 switch o.value.(type) {
29 case int64, uint64, bool, string, float64, time.Time,
30 []int64, []uint64, []bool, []string, []float64, []time.Time:
32 t.Fatalf("tomlValue at key %s containing incorrect type %T", path, o.value)
35 t.Fatalf("value at key %s is of incorrect type %T", path, object)
37 t.Logf("validation ok %s as %T", path, object)
40 func validateTree(t *testing.T, tree *Tree) {
44 func TestTreeCreateToTree(t *testing.T) {
45 data := map[string]interface{}{
55 "float32": float32(2),
57 "stringer": stringer{},
58 "nested": map[string]interface{}{
61 "array": []string{"a", "b", "c"},
62 "array_uint": []uint{uint(1), uint(2)},
63 "array_table": []map[string]interface{}{map[string]interface{}{"sub_map": 52}},
64 "array_times": []time.Time{time.Now(), time.Now()},
65 "map_times": map[string]time.Time{"now": time.Now()},
66 "custom_string_map_key": map[customString]interface{}{customString("custom"): "custom"},
68 tree, err := TreeFromMap(data)
70 t.Fatal("unexpected error:", err)
75 func TestTreeCreateToTreeInvalidLeafType(t *testing.T) {
76 _, err := TreeFromMap(map[string]interface{}{"foo": t})
77 expected := "cannot convert type *testing.T to Tree"
78 if err.Error() != expected {
79 t.Fatalf("expected error %s, got %s", expected, err.Error())
83 func TestTreeCreateToTreeInvalidMapKeyType(t *testing.T) {
84 _, err := TreeFromMap(map[string]interface{}{"foo": map[int]interface{}{2: 1}})
85 expected := "map key needs to be a string, not int (int)"
86 if err.Error() != expected {
87 t.Fatalf("expected error %s, got %s", expected, err.Error())
91 func TestTreeCreateToTreeInvalidArrayMemberType(t *testing.T) {
92 _, err := TreeFromMap(map[string]interface{}{"foo": []*testing.T{t}})
93 expected := "cannot convert type *testing.T to Tree"
94 if err.Error() != expected {
95 t.Fatalf("expected error %s, got %s", expected, err.Error())
99 func TestTreeCreateToTreeInvalidTableGroupType(t *testing.T) {
100 _, err := TreeFromMap(map[string]interface{}{"foo": []map[string]interface{}{map[string]interface{}{"hello": t}}})
101 expected := "cannot convert type *testing.T to Tree"
102 if err.Error() != expected {
103 t.Fatalf("expected error %s, got %s", expected, err.Error())
107 func TestRoundTripArrayOfTables(t *testing.T) {
108 orig := "\n[[stuff]]\n name = \"foo\"\n things = [\"a\",\"b\"]\n"
109 tree, err := Load(orig)
111 t.Fatalf("unexpected error: %s", err)
116 tree, err = TreeFromMap(m)
118 t.Fatalf("unexpected error: %s", err)
124 t.Errorf("want:\n%s\ngot:\n%s", want, got)