1 # SPDX-FileCopyrightText: 2023 Philippe Proulx <eeppeliteloop@gmail.com>
2 # SPDX-License-Identifier: MIT
11 def pytest_collect_file(parent
, file_path
):
14 if file_path
.suffix
!= ext
:
15 # Not a Normand test file: cancel
18 test_name
= "test-{}".format(file_path
.name
.replace(ext
, ""))
20 # Create the file node
21 if file_path
.name
.startswith("fail-"):
22 return _NormandTestFileFail
.from_parent(parent
, path
=file_path
, name
=test_name
)
23 elif file_path
.name
.startswith("pass-"):
24 return _NormandTestFilePass
.from_parent(parent
, path
=file_path
, name
=test_name
)
26 # `.nt` file isn't a test case
30 def _split_nt_file(path
):
33 cur_lines
= normand_lines
37 if line
.rstrip() == "---" and len(output_lines
) == 0:
38 cur_lines
= output_lines
41 cur_lines
.append(line
)
43 return "".join(normand_lines
), "".join(output_lines
).strip()
46 class _NormandTestItem(pytest
.Item
):
48 self
._runtest
(*_split_nt_file(self
.path
))
51 return self
.path
, None, self
.name
54 class _NormandTestItemFail(_NormandTestItem
):
55 def _runtest(self
, normand_text
, output
):
56 with pytest
.raises(normand
.ParseError
) as exc_info
:
57 normand
.parse(normand_text
)
62 for msg
in reversed(exc
.messages
):
63 expected_msg
+= "{}:{} - {}\n".format(
64 msg
.text_location
.line_no
, msg
.text_location
.col_no
, msg
.text
67 assert output
.strip() == expected_msg
.strip()
70 class _NormandTestItemPass(_NormandTestItem
):
72 def _data_from_output(output
):
73 hex_bytes
= re
.split(r
"\s+", output
.strip())
74 return bytearray([int(b
, 16) for b
in hex_bytes
])
76 def _runtest(self
, normand_text
, output
):
77 assert normand
.parse(normand_text
).data
== self
._data
_from
_output
(output
)
80 class _NormandTestFile(pytest
.File
):
81 def __init__(self
, name
, **kwargs
):
82 super().__init
__(**kwargs
)
87 yield self
._item
_cls
.from_parent(self
, name
=self
._name
)
90 class _NormandTestFileFail(_NormandTestFile
):
91 _item_cls
= _NormandTestItemFail
94 class _NormandTestFilePass(_NormandTestFile
):
95 _item_cls
= _NormandTestItemPass
This page took 0.031849 seconds and 4 git commands to generate.