Johan B.W. de Vries a2e1dfd799 Reworks type class instantiation
Before, a type class was a property of a type.
But that doesn't make any sense for multi parameter
type classes.

Had to make a hacky way for type classes with
type constructors.
2025-04-27 17:45:13 +02:00

184 lines
3.6 KiB
Python

import pytest
from phasm.type3.entry import Type3Exception
from ..helpers import Suite
INT_TYPES = ['u8', 'u32', 'u64', 'i8', 'i32', 'i64']
FLOAT_TYPES = ['f32', 'f64']
TYPE_MAP = {
'u8': int,
'u32': int,
'u64': int,
'i8': int,
'i32': int,
'i64': int,
'f32': float,
'f64': float,
}
@pytest.mark.integration_test
def test_equals_not_implemented():
code_py = """
class Foo:
val: i32
@exported
def testEntry(x: Foo, y: Foo) -> Foo:
return x == y
"""
with pytest.raises(Type3Exception, match='Missing type class instantation: Eq Foo'):
Suite(code_py).run_code()
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', INT_TYPES)
def test_equals_int_same(type_):
code_py = f"""
CONSTANT0: {type_} = 10
CONSTANT1: {type_} = 10
@exported
def testEntry() -> bool:
return CONSTANT0 == CONSTANT1
"""
result = Suite(code_py).run_code()
assert True is result.returned_value
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', INT_TYPES)
def test_equals_int_different(type_):
code_py = f"""
CONSTANT0: {type_} = 10
CONSTANT1: {type_} = 3
@exported
def testEntry() -> bool:
return CONSTANT0 == CONSTANT1
"""
result = Suite(code_py).run_code()
assert False is result.returned_value
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', FLOAT_TYPES)
def test_equals_float_same(type_):
code_py = f"""
CONSTANT0: {type_} = 10.125
CONSTANT1: {type_} = 10.125
@exported
def testEntry() -> bool:
return CONSTANT0 == CONSTANT1
"""
result = Suite(code_py).run_code()
assert True is result.returned_value
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', FLOAT_TYPES)
def test_equals_float_different(type_):
code_py = f"""
CONSTANT0: {type_} = 10.32
CONSTANT1: {type_} = 10.33
@exported
def testEntry() -> bool:
return CONSTANT0 == CONSTANT1
"""
result = Suite(code_py).run_code()
assert False is result.returned_value
@pytest.mark.integration_test
def test_not_equals_not_implemented():
code_py = """
class Foo:
val: i32
@exported
def testEntry(x: Foo, y: Foo) -> Foo:
return x != y
"""
with pytest.raises(Type3Exception, match='Missing type class instantation: Eq Foo'):
Suite(code_py).run_code()
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', INT_TYPES)
def test_not_equals_int_same(type_):
code_py = f"""
CONSTANT0: {type_} = 10
CONSTANT1: {type_} = 10
@exported
def testEntry() -> bool:
return CONSTANT0 != CONSTANT1
"""
result = Suite(code_py).run_code()
assert False is result.returned_value
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', INT_TYPES)
def test_not_equals_int_different(type_):
code_py = f"""
CONSTANT0: {type_} = 10
CONSTANT1: {type_} = 3
@exported
def testEntry() -> bool:
return CONSTANT0 != CONSTANT1
"""
result = Suite(code_py).run_code()
assert True is result.returned_value
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', FLOAT_TYPES)
def test_not_equals_float_same(type_):
code_py = f"""
CONSTANT0: {type_} = 10.125
CONSTANT1: {type_} = 10.125
@exported
def testEntry() -> bool:
return CONSTANT0 != CONSTANT1
"""
result = Suite(code_py).run_code()
assert False is result.returned_value
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', FLOAT_TYPES)
def test_not_equals_float_different(type_):
code_py = f"""
CONSTANT0: {type_} = 10.32
CONSTANT1: {type_} = 10.33
@exported
def testEntry() -> bool:
return CONSTANT0 != CONSTANT1
"""
result = Suite(code_py).run_code()
assert True is result.returned_value