[Declaration] If you need to copy, spread, please attach this statement, thank you. Original source: http://morningspace.51.net/ ,moyingzz@etang.com
assertion
From here, it will be described in Core, part of the assertion.
[Asserter]
Related files: asserter.h, asserter.cpp
Asserter is not classified, but an Name Space, which is embedded in CPPUnit Name Space. There are four functions in this domain to help write and assert the macro, which are: Fail, Failif, FailNotequal, FailNotequalif.
The function fail only throws an Exception object, which contains and generated errors related information:
Void Fail (std :: string message, sourceline sourceline)
{
Throw Exception (Message, Sourceline);
}
Function Failif plus conditional control, only throwing only when ShouldFail is True (call fail ": Void Shouldfail, std :: string message, sourine location
{
IF (ShouldFail)
Fail (Message, Location);
}
Similarly, the function FailNotequal throws the Notequalexception object, indicating that the Expected string and the Actual string are not equal, and the Notequalexception object contains and generated errors related information: void failnotequal (std :: string expected, std :: string actual,
Sourceline Sourceline, Std :: string additionalmessage
{
Throw Notequalexception (Expected, Actual, Sourceline, AdditionalMessage);
}
The function FailNotequalif adds conditional control: Voidwarenotequalif (Bool Shouldfail, std :: string expected, std :: string actual,
Sourceline Sourceline, Std :: string additionalmessage
{
IF (ShouldFail)
Failnotequal (Expected, Actual, Sourceline, AdditionalMessage);
}
The following example demonstrates how to write a macro associated with the asserted function using the function provided above, this example is selected from the example attached to the CPPUnit source code: #include
#include
/ / Check if the XML string is equal to the expected value
Void Checkxmlequal (std :: string expertedXML,
Std :: string actualxml,
CPPUnit :: Sourceline Sourceline)
{
// expected XML string
Std :: string expected = Xmluniformiser (ExpectedXML) .stripped ();
// actual XML string
Std :: string actual = Xmluniformiser (actualxml) .stripped (); if (expeected == actual) // actual value and expected value match,
Return;
// Do not match, report to abnormal
:: CPPUnit :: Asserter :: Failnotequal (Expected, Actual, Sourceline);
}
/// assertion: Two XML strings are equal
#define cppUnittest_assert_xml_equal (expected, actual) /
Checkxmlequal (Expected, Actual, CPPUnit_Source ())
[Testassert]
Related Documents: Testassert.h, Testassert.cpp
Testassert is not classified, but an Name Space, which is embedded in CPPUnit Name Space. There are also traces of cppUnit_enable_source-propUnit_enable_source -_deprecated, removed the code associated with it: Assertequals and AssertDoubleequals, the rest are macro.
Asserteauals is a template function, which is similar to the previous checkxmlequal function. If the actual value and the expected value do not match, call Asserter :: FailNotequal, come and see the code:
Template
Void Assertequals (Const T & Expected, Const T & Actual,
Sourceline SourceLine, Const std :: string & message = "" "
{
IF (! assertion_traits
{
// When you need to call Tostring, this is "Lazy Tostring Conversion"
AskERTER :: FailNotequal (assertion_traits
Assertion_traits
Souckline,
Message);
}
}
The reason is used to make it supports multiple types, but Assertion_Traits
Struct Assertion_Traits
{
Static Bool Equal (Const T & X, Const T & Y)
{
Return x == y;
}
Static st: string toString (const t & x)
{
Ostringstream OST;
OST << x;
Return OST.STR ();
}
}
The functionality of Assertion_Traits is two features related to the assertion from T: Equal and toString, which provides generalized version, as needed, you can define a special version, such as special STD :: string custom code as follows: Template <> struct assertion_traits
{
Static Bool Equal (Const std :: string & x, const st :: string & y)
{
Return x == y;
}
Static st: string torstring (const st: string & x)
{
Std :: string text = '"' x '"; //, two sides plus quotes to leave the blank
Ostringstream OST;
OST << Text;
Return OST.STR ();
}
}
Use Assertion_Traits
Double actual,
Double delta,
Sourceline Sourceline
{
Asserter :: Failnotequalif (Fabs (Expected - Actual)> Delta,
Assertion_traits
Assertion_traits
Sourceline);
}
When the absolute value of the difference between Expected and Actual is greater than the limit Delta, call Asserter :: FailNotequalif, use Assertion_Traits
// Ask the assertion condition Condition is true
#define cppUnit_assert (condition) /
(:: cppunit :: askSERTER :: Failif (! (Condition), /
(#condition), /
CPPUNIT_SOSOLINE ()))
#ELSE
#define cppUnit_assert (condition) /
(:: cppUnit :: asserter :: Failif (! (Condition), / "", / /
CPPUNIT_SOSOLINE ()))
#ENDIF
/ / The assertion condition is true, if it is false, the diagnostic information is indicated in Message.
#define cppUnit_assert_message (Message, Condition) /
(:: cppunit :: askSERTER :: Failif (! (Condition), /
(Message), /
CPPUNIT_SOSOLINE ()))
/ / The diagnostic information indicated in Message
#define cppUnit_fail (Message) /
(:: cppunit :: asserter :: fail (mess, /
CPPUNIT_SOSOLINE ()))
// Ask the two values, if it is not equal, the diagnostic information will be printed.
#define cppUnit_assert_equal (Expected, Actual) /
(:: CPPUnit :: Testassert :: assertequals ((expected), /
(Actual), /
CPPUNIT_SOSOLINE ()))
// Ask the two values, etc., Message indicates additional diagnostic information
#define cppUnit_assert_equal_message (Message, Expected, Actual) /
(:: CPPUnit :: Testassert :: assertequals ((expected), /
(Actual), /
CPPUNIT_SOURCELINE (), /
(Message))))))))
// Ask the two values is not accurate
#define cppUnit_assert_doubles_equal (Expected, Actual, Delta) /
(:: CPPUnit :: Testassert :: askERTDOUBLEEQUALS ((Expected), /
(Actual), /
(DELTA), /
CPPUNIT_SOSOLINE ()))
About cppUnit_assert_equal still has to be explained, the macro is required for Expected and Actual, which is called Requirement:
Has the same type (such as std :: string) can use << Serialized to std :: strStream (askSSERTION_TRAITS
#define assert (c) CPPUNIT_ASSERT (C)
#define assertequal (e, a) cppunit_assert_equal (e, a)
#define assertdoudouplanqual (e, a, d) cppunit_assert_doubles_equal (e, a, d)
#define assertlongsequal (e, a) cppUnit_assert_equal (e, a)
#ENDIF