the Compartmented Robust Posix C++ Unit Test system

crpcut::regex

Match a string against a regular expression.

Used in: Template parameter to crpcut::match<matcher>(...) in an ASSERT_PRED(pred, ...) check.The predicate accepts either a C-string or a std::string as value to match against the regular expression.

crpcut::match<crpcut::regex>(pattern, flags = 0)

Parameters

pattern

The pattern string that is a regular expression. The pattern can be either a C-string or a std::string.

[Note]Note
Beware of C/C++ escape rules when regular expressions contain \.

flags

A combination using the bit-wise or (|) operator using zero or more of:

Flag

crpcut::regex::i

Ignore case when matching. Equal to REG_ICASE in regcomp().

[Note]Note
crpcut::regex::i does not take any locale into consideration.

crpcut::regex::e

Use extended regular expressions. Equal to REG_EXTENDED in regcomp()

crpcut::regex::m

Pattern is multi line. Equal to REG_NEWLINE in regcomp()

Example: the test program

     
     #include <crpcut.hpp>
     #include <string>
     
     std::string name()
     {
       return "192.168.0.1 is a valid IP address";
     }
     
     TEST(fail_non_extended)
     {
       ASSERT_PRED(crpcut::match<crpcut::regex>("^([0-9]{1,3}\\.){3}[0-9]{1,3}.*"),
                   name());
     }
     
     TEST(pass_extended)
     {
       ASSERT_PRED(crpcut::match<crpcut::regex>("^([0-9]{1,3}\\.){3}[0-9]{1,3}.*",
                                                crpcut::regex::e),
                   name());
     }
     
     int main(int argc, char *argv[])
     {
       return crpcut::run(argc, argv);
     }

      

fails one test:


     FAILED: fail_non_extended
     phase="running"  --------------------------------------------------------------
     /home/bjorn/devel/crpcut/doc-src/samples/regex.cpp:39
     ASSERT_PRED(crpcut::match<crpcut::regex>("^([0-9]{1,3}\\.){3}[0-9]{1,3}.*"), name())
       param1 = 192.168.0.1 is a valid IP address
     crpcut::match<crpcut::regex>("^([0-9]{1,3}\\.){3}[0-9]{1,3}.*") :
     did not match
     
     -------------------------------------------------------------------------------
     ===============================================================================
     Total 2 test cases selected
     UNTESTED : 0
     PASSED   : 1
     FAILED   : 1