pholser/junit-quickcheck

Constrain the length of a generated String via Size annotation

Bill opened this issue · 4 comments

Bill commented

I saw the answer to #149 but I don't feel like it's a solution to my (simpler) problem. I simply want to generate Strings with length in a range. It seems obvious to me that the Size annotation should operate on String parameters.

But something like this:

@RunWith(JUnitQuickcheck.class)
public class QC1 {

  @Property(trials=100, shrink = false)
  public void fooProperty(@Size(min=1, max=2) final String s) {
    assertThat(s.length()).isLessThan(3);
    assertThat(s.length()).isGreaterThan(0);
  }

}

results in:

None of the candidate generators [com.pholser.junit.quickcheck.generator.java.lang.Encoded, com.pholser.junit.quickcheck.generator.java.lang.StringGenerator] understands all of the configuration annotations [com.pholser.junit.quickcheck.generator.Size]
com.pholser.junit.quickcheck.generator.GeneratorConfigurationException: None of the candidate generators [com.pholser.junit.quickcheck.generator.java.lang.Encoded, com.pholser.junit.quickcheck.generator.java.lang.StringGenerator] understands all of the configuration annotations [com.pholser.junit.quickcheck.generator.Size]

Am I not understanding the spirit of QuickCheck? Must I really resort to a custom generator for this?

@Bill nah, we can certainly make string generators honor @Size.

@Bill Let me know how this change suits your needs.

@Bill When you get a moment, would you have a look at the above change, and close this issue if the change meets your needs? Thanks!

Closing, as fix should be on HEAD. Re-open if it doesn't work for you.