-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap_test.go
More file actions
202 lines (199 loc) · 3.89 KB
/
map_test.go
File metadata and controls
202 lines (199 loc) · 3.89 KB
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
package golden
import (
"fmt"
"reflect"
"testing"
)
func Test_replaceTransient(t *testing.T) {
type args struct {
original map[string]any
transientFields []TransientField
}
tests := []struct {
name string
args args
want map[string]any
}{
{
name: "map with transient int",
args: args{
original: map[string]any{
"$.a": "foo",
"$.b": 2,
},
transientFields: []TransientField{{Key: ".b"}},
},
want: map[string]any{
"$.a": "foo",
"$.b": 123,
},
},
{
name: "map with transient float",
args: args{
original: map[string]any{
"$.a": "foo",
"$.b": 1.2,
},
transientFields: []TransientField{{Key: ".b"}},
},
want: map[string]any{
"$.a": "foo",
"$.b": 0.123,
},
},
{
name: "map with transient time",
args: args{
original: map[string]any{
"$.a": "foo",
"$.b": "2023-05-04T19:52:53Z",
},
transientFields: []TransientField{{Key: ".b"}},
},
want: map[string]any{
"$.a": "foo",
"$.b": "2023-01-01T00:00:00Z",
},
},
{
name: "map with transient time duration",
args: args{
original: map[string]any{
"$.a": "foo",
"$.b": "666ms",
},
transientFields: []TransientField{{Key: ".b"}},
},
want: map[string]any{
"$.a": "foo",
"$.b": "123ms",
},
},
{
name: "map with transient string",
args: args{
original: map[string]any{
"$.a": "foo",
"$.b": "bar",
},
transientFields: []TransientField{{Key: ".b"}},
},
want: map[string]any{
"$.a": "foo",
"$.b": "text",
},
},
{
name: "map with transient bool",
args: args{
original: map[string]any{
"$.a": "foo",
"$.b": true,
},
transientFields: []TransientField{{Key: ".b"}},
},
want: map[string]any{
"$.a": "foo",
"$.b": true,
},
},
{
name: "map with array",
args: args{
original: map[string]any{
"$.a[0].b": "foo",
"$.a[0].c": 1.2,
"$.a[1].b": "bar",
"$.a[1].c": 3.4,
},
transientFields: []TransientField{
{Key: "$.a[].b", Replacement: "text"},
},
},
want: map[string]any{
"$.a[0].b": "text",
"$.a[0].c": 1.2,
"$.a[1].b": "text",
"$.a[1].c": 3.4,
},
},
{
name: "map with replaced parent key",
args: args{
original: map[string]any{
"$.a.b": "foo",
"$.a.c": 1.2,
},
transientFields: []TransientField{
{Key: ".a", Replacement: map[string]int{"foo": 123}},
},
},
want: map[string]any{
"$.a": map[string]int{
"foo": 123,
},
},
},
{
name: "map with multiple replaced parent keys",
args: args{
original: map[string]any{
"$.a[0].foo.b": 123,
"$.a[0].foo.c": 123,
"$.a[1].foo.b": 456,
"$.a[1].foo.c": 456,
},
transientFields: []TransientField{
{Key: ".a[].foo", Replacement: "replaced"},
},
},
want: map[string]any{
"$.a[0].foo": "replaced",
"$.a[1].foo": "replaced",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := replaceTransient("test/path/to-file.golden", tt.args.original, tt.args.transientFields...)
if err != nil {
t.Errorf("replaceTransient() error = %v", err)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("replaceTransient() = %v, want %v", got, tt.want)
}
})
}
}
func Test_round(t *testing.T) {
tests := []struct {
num float64
want float64
precision int
}{
{
num: 1.23456789,
want: 1.235,
precision: 3,
},
{
num: 1.234567891234567891234,
want: 1.23456789123456789123,
precision: 20,
},
{
num: 1.234567891234567891234,
want: 1,
precision: 0,
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("round %d", tt.precision), func(t *testing.T) {
if got := round(tt.num, tt.precision); got != tt.want {
t.Errorf("round() = %v, want %v", got, tt.want)
}
})
}
}