118 lines
2.9 KiB
Python
118 lines
2.9 KiB
Python
import pytest
|
|
|
|
from phasm.exceptions import StaticError
|
|
from phasm.parser import phasm_parse
|
|
|
|
from ..helpers import Suite
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ('i32', 'f64', ))
|
|
def test_struct_0(type_):
|
|
code_py = f"""
|
|
class CheckedValue:
|
|
value: {type_}
|
|
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return helper(CheckedValue(23))
|
|
|
|
def helper(cv: CheckedValue) -> {type_}:
|
|
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(StaticError, match=f'Static error on line 6: Expected \\(i32, i32, \\), arg.param is actually {type_}'):
|
|
phasm_parse(code_py)
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ['i32', 'i64', 'f32', 'f64'])
|
|
def test_type_mismatch_tuple_member(type_):
|
|
code_py = f"""
|
|
def testEntry(arg: ({type_}, )) -> (i32, i32, ):
|
|
return arg[0]
|
|
"""
|
|
|
|
with pytest.raises(StaticError, match=f'Static error on line 3: Expected \\(i32, i32, \\), arg\\[0\\] is actually {type_}'):
|
|
phasm_parse(code_py)
|
|
|
|
@pytest.mark.integration_test
|
|
def test_tuple_constant_too_few_values():
|
|
code_py = """
|
|
CONSTANT: (u32, u8, u8, ) = (24, 57, )
|
|
"""
|
|
|
|
with pytest.raises(StaticError, match='Static error on line 2: Invalid number of tuple values'):
|
|
phasm_parse(code_py)
|
|
|
|
@pytest.mark.integration_test
|
|
def test_tuple_constant_too_many_values():
|
|
code_py = """
|
|
CONSTANT: (u32, u8, u8, ) = (24, 57, 1, 1, )
|
|
"""
|
|
|
|
with pytest.raises(StaticError, match='Static error on line 2: Invalid number of tuple values'):
|
|
phasm_parse(code_py)
|
|
|
|
@pytest.mark.integration_test
|
|
def test_tuple_constant_type_mismatch():
|
|
code_py = """
|
|
CONSTANT: (u32, u8, u8, ) = (24, 4000, 1, )
|
|
"""
|
|
|
|
with pytest.raises(StaticError, match='Static error on line 2: Integer value out of range; expected 0..255, actual 4000'):
|
|
phasm_parse(code_py)
|