Skip to content

Commit

Permalink
Contains assertions should always fail if there is a match error
Browse files Browse the repository at this point in the history
And print the details of the match error.
  • Loading branch information
ansel1 committed Mar 30, 2021
1 parent ecd3d94 commit 27a353f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
8 changes: 4 additions & 4 deletions mapstest/assertion_messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAssertContains_message(t *testing.T) {

require.False(t, b)
assert.Equal(t, `
Error Trace: assertions.go:51
Error Trace: assertions.go:54
assertion_messages_test.go:16
Error: v1 does not contain v2:
values are not equal
Expand All @@ -42,7 +42,7 @@ func TestAssertNotContains_message(t *testing.T) {

require.False(t, b)
assert.Equal(t, `
Error Trace: assertions.go:75
Error Trace: assertions.go:81
assertion_messages_test.go:41
Error: v1 should not contain v2:
v1: red
Expand All @@ -58,7 +58,7 @@ func TestAssertEquivalent_message(t *testing.T) {

require.False(t, b)
assert.Equal(t, `
Error Trace: assertions.go:111
Error Trace: assertions.go:120
assertion_messages_test.go:57
Error: v1 !≈ v2:
values are not equal
Expand All @@ -83,7 +83,7 @@ func TestAssertNotEquivalent_message(t *testing.T) {

require.False(t, b)
assert.Equal(t, `
Error Trace: assertions.go:135
Error Trace: assertions.go:147
assertion_messages_test.go:82
Error: v1 should not ≈ v2:
v1: red
Expand Down
14 changes: 13 additions & 1 deletion mapstest/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ func AssertContains(t TestingT, v1, v2 interface{}, optsMsgAndArgs ...interface{
}
opts, optsMsgAndArgs := splitOptions(optsMsgAndArgs)
match := maps.ContainsMatch(v1, v2, opts...)
if !assert.NoError(t, match.Error, match.Message) {
return false
}

if !match.Matches {
nv1, err := maps.Normalize(v1)
if assert.NoError(t, err, "error normalizing v1") {
v1 = nv1
}
nv2, err := maps.Normalize(v2)
if !assert.NoError(t, err, "error normalizing v2") {
if assert.NoError(t, err, "error normalizing v2") {
v2 = nv2
}
diff := containsDiff(v1, v2)
Expand All @@ -62,6 +65,9 @@ func AssertNotContains(t TestingT, v1, v2 interface{}, optsMsgAndArgs ...interfa
}
opts, optsMsgAndArgs := splitOptions(optsMsgAndArgs)
match := maps.ContainsMatch(v1, v2, opts...)
if !assert.NoError(t, match.Error, match.Message) {
return false
}

if match.Matches {
nv1, err := maps.Normalize(v1)
Expand Down Expand Up @@ -98,6 +104,9 @@ func AssertEquivalent(t TestingT, v1, v2 interface{}, optsMsgAndArgs ...interfac
}
opts, optsMsgAndArgs := splitOptions(optsMsgAndArgs)
match := maps.EquivalentMatch(v1, v2, opts...)
if !assert.NoError(t, match.Error, match.Message) {
return false
}

if !match.Matches {
nv1, err := maps.Normalize(v1)
Expand All @@ -122,6 +131,9 @@ func AssertNotEquivalent(t TestingT, v1, v2 interface{}, optsMsgAndArgs ...inter
}
opts, optsMsgAndArgs := splitOptions(optsMsgAndArgs)
match := maps.EquivalentMatch(v1, v2, opts...)
if !assert.NoError(t, match.Error, match.Message) {
return false
}

if match.Matches {
nv1, err := maps.Normalize(v1)
Expand Down
24 changes: 16 additions & 8 deletions mapstest/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAssertionsContains(t *testing.T) {
v1, v2 interface{}
contains bool
equiv bool
err bool
opts []interface{}
}{
{
Expand Down Expand Up @@ -74,6 +75,11 @@ func TestAssertionsContains(t *testing.T) {
contains: true,
equiv: false,
},
{
v1: "string",
v2: make(chan bool),
err: true,
},
}

for _, test := range tests {
Expand All @@ -87,6 +93,7 @@ func TestAssertionsContains(t *testing.T) {
af := func(fn assertFunc, expectSuccess bool) {
mt := mockTestingT{}
b := fn(&mt, test.v1, test.v2, test.opts...)
t.Logf("msg: " + mt.msg)
assert.Equal(t, expectSuccess, b)
if expectSuccess {
assert.False(t, mt.failed)
Expand All @@ -100,6 +107,7 @@ func TestAssertionsContains(t *testing.T) {
rf := func(fn requireFunc, expectSuccess bool) {
mt := mockTestingT{}
fn(&mt, test.v1, test.v2, test.opts...)
t.Logf("msg: " + mt.msg)
if expectSuccess {
assert.False(t, mt.failed)
assert.False(t, mt.failedNow)
Expand All @@ -109,15 +117,15 @@ func TestAssertionsContains(t *testing.T) {
}
}

af(AssertContains, test.contains)
af(AssertNotContains, !test.contains)
rf(RequireContains, test.contains)
rf(RequireNotContains, !test.contains)
af(AssertContains, test.contains && !test.err)
af(AssertNotContains, !test.contains && !test.err)
rf(RequireContains, test.contains && !test.err)
rf(RequireNotContains, !test.contains && !test.err)

af(AssertEquivalent, test.equiv)
af(AssertNotEquivalent, !test.equiv)
rf(RequireEquivalent, test.equiv)
rf(RequireNotEquivalent, !test.equiv)
af(AssertEquivalent, test.equiv && !test.err)
af(AssertNotEquivalent, !test.equiv && !test.err)
rf(RequireEquivalent, test.equiv && !test.err)
rf(RequireNotEquivalent, !test.equiv && !test.err)

})
}
Expand Down

0 comments on commit 27a353f

Please sign in to comment.