|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
- #include <iostream>
- #include <string>
-
- #include "CodeDweller/XMLReader.hpp"
-
- ////////////////////////////////////////////////////////////////////////////////
- // Configuration ///////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////////////////////////////
- // End of configuration ////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- int nTotalTests = 0;
- int nPass = 0;
- int nFail = 0;
-
- bool result;
-
- #define NO_EXCEPTION_TERM(msg) \
- std::cout \
- << msg << " failed to throw exception at line " \
- << __LINE__ << "." << std::endl
-
- #define EXCEPTION_TERM(msg) \
- std::cout \
- << msg << " threw unexpected exception: " << e.what() << std::endl
-
- #define RETURN_FALSE(msg) \
- std::cout \
- << msg << " at line " << __LINE__ << std::endl; \
- return false;
-
- #define RUN_TEST(test) \
- std::cout << " " #test ": "; \
- std::cout.flush(); \
- result = test(); \
- std::cout << (result ? "ok" : "fail") << std::endl; \
- nTotalTests++; \
- if (result) nPass++; else nFail++;
-
- #define SUMMARY \
- std::cout \
- << "\nPass: " << nPass \
- << ", Fail: " << nFail \
- << ", Total: " << nTotalTests << std::endl
-
- ////////////////////////////////////////////////////////////////////////////////
- // Tests ///////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- bool testEmptyElement() {
-
- std::string stageContent, stage1Content, stage2Content;
- std::string elementXml, element1Xml, element2Xml;
- CodeDweller::XMLReaderElement reader("elem");
-
- reader
- .Element("stage", stageContent)
- .RawData(elementXml)
- .End("stage")
- .Element("stage1", stage1Content)
- .RawData(element1Xml)
- .End("stage1")
- .Element("stage2", stage2Content)
- .RawData(element2Xml)
- .End("stage2")
- .End("elem");
-
- std::string xml;
-
- xml = "<elem/>";
-
- CodeDweller::XMLReaderData confData(xml.data(), xml.size());
-
- reader.initialize();
-
- if (0 == reader.interpret(confData)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if (!(stageContent.empty() &&
- stage1Content.empty() &&
- stage2Content.empty())) {
- RETURN_FALSE("empty element 1 read failure");
- }
-
- xml = "<elem></elem>";
-
- CodeDweller::XMLReaderData confData1(xml.data(), xml.size());
-
- reader.initialize();
-
- if (0 == reader.interpret(confData1)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if (!(stageContent.empty() &&
- stage1Content.empty() &&
- stage2Content.empty())) {
- RETURN_FALSE("empty element 2 read failure");
- }
-
- xml = "<elem>Should be<xx> abc </xx> ignored<stage></elem>";
-
- CodeDweller::XMLReaderData confData2(xml.data(), xml.size());
-
- reader.initialize();
-
- if (0 == reader.interpret(confData2)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if (!(stageContent.empty() &&
- stage1Content.empty() &&
- stage2Content.empty())) {
- RETURN_FALSE("empty element 3 read failure");
- }
-
- return true;
-
- }
-
- bool testRawData() {
-
- std::string stageContent;
- std::string elementXml, stageXml;
- CodeDweller::XMLReaderElement reader("elem");
-
- reader
- .RawData(elementXml)
- .Element("stage", stageContent)
- .RawData(stageXml)
- .End("stage")
- .End("elem");
-
- std::string xml;
-
- xml = "<elem><stage>Content</stage></elem>";
-
- CodeDweller::XMLReaderData confData(xml.data(), xml.size());
-
- reader.initialize();
-
- if (0 == reader.interpret(confData)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if (elementXml != xml) {
- std::cout << "\nExpected: '" << xml << "'\n"
- << "Got: '" << elementXml << "'\n";
- RETURN_FALSE("RawData() failure for root element");
- }
-
- if ("<stage>Content</stage>" != stageXml) {
- RETURN_FALSE("RawData() failure for child element");
- }
-
- if ("Content" != stageContent) {
- RETURN_FALSE("content failure for child element");
- }
-
- return true;
-
- }
-
- bool testSimilarElementName() {
-
- std::string stageContent, stage1Content, stage2Content;
- CodeDweller::XMLReaderElement reader("elem");
-
- reader
- .Element("stage", stageContent)
- .End("stage")
- .Element("stage1", stage1Content)
- .End("stage1")
- .Element("stage2", stage2Content)
- .End("stage2")
- .End("elem");
-
- std::string xml;
- std::string expectedStageContent = "StageContent";
- std::string expectedStage1Content = "Stage1Content";
- std::string expectedStage2Content = "Stage2Content";
-
- xml =
- "<elem>\n"
- " <stage2>" + expectedStage2Content + "</stage2>\n"
- " <stage>" + expectedStageContent + "</stage>\n"
- " <stage1>" + expectedStage1Content + "</stage1>\n"
- "</elem>";
-
- CodeDweller::XMLReaderData confData(xml.data(), xml.size());
-
- reader.initialize();
-
- if (0 == reader.interpret(confData)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if (expectedStageContent != stageContent) {
- RETURN_FALSE("<stage> content read failure");
- }
-
- if (expectedStage1Content != stage1Content) {
- RETURN_FALSE("<stage1> content read failure");
- }
-
- if (expectedStage2Content != stage2Content) {
- RETURN_FALSE("<stage2> content read failure");
- }
-
- return true;
-
- }
-
- bool testWrongClosingTag() {
-
- std::string stageContent;
- CodeDweller::XMLReaderElement reader("elem");
-
- reader
- .Element("stage", stageContent)
- .End("stage")
- .End("elem");
-
- std::string xml;
- std::string expectedStageContent = "StageContent";
-
- xml =
- "<elem>\n"
- " <stage>" + expectedStageContent + "</xxx>\n"
- "</elem>";
-
- CodeDweller::XMLReaderData confData(xml.data(), xml.size());
-
- reader.initialize();
-
- if (0 != reader.interpret(confData)) {
- std::cout << "\nParsing '" << xml << "'\n";
- RETURN_FALSE("Error: Wrong closing tag not detected.");
- }
-
- return true;
-
- }
-
- bool testSimilarAttributeName() {
-
- std::string nameAttr, naAttr, nameLongAttr;
- CodeDweller::XMLReaderElement reader("elem");
-
- reader
- .Element("stage")
- .Attribute("name", nameAttr)
- .Attribute("na", naAttr)
- .Attribute("nameLong", nameLongAttr)
- .End("stage")
- .End("elem");
-
- std::string xml;
- std::string expectedNameAttr = "NameValue";
- std::string expectedNaAttr = "NaValue";
- std::string expectedNameLongAttr = "NameLongValue";
-
- xml =
- "<elem>\n"
- " <stage\n"
- " name='" + expectedNameAttr + "'\n"
- " na='" + expectedNaAttr + "'\n"
- " nameLong='" + expectedNameLongAttr + "'\n"
- "/></elem>";
-
- CodeDweller::XMLReaderData confData(xml.data(), xml.size());
-
- reader.initialize();
-
- if (0 == reader.interpret(confData)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if (expectedNameAttr != nameAttr) {
- RETURN_FALSE("\"name\" attribute read failure");
- }
-
- if (expectedNaAttr != naAttr) {
- RETURN_FALSE("\"na\" attribute read failure");
- }
-
- if (expectedNameLongAttr != nameLongAttr) {
- RETURN_FALSE("\"nameLong\" attribute read failure");
- }
-
- return true;
-
- }
-
- bool testInvalidAttribute() {
-
- std::string nameAttr;
- CodeDweller::XMLReaderElement reader("elem");
-
- reader
- .Element("stage")
- .Attribute("name", nameAttr)
- .End("stage")
- .End("elem");
-
- std::string xml;
-
- xml = "<elem><stage name=/></elem>";
-
- CodeDweller::XMLReaderData confData(xml.data(), xml.size());
-
- reader.initialize();
-
- if (0 != reader.interpret(confData)) {
- std::cout << "\nParsing '" << xml << "'\n";
- RETURN_FALSE("Error: Invalid attribute not detected.");
- }
-
- return true;
-
- }
-
- bool testHugeAttributeValue() {
-
- std::string xml = "<data name='messageContent' value='";
- std::string expectedValue;
- size_t nSize = 1000 * 1000 * 10;
-
- expectedValue.reserve(nSize);
- expectedValue.assign(nSize, 'x');
-
- xml.reserve(nSize + 512);
- xml.append(expectedValue);
- xml.append("'/>");
-
- CodeDweller::XMLReaderData confData(xml.data(), xml.size());
-
- CodeDweller::XMLReaderElement reader("data");
- std::string nameAttr, valueAttr;
-
- reader
- .Attribute("name", nameAttr)
- .Attribute("value", valueAttr, ">")
- .End("data");
-
- reader.initialize();
-
- if (0 == reader.interpret(confData)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if ("messageContent" != nameAttr) {
- RETURN_FALSE("\"name\" attribute read failure");
- }
-
- if (expectedValue != valueAttr) {
- RETURN_FALSE("\"value\" attribute read failure");
- }
-
- return true;
-
- }
-
- bool testNullAttributeValue() {
-
- std::string xml = "<data name='messageContent' value=''/>";
- CodeDweller::XMLReaderData confData(xml.data(), xml.size());
-
- CodeDweller::XMLReaderElement reader("data");
- std::string nameAttr, valueAttr, setAttr;
-
- reader
- .Attribute("name", nameAttr)
- .Attribute("value", valueAttr, ">")
- .Attribute("set", setAttr, ">")
- .End("data");
-
- reader.initialize();
-
- if (0 == reader.interpret(confData)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if ("messageContent" != nameAttr) {
- RETURN_FALSE("\"name\" attribute read failure");
- }
-
- if ("" != valueAttr) {
- std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
- RETURN_FALSE("\"value\" attribute read failure");
- }
-
- if (">" != setAttr) {
- RETURN_FALSE("\"set\" attribute read failure");
- }
-
- return true;
-
- }
-
- bool testIndicator() {
-
- std::string xml = "<top><data name='messageContent' value=''/></top>";
- CodeDweller::XMLReaderData confData(xml.data(), xml.size());
-
- CodeDweller::XMLReaderElement reader("top");
- std::string nameAttr, valueAttr, setAttr;
- bool foundDataElem;
- bool foundNameAttr, foundValueAttr, foundSetAttr;
-
- reader
- .Element("data")
- .indicator(foundDataElem)
- .Attribute("name", nameAttr).indicator(foundNameAttr)
- .Attribute("value", valueAttr, ">").indicator(foundValueAttr)
- .Attribute("set", setAttr, ">").indicator(foundSetAttr)
- .End("data")
- .End("top");
-
- reader.initialize();
-
- if (0 == reader.interpret(confData)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if ("messageContent" != nameAttr) {
- RETURN_FALSE("\"name\" attribute read failure");
- }
-
- if ("" != valueAttr) {
- std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
- RETURN_FALSE("\"value\" attribute read failure");
- }
-
- if (">" != setAttr) {
- RETURN_FALSE("\"set\" attribute read failure");
- }
-
- if (!foundDataElem) {
- RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
- }
-
- if (!foundNameAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- if (!foundValueAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- if (foundSetAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- xml = "<top><data1 name='messageContent' value=''/></top>";
-
- CodeDweller::XMLReaderData confData1(xml.data(), xml.size());
-
- reader.initialize();
-
- if (0 == reader.interpret(confData1)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if (foundDataElem) {
- RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
- }
-
- if (foundNameAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- if (foundValueAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- if (foundSetAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- return true;
-
- }
-
- class TestFunctor : public CodeDweller::AttributeFunctor {
-
- public:
-
- int numCalled = 0;
-
- void operator()(CodeDweller::XMLReaderAttribute& A,
- CodeDweller::XMLReaderData& D) {
- numCalled++;
- }
-
- };
-
- bool testIndicatorFunctor() {
-
- std::string xml =
- "<top><data name='messageContent' value='' from='hello' "
- "to='xx'/></top>";
- CodeDweller::XMLReaderData confData(xml.data(), xml.size());
-
- CodeDweller::XMLReaderElement reader("top");
- std::string nameAttr, valueAttr, setAttr, fromAttr, toAttr;
- bool foundDataElem;
- bool foundNameAttr, foundValueAttr, foundSetAttr, foundFromAttr,
- foundToAttr;
-
- TestFunctor nameFunctor, fromToFunctor;
-
- reader
- .Element("data")
- .indicator(foundDataElem)
- .Attribute("name", nameAttr)
- .indicator(foundNameAttr, nameFunctor)
- .Attribute("value", valueAttr, ">")
- .indicator(foundValueAttr)
- .Attribute("from", fromAttr, ">")
- .indicator(foundFromAttr, fromToFunctor)
- .Attribute("to", toAttr, ">")
- .indicator(foundToAttr, fromToFunctor)
- .Attribute("set", setAttr, ">")
- .indicator(foundSetAttr, nameFunctor)
- .End("data")
- .End("top");
-
- reader.initialize();
-
- if (0 == reader.interpret(confData)) {
- RETURN_FALSE("Error parsing XML: " + confData.Log.str());
- }
-
- if ("messageContent" != nameAttr) {
- RETURN_FALSE("\"name\" attribute read failure");
- }
-
- if ("" != valueAttr) {
- std::cout << "Expected \"\", got \"" << valueAttr << "\".\n";
- RETURN_FALSE("\"value\" attribute read failure");
- }
-
- if ("hello" != fromAttr) {
- std::cout << "Expected \"hello\", got \"" << fromAttr << "\".\n";
- RETURN_FALSE("\"from\" attribute read failure");
- }
-
- if ("xx" != toAttr) {
- std::cout << "Expected \"xx\", got \"" << fromAttr << "\".\n";
- RETURN_FALSE("\"to\" attribute read failure");
- }
-
- if (">" != setAttr) {
- RETURN_FALSE("\"set\" attribute read failure");
- }
-
- if (!foundDataElem) {
- RETURN_FALSE("\"XMLReaderElement::indicator\" failure");
- }
-
- if (!foundNameAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- if (!foundValueAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- if (!foundFromAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- if (!foundToAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- if (foundSetAttr) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- if (1 != nameFunctor.numCalled) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- if (2 != fromToFunctor.numCalled) {
- RETURN_FALSE("\"XMLReaderAttribute::indicator\" failure");
- }
-
- return true;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // End of tests ////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- int main()
- {
- std::cout << "CodeDweller::XMLReader unit tests" << std::endl
- << std::endl;
-
- RUN_TEST(testEmptyElement);
- RUN_TEST(testRawData);
- RUN_TEST(testSimilarElementName);
- RUN_TEST(testWrongClosingTag);
- RUN_TEST(testSimilarAttributeName);
- RUN_TEST(testInvalidAttribute);
- RUN_TEST(testHugeAttributeValue);
- RUN_TEST(testNullAttributeValue);
- RUN_TEST(testIndicator);
- RUN_TEST(testIndicatorFunctor);
-
- SUMMARY;
-
- return 0;
- }
|