Skip to content

Commit cc0bdd7

Browse files
committed
Add unit tests
1 parent dd2b3ef commit cc0bdd7

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

test/ir/test_stmt.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from kirin.ir import Block
4+
from kirin.source import SourceInfo
45
from kirin.dialects import py
56

67

@@ -50,3 +51,12 @@ def test_stmt_from_stmt():
5051
y = x.from_stmt(x)
5152

5253
assert y.result.hints["const"] == py.constant.types.Int
54+
55+
56+
def test_stmt_from_stmt_preserves_source_info():
57+
x = py.Constant(1)
58+
x.source = SourceInfo(lineno=1, col_offset=0, end_lineno=None, end_col_offset=None)
59+
60+
y = x.from_stmt(x)
61+
assert y.source == x.source
62+
assert y.source is x.source

test/lowering/test_source_info.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
3+
from kirin.source import SourceInfo
4+
from kirin.prelude import basic_no_opt
5+
6+
7+
def get_line_of(target: str) -> int:
8+
for i, line in enumerate(open(__file__), 1):
9+
if target in line:
10+
return i
11+
12+
13+
@pytest.mark.parametrize("similar", [True, False])
14+
def test_stmt_source_info(similar: bool):
15+
@basic_no_opt
16+
def test(x: int):
17+
y = 2
18+
a = 4**2
19+
return y + 2 + a
20+
21+
if similar:
22+
test = test.similar()
23+
24+
stmts = test.callable_region.blocks[0].stmts
25+
26+
def get_line_from_source_info(source: SourceInfo) -> int:
27+
return source.lineno + source.lineno_begin
28+
29+
for stmt in stmts:
30+
assert stmt.source.file == __file__
31+
32+
assert get_line_from_source_info(stmts.at(0).source) == get_line_of("y = 2")
33+
assert get_line_from_source_info(stmts.at(2).source) == get_line_of("a = 4**2")
34+
assert get_line_from_source_info(stmts.at(4).source) == get_line_of(
35+
"return y + 2 + a"
36+
)

test/passes/test_inline_pass.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from kirin.prelude import basic_no_opt
22
from kirin.passes.inline import InlinePass
3+
from kirin.dialects.py.constant import Constant
34

45

56
@basic_no_opt
@@ -40,3 +41,25 @@ def main_inline_pass2(x: int):
4041
assert a == b
4142

4243
assert len(main_inline_pass2.callable_region.blocks[0].stmts) == 4
44+
45+
46+
def test_inline_preserves_source_info():
47+
def get_line_of(target: str) -> int:
48+
for i, line in enumerate(open(__file__), 1):
49+
if target in line:
50+
return i
51+
52+
@basic_no_opt
53+
def main_inline_pass(x: int):
54+
y = inline_func(x)
55+
return y + 2
56+
57+
inline = InlinePass(main_inline_pass.dialects)
58+
inline(main_inline_pass)
59+
60+
stmt = main_inline_pass.callable_region.blocks[0].stmts.at(0)
61+
line = stmt.source.lineno + stmt.source.lineno_begin
62+
assert stmt.value.data == 1
63+
assert isinstance(stmt, Constant)
64+
65+
assert get_line_of("return x - 1") == line

0 commit comments

Comments
 (0)