-
Notifications
You must be signed in to change notification settings - Fork 0
/
deep_test.go
205 lines (193 loc) · 6.32 KB
/
deep_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
package compare
import (
"fmt"
"math"
"regexp"
"testing"
"time"
)
func ExampleDeepEqualer_Equal_float() {
de := DeepEqualer{TolerantBasicEqualer{Float64Tolerance: 0.1}}
same, err := de.Equal([]float64{1.6, 3.8}, []float64{1.544, 3.89})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(same)
// Output: true
}
func ExampleDeepEqualer_Equal_string() {
de := DeepEqualer{TolerantBasicEqualer{
// ignore everything after first space
StringTransformer: SubstringDeleter{Regexp: regexp.MustCompile(" .*$")},
}}
same, err := de.Equal(
map[string]string{"greeting": "Hello Alice!"},
map[string]string{"greeting": "Hello Bob!"})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(same)
// Output: true
}
func TestDeepEqualer_Equal_exact(t *testing.T) {
// test cases and types copied more or less directly from https://golang.org/src/reflect/all_test.go
type testCase struct {
a interface{}
b interface{}
expected bool
err string
}
type Basic struct {
x int
y float32
}
type NotBasic Basic
type Unexported struct {
x chan int
}
type self struct{}
var fn1, fn2 func() // nil
fn3 := func() { fn1() } // not nil
ch1 := make(chan int)
ch2 := make(chan int)
tcs := []testCase{
// Equalities
{nil, nil, true, ""},
{true, true, true, ""},
{true, false, false, ""},
{1, 1, true, ""},
{int32(1), int32(1), true, ""},
{uint(1), uint(1), true, ""},
{uintptr(1), uintptr(1), true, ""},
{0.5, 0.5, true, ""},
{float32(0.5), float32(0.5), true, ""},
{2i, 2i, true, ""},
{"hello", "hello", true, ""},
{make([]int, 10), make([]int, 10), true, ""},
{&[3]int{1, 2, 3}, &[3]int{1, 2, 3}, true, ""},
{Basic{1, 0.5}, Basic{1, 0.5}, true, ""},
{error(nil), error(nil), true, ""},
{map[int]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, true, ""},
{map[int]*bool{1: nil}, map[int]*bool{1: nil}, true, ""},
{ch1, ch1, true, ""},
{fn1, fn2, true, ""},
// Inequalities
{1, 2, false, ""},
{int32(1), int32(2), false, ""},
{uint(1), uint(2), false, ""},
{0.5, 0.6, false, ""},
{float32(0.5), float32(0.6), false, ""},
{1i, 2i, false, ""},
{"hello", "hey", false, ""},
{make([]int, 10), make([]int, 11), false, ""},
{&[3]int{1, 2, 3}, &[3]int{1, 2, 4}, false, ""},
{Basic{1, 0.5}, Basic{1, 0.6}, false, ""},
{Basic{1, 0}, Basic{2, 0}, false, ""},
{map[int]string{1: "one", 3: "two"}, map[int]string{2: "two", 1: "one"}, false, ""},
{map[int]string{1: "one", 2: "txo"}, map[int]string{2: "two", 1: "one"}, false, ""},
{map[int]string{1: "one"}, map[int]string{2: "two", 1: "one"}, false, ""},
{map[int]string{2: "two", 1: "one"}, map[int]string{1: "one"}, false, ""},
{nil, 1, false, ""},
{1, nil, false, ""},
{ch1, ch2, false, ""},
{fn1, fn3, false, ""},
{fn3, fn3, false, ""},
{[][]int{{1}}, [][]int{{2}}, false, ""},
{math.NaN(), math.NaN(), false, ""},
{&[1]float64{math.NaN()}, &[1]float64{math.NaN()}, false, ""},
{&[1]float64{math.NaN()}, self{}, true, ""},
{[]float64{math.NaN()}, []float64{math.NaN()}, false, ""},
{[]float64{math.NaN()}, self{}, true, ""},
{map[float64]float64{math.NaN(): 1}, map[float64]float64{1: 2}, false, ""},
{map[float64]float64{math.NaN(): 1}, self{}, true, ""},
// Nil vs empty vs zero: not the same.
{[]int{}, []int(nil), false, ""},
{[]int{}, []int{}, true, ""},
{[]int(nil), []int(nil), true, ""},
{map[int]int{}, map[int]int(nil), false, ""},
{map[int]int{}, map[int]int{}, true, ""},
{map[int]int(nil), map[int]int(nil), true, ""},
{[]interface{}{nil}, []interface{}{nil}, true, ""},
{[]interface{}{""}, []interface{}{nil}, false, ""},
{[]interface{}{}, []interface{}{nil}, false, ""},
// Mismatched types
{1, 1.0, false, ""},
{int32(1), int64(1), false, ""},
{0.5, "hello", false, ""},
{[]int{1, 2, 3}, [3]int{1, 2, 3}, false, ""},
{&[3]interface{}{1, 2, 4}, &[3]interface{}{1, 2, "s"}, false, ""},
{Basic{1, 0.5}, NotBasic{1, 0.5}, false, ""},
{map[uint]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, false, ""},
// Unsupported
{
Unexported{x: ch1},
Unexported{x: ch1},
false,
"reflect.Value.Interface: cannot return value obtained from unexported field or method",
},
}
// since we specify no tolerances, the equaler will compare values exactly
e := DeepEqualer{TolerantBasicEqualer{}}
for _, tc := range tcs {
if tc.b == (self{}) {
tc.b = tc.a
}
actual, err := e.Equal(tc.a, tc.b)
if err != nil {
if tc.err == "" {
t.Errorf("[%v == %v] %v", tc.a, tc.b, err)
} else if err.Error() != tc.err {
t.Errorf("[%v == %v] expected error %v; got %v", tc.a, tc.b, tc.err, err)
}
} else if tc.err != "" {
t.Errorf("[%v == %v] expected error %v; got nothing", tc.a, tc.b, tc.err)
} else if actual != tc.expected {
t.Errorf("[%v == %v] expected %v; got %v", tc.a, tc.b, tc.expected, actual)
}
}
}
func TestDeepEqualer_Equal_tolerant(t *testing.T) {
type testCase struct {
a interface{}
b interface{}
expected bool
}
tcs := []testCase{
{0.1, 0.151, false},
{[]float64{0.1, 0.1, 0.1, 0.1, 0.1}, []float64{0.05, 0.1, 0.10, 0.14, 0.15}, true},
// The following test fails because the tolerance is specified as a float64 value,
// but the values are specified as float32 values, which are less precise.
// For example, the float32 value 0.05 is actually 0.05000000074505806.
// TODO: specify separate tolerances for float32 and float64?
//{[]float32{0.05}, []float32{0.1}, true},
{"foo_1_1", "foo_2_1", false},
{[]string{"foo_1_1", "foo_1_1"}, []string{"foo_1_2", "foo_1_abc"}, true},
{"2018-03-30T16:41:11.509Z", "2018-03-30T16:41:12.510Z", false},
{
[]string{"2018-03-30T16:41:11.509Z", "2018-03-30T16:41:12.5Z"},
[]string{"2018-03-30T16:41:11.509Z", "2018-03-30T16:41:12.509Z"},
true,
},
}
tolerance, err := time.ParseDuration("1s")
if err != nil {
t.Fatal(err)
}
e := DeepEqualer{TolerantBasicEqualer{
Float64Tolerance: 0.05,
// ignore everything after last underscore
StringTransformer: SubstringDeleter{Regexp: regexp.MustCompile("_[^_]*$")},
TimeLayout: time.RFC3339Nano,
TimeTolerance: tolerance,
}}
for _, tc := range tcs {
actual, err := e.Equal(tc.a, tc.b)
if err != nil {
t.Errorf("[%v == %v] %v", tc.a, tc.b, err)
} else if actual != tc.expected {
t.Errorf("[%v == %v] expected %v; got %v", tc.a, tc.b, tc.expected, actual)
}
}
}