CPPUnit Source Code Interpretation (5)

zhaozj2021-02-17  54

[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 :: Equal (expected, actual))

{

// When you need to call Tostring, this is "Lazy Tostring Conversion"

AskERTER :: FailNotequal (assertion_traits :: toString (Expected),

Assertion_traits :: Tostring (actual),

Souckline,

Message);

}

}

The reason is used to make it supports multiple types, but Assertion_Traits :: Equal is the sacred. Seeing this traits, I will definitely think that the famous characteristic extraction technique in generic programming. Yes, this is a small application of Traits techniques here. Assertion_Traits definition and testassert in one file: Template

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 :: Equal to determine whether the expected and actual is equal, if it is not equal, if it is not equal, the Asserter :: FailNotequal function is called. At this point, I believe that the reader has already understood the reason for the reader. With Assertion_Traits :: TOSTRING, no matter what type, a String version of Notequalexception is sufficient to deal with it. After understanding Assertequals, then the assertdoubleequals function is very simple, which is used to make a fuzzy equal judgment, targeting the Double type data: Void teststert :: askSERTDOUBLEEQUALS (Double Expected,

Double actual,

Double delta,

Sourceline Sourceline

{

Asserter :: Failnotequalif (Fabs (Expected - Actual)> Delta,

Assertion_traits :: toString (expected),

Assertion_traits :: toString (actual),

Sourceline);

}

When the absolute value of the difference between Expected and Actual is greater than the limit Delta, call Asserter :: FailNotequalif, use Assertion_Traits :: TOSTRING, T thisdery again. Ok, now I have all the best. With these tools, you can write a macro related to the assertion, as the Checkxmlequal function in front. With these macros, we can get the wrong file physical location and line number: #IF cppUnit_have_cpp_source_annotation

// 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 :: toString) == Comparison (Assertion_Traits :: Equal Indicate) However, the latter two can be removed by customization of the ASSERTION_TRAITS customization. Finally, Testassert also defines another group of macros comparable to the above macro function, based on the description of Changelog, this is another "historical legacy" problem: In order to be compatible with the earlier versions, it is used as the following set of macro. If you still need to use these macros, just define the macro cppunit_enable_naked_assert before all CPPUnit contain files: #IF cppunit_enable_naked_assert # undef assert

#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

转载请注明原文地址:https://www.9cbs.com/read-29137.html

New Post(0)