1 package objx_test
2
3 import (
4 "testing"
5
6 "github.com/stretchr/objx"
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestAccessorsAccessGetSingleField(t *testing.T) {
11 m := objx.Map{"name": "Tyler"}
12
13 assert.Equal(t, "Tyler", m.Get("name").Data())
14 }
15
16 func TestAccessorsAccessGetSingleFieldInt(t *testing.T) {
17 m := objx.Map{"name": 10}
18
19 assert.Equal(t, 10, m.Get("name").Data())
20 }
21
22 func TestAccessorsAccessGetDeep(t *testing.T) {
23 m := objx.Map{
24 "name": objx.Map{
25 "first": "Tyler",
26 "last": "Bunnell",
27 "friends": []string{
28 "Capitol",
29 "Bollocks",
30 },
31 "ifriends": []interface{}{
32 "Capitol",
33 "Bollocks",
34 },
35 },
36 }
37
38 assert.Equal(t, "Tyler", m.Get("name.first").Data())
39 assert.Equal(t, "Bunnell", m.Get("name.last").Data())
40 assert.Equal(t, "Capitol", m.Get("name.friends[0]").Data())
41 assert.Equal(t, "Capitol", m.Get("name.ifriends[0]").Data())
42 }
43
44 func TestAccessorsAccessGetDeepDeep(t *testing.T) {
45 m := objx.Map{
46 "one": objx.Map{
47 "two": objx.Map{
48 "three": objx.Map{
49 "four": 4,
50 },
51 },
52 },
53 }
54
55 assert.Equal(t, 4, m.Get("one.two.three.four").Data())
56 assert.Equal(t, 4, m.Get("one[two][three][four]").Data())
57 }
58
59 func TestAccessorsGetWithComplexKey(t *testing.T) {
60 m := objx.Map{
61 "domains": objx.Map{
62 "example-dot-com": objx.Map{
63 "apex": "example",
64 },
65 "example.com": objx.Map{
66 "apex": "example",
67 },
68 },
69 }
70
71 assert.Equal(t, "example", m.Get("domains.example-dot-com.apex").Data())
72
73 assert.Equal(t, "example", m.Get("domains[example.com].apex").Data())
74 assert.Equal(t, "example", m.Get("domains[example.com][apex]").Data())
75 }
76
77 func TestAccessorsAccessGetInsideArray(t *testing.T) {
78 m := objx.Map{
79 "names": []interface{}{
80 objx.Map{
81 "first": "Tyler",
82 "last": "Bunnell",
83 },
84 objx.Map{
85 "first": "Capitol",
86 "last": "Bollocks",
87 },
88 },
89 }
90
91 assert.Equal(t, "Tyler", m.Get("names[0].first").Data())
92 assert.Equal(t, "Bunnell", m.Get("names[0].last").Data())
93 assert.Equal(t, "Capitol", m.Get("names[1].first").Data())
94 assert.Equal(t, "Bollocks", m.Get("names[1].last").Data())
95
96 assert.Nil(t, m.Get("names[2]").Data())
97 assert.Nil(t, m.Get("names[]").Data())
98 assert.Nil(t, m.Get("names1]]").Data())
99 assert.Nil(t, m.Get("names[1]]").Data())
100 assert.Nil(t, m.Get("names[[1]]").Data())
101 assert.Nil(t, m.Get("names[[1]").Data())
102 assert.Nil(t, m.Get("names[[1").Data())
103 }
104
105 func TestAccessorsGet(t *testing.T) {
106 m := objx.Map{"name": "Tyler"}
107
108 assert.Equal(t, "Tyler", m.Get("name").Data())
109 }
110
111 func TestAccessorsAccessSetSingleField(t *testing.T) {
112 m := objx.Map{"name": "Tyler"}
113
114 m.Set("name", "Mat")
115 m.Set("age", 29)
116
117 assert.Equal(t, m.Get("name").Data(), "Mat")
118 assert.Equal(t, m.Get("age").Data(), 29)
119 }
120
121 func TestAccessorsAccessSetSingleFieldNotExisting(t *testing.T) {
122 m := objx.Map{
123 "first": "Tyler",
124 "last": "Bunnell",
125 }
126
127 m.Set("name", "Mat")
128
129 assert.Equal(t, m.Get("name").Data(), "Mat")
130 }
131
132 func TestAccessorsAccessSetDeep(t *testing.T) {
133 m := objx.Map{
134 "name": objx.Map{
135 "first": "Tyler",
136 "last": "Bunnell",
137 },
138 }
139
140 m.Set("name.first", "Mat")
141 m.Set("name.last", "Ryer")
142
143 assert.Equal(t, "Mat", m.Get("name.first").Data())
144 assert.Equal(t, "Ryer", m.Get("name.last").Data())
145 }
146
147 func TestAccessorsAccessSetDeepDeep(t *testing.T) {
148 m := objx.Map{
149 "one": objx.Map{
150 "two": objx.Map{
151 "three": objx.Map{
152 "four": 4,
153 },
154 },
155 },
156 }
157
158 m.Set("one.two.three.four", 5)
159
160 assert.Equal(t, 5, m.Get("one.two.three.four").Data())
161 }
162
163 func TestAccessorsAccessSetDeepDeepWithoutExisting(t *testing.T) {
164 m := objx.Map{}
165
166 m.Set("one.two.three.four", 5)
167 m.Set("one.two.three.five", 6)
168
169 assert.Equal(t, 5, m.Get("one.two.three.four").Data())
170 assert.Equal(t, 6, m.Get("one.two.three.five").Data())
171
172 m.Set("one.two", 7)
173 assert.Equal(t, 7, m.Get("one.two").Data())
174 assert.Equal(t, nil, m.Get("one.two.three.four").Data())
175
176 m.Set("one.two.three", 8)
177 assert.Equal(t, 8, m.Get("one.two.three").Data())
178 }
179
180 func TestAccessorsAccessSetArray(t *testing.T) {
181 m := objx.Map{
182 "names": []interface{}{"Tyler"},
183 }
184 m.Set("names[0]", "Mat")
185
186 assert.Equal(t, "Mat", m.Get("names[0]").Data())
187 }
188
189 func TestAccessorsAccessSetInsideArray(t *testing.T) {
190 m := objx.Map{
191 "names": []interface{}{
192 objx.Map{
193 "first": "Tyler",
194 "last": "Bunnell",
195 },
196 objx.Map{
197 "first": "Capitol",
198 "last": "Bollocks",
199 },
200 },
201 }
202
203 m.Set("names[0].first", "Mat")
204 m.Set("names[0].last", "Ryer")
205 m.Set("names[1].first", "Captain")
206 m.Set("names[1].last", "Underpants")
207
208 assert.Equal(t, "Mat", m.Get("names[0].first").Data())
209 assert.Equal(t, "Ryer", m.Get("names[0].last").Data())
210 assert.Equal(t, "Captain", m.Get("names[1].first").Data())
211 assert.Equal(t, "Underpants", m.Get("names[1].last").Data())
212 }
213
214 func TestAccessorsSet(t *testing.T) {
215 m := objx.Map{"name": "Tyler"}
216
217 m.Set("name", "Mat")
218
219 assert.Equal(t, "Mat", m.Get("name").Data())
220 }
221
222 func TestAccessorsSetWithinObjxMapChild(t *testing.T) {
223 m, err := objx.FromJSON(`{"a": {"b": 1}}`)
224 assert.NoError(t, err)
225
226 m.Set("a.c", 2)
227 jsonConverted, err := m.JSON()
228 assert.NoError(t, err)
229
230 m = objx.New(map[string]interface{}{
231 "a": map[string]interface{}{
232 "b": 1,
233 },
234 })
235 m.Set("a.c", 2)
236 jsonNewObj, err := m.JSON()
237
238 assert.NoError(t, err)
239 assert.Equal(t, jsonConverted, jsonNewObj)
240 }
241
242 func TestAccessorsNested(t *testing.T) {
243 d := objx.MustFromJSON(`{"values":[["test", "test1"], ["test2", {"name":"Mat"}, {"names": ["Captain", "Mat"]}]]}`)
244
245 value := d.Get("values[0][0]").String()
246 assert.Equal(t, "test", value)
247
248 value = d.Get("values[0][1]").String()
249 assert.Equal(t, "test1", value)
250
251 value = d.Get("values[1][0]").String()
252 assert.Equal(t, "test2", value)
253
254 value = d.Get("values[1][1].name").String()
255 assert.Equal(t, "Mat", value)
256
257 value = d.Get("values[1][2].names[0]").String()
258 assert.Equal(t, "Captain", value)
259 }
260
View as plain text