Debian Codegraph @

tests/display/test_console.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 pytest
26
27 from pymeasure.experiment.parameters import (BooleanParameter,
28 ListParameter,
29 FloatParameter,
30 IntegerParameter,
31 Parameter,
32 VectorParameter,
33 PhysicalParameter)
34 from pymeasure.experiment.procedure import Procedure
35 from pymeasure.display.console import ConsoleArgumentParser
36
37
38 class TestArgParsing:
39 @pytest.mark.parametrize("param", [
40 ('plain_param', 'a'),
41 ('int_param', 100),
42 ('float_param', 0.5),
43 ('bool_param', True),
44 ('vector_param', [1.0, 2, 3]),
45 ('list_param', '2'),
46 ('physical_param', [1.0, 0.1])
47 ])
48 def test_init_from_param(self, param):
49 class TestProcedure(Procedure):
50 plain_param = Parameter('Plain parameter', default=100)
51 int_param = IntegerParameter('Integer parameter', default=100)
52 float_param = FloatParameter('Float parameter', units='s', default=0.2)
53 bool_param = BooleanParameter('Boolean parameter', default=True)
54 list_param = ListParameter('List parameter',
55 default='1', choices=['1', '2', '3'])
56 vector_param = VectorParameter('Vector parameter',
57 default=[1, 5, 3])
58 physical_param = PhysicalParameter('Physical parameter', units='s',
59 default=[1.0, 0.1])
60
61 name, value = param
62 console = ConsoleArgumentParser(TestProcedure)
63 args = vars(console.parse_args(['--' + name, str(value)]))
64 assert args[name] == value
65
66
67 class TestArgHelpString:
68 @pytest.mark.parametrize("klass, kwargs", [
69 (Parameter, {'name': 'Plain parameter', 'default': 'a'}),
70 (IntegerParameter, {'name': 'Integer parameter', 'default': 100}),
71 (FloatParameter, {'name': 'Float parameter', 'default': 0.5}),
72 (BooleanParameter, {'name': 'Boolean parameter', 'default': True}),
73 (VectorParameter, {'name': 'Vector parameter', 'default': [1.0, 2, 3]}),
74 (ListParameter, {'name': 'List parameter',
75 'default': '2', 'choices': ['1', '2', '3']}),
76 (PhysicalParameter, {'name': 'Physical parameter',
77 'default': [1.0, 0.1]})
78 ])
79 def test_init_from_param(self, klass, kwargs):
80 class TestProcedure(Procedure):
81 parameter = klass(**kwargs)
82
83 desc = kwargs['name']
84 default_value = kwargs['default']
85 console = ConsoleArgumentParser(TestProcedure)
86 help_message = [value.strip() for value in console.format_help().split("\n")]
87 assert '--parameter PARAMETER' in help_message
88 for help_line in help_message:
89 if desc in help_line:
90 break
91 assert desc in help_line
92 assert 'default' in help_line
93 assert str(default_value) in help_line