Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}

same, ok := samePointers(expected, actual)
if !ok {
//fails when the arguments are not pointers
return !(Fail(t, "Both arguments must be pointers", msgAndArgs...))
// fails when the arguments are not pointers
Comment thread
ccoVeille marked this conversation as resolved.
return Fail(t, "Both arguments must be pointers", msgAndArgs...)
}

if same {
Expand Down
184 changes: 160 additions & 24 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,40 +610,176 @@ func ptr(i int) *int {
}

func TestSame(t *testing.T) {
p1 := ptr(1)
p1bis := ptr(1)
p2 := ptr(2)

mockT := new(testing.T)
tests := []struct {
name string
value interface{}
expected interface{}
result bool
resultErrMsg string
}{
{
name: "Same pointers",
value: p1,
expected: p1,
result: true,
},
{
name: "Different pointers to same variable",
value: p1,
expected: p1bis,
result: false,
resultErrMsg: "Not same: \n" +
fmt.Sprintf("expected: %v %#v\n", p1, p1) +
fmt.Sprintf("actual : %v %#v\n", p1bis, p1bis),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Use a single fmt.Sprintf expression for the whole string.
  • Use indexed formats like %#[1]v to remove parameter duplication (we should do that also in Same/NotSame code, but in a separate PR).
  • Use %p like in the tested code

},

if Same(mockT, ptr(1), ptr(1)) {
t.Error("Same should return false")
}
if Same(mockT, 1, 1) {
t.Error("Same should return false")
}
p := ptr(2)
if Same(mockT, p, *p) {
t.Error("Same should return false")
{
name: "Pointers to different variables",
value: p1,
expected: p2,
result: false,
resultErrMsg: "Not same: \n" +
fmt.Sprintf("expected: %v %#v\n", p1, p1) +
fmt.Sprintf("actual : %v %#v\n", p2, p2),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Use a single fmt.Sprintf expression for the whole string.
  • Use indexed formats like %#[1]v to remove parameter duplication (we should do that also in Same/NotSame code, but in a separate PR).
  • Use %p like in the tested code

},

{
name: "Pointer compared to nil",
value: p1,
expected: nil,
result: false,
resultErrMsg: "Both arguments must be pointers\n",
},

{
name: "nil compared to pointer",
value: nil,
expected: p1,
result: false,
resultErrMsg: "Both arguments must be pointers\n",
},

{
name: "pointer to value",
value: p1,
expected: *p1,
result: false,
resultErrMsg: "Both arguments must be pointers\n",
},

{
name: "pointer to target",
value: p1,
expected: *p1,
result: false,
resultErrMsg: "Both arguments must be pointers\n",
},

{
name: "Not pointers",
value: 1,
expected: 1,
result: false,
resultErrMsg: "Both arguments must be pointers\n",
},
}
if !Same(mockT, p, p) {
t.Error("Same should return true")

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
mockT := new(captureTestingT)
res := Same(mockT, tt.value, tt.expected)
mockT.checkResultAndErrMsg(t, tt.result, res, tt.resultErrMsg)
})
}
}

func TestNotSame(t *testing.T) {
p1 := ptr(1)
p1bis := ptr(1)
p2 := ptr(2)

mockT := new(testing.T)
tests := []struct {
name string
value interface{}
expected interface{}
result bool
resultErrMsg string
}{
{
name: "Same pointers",
value: p1,
expected: p1,
result: false,
resultErrMsg: "Expected and actual point to the same object: " +
fmt.Sprintf("%v %#v\n", p1, p1),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Use a single fmt.Sprintf expression for the whole string.
  • Use indexed formats like %#[1]v to remove parameter duplication (we should do that also in Same/NotSame code, but in a separate PR).
  • Use %p like in the tested code

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should do that also in Same/NotSame code, but in a separate PR

Done with #1742

},
{
name: "Different pointers to same variable",
value: p1,
expected: p1bis,
result: true,
},

if !NotSame(mockT, ptr(1), ptr(1)) {
t.Error("NotSame should return true; different pointers")
}
if !NotSame(mockT, 1, 1) {
t.Error("NotSame should return true; constant inputs")
}
p := ptr(2)
if !NotSame(mockT, p, *p) {
t.Error("NotSame should return true; mixed-type inputs")
{
name: "Pointers to different variables",
value: p1,
expected: p2,
result: true,
},

{
name: "Pointer compared to nil",
value: p1,
expected: nil,
result: false,
resultErrMsg: "Both arguments must be pointers\n",
},

{
name: "nil compared to pointer",
value: nil,
expected: p1,
result: false,
resultErrMsg: "Both arguments must be pointers\n",
},

{
name: "pointer to value",
value: p1,
expected: *p1,
result: false,
resultErrMsg: "Both arguments must be pointers\n",
},

{
name: "pointer to target",
value: p1,
expected: *p1,
result: false,
resultErrMsg: "Both arguments must be pointers\n",
},

{
name: "Not pointers",
value: 1,
expected: 1,
result: false,
resultErrMsg: "Both arguments must be pointers\n",
},
}
if NotSame(mockT, p, p) {
t.Error("NotSame should return false")

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
mockT := new(captureTestingT)
res := NotSame(mockT, tt.value, tt.expected)
mockT.checkResultAndErrMsg(t, tt.result, res, tt.resultErrMsg)
})
}
}

Expand Down