Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test/gtest): add map type support for AssertNI/AssertIN #4135

Merged
merged 5 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions test/gtest/gtest_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ func AssertLE(value, expect interface{}) {
// AssertIN checks `value` is IN `expect`.
// The `expect` should be a slice,
// but the `value` can be a slice or a basic type variable.
// TODO map support.
// TODO: gconv.Strings(0) is not [0]
func AssertIN(value, expect interface{}) {
var (
Expand Down Expand Up @@ -249,6 +248,14 @@ func AssertIN(value, expect interface{}) {
expectStr = gconv.String(expect)
)
passed = gstr.Contains(expectStr, valueStr)
case reflect.Map:
expectMap := gconv.Map(expect)
for _, v1 := range gconv.Strings(value) {
if _, exists := expectMap[v1]; !exists {
passed = false
break
}
}
default:
panic(fmt.Sprintf(`[ASSERT] INVALID EXPECT VALUE TYPE: %v`, expectKind))
}
Expand All @@ -260,7 +267,6 @@ func AssertIN(value, expect interface{}) {
// AssertNI checks `value` is NOT IN `expect`.
// The `expect` should be a slice,
// but the `value` can be a slice or a basic type variable.
// TODO map support.
func AssertNI(value, expect interface{}) {
var (
passed = true
Expand All @@ -287,6 +293,14 @@ func AssertNI(value, expect interface{}) {
expectStr = gconv.String(expect)
)
passed = !gstr.Contains(expectStr, valueStr)
case reflect.Map:
expectMap := gconv.Map(expect)
for _, v1 := range gconv.Strings(value) {
if _, exists := expectMap[v1]; exists {
passed = false
break
}
}
default:
panic(fmt.Sprintf(`[ASSERT] INVALID EXPECT VALUE TYPE: %v`, expectKind))
}
Expand Down
16 changes: 16 additions & 0 deletions test/gtest/gtest_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,22 @@ func TestAssertIN(t *testing.T) {
})
}

func TestAssertIN_Map(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertIN("k1", map[string]string{"k1": "v1", "k2": "v2"})
t.AssertIN(1, map[int64]string{1: "v1", 2: "v2"})
t.AssertIN([]string{"k1", "k2"}, map[string]string{"k1": "v1", "k2": "v2"})
})
}

func TestAssertNI_Map(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertNI("k3", map[string]string{"k1": "v1", "k2": "v2"})
t.AssertNI(3, map[int64]string{1: "v1", 2: "v2"})
t.AssertNI([]string{"k3", "k4"}, map[string]string{"k1": "v1", "k2": "v2"})
})
}

func TestAssertNI(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertNI("d", []string{"a", "b", "c"})
Expand Down
Loading