Jon Test
C++ Unit Test Tool. Not for production -- sample code only
Loading...
Searching...
No Matches
Macros
Assert.h File Reference
#include "JonTest/TestFailure.h"
Include dependency graph for Assert.h:

Go to the source code of this file.

Macros

#define jontest__assert2arg(first, second, message, binaryOperator)
 Internal compiler function macro to support the assert* macros.
 
#define assertFail(message)   throw JonTest::TestFailure(__FILE__, __LINE__, (message))
 Explicitly fail the current test case with the given message as explanation.
 
#define assert(first, message)   jontest__assert2arg(first, true, message, ==)
 Fail this test if (first) is not true with the given message as explanation.
 
#define assertTrue(first, message)   jontest__assert2arg(first, true, message, ==)
 Fail this test if (first) is not true with the given message as explanation.
 
#define assertFalse(first, message)   jontest__assert2arg(first, false, message, ==)
 Fail this test if (first) is not false with the given message as explanation.
 
#define assertNullPtr(first, message)   jontest__assert2arg(first, nullptr, message, ==)
 Fail this test if (first) is not a nullptr with the given message as explanation.
 
#define assertValidPtr(first, message)   jontest__assert2arg(first, nullptr, message, !=)
 Fail this test if (first) is nullptr with the given message as explanation.
 
#define assertEqual(first, second, message)   jontest__assert2arg(first, second, message, ==)
 Fail this test if (first) is not equal to (second) with the given message as explanation.
 
#define assertNotEqual(first, second, message)   jontest__assert2arg(first, second, message, !=)
 Fail this test if (first) is equal to (second) with the given message as explanation.
 
#define assertLess(first, second, message)   jontest__assert2arg(first, second, message, <)
 Fail this test if (first) is not less than (second) with the given message as explanation.
 
#define assertLessEqual(first, second, message)   jontest__assert2arg(first, second, message, <=)
 Fail this test if (first) is not less than or equal to (second) with the given message as explanation.
 
#define assertGreater(first, second, message)   jontest__assert2arg(first, second, message, >)
 Fail this test if (first) is not greater than (second) with the given message as explanation.
 
#define assertGreaterEqual(first, second, message)   jontest__assert2arg(first, second, message, >=)
 Fail this test if (first) is not greater than or equal to (second) with the given message as explanation.
 
#define assertException(expression, expected, message)
 Fail this test if (expression) does not throw the expected exception or a derived class exception with the given message as explanation.
 
#define EXPECTED_FAILURE(expression, message)   assertException(expression, JonTest::TestFailure, message)
 Fail this test if evaluating (expression) does not result itself in a failed test.
 

Macro Definition Documentation

◆ assert

#define assert (   first,
  message 
)    jontest__assert2arg(first, true, message, ==)

Fail this test if (first) is not true with the given message as explanation.

Examples
assertBool_test.cpp.

Definition at line 30 of file Assert.h.

◆ assertEqual

#define assertEqual (   first,
  second,
  message 
)    jontest__assert2arg(first, second, message, ==)

Fail this test if (first) is not equal to (second) with the given message as explanation.

Examples
SimpleTest.cpp, and assertComparison_test.cpp.

Definition at line 45 of file Assert.h.

◆ assertException

#define assertException (   expression,
  expected,
  message 
)
Value:
{ \
bool passed = false;\
try { (expression); passed = true; } \
catch (expected& object) { } \
if(passed) \
{ \
throw JonTest::TestFailure(__FILE__, __LINE__, (message)); \
} \
}
Exception thrown when a test fails an assert and should fail (unless wrapped in expectedFailure()).
Definition TestFailure.h:17

Fail this test if (expression) does not throw the expected exception or a derived class exception with the given message as explanation.

Note: We cannot explicitly wrap expected in parenthesis for safety as it is an operator.

Warning
assertException() cannot be captured with EXPECTED_FAILURE() for a wrong-exception type due to safely catching all exceptions catch(...) to report as "UNKNOWN EXCEPTION" – including throw int(0).
Examples
assertException_test.cpp.

Definition at line 69 of file Assert.h.

◆ assertFail

#define assertFail (   message)    throw JonTest::TestFailure(__FILE__, __LINE__, (message))

Explicitly fail the current test case with the given message as explanation.

Examples
assertFail_setup.cpp, assertFail_teardown.cpp, and assertFail_test.cpp.

Definition at line 27 of file Assert.h.

◆ assertFalse

#define assertFalse (   first,
  message 
)    jontest__assert2arg(first, false, message, ==)

Fail this test if (first) is not false with the given message as explanation.

Examples
assertBool_test.cpp.

Definition at line 36 of file Assert.h.

◆ assertGreater

#define assertGreater (   first,
  second,
  message 
)    jontest__assert2arg(first, second, message, >)

Fail this test if (first) is not greater than (second) with the given message as explanation.

Definition at line 57 of file Assert.h.

◆ assertGreaterEqual

#define assertGreaterEqual (   first,
  second,
  message 
)    jontest__assert2arg(first, second, message, >=)

Fail this test if (first) is not greater than or equal to (second) with the given message as explanation.

Definition at line 60 of file Assert.h.

◆ assertLess

#define assertLess (   first,
  second,
  message 
)    jontest__assert2arg(first, second, message, <)

Fail this test if (first) is not less than (second) with the given message as explanation.

Definition at line 51 of file Assert.h.

◆ assertLessEqual

#define assertLessEqual (   first,
  second,
  message 
)    jontest__assert2arg(first, second, message, <=)

Fail this test if (first) is not less than or equal to (second) with the given message as explanation.

Definition at line 54 of file Assert.h.

◆ assertNotEqual

#define assertNotEqual (   first,
  second,
  message 
)    jontest__assert2arg(first, second, message, !=)

Fail this test if (first) is equal to (second) with the given message as explanation.

Examples
assertComparison_test.cpp.

Definition at line 48 of file Assert.h.

◆ assertNullPtr

#define assertNullPtr (   first,
  message 
)    jontest__assert2arg(first, nullptr, message, ==)

Fail this test if (first) is not a nullptr with the given message as explanation.

Examples
assertPtr_test.cpp.

Definition at line 39 of file Assert.h.

◆ assertTrue

#define assertTrue (   first,
  message 
)    jontest__assert2arg(first, true, message, ==)

Fail this test if (first) is not true with the given message as explanation.

Examples
assertBool_test.cpp.

Definition at line 33 of file Assert.h.

◆ assertValidPtr

#define assertValidPtr (   first,
  message 
)    jontest__assert2arg(first, nullptr, message, !=)

Fail this test if (first) is nullptr with the given message as explanation.

Examples
assertPtr_test.cpp.

Definition at line 42 of file Assert.h.

◆ EXPECTED_FAILURE

#define EXPECTED_FAILURE (   expression,
  message 
)    assertException(expression, JonTest::TestFailure, message)

Fail this test if evaluating (expression) does not result itself in a failed test.

If evaluating (expression) does result in a failed test, this test will pass.

Examples
assertBool_test.cpp, assertComparison_test.cpp, assertException_test.cpp, assertFail_setup.cpp, assertFail_teardown.cpp, assertFail_test.cpp, and assertPtr_test.cpp.

Definition at line 83 of file Assert.h.

◆ jontest__assert2arg

#define jontest__assert2arg (   first,
  second,
  message,
  binaryOperator 
)
Value:
{ \
const auto firstEval = (first); \
const auto secondEval = (second); \
if(!(firstEval binaryOperator secondEval)) \
{ \
throw JonTest::TestFailure(__FILE__, __LINE__, (message), #first, firstEval, #second, secondEval, #binaryOperator); \
} \
}

Internal compiler function macro to support the assert* macros.

/warn Not for direct usage in testing.

Evaluates first and second once only. Compares precisely according to oper. If the precise oper comparising fails, fails the current test, with the given message as explanation.

Note: We cannot explicitly wrap binaryOperator in parenthesis for safety as it is an operator.

Definition at line 16 of file Assert.h.