Overview:
Ignore selected attributes or nodes
Assume you have the following xml tree:
static const std::string SHORT_DEMO = R"(
<SomeLogEntry>
<this>0x24315683</this>
<Child Id="1001" date="01.04.2015" time="23:55:00">
<Counter>13</Counter>
</Child>
<ComplexChild>
<UninterestingDetails>Details not shown here</UninterestingDetails>
</ComplexChild>
</SomeLogEntry>
)";
Default: check all data
A simple check of all XML contents can be done by calling XmlCheck::CheckNode(), which can be called both for a pugi::xml_document or a pugi::xml_node. XmlCheck will write all relevant XML data to TTB::TestEvents by using a single line / event entry for each found data item. The verification of the expected contents can be done by using TTB_EXP():
pugi::xml_document doc;
TTB::TheXmlCheck()->ReadFromString(doc, SHORT_DEMO);
TTB::TheXmlCheck()->CheckNode(doc);
TTB_EXP("SomeLogEntry");
TTB_EXP(" this");
TTB_EXP(" 0x24315683");
TTB_EXP(" Child");
TTB_EXP(" Id: 1001");
TTB_EXP(" date: 01.04.2015");
TTB_EXP(" time: 23:55:00");
TTB_EXP(" Counter");
TTB_EXP(" 13");
TTB_EXP(" ComplexChild");
TTB_EXP(" UninterestingDetails");
TTB_EXP(" Details not shown here");
Exclude some attributes or nodes from being checked
Some of the values above (e.g. hexadecimal addresses within memory, time and date stamps) may be relevant when reading the stored data. But they make an automatic verification of expected xml data very difficult. As a simple solution you can exclude certain parts of the XML data from being verified by using methods XmlCheck::IgnoreAttribute() and XmlCheck::IgnoreNode():
TTB::TheXmlCheck()->IgnoreAttribute("date");
TTB::TheXmlCheck()->IgnoreAttribute("time");
TTB::TheXmlCheck()->IgnoreNode("this");
TTB::TheXmlCheck()->IgnoreNode("ComplexChild");
Resulting check code without dependency from unwanted "random" values:
TTB::TheXmlCheck()->CheckNode(doc);
TTB_EXP("SomeLogEntry");
TTB_EXP(" Child");
TTB_EXP(" Id: 1001");
TTB_EXP(" Counter");
TTB_EXP(" 13");
For resetting your configurations you may call XmlCheck::SetDefaults().
Check double values with given precision
Algorithms of your application may calculate with double values. By rounding and converting the resulting values may only be close to a given expected value. To allow an automatic check of values you can specify via XmlCheck::UseAsDoubleNode/Attribute the precision to be used for checking:
Sample data:
static const std::string SHORT_DEMO =
R"(<SomeNode attrA="1.23456789">
<SubNodeB>0.2468012345</SubNodeB>
</SomeNode>
)";
Default: values have to be checked exact:
TTB_INFO("Check with standard settings");
pugi::xml_document doc;
TTB::TheXmlCheck()->ReadFromString(doc, SHORT_DEMO);
TTB::TheXmlCheck()->CheckNode(doc);
TTB_EXP("SomeNode");
TTB_EXP(" attrA: 1.23456789");
TTB_EXP(" SubNodeB");
TTB_EXP(" 0.2468012345");
Check with explicitly treating as double values with reduced precision:
TTB_INFO("\nCheck with small precision");
TTB::TheXmlCheck()->UseAsDoubleAttribute("attrA", true, 3);
TTB::TheXmlCheck()->UseAsDoubleNode("SubNodeB", true, 2);
TTB::TheXmlCheck()->CheckNode(doc);
TTB_EXP("SomeNode");
TTB_EXP(" attrA: 1.235");
TTB_EXP(" SubNodeB");
TTB_EXP(" 0.25");
Check with increased precision:
TTB_INFO("\nCheck with increased precision");
TTB::TheXmlCheck()->UseAsDoubleAttribute("attrA", true, 5);
TTB::TheXmlCheck()->UseAsDoubleNode("SubNodeB", true, 5);
TTB::TheXmlCheck()->CheckNode(doc);
TTB_EXP("SomeNode");
TTB_EXP(" attrA: 1.23457");
TTB_EXP(" SubNodeB");
TTB_EXP(" 0.24680");
Qualitative check of pointer values
Sample data:
static const std::string SHORT_DEMO = R"(
<SomeLogEntry>
<this>0x24315683</this>
<Child Id="1001" pSomething="0x24315683" pSomethingElse="0" />
</SomeLogEntry>
)";
Within the context of programming and unit testing the exact memory address value for a pointer value is not of interest (or at least disturbs automatic test verification). But it may be relevant if there exists a pointer entry within xml data and whether this entry is 0 or different from 0. You can perform these kind of checks by using XmlCheck::UseAsPointerAttribute() and XmlCheck::UseAsPointerNode():
First define which parts of the XML data shall be handled as pointer values:
TTB::TheXmlCheck()->UseAsPointerAttribute("pSomething");
TTB::TheXmlCheck()->UseAsPointerAttribute("pSomethingElse");
TTB::TheXmlCheck()->UseAsPointerNode("this");
Resulting check code without dependency from random memory address values:
pugi::xml_document doc;
TTB::TheXmlCheck()->ReadFromString(doc, SHORT_DEMO);
TTB::TheXmlCheck()->CheckNode(doc);
TTB_EXP("SomeLogEntry");
TTB_EXP(" this");
TTB_EXP(" exists");
TTB_EXP(" Child");
TTB_EXP(" Id: 1001");
TTB_EXP(" pSomething: exists");
TTB_EXP(" pSomethingElse: 0");
Write xml contents to test protocol without need for verification
For a quick test you can simply write an xml tree with all details to test protocol. There you can manually inspect all details you are interested in. To avoid the need for an automatic verification you can use method XmlCheck::SetOutputModeForWritingXmlContents():
TTB_INFO("Regular output of xml tree - no need for exact verification");
TTB::TheXmlCheck()->SetOutputModeForWritingXmlContents(TTB::CTX_INFO);
TTB::TheXmlCheck()->CheckNode(doc);
TTB_INFO("More detailed output of xml tree - no need for exact verification");
TTB::TheXmlCheck()->SetDetailedOutput(true);
TTB::TheXmlCheck()->CheckNode(doc);