quicktheories / QuickTheories

Property based testing for Java 8
Apache License 2.0
505 stars 51 forks source link

Generate Strings using Character generator #38

Open doctau opened 6 years ago

doctau commented 6 years ago

Currently the String generators and StringDSL let you specify a minimum and maximum codepoint to restrict the character set, but there is no easy way to do something like generate strings of alpha-numeric characters since that needs non-contiguous character ranges.

One option would be to add a String generator that accepts a Gen. Potentially betweenCodePoints could call through to the new method.

With the DSL changed, I'm thinking of something like

strings().ofCharacters(characters('0', '9').mix(characters('A', 'Z'))).withLength(8)

hyperpape commented 4 years ago

I started hacking together an idea for this, before checking and seeing it had been proposed. My idea was slightly different. I modified StringGeneratorBuilder to store a list of ranges of charpoints, instead of just one min/max pair. Then I added a set of methods to include or exclude ranges:

public StringGeneratorBuilder excludingCodePoint(int codePoint)

public StringGeneratorBuilder excludingRange(int minCodePoint, int maxCodePoint)

public StringGeneratorBuilder includingCodePoint(int codePoint)

public StringGeneratorBuilder includingRange(int minCodePoint, int maxCodePoint)

With that in place, I added a method to the Strings class that mixes the ranges together (automatically determining proportions for you).

static Gen<String> fromRanges(List<Pair<Integer, Integer>> ranges, int minLength, int maxLength)

I haven't finished writing all the methods, or tested it, but I have enough code to be confident it should work and I'd be happy to produce a pull request if you think this is an interesting direction.