NullableGenerator(Generator<T> delegate) { super(delegate.types()); this.delegate = delegate; }
/** * <p>Tells whether this generator is allowed to participate in the * {@link Shrink} process for the given "larger" value.</p> * * <p>Unless overridden, the only criterion for whether a generator is * allowed to participate in shrinking a value is if the value can be * safely cast to the type of values the generator produces.</p> * * @param larger the "larger" value * @return whether this generator can participate in "shrinking" the larger * value */ public boolean canShrink(Object larger) { return types().get(0).isInstance(larger); }
/** * <p>Attempts to "narrow" the given object to the type this generator * produces.</p> * * <p>This is intended for use only by junit-quickcheck itself, and not by * creators of custom generators.</p> * * @param wider target of the narrowing * @return narrowed the result of the narrowing * @throws ClassCastException if the narrowing cannot be performed */ protected final T narrow(Object wider) { return types().get(0).cast(wider); }
private void registerTypes(Generator<?> generator) { for (Class<?> each : generator.types()) registerHierarchy(each, generator); }
private void ensureCorrectType(Generator<?> generator) { org.javaruntype.type.Type<?> parameterTypeToken = Types.forJavaLangReflectType(type(), typeVariables); for (Class<?> each : generator.types()) { if (!maybeWrap(parameterTypeToken.getRawClass()).isAssignableFrom(maybeWrap(each))) { throw new IllegalArgumentException( format( EXPLICIT_GENERATOR_TYPE_MISMATCH_MESSAGE, each, From.class.getName(), parameterName)); } } }
@Test public void shrinkingChoosesAComponentCapableOfShrinkingTheValue() { stub(first.canShrink(7)).toReturn(true); stub(second.canShrink(7)).toReturn(false); stub(third.canShrink(7)).toReturn(true); when(first.types()).thenReturn(singletonList(Integer.class)); when(first.doShrink(random, 7)).thenReturn(asList(3, 6)); when(random.nextInt(9)).thenReturn(1); assertEquals(asList(3, 6), composite.shrink(random, 7)); verify(first, atLeastOnce()).doShrink(random, 7); } }
/** * <p>Attempts to "narrow" the given object to the type this generator * produces.</p> * * <p>This is intended for use only by junit-quickcheck itself, and not by * creators of custom generators.</p> * * @param wider target of the narrowing * @return narrowed the result of the narrowing * @throws ClassCastException if the narrowing cannot be performed */ protected final T narrow(Object wider) { return types().get(0).cast(wider); }
/** * <p>Tells whether this generator is allowed to participate in the * {@link Shrink} process for the given "larger" value.</p> * * <p>Unless overridden, the only criterion for whether a generator is * allowed to participate in shrinking a value is if the value can be * safely cast to the type of values the generator produces.</p> * * @param larger the "larger" value * @return whether this generator can participate in "shrinking" the larger * value */ public boolean canShrink(Object larger) { return types().get(0).isInstance(larger); }
private void registerTypes(Generator<?> generator) { for (Class<?> each : generator.types()) registerHierarchy(each, generator); }
private void ensureCorrectType(Generator<?> generator) { org.javaruntype.type.Type<?> parameterTypeToken = Types.forJavaLangReflectType(type(), typeVariables); for (Class<?> each : generator.types()) { if (!maybeWrap(parameterTypeToken.getRawClass()).isAssignableFrom(maybeWrap(each))) { throw new IllegalArgumentException( format( EXPLICIT_GENERATOR_TYPE_MISMATCH_MESSAGE, each, From.class.getName(), parameterName)); } } }