-
Notifications
You must be signed in to change notification settings - Fork 31
Open
Labels
Description
Is there a way to generate counter-examples to a regex pattern? This would be incredibly useful for writing tests for format validators. Think validates_format_of
for shoulda-matchers.
Conceptually, it would enumerate all strings that don't match the pattern.
Ideal behavior:
/a+/.counter_examples #=> ["", "b", "c", "d", ...]
/hello/.counter_examples #=> ["", "a", "b", "c", ...]
/.*/.counter_examples #=> [] (or nil)
it seems like this is computationally possible at least. the complement of a regular language is also regular, which means that you can enumerate the counter-examples by "negating" the regular expression's deterministic finite automaton representation, and then enumerating examples from that.
https://math.stackexchange.com/questions/685182/complement-of-a-regular-expression
ric2b