Add a specimen builder to generate enum values from a subset #1219
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.
I'm working on a code base at the moment where it would be useful to be able to restrict the values that are generated for a particular type of
enum
to a subset of the available values. If I use theTriState
enumeration as an example, I might want to exclude the valueTriState.First
, or alternatively to include only the valuesTriState.Second
andTriState.Third
.Although it is already possible to create a customization with functionality similar to that implemented here I don't think it is particularly obvious how to do so. This would make it relatively easy to create a customization that restricts enum values to a subset, as follows.
Admittedly, having to do this may be indicative of a code smell in certain circumstances but I still think it could be useful – particularly for those of use that have to work on existing/legacy code bases from time to time, where the abuse of enums can be quite common. Perhaps it may also be useful to exclude the zero value from an enum, which is often the equivalent of None and may not be expected to occur except in certain conditions.
I'll be using this code, or some variation of it, elsewhere so it won't go to waste if rejected but I thought it might be worth seeing if you think it's worth including in AutoFixture. I didn't think it was worth opening an issue for this so please accept my apologies if I should have done so. I'd be most grateful for your thoughts.