| the Compartmented Robust Posix C++ Unit Test system |
|
|---|
Match a string against a regular expression.
Used in: Template parameter to
crpcut::match<matcher>(...) in either an
ASSERT_PRED(pred, ...)
check
or as matcher argument to ASSERT_THROW(expr, exc_type, matcher?) or VERIFY_THROW(expr, exc_type, matcher?).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
patternThe pattern string that is a regular expression. The pattern can be either a C-string or a std::string.
![]() | Note |
|---|---|
| Beware of C/C++ escape rules when regular expressions contain “\”. |
flagsA combination using the bit-wise or (|) operator using zero or more of:
Flag
iIgnore case when matching. Equal to
REG_ICASE in
regcomp().
![]() | Note |
|---|---|
crpcut::regex::i
does not take any locale into consideration. |
eUse extended regular expressions. Equal to
REG_EXTENDED in
regcomp()
mPattern 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" --------------------------------------------------------------
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 signed char valid IP address
crpcut::match<crpcut::regex>("^([0-9]{1,3}\\.){3}[0-9]{1,3}.*") :
"192.168.0.1 is signed char valid IP address" does not match
-------------------------------------------------------------------------------
===============================================================================
2 test cases selected
Total : Sum Critical Non-critical
PASSED : 1 1 0
FAILED : 1 1 0