Skip to content

Commit

Permalink
👔 up: assert - add new helper func: ContainsElems
Browse files Browse the repository at this point in the history
- fix maputil test error
  • Loading branch information
inhere committed Mar 24, 2024
1 parent 9203a60 commit 1c08e6b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions maputil/get_test.go
Expand Up @@ -368,8 +368,8 @@ func TestEachTypedMap_HappyPath(t *testing.T) {
values = append(values, val)
})

assert.Eq(t, []string{"key1", "key2", "key3"}, keys)
assert.Eq(t, []int{1, 2, 3}, values)
assert.ContainsElems(t, keys, []string{"key1", "key2", "key3"})
assert.ContainsElems(t, values, []int{1, 2, 3})
}

func TestEachTypedMap_NonStringKeys(t *testing.T) {
Expand All @@ -387,6 +387,6 @@ func TestEachTypedMap_NonStringKeys(t *testing.T) {
values = append(values, val)
})

assert.Eq(t, []int{1, 2, 3}, keys)
assert.Eq(t, []int{1, 2, 3}, values)
assert.ContainsElems(t, keys, []int{1, 2, 3})
assert.ContainsElems(t, values, []int{1, 2, 3})
}
13 changes: 13 additions & 0 deletions testutil/assert/asserts.go
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/internal/checkfn"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/mathutil"
Expand Down Expand Up @@ -287,6 +288,18 @@ func NotContainsKeys(t TestingT, mp any, keys any, fmtAndArgs ...any) bool {
return true
}

// ContainsElems asserts that the given list should contains sub elements.
func ContainsElems[T comdef.ScalarType](t TestingT, list, sub []T, fmtAndArgs ...any) bool {
if arrutil.ContainsAll(list, sub) {
return true
}

t.Helper()

// not contains all
return fail(t, fmt.Sprintf("%#v\nShould contain: %#v", list, sub), fmtAndArgs)
}

// StrContains asserts that the given strings is contains sub-string
func StrContains(t TestingT, s, sub string, fmtAndArgs ...any) bool {
if strings.Contains(s, sub) {
Expand Down
3 changes: 3 additions & 0 deletions testutil/assert/asserts_test.go
Expand Up @@ -315,4 +315,7 @@ func TestContains(t *testing.T) {
assert.ContainsKeys(t, mp, []string{"name", "age"})
assert.NotContainsKey(t, mp, "addr")
assert.NotContainsKeys(t, mp, []string{"addr"})

assert.ContainsElems(t, []string{"def"}, []string{"def"})
assert.ContainsElems(t, []string{"def", "abc"}, []string{"def"})
}

0 comments on commit 1c08e6b

Please sign in to comment.