def test_conversion_construct(): cxt = instant_context() r = cxt.new_object(["A1", "B2B", "C3C3"], conversion="Sheet[Str]: (length, =)") sh = r.value assert values(sh.row_values(0)) == [2, "A1"] assert values(sh.row_values(1)) == [3, "B2B"] assert values(sh.row_values(2)) == [4, "C3C3"]
def test_bind1st(): StrType = fundamental_type.get("Str") cxt = instant_context() meth = StrType.select_method("reg-match") oinv = TypeMethodInvocation(StrType, meth) inv = Bind1stInvocation(oinv, "[A-Za-z]+", "str") arg = cxt.new_object("aiueo", type="str") assert inv.prepare_invoke(cxt, arg)._invokeaction() is True
def test_meta_method(): cxt = instant_context() t = fundamental_type.define(SomeValue) v = t.construct(cxt, 3, "2") assert isinstance(v, SomeValue) assert v.x == 3 assert v.y == 6 v = t.convert_to_string(SomeValue(1, 2)) assert v == "(1,2)"
def test_conversion_construct(): cxt = instant_context() # from dict col = instant_return_test(cxt, { "mackerel": "さば", "herring": "にしん", "cod": "たら", }, "ObjectCollection") assert col.test_truth() assert col.value.get("cod").value == "たら" assert col.value.get("herring").value == "にしん"
def test_newobject_conversion(): context = instant_context() t = context.new_object([1,2,3], conversion="Tuple") assert t.get_typename() == "Tuple" assert t.value.count() == 3 t = context.new_object(["A","BB","CCC"], conversion="Sheet[str]: (=, length)") assert t.get_typename() == "Sheet" assert t.value.count() == 3 assert [x.value for x in t.value.column_values(context, "=")] == ["A","BB","CCC"] assert [x.value for x in t.value.column_values(context, "length")] == [1,2,3]
def test_function(): cxt = instant_context() inv = FunctionInvocation(plus2mul) ent = InvocationEntry(inv, inv.get_action(), (2, 3), {}) assert not ent.is_failed() ent.invoke(cxt) assert get_first_result(ent) assert get_first_result(ent).value == plus2mul(2, 3) inv = FunctionInvocation(divide, BasicInvocation.MOD_REVERSE_ARGS) ent = InvocationEntry(inv, inv.get_action(), (4, 2), {}) ent.invoke(cxt) assert get_first_result(ent) assert get_first_result(ent).value == 2 / 4
def test_construct(tmp_path): FILEPATH = __file__ context = instant_context() context.define_type(TextFile) f = instant_return_test(context, FILEPATH, "TextFile").value assert isinstance(f, TextFile) assert isinstance(f.path(), Path) assert f.pathstr == FILEPATH p = tmp_path / "hello.txt" f = instant_return_test(context, p, "TextFile").value f.set_encoding("utf-8") assert f.encoding() == "utf-8" with f.open("w") as fi: fi.write("HELLO\n") fi.write("WORLD") assert f.text() == "HELLO\nWORLD"
def test_newobject_deduction(): context = instant_context() t = context.new_object(1) assert t.get_typename() == "Int" t = context.new_object(True) assert t.get_typename() == "Bool" import datetime t = context.new_object(datetime.datetime.now()) assert t.get_typename() == "Datetime" t = context.new_object([1,2,3]) assert t.get_typename() == "Tuple" t = context.new_object({"a":1, "b":2, "c":3}) assert t.get_typename() == "ObjectCollection"
def test_objectref(): StrType = fundamental_type.get("Str") cxt = instant_context() col = ObjectCollection() col.push("apple", Object(StrType, "リンゴ")) col.push("gorilla", Object(StrType, "ゴリラ")) col.push("trumpet", Object(StrType, "ラッパ")) arg = fundamental_type.get("ObjectCollection").new_object(col) inv = ObjectMemberInvocation("apple") assert inv.prepare_invoke(cxt, arg)._invokeaction().value == "リンゴ" assert isinstance(inv._resolved, ObjectMemberGetterInvocation) assert inv.get_min_arity() == 0 assert inv.get_max_arity() == 0 assert inv.get_parameter_spec(0) is None assert inv.get_result_spec().get_typename() == "Str" inv = ObjectMemberInvocation("trumpet") assert inv.prepare_invoke(cxt, arg)._invokeaction().value == "ラッパ" # generic method (Collectionを参照する) inv = ObjectMemberInvocation("=") assert full_qualified_name( type(inv.prepare_invoke( cxt, arg)._invokeaction())) == "machaon.core.object.Object" assert inv.prepare_invoke( cxt, arg)._invokeaction().get_typename() == "ObjectCollection" # delegation col.set_delegation(Object(StrType, "math.pi")) inv = ObjectMemberInvocation("gorilla") assert inv.prepare_invoke(cxt, arg)._invokeaction().value == "ゴリラ" # unary type method import math inv = ObjectMemberInvocation("pyvalue") assert inv.prepare_invoke(cxt, arg)._invokeaction() == math.pi assert isinstance(inv._resolved, TypeMethodInvocation) assert inv.get_min_arity() == 0 assert inv.get_max_arity() == 0 assert inv.get_parameter_spec(0) is None assert inv.get_result_spec().get_typename() == "Any" # unary instance method inv = ObjectMemberInvocation("islower") assert inv.prepare_invoke(cxt, arg)._invokeaction() is True assert isinstance(inv._resolved, InstanceMethodInvocation) # unary generic method inv = ObjectMemberInvocation("length") assert inv.prepare_invoke(cxt, arg)._invokeaction() == len("math.pi") assert isinstance(inv._resolved, TypeMethodInvocation) # binary type method inv = ObjectMemberInvocation("reg-match") assert inv.prepare_invoke( cxt, arg, StrType.new_object("[A-z]+"))._invokeaction() is True assert isinstance(inv._resolved, TypeMethodInvocation) assert inv.get_min_arity() == 1 assert inv.get_max_arity() == 1 assert inv.get_result_spec().get_typename() == "Bool" # generic method (移譲先のオブジェクトを参照する) inv = ObjectMemberInvocation("=") assert full_qualified_name( type(inv.prepare_invoke( cxt, arg)._invokeaction())) == "machaon.core.object.Object" assert inv.prepare_invoke(cxt, arg)._invokeaction().get_typename() == "Str" # generic method (明示的にCollectionを参照する) inv = ObjectMemberInvocation("=", BasicInvocation.MOD_BASE_RECIEVER) assert full_qualified_name( type(inv.prepare_invoke( cxt, arg)._invokeaction())) == "machaon.core.object.Object" assert inv.prepare_invoke( cxt, arg)._invokeaction().get_typename() == "ObjectCollection"
def instant(cls, expression): # 文字列を受け取り、即席のコンテキストでインスタンスを作る from machaon.core.invocation import instant_context cxt = instant_context() return cls.constructor(None, cxt, expression)
def hotelrooms(name): h = Hotel(name) cxt = instant_context() o = Sheet(h.rooms(), cxt.define_type(Room)) return o, cxt