Debian Codegraph @

pymeasure/test.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 from contextlib import contextmanager
26
27 from pymeasure.adapters.protocol import ProtocolAdapter
28
29
30 @contextmanager
31 def expected_protocol(instrument_cls, comm_pairs,
32 connection_attributes=None, connection_methods=None,
33 **kwargs):
34 """Context manager that checks sent/received instrument commands without a
35 device connected.
36
37 Given an instrument class and a list of command-response pairs, this
38 context manager confirms that the code in the context manager block
39 produces the expected messages.
40
41 Terminators are excluded from the protocol definition, as those are
42 typically a detail of the communication method (i.e. Adapter), and not the
43 protocol itself.
44
45 :param pymeasure.Instrument instrument_cls:
46 :class:`~pymeasure.instruments.Instrument` subclass to instantiate.
47 :param list[2-tuples[str]] comm_pairs:
48 List of command-response pairs, i.e. 2-tuples like `('VOLT?', '3.14')`.
49 'None' indicates that a pair member (command or response) does not
50 exist, e.g. `(None, 'RESP1')`. Commands and responses are without
51 termination characters.
52 :param connection_attributes: Dictionary of connection attributes and their values.
53 :param connection_methods: Dictionary of method names of the connection and their return values.
54 :param \\**kwargs:
55 Keyword arguments for the instantiation of the instrument.
56 """
57 protocol = ProtocolAdapter(comm_pairs, connection_attributes=connection_attributes,
58 connection_methods=connection_methods)
59 instr = instrument_cls(protocol, **kwargs)
60 yield instr
61 assert protocol._index == len(comm_pairs), (
62 "Unprocessed protocol definitions remain: "
63 f"{comm_pairs[protocol._index:]}.")
64 assert protocol._write_buffer is None, (
65 f"Non-empty write buffer remains: '{protocol._write_buffer}'.")
66 assert protocol._read_buffer is None, (
67 f"Non-empty read buffer remains: '{protocol._read_buffer}'.")