Skip to content

Commit

Permalink
Test UAST parser global and local variable lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
akeley98 committed Oct 23, 2024
1 parent 23befc4 commit 206f1e2
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions tests/test_uast.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from __future__ import annotations

import pytest

from exo import DRAM
from exo.frontend.pyparser import Parser, get_src_locals, get_ast_from_python
from exo.frontend.pyparser import (
Parser,
get_src_locals,
get_ast_from_python,
ParseError,
)


def to_uast(f):
Expand All @@ -10,7 +17,7 @@ def to_uast(f):
body,
getsrcinfo,
func_globals=f.__globals__,
srclocals=get_src_locals(depth=3),
srclocals=get_src_locals(depth=2),
instr=("TEST", ""),
as_func=True,
)
Expand Down Expand Up @@ -57,3 +64,58 @@ def alloc_nest(
res[i, j] = rloc[j]

assert str(to_uast(alloc_nest)) == golden


global_str = "What is 6 times 9?"
global_num = 42


def test_variable_lookup_positive():
def func(f: f32):
for i in seq(0, 42):
f += 1

Check warning on line 76 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L75-L76

Added lines #L75 - L76 were not covered by tests

reference = to_uast(func)

def func(f: f32):
for i in seq(0, global_num):
f += 1

Check warning on line 82 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L81-L82

Added lines #L81 - L82 were not covered by tests

test_global = to_uast(func)
assert str(test_global) == str(reference)

local_num = 42

def func(f: f32):
for i in seq(0, local_num):
f += 1

Check warning on line 91 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L90-L91

Added lines #L90 - L91 were not covered by tests

test_local = to_uast(func)
assert str(test_local) == str(reference)


def test_variable_lookup_type_error():
def func(f: f32):
for i in seq(0, global_str):
f += 1

Check warning on line 100 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L99-L100

Added lines #L99 - L100 were not covered by tests

with pytest.raises(ParseError, match="type <class 'str'>"):
to_uast(func)

local_str = "xyzzy"

def func(f: f32):
for i in seq(0, local_str):
f += 1

Check warning on line 109 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L108-L109

Added lines #L108 - L109 were not covered by tests

with pytest.raises(ParseError, match="type <class 'str'>"):
to_uast(func)


def test_variable_lookup_name_error():
def func(f: f32):
for i in seq(0, xyzzy):
f += 1

Check warning on line 118 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L117-L118

Added lines #L117 - L118 were not covered by tests

with pytest.raises(ParseError, match="'xyzzy' undefined"):
to_uast(func)

0 comments on commit 206f1e2

Please sign in to comment.