1
1
package objx
2
2
3
3
import (
4
- "github.com/stretchr/testify/assert"
5
4
"testing"
5
+
6
+ "github.com/stretchr/testify/assert"
6
7
)
7
8
8
9
func TestAccessorsAccessGetSingleField (t * testing.T ) {
9
-
10
10
current := map [string ]interface {}{"name" : "Tyler" }
11
- assert .Equal (t , "Tyler" , access (current , "name" , nil , false , true ))
12
11
12
+ assert .Equal (t , "Tyler" , access (current , "name" , nil , false , true ))
13
13
}
14
- func TestAccessorsAccessGetDeep (t * testing.T ) {
15
14
15
+ func TestAccessorsAccessGetDeep (t * testing.T ) {
16
16
current := map [string ]interface {}{"name" : map [string ]interface {}{"first" : "Tyler" , "last" : "Bunnell" }}
17
+
17
18
assert .Equal (t , "Tyler" , access (current , "name.first" , nil , false , true ))
18
19
assert .Equal (t , "Bunnell" , access (current , "name.last" , nil , false , true ))
19
-
20
20
}
21
- func TestAccessorsAccessGetDeepDeep (t * testing.T ) {
22
21
22
+ func TestAccessorsAccessGetDeepDeep (t * testing.T ) {
23
23
current := map [string ]interface {}{"one" : map [string ]interface {}{"two" : map [string ]interface {}{"three" : map [string ]interface {}{"four" : 4 }}}}
24
- assert .Equal (t , 4 , access (current , "one.two.three.four" , nil , false , true ))
25
24
25
+ assert .Equal (t , 4 , access (current , "one.two.three.four" , nil , false , true ))
26
26
}
27
- func TestAccessorsAccessGetInsideArray (t * testing.T ) {
28
27
28
+ func TestAccessorsAccessGetInsideArray (t * testing.T ) {
29
29
current := map [string ]interface {}{"names" : []interface {}{map [string ]interface {}{"first" : "Tyler" , "last" : "Bunnell" }, map [string ]interface {}{"first" : "Capitol" , "last" : "Bollocks" }}}
30
+
30
31
assert .Equal (t , "Tyler" , access (current , "names[0].first" , nil , false , true ))
31
32
assert .Equal (t , "Bunnell" , access (current , "names[0].last" , nil , false , true ))
32
33
assert .Equal (t , "Capitol" , access (current , "names[1].first" , nil , false , true ))
33
34
assert .Equal (t , "Bollocks" , access (current , "names[1].last" , nil , false , true ))
34
-
35
35
assert .Panics (t , func () {
36
36
access (current , "names[2]" , nil , false , true )
37
37
})
38
38
assert .Nil (t , access (current , "names[2]" , nil , false , false ))
39
-
40
39
}
41
40
42
41
func TestAccessorsAccessGetFromArrayWithInt (t * testing.T ) {
43
-
44
42
current := []interface {}{map [string ]interface {}{"first" : "Tyler" , "last" : "Bunnell" }, map [string ]interface {}{"first" : "Capitol" , "last" : "Bollocks" }}
45
43
one := access (current , 0 , nil , false , false )
46
44
two := access (current , 1 , nil , false , false )
@@ -49,66 +47,59 @@ func TestAccessorsAccessGetFromArrayWithInt(t *testing.T) {
49
47
assert .Equal (t , "Tyler" , one .(map [string ]interface {})["first" ])
50
48
assert .Equal (t , "Capitol" , two .(map [string ]interface {})["first" ])
51
49
assert .Nil (t , three )
52
-
53
50
}
54
51
55
52
func TestAccessorsGet (t * testing.T ) {
56
-
57
53
current := New (map [string ]interface {}{"name" : "Tyler" })
58
- assert .Equal (t , "Tyler" , current .Get ("name" ).data )
59
54
55
+ assert .Equal (t , "Tyler" , current .Get ("name" ).data )
60
56
}
61
57
62
58
func TestAccessorsAccessSetSingleField (t * testing.T ) {
63
-
64
59
current := map [string ]interface {}{"name" : "Tyler" }
65
- access (current , "name" , "Mat" , true , false )
66
- assert .Equal (t , current ["name" ], "Mat" )
67
60
61
+ access (current , "name" , "Mat" , true , false )
68
62
access (current , "age" , 29 , true , true )
69
- assert .Equal (t , current ["age" ], 29 )
70
63
64
+ assert .Equal (t , current ["name" ], "Mat" )
65
+ assert .Equal (t , current ["age" ], 29 )
71
66
}
72
67
73
68
func TestAccessorsAccessSetSingleFieldNotExisting (t * testing.T ) {
74
-
75
69
current := map [string ]interface {}{}
70
+
76
71
access (current , "name" , "Mat" , true , false )
77
- assert .Equal (t , current ["name" ], "Mat" )
78
72
73
+ assert .Equal (t , current ["name" ], "Mat" )
79
74
}
80
75
81
76
func TestAccessorsAccessSetDeep (t * testing.T ) {
82
-
83
77
current := map [string ]interface {}{"name" : map [string ]interface {}{"first" : "Tyler" , "last" : "Bunnell" }}
84
78
85
79
access (current , "name.first" , "Mat" , true , true )
86
80
access (current , "name.last" , "Ryer" , true , true )
87
81
88
82
assert .Equal (t , "Mat" , access (current , "name.first" , nil , false , true ))
89
83
assert .Equal (t , "Ryer" , access (current , "name.last" , nil , false , true ))
90
-
91
84
}
92
- func TestAccessorsAccessSetDeepDeep (t * testing.T ) {
93
85
86
+ func TestAccessorsAccessSetDeepDeep (t * testing.T ) {
94
87
current := map [string ]interface {}{"one" : map [string ]interface {}{"two" : map [string ]interface {}{"three" : map [string ]interface {}{"four" : 4 }}}}
95
88
96
89
access (current , "one.two.three.four" , 5 , true , true )
97
90
98
91
assert .Equal (t , 5 , access (current , "one.two.three.four" , nil , false , true ))
99
-
100
92
}
101
- func TestAccessorsAccessSetArray (t * testing.T ) {
102
93
94
+ func TestAccessorsAccessSetArray (t * testing.T ) {
103
95
current := map [string ]interface {}{"names" : []interface {}{"Tyler" }}
104
96
105
97
access (current , "names[0]" , "Mat" , true , true )
106
98
107
99
assert .Equal (t , "Mat" , access (current , "names[0]" , nil , false , true ))
108
-
109
100
}
110
- func TestAccessorsAccessSetInsideArray (t * testing.T ) {
111
101
102
+ func TestAccessorsAccessSetInsideArray (t * testing.T ) {
112
103
current := map [string ]interface {}{"names" : []interface {}{map [string ]interface {}{"first" : "Tyler" , "last" : "Bunnell" }, map [string ]interface {}{"first" : "Capitol" , "last" : "Bollocks" }}}
113
104
114
105
access (current , "names[0].first" , "Mat" , true , true )
@@ -120,11 +111,9 @@ func TestAccessorsAccessSetInsideArray(t *testing.T) {
120
111
assert .Equal (t , "Ryer" , access (current , "names[0].last" , nil , false , true ))
121
112
assert .Equal (t , "Captain" , access (current , "names[1].first" , nil , false , true ))
122
113
assert .Equal (t , "Underpants" , access (current , "names[1].last" , nil , false , true ))
123
-
124
114
}
125
115
126
116
func TestAccessorsAccessSetFromArrayWithInt (t * testing.T ) {
127
-
128
117
current := []interface {}{map [string ]interface {}{"first" : "Tyler" , "last" : "Bunnell" }, map [string ]interface {}{"first" : "Capitol" , "last" : "Bollocks" }}
129
118
one := access (current , 0 , nil , false , false )
130
119
two := access (current , 1 , nil , false , false )
@@ -133,13 +122,12 @@ func TestAccessorsAccessSetFromArrayWithInt(t *testing.T) {
133
122
assert .Equal (t , "Tyler" , one .(map [string ]interface {})["first" ])
134
123
assert .Equal (t , "Capitol" , two .(map [string ]interface {})["first" ])
135
124
assert .Nil (t , three )
136
-
137
125
}
138
126
139
127
func TestAccessorsSet (t * testing.T ) {
140
-
141
128
current := New (map [string ]interface {}{"name" : "Tyler" })
129
+
142
130
current .Set ("name" , "Mat" )
143
- assert .Equal (t , "Mat" , current .Get ("name" ).data )
144
131
132
+ assert .Equal (t , "Mat" , current .Get ("name" ).data )
145
133
}
0 commit comments