pholser/junit-quickcheck

StringGenerator always returns empty string

Closed this issue · 2 comments

Apologies for raising this as an issue, as the only issue is likely to be my understanding or lack thereof, but I have a problem with a custom generator. Within the generator generate method I use gen().type(String.class) to acquire a generator for Java Strings, and then call generate (passing in the SourceOfRandomness and GenerationStatus that were passed to my generate method). However the String generator always returns an empty string. Is this expected?

My generator looks something like this:

public class LimitGenerator extends Generator<Model.Limit> {

    public LimitGenerator() {
        super(Model.Limit.class);
    }

    @Override
    public Model.Limit generate(SourceOfRandomness sor, GenerationStatus gs) {
        Generator<String> z = gen().type(String.class);
        String s1 = z.generate(sor, gs);
        String s2 = z.generate(sor, gs);
        String s3 = z.generate(sor, gs);
        String s4 = z.generate(sor, gs);

        ...
    }
}

Here s1 - s4 are all empty strings. It's invoked from a test which looks something like:

@RunWith(JUnitQuickcheck.class)
public class JsonTest {
    @Property
    public void roundTripLimit(Model.Limit limit)  {
    ...

What am I doing wrong?

Actually, it looks like it only does this on the first pass. For my tests I actually need non-empty strings, so I guess I need to somehow constrain the string generation to prevent empty strings.

Not sure if this the most elegant way, but this seems to work:

gen().type(String.class).filter(s -> !s.isEmpty())