Generate series of values that are dependent on the previous value
See original GitHub issue💬 Question and Help
I have a reducer that maintains a set of numbers stored as an array. I’m trying to generate a series of actions to test the reducer with. (This is a simplified example of the actual problem)
[1, 2]
And there are the following actions:
{ type: "add", value: number }
{ type: "remove", value: number }
I want to generate a series of actions to dispatch, where the actions are dependent on the state after dispatching the previous action. For example, two remove actions for the same value would be invalid, because the first action would have already removed the value.
Valid Examples:
initial state: [1, 2]
// Can remove either of the start values
[{ type: "remove", value: 1 }, { type: "remove", value: 2 }]
// even though 3 is not in the initial state, after it's added it becomes a valid remove target
[{ type: "add", value: 3 }, { type: "remove", value: 3 }]
Invalid Examples:
initial state: [1, 2]
// Should not be able to remove a value not in state
[{ type: "remove", value: 3 }]
// Should not be able to remove a value twice
[{ type: "remove", value: 2 }, { type: "remove", value: 2 }]
I can generate an Arbitrary for valid actions based on a state, I just can’t figure out how to create an array where each element is dependent on the value of the previous.
I’ve poked around Model based testing / Commands, but it seems like it has the same problem, where you must define all your commands up front.
So my question is: Is this use case something the library currently supports? If not, is it something that could be added? I’d be happy to help out if so!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:22 (10 by maintainers)
Top GitHub Comments
I think that I’ll need a more concrete description of the issue to help you a bit more on that point. Maybe a minimal reproduction of the reducer code.
Please note that generating random values within your reducer code (outside of fast-check control) might break the shrink and replay capabilities provided my fast-check (mostly if the randomly generated value affects the way your code behave). You can have a look into https://github.com/dubzzz/fast-check/blob/master/documentation/1-Guides/Tips.md#combine-with-other-faker-or-random-generator-libraries to see how you can combine fast-check with other (seeded) sources of random. Basically you can create a fake
Math.random
to pass to your code with (this one does not break shrinking and replay capabilities):In order to leverage built-in shrinkers within custom arbitraries having poor shrinking capacities you can refer to my example: https://github.com/dubzzz/fast-check/issues/1503#issuecomment-851272804
It will be part of the official documentation as soon as I release v3 (no more need to fromNext/toNext in v3).