How To Use
 All Modules Pages
Comparing an xml tree with a pattern file

Instead of having to check the XML contents directly within your test case file via multiple calls to "TTB_EXP" you can perform a comparison with a predefined external pattern file with expected contents.

How to compare

Simply call method CompareXmlSubTreeWithPatternFile.

Examples:

// Get the example XML tree
pugi::xml_document doc;
TTB::TheXmlCheck()->ReadFromString(doc, DEMO_XML_STRING);
// All of the following compare methods write a filtered part of the XML tree
// to the file "<YourTargetDir>/<YourTestAppName>_<NamePatternFile>.xml.out"
// and compare it with given pattern file "<NamePatternFile>.xml"
// Simply check complete XML tree using XML document
TTB_CHECK_COND(CompareXmlSubTreeWithPatternFile(doc, MY_TEST_DIR + "/FullContentsFiltered.xml"));
// Check subtree using XPath expression relative to XML document root
TTB_CHECK_COND(CompareXmlSubTreeWithPatternFile(doc, MY_TEST_DIR + "/Vector_1.xml", "MyContainer/Group_A/Vector[1]"));
// Check subtree using XPath expression relative to arbitrary XML tree node
auto subNode = TTB::TheXmlCheck()->NodeFromXPath(doc, "MyContainer/Group_A");
TTB_CHECK_COND(CompareXmlSubTreeWithPatternFile(subNode, MY_TEST_DIR + "/Vector_2.xml", "Vector[2]"));
// Check subtree using XPath expression for given XML string
TTB_CHECK_COND(CompareXmlSubTreeWithPatternFile(DEMO_XML_STRING, MY_TEST_DIR + "/Vector_3.xml", "MyContainer/Group_A/Vector[@id='3']"));

Principles

Error detection

If there are differences between the observed xml tree and the pattern file

Example:

// Deliberately compare with differing pattern file.
// The first detected error will be reported as a test event
TTB_CHECK_EQUAL(CompareXmlSubTreeWithPatternFile(DEMO_XML_STRING, MY_TEST_DIR + "/Vector_3_with_2_errors.xml", "MyContainer/Group_A/Vector[@id='3']"), false);
TTB_EXP("Difference detected");
TTB_EXP("lhs: tolerance: 1.36(path: /Vector/tolerance, pos: 1)");
TTB_EXP("rhs: tolerance: 1.39(path: /Vector/tolerance, pos: 24)");

For a deaper analysis of differences consider the following:

Tip: automatic generation of a patternfile

To get a valid pattern file you can simply start without an existing pattern file.