API
 
Loading...
Searching...
No Matches
indiPropNode_test.cpp
Go to the documentation of this file.
1//#define CATCH_CONFIG_MAIN
2#include "../../../../tests/catch2/catch.hpp"
3
4#include <fstream>
5
6#include "../../../../libMagAOX/libMagAOX.hpp"
7
8#define XWC_XIGNODE_TEST
9#include "../indiPropNode.hpp"
10
12{
13 std::ofstream fout("/tmp/xigNode_test.xml");
14 fout << "<mxfile host=\"test\">\n";
15 fout << " <diagram id=\"test\" name=\"test\">\n";
16 fout << " <mxGraphModel>\n";
17 fout << " <root>\n";
18 fout << " <mxCell id=\"0\"/>\n";
19 fout << " <mxCell id=\"1\" parent=\"0\"/>\n";
20 fout << " <mxCell id=\"node:telescope\">\n";
21 fout << "</mxCell>\n";
22 fout << " </root>\n";
23 fout << " </mxGraphModel>\n";
24 fout << " </diagram>\n";
25 fout << "</mxfile>\n";
26 fout.close();
27}
28
29SCENARIO( "Creating and configuring an indiPropNode", "[instGraph::indiPropNode]" )
30{
31 GIVEN("a valid XML file, a valid config file")
32 {
33 WHEN("node is in file, default config")
34 {
35 ingr::instGraphXML parentGraph;
36 writeXML();
37 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
38 {"type", "propKey", "propEl", "propVal"},
39 {"indiProp", "tel.dome", "status", "on"} );
40 mx::app::appConfigurator config;
41 config.readConfig("/tmp/indiPropNode_test.conf");
42
43 std::string emsg;
44
45 int rv = parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
46
47 REQUIRE(rv == 0);
48 REQUIRE(emsg == "");
49
50 indiPropNode * tsn = nullptr;
51 bool pass = false;
52 try
53 {
54 tsn = new indiPropNode("telescope", &parentGraph);
55 pass = true;
56 }
57 catch(const std::exception & e)
58 {
59 std::cerr << e.what() << "\n";
60 }
61
62 REQUIRE(pass == true);
63 REQUIRE(tsn != nullptr);
64
65 REQUIRE( tsn->name() == "telescope");
66 REQUIRE( tsn->node()->name() == "telescope");
67
68 pass = false;
69 try
70 {
71 tsn->loadConfig(config);
72 pass = true;
73 }
74 catch(const std::exception & e)
75 {
76 std::cerr << e.what() << "\n";
77 }
78
79 REQUIRE(pass == true);
80
81 //check config-ed values
82 REQUIRE(tsn->propKey() == "tel.dome");
83 REQUIRE(tsn->propEl() == "status");
84 REQUIRE(tsn->propValStr() == "on");
85 REQUIRE(tsn->propValNum() == std::numeric_limits<double>::lowest());
86 REQUIRE(tsn->propValSw() == pcf::IndiElement::SwitchStateType::UnknownSwitchState);
87 REQUIRE(tsn->type() == pcf::IndiProperty::Type::Unknown);
88 REQUIRE(tsn->tol() == 1e-7);
89 REQUIRE(tsn->state() == false);
90
91 }
92 WHEN("node is in file, config setting tol")
93 {
94 ingr::instGraphXML parentGraph;
95 writeXML();
96 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope","telescope"},
97 {"type", "propKey", "propEl", "propVal", "tol"},
98 {"indiProp", "tel.dome", "status", "on", "1e-8"} );
99 mx::app::appConfigurator config;
100 config.readConfig("/tmp/indiPropNode_test.conf");
101
102 std::string emsg;
103
104 int rv = parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
105
106 REQUIRE(rv == 0);
107 REQUIRE(emsg == "");
108
109 indiPropNode * tsn = nullptr;
110 bool pass = false;
111 try
112 {
113 tsn = new indiPropNode("telescope", &parentGraph);
114 pass = true;
115 }
116 catch(const std::exception & e)
117 {
118 std::cerr << e.what() << "\n";
119 }
120
121 REQUIRE(pass == true);
122 REQUIRE(tsn != nullptr);
123
124 REQUIRE( tsn->name() == "telescope");
125 REQUIRE( tsn->node()->name() == "telescope");
126
127 pass = false;
128 try
129 {
130 tsn->loadConfig(config);
131 pass = true;
132 }
133 catch(const std::exception & e)
134 {
135 std::cerr << e.what() << "\n";
136 }
137
138 REQUIRE(pass == true);
139
140 //check config-ed values
141 REQUIRE(tsn->propKey() == "tel.dome");
142 REQUIRE(tsn->propEl() == "status");
143 REQUIRE(tsn->propValStr() == "on");
144 REQUIRE(tsn->propValNum() == std::numeric_limits<double>::lowest());
145 REQUIRE(tsn->propValSw() == pcf::IndiElement::SwitchStateType::UnknownSwitchState);
146 REQUIRE(tsn->type() == pcf::IndiProperty::Type::Unknown);
147 REQUIRE(tsn->tol() == 1e-8);
148 REQUIRE(tsn->state() == false);
149
150 }
151
152 WHEN("node is in file, handling a text property")
153 {
154 ingr::instGraphXML parentGraph;
155 writeXML();
156 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
157 {"type", "propKey", "propEl", "propVal"},
158 {"indiProp", "tel.dome", "status", "on"} );
159 mx::app::appConfigurator config;
160 config.readConfig("/tmp/indiPropNode_test.conf");
161
162 std::string emsg;
163
164 parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
165
166 indiPropNode * tsn = new indiPropNode("telescope", &parentGraph);
167 tsn->loadConfig(config);
168
169 pcf::IndiProperty ip(pcf::IndiProperty::Text);
170 ip.setDevice("tel");
171 ip.setName("dome");
172 ip.add(pcf::IndiElement("status"));
173 ip["status"]="on";
174
175 tsn->handleSetProperty(ip);
176 REQUIRE(tsn->type() == pcf::IndiProperty::Text);
177 REQUIRE(tsn->state() == true);
178
179 ip["status"]="off";
180 tsn->handleSetProperty(ip);
181 REQUIRE(tsn->state() == false);
182 }
183
184 WHEN("node is in file, handling a number property")
185 {
186 ingr::instGraphXML parentGraph;
187 writeXML();
188 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
189 {"type", "propKey", "propEl", "propVal"},
190 {"indiProp", "tel.dome", "status", "1.5"} );
191 mx::app::appConfigurator config;
192 config.readConfig("/tmp/indiPropNode_test.conf");
193
194 std::string emsg;
195
196 parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
197
198 indiPropNode * tsn = new indiPropNode("telescope", &parentGraph);
199 tsn->loadConfig(config);
200
201 pcf::IndiProperty ip(pcf::IndiProperty::Number);
202 ip.setDevice("tel");
203 ip.setName("dome");
204 ip.add(pcf::IndiElement("status"));
205 ip["status"]="1.5";
206
207 tsn->handleSetProperty(ip);
208 REQUIRE(tsn->type() == pcf::IndiProperty::Number);
209 REQUIRE(tsn->propValNum() == 1.5);
210 REQUIRE(tsn->state() == true);
211
212
213 ip["status"]="1.6";
214 tsn->handleSetProperty(ip);
215 REQUIRE(tsn->state() == false);
216 }
217
218 WHEN("node is in file, handling a switch property")
219 {
220 ingr::instGraphXML parentGraph;
221 writeXML();
222 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
223 {"type", "propKey", "propEl", "propVal"},
224 {"indiProp", "tel.dome", "status", "on"} );
225 mx::app::appConfigurator config;
226 config.readConfig("/tmp/indiPropNode_test.conf");
227
228 std::string emsg;
229
230 parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
231
232 indiPropNode * tsn = new indiPropNode("telescope", &parentGraph);
233 tsn->loadConfig(config);
234
235 pcf::IndiProperty ip(pcf::IndiProperty::Switch);
236 ip.setDevice("tel");
237 ip.setName("dome");
238 ip.add(pcf::IndiElement("status"));
239 ip["status"].setSwitchState(pcf::IndiElement::SwitchStateType::On);
240
241 tsn->handleSetProperty(ip);
242 REQUIRE(tsn->type() == pcf::IndiProperty::Switch);
243 REQUIRE(tsn->propValSw() == pcf::IndiElement::SwitchStateType::On);
244 REQUIRE(tsn->state() == true);
245
246
247 ip["status"].setSwitchState(pcf::IndiElement::SwitchStateType::Off);
248 tsn->handleSetProperty(ip);
249 REQUIRE(tsn->state() == false);
250 }
251 }
252
253 GIVEN("invalid configs")
254 {
255 WHEN("parent graph is null, default config")
256 {
257 indiPropNode * tsn = nullptr;
258 bool pass = false;
259 try
260 {
261 tsn = new indiPropNode("telescope", nullptr);
262 pass = true;
263 }
264 catch(const std::exception & e)
265 {
266 std::cerr << e.what() << "\n";
267 }
268
269 REQUIRE(pass == false);
270 REQUIRE(tsn == nullptr);
271 }
272 WHEN("node not in file, default config")
273 {
274 ingr::instGraphXML parentGraph;
275 writeXML();
276 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
277 {"type", "propKey", "propEl", "propVal"},
278 {"indiProp", "tel.dome", "status", "on"} );
279 mx::app::appConfigurator config;
280 config.readConfig("/tmp/indiPropNode_test.conf");
281
282 std::string emsg;
283
284 int rv = parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
285
286 REQUIRE(rv == 0);
287 REQUIRE(emsg == "");
288
289 indiPropNode * tsn = nullptr;
290 bool pass = false;
291 try
292 {
293 tsn = new indiPropNode("telescope2", &parentGraph);
294 pass = true;
295 }
296 catch(const std::exception & e)
297 {
298 std::cerr << e.what() << "\n";
299 }
300
301 REQUIRE(pass == false);
302 REQUIRE(tsn == nullptr);
303 }
304
305 WHEN("node is in file, default config, wrong node type")
306 {
307 ingr::instGraphXML parentGraph;
308 writeXML();
309 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
310 {"type", "propKey", "propEl", "propVal"},
311 {"fakeProp", "tel.dome", "status", "on"} );
312 mx::app::appConfigurator config;
313 config.readConfig("/tmp/indiPropNode_test.conf");
314
315 std::string emsg;
316
317 int rv = parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
318
319 REQUIRE(rv == 0);
320 REQUIRE(emsg == "");
321
322 indiPropNode * tsn = nullptr;
323 bool pass = false;
324 try
325 {
326 tsn = new indiPropNode("telescope", &parentGraph);
327 pass = true;
328 }
329 catch(const std::exception & e)
330 {
331 std::cerr << e.what() << "\n";
332 }
333
334 REQUIRE(pass == true);
335 REQUIRE(tsn != nullptr);
336
337 REQUIRE( tsn->name() == "telescope");
338 REQUIRE( tsn->node()->name() == "telescope");
339
340 pass = false;
341 try
342 {
343 tsn->loadConfig(config);
344 pass = true;
345 }
346 catch(const std::exception & e)
347 {
348 std::cerr << e.what() << "\n";
349 }
350
351 REQUIRE(pass == false);
352
353 }
354 WHEN("node is in file, default config, propKey empty")
355 {
356 ingr::instGraphXML parentGraph;
357 writeXML();
358 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
359 {"type", "propKey", "propEl", "propVal"},
360 {"indiProp", "", "status", "on"} );
361 mx::app::appConfigurator config;
362 config.readConfig("/tmp/indiPropNode_test.conf");
363
364 std::string emsg;
365
366 int rv = parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
367
368 REQUIRE(rv == 0);
369 REQUIRE(emsg == "");
370
371 indiPropNode * tsn = nullptr;
372 bool pass = false;
373 try
374 {
375 tsn = new indiPropNode("telescope", &parentGraph);
376 pass = true;
377 }
378 catch(const std::exception & e)
379 {
380 std::cerr << e.what() << "\n";
381 }
382
383 REQUIRE(pass == true);
384 REQUIRE(tsn != nullptr);
385
386 REQUIRE( tsn->name() == "telescope");
387 REQUIRE( tsn->node()->name() == "telescope");
388
389 pass = false;
390 try
391 {
392 tsn->loadConfig(config);
393 pass = true;
394 }
395 catch(const std::exception & e)
396 {
397 std::cerr << e.what() << "\n";
398 }
399
400 REQUIRE(pass == false);
401
402 }
403 WHEN("node is in file, default config, propEl empty")
404 {
405 ingr::instGraphXML parentGraph;
406 writeXML();
407 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
408 {"type", "propKey", "propEl", "propVal"},
409 {"indiProp", "tel.dome", "", "on"} );
410 mx::app::appConfigurator config;
411 config.readConfig("/tmp/indiPropNode_test.conf");
412
413 std::string emsg;
414
415 int rv = parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
416
417 REQUIRE(rv == 0);
418 REQUIRE(emsg == "");
419
420 indiPropNode * tsn = nullptr;
421 bool pass = false;
422 try
423 {
424 tsn = new indiPropNode("telescope", &parentGraph);
425 pass = true;
426 }
427 catch(const std::exception & e)
428 {
429 std::cerr << e.what() << "\n";
430 }
431
432 REQUIRE(pass == true);
433 REQUIRE(tsn != nullptr);
434
435 REQUIRE( tsn->name() == "telescope");
436 REQUIRE( tsn->node()->name() == "telescope");
437
438 pass = false;
439 try
440 {
441 tsn->loadConfig(config);
442 pass = true;
443 }
444 catch(const std::exception & e)
445 {
446 std::cerr << e.what() << "\n";
447 }
448
449 REQUIRE(pass == false);
450
451 }
452 WHEN("node is in file, default config, propVal empty")
453 {
454 ingr::instGraphXML parentGraph;
455 writeXML();
456 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
457 {"type", "propKey", "propEl", "propVal"},
458 {"indiProp", "tel.come", "status", ""} );
459 mx::app::appConfigurator config;
460 config.readConfig("/tmp/indiPropNode_test.conf");
461
462 std::string emsg;
463
464 int rv = parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
465
466 REQUIRE(rv == 0);
467 REQUIRE(emsg == "");
468
469 indiPropNode * tsn = nullptr;
470 bool pass = false;
471 try
472 {
473 tsn = new indiPropNode("telescope", &parentGraph);
474 pass = true;
475 }
476 catch(const std::exception & e)
477 {
478 std::cerr << e.what() << "\n";
479 }
480
481 REQUIRE(pass == true);
482 REQUIRE(tsn != nullptr);
483
484 REQUIRE( tsn->name() == "telescope");
485 REQUIRE( tsn->node()->name() == "telescope");
486
487 pass = false;
488 try
489 {
490 tsn->loadConfig(config);
491 pass = true;
492 }
493 catch(const std::exception & e)
494 {
495 std::cerr << e.what() << "\n";
496 }
497
498 REQUIRE(pass == false);
499
500 }
501 WHEN("a number property with invalid propVal")
502 {
503 ingr::instGraphXML parentGraph;
504 writeXML();
505 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
506 {"type", "propKey", "propEl", "propVal"},
507 {"indiProp", "tel.dome", "status", "abcde"} );
508 mx::app::appConfigurator config;
509 config.readConfig("/tmp/indiPropNode_test.conf");
510
511 std::string emsg;
512
513 parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
514
515 indiPropNode * tsn = new indiPropNode("telescope", &parentGraph);
516 tsn->loadConfig(config);
517
518 pcf::IndiProperty ip(pcf::IndiProperty::Number);
519 ip.setDevice("tel");
520 ip.setName("dome");
521 ip.add(pcf::IndiElement("status"));
522 ip["status"]="1.5";
523
524 bool pass = false;
525 try
526 {
527 tsn->handleSetProperty(ip);
528 pass = true;
529 }
530 catch(const std::exception& e)
531 {
532 std::cerr << e.what() << '\n';
533 }
534
535 REQUIRE(pass == false);
536 }
537 WHEN("invalid switch property")
538 {
539 ingr::instGraphXML parentGraph;
540 writeXML();
541 mx::app::writeConfigFile( "/tmp/indiPropNode_test.conf", {"telescope","telescope","telescope","telescope"},
542 {"type", "propKey", "propEl", "propVal"},
543 {"indiProp", "tel.dome", "status", "qq"} );
544 mx::app::appConfigurator config;
545 config.readConfig("/tmp/indiPropNode_test.conf");
546
547 std::string emsg;
548
549 parentGraph.loadXMLFile(emsg, "/tmp/xigNode_test.xml");
550
551 indiPropNode * tsn = new indiPropNode("telescope", &parentGraph);
552 tsn->loadConfig(config);
553
554 pcf::IndiProperty ip(pcf::IndiProperty::Switch);
555 ip.setDevice("tel");
556 ip.setName("dome");
557 ip.add(pcf::IndiElement("status"));
558 ip["status"].setSwitchState(pcf::IndiElement::SwitchStateType::On);
559
560 bool pass = false;
561 try
562 {
563 tsn->handleSetProperty(ip);
564 pass = true;
565 }
566 catch(const std::exception& e)
567 {
568 std::cerr << e.what() << '\n';
569 }
570
571 REQUIRE(pass == false);
572 }
573 }
574}
An instGraph node which tracks a specific INDI property and element of that property.
void propEl(const std::string &pe)
Set the element of the INDI property to track.
const pcf::IndiProperty::Type & type() const
Get the type of the INDI property being tracked.
const pcf::IndiElement::SwitchStateType & propValSw()
Get the target value of the INDI element if it's a switch.
void loadConfig(mx::app::appConfigurator &config)
Configure this node form an appConfigurator.
virtual int handleSetProperty(const pcf::IndiProperty &ipRecv)
INDI SetProperty callback.
const bool & state() const
Get the current value of the comparison.
void propKey(const std::string &pk)
Set the unique key of the INDI property to track.
void propValStr(const std::string &pv)
Set the target value of the INDI element.
const double & tol() const
Get the tolerance used for numeric comparison.
const double & propValNum() const
Get the target value of the INDI element if it's a number.
ingr::instNode * node()
Get the pointer to the underlying node.
Definition xigNode.hpp:121
std::string name()
Get the name of this node.
Definition xigNode.hpp:106
void writeXML()
SCENARIO("Creating and configuring an indiPropNode", "[instGraph::indiPropNode]")