Skip to content

Commit

Permalink
Implement NotEqualValues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivo van der Wijk authored and boyan-soubachov committed May 26, 2020
1 parent e72b029 commit ac1463f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
15 changes: 15 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,21 @@ func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{

}

// NotEqualValues asserts that two objects are not equal even when converted to the same type
//
// assert.NotEqualValues(t, obj1, obj2)
func NotEqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

if ObjectsAreEqualValues(expected, actual) {
return Fail(t, fmt.Sprintf("Should not be: %#v\n", actual), msgAndArgs...)
}

return true
}

// containsElement try loop over the list check if the list includes the element.
// return (false, false) if impossible.
// return (true, false) if element was not found.
Expand Down
69 changes: 69 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ func TestEqual(t *testing.T) {
if Equal(mockT, myType("1"), myType("2")) {
t.Error("Equal should return false")
}
// A case that might be confusing, especially with numeric literals
if Equal(mockT, 10, uint(10)) {
t.Error("Equal should return false")
}
}

func ptr(i int) *int {
Expand Down Expand Up @@ -544,6 +548,70 @@ func TestNotEqual(t *testing.T) {
if NotEqual(mockT, &struct{}{}, &struct{}{}) {
t.Error("NotEqual should return false")
}

// A case that might be confusing, especially with numeric literals
if !NotEqual(mockT, 10, uint(10)) {
t.Error("NotEqual should return false")
}
}

func TestNotEqualValues(t *testing.T) {

mockT := new(testing.T)

// Same tests as NotEqual since they behave the same when types are irrelevant
if !NotEqualValues(mockT, "Hello World", "Hello World!") {
t.Error("NotEqualValues should return true")
}
if !NotEqualValues(mockT, 123, 1234) {
t.Error("NotEqualValues should return true")
}
if !NotEqualValues(mockT, 123.5, 123.55) {
t.Error("NotEqualValues should return true")
}
if !NotEqualValues(mockT, []byte("Hello World"), []byte("Hello World!")) {
t.Error("NotEqualValues should return true")
}
if !NotEqualValues(mockT, nil, new(AssertionTesterConformingObject)) {
t.Error("NotEqualValues should return true")
}
if NotEqualValues(mockT, nil, nil) {
t.Error("NotEqualValues should return false")
}
if NotEqualValues(mockT, "Hello World", "Hello World") {
t.Error("NotEqualValues should return false")
}
if NotEqualValues(mockT, 123, 123) {
t.Error("NotEqualValues should return false")
}
if NotEqualValues(mockT, 123.5, 123.5) {
t.Error("NotEqualValues should return false")
}
if NotEqualValues(mockT, []byte("Hello World"), []byte("Hello World")) {
t.Error("NotEqualValues should return false")
}
if NotEqualValues(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
t.Error("NotEqualValues should return false")
}
if NotEqualValues(mockT, &struct{}{}, &struct{}{}) {
t.Error("NotEqualValues should return false")
}

// Special cases where NotEqualValues behaves differently
funcA := func() int { return 23 }
funcB := func() int { return 42 }
if !NotEqualValues(mockT, funcA, funcB) {
t.Error("NotEqualValues should return true")
}
if !NotEqualValues(mockT, int(10), int(11)) {
t.Error("NotEqualValues should return true")
}
if NotEqualValues(mockT, int(10), uint(10)) {
t.Error("NotEqualValues should return false")
}
if NotEqualValues(mockT, struct{}{}, struct{}{}) {
t.Error("NotEqualValues should return false")
}
}

type A struct {
Expand Down Expand Up @@ -2139,6 +2207,7 @@ func TestComparisonAssertionFunc(t *testing.T) {
{"isType", (*testing.T)(nil), t, IsType},
{"equal", t, t, Equal},
{"equalValues", t, t, EqualValues},
{"notEqualValues", t, nil, NotEqualValues},
{"exactly", t, t, Exactly},
{"notEqual", t, nil, NotEqual},
{"notContains", []int{1, 2, 3}, 4, NotContains},
Expand Down
24 changes: 24 additions & 0 deletions assert/forward_assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,30 @@ func TestNotEqualWrapper(t *testing.T) {
}
}

func TestNotEqualValuesWrapper(t *testing.T) {

assert := New(new(testing.T))

if !assert.NotEqualValues("Hello World", "Hello World!") {
t.Error("NotEqualValues should return true")
}
if !assert.NotEqualValues(123, 1234) {
t.Error("NotEqualValues should return true")
}
if !assert.NotEqualValues(123.5, 123.55) {
t.Error("NotEqualValues should return true")
}
if !assert.NotEqualValues([]byte("Hello World"), []byte("Hello World!")) {
t.Error("NotEqualValues should return true")
}
if !assert.NotEqualValues(nil, new(AssertionTesterConformingObject)) {
t.Error("NotEqualValues should return true")
}
if assert.NotEqualValues(10, uint(10)) {
t.Error("NotEqualValues should return false")
}
}

func TestContainsWrapper(t *testing.T) {

assert := New(new(testing.T))
Expand Down
1 change: 1 addition & 0 deletions require/requirements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ func TestComparisonAssertionFunc(t *testing.T) {
{"equalValues", t, t, EqualValues},
{"exactly", t, t, Exactly},
{"notEqual", t, nil, NotEqual},
{"NotEqualValues", t, nil, NotEqualValues},
{"notContains", []int{1, 2, 3}, 4, NotContains},
{"subset", []int{1, 2, 3, 4}, []int{2, 3}, Subset},
{"notSubset", []int{1, 2, 3, 4}, []int{0, 3}, NotSubset},
Expand Down

0 comments on commit ac1463f

Please sign in to comment.