-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Extend assert.Same to support pointer-like objects #1777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -650,6 +650,31 @@ func TestSame(t *testing.T) { | |
if !Same(mockT, p, p) { | ||
t.Error("Same should return true") | ||
} | ||
m1 := map[int]int{} | ||
m2 := map[int]int{} | ||
if Same(mockT, m1, m2) { | ||
t.Error("Same should return false") | ||
} | ||
if !Same(mockT, m1, m1) { | ||
t.Error("Same should return true") | ||
} | ||
c1 := make(chan int) | ||
c2 := make(chan int) | ||
if Same(mockT, c1, c2) { | ||
t.Error("Same should return false") | ||
} | ||
if !Same(mockT, c1, c1) { | ||
t.Error("Same should return true") | ||
} | ||
Comment on lines
+661
to
+668
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Encode in the tests that c1 and c2 are Same:
|
||
s1 := []int{} | ||
s2 := []int{} | ||
if Same(mockT, s1, s2) { | ||
t.Error("Same should return false") | ||
} | ||
if Same(mockT, s1, s1) { | ||
// Slices are not pointer-like | ||
t.Error("Same should return false") | ||
} | ||
} | ||
|
||
func TestNotSame(t *testing.T) { | ||
|
@@ -670,12 +695,39 @@ func TestNotSame(t *testing.T) { | |
if NotSame(mockT, p, p) { | ||
t.Error("NotSame should return false") | ||
} | ||
m1 := map[int]int{} | ||
m2 := map[int]int{} | ||
if !NotSame(mockT, m1, m2) { | ||
t.Error("NotSame should return true; different maps") | ||
} | ||
if NotSame(mockT, m1, m1) { | ||
t.Error("NotSame should return false; same maps") | ||
} | ||
c1 := make(chan int) | ||
c2 := make(chan int) | ||
if !NotSame(mockT, c1, c2) { | ||
t.Error("NotSame should return true; different chans") | ||
} | ||
if NotSame(mockT, c1, c1) { | ||
t.Error("NotSame should return false; same chans") | ||
} | ||
s1 := []int{} | ||
s2 := []int{} | ||
if !NotSame(mockT, s1, s1) { | ||
t.Error("NotSame should return true; slices are not pointer-like") | ||
} | ||
if !NotSame(mockT, s1, s2) { | ||
t.Error("NotSame should return true; slices are not pointer-like") | ||
} | ||
} | ||
|
||
func Test_samePointers(t *testing.T) { | ||
func Test_sameReferences(t *testing.T) { | ||
t.Parallel() | ||
|
||
p := ptr(2) | ||
m := map[int]int{} | ||
c := make(chan int) | ||
s := []int{} | ||
|
||
type args struct { | ||
first interface{} | ||
|
@@ -717,6 +769,12 @@ func Test_samePointers(t *testing.T) { | |
same: False, | ||
ok: False, | ||
}, | ||
{ | ||
name: "slice disallowed", | ||
args: args{first: s, second: s}, | ||
same: False, | ||
ok: False, | ||
}, | ||
Comment on lines
+772
to
+777
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also test that string and func are disallowed. |
||
{ | ||
name: "non-pointer vs pointer (1 != ptr(2))", | ||
args: args{first: 1, second: p}, | ||
|
@@ -729,10 +787,46 @@ func Test_samePointers(t *testing.T) { | |
same: False, | ||
ok: False, | ||
}, | ||
{ | ||
name: "map1 == map1", | ||
args: args{first: m, second: m}, | ||
same: True, | ||
ok: True, | ||
}, | ||
{ | ||
name: "map1 != map2", | ||
args: args{first: m, second: map[int]int{}}, | ||
same: False, | ||
ok: True, | ||
}, | ||
{ | ||
name: "map1 != map2 (different types)", | ||
args: args{first: m, second: map[int]string{}}, | ||
same: False, | ||
ok: True, | ||
}, | ||
{ | ||
name: "chan1 == chan1", | ||
args: args{first: c, second: c}, | ||
same: True, | ||
ok: True, | ||
}, | ||
{ | ||
name: "chan1 != chan2", | ||
args: args{first: c, second: make(chan int)}, | ||
same: False, | ||
ok: True, | ||
}, | ||
{ | ||
name: "chan1 != chan2 (different types)", | ||
args: args{first: c, second: make(chan string)}, | ||
same: False, | ||
ok: True, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
same, ok := samePointers(tt.args.first, tt.args.second) | ||
same, ok := sameReferences(tt.args.first, tt.args.second) | ||
tt.same(t, same) | ||
tt.ok(t, ok) | ||
}) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's coercible mean? Just say "Both arguments must be of kind pointer, map, or channel".