Exemple #1
0
def test_objcol_select():
    StrType = fundamental_type.get("Str")

    # delegate有り
    col = ObjectCollection()
    col.push("apple", Object(StrType, "リンゴ"))
    col.push("gorilla", Object(StrType, "ゴリラ"))
    col.push("trumpet", Object(StrType, "ラッパ"))
    col.set_delegation(Object(StrType, "コレクション"))

    ColType = fundamental_type.get("ObjectCollection")
    om = select_method("apple", ColType, reciever=col)
    assert om
    assert om.display() == ("ObjectMember", "apple", "")

    # メソッドの移譲
    dm = select_method("startswith", ColType, reciever=col)
    assert dm
    assert dm.display() == ("ObjectMember", "startswith", "")

    # delegate無し
    col.set_delegation(None)

    om = select_method("gorilla", ColType, reciever=col)
    assert om
    assert om.display() == ("ObjectMember", "gorilla", "")
Exemple #2
0
def test_method_loading():
    Str = fundamental_type.get("Str")
    newmethod = Str.select_method("reg-match")
    assert newmethod
    assert newmethod.is_loaded()
    assert newmethod.get_name() == "reg-match"
    assert newmethod.get_param_count() == 1
    assert newmethod.params[0].is_required()
    assert newmethod.get_required_argument_min() == 1
    assert newmethod.get_acceptable_argument_max() == 1
    assert newmethod.is_type_bound() is True

    Type = fundamental_type.get("Type")
    newmethod = Type.select_method("new")
    assert newmethod
    assert newmethod.is_loaded()
    assert newmethod.get_name() == "new"
    assert newmethod.get_param_count() == 0
    assert newmethod.get_required_argument_min() == 0
    assert newmethod.get_acceptable_argument_max() == 0
    assert newmethod.is_type_bound() is True

    t = fundamental_type.define(SomeValue)
    newmethod = t.select_method("perimeter")
    assert newmethod.get_param_count() == 0
    assert newmethod.get_name() == "perimeter"
    assert not newmethod.is_type_bound()
Exemple #3
0
def test_context():
    from machaon.core.object import ObjectCollection
    from machaon.core.invocation import InvocationContext
    from machaon.types.fundamental import fundamental_type

    inputs = ObjectCollection()
    inputs.new("this-year", fundamental_type.get("Int"), 2020)
    inputs.new("this-month", fundamental_type.get("Int"), 8)
    inputs.new("customer-name", fundamental_type.get("Str"), "Yokomizo")
    context = InvocationContext(input_objects=inputs,
                                type_module=fundamental_type)
    return context
def test_scoped_define():
    types = TypeModule()

    assert not hasattr(SpecStrType, "Type_typename")
    spec_str_t = types.define(SpecStrType, typename="Str", scope="spec")
    assert spec_str_t is not None
    assert spec_str_t.is_scope("spec")
    assert spec_str_t.typename == "Str"
    assert spec_str_t.get_value_type() is SpecStrType
    assert spec_str_t.get_describer_qualname(
    ) == "tests.test_object_type.SpecStrType"
    assert getattr(SpecStrType, "Type_typename") == "spec.Str"
    assert not spec_str_t.is_methods_type_bound()
    assert spec_str_t.is_methods_instance_bound()
    assert not spec_str_t.is_using_instance_method()

    str_t = types.define(fundamental_type.get("Str"))
    assert types.get("Str", scope="spec") is spec_str_t
    assert types.get("Str") is str_t

    # enum
    typelist = list(types.enum())
    assert len(typelist) == 2
    assert typelist[0] is spec_str_t  # 追加順で取り出される
    assert typelist[1] is str_t
Exemple #5
0
    def ltest(s, subject, *rhs):
        from machaon.core.object import ObjectCollection, Object
        from machaon.core.invocation import InvocationContext
        from machaon.types.fundamental import fundamental_type

        context = InvocationContext(input_objects=ObjectCollection(),
                                    type_module=fundamental_type)

        engine = MessageEngine(s)
        returned = engine.run(context)
        if returned.is_error():
            spi = TempSpirit()
            returned.pprint(spi)
            spi.printout()
        assert parse_test(engine, context, returned.get_typename(), "Function")

        fundamental_type.define({
            "Typename":
            "Dog",
            "name": ("Returns: Str", lambda x: "lucky"),
            "type": ("Returns: Str", lambda x: "Golden Retriever"),
            "sex": ("Returns: Str", lambda x: "female"),
            "age": ("Returns: Int", lambda x: 3),
        })

        subcontext = context.inherit()
        subj = Object(fundamental_type.get("Dog"), subject)
        fn = returned.value

        lhso = fn.run_function(subj, subcontext)
        assert parse_test(fn, subcontext, lhso, rhs[0])

        # 再入
        lhso = fn.run_function(subj, subcontext)
        assert parse_test(fn, subcontext, lhso, rhs[0])
def test_any():
    anytype = fundamental_type.get("Any")
    assert anytype.is_loaded()
    assert anytype.is_any()
    assert anytype.value_type is None
    inst = Dummy_Rabbit()
    assert anytype.convert_to_string(inst) == anytype.describer.stringify(
        anytype, inst)
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
Exemple #8
0
def test_anyobject_method_select():
    AnyType = fundamental_type.get("Any")

    im = select_method("instance-method", AnyType)
    assert im
    assert im.display() == ("InstanceMethod", "instance_method", "")

    gm = select_method("<", AnyType)
    assert gm
    assert gm.display() == ("TypeMethod", "GenericMethods:less", "")
def test_method():
    regmatch = fundamental_type.get("Str").select_method("reg-match")
    assert regmatch is not None
    assert regmatch.name == "reg-match"
    assert regmatch.get_result().get_typename() == "Bool"

    act = regmatch.get_action()
    assert act(None, "0123.txt", "[0-9]+")
    assert not act(None, "AIUEO.wav", "[0-9]+")

    assert regmatch.get_action_target() == "Str:reg-match"
def test_fundamental():
    Int = fundamental_type.get("int")
    assert Int.typename == "Int"
    assert Int.construct(None, "32") == 32
    assert Int.convert_to_string(32) == "32"
    assert Int.convert_to_string(0xFF) == "255"

    Bool = fundamental_type.get("bool")
    assert Bool.typename == "Bool"
    assert Bool.construct(None, "False") is False

    Float = fundamental_type.get("float")
    assert Float.typename == "Float"
    assert Float.construct(None, "-0.05") == -0.05

    Complex = fundamental_type.get("complex")
    assert Complex.typename == "Complex"
    assert Complex.construct(None, "2+3j") == 2 + 3j

    Str = fundamental_type.get("str")
    assert Str.typename == "Str"
    assert Str.construct(None, "AAA") == "AAA"
Exemple #11
0
def test_straight_select():
    StrType = fundamental_type.get("Str")

    tm = select_method("reg-match", StrType)
    assert tm
    assert tm.display() == ("TypeMethod", "Str:reg-match", "")

    im = select_method("startswith", StrType)
    assert im
    assert im.display() == ("InstanceMethod", "startswith", "")

    gm = select_method("+", StrType)
    assert gm
    assert gm.display() == ("TypeMethod", "GenericMethods:add", "")
def test_typemodule_move():
    new_typeset = TypeModule()
    new_typeset.define(typename="AltString")
    new_typeset.define(Dummy_Rabbit, typename="Second-Rabbit")

    fundamental_type.add_ancestor(new_typeset)

    assert fundamental_type.get("Int").construct(None, "0x35") == 0x35
    assert fundamental_type.get("AltString").typename == "AltString"
    assert fundamental_type.get("Dummy_Rabbit") is not None
    assert fundamental_type.get("Dummy_Rabbit").typename == "Dummy-Rabbit"
    assert fundamental_type.get(
        "Dummy_Rabbit"
    ).describer.describe_count == 2  # Dummy-Rabbit, Second-Rabbitの両方で呼ばれる
    assert fundamental_type.get("Second_Rabbit") is not None
    assert fundamental_type.get("Second_Rabbit").typename == "Second-Rabbit"
Exemple #13
0
def test_deduce():
    assert fundamental_type.deduce(int) is fundamental_type.get("Int")
    assert fundamental_type.deduce(str) is fundamental_type.get("Str")
    assert fundamental_type.deduce(float) is fundamental_type.get("Float")
    assert fundamental_type.deduce(complex) is fundamental_type.get("Complex")
    assert fundamental_type.deduce(bool) is fundamental_type.get("Bool")
    assert fundamental_type.deduce(ValueError) is None

    types = TypeModule()
    spec_str_t = types.define(SpecStrType, scope="spec")
    str_t = types.define(fundamental_type.get("Str"))
    assert types.deduce(str) is str_t
    assert types.deduce(SpecStrType) is spec_str_t
Exemple #14
0
def test_modified_select():
    StrType = fundamental_type.get("Str")

    tm = select_method("!reg-search", StrType)
    assert tm
    assert tm.display() == ("TypeMethod", "Str:reg-search", "negate")

    im = select_method("!startswith", StrType)
    assert im
    assert im.display() == ("InstanceMethod", "startswith", "negate")

    gm = select_method("~in", StrType)
    assert gm
    assert gm.display() == ("TypeMethod", "GenericMethods:is-in",
                            "reverse-args")

    gm = select_method("`=", StrType)
    assert gm
    assert gm.display() == ("TypeMethod", "GenericMethods:identical", "basic")
def test_function():
    fntype = fundamental_type.get("Function")
    fnpower = fntype.construct(None, "@ * @")
    assert fnpower
    from machaon.core.message import MessageEngine
    assert isinstance(fnpower, MessageEngine)
Exemple #16
0
import pytest
from machaon.core.object import Object, ObjectCollection
from machaon.types.fundamental import fundamental_type

Int = fundamental_type.get("Int")
Float = fundamental_type.get("Float")
Complex = fundamental_type.get("Complex")


def run(f):
    f()


def test_desktop():
    desk = ObjectCollection()

    desk.push("obj-1", Object(Int, 100))
    desk.push("obj-2", Object(Int, 7))
    desk.push("obj-3", Object(Complex, 3 + 5j))
    #    desk.push("obj-4", Object(fundamental_type.new("IpAddress"), "128.0.0.1"))

    assert desk.get("obj-1").value == 100


def test_object_new():
    desk = ObjectCollection()

    o = desk.new("obj-new", Int, 128)
    assert o.name == "obj-new"
    assert o.value == 128
    assert o.type == Int
Exemple #17
0
def test_method_alias():
    Str = fundamental_type.get("Str")
    newmethod1 = Str.select_method("location")
    newmethod2 = Str.select_method("loc")
    assert newmethod1 is newmethod2
def test_typemodule_get():
    assert fundamental_type.get("Dummy_Rabbit").typename == "Dummy-Rabbit"
    assert fundamental_type.get(Dummy_Rabbit).typename == "Dummy-Rabbit"
    assert fundamental_type.get(Dummy_Rabbit).describer.describe_count == 1
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"