DEV Community

Cover image for Automatically Test Your Regex Without Writing a Single C# Line of Code
Dmitry Dorogoy
Dmitry Dorogoy

Posted on • Edited on

Automatically Test Your Regex Without Writing a Single C# Line of Code

Introduction

If you've ever used regular expressions (regex), you know there's a saying: "Every problem can be solved with regex. But then you have one more problem."

I hope readers can decide for themselves whether to use regular expressions. However, making sure your regex works correctly can be challenging.

In many development teams, there's always someone who asks, "Have you written unit tests for this regex?"

Developers often add comments above their regex with sample strings that should match or not match.

Often, developers will add comments above their regex with sample strings that should match or not match. For example:

// Matches email addresses
// Example: "test@example.com"
var emailRegex = @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$";
Enter fullscreen mode Exit fullscreen mode

However, there’s a hidden problem here: these comments can be misleading. A comment might claim a regex matches an example, but without actual testing, there's no guarantee.

The Ideal Solution

According to TRIZ (Theory of Inventive Problem Solving), the best solution is one where the system works effortlessly, without additional manual input. In our context, the perfect scenario would be a system that automatically tests your regex patterns without needing you to write explicit unit tests.

Presenting the Solution: DimonSmart.RegexUnitTester.TestAdapter

The DimonSmart.RegexUnitTester.TestAdapter is here to save the day! This tool allows you to automatically check your regex patterns against provided samples using custom attributes. No more manual unit test writing or misleading comments—just straightforward and reliable regex testing.

How It Works

This library leverages three main attributes to streamline regex testing:

  1. ShouldMatchAttribute: This ensures that your regex correctly matches the expected data.
  2. ShouldNotMatchAttribute: This confirms that your regex does not match unwanted data.
  3. InfoMatchAttribute: This handles ambiguous cases, allowing further analysis without affecting the overall test outcome.

Code Samples

Here are some examples of how to use these attributes in your code:

[ShouldMatch("test@example.com")]
[ShouldNotMatch("test@example")]
public const string EmailRegex = @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$";

[InfoMatch("123-45-6789", "SSN format")]
public const string SSNRegex = @"^\d{3}-\d{2}-\d{4}$";
Enter fullscreen mode Exit fullscreen mode

With these attributes, you can clearly define which strings should and shouldn’t match your regex patterns. The InfoMatchAttribute is particularly useful for documenting ambiguous cases that need further review.

Conclusion

By using DimonSmart.RegexUnitTester.TestAdapter, you can ensure your regex patterns are accurate and reliable without the hassle of writing manual tests. Give it a try and see how it simplifies your development workflow.

I'm the author of this library. If you have any questions or feedback, please don't hesitate to write me a few words about my library. Happy coding!


You can find more details on installation and usage in the library’s GITHUB.

Top comments (0)