From e0d1ff125c9e0eea513a86886274269ccef1c466 Mon Sep 17 00:00:00 2001 From: sikehish Date: Thu, 17 Oct 2024 13:56:15 +0530 Subject: [PATCH 01/10] Modified samePointers signature and additional boolean returned. --- assert/assertions.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 44b854da6..1b93b95a2 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -532,19 +532,19 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{} // samePointers compares two generic interface objects and returns whether // they point to the same object -func samePointers(first, second interface{}) bool { +func samePointers(first, second interface{}) (same bool, ok bool) { firstPtr, secondPtr := reflect.ValueOf(first), reflect.ValueOf(second) if firstPtr.Kind() != reflect.Ptr || secondPtr.Kind() != reflect.Ptr { - return false + return false, false //not both are pointers } firstType, secondType := reflect.TypeOf(first), reflect.TypeOf(second) if firstType != secondType { - return false + return false, true // both are pointers, but of different types } // compare pointer addresses - return first == second + return first == second, true } // formatUnequalValues takes two values of arbitrary types and returns string From 8bff88f4cc54f57e4c85ac969f98eb2bb80b416e Mon Sep 17 00:00:00 2001 From: sikehish Date: Thu, 17 Oct 2024 15:42:13 +0530 Subject: [PATCH 02/10] Modified return statement in assert.NotSame --- assert/assertions.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 1b93b95a2..db77691b9 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -502,7 +502,13 @@ func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) b h.Helper() } - if !samePointers(expected, actual) { + same, ok := samePointers(expected, actual) + if !ok { + return Fail(t, "Both arguments must be pointers", msgAndArgs...) + } + + //both are pointers but pointing to different addresses + if !same { return Fail(t, fmt.Sprintf("Not same: \n"+ "expected: %p %#v\n"+ "actual : %p %#v", expected, expected, actual, actual), msgAndArgs...) @@ -521,13 +527,19 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{} if h, ok := t.(tHelper); ok { h.Helper() } - - if samePointers(expected, actual) { - return Fail(t, fmt.Sprintf( - "Expected and actual point to the same object: %p %#v", - expected, expected), msgAndArgs...) + if same, ok := samePointers(expected, actual); ok { + //if ok is true, then both arguments are pointers + if same { + return Fail(t, fmt.Sprintf( + "Expected and actual point to the same object: %p %#v", + expected, expected), msgAndArgs...) //because Fail return false, but we need to + } + return true } - return true + + //fails when the arguments are not pointers + return !(Fail(t, "Both arguments must be pointers", msgAndArgs...)) + //we return !Fail because Fail return false, but we need to return true if the arguments are not pointers(by default) } // samePointers compares two generic interface objects and returns whether From 65657a1b76fe1524e2e7f777ecae0e654832c60f Mon Sep 17 00:00:00 2001 From: sikehish Date: Thu, 17 Oct 2024 15:43:15 +0530 Subject: [PATCH 03/10] Modified Test_samePointers based on the modified samePointers function in assertions_test.go --- assert/assertions_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index e158688f2..0d347dd01 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -637,7 +637,12 @@ func Test_samePointers(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.assertion(t, samePointers(tt.args.first, tt.args.second)) + same, ok := samePointers(tt.args.first, tt.args.second) + if !ok { + tt.assertion(t, false) // both arguments must be pointers + return + } + tt.assertion(t, same) }) } } From c64e59589ab81b2e8bf618ba5124a9689a394a15 Mon Sep 17 00:00:00 2001 From: Hisham Akmal Date: Fri, 18 Oct 2024 22:15:41 +0530 Subject: [PATCH 04/10] Update assert/assertions.go Co-authored-by: Bracken --- assert/assertions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assert/assertions.go b/assert/assertions.go index db77691b9..546cfebdf 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -507,8 +507,8 @@ func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) b return Fail(t, "Both arguments must be pointers", msgAndArgs...) } - //both are pointers but pointing to different addresses if !same { + // both are pointers but not the same type & pointing to the same address return Fail(t, fmt.Sprintf("Not same: \n"+ "expected: %p %#v\n"+ "actual : %p %#v", expected, expected, actual, actual), msgAndArgs...) From e2f36683adbd4f027da168b235b867b9cda3eb38 Mon Sep 17 00:00:00 2001 From: sikehish Date: Fri, 18 Oct 2024 22:20:37 +0530 Subject: [PATCH 05/10] Updated doc-string of samePointers in assert/assertions.go --- assert/assertions.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 546cfebdf..d450ae7c9 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -542,8 +542,10 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{} //we return !Fail because Fail return false, but we need to return true if the arguments are not pointers(by default) } -// samePointers compares two generic interface objects and returns whether -// they point to the same object +// samePointers checks if two generic interface objects are pointers to +// the same object. It returns two values: a boolean indicating if they +// point to the same object, and another boolean indicating whether both +// inputs are valid pointers of the same type. func samePointers(first, second interface{}) (same bool, ok bool) { firstPtr, secondPtr := reflect.ValueOf(first), reflect.ValueOf(second) if firstPtr.Kind() != reflect.Ptr || secondPtr.Kind() != reflect.Ptr { From 31b28ee205a8dac73974f64a1ca47ed32fcbfb5a Mon Sep 17 00:00:00 2001 From: sikehish Date: Fri, 18 Oct 2024 22:32:00 +0530 Subject: [PATCH 06/10] Refactored NotSame in assert/assertions.go to preserve line of sight --- assert/assertions.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/assert/assertions.go b/assert/assertions.go index d450ae7c9..06f45b7ad 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -527,7 +527,10 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{} if h, ok := t.(tHelper); ok { h.Helper() } - if same, ok := samePointers(expected, actual); ok { + + same, ok := samePointers(expected, actual) + + if ok { //if ok is true, then both arguments are pointers if same { return Fail(t, fmt.Sprintf( From c7b4b096faf5ba1eb51d644227361f7e205c2f92 Mon Sep 17 00:00:00 2001 From: sikehish Date: Fri, 18 Oct 2024 22:48:51 +0530 Subject: [PATCH 07/10] Refactored Test_samePointers --- assert/assertions_test.go | 49 +++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 0d347dd01..63c3a8c88 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -605,44 +605,47 @@ func Test_samePointers(t *testing.T) { second interface{} } tests := []struct { - name string - args args - assertion BoolAssertionFunc + name string + args args + same BoolAssertionFunc + ok BoolAssertionFunc }{ { - name: "1 != 2", - args: args{first: 1, second: 2}, - assertion: False, + name: "1 != 2", + args: args{first: 1, second: 2}, + same: False, + ok: False, }, { - name: "1 != 1 (not same ptr)", - args: args{first: 1, second: 1}, - assertion: False, + name: "1 != 1 (not same ptr)", + args: args{first: 1, second: 1}, + same: False, + ok: False, }, { - name: "ptr(1) == ptr(1)", - args: args{first: p, second: p}, - assertion: True, + name: "ptr(1) == ptr(1)", + args: args{first: p, second: p}, + same: True, + ok: True, }, { - name: "int(1) != float32(1)", - args: args{first: int(1), second: float32(1)}, - assertion: False, + name: "int(1) != float32(1)", + args: args{first: int(1), second: float32(1)}, + same: False, + ok: False, }, { - name: "array != slice", - args: args{first: [2]int{1, 2}, second: []int{1, 2}}, - assertion: False, + name: "array != slice", + args: args{first: [2]int{1, 2}, second: []int{1, 2}}, + same: False, + ok: False, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { same, ok := samePointers(tt.args.first, tt.args.second) - if !ok { - tt.assertion(t, false) // both arguments must be pointers - return - } - tt.assertion(t, same) + tt.same(t, same) + tt.ok(t, ok) }) } } From 17dcafd1d6626387237471bf84b3c96a2c9bf6a8 Mon Sep 17 00:00:00 2001 From: sikehish Date: Sun, 27 Oct 2024 00:40:10 +0530 Subject: [PATCH 08/10] Add test cases for pointer vs non-pointer comparisons in Test_samePointers in assert/assertions_test.go --- assert/assertions_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 63c3a8c88..f522470c0 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -640,6 +640,18 @@ func Test_samePointers(t *testing.T) { same: False, ok: False, }, + { + name: "non-pointer vs pointer (1 != ptr(2))", + args: args{first: 1, second: p}, + same: False, + ok: False, + }, + { + name: "pointer vs non-pointer (ptr(2) != 1)", + args: args{first: p, second: 1}, + same: False, + ok: False, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From e7392c621d96421fdf543df82dbde6c42ef21dd5 Mon Sep 17 00:00:00 2001 From: sikehish Date: Sun, 27 Oct 2024 00:50:45 +0530 Subject: [PATCH 09/10] Refactor NotSame to improve readability and streamline error handling in assert/assertions.go --- assert/assertions.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 06f45b7ad..d225747eb 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -529,20 +529,17 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{} } same, ok := samePointers(expected, actual) - - if ok { - //if ok is true, then both arguments are pointers - if same { - return Fail(t, fmt.Sprintf( - "Expected and actual point to the same object: %p %#v", - expected, expected), msgAndArgs...) //because Fail return false, but we need to - } - return true + 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 - return !(Fail(t, "Both arguments must be pointers", msgAndArgs...)) - //we return !Fail because Fail return false, but we need to return true if the arguments are not pointers(by default) + if same { + return Fail(t, fmt.Sprintf( + "Expected and actual point to the same object: %p %#v", + expected, expected), msgAndArgs...) + } + return true } // samePointers checks if two generic interface objects are pointers to From db25ad04afad4ce7d6c580b4767fd892cfd48b99 Mon Sep 17 00:00:00 2001 From: Hisham Akmal Date: Sun, 27 Oct 2024 19:14:12 +0530 Subject: [PATCH 10/10] Update assert/assertions.go Co-authored-by: Bracken --- assert/assertions.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index d225747eb..9a2ec7bb8 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -542,10 +542,10 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{} return true } -// samePointers checks if two generic interface objects are pointers to -// the same object. It returns two values: a boolean indicating if they -// point to the same object, and another boolean indicating whether both -// inputs are valid pointers of the same type. +// samePointers checks if two generic interface objects are pointers of the same +// type pointing to the same object. It returns two values: same indicating if +// they are the same type and point to the same object, and ok indicating that +// both inputs are pointers. func samePointers(first, second interface{}) (same bool, ok bool) { firstPtr, secondPtr := reflect.ValueOf(first), reflect.ValueOf(second) if firstPtr.Kind() != reflect.Ptr || secondPtr.Kind() != reflect.Ptr {