Skip to content

enhancement: negative examples?Β #29

@tlubz

Description

@tlubz

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions