jqwik-team / jqwik

Property-Based Testing on the JUnit Platform
http://jqwik.net
Eclipse Public License 2.0
568 stars 65 forks source link

Generic @Provide methods are sensitive to the type variables' names #580

Closed lcahmu closed 3 months ago

lcahmu commented 3 months ago

Testing Problem

@Provide methods with generic type parameters are apparently sensitive to the name of the type parameter. For example, the following fails at runtime under 1.8.5 and 1.9.0-SNAPSHOT (4d419a3). A more realistic scenario would be for a domain-specific wrapper type rather than Optional, but Optional is convenient for demonstration purposes.

@Property
public <T> void property(@ForAll("provided") Optional<T> optional) { } 

@Provide
public <R> Arbitrary<Optional<R>> provided(@ForAll Optional<R> optional) {
    return Arbitraries.just(optional);  // real usage would do something more substantial here
}
Cannot find an Arbitrary [provided] for Parameter of type [@net.jqwik.api.ForAll(value="provided", supplier=net.jqwik.api.ArbitrarySupplier.NONE.class) Optional<T>]

Updating the type variable names from either method to match the other works around the error:

  @Provide
- public <R> Arbitrary<Optional<R>> provided(@ForAll Optional<R> value) {
+ public <T> Arbitrary<Optional<T>> provided(@ForAll Optional<T> value) {

This can get pretty clunky in cases where separate parameters are necessary. For example:

@Property
public <T, U> void property(@ForAll Function<@From("providedT") Optional<T>, @From("providedU") Optional<U>> fn) { }

@Provide
public <T> Arbitrary<Optional<T>> providedT(@ForAll Optional<T> value) { ... }

// Identical to `providedT`, but required to handle necessarily-different type variable names
@Provide
public <U> Arbitrary<Optional<U>> providedU(@ForAll Optional<U> value) {
    return providedT(value);
}

Suggested Solution

The name of the type parameter should not be considered for matching purposes.

jlink commented 3 months ago

It's the same piece of code I had to touch for #575. Hm, I'm wondering what my thinking at the time was. Hopefully, I was just being too lazy to jump through all the hoops as I do now.

jlink commented 3 months ago

Could be fixed with https://github.com/jqwik-team/jqwik/commit/ba4c9cf74d09a58ce98d1cec8010152079226cbe

Has been published as 1.9.0-SNAPSHOT

jlink commented 3 months ago

Please re-open if issue still occurs