winIDEA SDK
persistent_vars.py
1# This script is licensed under BSD License, see file LICENSE.txt.
2#
3# (c) TASKING Germany GmbH, 2023
4
5import isystem.connect as ic
6import isystem.itest as it
7
8
9winidea_id = ''
10
11
12def verifyResult(results):
13 while results.hasNextTestResult():
14 result = results.nextTestResult()
15 if result.isError():
16 out = ic.CStringStream()
17 emitter = ic.EmitterFactory.createYamlEmitter(out)
18 emitter.startStream()
19 emitter.startDocument(False)
20 result.serializeErrorsOnly(emitter, None)
21 emitter.endDocument(False)
22 emitter.endStream()
23 print(out.getString())
24 else:
25 print("OK")
26
27
28def init_target() -> ic.ConnectionMgr:
29 cmgr = ic.ConnectionMgr()
30 cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
31
32 debug_ctrl = ic.CDebugFacade(cmgr)
33
34 # initialize the target - this may differ in your environment
35 debug_ctrl.download()
36 debug_ctrl.runUntilFunction("main")
37 debug_ctrl.waitUntilStopped()
38 return cmgr
39
40
41def run_tests(cmgr: ic.ConnectionMgr):
42 test_case = it.PTestCase(cmgr)
43
44 itest_ctrl = ic.CSystemTestController(cmgr)
45
46 itest_ctrl.createPersistentVariable('myvar1', 'int')
47 itest_ctrl.createPersistentVariable('myvar2', 'int')
48 itest_ctrl.createPersistentVariable('myvar3', 'int')
49 itest_ctrl.initPersistentVars()
50
51 itest_ctrl.modify('myvar1', '123')
52 itest_ctrl.modify('myvar2', '456')
53 itest_ctrl.modify('myvar3', '789')
54
55 print('Variable exists before the test: ', itest_ctrl.evaluate('myvar1,d'))
56 print('Variable exists before the test: ', itest_ctrl.evaluate('myvar2,d'))
57 print('Variable exists before the test: ', itest_ctrl.evaluate('myvar3,d'))
58
59 test_case.itest(
60 """
61 func: [sum, [myvar1, myvar2, myvar3], retVal]
62 expect:
63 - myvar1 == 123
64 - myvar2 == 456
65 - myvar3 == 789
66 - retVal == 1368
67 """,
68 None, False)
69
70 verifyResult(test_case.getTestResultsContainer())
71
72 print('Variable exists between tests: ', itest_ctrl.evaluate('myvar1,d'))
73 print('Variable exists between tests: ', itest_ctrl.evaluate('myvar2,d'))
74 print('Variable exists between tests: ', itest_ctrl.evaluate('myvar3,d'))
75
76 test_case.itest(
77 """
78 func: [sum, [123, 456, 789], retVal]
79 expect:
80 - myvar1 == 123
81 - myvar2 == 456
82 - myvar3 == 789
83 - retVal == (myvar1 + myvar2 + myvar3)
84 """,
85 None, False)
86
87 verifyResult(test_case.getTestResultsContainer())
88
89 print('Variable exists after tests: ', itest_ctrl.evaluate('myvar1,d'))
90 print('Variable exists after tests: ', itest_ctrl.evaluate('myvar2,d'))
91 print('Variable exists after tests: ', itest_ctrl.evaluate('myvar3,d'))
92
93 itest_ctrl.cleanPersistentVars()
94
95 try:
96 print('Variable should no longer exist after cleanup: ', itest_ctrl.evaluate('myvar1,d'))
97 print('Variable should no longer exist after cleanup: ', itest_ctrl.evaluate('myvar2,d'))
98 print('Variable should no longer exist after cleanup: ', itest_ctrl.evaluate('myvar3,d'))
99 except OSError:
100 print('\nPersistent variables no longer exist.')
101 else:
102 raise Exception('Variable should no longer exist after cleanup!')
103
104
105def main():
106 cmgr = init_target()
107 run_tests(cmgr)
108
109
110if __name__ == '__main__':
111 main()