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_typeref__init__keywords(): ref = TypeRef(language="lang", name="name", id="id", namespace="namespace", kind="kind", prefix="prefix", suffix="suffix", nested=[TypeRef(name="nested")], args=[Parameter(name="parameter")], returns=TypeRef(name="returns"), prot="public") assert ref.id == "id" assert ref.name == "name" assert ref.language == "lang" assert ref.namespace == "namespace" assert ref.kind == "kind" assert ref.prefix == "prefix" assert ref.suffix == "suffix" assert ref.prot == "public" assert len(ref.nested) == 1 assert ref.nested[0].name == "nested" assert len(ref.args) == 1 assert ref.args[0].name == "parameter" assert ref.returns is not None assert ref.returns.name == "returns"
def test_parameter__eq__full(): first = Parameter(type=TypeRef(name="type"), name="name", description="description", default_value="default_value", prefix="prefix") second = Parameter(type=TypeRef(name="type"), name="name", description="description", default_value="default_value", prefix="prefix") assert first == second assert second == first for attr_name in ("name", "description", "default_value", "prefix"): setattr(second, attr_name, "other") assert first != second assert second != first setattr(second, attr_name, getattr(first, attr_name)) second.type.name = "other" assert first != second assert second != first second.type.name = first.type.name
def test_objc_method_signature__multiple_params_linked_return(context): method = Member("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 (objc_method_signature( method, context) == "- (xref:objc-value[Value])setValue:(Type1)arg1\n" " withUnit:(xref:objc-type2[Type2])arg2\n" " andALongerParam:(Type3)arg3")
def test_link_from_ref__name_prefix_suffix_no_id(context_mock): ref = TypeRef("lang") ref.name = "MyType" ref.prefix = "const " ref.suffix = " &" assert link_from_ref(ref, context_mock) == "const MyType &" context_mock.assert_not_called()
def test_print_ref__link__name_prefix_suffix_no_id(api_mock): ref = TypeRef("lang") ref.name = "MyType" ref.prefix = "const " ref.suffix = " &" helper = TemplateHelper(api_mock) assert helper.print_ref(ref) == "const MyType &" api_mock.link_to_element.assert_not_called()
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_parameter(helper): ref = TypeRef("lang") ref.name = "MyType" ref.id = "lang-tomtom_1_MyType" param = Parameter() param.type = ref param.name = "arg" param.default_value = "12" assert (helper.parameter(param, default_value=True) == "arg: xref:lang-tomtom_1_MyType[++MyType++] = 12")
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_print_ref__no_link__empty_closure(api_mock): return_type = TypeRef("lang") return_type.name = "MyType" return_type.prefix = "const " return_type.suffix = " &" return_type.id = "lang-tomtom_1_MyType" ref = TypeRef("lang") ref.args = [] ref.returns = return_type helper = TemplateHelper(api_mock) assert helper.print_ref(ref, link=False) == "const MyType &()"
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_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")
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_argument_list__empty_type(empty_context): type1 = TypeRef("lang") type2 = TypeRef("lang") param1 = Parameter() param1.type = type1 param1.name = "arg1" param2 = Parameter() param2.type = type2 param2.name = "arg2" assert (argument_list([param1, param2], empty_context) == "(arg1, arg2)")
def test_print_ref__link__complex_closure(api_mock): ref = TypeRef("lang") ref.name = "std::function" ref.nested = [TypeRef("lang")] ref.nested[0].returns = TypeRef("lang") ref.nested[0].returns.name = "void" ref.nested[0].args = [Parameter()] ref.nested[0].args[0].type = TypeRef("lang") ref.nested[0].args[0].type.name = "std::shared_ptr" ref.nested[0].args[0].type.prefix = "const " ref.nested[0].args[0].type.suffix = "&" ref.nested[0].args[0].type.nested = [TypeRef("lang")] ref.nested[0].args[0].type.nested[0].name = "detail::SuccessDescriptor" ref.nested[0].args[0].type.nested[0].id = "lang-successdescriptor" helper = TemplateHelper(api_mock) assert ( helper.print_ref(ref) == "std::function<void(const std::shared_ptr<xref:lang-successdescriptor" "[++detail::SuccessDescriptor++]>&)>") api_mock.link_to_element.assert_has_calls([ call(ref.nested[0].args[0].type.nested[0].id, ref.nested[0].args[0].type.nested[0].name) ])
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_objc_method_signature__one_param(context): method = Member("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 objc_method_signature( method, context) == "- (xref:objc-value[Value])setValue:(Type1)arg1"
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")
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_typeref__eq__none(): ref = TypeRef() assert not ref == None # noqa: E711 assert not None == ref # noqa: E711 assert ref != None # noqa: E711 assert None != ref # noqa: E711
def test_print_ref__link__empty_closure(api_mock): return_type = TypeRef("lang") return_type.name = "MyType" return_type.prefix = "const " return_type.suffix = " &" return_type.id = "lang-tomtom_1_MyType" ref = TypeRef("lang") ref.args = [] ref.returns = return_type helper = TemplateHelper(api_mock) assert helper.print_ref( ref) == "const xref:lang-tomtom_1_MyType[++MyType++] &()" api_mock.link_to_element.assert_called_once_with(return_type.id, return_type.name)
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"
def test_throws_clause__eq__full(): first = ThrowsClause(type=TypeRef(name="type"), description="description") second = ThrowsClause(type=TypeRef(name="type"), description="description") assert first == second assert second == first second.type.name = "other" assert first != second assert second != first second.type.name = first.type.name second.description = "other" assert first != second assert second != first second.description = first.description
def test_return_value__eq__full(): first = ReturnValue(type=TypeRef(name="type"), description="description") second = ReturnValue(type=TypeRef(name="type"), description="description") assert first == second assert second == first second.type.name = "other" assert first != second assert second != first second.type.name = first.type.name second.description = "other" assert first != second assert second != first second.description = first.description
def test_method_signature__ignore_param_type_xref_length(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") method.params[0].type.id = "ab" * 80 helper = TemplateHelper(empty_generating_api) assert (helper.method_signature(method) == f"void ShortMethod(xref:{'ab' * 80}[++int++] " "value)")
def test_objc_method_signature__class_method(context): method = Member("objc") method.name = "start" method.static = True method.returns = ReturnValue() method.returns.type = TypeRef("objc", name="void") assert objc_method_signature(method, context) == "+ (void)start"
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 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 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()"
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"