Debian Codegraph @

tests/experiment/test_sequencer.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 io import StringIO
28 from pymeasure.experiment.sequencer import SequenceHandler, SequenceEvaluationError
29
30
31 def non_empty_lines(text):
32 lines = text.split("\n")
33 linect = 0
34 for line in lines:
35 if line.strip() != "":
36 linect += 1
37 return linect
38
39
40 seq_file_text_1 = """
41 - "P1", "[1,2]"
42 -- "P2", "[3, 4, 5]"
43 """
44
45 seq_file_text_2 = """
46 - "P1", "[1,2]"
47 -- "P2", "[3, 4, 5]"
48 - "P1", "[4,5]"
49 """
50
51 seq_file_text_3 = """
52 - "P1", "[1,2]"
53 -- "P2", "[3, 4, 5]"
54 --- "P3", "[4, 5, 6]"
55 """
56
57
58 @pytest.mark.parametrize("seq_file_text",
59 [
60 (seq_file_text_1,
61 (0, 1),
62 (1, 0),
63 ("P1", "P2")),
64 (seq_file_text_2,
65 (0, 1, 0),
66 (1, 0, 0),
67 ("P1", "P2", "P1")),
68 (seq_file_text_3,
69 (0, 1, 2),
70 (1, 1, 0),
71 ("P1", "P2", "P3")),
72 ])
73 def test_sequencer(seq_file_text):
74
75 file_text, levels, children, params = seq_file_text
76 fd = StringIO(file_text)
77
78 s = SequenceHandler(file_obj=fd)
79 assert len(s._sequences) == non_empty_lines(file_text)
80
81 for index, lev in enumerate(levels):
82 assert s._sequences[index].level == lev
83 for index, c in enumerate(children):
84 assert len(s.children(s._sequences[index])) == c
85
86 for index, c in enumerate(params):
87 assert s._sequences[index].parameter == c
88
89
90 seq_file_text_err1 = """
91 - "P1", "[1,2"
92 -- "P2", "[3, 4, 5]"
93 """
94
95 seq_file_text_err2 = """
96 - "P1", "[1,2]"
97 --- "P2", "[3, 4, 5]"
98 - "P1", "[4,5]"
99 """
100
101 seq_file_text_err3 = """
102 - "P1", "[1,2]"
103 -- "P2", "[3, 4, 5]"
104 --- "P3", ""
105 """
106
107
108 @pytest.mark.parametrize("seq_file_text_err",
109 [(seq_file_text_err1,
110 SequenceEvaluationError,
111 "SyntaxError, likely unbalanced brackets"),
112 (seq_file_text_err2,
113 SequenceEvaluationError,
114 'Invalid file format: level missing ?'),
115 (seq_file_text_err3,
116 SequenceEvaluationError,
117 "No sequence entered")])
118 def test_sequencer_errors(seq_file_text_err):
119
120 file_text, exception, exc_text = seq_file_text_err
121
122 fd = StringIO(file_text)
123
124 with pytest.raises(exception, match=exc_text):
125 seq = SequenceHandler(file_obj=fd)
126 seq.parameters_sequence()