pholser / junit-quickcheck

Property-based testing, JUnit-style
MIT License
955 stars 121 forks source link

Constrain the length of a generated String via Size annotation #472

Closed Bill closed 9 months ago

Bill commented 1 year ago

I saw the answer to https://github.com/pholser/junit-quickcheck/issues/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?

pholser commented 1 year ago

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

pholser commented 1 year ago

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

pholser commented 12 months ago

@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!

pholser commented 9 months ago

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