Hacker News new | past | comments | ask | show | jobs | submit login

PBT libraries usually have some support for filtering ("if you generate a value x such that P(x), discard it and try again"), but it's frowned upon. You should only use it when you're relatively certain it's not going to happen very often. But, it's an option.

You could try to somehow be clever about how you generate the inputs: ("A" + nonempty string | "B-Z" ++ possibly empty string)

Or you could generate a string, check it for "A"-ness, and if true, fix it before running the actual test by appending some random non-empty string after it.

The three approaches might look like (in pseudocode)

1. Filtering

gen.string().filter(s => s !== "A")

2. "Smart" construction

gen.oneOf([ "A" + gen.string().nonempty(), gen.charBetween("B","Z") + gen.string() ])

3. Naive generation + fix

gen.string().andThen(s => (s === "A") ? s + gen.string().nonempty() : s)




Thanks. Why is filtering frowned upon? It appears to me the most succinct and elegant description of what I want looking above.


It slows down the testing. All the invalid inputs are generated and then discarded, so it’s much more efficient to come up with a construct that succeeds all the time or at least most of the time.

If you ask for an integer but only want odds (as an example), 50% of the generated values will be tossed out with filtering. If you instead take every integer and make them odd (double and add 1, for instance) then all the generated inputs will be usable.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: