Ejemplo n.º 1
0
def test_compound__init__keyword():
    compound = Compound(id="id",
                        name="name",
                        full_name="full_name",
                        language="lang",
                        kind="kind",
                        members=[Compound(name="member_name")],
                        params=[Parameter(name="parameter")],
                        exceptions=[ThrowsClause(description="exception")],
                        returns=ReturnValue(description="returns"),
                        include="include",
                        namespace="namespace",
                        prot="prot",
                        definition="definition",
                        args="args",
                        initializer=" = 2",
                        brief="brief",
                        description="description",
                        static=True,
                        const=True,
                        deleted=True,
                        default=True,
                        constexpr=True)

    assert compound.id == "id"
    assert compound.name == "name"
    assert compound.full_name == "full_name"
    assert compound.language == "lang"
    assert compound.kind == "kind"

    assert len(compound.members) == 1
    assert compound.members[0].name == "member_name"
    assert len(compound.params) == 1
    assert compound.params[0].name == "parameter"
    assert len(compound.exceptions) == 1
    assert compound.exceptions[0].description == "exception"
    assert compound.returns is not None
    assert compound.returns.description == "returns"

    assert compound.include == "include"
    assert compound.namespace == "namespace"

    assert compound.prot == "prot"
    assert compound.definition == "definition"
    assert compound.args == "args"
    assert compound.initializer == " = 2"

    assert compound.brief == "brief"
    assert compound.description == "description"

    assert compound.static is True
    assert compound.const is True
    assert compound.deleted is True
    assert compound.default is True
    assert compound.constexpr is True
Ejemplo n.º 2
0
def cpp_class():
    compound = Compound("cpp")
    compound.name = "MyClass"

    def generate_member(kind: str, prot: str) -> Member:
        member = Member("cpp")
        member.kind = kind
        member.name = prot.capitalize() + kind.capitalize()
        member.prot = prot
        return member

    def generate_member_function(prot: str,
                                 name: str,
                                 has_return_value: bool = True,
                                 is_static: bool = False) -> Member:
        member = Member("cpp")
        member.kind = "function"
        member.name = name
        member.prot = prot
        if has_return_value:
            member.returns = ReturnValue()
        if is_static:
            member.static = True
        return member

    # fill class with typical members
    for visibility in ("public", "protected", "private"):
        for member_type in ("variable", "enum", "class", "typedef", "struct", "trash"):
            compound.members.append(generate_member(kind=member_type, prot=visibility))

        # adds constructors
        compound.members.append(
            generate_member_function(prot=visibility, name="MyClass", has_return_value=False))
        # add some operator
        compound.members.append(generate_member_function(prot=visibility, name="operator++"))
        # add some method
        compound.members.append(
            generate_member_function(prot=visibility, name=visibility.capitalize() + "Method"))
        # add static method
        compound.members.append(
            generate_member_function(prot=visibility,
                                     name=visibility.capitalize() + "StaticMethod",
                                     is_static=True))

    # insert nested type
    nested_class = Compound("cpp")
    nested_class.name = "NestedClass"
    inner_class_reference = InnerTypeReference(language="cpp")
    inner_class_reference.name = nested_class.name
    inner_class_reference.referred_object = nested_class
    compound.inner_classes.append(inner_class_reference)

    return compound
Ejemplo n.º 3
0
def objc_class():
    compound = Compound("objc")
    compound.name = "MyClass"

    def generate_member(kind: str, prot: str) -> Member:
        member = Member("objc")
        member.kind = kind
        member.name = prot.capitalize() + kind.capitalize()
        member.prot = prot
        return member

    def generate_member_function(prot: str,
                                 name: str,
                                 has_return_value: bool = True,
                                 is_static: bool = False) -> Member:
        member = Member("objc")
        member.kind = "function"
        member.name = name
        member.prot = prot
        if has_return_value:
            member.returns = ReturnValue()
        if is_static:
            member.static = True
        return member

    def generate_property(prot: str) -> Member:
        property = generate_member_function(prot=prot,
                                            name=prot.capitalize() +
                                            "Property")
        property.kind = "property"
        return property

    # fill class with typical members
    for visibility in ("public", "protected", "private"):
        for member_type in ("enum", "class", "protocol", "trash"):
            compound.members.append(
                generate_member(kind=member_type, prot=visibility))

        # add property
        compound.members.append(generate_property(prot=visibility))
        # add some method
        compound.members.append(
            generate_member_function(prot=visibility,
                                     name=visibility.capitalize() + "Method"))
        # add static method
        compound.members.append(
            generate_member_function(prot=visibility,
                                     name=visibility.capitalize() +
                                     "StaticMethod",
                                     is_static=True))

    # forbidden method
    member = Member("objc")
    member.kind = "function"
    member.name = "NS_UNAVAILABLE"
    member.prot = "public"
    member.returns = ReturnValue()
    compound.members.append(member)

    return compound
def test_objc_method_signature__multiple_params_linked_return(helper):
    method = Compound("objc")
    method.name = "setValue:withUnit:andALongerParam:"
    method.returns = ReturnValue()
    method.returns.type = TypeRef("objc", name="Value")
    method.returns.type.id = "objc-value"

    param1 = Parameter()
    param1.name = "arg1"
    param1.type = TypeRef("objc", "Type1")

    param2 = Parameter()
    param2.name = "arg2"
    param2.type = TypeRef("objc", "Type2")
    param2.type.id = "objc-type2"

    param3 = Parameter()
    param3.name = "arg3"
    param3.type = TypeRef("objc", "Type3")

    method.params = [param1, param2, param3]

    assert (helper.method_signature(method) == """\
- (xref:objc-value[++Value++])setValue:(Type1)arg1
         withUnit:(xref:objc-type2[++Type2++])arg2
  andALongerParam:(Type3)arg3""")
def test_objc_method_signature__class_method(helper):
    method = Compound("objc")
    method.name = "start"
    method.static = True
    method.returns = ReturnValue()
    method.returns.type = TypeRef("objc", name="void")
    assert helper.method_signature(method) == "+ (void)start"
Ejemplo n.º 6
0
def test_compound__init__positional():
    compound = Compound("lang")
    assert compound.id is None
    assert compound.name == ""
    assert compound.full_name == ""
    assert compound.language == "lang"
    assert compound.kind == ""

    assert compound.members == []
    assert compound.params == []
    assert compound.exceptions == []
    assert compound.returns is None

    assert compound.include is None
    assert compound.namespace is None

    assert compound.prot == ""
    assert compound.definition == ""
    assert compound.args == ""
    assert compound.initializer == ""

    assert compound.brief == ""
    assert compound.description == ""

    assert compound.static is False
    assert compound.const is False
    assert compound.deleted is False
    assert compound.default is False
    assert compound.constexpr is False
Ejemplo n.º 7
0
def test_compound__eq__none():
    compound = Compound()
    assert not compound == None  # noqa: E711
    assert not None == compound  # noqa: E711

    assert compound != None  # noqa: E711
    assert None != compound  # noqa: E711
Ejemplo n.º 8
0
def make_compound(*,
                  id=None,
                  name="",
                  full_name=None,
                  language="",
                  kind="class",
                  include="include.file",
                  namespace="asciidoxy",
                  prot="public",
                  definition="definition",
                  args="args",
                  brief="Brief description",
                  description="Long description",
                  **kwargs):
    if id is None:
        id = f"{language}-{name.lower()}"
    if full_name is None:
        full_name = f"{namespace}.{name}"
    return Compound(id=id,
                    name=name,
                    language=language,
                    full_name=full_name,
                    kind=kind,
                    include=include,
                    namespace=namespace,
                    prot=prot,
                    definition=definition,
                    args=args,
                    brief=brief,
                    description=description,
                    **kwargs)
Ejemplo n.º 9
0
 def generate_inner_class(name: str) -> InnerTypeReference:
     nested_class = Compound("python")
     nested_class.name = name
     inner_class_reference = InnerTypeReference(language="python")
     inner_class_reference.name = nested_class.name
     inner_class_reference.referred_object = nested_class
     return inner_class_reference
def test_method_signature__no_params_simple_return__throws(helper):
    method = Compound("swift")
    method.name = "start"
    method.returns = ReturnValue()
    method.returns.type = TypeRef("swift", name="Int")
    method.exceptions = [ThrowsClause("swift")]
    assert helper.method_signature(method) == "func start() throws -> Int"
def test_method_signature__no_params(helper):
    method = Compound("python")
    method.name = "ShortMethod"

    method.returns = ReturnValue()
    method.returns.type = TypeRef("python", "None")

    assert helper.method_signature(method) == "def ShortMethod() -> None"
def test_method_signature__no_params_link_return(helper):
    method = Compound("swift")
    method.name = "retrieveValue"
    method.returns = ReturnValue()
    method.returns.type = TypeRef("swift", name="Value")
    method.returns.type.id = "swift-value"
    assert (helper.method_signature(method) ==
            "func retrieveValue() -> xref:swift-value[++Value++]")
Ejemplo n.º 13
0
def test_method_signature(helper):
    method = Compound("cpp")
    method.name = "ShortMethod"

    method.returns = ReturnValue()
    method.returns.type = TypeRef("cpp", "void")

    assert helper.method_signature(method) == "void ShortMethod()"
Ejemplo n.º 14
0
def test_method_signature__no_params_link_return(helper):
    method = Compound("kotlin")
    method.name = "retrieveValue"
    method.returns = ReturnValue()
    method.returns.type = TypeRef("kotlin", name="Value")
    method.returns.type.id = "kotlin-value"
    assert helper.method_signature(
        method) == "fun retrieveValue(): xref:kotlin-value[++Value++]"
Ejemplo n.º 15
0
def test_typeref__resolve():
    ref = TypeRef(name="name")
    assert ref.id is None
    assert ref.kind is None

    ref.resolve(Compound(id="id", kind="kind"))
    assert ref.id == "id"
    assert ref.kind == "kind"
def test_objc_method_signature__no_params_link_return(helper):
    method = Compound("objc")
    method.name = "retrieveValue"
    method.returns = ReturnValue()
    method.returns.type = TypeRef("objc", name="Value")
    method.returns.type.id = "objc-value"
    assert helper.method_signature(
        method) == "- (xref:objc-value[++Value++])retrieveValue"
Ejemplo n.º 17
0
def test_minimal_constructed_repr():
    assert repr(TypeRef("lang"))
    assert repr(Parameter())
    assert repr(ReturnValue())
    assert repr(ThrowsClause("lang"))
    assert repr(EnumValue("lang"))
    assert repr(Member("lang"))
    assert repr(Compound("lang"))
    assert repr(InnerTypeReference("lang"))
def test_method_signature__no_params_link_return__throws(helper):
    method = Compound("swift")
    method.name = "retrieveValue"
    method.returns = ReturnValue()
    method.returns.type = TypeRef("swift", name="Value")
    method.returns.type.id = "swift-value"
    method.exceptions = [ThrowsClause("swift")]
    assert (helper.method_signature(method) ==
            "func retrieveValue() throws -> xref:swift-value[++Value++]")
def test_closure_definition__no_params__void_return(helper):
    closure = Compound("swift")
    closure.name = "SuccessClosure"
    closure.returns = ReturnValue()
    closure.returns.type = TypeRef("swift", name="Void")
    closure.returns.type.args = []

    assert helper.closure_definition(
        closure) == "typealias SuccessClosure = () -> Void"
Ejemplo n.º 20
0
def test_method_signature__no_params(empty_generating_api):
    method = Compound("lang")
    method.name = "ShortMethod"

    method.returns = ReturnValue()
    method.returns.type = TypeRef("lang", "void")

    helper = TemplateHelper(empty_generating_api)
    assert helper.method_signature(method) == "void ShortMethod()"
def test_closure_definition__no_params__return_type(helper):
    closure = Compound("swift")
    closure.name = "SuccessClosure"
    closure.returns = ReturnValue()
    closure.returns.type = TypeRef("swift", name="Data")
    closure.returns.type.id = "swift-data"
    closure.returns.type.args = []

    assert (helper.closure_definition(closure) ==
            "typealias SuccessClosure = () -> xref:swift-data[++Data++]")
Ejemplo n.º 22
0
def python_class():
    compound = Compound("python")
    compound.name = "MyClass"

    def generate_member_function(name: str,
                                 has_return_value: bool = True,
                                 is_static: bool = False) -> Member:
        member = Member("python")
        member.kind = "function"
        member.name = name
        member.prot = "public"
        if has_return_value:
            member.returns = ReturnValue()
        if is_static:
            member.static = True
        return member

    def generate_member_variable(name: str) -> Member:
        member_variable = Member("python")
        member_variable.kind = "variable"
        member_variable.name = name
        member_variable.returns = ReturnValue()
        member_variable.returns.type = TypeRef("python")
        return member_variable

    def generate_inner_class(name: str) -> InnerTypeReference:
        nested_class = Compound("python")
        nested_class.name = name
        inner_class_reference = InnerTypeReference(language="python")
        inner_class_reference.name = nested_class.name
        inner_class_reference.referred_object = nested_class
        return inner_class_reference

    compound.members = [
        generate_member_function("__init__", has_return_value=False, is_static=False),
        generate_member_function("public_static_method", is_static=True),
        generate_member_function("_private_static_method", is_static=True),
        generate_member_function("__mangled_private_static_method", is_static=True),
        generate_member_function("public_method"),
        generate_member_function("_private_method"),
        generate_member_function("__mangled_private_method"),
        generate_member_function("__add__"),
        generate_member_variable("public_variable"),
        generate_member_variable("_private_variable"),
        generate_member_variable("__mangled_private_variable"),
    ]

    compound.inner_classes = [
        generate_inner_class("NestedClass"),
        generate_inner_class("_PrivateNestedClass"),
        generate_inner_class("__MangledPrivateNestedClass"),
    ]

    return compound
def test_method_signature__one_param(helper):
    method = Compound("swift")
    method.name = "setValue"
    method.returns = ReturnValue()
    method.returns.type = TypeRef("swift", name="Value")

    param1 = Parameter()
    param1.name = "arg1"
    param1.type = TypeRef("objc", "Type1")
    method.params = [param1]

    assert helper.method_signature(
        method) == "func setValue(arg1: Type1) -> Value"
def test_closure_definition__multiple_params_type_only__void_return(helper):
    closure = Compound("swift")
    closure.name = "SuccessClosure"
    closure.returns = ReturnValue()
    closure.returns.type = TypeRef("swift", name="Void")
    closure.returns.type.args = [Parameter(), Parameter()]
    closure.returns.type.args[0].type = TypeRef("swift", "int")
    closure.returns.type.args[1].type = TypeRef("swift", "Data")
    closure.returns.type.args[1].type.id = "swift-data"

    assert (
        helper.closure_definition(closure) ==
        "typealias SuccessClosure = (int, xref:swift-data[++Data++]) -> Void")
Ejemplo n.º 25
0
def test_method_signature__single_param(empty_generating_api):
    method = Compound("lang")
    method.name = "ShortMethod"

    method.returns = ReturnValue()
    method.returns.type = TypeRef("lang", "void")

    method.params = [Parameter()]
    method.params[0].name = "value"
    method.params[0].type = TypeRef("lang", "int")

    helper = TemplateHelper(empty_generating_api)
    assert helper.method_signature(method) == "void ShortMethod(int value)"
def test_params__no_type():
    param1 = Parameter()
    param1.type = None
    param1.name = "arg1"

    param2 = Parameter()
    param2.type = None
    param2.name = "arg2"

    member = Compound("lang")
    member.params = [param1, param2]

    assert list(params(member)) == [param1, param2]
def test_method_signature__single_param(helper):
    method = Compound("python")
    method.name = "ShortMethod"

    method.returns = ReturnValue()
    method.returns.type = TypeRef("python", "int")

    method.params = [Parameter()]
    method.params[0].name = "value"
    method.params[0].type = TypeRef("python", "int")

    assert helper.method_signature(
        method) == "def ShortMethod(value: int) -> int"
def test_objc_method_signature__one_param(helper):
    method = Compound("objc")
    method.name = "setValue:"
    method.returns = ReturnValue()
    method.returns.type = TypeRef("objc", name="Value")
    method.returns.type.id = "objc-value"

    param1 = Parameter()
    param1.name = "arg1"
    param1.type = TypeRef("objc", "Type1")
    method.params = [param1]

    assert helper.method_signature(
        method) == "- (xref:objc-value[++Value++])setValue:(Type1)arg1"
def test_method_signature__single_param__too_wide(helper):
    method = Compound("python")
    method.name = "ShortMethod"

    method.returns = ReturnValue()
    method.returns.type = TypeRef("python", "str")

    method.params = [Parameter()]
    method.params[0].name = "value"
    method.params[0].type = TypeRef("python", "int")

    assert (helper.method_signature(method, max_width=20) == """\
def ShortMethod(
    value: int) -> str""")
def test_method_signature__ignore_param_type_xref_length(helper):
    method = Compound("python")
    method.name = "ShortMethod"

    method.returns = ReturnValue()
    method.returns.type = TypeRef("python", "None")

    method.params = [Parameter()]
    method.params[0].name = "value"
    method.params[0].type = TypeRef("python", "int")
    method.params[0].type.id = "ab" * 80

    assert (helper.method_signature(method) ==
            f"def ShortMethod(value: xref:{'ab' * 80}[++int++]) -> None")