-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_summary.py
318 lines (269 loc) · 9.83 KB
/
test_summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from dataclasses import dataclass
from typing import Optional
import pytest
from .. import transform as tf
from ..dependency_store import DependencyStore, PlcProjectMetadata
from ..summary import DeclarationSummary, MethodSummary, PropertySummary
@dataclass
class DeclarationCheck:
name: str
base_type: str
value: Optional[str] = None
location: Optional[str] = None
comments: Optional[list[str]] = None
def check_declarations(
container: dict[str, DeclarationSummary],
checks: list[DeclarationCheck]
):
"""
Check that ``container`` has the listed declarations - and only those.
Parameters
----------
container : dict[str, DeclarationSummary]
checks : list[DeclarationCheck]
"""
for check in checks:
try:
summary = container[check.name]
except KeyError:
raise KeyError(f"{check.name} not listed in the declaration section")
assert summary.base_type == check.base_type
if check.value is None:
assert summary.value == "None" # TODO?
if hasattr(summary.item, "init"):
assert summary.item.init.value is None
else:
assert summary.value == check.value
assert str(summary.item.init.value) == check.value
if check.comments is not None:
comments = list(str(comment) for comment in summary.item.meta.comments)
assert comments == check.comments
if check.location is not None:
assert summary.location == check.location
all_decls = {str(decl) for decl in container}
expected_decls = {str(decl.name) for decl in checks}
assert all_decls == expected_decls
@pytest.fixture
def project_a(store: DependencyStore) -> PlcProjectMetadata:
# This is project a from the git submodule included with the blark test
# suite
proj, = store.get_dependency("project_a")
return proj
def test_project_a_summary(project_a: PlcProjectMetadata):
summary = project_a.summary
assert list(summary.function_blocks) == ["FB_ProjectA"]
fb_projecta = summary.function_blocks["FB_ProjectA"]
assert list(fb_projecta.declarations) == ["fValue"]
assert fb_projecta.declarations["fValue"].base_type == "LREAL"
assert "fValue := 1.0;" in str(fb_projecta.implementation)
st_projecta = summary.data_types["ST_ProjectA"]
assert list(st_projecta.declarations) == ["iValue"]
assert st_projecta.declarations["iValue"].base_type == "INT"
main = summary.programs["MAIN"]
assert main.implementation is None
assert list(main.declarations) == []
gvl = summary.globals["GVL_ProjectA"]
gvl_bprojecta = gvl.declarations["bProjectA"]
assert gvl_bprojecta.base_type == "BOOL"
assert isinstance(gvl_bprojecta.item.init, tf.TypeInitialization)
assert str(gvl_bprojecta.item.init.value) == "FALSE"
@pytest.fixture
def project_b(store: DependencyStore) -> PlcProjectMetadata:
# This is project b from the git submodule included with the blark test
# suite
proj, = store.get_dependency("project_b")
return proj
def test_project_b_summary(project_b: PlcProjectMetadata):
summary = project_b.summary
st_projectb = summary.data_types["ST_ProjectB"]
assert list(st_projectb.declarations) == ["iProjectB"]
assert st_projectb.declarations["iProjectB"].base_type == "INT"
main = summary.programs["MAIN"]
assert str(main.implementation) == "fbLogger();"
assert list(main.declarations) == ["bProjectB", "fbLogger"]
gvl = summary.globals["GVL_ProjectB"]
gvl_bprojecta = gvl.declarations["bProjectB"]
assert gvl_bprojecta.base_type == "BOOL"
assert isinstance(gvl_bprojecta.item.init, tf.TypeInitialization)
assert gvl_bprojecta.item.init.value is None
@pytest.fixture
def twincat_general_281(store: DependencyStore) -> PlcProjectMetadata:
# This is project lcls-twincat-general v2.8.1 from the git submodule
# included with the blark test suite.
# This is *not* the full version from pcdshub/lcls-twincat-general, but
# rather a part of it for the purposes of the blark test suite.
proj, = store.get_dependency("LCLS_General", "v2.8.1")
return proj
def test_twincat_general(twincat_general_281: PlcProjectMetadata):
summary = twincat_general_281.summary
assert list(summary.function_blocks) == ["FB_LogHandler"]
loghandler = summary.function_blocks["FB_LogHandler"]
check_declarations(
loghandler.declarations,
[
DeclarationCheck(
name="bLogHandlerInput",
base_type="BOOL",
value=None,
),
DeclarationCheck(
name="nNumListeners",
base_type="UINT",
value="6",
),
DeclarationCheck(
name="DisarmCountDefault",
base_type="UINT",
value="5",
),
]
)
# Ensure comments transferred over
assert loghandler.implementation is None
impl = str(loghandler["CircuitBreaker"].implementation)
assert "Logic explanation" in impl
assert "// reset the count for the next" in impl
# Check about reformatting
assert "bTripCon := GVL_Logger.nGlobAccEvents > 0;" in impl
assert list(summary.globals) == ["DefaultGlobals", "Global_Variables_EtherCAT"]
ecat = summary.globals["Global_Variables_EtherCAT"]
check_declarations(
ecat.declarations,
[
DeclarationCheck(
name="iSLAVEADDR_ARR_SIZE",
base_type="UINT",
value="256",
),
DeclarationCheck(
name="ESC_MAX_PORTS",
base_type="UINT",
value="3",
comments=["// Maximum number of ports (4) on ESC"],
),
]
)
defaults = summary.globals["DefaultGlobals"]
check_declarations(
defaults.declarations,
[
DeclarationCheck(
name="stSys",
base_type="ST_System",
comments=["//Included for you"],
),
DeclarationCheck(
name="fTimeStamp",
base_type="LREAL",
),
]
)
system = summary.data_types["ST_System"]
check_declarations(
system.declarations,
[
DeclarationCheck(
name="xSwAlmRst",
base_type="BOOL",
comments=["(* Global Alarm Reset - EPICS Command *)"],
),
DeclarationCheck(
name="xAtVacuum",
base_type="BOOL",
comments=["(* System At Vacuum *)"],
),
DeclarationCheck(
name="xFirstScan",
base_type="BOOL",
comments=[
"(* This boolean is true for the first scan, and is false "
"thereafter, use for initialization of stuff *)",
],
),
DeclarationCheck(
name="xOverrideMode",
base_type="BOOL",
comments=[
"//This bit is set when using the override features of the system"
],
),
DeclarationCheck(
name="xIOState",
base_type="BOOL",
comments=["(* ECat Bus Health *)"],
),
DeclarationCheck(
name="I_EcatMaster1",
base_type="AMSNETID",
location="input",
comments=[
"{attribute 'naming' := 'omit'}",
"(* AMS Net ID used for FB_EcatDiag, among others *)"
],
),
],
)
def test_twincat_general_interface(twincat_general_281: PlcProjectMetadata):
summary = twincat_general_281.summary
assert set(str(itf) for itf in summary.interfaces) == {"I_Base", "I_Interface"}
base = summary.interfaces["I_Base"]
itf = summary.interfaces["I_Interface"]
check_declarations(
base.declarations,
[
DeclarationCheck(
name="iTestBase",
base_type="INT",
value=None,
),
]
)
check_declarations(
itf.declarations,
[
# Inherited
DeclarationCheck(
name="iTestBase",
base_type="INT",
value=None,
),
# Defined
DeclarationCheck(
name="iVar1",
base_type="INT",
value=None,
),
DeclarationCheck(
name="iInputVar1",
base_type="INT",
value=None,
),
DeclarationCheck(
name="iOutputVar1",
base_type="INT",
value=None,
),
DeclarationCheck(
name="iExternVar1",
base_type="INT",
value=None,
),
]
)
assert {prop.name for prop in base.properties} == {"BaseProperty"}
assert {method.name for method in base.methods} == {"BaseMethod"}
for test_if in (base, itf):
base_prop = test_if["BaseProperty"]
assert isinstance(base_prop, PropertySummary)
assert str(base_prop.getter.item.return_type) == "INT"
base_method = test_if["BaseMethod"]
assert isinstance(base_method, MethodSummary)
assert base_method.return_type == "BOOL"
assert {prop.name for prop in itf.properties} == {"BaseProperty", "Property1"}
assert {method.name for method in itf.methods} == {"BaseMethod", "Method1"}
prop1 = itf["Property1"]
assert isinstance(prop1, PropertySummary)
assert str(prop1.getter.item.return_type) == "INT"
method1 = itf["Method1"]
assert isinstance(method1, MethodSummary)
assert method1.return_type == "BOOL"