-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_parser_errors.py
More file actions
202 lines (151 loc) · 5.55 KB
/
Copy pathtest_parser_errors.py
File metadata and controls
202 lines (151 loc) · 5.55 KB
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
import io
import pytest
from cp2k_input_tools import DEFAULT_CP2K_INPUT_XML
from cp2k_input_tools.parser import CP2KInputParser
from cp2k_input_tools.parser_errors import (
InvalidNameError,
InvalidParameterError,
InvalidSectionError,
PreprocessorError,
SectionMismatchError,
)
from cp2k_input_tools.tokenizer import UnterminatedStringError
from . import TEST_DIR
def test_error_invalid_number_of_parameters():
cp2k_parser = CP2KInputParser(DEFAULT_CP2K_INPUT_XML)
with open(TEST_DIR.joinpath("inputs/error_nvar.inp"), "r") as fhandle:
with pytest.raises(InvalidParameterError) as excinfo:
cp2k_parser.parse(fhandle)
assert "invalid values for keyword: A" in excinfo.value.args[0]
assert excinfo.value.args[1].linenr == 41
assert isinstance(excinfo.value.__cause__, InvalidParameterError)
assert "keyword expects exactly 3 values, 2 were given" in excinfo.value.__cause__.args[0]
def test_unterminated_string():
cp2k_parser = CP2KInputParser(DEFAULT_CP2K_INPUT_XML)
with open(TEST_DIR.joinpath("inputs/unterminated_string.inp"), "r") as fhandle:
with pytest.raises(UnterminatedStringError) as excinfo:
cp2k_parser.parse(fhandle)
assert excinfo.value.args[1].linenr == 14
def test_undefined_preprocessor_var():
cp2k_parser = CP2KInputParser(DEFAULT_CP2K_INPUT_XML)
with open(TEST_DIR.joinpath("inputs/preprocesser_undefined_var.inp"), "r") as fhandle:
with pytest.raises(PreprocessorError) as excinfo:
cp2k_parser.parse(fhandle)
assert "undefined variable 'HP'" in excinfo.value.args[0]
assert excinfo.value.args[1].linenr == 30
def test_multiple_defined_non_repeating_section():
cp2k_parser = CP2KInputParser(DEFAULT_CP2K_INPUT_XML)
fhandle = io.StringIO("""
&GLOBAL
&END GLOBAL
&GLOBAL
&END GLOBAL
""")
with pytest.raises(InvalidNameError) as excinfo:
cp2k_parser.parse(fhandle)
assert "the section 'GLOBAL' can not be defined multiple times" in excinfo.value.args[0]
def test_missing_section_end():
cp2k_parser = CP2KInputParser(DEFAULT_CP2K_INPUT_XML)
fhandle = io.StringIO("""
&GLOBAL
! &END GLOBAL
&FORCE_EVAL
&END FORCE_EVAL
""")
with pytest.raises(InvalidSectionError) as excinfo:
cp2k_parser.parse(fhandle)
assert "invalid section" in excinfo.value.args[0]
fhandle = io.StringIO("""
&GLOBAL
&END GLOBAL
&FORCE_EVAL
""")
with pytest.raises(SectionMismatchError) as excinfo:
cp2k_parser.parse(fhandle)
assert "not closed" in excinfo.value.args[0]
def test_section_end_mismatch():
cp2k_parser = CP2KInputParser(DEFAULT_CP2K_INPUT_XML)
fhandle = io.StringIO("""
&GLOBAL
&END GLOBI
&FORCE_EVAL
&END FORCE_EVAL
""")
with pytest.raises(SectionMismatchError) as excinfo:
cp2k_parser.parse(fhandle)
assert "could not match open section" in excinfo.value.args[0]
def test_section_parameter_error():
cp2k_parser = CP2KInputParser(DEFAULT_CP2K_INPUT_XML)
fhandle = io.StringIO("""
&GLOBAL invalidparam
&END GLOBAL
""")
with pytest.raises(InvalidParameterError) as excinfo:
cp2k_parser.parse(fhandle)
assert "section parameters given for non-parametrized section" in excinfo.value.args[0]
def test_invalid_keyword():
cp2k_parser = CP2KInputParser(DEFAULT_CP2K_INPUT_XML)
fhandle = io.StringIO("""
&FORCE_EVAL
&SUBSYS
BASIS_SET TZVPd-MOLOPT-SR-GTH
&END SUBSYS
&END FORCE_EVAL
""")
with pytest.raises(InvalidNameError) as excinfo:
cp2k_parser.parse(fhandle)
assert "invalid keyword" in excinfo.value.args[0]
def test_internal_cp2k_unit():
"""
Ensure that values for keywords with a default internal_cp2k unit are accepted unless there is an explicit unit.
The first one is a reproducer of https://github.com/cp2k/cp2k-input-tools/issues/54
"""
cp2k_parser = CP2KInputParser(DEFAULT_CP2K_INPUT_XML)
fhandle_no_extra_unit = io.StringIO("""
&FORCE_EVAL
METHOD FIST
&MM
&FORCEFIELD
IGNORE_MISSING_CRITICAL_PARAMS T
&BOND
ATOMS O H
KIND HARMONIC
K 4.7265512325474252E-01
R0 1.9124028464802707E+00
&END BOND
&BEND
ATOMS H O H
KIND HARMONIC
K 1.2095435014802065E-01
THETA0 1.9764108449583786E+00
&END BEND
&END FORCEFIELD
&END MM
&END FORCE_EVAL
""")
cp2k_parser.parse(fhandle_no_extra_unit)
fhandle_extra_unit = io.StringIO("""
&FORCE_EVAL
METHOD FIST
&MM
&FORCEFIELD
IGNORE_MISSING_CRITICAL_PARAMS T
&BOND
ATOMS O H
KIND HARMONIC
K [hartree] 4.7265512325474252E-01
R0 1.9124028464802707E+00
&END BOND
&BEND
ATOMS H O H
KIND HARMONIC
K [hartree] 1.2095435014802065E-01
THETA0 1.9764108449583786E+00
&END BEND
&END FORCEFIELD
&END MM
&END FORCE_EVAL
""")
with pytest.raises(InvalidParameterError) as excinfo:
cp2k_parser.parse(fhandle_extra_unit)
assert "invalid values" in excinfo.value.args[0]