Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: negative examples? #29

Open
tlubz opened this issue Feb 22, 2019 · 2 comments
Open

enhancement: negative examples? #29

tlubz opened this issue Feb 22, 2019 · 2 comments

Comments

@tlubz
Copy link

tlubz commented Feb 22, 2019

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

@tom-lord
Copy link
Owner

tom-lord commented Mar 21, 2019

I like the idea, but I suppose this problem should be solved be a separate gem - what do you think?

For example, we could have RegexpNegator.new(regex) #=> negated_regex

Or even, if the problem is feasibly doable, this could be a core ruby method - e.g. Regexp#negate? In which case, this gem could be used as follows:

/hello/.negate.random_example

@tom-lord
Copy link
Owner

tom-lord commented Mar 21, 2019

...But what does a negated /hello/ actually look like? Without using irregular aspects of regex (i.e. look-arounds), that is. I suspect this may closely relate to this problem I encountered with the absence operator.

Edit -- See also: https://bugs.ruby-lang.org/issues/5588

Repository owner deleted a comment from anderstroker Nov 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants