Skip to content

Commit

Permalink
tests: add testing for xdsl_opt_main
Browse files Browse the repository at this point in the history
  • Loading branch information
webmiche committed Jan 9, 2023
1 parent e750bd0 commit 180ed6f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions tests/xdsl_opt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This folder contains the infrastructure to test `xdsl_opt_main` through pytest.
The main driver sits in `test_xdsl_opt.py`, which needs the other files to read
and reprint them.
1 change: 1 addition & 0 deletions tests/xdsl_opt/empty_program.xdsl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
builtin.module() {}
18 changes: 18 additions & 0 deletions tests/xdsl_opt/test_xdsl_opt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from xdsl.xdsl_opt_main import xDSLOptMain
from contextlib import redirect_stdout
from io import StringIO


def test_opt():
filename = 'tests/xdsl_opt/substitute_ops.xdsl'
opt = xDSLOptMain(args=[filename])
assert list(opt.available_frontends.keys()) == ['xdsl', 'mlir']
assert list(opt.available_targets.keys()) == ['xdsl', 'irdl', 'mlir']
assert list(opt.available_passes.keys()) == []

f = StringIO("")
with redirect_stdout(f):
opt.run()

expected = open(filename, 'r').read()
assert f.getvalue().strip() == expected.strip()
6 changes: 4 additions & 2 deletions xdsl/xdsl_opt_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class xDSLOptMain:
pipeline: List[tuple[str, Callable[[ModuleOp], None]]]
""" The pass-pipeline to be applied. """

def __init__(self, description: str = 'xDSL modular optimizer driver'):
def __init__(self,
description: str = 'xDSL modular optimizer driver',
args=None):
self.ctx = MLContext()
self.register_all_dialects()
self.register_all_frontends()
Expand All @@ -63,7 +65,7 @@ def __init__(self, description: str = 'xDSL modular optimizer driver'):
# arg handling
arg_parser = argparse.ArgumentParser(description=description)
self.register_all_arguments(arg_parser)
self.args = arg_parser.parse_args()
self.args = arg_parser.parse_args(args=args)

self.setup_pipeline()

Expand Down

0 comments on commit 180ed6f

Please sign in to comment.