From 3382b35975596fc32fd56aa988aba49c1d29de57 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Mon, 30 Aug 2021 12:09:55 -0700 Subject: [PATCH] testStrMatch() portability --- src/pvxs/unittest.h | 4 +++- src/unittest.cpp | 34 +++++++++++++++++++++++++++++++--- test/testutil.cpp | 4 +++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/pvxs/unittest.h b/src/pvxs/unittest.h index 4849001..c1a2558 100644 --- a/src/pvxs/unittest.h +++ b/src/pvxs/unittest.h @@ -79,7 +79,8 @@ public: } //! Override current pass/fail result if input matches a regular expression - //! @since 0.1.1 + //! @since UNRELEASED Expression syntax is POSIX extended. + //! @since 0.1.1 Added testCase& setPassMatch(const std::string& expr, const std::string& inp); //! Append to message @@ -288,6 +289,7 @@ testCase testThrowsMatch(const std::string& expr, FN fn) //! Macro which asserts that STR matches the regular expression EXPR //! Evaluates to a pvxs::testCase +//! @since UNRELEASED Expression syntax is POSIX extended. //! @since 0.1.1 #define testStrMatch(EXPR, STR) ::pvxs::detail::_testStrMatch(#EXPR, EXPR, #STR, STR) diff --git a/src/unittest.cpp b/src/unittest.cpp index 6ada8d5..f4d9415 100644 --- a/src/unittest.cpp +++ b/src/unittest.cpp @@ -4,7 +4,18 @@ * in file LICENSE that is included with this distribution. */ -#include +#include "pvxs/version.h" + +#if !defined(GCC_VERSION) || GCC_VERSION>VERSION_INT(4,9,0,0) +# include + +#else +// GCC 4.8 provides the regex header and symbols, but with a no-op implementation +// so fill in the gap with POSIX regex +# include +# include +# define USE_POSIX_REGEX +#endif #include @@ -96,15 +107,33 @@ testCase::~testCase() testCase& testCase::setPassMatch(const std::string& expr, const std::string& inp) { +#ifdef USE_POSIX_REGEX + regex_t ex{}; + + if(auto err = regcomp(&ex, expr.c_str(), REG_EXTENDED|REG_NOSUB)) { + auto len = regerror(err, &ex, nullptr, 0u); + std::vector msg(len+1); + (void)regerror(err, &ex, msg.data(), len); + msg[len] = '\0'; // paranoia + setPass(false); + (*this)<<" expression error: "<