Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 81e316b3 authored by Josh Gao's avatar Josh Gao
Browse files

base: avoid evaluating macro argument multiple times.

Previously, in the regex test helpers, we would evaluate the haystack
expression again to generate the error message, which leads to
nonsensical errors if the expression returns a different value on the
second call (e.g. functions like dlerror which return null on subsequent
calls).

Test: bionic-unit-tests with a failure
Test: treehugger
Change-Id: I2126cefeb45e26638194af8a82d0f2a9d7196edf
parent 40a8b07e
Loading
Loading
Loading
Loading
+24 −20
Original line number Diff line number Diff line
@@ -55,28 +55,32 @@ class CapturedStdout : public CapturedStdFd {

#define ASSERT_MATCH(str, pattern)                                           \
  do {                                                                       \
    if (!std::regex_search((str), std::regex((pattern)))) {                    \
      FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
    auto __s = (str);                                                        \
    if (!std::regex_search(__s, std::regex((pattern)))) {                    \
      FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << __s; \
    }                                                                        \
  } while (0)

#define ASSERT_NOT_MATCH(str, pattern)                                                   \
  do {                                                                                   \
    if (std::regex_search((str), std::regex((pattern)))) {                                 \
      FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
    auto __s = (str);                                                                    \
    if (std::regex_search(__s, std::regex((pattern)))) {                                 \
      FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << __s; \
    }                                                                                    \
  } while (0)

#define EXPECT_MATCH(str, pattern)                                                  \
  do {                                                                              \
    if (!std::regex_search((str), std::regex((pattern)))) {                           \
      ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
    auto __s = (str);                                                               \
    if (!std::regex_search(__s, std::regex((pattern)))) {                           \
      ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << __s; \
    }                                                                               \
  } while (0)

#define EXPECT_NOT_MATCH(str, pattern)                                                          \
  do {                                                                                          \
    if (std::regex_search((str), std::regex((pattern)))) {                                        \
      ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
    auto __s = (str);                                                                           \
    if (std::regex_search(__s, std::regex((pattern)))) {                                        \
      ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << __s; \
    }                                                                                           \
  } while (0)