question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
dubzzzcommented, Sep 6, 2019

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):

const mathRandomArb = fc.infiniteStream(fc.double().noBias())
  .noShrink()
  .map(s => {
    const rng = () => s.next().value; // prng like Math.random but controlled by fast-check
    return rng;
  });
0reactions
dubzzzcommented, Aug 28, 2021

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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use mutate() to generate variables that depend on ...
r - How to use mutate() to generate variables that depend on previous row values of other new variables? - Stack Overflow. Stack...
Read more >
Create sequence based on the value of another column
Each sequence number sholud be between 1 and 9999. After 9999, it shoud leap to 1 and continue from there. (CYCLE property of...
Read more >
SEQUENCE function in Excel - auto generate number series
See how to use the Excel SEQUENCE function to create a number series starting at a specific value.
Read more >
Generate a set or sequence without loops - part 1
So the workaround is to use ROW_NUMBER() to generate a contiguous sequence, starting at 1, based on the values in the table.
Read more >
Create number series - Get Digital Help
In this example, I am going to create a repeating number sequence 1, 2, 3, 4. This formula checks if the previous sequence...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found