How do you unit test regular expressions?

后端 未结 10 740
长发绾君心
长发绾君心 2020-12-12 11:05

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?

10条回答
  •  难免孤独
    2020-12-12 11:51

    Presumably your regular expressions are contained within a method of a class. For example:

    public bool ValidateEmailAddress( string emailAddr )
    {
        // Validate the email address using regular expression.
        return RegExProvider.Match( this.ValidEmailRegEx, emailAddr );
    }
    

    You can now write tests for this method. I guess the point is is that the regex is an implementation detail - your test needs to test the interface, which in this case is just the validate email method.

提交回复
热议问题