ChimeraTK-ApplicationCore 04.06.00
Loading...
Searching...
No Matches
testPythonUserInputValidator.py
Go to the documentation of this file.
1import datetime as dt
2import os.path
3import sys
4import traceback
5
6
7# fmt: off
8# Hack to insert the python path for the locally compiled module in the
9# test script
10sys.path.insert(0, os.path.abspath(os.path.join(os.curdir, "..")))
11import PyApplicationCore as ac # NOQA
12# fmt: on
13
14
15class TestRunner(ac.ApplicationModule):
16 def __init__(self, owner, name, description):
17 super().__init__(owner, name, description)
18
19 self.in1 = ac.ScalarPushInputWB(
20 ac.DataType.int32, self, "in1", "", "First validated input"
21 )
22 self.in2 = ac.ArrayPushInputWB(
23 ac.DataType.int32, self, "in2", "", 5, "First validated input"
24 )
25 self.testError = ac.ScalarOutput(ac.DataType.string, self, "TestError", "", "")
26 self.validator = ac.UserInputValidator()
27 self.errorFunctionCalled = ac.VoidOutput(self, "errorFunctionCalled", "")
28
29 def prepare(self):
30 self.validator.add(
31 f"From Python: ({self.getName()}) in1 needs to be smaller than 10",
32 lambda: self.in1.get() < 10,
33 self.in1,
34 )
35 self.validator.add(
36 f"From Python: ({self.getName()}) Sum of in2 needs to be smaller than 10",
37 lambda: sum(self.in2.get()) < 10,
38 self.in2,
39 )
40 self.validator.add(
41 f"From Python: ({self.getName()}) Elements in in2 need to be smaller than in1",
42 lambda: all(v <= self.in1.get() for v in self.in2.get()),
43 self.in1,
44 self.in2,
45 )
46
47 # This is abusing that the return value of print is None so it is false and we do the write
48 self.validator.setErrorFunction(
49 lambda x: print(x) or self.errorFunctionCalled.write()
50 )
51 self.validator.setFallback(self.in1, 7)
52 self.validator.setFallback(self.in2, [1, 1, 1, 1, 1])
53
54 def mainLoop(self):
55 try:
56
57 group = self.readAnyGroup()
58 change = ac.TransferElementID()
59 assert self.validator.validate(change)
60 assert self.in1.get() == 7, "Using the set Fall-back value"
61 assert (self.in2.get() == [1, 1, 1, 1, 1]).all(), "Using the set fall-back value"
62
63 change = group.readAny()
64 assert not self.validator.validate(change)
65 assert self.in1.get() == 8, "Setting correct value"
66
67 change = group.readAny()
68 assert self.validator.validate(change)
69 assert self.in1.get() == 8, "Keeping previous correct value"
70
71 change = group.readAny()
72 assert not self.validator.validate(change)
73 assert (self.in2.get() == [2, 2, 2, 2, 1]).all(), "Setting a new value"
74
75 change = group.readAny()
76 assert self.validator.validate(change)
77 assert (self.in2.get() == [2, 2, 2, 2, 1]).all(), "Using the previous value"
78
79 change = group.readAny()
80 assert self.validator.validate(change)
81 assert (self.in2.get() == [2, 2, 2, 2, 1]).all(), "Using the previous value"
82
83 except AssertionError as e:
84 self.testError.setAndWrite("\n".join(traceback.format_exception(e)))
85
86
87ac.app.testRunner = TestRunner(
88 ac.app,
89 "UserInputValidatorTestRunner",
90 "Simple module for testing the UserInputValidator binding",
91)
void mainLoop() override