Class XmlCheck allows to check the contents of an XML tree/subtree. More...
|
Class XmlCheck allows to check the contents of an XML tree/subtree.
Supported features
Depends on
HowTo access XML nodes with TinyXml
// Read XML string into XML document structure TiXmlDocument doc; doc.Parse(DEMO_XML_STRING); // Save XML document to file std::string filePath = MY_TEST_DIR + "/GeneratedXmlFile.xml"; doc.SaveFile(filePath.c_str()); // Read XML file into XML document structure TiXmlDocument docFromFile; docFromFile.LoadFile(filePath.c_str());
HowTo address subnodes with TinyXpath
| /MyContainer/Group_B | subnode Group_B |
| /MyContainer/Group_B/x | first x element within Group_B |
| /MyContainer/Group_B/x[3] | third x element within Group_B |
| /MyContainer/Group_B/x[last()] | last x element of Group_B |
| /MyContainer/Country[@name='Germany'] | first country with specific attribute 'name' |
| /MyContainer/Country[attribute::name='USA'] | first country with specific attribute 'name' (explicit syntax) |
Typical code snippets when used with Boost.Test
see also TestXmlCheckWithBoost
BOOST_MESSAGE("\nComparing two XML trees");
BOOST_CHECK(TTB::TheXmlCheck()->Compare(&docA,&docB));
BOOST_MESSAGE(TTB::TheXmlCheck()->GetCompareResult());
BOOST_MESSAGE("\nComparing two XML subtrees using XPath, ignoring time");
TTB::TheXmlCheck()->IgnoreAttribute("time");
BOOST_CHECK(TTB::TheXmlCheck()->Compare(
TTB::XmlCheck::SubNodeFromXPath(docA.RootElement(), "/MyContainer/Group_B"),
TTB::XmlCheck::SubNodeFromXPath(docB.RootElement(), "/MyContainer/Group_B")));
BOOST_MESSAGE(TTB::TheXmlCheck()->GetCompareResult());
Typical code snippets when used with Boost.Test and TestToolBox::TestEvents
see also TestXmlCheckWithBoostExtensions
TTB::TheXmlCheck()->IgnoreAttribute("date");
TTB::TheXmlCheck()->IgnoreAttribute("time");
TTB::TheXmlCheck()->IgnoreNode("date");
TTB::TheXmlCheck()->UseAsDoubleAttribute("tolerance", true, 3);
TTB::TheXmlCheck()->UseAsDoubleNode("x", true, 4);
TTB::TheXmlCheck()->CheckNode(&doc); TTB_EXP("MyContainer");
TTB_EXP(" Group_A");
TTB_EXP(" Vector");
TTB_EXP(" id: 1");
TTB_EXP(" tolerance: 0.026");
TTB_EXP(" This is the text of the first vector element");
TTB_EXP(" x");
TTB_EXP(" 3.1424");
TTB_EXP(" Vector");
TTB_EXP(" id: 2");
TTB_EXP(" tolerance: 0.026");
TTB_EXP(" This is the text of the second vector element");
TTB_EXP(" x");
TTB_EXP(" 1.3264");
TTB::TheXmlCheck()->CheckSubNode(doc.RootElement(),""/MyContainer/Group_A/Vector[last()]");
TTB_EXP("Vector");
TTB_EXP(" id: 3");
TTB_EXP(" tolerance: 1.357");
TTB_EXP(" x");
TTB_EXP(" 6.28532");or as alternative syntax:
TTB::TheXmlCheck()->CheckSubNode(
TTB::XmlCheck::SubNodeFromXPath(doc.RootElement(),"/MyContainer/Group_A"),
"Vector[last()]");or identifying XML contents via multiple attributes / multiple XPath expressions:
XPath: Direct acess to third node hierarchy (street within town within country)
TTB::TheXmlCheck()->CheckSubNode(doc.RootElement(),
"Country[@name='Germany']",
"Town[@name='Regensburg']",
"Street[@name='LappersdorferStrasse']"); TTB_EXP("Street");
TTB_EXP(" name: LappersdorferStrasse");
TTB_EXP(" Contents for LappersdorferStrasse in Regensburg, Germany");
TTB::TheXmlCheck()->IgnoreAttribute("time");
TTB::TheXmlCheck()->Compare(&docA,&docB); TTB_EXP("Difference detected");
TTB_EXP("A: tolerance: 0.0258 (line: 7 col: 54, path: //MyContainer/Group_A/Vector)");
TTB_EXP("B: tolerance: 0.8258 (line: 9 col: 54, path: //MyContainer/Group_A/Vector)"); TTB::TheXmlCheck()->Compare(
TTB::XmlCheck::SubNodeFromXPath(docA.RootElement(), "/MyContainer/Group_B"),
TTB::XmlCheck::SubNodeFromXPath(docB.RootElement(), "/MyContainer/Group_B")); TTB_EXP("Difference detected");
TTB_EXP("A: 3.132437 (line: 18 col: 8, path: //MyContainer/Group_B/x/3.132437/)");
TTB_EXP("B: 12.132437 (line: 20 col: 8, path: //MyContainer/Group_B/x/12.132437/)");
Code usage
There are two main possibilities to use XmlCheck within your test applications:
include "TestToolBox\XmlCheckImpl.h"include "TestToolBox\XmlCheck.h"// Compile as real functions which are available to other // source files which need only include TestToolBox\XmlCheck.h #define TTB_NOT_INLINE #include "TestToolBox\XmlCheckImpl.h"
1.6.3