|
1 | 1 | import os |
2 | 2 | import pathlib |
3 | 3 |
|
| 4 | +import pytest |
| 5 | +from lark.exceptions import UnexpectedToken |
| 6 | + |
4 | 7 | from bloqade.qasm2.parse import loads, spprint, loadfile |
5 | | -from bloqade.qasm2.parse.parser import qasm2_parser as lark_parser |
6 | 8 |
|
7 | 9 |
|
8 | | -def roundtrip(file): |
9 | | - ast1 = loadfile(os.path.join(os.path.dirname(__file__), "programs", file)) |
| 10 | +def roundtrip(file, dirname): |
| 11 | + ast1 = loadfile(os.path.join(os.path.dirname(__file__), dirname, file)) |
10 | 12 | ast2 = loads(spprint(ast1)) |
11 | 13 | return ast1 == ast2 |
12 | 14 |
|
13 | 15 |
|
14 | 16 | def test_roundtrip(): |
15 | | - path = pathlib.Path(__file__).parent / "programs" |
| 17 | + dirname = "programs" |
| 18 | + path = pathlib.Path(__file__).parent / dirname |
16 | 19 | for file in path.glob("*.qasm"): |
17 | | - assert roundtrip(file.name), f"Failed roundtrip for {file}" |
18 | | - |
| 20 | + assert roundtrip(file.name, dirname), f"Failed roundtrip for {file}" |
19 | 21 |
|
20 | | -if __name__ == "__main__": |
21 | | - filepath = os.path.join(os.path.dirname(__file__), "programs", "global.qasm") |
22 | | - print(filepath) |
23 | | - with open(filepath) as f: |
24 | | - qasm_str = f.read() |
25 | 22 |
|
26 | | - parse_tree = lark_parser.parse(qasm_str) # raw parsing seems to be fine |
27 | | - print(parse_tree.pretty()) |
28 | | - |
29 | | - ast = loads(qasm_str) |
30 | | - print(spprint(ast)) |
| 23 | +def test_invalids(): |
| 24 | + dirname = "invalid_programs" |
| 25 | + path = pathlib.Path(__file__).parent / dirname |
| 26 | + for file in path.glob("*.qasm"): |
| 27 | + with pytest.raises(UnexpectedToken): |
| 28 | + roundtrip(file.name, dirname) |
0 commit comments