Regular Expressions and Unit Tests, A Beautiful Match

valid_url | Drupal APINot to long ago I was writing a complex Regular Expression and ran into a rather difficult problem. This was an update to a regular expression in Drupal and needed to be validated before the bug fix could be committed. The problem was that I couldn't find anyone who was comfortable working through the regex with me. This is where the required tests in Drupal saved the day. The tests provided feedback when things failed and eventually validated the expression when it was right so other developers didn't need to. Let me walk through the process of how this worked.

A Black Box

The first thing I started with was a black box function for the unit tests to call what will eventually contain the regex. The unit tests don't care about the inner working of the function just the inputs and outputs.

I wrote unit tests to pass in a known input and look for the expected output from the function. The unit tests are testing a unit of code, in this case a function, and don't care about the inner workings of that code. They care if the inputs and outputs are as expected.

To make these tests more well rounded I wrote tests that would fail the regex but are very similar to ones that would pass. It's important to test for false positives.

My First Pass At The Regex

Next up I filled out the code in the function with the regex so it would take and input, process it through the regex, and return the output. Then I ran the unit tests and watched about half of them fail. This was really an exciting moment. I was getting feedback that the regex didn't work and had an idea why it wasn't based on the tests that passed and the ones that failed. Finally, constructive feedback!

Iterative Fixes

From here on out it was a matter of altering the regex and running the tests to get feedback. I didn't have to wait for anyone to get back to me. Eventually, the regex passed all the tests and made it into Drupal. And, we don't just think it will work right. We have tests to know it works as expected.

In Action

If you'd like to see this in action you can install Drupal 7 (still in decelopment) and take the testing module for a spin. The function being tested in valid_url and you can find the tests in the source code.