Skip to content

Commit 20b1987

Browse files
authored
feat(test/gtest): add map type support for AssertNI/AssertIN (#4135)
1 parent f9c7eae commit 20b1987

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

test/gtest/gtest_util.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ func AssertLE(value, expect interface{}) {
220220
// AssertIN checks `value` is IN `expect`.
221221
// The `expect` should be a slice,
222222
// but the `value` can be a slice or a basic type variable.
223-
// TODO map support.
224223
// TODO: gconv.Strings(0) is not [0]
225224
func AssertIN(value, expect interface{}) {
226225
var (
@@ -249,6 +248,14 @@ func AssertIN(value, expect interface{}) {
249248
expectStr = gconv.String(expect)
250249
)
251250
passed = gstr.Contains(expectStr, valueStr)
251+
case reflect.Map:
252+
expectMap := gconv.Map(expect)
253+
for _, v1 := range gconv.Strings(value) {
254+
if _, exists := expectMap[v1]; !exists {
255+
passed = false
256+
break
257+
}
258+
}
252259
default:
253260
panic(fmt.Sprintf(`[ASSERT] INVALID EXPECT VALUE TYPE: %v`, expectKind))
254261
}
@@ -260,7 +267,6 @@ func AssertIN(value, expect interface{}) {
260267
// AssertNI checks `value` is NOT IN `expect`.
261268
// The `expect` should be a slice,
262269
// but the `value` can be a slice or a basic type variable.
263-
// TODO map support.
264270
func AssertNI(value, expect interface{}) {
265271
var (
266272
passed = true
@@ -287,6 +293,14 @@ func AssertNI(value, expect interface{}) {
287293
expectStr = gconv.String(expect)
288294
)
289295
passed = !gstr.Contains(expectStr, valueStr)
296+
case reflect.Map:
297+
expectMap := gconv.Map(expect)
298+
for _, v1 := range gconv.Strings(value) {
299+
if _, exists := expectMap[v1]; exists {
300+
passed = false
301+
break
302+
}
303+
}
290304
default:
291305
panic(fmt.Sprintf(`[ASSERT] INVALID EXPECT VALUE TYPE: %v`, expectKind))
292306
}

test/gtest/gtest_z_unit_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,22 @@ func TestAssertIN(t *testing.T) {
322322
})
323323
}
324324

325+
func TestAssertIN_Map(t *testing.T) {
326+
gtest.C(t, func(t *gtest.T) {
327+
t.AssertIN("k1", map[string]string{"k1": "v1", "k2": "v2"})
328+
t.AssertIN(1, map[int64]string{1: "v1", 2: "v2"})
329+
t.AssertIN([]string{"k1", "k2"}, map[string]string{"k1": "v1", "k2": "v2"})
330+
})
331+
}
332+
333+
func TestAssertNI_Map(t *testing.T) {
334+
gtest.C(t, func(t *gtest.T) {
335+
t.AssertNI("k3", map[string]string{"k1": "v1", "k2": "v2"})
336+
t.AssertNI(3, map[int64]string{1: "v1", 2: "v2"})
337+
t.AssertNI([]string{"k3", "k4"}, map[string]string{"k1": "v1", "k2": "v2"})
338+
})
339+
}
340+
325341
func TestAssertNI(t *testing.T) {
326342
gtest.C(t, func(t *gtest.T) {
327343
t.AssertNI("d", []string{"a", "b", "c"})

0 commit comments

Comments
 (0)