@@ -28,82 +28,68 @@ func NotEqual[K comparable](t testing.TB, expected K, actual K) {
28
28
}
29
29
30
30
/*
31
- Asserts that the given value is nil
31
+ Asserts that the two given valus are deeply equal. Internally uses reflect.DeepEqual.
32
+ The equality of arrays, slices and maps can be asserted with this method
32
33
*/
33
- func Nil (t testing.TB , actual interface {} ) {
34
+ func DeepEqual [ T any ] (t testing.TB , expected T , actual T ) {
34
35
t .Helper ()
35
36
36
- if ! isNil ( actual ) {
37
- t .Error (inequalityMsg (nil , actual ))
37
+ if ! reflect . DeepEqual ( expected , actual ) {
38
+ t .Error (inequalityMsg (expected , actual ))
38
39
}
39
40
}
40
41
41
42
/*
42
- Asserts that the given value is not nil
43
+ Asserts that the two given valus are not deeply equal. Internally uses reflect.DeepEqual.
44
+ The inequality of arrays, slices and maps can be asserted with this method
43
45
*/
44
- func NotNil (t testing.TB , actual interface {} ) {
46
+ func NotDeepEqual [ T any ] (t testing.TB , expected T , actual T ) {
45
47
t .Helper ()
46
48
47
- if isNil (actual ) {
48
- t .Error (equalityMsg ("nil" ))
49
- }
50
- }
51
-
52
- func isNil (value interface {}) bool {
53
- if value == nil {
54
- return true
55
- }
56
-
57
- switch reflect .TypeOf (value ).Kind () {
58
- case reflect .Ptr , reflect .Map , reflect .Slice , reflect .Chan , reflect .Func :
59
- return reflect .ValueOf (value ).IsNil ()
49
+ if reflect .DeepEqual (expected , actual ) {
50
+ t .Error (equalityMsg (expected ))
60
51
}
61
-
62
- return false
63
52
}
64
53
65
54
/*
66
- Asserts that the two given slices have the same elements in the same order. The elements must be [comparable]
55
+ Asserts that the given value is nil
67
56
*/
68
- func EqualSlice [ K comparable ] (t testing.TB , expected [] K , actual [] K ) {
57
+ func Nil (t testing.TB , actual interface {} ) {
69
58
t .Helper ()
70
59
71
- if ! areEqualSlices ( expected , actual ) {
72
- t .Error (inequalityMsg (expected , actual ))
60
+ if ! isNil ( actual ) {
61
+ t .Error (inequalityMsg (nil , actual ))
73
62
}
74
63
}
75
64
76
65
/*
77
- Asserts that the two given slices does not have the same elements in the same order. The elements must be [comparable]
66
+ Asserts that the given value is not nil
78
67
*/
79
- func NotEqualSlice [ K comparable ] (t testing.TB , expected [] K , actual [] K ) {
68
+ func NotNil (t testing.TB , actual interface {} ) {
80
69
t .Helper ()
81
70
82
- if areEqualSlices ( expected , actual ) {
83
- t .Error (equalityMsg (expected ))
71
+ if isNil ( actual ) {
72
+ t .Error (equalityMsg ("nil" ))
84
73
}
85
74
}
86
75
87
- func areEqualSlices [K comparable ](expected []K , actual []K ) bool {
88
- expectedLength := len (expected )
89
- actualLength := len (actual )
90
- if actualLength != expectedLength {
91
- return false
76
+ func isNil (value interface {}) bool {
77
+ if value == nil {
78
+ return true
92
79
}
93
80
94
- for i := 0 ; i < expectedLength ; i ++ {
95
- if actual [i ] != expected [i ] {
96
- return false
97
- }
81
+ switch reflect .TypeOf (value ).Kind () {
82
+ case reflect .Pointer , reflect .Map , reflect .Slice , reflect .Chan , reflect .Func :
83
+ return reflect .ValueOf (value ).IsNil ()
98
84
}
99
85
100
- return true
86
+ return false
101
87
}
102
88
103
89
/*
104
90
Asserts that the two given slices have the same values in any order. The elements must be [comparable]
105
91
*/
106
- func SimilarSlice [K comparable ](t testing.TB , expected []K , actual []K ) {
92
+ func SimilarSlice [T any ](t testing.TB , expected []T , actual []T ) {
107
93
t .Helper ()
108
94
109
95
if ! areSimilarSlices (expected , actual ) {
@@ -114,69 +100,34 @@ func SimilarSlice[K comparable](t testing.TB, expected []K, actual []K) {
114
100
/*
115
101
Asserts that the two given slices does not have the same values. The elements must be [comparable]
116
102
*/
117
- func NotSimilarSlice [K comparable ](t testing.TB , expected []K , actual []K ) {
103
+ func NotSimilarSlice [T any ](t testing.TB , expected []T , actual []T ) {
118
104
t .Helper ()
119
105
120
106
if areSimilarSlices (expected , actual ) {
121
107
t .Error (equalityMsg (expected ))
122
108
}
123
109
}
124
110
125
- func areSimilarSlices [K comparable ](expected []K , actual []K ) bool {
111
+ func areSimilarSlices [T any ](expected []T , actual []T ) bool {
126
112
expectedLength := len (expected )
127
113
actualLength := len (actual )
128
114
if actualLength != expectedLength {
129
115
return false
130
116
}
131
117
132
- expectedElementsMap := make (map [K ]int , expectedLength )
133
- for _ , v := range expected {
134
- expectedElementsMap [v ] += 1
135
- }
136
-
137
- actualElementsMap := make (map [K ]int , actualLength )
138
- for _ , v := range actual {
139
- actualElementsMap [v ] += 1
140
- }
141
-
142
- return isEqualMap (expectedElementsMap , actualElementsMap )
143
- }
144
-
145
- /*
146
- Asserts that the two given maps have the same key-value pairs. The values must be [comparable]
147
- */
148
- func EqualMap [K , V comparable ](t testing.TB , expected map [K ]V , actual map [K ]V ) {
149
- t .Helper ()
150
-
151
- if ! isEqualMap (expected , actual ) {
152
- t .Error (inequalityMsg (expected , actual ))
153
- }
154
- }
155
-
156
- /*
157
- Asserts that the two given maps does not have the same key-value pairs. The values must be [comparable]
158
- */
159
- func NotEqualMap [K , V comparable ](t testing.TB , expected map [K ]V , actual map [K ]V ) {
160
- t .Helper ()
161
-
162
- if isEqualMap (expected , actual ) {
163
- t .Error (equalityMsg (expected ))
164
- }
165
- }
166
-
167
- func isEqualMap [K , V comparable ](expected map [K ]V , actual map [K ]V ) bool {
168
- expectedLength := len (expected )
169
- actualLength := len (actual )
170
- if actualLength != expectedLength {
171
- return false
172
- }
118
+ matchedIndexMap := make (map [int ]interface {}, expectedLength )
119
+ for _ , expectedValue := range expected {
120
+ for j , actualValue := range actual {
121
+ if ! reflect .DeepEqual (expectedValue , actualValue ) {
122
+ continue
123
+ }
173
124
174
- for actualKey , actualValue := range actual {
175
- expectedValue , found := expected [ actualKey ]
176
- if ! found || actualValue != expectedValue {
177
- return false
125
+ if _ , alreadyMatched := matchedIndexMap [ j ]; ! alreadyMatched {
126
+ matchedIndexMap [ j ] = nil
127
+ break
128
+ }
178
129
}
179
130
}
180
131
181
- return true
132
+ return len ( matchedIndexMap ) == expectedLength
182
133
}
0 commit comments