def test_testInterfaces(): friendly: IFriendly friendlier: IFriendlier random_number_generator: IRandomNumberGenerator friendly_random_generator: IFriendlyRandomGenerator add = Add(Number(10), Number(20)) friendly = add assert friendly.hello( ) == "Hello, I am a binary operation. What's your name?" multiply = Multiply(Number(10), Number(30)) friendly = multiply friendlier = multiply random_number_generator = multiply assert friendly.hello( ) == "Hello, I am a binary operation. What's your name?" assert friendlier.goodbye() == "Goodbye from Multiply!" assert random_number_generator.next() == 89 friendly_random_generator = DoubleTrouble() assert friendly_random_generator.hello() == "world" assert friendly_random_generator.next() == 12 poly = Polymorphism() assert (poly.say_hello(friendly) == "oh, Hello, I am a binary operation. What's your name?") assert poly.say_hello(friendly_random_generator) == "oh, world" assert (poly.say_hello( SubclassNativeFriendlyRandom()) == "oh, SubclassNativeFriendlyRandom") assert poly.say_hello(PureNativeFriendlyRandom()) == "oh, I am a native!"
def test_getAndSetNonPrimitiveProperties(): calc = Calculator() calc.add(3_200_000) calc.neg() calc.curr = Multiply(Number(2), calc.curr) assert calc.value == -6_400_000
def test_arrays(): sum_ = Sum() sum_.parts = [Number(5), Number(10), Multiply(Number(2), Number(3))] assert sum_.value == 5 + 10 + (2 * 3) assert sum_.parts[0].value == 5 assert sum_.parts[2].value == 6 assert sum_.to_string() == "(((0 + 5) + 10) + (2 * 3))"
def test_getSetPrimitiveProperties(): number = Number(20) assert number.value == 20 assert number.double_value == 40 assert Negate(Add(Number(20), Number(10))).value == -30 assert Multiply(Add(Number(5), Number(5)), Number(2)).value == 20 assert Power(Number(3), Number(4)).value == 3 ** 4 assert Power(Number(999), Number(1)).value == 999 assert Power(Number(999), Number(0)).value == 1
def test_unionProperties(): calc3 = Calculator() calc3.union_property = Multiply(Number(9), Number(3)) assert isinstance(calc3.union_property, Multiply) assert calc3.read_union_value() == 9 * 3 calc3.union_property = Power(Number(10), Number(3)) assert isinstance(calc3.union_property, Power) assert calc3.read_union_value() == 10 ** 3
def test_dynamicTypes(): types = AllTypes() # boolean types.any_property = False assert not types.any_property # string types.any_property = "String" assert types.any_property == "String" # number types.any_property = 12 assert types.any_property == 12 # date types.any_property = datetime.fromtimestamp(1234 / 1000.0, tz=timezone.utc) assert types.any_property == datetime.fromtimestamp(1234 / 1000.0, tz=timezone.utc) # json (notice that when deserialized, it is deserialized as a map). types.any_property = {"Goo": ["Hello", {"World": 123}]} assert types.any_property.get("Goo")[1].get("World") == 123 # array types.any_property = ["Hello", "World"] assert types.any_property[0] == "Hello" assert types.any_property[1] == "World" # array of any types.any_array_property = ["Hybrid", Number(12), 123, False] assert types.any_array_property[2] == 123 # map map_ = {} map_["MapKey"] = "MapValue" types.any_property = map_ assert types.any_property.get("MapKey") == "MapValue" # map of any map_["Goo"] = 19_289_812 types.any_map_property = map_ types.any_map_property.get("Goo") == 19_289_812 # classes mult = Multiply(Number(10), Number(20)) types.any_property = mult assert types.any_property is mult assert isinstance(types.any_property, Multiply) assert types.any_property.value == 200
def test_unionTypes(): types = AllTypes() # single valued property types.union_property = 1234 assert types.union_property == 1234 types.union_property = "Hello" assert types.union_property == "Hello" types.union_property = Multiply(Number(2), Number(12)) assert types.union_property.value == 24 # map map_ = {} map_["Foo"] = Number(99) types.union_map_property = map_ # TODO: No Assertion? # array types.union_array_property = [123, Number(33)] assert types.union_array_property[1].value == 33