Skip to content

Commit 1b4fca7

Browse files
Merge pull request #1600 from hendrywiranto/not-element-match
assert: new assertion NotElementsMatch
2 parents 6b275ad + cb4e70c commit 1b4fca7

File tree

6 files changed

+204
-0
lines changed

6 files changed

+204
-0
lines changed

assert/assertion_format.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assert/assertion_forward.go

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assert/assertions.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,39 @@ func formatListDiff(listA, listB interface{}, extraA, extraB []interface{}) stri
11761176
return msg.String()
11771177
}
11781178

1179+
// NotElementsMatch asserts that the specified listA(array, slice...) is NOT equal to specified
1180+
// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements,
1181+
// the number of appearances of each of them in both lists should not match.
1182+
// This is an inverse of ElementsMatch.
1183+
//
1184+
// assert.NotElementsMatch(t, [1, 1, 2, 3], [1, 1, 2, 3]) -> false
1185+
//
1186+
// assert.NotElementsMatch(t, [1, 1, 2, 3], [1, 2, 3]) -> true
1187+
//
1188+
// assert.NotElementsMatch(t, [1, 2, 3], [1, 2, 4]) -> true
1189+
func NotElementsMatch(t TestingT, listA, listB interface{}, msgAndArgs ...interface{}) (ok bool) {
1190+
if h, ok := t.(tHelper); ok {
1191+
h.Helper()
1192+
}
1193+
if isEmpty(listA) && isEmpty(listB) {
1194+
return Fail(t, "listA and listB contain the same elements", msgAndArgs)
1195+
}
1196+
1197+
if !isList(t, listA, msgAndArgs...) {
1198+
return Fail(t, "listA is not a list type", msgAndArgs...)
1199+
}
1200+
if !isList(t, listB, msgAndArgs...) {
1201+
return Fail(t, "listB is not a list type", msgAndArgs...)
1202+
}
1203+
1204+
extraA, extraB := diffLists(listA, listB)
1205+
if len(extraA) == 0 && len(extraB) == 0 {
1206+
return Fail(t, "listA and listB contain the same elements", msgAndArgs)
1207+
}
1208+
1209+
return true
1210+
}
1211+
11791212
// Condition uses a Comparison to assert a complex condition.
11801213
func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool {
11811214
if h, ok := t.(tHelper); ok {

assert/assertions_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,52 @@ func TestDiffLists(t *testing.T) {
13191319
}
13201320
}
13211321

1322+
func TestNotElementsMatch(t *testing.T) {
1323+
mockT := new(testing.T)
1324+
1325+
cases := []struct {
1326+
expected interface{}
1327+
actual interface{}
1328+
result bool
1329+
}{
1330+
// not mathing
1331+
{[]int{1}, []int{}, true},
1332+
{[]int{}, []int{2}, true},
1333+
{[]int{1}, []int{2}, true},
1334+
{[]int{1}, []int{1, 1}, true},
1335+
{[]int{1, 2}, []int{3, 4}, true},
1336+
{[]int{3, 4}, []int{1, 2}, true},
1337+
{[]int{1, 1, 2, 3}, []int{1, 2, 3}, true},
1338+
{[]string{"hello"}, []string{"world"}, true},
1339+
{[]string{"hello", "hello"}, []string{"world", "world"}, true},
1340+
{[3]string{"hello", "hello", "hello"}, [3]string{"world", "world", "world"}, true},
1341+
1342+
// matching
1343+
{nil, nil, false},
1344+
{[]int{}, nil, false},
1345+
{[]int{}, []int{}, false},
1346+
{[]int{1}, []int{1}, false},
1347+
{[]int{1, 1}, []int{1, 1}, false},
1348+
{[]int{1, 2}, []int{2, 1}, false},
1349+
{[2]int{1, 2}, [2]int{2, 1}, false},
1350+
{[]int{1, 1, 2}, []int{1, 2, 1}, false},
1351+
{[]string{"hello", "world"}, []string{"world", "hello"}, false},
1352+
{[]string{"hello", "hello"}, []string{"hello", "hello"}, false},
1353+
{[]string{"hello", "hello", "world"}, []string{"hello", "world", "hello"}, false},
1354+
{[3]string{"hello", "hello", "world"}, [3]string{"hello", "world", "hello"}, false},
1355+
}
1356+
1357+
for _, c := range cases {
1358+
t.Run(fmt.Sprintf("NotElementsMatch(%#v, %#v)", c.expected, c.actual), func(t *testing.T) {
1359+
res := NotElementsMatch(mockT, c.actual, c.expected)
1360+
1361+
if res != c.result {
1362+
t.Errorf("NotElementsMatch(%#v, %#v) should return %v", c.actual, c.expected, c.result)
1363+
}
1364+
})
1365+
}
1366+
}
1367+
13221368
func TestCondition(t *testing.T) {
13231369
mockT := new(testing.T)
13241370

require/require.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

require/require_forward.go

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)