Skip to content

Commit 01d5674

Browse files
committed
Replace naive comparisons with reflect.DeepEqual
1 parent b4c4ec3 commit 01d5674

File tree

1 file changed

+2
-106
lines changed

1 file changed

+2
-106
lines changed

pkg/chewbyte/importer_test.go

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -6,86 +6,6 @@ import (
66
"testing"
77
)
88

9-
func compareStringSlices(d1, d2 []string) bool {
10-
if len(d1) != len(d2) {
11-
return false
12-
}
13-
14-
for i, v := range d1 {
15-
if v != d2[i] {
16-
return false
17-
}
18-
}
19-
20-
return true
21-
}
22-
23-
func compareStringKeyedMaps(m1, m2 map[string]interface{}) bool {
24-
if len(m1) != len(m2) {
25-
return false // unequal number of items in maps
26-
}
27-
28-
for k, v1 := range m1 {
29-
v2, ok := m2[k]
30-
if !ok {
31-
return false // key exists only in first map
32-
}
33-
if reflect.TypeOf(v1) != reflect.TypeOf(v2) {
34-
return false // values are of diffrent types
35-
}
36-
if v1 != v2 {
37-
return false // values differ
38-
}
39-
}
40-
41-
return true
42-
}
43-
44-
func TestCompareStringSlices(t *testing.T) {
45-
testTable := []struct {
46-
d1, d2 []string
47-
expected bool
48-
}{
49-
{[]string{}, []string{}, true},
50-
{[]string{"foo"}, []string{"foo"}, true},
51-
{[]string{"foo"}, []string{"bar"}, false},
52-
}
53-
54-
for _, testData := range testTable {
55-
testName := fmt.Sprintf("%s == %s = %t", testData.d1, testData.d2, testData.expected)
56-
t.Run(testName, func(t *testing.T) {
57-
output := compareStringSlices(testData.d1, testData.d2)
58-
if output != testData.expected {
59-
t.Errorf("%t != %t", output, testData.expected)
60-
}
61-
})
62-
}
63-
}
64-
65-
func TestCompareStringKeyedMaps(t *testing.T) {
66-
testTable := []struct {
67-
m1, m2 map[string]interface{}
68-
expected bool
69-
}{
70-
{map[string]interface{}{}, map[string]interface{}{}, true},
71-
{map[string]interface{}{"a": 1}, map[string]interface{}{"a": 1}, true},
72-
{map[string]interface{}{"a": 1}, map[string]interface{}{"b": 2}, false},
73-
{map[string]interface{}{"a": 1}, map[string]interface{}{"a": 2}, false},
74-
{map[string]interface{}{"a": 1}, map[string]interface{}{}, false},
75-
{map[string]interface{}{}, map[string]interface{}{"a": 1}, false},
76-
}
77-
78-
for _, testData := range testTable {
79-
testName := fmt.Sprintf("%s == %s = %t", testData.m1, testData.m2, testData.expected)
80-
t.Run(testName, func(t *testing.T) {
81-
output := compareStringKeyedMaps(testData.m1, testData.m2)
82-
if output != testData.expected {
83-
t.Errorf("%t != %t", output, testData.expected)
84-
}
85-
})
86-
}
87-
}
88-
899
func TestMerge(t *testing.T) {
9010
testTable := []struct {
9111
d1, d2 interface{}
@@ -116,32 +36,8 @@ func TestMerge(t *testing.T) {
11636
t.Fatalf("mergeData returned error %s", err)
11737
}
11838

119-
outputTypeStr := reflect.TypeOf(output)
120-
expectedTypeStr := reflect.TypeOf(testData.expected)
121-
122-
switch output.(type) {
123-
case []string:
124-
expectedSlice, expectedValid := testData.expected.([]string)
125-
if expectedValid {
126-
if !compareStringSlices(output.([]string), expectedSlice) {
127-
t.Errorf("%s != %s", output, testData.expected)
128-
}
129-
} else {
130-
t.Errorf("output type %s != expected type %s", outputTypeStr, expectedTypeStr)
131-
}
132-
case map[string]interface{}:
133-
expectedSlice, expectedValid := testData.expected.(map[string]interface{})
134-
if expectedValid {
135-
if !compareStringKeyedMaps(output.(map[string]interface{}), expectedSlice) {
136-
t.Errorf("%s != %s", output, testData.expected)
137-
}
138-
} else {
139-
t.Errorf("output type %s != expected type %s", outputTypeStr, expectedTypeStr)
140-
}
141-
default:
142-
if output != testData.expected {
143-
t.Errorf("%s != %s", output, testData.expected)
144-
}
39+
if !reflect.DeepEqual(output, testData.expected) {
40+
t.Errorf("%v != %v", output, testData.expected)
14541
}
14642
})
14743
}

0 commit comments

Comments
 (0)