Skip to content
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

assert: handle array case in copyExportedFields #1404 #1436

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,19 @@ func copyExportedFields(expected interface{}) interface{} {
result.Elem().Set(reflect.ValueOf(unexportedRemoved))
return result.Interface()

case reflect.Array, reflect.Slice:
case reflect.Array:
result := reflect.New(expectedType).Elem()
for i := 0; i < expectedValue.Len(); i++ {
index := expectedValue.Index(i)
if isNil(index.Interface()) {
continue
Copy link
Collaborator

Choose a reason for hiding this comment

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

This case is not covered by the test suite.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

I added one more test for nil-value, but it failed. I fixed it by passing an actual interface{} to the isNil function.

}
unexportedRemoved := copyExportedFields(index.Interface())
result.Index(i).Set(reflect.ValueOf(unexportedRemoved))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
result.Index(i).Set(reflect.ValueOf(unexportedRemoved))
index.Set(reflect.ValueOf(unexportedRemoved))

}
return result.Interface()

case reflect.Slice:
result := reflect.MakeSlice(expectedType, expectedValue.Len(), expectedValue.Len())
for i := 0; i < expectedValue.Len(); i++ {
index := expectedValue.Index(i)
Expand Down
71 changes: 71 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ type S6 struct {
unexported string
}

type S7 struct {
Exported Nested
unexported Nested
}

func TestObjectsExportedFieldsAreEqual(t *testing.T) {

intValue := 1
Expand Down Expand Up @@ -402,6 +407,72 @@ func TestEqualExportedValues(t *testing.T) {
+ Exported: (int) 2,
notExported: (interface {}) <nil>`,
},
{
value1: S7{
Nested{[2]int{1, 2}, [2]int{1, 2}},
Nested{[2]int{1, 2}, [2]int{3, 4}},
},
value2: S7{
Nested{[3]int{1, 2, 3}, [2]int{3, 4}},
Nested{[2]int{5, 6}, [2]int{7, 8}},
},
expectedEqual: false,
expectedFail: `
Diff:
--- Expected
+++ Actual
@@ -2,5 +2,6 @@
Exported: (assert.Nested) {
- Exported: ([2]int) (len=2) {
+ Exported: ([3]int) (len=3) {
(int) 1,
- (int) 2
+ (int) 2,
+ (int) 3
},`,
},
{
value1: S7{
Nested{[2]int{1, 2}, [2]int{1, 2}},
Nested{[2]int{1, 2}, [2]int{3, 4}},
},
value2: S7{
Nested{[2]int{1, 3}, [2]int{3, 4}},
Nested{[2]int{5, 6}, [2]int{7, 8}},
},
expectedEqual: false,
expectedFail: `
Diff:
--- Expected
+++ Actual
@@ -4,3 +4,3 @@
(int) 1,
- (int) 2
+ (int) 3
},`,
},
{
value1: S7{
Nested{[2]interface{}{nil, 2}, [2]int{1, 2}},
Nested{[2]int{1, 2}, [2]int{3, 4}},
},
value2: S7{
Nested{[2]interface{}{nil, 2}, [2]int{3, 4}},
Copy link
Collaborator

Choose a reason for hiding this comment

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

As nil is a special value which means anything matches, it would be better to use a concrete value here. Or better: add more test cases to validate that anything matches, not just that nil matches anything.

Nested{[2]int{5, 6}, [2]int{7, 8}},
},
expectedEqual: true,
},
{
value1: S7{
Nested{[2]int{1, 2}, [2]int{1, 2}},
Nested{[2]int{1, 2}, [2]int{3, 4}},
},
value2: S7{
Nested{[2]int{1, 2}, [2]int{3, 4}},
Nested{[2]int{5, 6}, [2]int{7, 8}},
},
expectedEqual: true,
},
}

for _, c := range cases {
Expand Down