Prior to this PR, each type would have its own handwritten test suite. The end result was that not all types were tested for all situations. This PR adds a framework based on a Markdown file, which generates the basic tests for the types defined in json files. These are auto generated and updated by the Makefile before the test suite is run. Also, a number of unsupported type combinations are now supported. Also, we now support negative literals. Also, allocation calculation fixes for nested types. Also, the test helpers can now properly import and export typed variables such as bytes, static arrays and tuples. This may come in handy when it comes to phasm platform wanting to route data. Also, adds better support for i8 type. Also, started on a runtime.py, since there's quite some code now that deals with compile time handling of WebAssembly stuff. Also, minor improvement to the type constrains, namely we better match 'tuple' literals with static array types. Also, reduced spam when printing the type analysis results; constraints that go back on the backlog are now no longer printed one by one. It now also prints the end results of the typing analysis. Also, reorganized the big test_primitives test into type classes. Also, replaced pylint with ruff.
79 lines
1.6 KiB
Python
79 lines
1.6 KiB
Python
import pytest
|
|
|
|
from phasm.type3.entry import Type3Exception
|
|
|
|
from ..helpers import Suite
|
|
|
|
|
|
@pytest.mark.integration_test
|
|
def test_struct_0():
|
|
code_py = """
|
|
class CheckedValue:
|
|
value: i32
|
|
|
|
@exported
|
|
def testEntry() -> i32:
|
|
return helper(CheckedValue(23))
|
|
|
|
def helper(cv: CheckedValue) -> i32:
|
|
return cv.value
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 23 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_struct_1():
|
|
code_py = """
|
|
class Rectangle:
|
|
height: i32
|
|
width: i32
|
|
border: i32
|
|
|
|
@exported
|
|
def testEntry() -> i32:
|
|
return helper(Rectangle(100, 150, 2))
|
|
|
|
def helper(shape: Rectangle) -> i32:
|
|
return shape.height + shape.width + shape.border
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 252 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_struct_2():
|
|
code_py = """
|
|
class Rectangle:
|
|
height: i32
|
|
width: i32
|
|
border: i32
|
|
|
|
@exported
|
|
def testEntry() -> i32:
|
|
return helper(Rectangle(100, 150, 2), Rectangle(200, 90, 3))
|
|
|
|
def helper(shape1: Rectangle, shape2: Rectangle) -> i32:
|
|
return shape1.height + shape1.width + shape1.border + shape2.height + shape2.width + shape2.border
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 545 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ['i32', 'i64', 'f32', 'f64'])
|
|
def test_type_mismatch_struct_member(type_):
|
|
code_py = f"""
|
|
class Struct:
|
|
param: {type_}
|
|
|
|
def testEntry(arg: Struct) -> (i32, i32, ):
|
|
return arg.param
|
|
"""
|
|
|
|
with pytest.raises(Type3Exception, match=type_ + r' must be tuple \(i32\) \(i32\) instead'):
|
|
Suite(code_py).run_code()
|