Skip to content

Commit

Permalink
Document cloneMethod and stateful values
Browse files Browse the repository at this point in the history
Fixes #228
  • Loading branch information
dubzzz committed Nov 3, 2018
1 parent 34e7062 commit c7abd33
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions documentation/AdvancedArbitraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,25 @@ Some frameworks actually use this approach. Unfortunately using this approach ma
Let's imagine you are using a `oneof(integer(0, 10), integer(20, 30))` relying on the `DummyArbitrary<number>` above. As soon as you have generated a value - a `number` - you cannot call shrink anymore as you do not know if it has been produced by `integer(0, 10)` or `integer(20, 30)` - in this precise case you can easily infer the producer.

For this reason, the `shrink` method is not part of `Arbitrary` in fast-check but is part of the values instantiated by `generate`.

### Cloneable

Any generated value having a key for `fc.cloneMethod` would be handled a bit differently during the execution. Indeed those values explicitly requires to be cloned before being transmitted again to the predicate.

Cloneable values can be seen as stateful values that would be altered as soon as we use them inside the predicate. For thsi precise reason they have to be recreated if they need to be used inside other runs of the predicate.

Example of usages:
- `fc.context`: is a stateful instance that gathers all the logs for a given predicate execution. In order to provide only the logs linked to the run itself it has to be cloned between all the runs
- stream structure

Example of a stream arbitrary:

```typescript
const streamInt = fc.nat()
.map(seed => {
return Object.assign(
new SeededRandomStream(seed),
{ [fc.cloneMethod]: () => new SeededRandomStream(seed) }
);
});
```

0 comments on commit c7abd33

Please sign in to comment.