c5d41793f67392555ef0b7fb7ab5b3d3f1e95fd9
1 # The MIT License (MIT)
3 # Copyright (c) 2023 Philippe Proulx <eeppeliteloop@gmail.com>
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 def pytest_collect_file(parent
, file_path
):
34 if file_path
.suffix
!= ext
:
35 # Not a Normand test file: cancel
38 test_name
= "test-{}".format(file_path
.name
.replace(ext
, ""))
40 # Create the file node
41 if file_path
.name
.startswith("fail-"):
42 return _NormandTestFileFail
.from_parent(parent
, path
=file_path
, name
=test_name
)
43 elif file_path
.name
.startswith("pass-"):
44 return _NormandTestFilePass
.from_parent(parent
, path
=file_path
, name
=test_name
)
46 # `.nt` file isn't a test case
50 def _split_nt_file(path
):
53 cur_lines
= normand_lines
57 if line
.rstrip() == "---" and len(output_lines
) == 0:
58 cur_lines
= output_lines
61 cur_lines
.append(line
)
63 return "".join(normand_lines
), "".join(output_lines
).strip()
66 class _NormandTestItem(pytest
.Item
):
68 self
._runtest
(*_split_nt_file(self
.path
))
71 return self
.path
, None, self
.name
74 class _NormandTestItemFail(_NormandTestItem
):
75 def _runtest(self
, normand_text
, output
):
76 with pytest
.raises(normand
.ParseError
) as exc_info
:
77 normand
.parse(normand_text
)
80 expected_msg
= "{}:{} - {}".format(
81 exc
.text_loc
.line_no
, exc
.text_loc
.col_no
, str(exc
)
83 assert output
.strip() == expected_msg
86 class _NormandTestItemPass(_NormandTestItem
):
88 def _data_from_output(output
):
89 hex_bytes
= re
.split(r
"\s+", output
.strip())
90 return bytearray([int(b
, 16) for b
in hex_bytes
])
92 def _runtest(self
, normand_text
, output
):
93 assert normand
.parse(normand_text
).data
== self
._data
_from
_output
(output
)
96 class _NormandTestFile(pytest
.File
):
97 def __init__(self
, name
, **kwargs
):
98 super().__init
__(**kwargs
)
102 # Yield a single item
103 yield self
._item
_cls
.from_parent(self
, name
=self
._name
)
106 class _NormandTestFileFail(_NormandTestFile
):
107 _item_cls
= _NormandTestItemFail
110 class _NormandTestFilePass(_NormandTestFile
):
111 _item_cls
= _NormandTestItemPass
This page took 0.031412 seconds and 3 git commands to generate.