CppUnit - The Unit Testing Library

CppUnit project page | FAQ

Asserter Struct Reference
Creating custom assertions

A set of functions to help writing assertion macros. More...

`#include <Asserter.h>`

## Static Public Member Functions
static void CPPUNIT_API | fail (const Message &message, const SourceLine &sourceLine=SourceLine())
| Throws a Exception with the specified message and location.
static void CPPUNIT_API | fail (std::string message, const SourceLine &sourceLine=SourceLine())
| Throws a Exception with the specified message and location.
static void CPPUNIT_API | failIf (bool shouldFail, const Message &message, const SourceLine &sourceLine=SourceLine())
| Throws a Exception with the specified message and location.
static void CPPUNIT_API | failIf (bool shouldFail, std::string message, const SourceLine &sourceLine=SourceLine())
| Throws a Exception with the specified message and location.
static std::string CPPUNIT_API | makeExpected (const std::string &expectedValue)
| Returns a expected value string for a message, case equal than Typically used to create 'not equal' message, or to check that a message contains the expected content when writing unit tests for your custom assertions.
static std::string CPPUNIT_API | makeExpectedEqual (const std::string &expectedValue)
| Returns a expected value string for a message, case equal than Typically used to create 'not equal' message, or to check that a message contains the expected content when writing unit tests for your custom assertions.
static std::string CPPUNIT_API | makeExpectedLess (const std::string &expectedValue)
| Returns a expected value string for a message, case less than.
static std::string CPPUNIT_API | makeExpectedLessEqual (const std::string &expectedValue)
| Returns a expected value string for a message, case less or equal than.
static std::string CPPUNIT_API | makeExpectedGreater (const std::string &expectedValue)
| Returns a expected value string for a message, case greater than.
static std::string CPPUNIT_API | makeExpectedGreaterEqual (const std::string &expectedValue)
| Returns a expected value string for a message, greater or equal than.
static std::string CPPUNIT_API | makeActual (const std::string &actualValue)
| Returns an actual value string for a message. Typically used to create 'not equal' message, or to check that a message contains the expected content when writing unit tests for your custom assertions.
static Message CPPUNIT_API | makeNotEqualMessage (const std::string &expectedValue, const std::string &actualValue, const AdditionalMessage &additionalMessage=AdditionalMessage(), const std::string &shortDescription="equality assertion failed")
static Message CPPUNIT_API | makeMessage (const std::string &expectedValue, const std::string &actualValue, const std::string &shortDescription, const AdditionalMessage &additionalMessage=AdditionalMessage())
static void CPPUNIT_API | failNotEqual (std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="equality assertion failed")
| Throws an Exception with the specified message and location.
static void CPPUNIT_API | failNotLess (std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="less assertion failed")
| Throws an Exception with the specified message and location.
static void CPPUNIT_API | failNotGreater (std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="greater assertion failed")
| Throws an Exception with the specified message and location.
static void CPPUNIT_API | failNotLessEqual (std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="less equal assertion failed")
| Throws an Exception with the specified message and location.
static void CPPUNIT_API | failNotGreaterEqual (std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="greater equal assertion failed")
| Throws an Exception with the specified message and location.
static void CPPUNIT_API | failNotEqualIf (bool shouldFail, std::string expected, std::string actual, const SourceLine &sourceLine, const AdditionalMessage &additionalMessage=AdditionalMessage(), std::string shortDescription="equality assertion failed")
| Throws an Exception with the specified message and location.

## Detailed Description

A set of functions to help writing assertion macros.

Here is an example of assertion, a simplified version of the actual assertion implemented in examples/cppunittest/XmlUniformiser.h:

#include <cppunit/SourceLine.h>
#include <cppunit/TestAssert.h>
void
checkXmlEqual( std::string expectedXml,
std::string actualXml,
CppUnit::SourceLine sourceLine )
{
std::string expected = XmlUniformiser( expectedXml ).stripped();
std::string actual = XmlUniformiser( actualXml ).stripped();
if ( expected == actual )
return;
::CppUnit::Asserter::failNotEqual( expected,
actual,
sourceLine );
}
#define CPPUNITTEST_ASSERT_XML_EQUAL( expected, actual ) \
checkXmlEqual( expected, actual, \
CPPUNIT_SOURCELINE() )
SourceLine.h
TestAssert.h

## Member Function Documentation

## ◆ fail() [1/2]

void Asserter::fail | ( | const Message & | message,
|  | const SourceLine & | sourceLine = SourceLine() )
| static

Throws a Exception with the specified message and location.

## ◆ fail() [2/2]

CPPUNIT_NS_BEGIN void Asserter::fail | ( | std::string | message,
|  | const SourceLine & | sourceLine = SourceLine() )
| static

Throws a Exception with the specified message and location.

Deprecated
  Use fail( Message, SourceLine ) instead.

## ◆ failIf() [1/2]

void Asserter::failIf | ( | bool | shouldFail,
|  | const Message & | message,
|  | const SourceLine & | sourceLine = SourceLine() )
| static

Throws a Exception with the specified message and location.

Parameters

  shouldFail | if `true` then the exception is thrown. Otherwise nothing happen.
  message | Message explaining the assertion failiure.
  sourceLine | Location of the assertion.

## ◆ failIf() [2/2]

void Asserter::failIf | ( | bool | shouldFail,
|  | std::string | message,
|  | const SourceLine & | sourceLine = SourceLine() )
| static

Throws a Exception with the specified message and location.

Deprecated
  Use failIf( bool, Message, SourceLine ) instead.

Parameters

  shouldFail | if `true` then the exception is thrown. Otherwise nothing happen.
  message | Message explaining the assertion failiure.
  sourceLine | Location of the assertion.

## ◆ failNotEqual()

void Asserter::failNotEqual | ( | std::string | expected,
|  | std::string | actual,
|  | const SourceLine & | sourceLine,
|  | const AdditionalMessage & | additionalMessage = AdditionalMessage(),
|  | std::string | shortDescription = "equality assertion failed" )
| static

Throws an Exception with the specified message and location.

Parameters

  expected | Text describing the expected value.
  actual | Text describing the actual value.
  sourceLine | Location of the assertion.
  additionalMessage | Additional message. Usually used to report what are the differences between the expected and actual value.
  shortDescription | Short description for the failure message.

## ◆ failNotEqualIf()

void Asserter::failNotEqualIf | ( | bool | shouldFail,
|  | std::string | expected,
|  | std::string | actual,
|  | const SourceLine & | sourceLine,
|  | const AdditionalMessage & | additionalMessage = AdditionalMessage(),
|  | std::string | shortDescription = "equality assertion failed" )
| static

Throws an Exception with the specified message and location.

Parameters

  shouldFail | if `true` then the exception is thrown. Otherwise nothing happen.
  expected | Text describing the expected value.
  actual | Text describing the actual value.
  sourceLine | Location of the assertion.
  additionalMessage | Additional message. Usually used to report where the "difference" is located.
  shortDescription | Short description for the failure message.

## ◆ failNotGreater()

void Asserter::failNotGreater | ( | std::string | expected,
|  | std::string | actual,
|  | const SourceLine & | sourceLine,
|  | const AdditionalMessage & | additionalMessage = AdditionalMessage(),
|  | std::string | shortDescription = "greater assertion failed" )
| static

Throws an Exception with the specified message and location.

Parameters

  expected | Text describing the expected value.
  actual | Text describing the actual value.
  sourceLine | Location of the assertion.
  additionalMessage | Additional message. Usually used to report what are the differences between the expected and actual value.
  shortDescription | Short description for the failure message.

## ◆ failNotGreaterEqual()

void Asserter::failNotGreaterEqual | ( | std::string | expected,
|  | std::string | actual,
|  | const SourceLine & | sourceLine,
|  | const AdditionalMessage & | additionalMessage = AdditionalMessage(),
|  | std::string | shortDescription = "greater equal assertion failed" )
| static

Throws an Exception with the specified message and location.

Parameters

  expected | Text describing the expected value.
  actual | Text describing the actual value.
  sourceLine | Location of the assertion.
  additionalMessage | Additional message. Usually used to report what are the differences between the expected and actual value.
  shortDescription | Short description for the failure message.

## ◆ failNotLess()

void Asserter::failNotLess | ( | std::string | expected,
|  | std::string | actual,
|  | const SourceLine & | sourceLine,
|  | const AdditionalMessage & | additionalMessage = AdditionalMessage(),
|  | std::string | shortDescription = "less assertion failed" )
| static

Throws an Exception with the specified message and location.

Parameters

  expected | Text describing the expected value.
  actual | Text describing the actual value.
  sourceLine | Location of the assertion.
  additionalMessage | Additional message. Usually used to report what are the differences between the expected and actual value.
  shortDescription | Short description for the failure message.

## ◆ failNotLessEqual()

void Asserter::failNotLessEqual | ( | std::string | expected,
|  | std::string | actual,
|  | const SourceLine & | sourceLine,
|  | const AdditionalMessage & | additionalMessage = AdditionalMessage(),
|  | std::string | shortDescription = "less equal assertion failed" )
| static

Throws an Exception with the specified message and location.

Parameters

  expected | Text describing the expected value.
  actual | Text describing the actual value.
  sourceLine | Location of the assertion.
  additionalMessage | Additional message. Usually used to report what are the differences between the expected and actual value.
  shortDescription | Short description for the failure message.

## ◆ makeActual()

std::string Asserter::makeActual | ( | const std::string & | actualValue | ) |
| static

Returns an actual value string for a message. Typically used to create 'not equal' message, or to check that a message contains the expected content when writing unit tests for your custom assertions.

Parameters

  actualValue | String that represents the actual value.

Returns
  actualValue prefixed with "Actual : ".

See also
  makeExpected().

## ◆ makeExpected()

std::string Asserter::makeExpected | ( | const std::string & | expectedValue | ) |
| static

Returns a expected value string for a message, case equal than Typically used to create 'not equal' message, or to check that a message contains the expected content when writing unit tests for your custom assertions.

Parameters

  expectedValue | String that represents the expected value.

Returns
  expectedValue prefixed with "Expected: ".

Deprecated
  Use makeExpectedEqual instead

See also
  makeActual().

## ◆ makeExpectedEqual()

std::string Asserter::makeExpectedEqual | ( | const std::string & | expectedValue | ) |
| static

Returns a expected value string for a message, case equal than Typically used to create 'not equal' message, or to check that a message contains the expected content when writing unit tests for your custom assertions.

Parameters

  expectedValue | String that represents the expected value.

Returns
  expectedValue prefixed with "Expected: ".

See also
  makeActual().

## ◆ makeExpectedGreater()

std::string Asserter::makeExpectedGreater | ( | const std::string & | expectedValue | ) |
| static

Returns a expected value string for a message, case greater than.

Parameters

  expectedValue | String that represents the expected value.

Returns
  expectedValue prefixed with "Expected: ".

See also
  makeExpectedEqual().

## ◆ makeExpectedGreaterEqual()

std::string Asserter::makeExpectedGreaterEqual | ( | const std::string & | expectedValue | ) |
| static

Returns a expected value string for a message, greater or equal than.

Parameters

  expectedValue | String that represents the expected value.

Returns
  expectedValue prefixed with "Expected: ".

See also
  makeExpectedEqual().

## ◆ makeExpectedLess()

std::string Asserter::makeExpectedLess | ( | const std::string & | expectedValue | ) |
| static

Returns a expected value string for a message, case less than.

Parameters

  expectedValue | String that represents the expected value.

Returns
  expectedValue prefixed with "Expected less than: ".

See also
  makeExpectedEqual().

## ◆ makeExpectedLessEqual()

std::string Asserter::makeExpectedLessEqual | ( | const std::string & | expectedValue | ) |
| static

Returns a expected value string for a message, case less or equal than.

Parameters

  expectedValue | String that represents the expected value.

Returns
  expectedValue prefixed with "Expected: ".

See also
  makeExpectedEqual().

## ◆ makeMessage()

Message Asserter::makeMessage | ( | const std::string & | expectedValue,
|  | const std::string & | actualValue,
|  | const std::string & | shortDescription,
|  | const AdditionalMessage & | additionalMessage = AdditionalMessage() )
| static

## ◆ makeNotEqualMessage()

Message Asserter::makeNotEqualMessage | ( | const std::string & | expectedValue,
|  | const std::string & | actualValue,
|  | const AdditionalMessage & | additionalMessage = AdditionalMessage(),
|  | const std::string & | shortDescription = "equality assertion failed" )
| static

Deprecated
  Use makeMessage instead

The documentation for this struct was generated from the following files:

- Asserter.h
- Asserter.cpp

| Send comments to: CppUnit Developers