Should I write unit tests for complex regular expressions in my application?
- On the one hand: they are easy to test because input and output format is often simple and well-defined, and they can often become so complex so tests of them specifically are valuable.
- On the other hand: they themselves are seldom part of the interface of some unit. It might be better to only test the interface and do that in a way that implicitly tests the regexes.
But as internal components regexes have a few special characteristics:
- A single line regex can be really complex without really being a separate module.
- Regexes map input to output without any side effects and hence are really easy to test separately.