Skip to content

Commit

Permalink
Merge pull request #1600 from hendrywiranto/not-element-match
Browse files Browse the repository at this point in the history
assert: new assertion NotElementsMatch
  • Loading branch information
brackendawson committed May 29, 2024
2 parents 6b275ad + cb4e70c commit 1b4fca7
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
17 changes: 17 additions & 0 deletions assert/assertion_format.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions assert/assertion_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,39 @@ func formatListDiff(listA, listB interface{}, extraA, extraB []interface{}) stri
return msg.String()
}

// NotElementsMatch asserts that the specified listA(array, slice...) is NOT equal to specified
// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements,
// the number of appearances of each of them in both lists should not match.
// This is an inverse of ElementsMatch.
//
// assert.NotElementsMatch(t, [1, 1, 2, 3], [1, 1, 2, 3]) -> false
//
// assert.NotElementsMatch(t, [1, 1, 2, 3], [1, 2, 3]) -> true
//
// assert.NotElementsMatch(t, [1, 2, 3], [1, 2, 4]) -> true
func NotElementsMatch(t TestingT, listA, listB interface{}, msgAndArgs ...interface{}) (ok bool) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
if isEmpty(listA) && isEmpty(listB) {
return Fail(t, "listA and listB contain the same elements", msgAndArgs)
}

if !isList(t, listA, msgAndArgs...) {
return Fail(t, "listA is not a list type", msgAndArgs...)
}
if !isList(t, listB, msgAndArgs...) {
return Fail(t, "listB is not a list type", msgAndArgs...)
}

extraA, extraB := diffLists(listA, listB)
if len(extraA) == 0 && len(extraB) == 0 {
return Fail(t, "listA and listB contain the same elements", msgAndArgs)
}

return true
}

// Condition uses a Comparison to assert a complex condition.
func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
Expand Down
46 changes: 46 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,52 @@ func TestDiffLists(t *testing.T) {
}
}

func TestNotElementsMatch(t *testing.T) {
mockT := new(testing.T)

cases := []struct {
expected interface{}
actual interface{}
result bool
}{
// not mathing
{[]int{1}, []int{}, true},
{[]int{}, []int{2}, true},
{[]int{1}, []int{2}, true},
{[]int{1}, []int{1, 1}, true},
{[]int{1, 2}, []int{3, 4}, true},
{[]int{3, 4}, []int{1, 2}, true},
{[]int{1, 1, 2, 3}, []int{1, 2, 3}, true},
{[]string{"hello"}, []string{"world"}, true},
{[]string{"hello", "hello"}, []string{"world", "world"}, true},
{[3]string{"hello", "hello", "hello"}, [3]string{"world", "world", "world"}, true},

// matching
{nil, nil, false},
{[]int{}, nil, false},
{[]int{}, []int{}, false},
{[]int{1}, []int{1}, false},
{[]int{1, 1}, []int{1, 1}, false},
{[]int{1, 2}, []int{2, 1}, false},
{[2]int{1, 2}, [2]int{2, 1}, false},
{[]int{1, 1, 2}, []int{1, 2, 1}, false},
{[]string{"hello", "world"}, []string{"world", "hello"}, false},
{[]string{"hello", "hello"}, []string{"hello", "hello"}, false},
{[]string{"hello", "hello", "world"}, []string{"hello", "world", "hello"}, false},
{[3]string{"hello", "hello", "world"}, [3]string{"hello", "world", "hello"}, false},
}

for _, c := range cases {
t.Run(fmt.Sprintf("NotElementsMatch(%#v, %#v)", c.expected, c.actual), func(t *testing.T) {
res := NotElementsMatch(mockT, c.actual, c.expected)

if res != c.result {
t.Errorf("NotElementsMatch(%#v, %#v) should return %v", c.actual, c.expected, c.result)
}
})
}
}

func TestCondition(t *testing.T) {
mockT := new(testing.T)

Expand Down
40 changes: 40 additions & 0 deletions require/require.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions require/require_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1b4fca7

Please sign in to comment.