Skip to content

Commit

Permalink
Made Arbitrary provider method matching looser.
Browse files Browse the repository at this point in the history
Tackling bug #599
  • Loading branch information
jlink committed Nov 28, 2024
1 parent 011e356 commit 48cda86
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,7 @@ private Optional<Method> findProviderMethodByName(String generatorToFind, TypeUs
return provideAnnotation.map(Provide::value).orElse("");
};

TypeUsage effectiveTargetType = targetType.isTypeVariableOrWildcard() ? targetType : TypeUsage.wildcard(targetType);
TypeUsage expectedReturnType = TypeUsage.of(
Arbitrary.class,
effectiveTargetType
);

TypeUsage expectedReturnType = TypeUsage.of(Arbitrary.class);
return findGeneratorMethod(generatorToFind, contextInstance().getClass(), Provide.class, generatorNameSupplier, expectedReturnType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public String toString() {
}
}

private class ThingArbitrary extends ArbitraryDecorator<Thing> {

@Override
protected Arbitrary<Thing> arbitrary() {
return Arbitraries.just(new Thing());
}
}



@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Provide
Expand Down Expand Up @@ -208,6 +218,14 @@ void providerMethodCanHaveWildcardArbitrary() {
assertThingArbitrary(arbitraries.iterator().next());
}

@Example
void providerMethodCanReturnArbitrarySubtype() {
PropertyMethodArbitraryResolver provider = getResolver(WithNamedProviders.class);
MethodParameter parameter = getParameter(WithNamedProviders.class, "thingFromArbitrarySubtype");
Set<Arbitrary<?>> arbitraries = provider.forParameter(parameter);
assertThingArbitrary(arbitraries.iterator().next());
}

@Example
void providerMethodCanHaveTypeUsageParameter() {
PropertyMethodArbitraryResolver provider = getResolver(WithNamedProviders.class);
Expand Down Expand Up @@ -240,6 +258,13 @@ void findGeneratorByName() {
assertThingArbitrary(arbitraries.iterator().next());
}

@Example
void failWhenProviderMethodDoesNotReturnArbitrary() {
PropertyMethodArbitraryResolver provider = getResolver(WithNamedProviders.class);
MethodParameter parameter = getParameter(WithNamedProviders.class, "providerDoesNotReturnArbitrary");
assertThat(provider.forParameter(parameter)).isEmpty();
}

@Example
void findGeneratorBySupplier() {
PropertyMethodArbitraryResolver provider = getResolver(WithNamedProviders.class);
Expand Down Expand Up @@ -402,7 +427,7 @@ Arbitrary<Thing> aThingy() {
}

@Property
boolean thingFromWildcard(@ForAll("aThingFromWildcard") Object aThing) {
boolean thingFromWildcard(@ForAll("aThingFromWildcard") Thing aThing) {
return true;
}

Expand All @@ -411,6 +436,16 @@ Arbitrary<?> aThingFromWildcard() {
return Arbitraries.just(new Thing());
}

@Property
boolean thingFromArbitrarySubtype(@ForAll("aThingFromArbitrarySubtype") Thing aThing) {
return true;
}

@Provide
ThingArbitrary aThingFromArbitrarySubtype() {
return new ThingArbitrary();
}

@Property
boolean thingBySupplier(@ForAll(supplier = ThingSupplier.class) Thing aThing) {
return true;
Expand Down Expand Up @@ -514,6 +549,16 @@ Arbitrary<Thing> aThing() {
return Arbitraries.just(new Thing());
}

@Property
boolean providerDoesNotReturnArbitrary(@ForAll("notAnArbitrary") Thing aThing) {
return true;
}

@Provide
Thing notAnArbitrary() {
return new Thing();
}

@Group
class NestedWithNamedProviders {
@Property
Expand Down

0 comments on commit 48cda86

Please sign in to comment.