def test_method_not_equal_param(): parameter1 = Attribute("b", "int") parameter2 = Attribute("a", "int") a = Method("a", parameters=[parameter1]) b = Method("a", parameters=[parameter2]) output = (a == b) assert output == False
def test_method_params_unordered(): parameter1 = Attribute("a", "int") parameter2 = Attribute("a", "int") parameter3 = Attribute("b", "int") parameter4 = Attribute("b", "int") a = Method("a", parameters=[parameter1, parameter3]) b = Method("a", parameters=[parameter4, parameter2]) output = (a == b) assert output == False
def test_method_equal_params(): parameter1 = Attribute("a", "int") parameter2 = Attribute("a", "int") parameter3 = Attribute("b", "int") parameter4 = Attribute("b", "int") a = Method("a", parameters=[parameter1, parameter3]) b = Method("a", parameters=[parameter2, parameter4]) output = (a == b) assert output == True
def test_python_example_3(): field1 = Attribute("mail_reader", "MailReader", Visibility.public) field2 = Attribute("configs", "Configs", Visibility.private) field3 = Attribute("network_adapter", "NetworkAdapter", Visibility.public) field4 = Attribute("test_runner", "TestRunner", Visibility.protected) param1 = Attribute("how_much", "int") param2 = Attribute("flag1", "bool") param3 = Attribute("flag2", "bool") method1 = Method("initialize", "void") method2 = Method("initialize_2", "void", modifier=Modifier.abstract) method3 = Method("do_things", "void", [param1, param2, param3], Visibility.protected, Modifier.static) method4 = Method( "no_one_will_ever_use_this", "string", [Attribute("trash", "void"), Attribute("trash_can", "void")], Visibility.private) weird_god_class = ClassData("WeirdGodClass", [field1, field2, field3, field4], [method1, method2, method3, method4], [], [Interface("IDoManyThings", [])]) with open(examples_folder + "/python_example_3.txt", 'r') as python_example: expected = python_example.read() result = ClassToPython(weird_god_class).convert() assert result == expected
def read_xml(uml_data: str): uml_data = uml_data.replace("<br>", "") uml_data = uml_data.replace("<br/>", "") list_of_attributes = [] list_of_methods = [] id_to_name = {} html = bs(uml_data, 'html.parser') result = html.find_all('p') class_name = '' for i in range(len(result)): if result[i].string is not None: if i == 0: class_name = result[i].b.string id_to_name[id] = class_name else: visibility = VisibilityExtractor.extract_visibility( result[i].string) type_ = ReturnTypeExtractor.extract_type(result[i].string) if '(' in result[i].string: name = MethodNameExtractor.extract_name( result[i].string) list_of_parameters_string = ParametersExtractor.extract_parameters_string( result[i].string) list_of_parameters = [] if len(list_of_parameters_string ) != 0 and list_of_parameters_string[0] != '': for parameter_string in list_of_parameters_string: parameter_name = AttributeNameExtractor.extract_name( parameter_string) parameter_type = ReturnTypeExtractor.extract_type( parameter_string) parameter = Attribute(parameter_name, parameter_type) list_of_parameters.append(parameter) method = Method(name, type_, list_of_parameters, visibility) list_of_methods.append(method) else: name = AttributeNameExtractor.extract_name( result[i].string) attribute = Attribute(name, type_, visibility) list_of_attributes.append(attribute) return ClassData(class_name, list_of_attributes, list_of_methods)
def test_interface_example_2(): method1 = Method( "foo", "void", [Attribute("a", "void"), Attribute("b", "int")], Visibility.public) method2 = Method("do", "void", [], Visibility.public) example = Interface("Example", [method1, method2], Visibility.public, [Interface("IFoo", [])]) with open(path.join(examples_folder, "interface_example_2.txt"), 'r') as python_example: expected = python_example.read() result = InterfaceToPython(example).convert() assert result == expected
def create_weapon(): name = Attribute("name", "str", visibility=Visibility.public) age = Attribute("age", "int", visibility=Visibility.private) attribute = Attribute("attribute", "Attribute", visibility=Visibility.protected) getAttribute = Method("getAttribute", return_type="Attribute") setAttribute = Method("setAttribute", return_type="void", parameters=[attribute]) weapon = ClassData("Weapon", methods=[getAttribute, setAttribute], fields=[name, age, attribute]) return weapon
def test_java_example_1(): att1 = Attribute("name", "String", Visibility.public) att2 = Attribute("age", "int", Visibility.private) method1 = Method("bark", "void", [], Visibility.public) param1 = Attribute("byAge", "int", Visibility.public) method2 = Method("growUp", "bool", [param1], Visibility.private) dog_class = ClassData("Dog", [att1, att2], [method1, method2]) with open(path.join(examples_folder, "java_example_1.txt"), 'r') as java_example: expected = java_example.read() result = ClassToJava(dog_class).convert() assert result == expected
def test_interfaces_returned_read_xml(): examples_folder = path.abspath( path.join(__file__, "../../../src/xmlToData/uml_samples/uml_interface.xml")) interface_ = Interface("CalculoDeSalario", [ Method("calcular_salario_1", "float", [Attribute("funcionario", "Funcionario")]), Method("calcular_salario_2", "float", [ Attribute("funcionario", "Funcionario"), Attribute("carga_horaria", "int") ]) ], Visibility.public) list_of_classes, list_of_interfaces = DrawIoXmlParser( examples_folder).read_xml() assert list_of_interfaces[0] == interface_
def test_classes_returned_read_xml(): examples_folder = path.abspath( path.join(__file__, "../../../src/xmlToData/uml_samples/uml1.xml")) class_ = ClassData("Humano", [ Attribute("idade", "int", Visibility.public), Attribute("anos", "float", Visibility.private) ], [ Method("get_idade", "int", [Attribute("nome", "string"), Attribute("altura", "double")], Visibility.public), Method("get_anos", "float", [], Visibility.private) ]) list_of_classes, list_of_interfaces = DrawIoXmlParser( examples_folder).read_xml() assert list_of_classes[0] == class_
def create_obese_orc(): food = Attribute("food", "IFood", visibility=Visibility.public) heart_attack = Attribute("heartAttackChance", "int", visibility=Visibility.public) eat = Method("eat", parameters=[food], modifier=Modifier.override) fat_orc = create_fat_orc() obese_orc = ClassData("ObeseOrc", methods=[eat], fields=[heart_attack], inheritances=[fat_orc]) return obese_orc
def test_java_example_2(): param1 = Attribute("damage", "int") param2 = Attribute("entity", "Entity") param3 = Attribute("bonus", "Bonus") method1 = Method("attack", "void", [param1, param2, param3]) method2 = Method("cry", "void", [], Visibility.protected, Modifier.static) orc_class = ClassData("Orc", [], [method1, method2], [ClassData("Monster")], [Interface("IWalk", []), Interface("IAttack", [])]) with open(examples_folder + "/java_example_2.txt", 'r') as java_example: expected = java_example.read() result = ClassToJava(orc_class).convert() assert result == expected
def test_static_protected_method_with_parameters(): param = Attribute("name", "String") method = Method("example", "int", [param], Visibility.protected, Modifier.static) method_to_python = MethodToPython([method], False) expected = "\t@staticmethod\n\tdef _example(name):\n\t\tpass" assert method_to_python.get_formatted_methods() == expected
def create_context(): attribute = Attribute("strategy", "Strategy", visibility=Visibility.public) method = Method("doSomeBusinessLogic") context = ClassData("Context", methods=[method], fields=[attribute]) return context
def create_fat_orc(): food = Attribute("food", "IFood", visibility=Visibility.public) eat = Method("eat", parameters=[food]) orc = create_orc() fat_orc = ClassData("FatOrc", methods=[eat], inheritances=[orc]) return fat_orc
def test_formatted_method_parameters(parameters, expected): parameter_list: List[Attribute] = [] for parameter in parameters: name, return_type = parameter new_parameter = Attribute(name, return_type) parameter_list.append(new_parameter) method = Method("example", parameters=parameter_list) method_to_python = MethodToPython([method], False) assert method_to_python.get_formatted_methods() == expected
def create_orc(): name = Attribute("name", "str", visibility=Visibility.public) age = Attribute("age", "int", visibility=Visibility.private) damage = Attribute("damage", "int", visibility=Visibility.public) hours = Attribute("hours", "int", visibility=Visibility.public) walk = create_walk() attack_interface = create_attack() attack_method = Method("attack", parameters=[damage]) sleep = Method("sleep", parameters=[hours], visibility=Visibility.private) orc = ClassData("Orc", methods=[attack_method, sleep], fields=[name, age], implementations=[attack_interface, walk]) return orc
def create_high_orc(): damage = Attribute("damage", "int", visibility=Visibility.public) hours = Attribute("hours", "int", visibility=Visibility.public) spell = Attribute("spell", "ISpell", visibility=Visibility.public) attack = Method("attack", parameters=[damage], modifier=Modifier.override) sleep = Method("sleep", parameters=[hours], visibility=Visibility.private, modifier=Modifier.override) orc = create_orc() high_orc = ClassData("HighOrc", methods=[attack, sleep], fields=[spell], inheritances=[orc]) return high_orc
def test_python_example_4(): att1 = Attribute("m", "String", Visibility.public) method = Method("v", "void", [], Visibility.public, modifier=Modifier.abstract) dog_class = ClassData("Test4", [att1], [method]) with open(path.join(examples_folder, "python_example_4.txt"), 'r') as python_example: expected = python_example.read() result = ClassToPython(dog_class).convert() assert result == expected
def read_xml(uml_data: str): list_of_methods = [] list_of_methods_string_literals = [] html = bs(uml_data, 'html.parser') result = html.find_all('p') if result[0].b is None: interface_name = result[1].b.string else: interface_name = result[0].b.string list_of_methods_string_literals += str(result[-1]).split("<br>") if len(list_of_methods_string_literals) == 1: list_of_methods_string_literals = str(result[-1]).split("<br/>") list_of_methods_string_literals[0] = \ list_of_methods_string_literals[0].replace("<p style=\"margin: 0px ; margin-left: 4px\">", "") list_of_methods_string_literals[-1] = \ list_of_methods_string_literals[-1].replace("</p>", "") for xml_string in list_of_methods_string_literals: if xml_string != "": visibility = VisibilityExtractor.extract_visibility(xml_string) type_ = ReturnTypeExtractor.extract_type(xml_string) name = MethodNameExtractor.extract_name(xml_string) list_of_parameters_string = ParametersExtractor.extract_parameters_string( xml_string) list_of_parameters = [] if len(list_of_parameters_string ) != 0 and list_of_parameters_string[0] != '': for parameter_string in list_of_parameters_string: parameter_name = AttributeNameExtractor.extract_name( parameter_string) parameter_type = ReturnTypeExtractor.extract_type( parameter_string) parameter = Attribute(parameter_name, parameter_type) list_of_parameters.append(parameter) method = Method(name, type_, list_of_parameters, visibility) list_of_methods.append(method) return Interface(interface_name, list_of_methods)
def create_attack(): damage = Attribute("damage", "int", visibility=Visibility.public) method = Method("attack", parameters=[damage]) interface = Interface("IAttack", methods=[method]) return interface
def create_attribute(): field1 = Attribute("value", "float", visibility=Visibility.public) field2 = Attribute("multiplier", "float", visibility=Visibility.public) attribute = ClassData("Attribute", fields=[field1, field2]) return attribute
def create_do_algorithm(): attribute = Attribute("data", "ArrayList<String>") method = Method("doAlgorithm", parameters=[attribute], return_type="ArrayList<String>") return method
def test_method_diffent_param_number(): a = Method("a") parameter = Attribute("a", "int") b = Method("a", parameters=[parameter]) output = (a == b) assert output == False
def test_attribute_equal(a, b, expected): a = Attribute(a[0], a[1], a[2]) b = Attribute(b[0], b[1], b[2]) output = (a == b) assert output == expected
def create_attribute(): method = Method("method") field = Attribute("field", "Type", visibility=Visibility.public) attribute = ClassData("Attribute", methods=[method], fields=[field]) return attribute
def create_do_algorithm(): attribute = Attribute("data", "str") method = Method("doAlgorithm", parameters=[attribute]) return method