Skip to content

Commit b582d7f

Browse files
OlekRaymondMartchus
authored andcommitted
Allow compiling tests with CppUnit2Gtest
* Turn check for avoiding copy/move into compile-time check * Adapt TESTUTILS_ASSERT_LIKE_FLAGS * Adapt OutputCheck
1 parent 56bb3d0 commit b582d7f

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

tests/conversiontests.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,8 @@ struct StringThatDoesNotLikeToBeCopiedOrMoved : public std::string {
418418
: std::string(value)
419419
{
420420
}
421-
[[noreturn]] StringThatDoesNotLikeToBeCopiedOrMoved(const StringThatDoesNotLikeToBeCopiedOrMoved &other)
422-
: std::string(other)
423-
{
424-
CPPUNIT_FAIL("attempt to copy string: " + other);
425-
}
426-
[[noreturn]] StringThatDoesNotLikeToBeCopiedOrMoved(StringThatDoesNotLikeToBeCopiedOrMoved &&other)
427-
: std::string(std::move(other))
428-
{
429-
CPPUNIT_FAIL("attempt to move string: " + other);
430-
}
421+
[[noreturn]] StringThatDoesNotLikeToBeCopiedOrMoved(const StringThatDoesNotLikeToBeCopiedOrMoved &other) = delete;
422+
[[noreturn]] StringThatDoesNotLikeToBeCopiedOrMoved(StringThatDoesNotLikeToBeCopiedOrMoved &&other) = delete;
431423
};
432424

433425
/// \endcond
@@ -478,6 +470,7 @@ void ConversionTests::testStringBuilder()
478470
// check that for the internal tuple construction no copies are made
479471
StringThatDoesNotLikeToBeCopiedOrMoved str(" happen ");
480472
const StringThatDoesNotLikeToBeCopiedOrMoved str2("for this");
473+
// Both deleted so becomes a compilation test rather than a runtime one
481474
CPPUNIT_ASSERT_EQUAL("no copy/move should happen for this!"s,
482475
argsToString(StringThatDoesNotLikeToBeCopiedOrMoved("no copy/move should"), str, str2, StringThatDoesNotLikeToBeCopiedOrMoved("!")));
483476
}

tests/outputcheck.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,29 @@ inline OutputCheck::OutputCheck(std::function<void(const std::string &)> &&custo
7272
*/
7373
inline OutputCheck::~OutputCheck() noexcept(false)
7474
{
75+
# ifdef CppUnit2Gtest
76+
# define ExpectEqual(a, b) EXPECT_EQ(a,b); if (!(a == b) ) throw std::logic_error("bad assert")
77+
# define Fail(msg) ADD_FAILURE() << msg; throw std::logic_error("bad assert")
78+
# else
79+
# define ExpectEqual(a, b) CPPUNIT_ASSERT_EQUAL(a,b)
80+
# define Fail(msg) CPPUNIT_FAIL(msg)
81+
#endif
7582
m_os.rdbuf(m_regularOutputBuffer);
7683
const std::string actualOutput(m_buffer.str());
7784
if (m_customCheck) {
7885
m_customCheck(actualOutput);
7986
return;
8087
}
8188
if (m_alternativeOutput.empty()) {
82-
CPPUNIT_ASSERT_EQUAL(m_expectedOutput, actualOutput);
89+
ExpectEqual(m_expectedOutput, actualOutput);
8390
return;
8491
}
8592
if (m_expectedOutput != actualOutput && m_alternativeOutput != actualOutput) {
8693
using namespace CppUtilities;
87-
CPPUNIT_FAIL("Output is not either \"" % m_expectedOutput % "\" or \"" % m_alternativeOutput % "\". Got instead:\n" + actualOutput);
94+
Fail("Output is not either \"" % m_expectedOutput % "\" or \"" % m_alternativeOutput % "\". Got instead:\n" + actualOutput);
8895
}
96+
#undef Fail
97+
#undef ExpectEqual
8998
}
9099

91100
} // namespace CppUtilities

tests/testutils.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,11 @@ template <typename T, Traits::DisableIf<std::is_integral<T>> * = nullptr> const
325325
* \brief Asserts whether the specified \a string matches the specified \a regex.
326326
* \remarks Requires cppunit.
327327
*/
328-
#define TESTUTILS_ASSERT_LIKE_FLAGS(message, expectedRegex, regexFlags, actualString) \
329-
(CPPUNIT_NS::Asserter::failIf(!(std::regex_match(actualString, std::regex(expectedRegex, regexFlags))), \
330-
CPPUNIT_NS::Message( \
331-
CppUtilities::argsToString('\"', actualString, "\"\n not like\n\"", expectedRegex, '\"'), "Expression: " #actualString, message), \
332-
CPPUNIT_SOURCELINE()))
333-
328+
#define TESTUTILS_ASSERT_LIKE_FLAGS(message, expectedRegex, regexFlags, actualString) \
329+
CPPUNIT_ASSERT_MESSAGE( \
330+
CppUtilities::argsToString('\"', actualString, "\"\n not like\n\"", expectedRegex, '\"', "\n\nExpression: " #actualString, message), \
331+
(std::regex_match(actualString, std::regex(expectedRegex, regexFlags)) ) \
332+
)
334333
/*!
335334
* \brief Asserts whether the specified \a string matches the specified \a regex.
336335
* \remarks Requires cppunit.

0 commit comments

Comments
 (0)