Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

regex - How do you unit test regular expressions?

I'm new to TDD, and I find RegExp quite a particular case. Is there any special way to unit test them, or may I just treat them as regular functions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should always test your regexen, much like any other chunk of code. They're at the most simple a function that takes a string and returns a bool, or returns an array of values.

Here are some suggestions on what to think about when it comes to designing unit tests for regexen. These are not not hard and fast prescriptions for unit test design, but some guidelines to shape your thinking. As always, weigh the needs of your testing versus cost of failure balanced with the time required to implement them all. (I find that 'implementing' the test is the easy part! :-] )

Points to consider:

  • Think of every group (the parentheses) as a curly brace.
  • Think of every | as a condition. Make sure to test for each branch.
  • Think of every modifier (*, +, ? ) as a different path.
  • (side note to the above: remember the difference between *, +, ? and *?, +?, and ??.)
  • for d, s, w, and their negations, give several in each range a try.
  • For * and +, you need to test for the 'no value', 'one of', and 'one or more' for each.
  • For important 'control' characters (eg, strings in the regex you look for) test to see what happens if they show up in the wrong places. This may surprise you.
  • If you have real world data, use as much of it as you can.
  • If you don't, make sure to test both the simple and complex forms that should be valid.
  • Make sure to test what regex control characters do when inserted.
  • Make sure to verify that the empty string is properly accepted/rejected.
  • Make sure to verify that a string of each of the different kind of space characters are properly accepted or rejected.
  • Make sure that proper handling of case insensitivity is done (the i flag). This has bit me more times than almost anything else in text parsing (other than spaces).
  • If you have the x, m or s options, make sure you understand what they do and test for it (the behavior here can be different)

For a regex that returns lists, also remember:

  • Verify that the data you expect is returned, in the right order, in the right fields.
  • Verify that slight modifications do not return good data.
  • Verify that mixed anonymous groups and named groups parse correctly (eg, (?<name> thing1 ( thing2) )) - this behavior can be different based on the regex engine you're using.
  • Once again, give lots of real world trials.

If you use any advanced features, such as non-backtracking groups, make sure you understand completely how the feature works, and using the guidelines above, build example strings that should work for and against each of them.

Depending on your regex library implementation, the way groups are captured may be different as well. Perl 5 has a 'open paren order' ordering, C# has that partially except for named groups and so on. Make sure to experiment with your flavor to know exactly what it does.

Then, integrate them right in with your other unit tests, either in their own module or alongside the module that contains the regex. For particularly nasty regexen, you may find you need lots and lots of tests to verify that the pattern and all the features you use are correct. If the regex makes up a large (or nearly all) of the work that the method is doing, I will use the advice above to fashion inputs to test that function and not the regex directly. That way, if later you decide that the regex is not the way to go, or you want to break it up, you can capture the behavior the regex provided without changing the interface - ie, the method that invokes the regex.

As long as you really know how a regex feature is supposed to work in your flavor of regex, you should be able to develop decent test cases for it. Just make sure you really, really, really do understand how the feature works!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...