1
2
3 package toml
4
5 import (
6 "reflect"
7 "testing"
8 )
9
10 func TestTomlHas(t *testing.T) {
11 tree, _ := Load(`
12 [test]
13 key = "value"
14 `)
15
16 if !tree.Has("test.key") {
17 t.Errorf("Has - expected test.key to exists")
18 }
19
20 if tree.Has("") {
21 t.Errorf("Should return false if the key is not provided")
22 }
23 }
24
25 func TestTomlGet(t *testing.T) {
26 tree, _ := Load(`
27 [test]
28 key = "value"
29 `)
30
31 if tree.Get("") != tree {
32 t.Errorf("Get should return the tree itself when given an empty path")
33 }
34
35 if tree.Get("test.key") != "value" {
36 t.Errorf("Get should return the value")
37 }
38 if tree.Get(`\`) != nil {
39 t.Errorf("should return nil when the key is malformed")
40 }
41 }
42
43 func TestTomlGetArray(t *testing.T) {
44 tree, _ := Load(`
45 [test]
46 key = ["one", "two"]
47 key2 = [true, false, false]
48 key3 = [1.5,2.5]
49 `)
50
51 if tree.GetArray("") != tree {
52 t.Errorf("GetArray should return the tree itself when given an empty path")
53 }
54
55 expect := []string{"one", "two"}
56 actual := tree.GetArray("test.key").([]string)
57 if !reflect.DeepEqual(actual, expect) {
58 t.Errorf("GetArray should return the []string value")
59 }
60
61 expect2 := []bool{true, false, false}
62 actual2 := tree.GetArray("test.key2").([]bool)
63 if !reflect.DeepEqual(actual2, expect2) {
64 t.Errorf("GetArray should return the []bool value")
65 }
66
67 expect3 := []float64{1.5, 2.5}
68 actual3 := tree.GetArray("test.key3").([]float64)
69 if !reflect.DeepEqual(actual3, expect3) {
70 t.Errorf("GetArray should return the []float64 value")
71 }
72
73 if tree.GetArray(`\`) != nil {
74 t.Errorf("should return nil when the key is malformed")
75 }
76 }
77
78 func TestTomlGetDefault(t *testing.T) {
79 tree, _ := Load(`
80 [test]
81 key = "value"
82 `)
83
84 if tree.GetDefault("", "hello") != tree {
85 t.Error("GetDefault should return the tree itself when given an empty path")
86 }
87
88 if tree.GetDefault("test.key", "hello") != "value" {
89 t.Error("Get should return the value")
90 }
91
92 if tree.GetDefault("whatever", "hello") != "hello" {
93 t.Error("GetDefault should return the default value if the key does not exist")
94 }
95 }
96
97 func TestTomlHasPath(t *testing.T) {
98 tree, _ := Load(`
99 [test]
100 key = "value"
101 `)
102
103 if !tree.HasPath([]string{"test", "key"}) {
104 t.Errorf("HasPath - expected test.key to exists")
105 }
106 }
107
108 func TestTomlDelete(t *testing.T) {
109 tree, _ := Load(`
110 key = "value"
111 `)
112 err := tree.Delete("key")
113 if err != nil {
114 t.Errorf("Delete - unexpected error while deleting key: %s", err.Error())
115 }
116
117 if tree.Get("key") != nil {
118 t.Errorf("Delete should have removed key but did not.")
119 }
120
121 }
122
123 func TestTomlDeleteUnparsableKey(t *testing.T) {
124 tree, _ := Load(`
125 key = "value"
126 `)
127 err := tree.Delete(".")
128 if err == nil {
129 t.Errorf("Delete should error")
130 }
131 }
132
133 func TestTomlDeleteNestedKey(t *testing.T) {
134 tree, _ := Load(`
135 [foo]
136 [foo.bar]
137 key = "value"
138 `)
139 err := tree.Delete("foo.bar.key")
140 if err != nil {
141 t.Errorf("Error while deleting nested key: %s", err.Error())
142 }
143
144 if tree.Get("key") != nil {
145 t.Errorf("Delete should have removed nested key but did not.")
146 }
147
148 }
149
150 func TestTomlDeleteNonexistentNestedKey(t *testing.T) {
151 tree, _ := Load(`
152 [foo]
153 [foo.bar]
154 key = "value"
155 `)
156 err := tree.Delete("foo.not.there.key")
157 if err == nil {
158 t.Errorf("Delete should have thrown an error trying to delete key in nonexistent tree")
159 }
160 }
161
162 func TestTomlGetPath(t *testing.T) {
163 node := newTree()
164
165
166 for idx, item := range []struct {
167 Path []string
168 Expected *Tree
169 }{
170 {
171 []string{},
172 node,
173 },
174 } {
175 result := node.GetPath(item.Path)
176 if result != item.Expected {
177 t.Errorf("GetPath[%d] %v - expected %v, got %v instead.", idx, item.Path, item.Expected, result)
178 }
179 }
180
181 tree, _ := Load("[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6")
182 if tree.GetPath([]string{"whatever"}) != nil {
183 t.Error("GetPath should return nil when the key does not exist")
184 }
185 }
186
187 func TestTomlGetArrayPath(t *testing.T) {
188 for idx, item := range []struct {
189 Name string
190 Path []string
191 Make func() (tree *Tree, expected interface{})
192 }{
193 {
194 Name: "empty",
195 Path: []string{},
196 Make: func() (tree *Tree, expected interface{}) {
197 tree = newTree()
198 expected = tree
199 return
200 },
201 },
202 {
203 Name: "int64",
204 Path: []string{"a"},
205 Make: func() (tree *Tree, expected interface{}) {
206 var err error
207 tree, err = Load(`a = [1,2,3]`)
208 if err != nil {
209 panic(err)
210 }
211 expected = []int64{1, 2, 3}
212 return
213 },
214 },
215 } {
216 t.Run(item.Name, func(t *testing.T) {
217 tree, expected := item.Make()
218 result := tree.GetArrayPath(item.Path)
219 if !reflect.DeepEqual(result, expected) {
220 t.Errorf("GetArrayPath[%d] %v - expected %#v, got %#v instead.", idx, item.Path, expected, result)
221 }
222 })
223 }
224
225 tree, _ := Load("[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6")
226 if tree.GetArrayPath([]string{"whatever"}) != nil {
227 t.Error("GetArrayPath should return nil when the key does not exist")
228 }
229
230 }
231
232 func TestTomlFromMap(t *testing.T) {
233 simpleMap := map[string]interface{}{"hello": 42}
234 tree, err := TreeFromMap(simpleMap)
235 if err != nil {
236 t.Fatal("unexpected error:", err)
237 }
238 if tree.Get("hello") != int64(42) {
239 t.Fatal("hello should be 42, not", tree.Get("hello"))
240 }
241 }
242
243 func TestLoadBytesBOM(t *testing.T) {
244 payloads := [][]byte{
245 []byte("\xFE\xFFhello=1"),
246 []byte("\xFF\xFEhello=1"),
247 []byte("\xEF\xBB\xBFhello=1"),
248 []byte("\x00\x00\xFE\xFFhello=1"),
249 []byte("\xFF\xFE\x00\x00hello=1"),
250 }
251 for _, data := range payloads {
252 tree, err := LoadBytes(data)
253 if err != nil {
254 t.Fatal("unexpected error:", err, "for:", data)
255 }
256 v := tree.Get("hello")
257 if v != int64(1) {
258 t.Fatal("hello should be 1, not", v)
259 }
260 }
261 }
262
View as plain text