File: test_generator.py

package info (click to toggle)
python-pymeasure 0.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,788 kB
  • sloc: python: 47,201; makefile: 155
file content (661 lines) | stat: -rw-r--r-- 24,481 bytes parent folder | download
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#
# This file is part of the PyMeasure package.
#
# Copyright (c) 2013-2024 PyMeasure Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import io
import logging
import pytest

from pymeasure.adapters import ProtocolAdapter
from pymeasure.instruments import Channel, Instrument
from pymeasure.instruments.hcp import TC038, TC038D

from pymeasure.generator import (write_test, write_parametrized_test,
                                 write_parametrized_method_test,
                                 parse_stream, Generator, ByteStreamHandler)


class FakeChildChannel(Channel):

    channel_control = Channel.control("G{{ch}}.{ch}", "S{{ch}}{ch} %f",
                                      "Control something. (float)")


class FakeChannel(Channel):

    port = Channel.ChannelCreator(FakeChildChannel, id=1)

    channel_control = Channel.control("G{ch}", "S{ch} %f", "Control something. (float)")

    def funny_method(self, value=7):
        """Some method for testing purposes."""
        return float(self.ask("M{ch} " + str(value)))


class FakeTestInstrument(Instrument):

    ch_A = Instrument.ChannelCreator(FakeChannel, id="A")

    def __init__(self, adapter, name="Fake"):
        super().__init__(adapter, name)

    i_control = Instrument.control("G", "S %f", "Control instrument. (float)")


@pytest.fixture
def file():
    s = io.StringIO()
    yield s
    s.close()


class Test_write_test:
    def test_write(self, file):
        write_test(file, "init", "Super", [(b"sent", b"received")],
                   "pass  # Verify the expected communication.")
        assert file.getvalue() == """

def test_init():
    with expected_protocol(
            Super,
            [(b'sent', b'received')],
    ):
        pass  # Verify the expected communication.
"""

    def test_write_multiple_comm_pairs(self, file):
        write_test(file, "init", "Super", [(b"sent", b"received"),
                                           (b"sent2", b'rec2')], "pass")
        assert file.getvalue() == """

def test_init():
    with expected_protocol(
            Super,
            [(b'sent', b'received'),
             (b'sent2', b'rec2')],
    ):
        pass
"""

    def test_write_init_kwargs(self, file):
        write_test(file, "init", "Super", [(b"sent", b"received")],
                   "pass  # Verify the expected communication.",
                   {'address': 7, 'name': "my name", 'bool': True})
        assert file.getvalue() == """

def test_init():
    with expected_protocol(
            Super,
            [(b'sent', b'received')],
            address=7,
            name='my name',
            bool=True,
    ):
        pass  # Verify the expected communication.
"""

    def test_write_bytes(self, file):
        write_test(file, "init", "Super",
                   [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
                   "del instr")
        assert file.getvalue() == r"""

def test_init():
    with expected_protocol(
            Super,
            [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
    ) as inst:
        del instr
"""

    def test_write_replaces_period_with_underscore_in_name(self, file):
        write_test(file, "ch.init", "Super", [(b"sent", b"received")],
                   "pass  # Verify the expected communication.")
        assert file.getvalue() == """

def test_ch_init():
    with expected_protocol(
            Super,
            [(b'sent', b'received')],
    ):
        pass  # Verify the expected communication.
"""


class Test_write_parametrized_test:
    def test_write(self, file):
        write_parametrized_test(file, "init", "Super", [[(b"sent", b"received")]], [None],
                                "assert inst.xyz == value")
        assert file.getvalue() == """

@pytest.mark.parametrize("comm_pairs, value", (
    ([(b'sent', b'received')],
     None),
))
def test_init(comm_pairs, value):
    with expected_protocol(
            Super,
            comm_pairs,
    ) as inst:
        assert inst.xyz == value
"""

    def test_write_multiple_comm_pairs(self, file):
        write_parametrized_test(file, "init", "Super",
                                [[(b"sent", b"received"), (b"sent2", b'rec2')]], [None],
                                "pass")
        assert file.getvalue() == """

@pytest.mark.parametrize("comm_pairs, value", (
    ([(b'sent', b'received'),
      (b'sent2', b'rec2')],
     None),
))
def test_init(comm_pairs, value):
    with expected_protocol(
            Super,
            comm_pairs,
    ):
        pass
"""

    def test_write_replaces_period_with_underscore_in_name(self, file):
        write_parametrized_test(file, "ch.init", "Super", [[(b"sent", b"received")]], [None],
                                "assert inst.xyz == value")
        assert file.getvalue() == """

@pytest.mark.parametrize("comm_pairs, value", (
    ([(b'sent', b'received')],
     None),
))
def test_ch_init(comm_pairs, value):
    with expected_protocol(
            Super,
            comm_pairs,
    ) as inst:
        assert inst.xyz == value
"""


def test_write_parametrized_method(file):
    """Test also, that a period in the name is changed to underscore."""
    write_parametrized_method_test(file, "set_monitored.quantity", "TC038",
                                   [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
                                    [(b'\x0201010W0002\x03', b'\x020K\x03')]],
                                   [('temperature',), ()],
                                   [{}, {'quantity': 'temperature'}],
                                   [None, "'xyz'"],
                                   "assert inst.set_monitored_quantity(*args, **kwargs) == value"
                                   )
    assert file.getvalue() == r"""

@pytest.mark.parametrize("comm_pairs, args, kwargs, value", (
    ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
     ('temperature',), {}, None),
    ([(b'\x0201010W0002\x03', b'\x020K\x03')],
     (), {'quantity': 'temperature'}, 'xyz'),
))
def test_set_monitored_quantity(comm_pairs, args, kwargs, value):
    with expected_protocol(
            TC038,
            comm_pairs,
    ) as inst:
        assert inst.set_monitored_quantity(*args, **kwargs) == value
"""


class Test_parse_stream:
    @pytest.mark.parametrize(
        "text, comms", (
            (b"WRITE:abc\n", [(b"abc", None)]),
            (b"READ:def\n", [(None, b"def")]),
            (b"READ:a\nREAD:bc\n", [(None, b"abc")]),
            (b"WRITE:abc\nREAD:def\n", [(b"abc", b"def")]),
            (b"WRITE:abc\nREAD:d\nREAD:ef\n", [(b"abc", b"def")]),
            (b"WRITE:abc\nREAD:def\nWRITE:ghi\nREAD:jkl\n",
             [(b"abc", b"def"), (b"ghi", b"jkl")]),
            (b"WRITE:abc\nWRITE:def\n", [(b"abc", None), (b"def", None)]),
            (b"WRITE:\x03ab\x04\nREAD:super\x05\n",  # test for non ASCII byte values
             [(b'\x03ab\x04', b'super\x05')]),
            (b"WRITE:ho\n 9\nWRITE:\n hey\nREAD:7\n9\n",  # test for additional newline chars
             [(b"ho\n 9", None), (b"\n hey", b"7\n9")]),
        ))
    def test_parsing(self, text, comms):
        with io.BytesIO(text) as buf:
            assert parse_stream(buf) == comms


class Test_generator:
    @pytest.fixture
    def generator(self):
        generator = Generator()
        adapter = ProtocolAdapter(
            [(b"\x0201010WRS01D0002\x03", b"\x020101OK\x03")])
        TC038.string_test = TC038.control("test?", "test %s",  # type: ignore
                                          "Control some string.", cast=str,  # type: ignore
                                          get_process=lambda v: v[7:-1])
        generator.instantiate(TC038, adapter, "hcp")
        return generator

    def test_instantiate(self, generator):
        assert generator._init_comm_pairs == [(b"\x0201010WRS01D0002\x03", b"\x020101OK\x03")]

    def test_write_init_test(self, generator, file):
        generator.write_init_test(file)
        assert file.getvalue() == r"""import pytest

from pymeasure.test import expected_protocol
from pymeasure.instruments.hcp import TC038


def test_init():
    with expected_protocol(
            TC038,
            [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
    ):
        pass  # Verify the expected communication.
"""

    def test_instantiate_with_kwargs(self, file):
        generator = Generator()
        adapter = ProtocolAdapter(
            [(b"\x0201010WRS01D0002\x03", b"\x020101OK\x03")])
        # add a control with a str for test purposes.
        TC038.string_test = TC038.control("test?", "test %s",  # type: ignore
                                          "Control some string.",  cast=str,  # type: ignore
                                          get_process=lambda v: v[7:-1])
        generator.instantiate(TC038, adapter, "hcp", some_kwarg=5.7, other_kwarg=True,
                              str_kwarg="abc")
        generator.write_init_test(file)
        assert file.getvalue() == r"""import pytest

from pymeasure.test import expected_protocol
from pymeasure.instruments.hcp import TC038


def test_init():
    with expected_protocol(
            TC038,
            [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
            some_kwarg=5.7,
            other_kwarg=True,
            str_kwarg='abc',
    ):
        pass  # Verify the expected communication.
"""

    def test_property(self, generator):
        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010WRDD0002,01\x03", b"\x020101OK00C8\x03")])
        assert generator.test_property_getter("temperature") == 20
        assert generator._getters == {'temperature': (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WRDD0002,01\x03', b'\x020101OK00C8\x03')]],
            [20],
        )}

    def test_property_with_test_instrument(self, generator):
        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010WRDD0002,01\x03", b"\x020101OK00C8\x03")])
        assert generator.test_inst.temperature == 20
        assert generator._getters == {'temperature': (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WRDD0002,01\x03', b'\x020101OK00C8\x03')]],
            [20],
        )}

    def test_property_string(self, generator):
        """Ensure that a returned string is encapsulated by single ticks."""
        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010test?\x03", b"\x020101OKall fine\x03")])
        assert generator.test_property_getter("string_test") == "all fine"
        assert generator._getters == {'string_test': (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b"\x0201010test?\x03", b"\x020101OKall fine\x03")]],
            ["'all fine'"],
        )}

    def test_write_getter_test(self, generator, file):
        generator.write_getter_test(file, 'temperature', (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WRDD0002,01\x03', b'\x020101OK00C8\x03')]],
            [20],
        ))
        assert file.getvalue() == r"""

def test_temperature_getter():
    with expected_protocol(
            TC038,
            [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WRDD0002,01\x03', b'\x020101OK00C8\x03')],
    ) as inst:
        assert inst.temperature == 20
"""

    @pytest.mark.parametrize("value, test", (
        (None, "is None"),
        (True, "is True"),
        (False, "is False"),
        (7, "== 7"),
        (12.34, "== 12.34"),
    ))
    def test_write_getter_test_comparison(self, generator, file, value, test):
        """Test whether the comparison is changed to 'is', if the value is a boolean or None."""
        generator.write_getter_test(file, 'temperature', (
            [[]],
            [value],
        ))
        assert file.getvalue().endswith(test + "\n")

    def test_property_setter(self, generator):
        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010WWRD0120,01,00C8\x03", b"\x020101OK\x03")])
        generator.test_property_setter("setpoint", 20)
        assert generator._setters == {'setpoint': (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WWRD0120,01,00C8\x03', b'\x020101OK\x03')]],
            [20],
        )}

    def test_property_setter_with_test_instrument(self, generator):
        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010WWRD0120,01,00C8\x03", b"\x020101OK\x03")])
        generator.test_inst.setpoint = 20
        assert generator._setters == {'setpoint': (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WWRD0120,01,00C8\x03', b'\x020101OK\x03')]],
            [20],
        )}

    def test_property_setter_string(self, generator):
        """Ensure that a string value is encapsulated by single ticks."""
        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010test xy\x03", b"\x020101OK\x03")])
        generator.test_property_setter("string_test", "xy")
        assert generator._setters == {'string_test': (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010test xy\x03', None)]],
            ["'xy'"],
        )}

    def test_write_setter_test(self, generator, file):
        generator.write_setter_test(file, 'setpoint', (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WWRD0120,01,00C8\x03', b'\x020101OK\x03')]],
            [20],
        ))
        assert file.getvalue() == r"""

def test_setpoint_setter():
    with expected_protocol(
            TC038,
            [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WWRD0120,01,00C8\x03', b'\x020101OK\x03')],
    ) as inst:
        inst.setpoint = 20
"""

    @pytest.mark.parametrize("args, kwargs", (
        (('temperature',), {}),
        ((), {'quantity': 'temperature'})
    ))
    def test_method_arg(self, generator, args, kwargs):
        generator.inst.adapter.comm_pairs.extend(
            [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')])
        generator.test_method("set_monitored_quantity", *args, **kwargs)
        assert generator._calls == {'set_monitored_quantity': (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')]],
            [args],
            [kwargs],
            [None],
        )}

    @pytest.mark.parametrize("args, kwargs", (
        (('temperature',), {}),
        ((), {'quantity': 'temperature'})
    ))
    def test_method_arg_with_test_instrument(self, generator, args, kwargs):
        generator.inst.adapter.comm_pairs.extend(
            [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')])
        generator.test_inst.set_monitored_quantity(*args, **kwargs)
        assert generator._calls == {'set_monitored_quantity': (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')]],
            [args],
            [kwargs],
            [None],
        )}

    @pytest.mark.parametrize("args, kwargs, value, test", (
        (('temperature',), {}, None, "(*('temperature',), ) is None"),
        (('temperature',), {}, True, "(*('temperature',), ) is True"),
        ((), {'quantity': 'temperature'}, 7, "(**{'quantity': 'temperature'}) == 7"),
        ((), {'quantity': 'temperature'}, False, "(**{'quantity': 'temperature'}) is False"),
    ))
    def test_write_method_single(self, generator, file, args, kwargs, value, test):
        generator.write_method_test(file, 'set_monitored_quantity', (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')]],
            [args],
            [kwargs],
            [value],
        ))
        assert file.getvalue() == r"""

def test_set_monitored_quantity():
    with expected_protocol(
            TC038,
            [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
    ) as inst:
        assert inst.set_monitored_quantity
"""[:-1] + test + "\n"

    def test_write_method_parametrized(self, generator, file):
        generator.write_method_test(file, 'set_monitored_quantity', (
            [[(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
             [(b'\x0201010W0002\x03', b'\x020K\x03')]],
            [('temperature',), (), ],
            [{}, {'quantity': 'temperature'}],
            [None, 7],
        ))
        assert file.getvalue() == r"""

@pytest.mark.parametrize("comm_pairs, args, kwargs, value", (
    ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03')],
     ('temperature',), {}, None),
    ([(b'\x0201010W0002\x03', b'\x020K\x03')],
     (), {'quantity': 'temperature'}, 7),
))
def test_set_monitored_quantity(comm_pairs, args, kwargs, value):
    with expected_protocol(
            TC038,
            comm_pairs,
    ) as inst:
        assert inst.set_monitored_quantity(*args, **kwargs) == value
"""

    def test_bytes_communication(self, file):
        generator = Generator()
        a = ProtocolAdapter([(b"\x01\x03\x00\x00\x00\x02\xC4\x0B",
                              b"\x01\x03\x04\x00\x00\x03\xE8\xFA\x8D")])
        a.log.addHandler(ByteStreamHandler(generator._stream))
        a.log.setLevel(logging.DEBUG)
        generator.inst = TC038D(a)
        generator._class = "TC038D"
        assert generator.test_property_getter("temperature") == 100
        assert generator._getters == {'temperature': (
            [[(b'\x01\x03\x00\x00\x00\x02\xc4\x0b', b'\x01\x03\x04\x00\x00\x03\xe8\xfa\x8d')]],
            [100.0],
        )}

    def test_write_bytes_communication(self, generator, file):
        generator._getters = {'temperature': (
            [[(b'\x01\x03\x00\x00\x00\x02\xc4\x0b', b'\x01\x03\x04\x00\x00\x03\xe8\xfa\x8d')]],
            [100.0],
        )}
        generator._class = "TC038D"
        generator.write_property_tests(file)
        assert file.getvalue() == r"""

def test_temperature_getter():
    with expected_protocol(
            TC038D,
            [(b'\x01\x03\x00\x00\x00\x02\xc4\x0b', b'\x01\x03\x04\x00\x00\x03\xe8\xfa\x8d')],
    ) as inst:
        assert inst.temperature == 100.0
"""

    @pytest.fixture
    def generator_multiple(self, generator):
        """Test an interweaved mix of getters and setters."""
        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010WWRD0120,01,00C8\x03", b"\x020101OK\x03")])
        generator.test_property_setter("setpoint", 20)

        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010WRDD0002,01\x03", b"\x020101OK00C8\x03")])
        assert generator.test_property_getter("temperature") == 20

        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010WWRD0120,01,0258\x03", b"\x020101OK\x03")])
        generator.test_property_setter("setpoint", 60)

        generator.inst.adapter.comm_pairs.extend(
            [(b'\x0201010INF6\x03', b'\x020101OKUT150333 V01.R001111222233334444\x03')])
        assert generator.test_property_getter("information") == "UT150333 V01.R001111222233334444"

        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010WRDD0002,01\x03", b"\x020101OK0258\x03")])
        assert generator.test_property_getter("temperature") == 60

        generator.inst.adapter.comm_pairs.extend(
            [(b"\x0201010WRDD0120,01\x03", b"\x020101OK00C8\x03")])
        assert generator.test_property_getter("setpoint") == 20

        return generator

    def test_write_property_tests(self, generator_multiple, file):
        """Test that they are sorted alphabetically and collected in a single, parametrized test."""
        generator_multiple.write_property_tests(file)
        assert file.getvalue() == r"""

def test_information_getter():
    with expected_protocol(
            TC038,
            [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010INF6\x03', b'\x020101OKUT150333 V01.R001111222233334444\x03')],
    ) as inst:
        assert inst.information == 'UT150333 V01.R001111222233334444'


@pytest.mark.parametrize("comm_pairs, value", (
    ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
      (b'\x0201010WWRD0120,01,00C8\x03', b'\x020101OK\x03')],
     20),
    ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
      (b'\x0201010WWRD0120,01,0258\x03', b'\x020101OK\x03')],
     60),
))
def test_setpoint_setter(comm_pairs, value):
    with expected_protocol(
            TC038,
            comm_pairs,
    ) as inst:
        inst.setpoint = value


def test_setpoint_getter():
    with expected_protocol(
            TC038,
            [(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
             (b'\x0201010WRDD0120,01\x03', b'\x020101OK00C8\x03')],
    ) as inst:
        assert inst.setpoint == 20.0


@pytest.mark.parametrize("comm_pairs, value", (
    ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
      (b'\x0201010WRDD0002,01\x03', b'\x020101OK00C8\x03')],
     20.0),
    ([(b'\x0201010WRS01D0002\x03', b'\x020101OK\x03'),
      (b'\x0201010WRDD0002,01\x03', b'\x020101OK0258\x03')],
     60.0),
))
def test_temperature_getter(comm_pairs, value):
    with expected_protocol(
            TC038,
            comm_pairs,
    ) as inst:
        assert inst.temperature == value
"""


class TestTestInstrument:
    @pytest.fixture(scope="function")
    def inst(self):
        generator = Generator()
        adapter = ProtocolAdapter()
        inst = generator.instantiate(FakeTestInstrument, adapter, "fake")
        return inst

    def test_channel_setter(self, inst):
        inst.adapter.comm_pairs.extend([("SA 15.000000", None)])
        inst.ch_A.channel_control = 15
        assert inst._generator._setters == {'ch_A.channel_control':
                                            ([[(b"SA 15.000000", None)]], [15])}

    def test_channel_getter(self, inst):
        inst.adapter.comm_pairs.extend([("GA", "123.5")])
        assert inst.ch_A.channel_control == 123.5
        assert inst._generator._getters == {'ch_A.channel_control':
                                            ([[(b"GA", b"123.5")]], [123.5])}

    def test_write_channel_getter_test(self, inst, file):
        """Importantly, this test checks also, that the test name does not contain a period."""
        inst.adapter.comm_pairs.extend([("GA", "123.5")])
        assert inst.ch_A.channel_control == 123.5
        inst._generator.write_property_tests(file)
        assert file.getvalue() == r"""

def test_ch_A_channel_control_getter():
    with expected_protocol(
            FakeTestInstrument,
            [(b'GA', b'123.5')],
    ) as inst:
        assert inst.ch_A.channel_control == 123.5
"""

    def test_channel_method(self, inst):
        inst.adapter.comm_pairs.extend([("MA 9", "11")])
        assert inst.ch_A.funny_method(9) == 11
        assert inst._generator._calls == {'ch_A.funny_method':
                                          ([[(b"MA 9", b"11")]], [(9,)], [{}], [11.])}

    def test_child_channel(self, inst):
        """Whether the child of a channel can be accessed as expected."""
        inst.adapter.comm_pairs.extend([("GA.1", "7")])
        assert inst.ch_A.port.channel_control == 7
        assert inst._generator._getters == {'ch_A.port.channel_control':
                                            ([[(b"GA.1", b"7")]], [7])}