PageRenderTime 61ms CodeModel.GetById 10ms app.highlight 48ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/testXMLUtil.cpp

https://github.com/deltaforge/nebu-common-cpp
C++ | 253 lines | 205 code | 45 blank | 3 comment | 0 complexity | 212f4af65a7427eeb20b4c08dc859bed MD5 | raw file
  1
  2#include "gtest/gtest.h"
  3#include "gmock/gmock.h"
  4
  5#include "tinyxml2.h"
  6
  7#include "nebu/util/exceptions.h"
  8#include "nebu/util/xmlUtil.h"
  9
 10// Using declarations - tinyxml2
 11using tinyxml2::XMLAttribute;
 12using tinyxml2::XMLDocument;
 13using tinyxml2::XMLElement;
 14// Using declarations - nebu-common
 15using nebu::common::XMLUtil;
 16using nebu::common::XMLParseException;
 17
 18// Using declarations - gtest/gmock
 19using testing::Eq;
 20using testing::IsNull;
 21using testing::NotNull;
 22using testing::StrEq;
 23
 24TEST(XMLUtilTest, testParseDocumentFromStringWithEmptyString) {
 25	try {
 26		XMLDocument doc;
 27		XMLUtil::parseDocumentFromString(doc, "");
 28		FAIL() << "Expected XMLParseException";
 29	} catch (XMLParseException &expected) {
 30		SUCCEED();
 31	}
 32}
 33
 34TEST(XMLUtilTest, testParseDocumentFromStringWithInvalidXML) {
 35	try {
 36		XMLDocument doc;
 37		XMLUtil::parseDocumentFromString(doc, "<element>Missing end tag");
 38		FAIL() << "Expected XMLParseException";
 39	} catch (XMLParseException &expected) {
 40		SUCCEED();
 41	}
 42}
 43
 44TEST(XMLUtilTest, testParseDocumentFromStringWithValidXML) {
 45	XMLDocument doc;
 46	XMLUtil::parseDocumentFromString(doc, "<rootElement />");
 47
 48	EXPECT_THAT(doc.Error(), Eq(false));
 49}
 50
 51TEST(XMLUtilTest, testFindChildElementOfDocForNonExistingNameNonMandatory) {
 52	XMLDocument doc;
 53	XMLUtil::parseDocumentFromString(doc, "<rootElement />");
 54
 55	const XMLElement *childElement = XMLUtil::findChildElement(doc, "nonExistingChild");
 56	EXPECT_THAT(childElement, IsNull());
 57}
 58
 59TEST(XMLUtilTest, testFindChildElementOfDocForNonExistingNameMandatory) {
 60	XMLDocument doc;
 61	XMLUtil::parseDocumentFromString(doc, "<rootElement />");
 62
 63	try {
 64		XMLUtil::findChildElement(doc, "nonExistingChild", true);
 65		FAIL() << "Expected XMLParseException";
 66	} catch (XMLParseException &expected) {
 67		SUCCEED();
 68	}
 69}
 70
 71TEST(XMLUtilTest, testFindChildElementOfDocForExistingName) {
 72	XMLDocument doc;
 73	XMLUtil::parseDocumentFromString(doc, "<rootElement />");
 74
 75	const XMLElement *childElement = XMLUtil::findChildElement(doc, "rootElement", true);
 76	EXPECT_THAT(childElement, NotNull());
 77	EXPECT_THAT(childElement->Name(), StrEq("rootElement"));
 78}
 79
 80TEST(XMLUtilTest, testFindChildElementOfNullElem) {
 81	try {
 82		XMLUtil::findChildElement(NULL, "nonExistentElement");
 83		FAIL() << "Expected XMLParseException";
 84	} catch (XMLParseException &expected) {
 85		SUCCEED();
 86	}
 87}
 88
 89TEST(XMLUtilTest, testFindChildElementOfElemForNonExistingNameNonMandatory) {
 90	XMLDocument doc;
 91	XMLUtil::parseDocumentFromString(doc, "<rootElement><childElement /></rootElement>");
 92
 93	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
 94	const XMLElement *childElement = XMLUtil::findChildElement(rootElement, "nonExistentElement");
 95	EXPECT_THAT(childElement, IsNull());
 96}
 97
 98TEST(XMLUtilTest, testFindChildElementOfElemForNonExistingNameMandatory) {
 99	XMLDocument doc;
100	XMLUtil::parseDocumentFromString(doc, "<rootElement><childElement /></rootElement>");
101
102	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
103	try {
104		XMLUtil::findChildElement(rootElement, "nonExistentElement", true);
105		FAIL() << "Expected XMLParseException";
106	} catch (XMLParseException &expected) {
107		SUCCEED();
108	}
109}
110
111TEST(XMLUtilTest, testFindChildElementOfElemForExistingName) {
112	XMLDocument doc;
113	XMLUtil::parseDocumentFromString(doc, "<rootElement><childElement /></rootElement>");
114
115	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
116	const XMLElement *childElement = XMLUtil::findChildElement(rootElement, "childElement", true);
117	EXPECT_THAT(childElement, NotNull());
118	EXPECT_THAT(childElement->Name(), StrEq("childElement"));
119}
120
121TEST(XMLUtilTest, testFindNextSiblingForNullElem) {
122	try {
123		XMLUtil::findNextSibling(NULL, "nonExistentElement");
124		FAIL() << "Expected XMLParseException";
125	} catch (XMLParseException &expected) {
126		SUCCEED();
127	}
128}
129
130TEST(XMLUtilTest, testFindNextSiblingForNonExistingNameNonMandatory) {
131	XMLDocument doc;
132	XMLUtil::parseDocumentFromString(doc, "<rootElement /><siblingElement />");
133
134	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
135	const XMLElement *siblingElement = XMLUtil::findNextSibling(rootElement, "nonExistentElement");
136	EXPECT_THAT(siblingElement, IsNull());
137}
138
139TEST(XMLUtilTest, testFindNextSiblingForNonExistingNameMandatory) {
140	XMLDocument doc;
141	XMLUtil::parseDocumentFromString(doc, "<rootElement /><siblingElement />");
142
143	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
144	try {
145		XMLUtil::findNextSibling(rootElement, "nonExistentElement", true);
146		FAIL() << "Expected XMLParseException";
147	} catch (XMLParseException &expected) {
148		SUCCEED();
149	}
150}
151
152TEST(XMLUtilTest, testFindNextSiblingForExistingName) {
153	XMLDocument doc;
154	XMLUtil::parseDocumentFromString(doc, "<rootElement /><siblingElement />");
155
156	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
157	const XMLElement *siblingElement = XMLUtil::findNextSibling(rootElement, "siblingElement", true);
158	EXPECT_THAT(siblingElement, NotNull());
159	EXPECT_THAT(siblingElement->Name(), StrEq("siblingElement"));
160}
161
162TEST(XMLUtilTest, testFindNextSiblingChain) {
163	XMLDocument doc;
164	XMLUtil::parseDocumentFromString(doc, "<rootElement /><siblingElement /><siblingElement />");
165
166	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
167	const XMLElement *siblingElementA = XMLUtil::findNextSibling(rootElement, "siblingElement", true);
168	EXPECT_THAT(siblingElementA, NotNull());
169	EXPECT_THAT(siblingElementA->Name(), StrEq("siblingElement"));
170
171	const XMLElement *siblingElementB = XMLUtil::findNextSibling(siblingElementA, "siblingElement", true);
172	EXPECT_THAT(siblingElementB, NotNull());
173	EXPECT_THAT(siblingElementB->Name(), StrEq("siblingElement"));
174
175	const XMLElement *siblingElementC = XMLUtil::findNextSibling(siblingElementB, "siblingElement");
176	EXPECT_THAT(siblingElementC, IsNull());
177}
178
179TEST(XMLUtilTest, testFindAttributeOfNullElem) {
180	try {
181		XMLUtil::findAttribute(NULL, "nonExistentAttribute");
182		FAIL() << "Expected XMLParseException";
183	} catch (XMLParseException &expected) {
184		SUCCEED();
185	}
186}
187
188TEST(XMLUtilTest, testFindAttributeForNonExistingNameNonMandatory) {
189	XMLDocument doc;
190	XMLUtil::parseDocumentFromString(doc, "<rootElement attributeA='An attribute'/>");
191
192	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
193	const XMLAttribute *attribute = XMLUtil::findAttribute(rootElement, "nonExistentAttribute");
194	EXPECT_THAT(attribute, IsNull());
195}
196
197TEST(XMLUtilTest, testFindAttributeForNonExistingNameMandatory) {
198	XMLDocument doc;
199	XMLUtil::parseDocumentFromString(doc, "<rootElement attributeA='An attribute' />");
200
201	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
202	try {
203		XMLUtil::findAttribute(rootElement, "nonExistentAttribute", true);
204		FAIL() << "Expected XMLParseException";
205	} catch (XMLParseException &expected) {
206		SUCCEED();
207	}
208}
209
210TEST(XMLUtilTest, testFindAttributeForExistingName) {
211	XMLDocument doc;
212	XMLUtil::parseDocumentFromString(doc, "<rootElement attributeA='An attribute' />");
213
214	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
215	const XMLAttribute *attribute = XMLUtil::findAttribute(rootElement, "attributeA", true);
216	EXPECT_THAT(attribute, NotNull());
217	EXPECT_THAT(attribute->Name(), StrEq("attributeA"));
218	EXPECT_THAT(attribute->Value(), StrEq("An attribute"));
219}
220
221TEST(XMLUtilTest, testVerifyElementNameOfNullElem) {
222	try {
223		XMLUtil::verifyElementName(NULL, "incorrectName");
224		FAIL() << "Expected XMLParseException";
225	} catch (XMLParseException &expected) {
226		SUCCEED();
227	}
228}
229
230TEST(XMLUtilTest, testVerifyElementNameWithIncorrectName) {
231	XMLDocument doc;
232	XMLUtil::parseDocumentFromString(doc, "<rootElement />");
233	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
234
235	try {
236		XMLUtil::verifyElementName(rootElement, "incorrectName");
237		FAIL() << "Expected XMLParseException";
238	} catch (XMLParseException &expected) {
239		SUCCEED();
240	}
241}
242
243TEST(XMLUtilTest, testVerifyElementNameWithCorrectName) {
244	XMLDocument doc;
245	XMLUtil::parseDocumentFromString(doc, "<rootElement />");
246	const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
247	XMLUtil::verifyElementName(rootElement, "rootElement");
248}
249
250int main(int argc, char **argv) {
251	testing::InitGoogleMock(&argc, argv);
252	return RUN_ALL_TESTS();
253}