Start of second practical exercise with XML and classes

This commit is contained in:
trotFunky 2019-05-14 13:07:25 +02:00
parent 004142f85a
commit 5567a9910b
4 changed files with 31 additions and 19 deletions

View file

@ -39,7 +39,7 @@ endif()
add_executable(compte_mots compte_mots.cpp) 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) find_path(PugiXML_INCLUDE_DIR pugixml.hpp)
target_link_libraries(parseXML pugixml) target_link_libraries(parseXML pugixml)
@ -48,7 +48,7 @@ target_link_directories(parseXML PRIVATE ${PugiXML_INCLUDE_DIR})
if(GTest_FOUND) 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_include_directories(xmlTest PRIVATE ${PugiXML_INCLUDE_DIR})
target_link_directories(xmlTest PRIVATE ${PugiXML_INCLUDE_DIR}) target_link_directories(xmlTest PRIVATE ${PugiXML_INCLUDE_DIR})

15
snippets/Circle.cpp Normal file
View file

@ -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"))
{}
}

View file

@ -10,22 +10,17 @@
namespace xmlParser namespace xmlParser
{ {
typedef struct Circle class Circle
{ {
public:
Circle();
explicit Circle(pugi::xml_node&);
int x; int x;
int y; int y;
int r; int r;
std::string label; 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 #endif //SNIPPETS_CIRCLE_H

View file

@ -9,8 +9,7 @@
TEST(readXML,initEmptyCircle) TEST(readXML,initEmptyCircle)
{ {
xmlParser::Circle cercle; xmlParser::Circle cercle = xmlParser::Circle();
cercle.initCirle();
ASSERT_EQ(cercle.x,0); ASSERT_EQ(cercle.x,0);
ASSERT_EQ(cercle.y,1); ASSERT_EQ(cercle.y,1);
@ -20,9 +19,8 @@ TEST(readXML,initEmptyCircle)
TEST(readXML,initEmptyCirclePugiXML) TEST(readXML,initEmptyCirclePugiXML)
{ {
xmlParser::Circle cercle; xmlParser::Circle cercle = xmlParser::Circle();
pugi::xml_node node; pugi::xml_node node;
cercle.initCircle(node);
ASSERT_EQ(cercle.x,0); ASSERT_EQ(cercle.x,0);
ASSERT_EQ(cercle.y,1); ASSERT_EQ(cercle.y,1);
@ -39,9 +37,13 @@ TEST(readXML,parseXMLCircle)
pugi::xml_parse_result result = doc.load_string(xml.c_str()); pugi::xml_parse_result result = doc.load_string(xml.c_str());
EXPECT_NE(0,result); 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) int main(int argc, char** argv)