-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
withSamples() broken? #57
Comments
I’ll later check if there’s a bug in withSamples(). However, 0 will be generated anyway as one of the first examples if it is in the range of allowed numbers.
… Am 29.05.2019 um 13:53 schrieb Oliver Becker ***@***.***>:
Testing Problem
We want to generate a sequence of BigIntegers, however since we encountered already a problem with 0.00 we would like to add this value as a special case to all our property based tests.
From the documentation we thought that using samples is the way to go:
@provide
Arbitrary<BigDecimal> decimals() {
return Arbitraries.bigDecimals().withSamples(new BigDecimal("0.00"));
}
However, when using this arbitrary we only get the single sample value and no random value at all:
@Property
void ***@***.***("decimals") BigDecimal v) {
System.out.println(v);
}
This will output
0.00
0.00
0.00
0.00
0.00
etc...
What have we done wrong?
I tried to debug this, but all I found out was that the WithSamplesGenerator will be newly created for every new property, thus starting again with the first sample value.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
I can replicate the problem. Thanks for catching it. |
After rethinking the problem I've come to the conclusion that - as strange as it might sound at first - it works as designed. Bear with me, I'll explain why and what you can do to cover your intention... Why it works as designedThe declarative way to combine, constrain and modify Arbitrary instances is supposed to specify value generators. And, that's the crucial part, generators are created freshly for each try, i.e. for each invocation of a property/test method. That's why
will always generate
will generate a list of three numbers, the first element of which would always be 0.00 and the others random. TLDR: What you can doI see 4 options 1. Do nothingBy default edge cases for common generators, e.g. BigDecimal generator, are injected with a certain probability. As a response to this issue report I raised the probability for 0 to be injected somewhat. This will take effect starting in version 1.1.5. Moreover, whenever jqwik discovers a failing property shrinking usually will try 0 as one of the first options and shrink to it. 2. Add an explicit example test
3. Add a data-driven property
See https://jqwik.net/docs/current/user-guide.html#data-driven-properties for more details about that feature. 4. Open a feature request to make option 3 a bit easierSomething like
I hope I could explain and give reasonable options. Feel free to reopen the issue if anything's not clear. |
One note from me, javadoc for
It does evoke in me, that before generating some random things, provided sample values will be tested at first. This does not happen. All tests use just single value (first from withSamples list). I would suggest to change javadoc to something like this:
Second part - enhancement proposal/request. I am not very comfortable with provided solutions. I really want my arbitrary string generator for names to generate arbitrary random string, with some hand-crafted samples with sql injections for example. I expect that it will use samples provided by me with some probability. My approach to this problem consist of this: Arbitraries.oneOf(
Arbitraries.strings().numeric()
.alpha()
.withChars('.')
.ofMinLength(6)
.ofMaxLength(16),
Arbitraries.samples(";-- drop all tables now();")
); Using I would like to see some easiest (fluent) way of achieving this behavior. For example method Would it make any use for someone? Does it reasonably fit to jqwik api? |
"It does evoke in me, that before generating some random things, provided sample values will be tested at first. This does not happen. All tests use just single value (first from withSamples list)." Well, it does happen if ENOUGH EXAMPLES ARE BEING CREATED - e.g. when you use it to fill a list or a set. That said and given the history of it being misunderstood I wonder if As for |
Hmmm, ok, deprecating (later removing) Introducing If you see more such "shortcut" utility methods, and you see where to put them, I can have a look at code and migrate there import net.jqwik.api.Arbitraries;
import net.jqwik.api.Arbitrary;
import net.jqwik.api.Tuple;
public class JqwikUtils {
/**
* Returned {@link Arbitrary} will use first parameter in 41 cases and in case 42, will randomly choose from
* provided samples.
*
* @param arbitrary {@link Arbitrary} to use for value generation in general
* @param samples list of samples to be randomly injected with 1:42 ratio
* @param <T> type of {@link Arbitrary}
* @return newly created {@link Arbitrary} with "sometimes" injected sample value.
*/
public static <T> Arbitrary<T> withInjectedSamples(Arbitrary<T> arbitrary, T... samples) {
return Arbitraries.frequencyOf(
Tuple.of(42, arbitrary),
Tuple.of(1, Arbitraries.of(samples))
);
}
} |
In my experience people develop their individual styles and preferences of how to combine arbitraries. jqwik should enable many or most styles but not necessarily incorporate each variant into its core. Thus I'd rather defer such convenience methods until there's a stronger use case. |
BTW, |
Testing Problem
We want to generate a sequence of BigDecimals, however since we encountered already a problem with 0.00 we would like to add this value as a special case to all our property based tests.
From the documentation we thought that using samples is the way to go:
However, when using this arbitrary we only get the single sample value and no random value at all:
This will output
What have we done wrong?
I tried to debug this, but all I found out was that the
WithSamplesGenerator
will be newly created for every new property, thus starting again with the first sample value.The text was updated successfully, but these errors were encountered: