-
Notifications
You must be signed in to change notification settings - Fork 2
/
group_test.go
146 lines (142 loc) · 2.65 KB
/
group_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package m2obj
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestObject_GroupForeach(t *testing.T) {
var (
grp1 *Object
grp2 *Object
grp3 map[string]string
)
grp1 = New(Group{
"1": "1",
"2": "2",
"3": "3",
})
grp2 = New(Group{
"1": "1",
"2": "2",
"3": 3,
})
grp3 = make(map[string]string)
// no error and no panic
assert.NotPanics(t, func() {
assert.NoError(t, grp1.GroupForeach(func(key string, obj *Object) error {
grp3[key] = obj.ValStr()
return nil
}))
})
assert.Equal(t, map[string]string{
"1": "1",
"2": "2",
"3": "3",
}, grp3)
grp3 = make(map[string]string)
// transform the panic to error
assert.NotPanics(t, func() {
assert.Error(t, grp2.GroupForeach(func(key string, obj *Object) error {
switch obj.Val().(type) {
case string:
grp3[key] = obj.ValStr()
return nil
default:
return invalidTypeErr("")
}
}))
})
grp3 = make(map[string]string)
}
func groupMergeInit() (grp1, grp2 *Object) {
grp1 = New(Group{
"a": "a1",
"b": "b1",
"c": true,
"grp": Group{
"aa": "aa1",
"cc": "cc1",
"grp_inner": Group{
"aaa": "aaa1",
},
},
"grp2": Group{
"dd": "dd1",
},
"arr": Array{1, 2, 3},
"dynamic": Group{
"1": 1,
},
})
grp2 = New(Group{
"a": "a2",
"b": "b2",
"c": false,
"d": "d2",
"grp": Group{
"aa": "aa2",
"bb": "bb2",
"grp_inner": Group{
"bbb": "bbb2",
},
},
"grp2": nil,
"arr": Array{4, 5, 6},
"dynamic": "dynamic",
})
return
}
func TestObject_GroupMerge(t *testing.T) {
var grp1, grp2 *Object
// error type
grp1, grp2 = groupMergeInit()
assert.Error(t, New(grp1).GroupMerge(New(nil), true))
assert.Error(t, New(nil).GroupMerge(New(grp2), true))
assert.Error(t, New(nil).GroupMerge(New(nil), true))
assert.NoError(t, New(grp1).GroupMerge(New(grp2), true))
// forced
grp1, grp2 = groupMergeInit()
assert.NoError(t, grp1.GroupMerge(grp2, true))
assert.Equal(t, New(Group{
"a": "a2",
"b": "b2",
"c": false,
"d": "d2",
"grp": Group{
"aa": "aa2",
"bb": "bb2",
"cc": "cc1",
"grp_inner": Group{
"aaa": "aaa1",
"bbb": "bbb2",
},
},
"grp2": nil,
"arr": Array{4, 5, 6},
"dynamic": "dynamic",
}).Staticize(), grp1.Staticize())
// unforced
grp1, grp2 = groupMergeInit()
assert.NoError(t, grp1.GroupMerge(grp2, false))
assert.Equal(t, New(Group{
"a": "a1",
"b": "b1",
"c": true,
"d": "d2",
"grp": Group{
"aa": "aa1",
"bb": "bb2",
"cc": "cc1",
"grp_inner": Group{
"aaa": "aaa1",
"bbb": "bbb2",
},
},
"grp2": Group{
"dd": "dd1",
},
"arr": Array{1, 2, 3},
"dynamic": Group{
"1": 1,
},
}).Staticize(), grp1.Staticize())
}