Skip to content

Commit

Permalink
Adds DeepNotEqual assertion (#7092)
Browse files Browse the repository at this point in the history
* adds deepnotequal func
  • Loading branch information
farazdagi authored Aug 24, 2020
1 parent 78a25f9 commit 5cee10f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
5 changes: 5 additions & 0 deletions shared/testutil/assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func DeepEqual(tb assertions.AssertionTestingTB, expected, actual interface{}, m
assertions.DeepEqual(tb.Errorf, expected, actual, msg...)
}

// DeepNotEqual compares values using DeepEqual.
func DeepNotEqual(tb assertions.AssertionTestingTB, expected, actual interface{}, msg ...interface{}) {
assertions.DeepNotEqual(tb.Errorf, expected, actual, msg...)
}

// NoError asserts that error is nil.
func NoError(tb assertions.AssertionTestingTB, err error, msg ...interface{}) {
assertions.NoError(tb.Errorf, err, msg...)
Expand Down
9 changes: 9 additions & 0 deletions shared/testutil/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ func DeepEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...
}
}

// DeepNotEqual compares values using DeepEqual.
func DeepNotEqual(loggerFn assertionLoggerFn, expected, actual interface{}, msg ...interface{}) {
errMsg := parseMsg("Values are equal", msg...)
if reflect.DeepEqual(expected, actual) {
_, file, line, _ := runtime.Caller(2)
loggerFn("%s:%d %s, want: %#v, got: %#v", filepath.Base(file), line, errMsg, expected, actual)
}
}

// NoError asserts that error is nil.
func NoError(loggerFn assertionLoggerFn, err error, msg ...interface{}) {
errMsg := parseMsg("Unexpected error", msg...)
Expand Down
69 changes: 69 additions & 0 deletions shared/testutil/assertions/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,75 @@ func TestAssert_DeepEqual(t *testing.T) {
}
}

func TestAssert_DeepNotEqual(t *testing.T) {
type args struct {
tb *assertions.TBMock
expected interface{}
actual interface{}
msgs []interface{}
}
tests := []struct {
name string
args args
expectedErr string
}{
{
name: "equal values",
args: args{
tb: &assertions.TBMock{},
expected: struct{ i int }{42},
actual: struct{ i int }{42},
},
expectedErr: "Values are equal, want: struct { i int }{i:42}, got: struct { i int }{i:42}",
},
{
name: "non-equal values",
args: args{
tb: &assertions.TBMock{},
expected: struct{ i int }{42},
actual: struct{ i int }{41},
},
},
{
name: "custom error message",
args: args{
tb: &assertions.TBMock{},
expected: struct{ i int }{42},
actual: struct{ i int }{42},
msgs: []interface{}{"Custom values are equal"},
},
expectedErr: "Custom values are equal, want: struct { i int }{i:42}, got: struct { i int }{i:42}",
},
{
name: "custom error message with params",
args: args{
tb: &assertions.TBMock{},
expected: struct{ i int }{42},
actual: struct{ i int }{42},
msgs: []interface{}{"Custom values are equal (for slot %d)", 12},
},
expectedErr: "Custom values are equal (for slot 12), want: struct { i int }{i:42}, got: struct { i int }{i:42}",
},
}
for _, tt := range tests {
verify := func() {
if tt.expectedErr == "" && tt.args.tb.ErrorfMsg != "" {
t.Errorf("Unexpected error: %v", tt.args.tb.ErrorfMsg)
} else if !strings.Contains(tt.args.tb.ErrorfMsg, tt.expectedErr) {
t.Errorf("got: %q, want: %q", tt.args.tb.ErrorfMsg, tt.expectedErr)
}
}
t.Run(fmt.Sprintf("Assert/%s", tt.name), func(t *testing.T) {
assert.DeepNotEqual(tt.args.tb, tt.args.expected, tt.args.actual, tt.args.msgs...)
verify()
})
t.Run(fmt.Sprintf("Require/%s", tt.name), func(t *testing.T) {
require.DeepNotEqual(tt.args.tb, tt.args.expected, tt.args.actual, tt.args.msgs...)
verify()
})
}
}

func TestAssert_NoError(t *testing.T) {
type args struct {
tb *assertions.TBMock
Expand Down
5 changes: 5 additions & 0 deletions shared/testutil/require/requires.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func DeepEqual(tb assertions.AssertionTestingTB, expected, actual interface{}, m
assertions.DeepEqual(tb.Fatalf, expected, actual, msg...)
}

// DeepNotEqual compares values using DeepEqual.
func DeepNotEqual(tb assertions.AssertionTestingTB, expected, actual interface{}, msg ...interface{}) {
assertions.DeepNotEqual(tb.Fatalf, expected, actual, msg...)
}

// NoError asserts that error is nil.
func NoError(tb assertions.AssertionTestingTB, err error, msg ...interface{}) {
assertions.NoError(tb.Fatalf, err, msg...)
Expand Down

0 comments on commit 5cee10f

Please sign in to comment.