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

Allow WithTransform()'s function to accept a nil value #422

Merged
merged 1 commit into from Feb 26, 2021

Conversation

ghost
Copy link

@ghost ghost commented Feb 25, 2021

Add a unit test to verify this use case.

Add a unit test to verify this use case.
@ghost ghost mentioned this pull request Feb 25, 2021
@onsi
Copy link
Owner

onsi commented Feb 26, 2021

awesome, thanks!

@onsi onsi merged commit b75d2f2 into onsi:master Feb 26, 2021
@ghost ghost deleted the fix_with_transform_nil_param branch February 26, 2021 05:00
Comment on lines +53 to +56
} else {
// make a nil value of the expected type
param = reflect.New(m.transformArgType).Elem()
}
Copy link
Contributor

@cbandy cbandy Feb 26, 2021

Choose a reason for hiding this comment

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

This is no longer checking that the actual = nil is appropriate for the transform function. This incorrectly succeeds:

gomega.WithTransform(
	func(int) bool { return true },
	gomega.BeTrue(),
).Match(nil)

I recommend the following:

	var param reflect.Value
	if actual != nil && reflect.TypeOf(actual).AssignableTo(m.transformArgType) {
		// The original type of actual is compatible with the transform argument.
		param = reflect.ValueOf(actual)

	} else if actual == nil && m.transformArgType.Kind() == reflect.Interface {
		// The original type of actual is unknown, so there's no way to make its
		// reflect.Value. Create a nil of the transform argument, which is known.
		param = reflect.Zero(m.transformArgType)

	} else {
		return false, fmt.Errorf("Transform function expects '%s' but we have '%T'", m.transformArgType, actual)
	}

Copy link
Owner

Choose a reason for hiding this comment

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

thanks @cbandy - are you up for submitting this as a PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, no problem. Submitted #423.

This was referenced Mar 8, 2021
This was referenced Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants