diff --git a/snippets/CMakeLists.txt b/snippets/CMakeLists.txt index be599e4..9ebb729 100644 --- a/snippets/CMakeLists.txt +++ b/snippets/CMakeLists.txt @@ -39,7 +39,7 @@ endif() add_executable(compte_mots compte_mots.cpp) -add_executable(parseXML xmlParser.cpp xmlParser.h Circle.h) +add_executable(parseXML xmlParser.cpp xmlParser.h Circle.h Circle.cpp) find_path(PugiXML_INCLUDE_DIR pugixml.hpp) target_link_libraries(parseXML pugixml) @@ -48,7 +48,7 @@ target_link_directories(parseXML PRIVATE ${PugiXML_INCLUDE_DIR}) if(GTest_FOUND) - add_executable(xmlTest gTestXMLParser.cpp Circle.h xmlParser.h xmlParser.cpp) + add_executable(xmlTest gTestXMLParser.cpp Circle.h xmlParser.h xmlParser.cpp Circle.cpp) target_include_directories(xmlTest PRIVATE ${PugiXML_INCLUDE_DIR}) target_link_directories(xmlTest PRIVATE ${PugiXML_INCLUDE_DIR}) diff --git a/snippets/Circle.cpp b/snippets/Circle.cpp new file mode 100644 index 0000000..8b2243c --- /dev/null +++ b/snippets/Circle.cpp @@ -0,0 +1,15 @@ +// +// Created by trotfunky on 14/05/19. +// + +#include "Circle.h" + +namespace xmlParser +{ + Circle::Circle() : x(0), y(1), r(2), label("Test circle") + {} + + Circle::Circle(pugi::xml_node& node) : x(node.attribute("x").as_int(0)), y(node.attribute("y").as_int(1)), + r(node.attribute("r").as_int(2)), label(node.attribute("label").as_string("Test circle")) + {} +} \ No newline at end of file diff --git a/snippets/Circle.h b/snippets/Circle.h index d626800..2a90cab 100644 --- a/snippets/Circle.h +++ b/snippets/Circle.h @@ -10,22 +10,17 @@ namespace xmlParser { - typedef struct Circle + class Circle { + public: + Circle(); + explicit Circle(pugi::xml_node&); + int x; int y; int r; std::string label; - - void initCirle() { x = 0; y = 1; r = 2; label = "Test circle";} - void initCircle(pugi::xml_node node) - { - x = node.attribute("x").as_int(0); - y = node.attribute("y").as_int(1); - r = node.attribute("r").as_int(2); - label = node.attribute("label").as_string("Test circle"); - } - } Circle; + }; } #endif //SNIPPETS_CIRCLE_H diff --git a/snippets/gTestXMLParser.cpp b/snippets/gTestXMLParser.cpp index 4883700..5687076 100644 --- a/snippets/gTestXMLParser.cpp +++ b/snippets/gTestXMLParser.cpp @@ -9,8 +9,7 @@ TEST(readXML,initEmptyCircle) { - xmlParser::Circle cercle; - cercle.initCirle(); + xmlParser::Circle cercle = xmlParser::Circle(); ASSERT_EQ(cercle.x,0); ASSERT_EQ(cercle.y,1); @@ -20,9 +19,8 @@ TEST(readXML,initEmptyCircle) TEST(readXML,initEmptyCirclePugiXML) { - xmlParser::Circle cercle; + xmlParser::Circle cercle = xmlParser::Circle(); pugi::xml_node node; - cercle.initCircle(node); ASSERT_EQ(cercle.x,0); ASSERT_EQ(cercle.y,1); @@ -39,9 +37,13 @@ TEST(readXML,parseXMLCircle) pugi::xml_parse_result result = doc.load_string(xml.c_str()); EXPECT_NE(0,result); - xmlParser::Circle cercle; + pugi::xml_node node = doc.child("Circle"); + xmlParser::Circle cercle = xmlParser::Circle(node); - cercle.initCircle(doc.child("Circle")); + ASSERT_EQ(cercle.x,0); + ASSERT_EQ(cercle.y,1); + ASSERT_EQ(cercle.r,2); + ASSERT_EQ(cercle.label,"testCircle"); } int main(int argc, char** argv)