forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
139 lines (133 loc) · 3.06 KB
/
helpers_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
package playwright
import (
"testing"
"github.com/stretchr/testify/require"
)
type testOptionsJSONSerialization struct {
StringPointer *string `json:"stringPointer"`
NormalString string `json:"normalString"`
WithoutJSONTag string
WithJSONTag string `json:"withJSONTag"`
SkipNilPtrs *string `json:"skipNilPtrs"`
SkipMe *int `json:"skipMe"`
}
func TestTransformOptions(t *testing.T) {
// test data
structVar := &testOptionsJSONSerialization{
StringPointer: String("1"),
NormalString: "2",
WithoutJSONTag: "3",
WithJSONTag: "4",
}
var nilStrPtr *string
testCases := []struct {
name string
baseMap map[string]interface{}
optionalStruct interface{}
expected interface{}
}{
{
name: "No options supplied",
baseMap: map[string]interface{}{
"1234": nilStrPtr,
"foo": "bar",
},
expected: map[string]interface{}{
"foo": "bar",
},
},
{
name: "Options are nil",
baseMap: map[string]interface{}{
"foo": "bar",
},
optionalStruct: nil,
expected: map[string]interface{}{
"foo": "bar",
},
},
{
name: "JSON serialization works",
baseMap: map[string]interface{}{
"foo": "bar",
},
optionalStruct: structVar,
expected: map[string]interface{}{
"foo": "bar",
"stringPointer": String("1"),
"normalString": "2",
"WithoutJSONTag": "3",
"withJSONTag": "4",
},
},
{
name: "Second overwrites the first one",
baseMap: map[string]interface{}{
"foo": "1",
},
optionalStruct: map[string]interface{}{
"foo": "2",
},
expected: map[string]interface{}{
"foo": "2",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expected, transformOptions(tc.baseMap, tc.optionalStruct))
})
}
}
func TestRemapMapToStruct(t *testing.T) {
ourStruct := struct {
V1 string `json:"v1"`
}{}
inMap := map[string]interface{}{
"v1": "foobar",
}
remapMapToStruct(inMap, &ourStruct)
require.Equal(t, ourStruct.V1, "foobar")
}
func TestConvertSelectOptionSet(t *testing.T) {
testCases := []struct {
name string
optionValues SelectOptionValues
expected interface{}
}{
{
name: "SelectOptionValues is nil",
optionValues: SelectOptionValues{},
expected: make(map[string]interface{}),
},
{
name: "SelectOptionValues is supplied",
optionValues: SelectOptionValues{
Values: StringSlice("a", "b"),
Indexes: IntSlice(1),
Labels: StringSlice("x"),
},
expected: map[string]interface{}{
"options": []map[string]interface{}{
{"value": "a"}, {"value": "b"}, {"index": 1}, {"label": "x"},
},
},
},
{
name: "Only value is supplied",
optionValues: SelectOptionValues{
Values: StringSlice("a", "b"),
},
expected: map[string]interface{}{
"options": []map[string]interface{}{
{"value": "a"}, {"value": "b"},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expected, convertSelectOptionSet(tc.optionValues))
})
}
}