Debian Codegraph @

tests/test_generator.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import io
26 import logging
27 import pytest
28
29 from pymeasure.adapters import ProtocolAdapter
30 from pymeasure.instruments import Channel, Instrument
31 from pymeasure.instruments.hcp import TC038, TC038D
32
33 from pymeasure.generator import (write_test, write_parametrized_test,
34 write_parametrized_method_test,
35 parse_stream, Generator, ByteStreamHandler)
36
37
38 class FakeChildChannel(Channel):
39
40 channel_control = Channel.control("G{{ch}}.{ch}", "S{{ch}}{ch} %f",
41 "Control something. (float)")
42
43
44 class FakeChannel(Channel):
45
46 port = Channel.ChannelCreator(FakeChildChannel, id=1)
47
48 channel_control = Channel.control("G{ch}", "S{ch} %f", "Control something. (float)")
49
50 def funny_method(self, value=7):
51 """Some method for testing purposes."""
52 return float(self.ask("M{ch} " + str(value)))
53
54
55 class FakeTestInstrument(Instrument):
56
57 ch_A = Instrument.ChannelCreator(FakeChannel, id="A")
58
59 def __init__(self, adapter, name="Fake"):
60 super().__init__(adapter, name)
61
62 i_control = Instrument.control("G", "S %f", "Control instrument. (float)")
63
64
65 @pytest.fixture
66 def file():
67 s = io.StringIO()
68 yield s
69 s.close()
70
71
72 class Test_write_test:
73 def test_write(self, file):
74 write_test(file, "init", "Super", [(b"sent", b"received")],
75 "pass # Verify the expected communication.")
76 assert file.getvalue() == """
77
78 def test_init():
79 with expected_protocol(
80 Super,
81 [(b'sent', b'received')],
82 ):
83 pass # Verify the expected communication.
84 """
85
86 def test_write_multiple_comm_pairs(self, file):
87 write_test(file, "init", "Super", [(b"sent", b"received"),
88 (b"sent2", b'rec2')], "pass")
89 assert file.getvalue() == """
90
91 def test_init():
92 with expected_protocol(
93 Super,
94 [(b'sent', b'received'),
95 (b'sent2', b'rec2')],
96 ):
97 pass
98 """
99
100 def test_write_init_kwargs(self, file):
101 write_test(file, "init", "Super", [(b"sent", b"received")],
102 "pass # Verify the expected communication.",
103 {'address': 7, 'name': "my name", 'bool': True})
104 assert file.getvalue() == """
105
106 def test_init():
107 with expected_protocol(
108 Super,
109 [(b'sent', b'received')],
110 address=7,
111 name='my name',
112 bool=True,
113 ):
114 pass # Verify the expected communication.
115 """
116
117 def test_write_bytes(self, file):
118 write_test(file, "init", "Super",
119 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
120 "del instr")
121 assert file.getvalue() == r"""
122
123 def test_init():
124 with expected_protocol(
125 Super,
126 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
127 ) as inst:
128 del instr
129 """
130
131 def test_write_replaces_period_with_underscore_in_name(self, file):
132 write_test(file, "ch.init", "Super", [(b"sent", b"received")],
133 "pass # Verify the expected communication.")
134 assert file.getvalue() == """
135
136 def test_ch_init():
137 with expected_protocol(
138 Super,
139 [(b'sent', b'received')],
140 ):
141 pass # Verify the expected communication.
142 """
143
144
145 class Test_write_parametrized_test:
146 def test_write(self, file):
147 write_parametrized_test(file, "init", "Super", [[(b"sent", b"received")]], [None],
148 "assert inst.xyz == value")
149 assert file.getvalue() == """
150
151 @pytest.mark.parametrize("comm_pairs, value", (
152 ([(b'sent', b'received')],
153 None),
154 ))
155 def test_init(comm_pairs, value):
156 with expected_protocol(
157 Super,
158 comm_pairs,
159 ) as inst:
160 assert inst.xyz == value
161 """
162
163 def test_write_multiple_comm_pairs(self, file):
164 write_parametrized_test(file, "init", "Super",
165 [[(b"sent", b"received"), (b"sent2", b'rec2')]], [None],
166 "pass")
167 assert file.getvalue() == """
168
169 @pytest.mark.parametrize("comm_pairs, value", (
170 ([(b'sent', b'received'),
171 (b'sent2', b'rec2')],
172 None),
173 ))
174 def test_init(comm_pairs, value):
175 with expected_protocol(
176 Super,
177 comm_pairs,
178 ):
179 pass
180 """
181
182 def test_write_replaces_period_with_underscore_in_name(self, file):
183 write_parametrized_test(file, "ch.init", "Super", [[(b"sent", b"received")]], [None],
184 "assert inst.xyz == value")
185 assert file.getvalue() == """
186
187 @pytest.mark.parametrize("comm_pairs, value", (
188 ([(b'sent', b'received')],
189 None),
190 ))
191 def test_ch_init(comm_pairs, value):
192 with expected_protocol(
193 Super,
194 comm_pairs,
195 ) as inst:
196 assert inst.xyz == value
197 """
198
199
200 def test_write_parametrized_method(file):
201 """Test also, that a period in the name is changed to underscore."""
202 write_parametrized_method_test(file, "set_monitored.quantity", "TC038",
203 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
204 [(b'\x0201010W0002\x03', b'\x020K\x03')]],
205 [('temperature',), ()],
206 [{}, {'quantity': 'temperature'}],
207 [None, "'xyz'"],
208 "assert inst.set_monitored_quantity(*args, **kwargs) == value"
209 )
210 assert file.getvalue() == r"""
211
212 @pytest.mark.parametrize("comm_pairs, args, kwargs, value", (
213 ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
214 ('temperature',), {}, None),
215 ([(b'\x0201010W0002\x03', b'\x020K\x03')],
216 (), {'quantity': 'temperature'}, 'xyz'),
217 ))
218 def test_set_monitored_quantity(comm_pairs, args, kwargs, value):
219 with expected_protocol(
220 TC038,
221 comm_pairs,
222 ) as inst:
223 assert inst.set_monitored_quantity(*args, **kwargs) == value
224 """
225
226
227 class Test_parse_stream:
228 @pytest.mark.parametrize(
229 "text, comms", (
230 (b"WRITE:abc\n", [(b"abc", None)]),
231 (b"READ:def\n", [(None, b"def")]),
232 (b"READ:a\nREAD:bc\n", [(None, b"abc")]),
233 (b"WRITE:abc\nREAD:def\n", [(b"abc", b"def")]),
234 (b"WRITE:abc\nREAD:d\nREAD:ef\n", [(b"abc", b"def")]),
235 (b"WRITE:abc\nREAD:def\nWRITE:ghi\nREAD:jkl\n",
236 [(b"abc", b"def"), (b"ghi", b"jkl")]),
237 (b"WRITE:abc\nWRITE:def\n", [(b"abc", None), (b"def", None)]),
238 (b"WRITE:\x03ab\x04\nREAD:super\x05\n", # test for non ASCII byte values
239 [(b'\x03ab\x04', b'super\x05')]),
240 (b"WRITE:ho\n 9\nWRITE:\n hey\nREAD:7\n9\n", # test for additional newline chars
241 [(b"ho\n 9", None), (b"\n hey", b"7\n9")]),
242 ))
243 def test_parsing(self, text, comms):
244 with io.BytesIO(text) as buf:
245 assert parse_stream(buf) == comms
246
247
248 class Test_generator:
249 @pytest.fixture
250 def generator(self):
251 generator = Generator()
252 adapter = ProtocolAdapter(
253 [(b"\x0201010WRS01D0002\x03", b"\x020101OK\x03")])
254 TC038.string_test = TC038.control("test?", "test %s", # type: ignore
255 "Control some string.", cast=str, # type: ignore
256 get_process=lambda v: v[7:-1])
257 generator.instantiate(TC038, adapter, "hcp")
258 return generator
259
260 def test_instantiate(self, generator):
261 assert generator._init_comm_pairs == [(b"\x0201010WRS01D0002\x03", b"\x020101OK\x03")]
262
263 def test_write_init_test(self, generator, file):
264 generator.write_init_test(file)
265 assert file.getvalue() == r"""import pytest
266
267 from pymeasure.test import expected_protocol
268 from pymeasure.instruments.hcp import TC038
269
270
271 def test_init():
272 with expected_protocol(
273 TC038,
274 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
275 ):
276 pass # Verify the expected communication.
277 """
278
279 def test_instantiate_with_kwargs(self, file):
280 generator = Generator()
281 adapter = ProtocolAdapter(
282 [(b"\x0201010WRS01D0002\x03", b"\x020101OK\x03")])
283 # add a control with a str for test purposes.
284 TC038.string_test = TC038.control("test?", "test %s", # type: ignore
285 "Control some string.", cast=str, # type: ignore
286 get_process=lambda v: v[7:-1])
287 generator.instantiate(TC038, adapter, "hcp", some_kwarg=5.7, other_kwarg=True,
288 str_kwarg="abc")
289 generator.write_init_test(file)
290 assert file.getvalue() == r"""import pytest
291
292 from pymeasure.test import expected_protocol
293 from pymeasure.instruments.hcp import TC038
294
295
296 def test_init():
297 with expected_protocol(
298 TC038,
299 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
300 some_kwarg=5.7,
301 other_kwarg=True,
302 str_kwarg='abc',
303 ):
304 pass # Verify the expected communication.
305 """
306
307 def test_property(self, generator):
308 generator.inst.adapter.comm_pairs.extend(
309 [(b"\x0201010WRDD0002,01\x03", b"\x020101OK00C8\x03")])
310 assert generator.test_property_getter("temperature") == 20
311 assert generator._getters == {'temperature': (
312 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
313 (b'\x0201010WRDD0002,01\x03', b'\x020101OK00C8\x03')]],
314 [20],
315 )}
316
317 def test_property_with_test_instrument(self, generator):
318 generator.inst.adapter.comm_pairs.extend(
319 [(b"\x0201010WRDD0002,01\x03", b"\x020101OK00C8\x03")])
320 assert generator.test_inst.temperature == 20
321 assert generator._getters == {'temperature': (
322 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
323 (b'\x0201010WRDD0002,01\x03', b'\x020101OK00C8\x03')]],
324 [20],
325 )}
326
327 def test_property_string(self, generator):
328 """Ensure that a returned string is encapsulated by single ticks."""
329 generator.inst.adapter.comm_pairs.extend(
330 [(b"\x0201010test?\x03", b"\x020101OKall fine\x03")])
331 assert generator.test_property_getter("string_test") == "all fine"
332 assert generator._getters == {'string_test': (
333 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
334 (b"\x0201010test?\x03", b"\x020101OKall fine\x03")]],
335 ["'all fine'"],
336 )}
337
338 def test_write_getter_test(self, generator, file):
339 generator.write_getter_test(file, 'temperature', (
340 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
341 (b'\x0201010WRDD0002,01\x03', b'\x020101OK00C8\x03')]],
342 [20],
343 ))
344 assert file.getvalue() == r"""
345
346 def test_temperature_getter():
347 with expected_protocol(
348 TC038,
349 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
350 (b'\x0201010WRDD0002,01\x03', b'\x020101OK00C8\x03')],
351 ) as inst:
352 assert inst.temperature == 20
353 """
354
355 @pytest.mark.parametrize("value, test", (
356 (None, "is None"),
357 (True, "is True"),
358 (False, "is False"),
359 (7, "== 7"),
360 (12.34, "== 12.34"),
361 ))
362 def test_write_getter_test_comparison(self, generator, file, value, test):
363 """Test whether the comparison is changed to 'is', if the value is a boolean or None."""
364 generator.write_getter_test(file, 'temperature', (
365 [[]],
366 [value],
367 ))
368 assert file.getvalue().endswith(test + "\n")
369
370 def test_property_setter(self, generator):
371 generator.inst.adapter.comm_pairs.extend(
372 [(b"\x0201010WWRD0120,01,00C8\x03", b"\x020101OK\x03")])
373 generator.test_property_setter("setpoint", 20)
374 assert generator._setters == {'setpoint': (
375 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
376 (b'\x0201010WWRD0120,01,00C8\x03', b'\x020101OK\x03')]],
377 [20],
378 )}
379
380 def test_property_setter_with_test_instrument(self, generator):
381 generator.inst.adapter.comm_pairs.extend(
382 [(b"\x0201010WWRD0120,01,00C8\x03", b"\x020101OK\x03")])
383 generator.test_inst.setpoint = 20
384 assert generator._setters == {'setpoint': (
385 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
386 (b'\x0201010WWRD0120,01,00C8\x03', b'\x020101OK\x03')]],
387 [20],
388 )}
389
390 def test_property_setter_string(self, generator):
391 """Ensure that a string value is encapsulated by single ticks."""
392 generator.inst.adapter.comm_pairs.extend(
393 [(b"\x0201010test xy\x03", b"\x020101OK\x03")])
394 generator.test_property_setter("string_test", "xy")
395 assert generator._setters == {'string_test': (
396 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
397 (b'\x0201010test xy\x03', None)]],
398 ["'xy'"],
399 )}
400
401 def test_write_setter_test(self, generator, file):
402 generator.write_setter_test(file, 'setpoint', (
403 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
404 (b'\x0201010WWRD0120,01,00C8\x03', b'\x020101OK\x03')]],
405 [20],
406 ))
407 assert file.getvalue() == r"""
408
409 def test_setpoint_setter():
410 with expected_protocol(
411 TC038,
412 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
413 (b'\x0201010WWRD0120,01,00C8\x03', b'\x020101OK\x03')],
414 ) as inst:
415 inst.setpoint = 20
416 """
417
418 @pytest.mark.parametrize("args, kwargs", (
419 (('temperature',), {}),
420 ((), {'quantity': 'temperature'})
421 ))
422 def test_method_arg(self, generator, args, kwargs):
423 generator.inst.adapter.comm_pairs.extend(
424 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')])
425 generator.test_method("set_monitored_quantity", *args, **kwargs)
426 assert generator._calls == {'set_monitored_quantity': (
427 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
428 (b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')]],
429 [args],
430 [kwargs],
431 [None],
432 )}
433
434 @pytest.mark.parametrize("args, kwargs", (
435 (('temperature',), {}),
436 ((), {'quantity': 'temperature'})
437 ))
438 def test_method_arg_with_test_instrument(self, generator, args, kwargs):
439 generator.inst.adapter.comm_pairs.extend(
440 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')])
441 generator.test_inst.set_monitored_quantity(*args, **kwargs)
442 assert generator._calls == {'set_monitored_quantity': (
443 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
444 (b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')]],
445 [args],
446 [kwargs],
447 [None],
448 )}
449
450 @pytest.mark.parametrize("args, kwargs, value, test", (
451 (('temperature',), {}, None, "(*('temperature',), ) is None"),
452 (('temperature',), {}, True, "(*('temperature',), ) is True"),
453 ((), {'quantity': 'temperature'}, 7, "(**{'quantity': 'temperature'}) == 7"),
454 ((), {'quantity': 'temperature'}, False, "(**{'quantity': 'temperature'}) is False"),
455 ))
456 def test_write_method_single(self, generator, file, args, kwargs, value, test):
457 generator.write_method_test(file, 'set_monitored_quantity', (
458 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
459 (b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')]],
460 [args],
461 [kwargs],
462 [value],
463 ))
464 assert file.getvalue() == r"""
465
466 def test_set_monitored_quantity():
467 with expected_protocol(
468 TC038,
469 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
470 (b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
471 ) as inst:
472 assert inst.set_monitored_quantity
473 """[:-1] + test + "\n"
474
475 def test_write_method_parametrized(self, generator, file):
476 generator.write_method_test(file, 'set_monitored_quantity', (
477 [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
478 [(b'\x0201010W0002\x03', b'\x020K\x03')]],
479 [('temperature',), (), ],
480 [{}, {'quantity': 'temperature'}],
481 [None, 7],
482 ))
483 assert file.getvalue() == r"""
484
485 @pytest.mark.parametrize("comm_pairs, args, kwargs, value", (
486 ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
487 ('temperature',), {}, None),
488 ([(b'\x0201010W0002\x03', b'\x020K\x03')],
489 (), {'quantity': 'temperature'}, 7),
490 ))
491 def test_set_monitored_quantity(comm_pairs, args, kwargs, value):
492 with expected_protocol(
493 TC038,
494 comm_pairs,
495 ) as inst:
496 assert inst.set_monitored_quantity(*args, **kwargs) == value
497 """
498
499 def test_bytes_communication(self, file):
500 generator = Generator()
501 a = ProtocolAdapter([(b"\x01\x03\x00\x00\x00\x02\xC4\x0B",
502 b"\x01\x03\x04\x00\x00\x03\xE8\xFA\x8D")])
503 a.log.addHandler(ByteStreamHandler(generator._stream))
504 a.log.setLevel(logging.DEBUG)
505 generator.inst = TC038D(a)
506 generator._class = "TC038D"
507 assert generator.test_property_getter("temperature") == 100
508 assert generator._getters == {'temperature': (
509 [[(b'\x01\x03\x00\x00\x00\x02\xc4\x0b', b'\x01\x03\x04\x00\x00\x03\xe8\xfa\x8d')]],
510 [100.0],
511 )}
512
513 def test_write_bytes_communication(self, generator, file):
514 generator._getters = {'temperature': (
515 [[(b'\x01\x03\x00\x00\x00\x02\xc4\x0b', b'\x01\x03\x04\x00\x00\x03\xe8\xfa\x8d')]],
516 [100.0],
517 )}
518 generator._class = "TC038D"
519 generator.write_property_tests(file)
520 assert file.getvalue() == r"""
521
522 def test_temperature_getter():
523 with expected_protocol(
524 TC038D,
525 [(b'\x01\x03\x00\x00\x00\x02\xc4\x0b', b'\x01\x03\x04\x00\x00\x03\xe8\xfa\x8d')],
526 ) as inst:
527 assert inst.temperature == 100.0
528 """
529
530 @pytest.fixture
531 def generator_multiple(self, generator):
532 """Test an interweaved mix of getters and setters."""
533 generator.inst.adapter.comm_pairs.extend(
534 [(b"\x0201010WWRD0120,01,00C8\x03", b"\x020101OK\x03")])
535 generator.test_property_setter("setpoint", 20)
536
537 generator.inst.adapter.comm_pairs.extend(
538 [(b"\x0201010WRDD0002,01\x03", b"\x020101OK00C8\x03")])
539 assert generator.test_property_getter("temperature") == 20
540
541 generator.inst.adapter.comm_pairs.extend(
542 [(b"\x0201010WWRD0120,01,0258\x03", b"\x020101OK\x03")])
543 generator.test_property_setter("setpoint", 60)
544
545 generator.inst.adapter.comm_pairs.extend(
546 [(b'\x0201010INF6\x03', b'\x020101OKUT150333 V01.R001111222233334444\x03')])
547 assert generator.test_property_getter("information") == "UT150333 V01.R001111222233334444"
548
549 generator.inst.adapter.comm_pairs.extend(
550 [(b"\x0201010WRDD0002,01\x03", b"\x020101OK0258\x03")])
551 assert generator.test_property_getter("temperature") == 60
552
553 generator.inst.adapter.comm_pairs.extend(
554 [(b"\x0201010WRDD0120,01\x03", b"\x020101OK00C8\x03")])
555 assert generator.test_property_getter("setpoint") == 20
556
557 return generator
558
559 def test_write_property_tests(self, generator_multiple, file):
560 """Test that they are sorted alphabetically and collected in a single, parametrized test."""
561 generator_multiple.write_property_tests(file)
562 assert file.getvalue() == r"""
563
564 def test_information_getter():
565 with expected_protocol(
566 TC038,
567 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
568 (b'\x0201010INF6\x03', b'\x020101OKUT150333 V01.R001111222233334444\x03')],
569 ) as inst:
570 assert inst.information == 'UT150333 V01.R001111222233334444'
571
572
573 @pytest.mark.parametrize("comm_pairs, value", (
574 ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
575 (b'\x0201010WWRD0120,01,00C8\x03', b'\x020101OK\x03')],
576 20),
577 ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
578 (b'\x0201010WWRD0120,01,0258\x03', b'\x020101OK\x03')],
579 60),
580 ))
581 def test_setpoint_setter(comm_pairs, value):
582 with expected_protocol(
583 TC038,
584 comm_pairs,
585 ) as inst:
586 inst.setpoint = value
587
588
589 def test_setpoint_getter():
590 with expected_protocol(
591 TC038,
592 [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
593 (b'\x0201010WRDD0120,01\x03', b'\x020101OK00C8\x03')],
594 ) as inst:
595 assert inst.setpoint == 20.0
596
597
598 @pytest.mark.parametrize("comm_pairs, value", (
599 ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
600 (b'\x0201010WRDD0002,01\x03', b'\x020101OK00C8\x03')],
601 20.0),
602 ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
603 (b'\x0201010WRDD0002,01\x03', b'\x020101OK0258\x03')],
604 60.0),
605 ))
606 def test_temperature_getter(comm_pairs, value):
607 with expected_protocol(
608 TC038,
609 comm_pairs,
610 ) as inst:
611 assert inst.temperature == value
612 """
613
614
615 class TestTestInstrument:
616 @pytest.fixture(scope="function")
617 def inst(self):
618 generator = Generator()
619 adapter = ProtocolAdapter()
620 inst = generator.instantiate(FakeTestInstrument, adapter, "fake")
621 return inst
622
623 def test_channel_setter(self, inst):
624 inst.adapter.comm_pairs.extend([("SA 15.000000", None)])
625 inst.ch_A.channel_control = 15
626 assert inst._generator._setters == {'ch_A.channel_control':
627 ([[(b"SA 15.000000", None)]], [15])}
628
629 def test_channel_getter(self, inst):
630 inst.adapter.comm_pairs.extend([("GA", "123.5")])
631 assert inst.ch_A.channel_control == 123.5
632 assert inst._generator._getters == {'ch_A.channel_control':
633 ([[(b"GA", b"123.5")]], [123.5])}
634
635 def test_write_channel_getter_test(self, inst, file):
636 """Importantly, this test checks also, that the test name does not contain a period."""
637 inst.adapter.comm_pairs.extend([("GA", "123.5")])
638 assert inst.ch_A.channel_control == 123.5
639 inst._generator.write_property_tests(file)
640 assert file.getvalue() == r"""
641
642 def test_ch_A_channel_control_getter():
643 with expected_protocol(
644 FakeTestInstrument,
645 [(b'GA', b'123.5')],
646 ) as inst:
647 assert inst.ch_A.channel_control == 123.5
648 """
649
650 def test_channel_method(self, inst):
651 inst.adapter.comm_pairs.extend([("MA 9", "11")])
652 assert inst.ch_A.funny_method(9) == 11
653 assert inst._generator._calls == {'ch_A.funny_method':
654 ([[(b"MA 9", b"11")]], [(9,)], [{}], [11.])}
655
656 def test_child_channel(self, inst):
657 """Whether the child of a channel can be accessed as expected."""
658 inst.adapter.comm_pairs.extend([("GA.1", "7")])
659 assert inst.ch_A.port.channel_control == 7
660 assert inst._generator._getters == {'ch_A.port.channel_control':
661 ([[(b"GA.1", b"7")]], [7])}