Add wantCase and isCase to Expect module#526
Merged
Merged
Conversation
Collaborator
|
Hmm. I agree keeping these as functions with quotations over methods with ReflectionDefinition feels like the right call. I'm ok with merging this, unless you had some ideas you wanted to discuss? |
Collaborator
|
It's worth noting that F# unions now have built-in properties for determining if a value belongs to a certain case. An existing alternative to type Answer = Yes | No
...
let actual = Answer.Yes
Expect.isTrue actual.IsYes "should be yes"This doesn't help with unwrapping, and I think there's a style/readability argument for |
Contributor
Author
Right - and the main argument IMO is that you get nicer error messages out of the test with I'm happy with it as is if nobody has any objections or ideas for further improvement. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds two helper functions (
wantCaseandisCase) to simplify extracting and matching a case of a discriminated union, along with flipped versions. This is accomplished via code quotations, similar to Argu's approach inGetResultfor matching and extracting case data.This lets you write things like:
instead of the cumbersome:
I have been using this in my own test suite recently and figured this could be a useful addition to the library.
NOTE: it would be nice to be able to use
[<ReflectionDefinition>]on it so that the user doesn't have to wrap it in an explicit quotation (i.e.SomeCaseinstead of<@ SomeCase @>), but unfortunately this is only supported in methods, not functions, and I didn't want to break from the established pattern. But I am open to ideas.