]> SALOME platform Git repositories - modules/shaper.git/blob - src/ConstructionAPI/Test/TestPoint.cpp
Salome HOME
Test addPoint
[modules/shaper.git] / src / ConstructionAPI / Test / TestPoint.cpp
1 #include <gtest/gtest.h>
2 #include <gmock/gmock.h>
3
4 #include <Events_Loop.h>
5 #include <Events_Error.h>
6
7 #include <ModelAPI_Feature.h>
8 #include <ModelHighAPI_Double.h>
9 #include <ConstructionAPI_Point.h>
10
11 #include "MockModelAPI_AttributeDouble.h"
12 #include "MockModelAPI_Data.h"
13 #include "MockModelAPI_Document.h"
14 #include "MockModelAPI_Feature.h"
15
16 using ::testing::_;
17 using ::testing::Return;
18 using ::testing::ReturnRefOfCopy;
19 using ::testing::Test;
20
21 // TODO(spo): should be common function
22 void null_deleter(void *) {}
23
24 class MockEvents_Listener : public Events_Listener {
25 public:
26   MOCK_METHOD1(processEvent,
27     void(const std::shared_ptr<Events_Message>& theMessage));
28 };
29
30 class ConstructionAPI_Point_Constructor_Test : public Test {
31 public:
32   MockEvents_Listener aErrorListener;
33   MockModelAPI_AttributeDouble aMockAttributeDouble;
34   MockModelAPI_Data aMockData;
35   MockModelAPI_Feature aMockFeature;
36   MockModelAPI_Document aMockDocument;
37
38   ConstructionAPI_Point_Constructor_Test() {
39     ON_CALL(aMockDocument, addFeature(_, _))
40       .WillByDefault(Return(std::shared_ptr<ModelAPI_Feature>(&aMockFeature, &null_deleter)));
41
42     ON_CALL(aMockFeature, getKind())
43       .WillByDefault(ReturnRefOfCopy(std::string("Point")));
44
45     ON_CALL(aMockFeature, data())
46       .WillByDefault(Return(std::shared_ptr<ModelAPI_Data>(&aMockData, &null_deleter)));
47
48     ON_CALL(aMockData, real(_))
49       .WillByDefault(Return(std::shared_ptr<ModelAPI_AttributeDouble>(&aMockAttributeDouble, &null_deleter)));
50
51     Events_Loop::loop()->registerListener(&aErrorListener, Events_Error::errorID());
52   }
53
54   ~ConstructionAPI_Point_Constructor_Test() {
55     Events_Loop::loop()->removeListener(&aErrorListener);
56   }
57
58   void testInitializeFeature() {
59     EXPECT_CALL(aMockFeature, getKind());
60
61     EXPECT_CALL(aMockFeature, data())
62       .Times(3);
63
64     EXPECT_CALL(aMockData, real("x"));
65     EXPECT_CALL(aMockData, real("y"));
66     EXPECT_CALL(aMockData, real("z"));
67   }
68
69   void testSetAttributes() {
70     EXPECT_CALL(aMockAttributeDouble, setValue(10));
71     EXPECT_CALL(aMockAttributeDouble, setText("20"));
72     EXPECT_CALL(aMockAttributeDouble, setText("x + 30"));
73
74     EXPECT_CALL(aMockFeature, execute());
75   }
76 };
77
78 TEST_F(ConstructionAPI_Point_Constructor_Test, GetEmptyFeature_SendException) {
79   FeaturePtr aFeature;
80
81   EXPECT_CALL(aErrorListener, processEvent(_));
82
83   ConstructionAPI_Point aPoint(aFeature);
84 }
85
86 TEST_F(ConstructionAPI_Point_Constructor_Test, GetEmptyFeatureAndValues_SendException) {
87   FeaturePtr aFeature;
88
89   EXPECT_CALL(aErrorListener, processEvent(_));
90
91   ConstructionAPI_Point aPoint(aFeature, 10, "20", std::string("x + 30"));
92 }
93
94 TEST_F(ConstructionAPI_Point_Constructor_Test, GetWrongFeature_SendException) {
95   FeaturePtr aFeature(&aMockFeature, &null_deleter);
96
97   ON_CALL(aMockFeature, getKind())
98     .WillByDefault(ReturnRefOfCopy(std::string("WrongKind")));
99
100   EXPECT_CALL(aMockFeature, getKind());
101   EXPECT_CALL(aErrorListener, processEvent(_));
102
103   ConstructionAPI_Point aPoint(aFeature);
104 }
105
106
107 TEST_F(ConstructionAPI_Point_Constructor_Test, GetFeature) {
108   FeaturePtr aFeature(&aMockFeature, &null_deleter);
109
110   testInitializeFeature();
111
112   ConstructionAPI_Point aPoint(aFeature);
113 }
114
115 TEST_F(ConstructionAPI_Point_Constructor_Test, GetFeatureAndValues) {
116   FeaturePtr aFeature(&aMockFeature, &null_deleter);
117
118   testInitializeFeature();
119   testSetAttributes();
120
121   ConstructionAPI_Point aPoint(aFeature, 10, "20", std::string("x + 30"));
122 }
123
124 TEST_F(ConstructionAPI_Point_Constructor_Test, addPoint) {
125   DocumentPtr aDocument(&aMockDocument, &null_deleter);
126
127   EXPECT_CALL(aMockDocument, addFeature("Point", true));
128
129   testInitializeFeature();
130
131   PointPtr aPoint = addPoint(aDocument);
132 }
133
134 TEST_F(ConstructionAPI_Point_Constructor_Test, addPointWithValues) {
135   DocumentPtr aDocument(&aMockDocument, &null_deleter);
136
137   EXPECT_CALL(aMockDocument, addFeature("Point", true));
138
139   testInitializeFeature();
140   testSetAttributes();
141
142   PointPtr aPoint = addPoint(aDocument, 10, "20", std::string("x + 30"));
143 }