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