-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils_test.go
263 lines (202 loc) · 7.48 KB
/
utils_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package abstractions
import (
"errors"
"strconv"
"testing"
"github.com/microsoft/kiota-abstractions-go/internal"
"github.com/microsoft/kiota-abstractions-go/serialization"
"github.com/stretchr/testify/assert"
)
func TestSetValueWithoutError(t *testing.T) {
person := internal.NewPerson()
callFactory := func() (*internal.CallRecord, error) {
return internal.NewCallRecord(), nil
}
err := SetValue(callFactory, person.SetCallRecord)
assert.Nil(t, err)
assert.NotNil(t, person.GetCallRecord())
}
func TestSetValueWithError(t *testing.T) {
person := internal.NewPerson()
callFactory := func() (*internal.CallRecord, error) {
return nil, errors.New("could not get from factory")
}
err := SetValue(callFactory, person.SetCallRecord)
assert.NotNil(t, err)
assert.Nil(t, person.GetCallRecord())
}
func createCallRecordNode(parseNode serialization.ParseNode) (serialization.Parsable, error) {
return internal.NewCallRecord(), nil
}
func getObjectsValues(ctor serialization.ParsableFactory) ([]serialization.Parsable, error) {
slice := []serialization.Parsable{internal.NewCallRecord(), internal.NewCallRecord(), internal.NewCallRecord()}
return slice, nil
}
func getObjectsValuesWithError(ctor serialization.ParsableFactory) ([]serialization.Parsable, error) {
return nil, errors.New("could not get from factory")
}
func TestSetCollectionValueValueWithoutError(t *testing.T) {
person := internal.NewPerson()
err := SetCollectionValue(getObjectsValues, createCallRecordNode, person.SetCallRecords)
assert.Nil(t, err)
assert.NotNil(t, person.GetCallRecords())
assert.Equal(t, len(person.GetCallRecords()), 3)
}
func TestSetCollectionValueValueWithError(t *testing.T) {
person := internal.NewPerson()
err := SetCollectionValue(getObjectsValuesWithError, createCallRecordNode, person.SetCallRecords)
assert.NotNil(t, err)
assert.Nil(t, person.GetCallRecords())
assert.Equal(t, len(person.GetCallRecords()), 0)
}
func TestCollectionApply(t *testing.T) {
slice := []string{"1", "2", "3"}
response := CollectionApply(slice, func(s string) int {
i, _ := strconv.Atoi(s)
return i
})
assert.Equal(t, len(response), 3)
assert.Equal(t, response, []int{1, 2, 3})
}
func TestCollectionCast1(t *testing.T) {
slice := []*int{p(1), p(2), p(3)}
response := CollectionValueCast[int](slice)
assert.Equal(t, len(response), 3)
assert.Equal(t, response, []int{1, 2, 3})
}
func TestCollectionCast(t *testing.T) {
var val1 interface{}
val1 = 1
var val2 interface{}
val2 = 2
var val3 interface{}
val3 = 3
slice := []interface{}{val1, val2, val3}
response := CollectionCast[int](slice)
assert.Equal(t, len(response), 3)
assert.Equal(t, response, []int{1, 2, 3})
}
func TestCollectionCastAsParsable(t *testing.T) {
slice := []interface{}{&internal.CallRecord{}, &internal.CallRecord{}, &internal.CallRecord{}}
response := CollectionCast[serialization.Parsable](slice)
assert.Equal(t, len(response), 3)
}
func getEnumValue(parser serialization.EnumFactory) (interface{}, error) {
status := internal.ACTIVE
return &status, nil
}
func getEnumValueWithError(parser serialization.EnumFactory) (interface{}, error) {
return nil, errors.New("could not get from factory")
}
func TestSetReferencedEnumValueValueValueWithoutError(t *testing.T) {
person := internal.NewPerson()
err := SetReferencedEnumValue(getEnumValue, internal.ParsePersonStatus, person.SetStatus)
assert.Nil(t, err)
assert.Equal(t, person.GetStatus().String(), internal.ACTIVE.String())
}
func TestSetReferencedEnumValueValueValueWithError(t *testing.T) {
person := internal.NewPerson()
err := SetReferencedEnumValue(getEnumValueWithError, internal.ParsePersonStatus, person.SetStatus)
assert.NotNil(t, err)
assert.Nil(t, person.GetStatus())
}
func TestSetCollectionOfReferencedEnumValueWithoutError(t *testing.T) {
person := internal.NewPerson()
slice := []interface{}{p(internal.ACTIVE), p(internal.SUSPENDED)}
enumSource := func(parser serialization.EnumFactory) ([]interface{}, error) {
return slice, nil
}
err := SetCollectionOfReferencedEnumValue(enumSource, internal.ParsePersonStatus, person.SetPreviousStatus)
assert.Nil(t, err)
assert.Equal(t, person.GetPreviousStatus()[0].String(), internal.ACTIVE.String())
assert.Equal(t, person.GetPreviousStatus()[1].String(), internal.SUSPENDED.String())
}
func TestSetCollectionOfReferencedEnumValueWithError(t *testing.T) {
person := internal.NewPerson()
enumSource := func(parser serialization.EnumFactory) ([]interface{}, error) {
return nil, errors.New("could not get from factory")
}
err := SetCollectionOfReferencedEnumValue(enumSource, internal.ParsePersonStatus, person.SetPreviousStatus)
assert.NotNil(t, err)
assert.Nil(t, person.GetPreviousStatus())
}
func TestSetCollectionOfReferencedPrimitiveValueWithoutError(t *testing.T) {
person := internal.NewPerson()
slice := []interface{}{p(1), p(2), p(3)}
dataSource := func(targetType string) ([]interface{}, error) {
return slice, nil
}
err := SetCollectionOfReferencedPrimitiveValue(dataSource, "int", person.SetCardNumbers)
assert.Nil(t, err)
assert.Equal(t, person.GetCardNumbers()[0], 1)
assert.Equal(t, person.GetCardNumbers()[1], 2)
assert.Equal(t, person.GetCardNumbers()[2], 3)
}
func TestSetCollectionOfReferencedPrimitiveValueWithError(t *testing.T) {
person := internal.NewPerson()
dataSource := func(targetType string) ([]interface{}, error) {
return nil, errors.New("could not get from factory")
}
err := SetCollectionOfReferencedPrimitiveValue(dataSource, "int", person.SetCardNumbers)
assert.NotNil(t, err)
assert.Nil(t, person.GetCardNumbers())
}
func TestSetCollectionOfPrimitiveValueWithoutError(t *testing.T) {
person := internal.NewPerson()
slice := []interface{}{1, 2, 3}
dataSource := func(targetType string) ([]interface{}, error) {
return slice, nil
}
err := SetCollectionOfPrimitiveValue(dataSource, "int", person.SetCardNumbers)
assert.Nil(t, err)
assert.Equal(t, person.GetCardNumbers()[0], 1)
assert.Equal(t, person.GetCardNumbers()[1], 2)
assert.Equal(t, person.GetCardNumbers()[1], 2)
assert.Equal(t, person.GetCardNumbers()[2], 3)
}
func TestSetCollectionOfPrimitiveValueWithError(t *testing.T) {
person := internal.NewPerson()
dataSource := func(targetType string) ([]interface{}, error) {
return nil, errors.New("could not get from factory")
}
err := SetCollectionOfPrimitiveValue(dataSource, "int", person.SetCardNumbers)
assert.NotNil(t, err)
assert.Nil(t, person.GetCardNumbers())
}
func TestGetValueReturn(t *testing.T) {
person := internal.NewPerson()
assert.Equal(t, GetValueOrDefault(person.GetDisplayName, "Unknown"), "Unknown")
person.SetDisplayName(p("Jane"))
assert.Equal(t, GetValueOrDefault(person.GetDisplayName, "Unknown"), "Jane")
}
type foo struct {
Name string
}
type animal interface {
Eat()
Call() string
}
type dog interface {
Bark()
Call() string
}
func (f *foo) Bark() {}
func (f *foo) Eat() {}
func (f *foo) Call() string { return f.Name }
func TestCollectionStructCast(t *testing.T) {
foos := []foo{{Name: "Cooper"}, {Name: "Buddy"}, {Name: "Peanut"}}
animals := CollectionStructCast[animal](foos)
assert.Equal(t, "Cooper", animals[0].Call())
assert.Equal(t, "Buddy", animals[1].Call())
assert.Equal(t, "Peanut", animals[2].Call())
}
func TestCopyMap(t *testing.T) {
source := map[string]int{"foo": 1, "bar": 2}
duplicate := CopyMap(source)
assert.Equal(t, duplicate, source)
}
func TestStringCopyMap(t *testing.T) {
source := map[string]string{"foo": "1", "bar": "2"}
duplicate := CopyStringMap(source)
assert.Equal(t, duplicate, source)
}