Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared random value between providers #309

Closed
dhoepelman opened this issue Feb 28, 2022 · 4 comments
Closed

Shared random value between providers #309

dhoepelman opened this issue Feb 28, 2022 · 4 comments

Comments

@dhoepelman
Copy link

Testing Problem

I have a domain where a lot of providers depend on some shared value, let's say it's the current timestamp.
Currently it's possible to have a fixed value and share this, but not test random different values.

class MyDomain extends DomainContextBase {
  private final Instant NOW = /* ... */

  @Provide
  Arbitrary<Instant> past() {
    return DateTimes.between(Instant.MIN, NOW);
  }

  @Provide
  Arbitrary<Instant> future() {
    return DateTimes.between(NOW, Instant.MAX);
  }
}

@Domain(MyDomain.class)
class MyPropertyTests {
   @Property
   void myProperty(List<@From("past") Instant> pastTimestamps, List<@From("future") Instant> futureTimestamps) {
      // ...
   }
}

In this example there are trivial ways to not need this (such as generating 1 list and partitioning), but this is an example of the base case where you have dependencies between providers.

Suggested Solution

It would be great if there was a way to have a random value from an Arbitrary, which is available and shared between multiple other @Providers.

One way I see that could be achieved is by allowing a @Domain to be initialized with a number of values from a Arbitrary.
This value can be random during normal runs, but should be fixed once a failure is detected and shrinking starts.

Discussion

There might already be a good way to do this, but I couldn't distill it from the documentation.
Lifecycle hooks seem related to this, but I don't think this use-case can be achieved with them.

@jlink
Copy link
Collaborator

jlink commented Feb 28, 2022

This issue might be related or even the same feature request.

@jlink
Copy link
Collaborator

jlink commented Feb 28, 2022

The standard solution for this problem is to provide related values in a single collecting object (self-made or some container like Tuple) and use flat mapping to derive generators from the common dependency. That sometimes requires some boilerplating, though.

@dhoepelman
Copy link
Author

You linked this issue, but I see #294 now. That's indeed the same feature request, thanks!

@jlink
Copy link
Collaborator

jlink commented Feb 28, 2022

You linked this issue

It’s 2022 and web editors are still a PITA ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants