예제 #1
0
 def test_read_txt_file(self):
     print_class = PrintClass()
     actual = print_class.read_txt_file("test2.txt")
     expect = [
         "@startuml\n", "ToyBox *-- Toy\n", "\n", "class ToyBox {\n",
         "    name : String\n", "}\n", "\n", "class Toy {\n", "}\n",
         "@enduml\n"
     ]
     self.assertEqual(expect, actual)
예제 #2
0
class TestDataExtraction(unittest.TestCase):
    # Clement
    def setUp(self):
        # be executed before each test
        self.test_class = PrintClass()
        self.validator = Validator()
        self.controller = Controller()

    def test_get_class_name(self):
        list_1 = ['class a {\n', '    n : String\n', '    add()\n', '}\n']
        list_2 = ['class  {\n', '    n : String\n', '    add()\n', '}\n']
        list_3 = ['    name : String\n', '    add_attributes()\n', '}\n']
        self.assertEqual(self.test_class.get_class_name(list_1), 'a')
        self.assertEqual(self.test_class.get_class_name(list_2), '')
        self.assertIsNone(self.test_class.get_class_name(list_3))

    def test_validate_attribute_name_true(self):
        self.assertTrue(self.validator.validate_attribute_name("attribute"))
        self.assertTrue(self.validator.validate_attribute_name("clement"))

    def test_validate_attribute_name_false(self):
        self.assertFalse(self.validator.validate_attribute_name("<clement"))
        self.assertFalse(self.validator.validate_attribute_name("??i"))
        self.assertFalse(self.validator.validate_attribute_name("break"))
        self.assertFalse(self.validator.validate_attribute_name("pass"))
        self.assertFalse(self.validator.validate_attribute_name(42))
        self.assertFalse(self.validator.validate_attribute_name(3.14))
        self.assertFalse(
            self.validator.validate_attribute_name(
                '--------------------------------------'
                '-------------------------------------'))

    # Luna
    def test_validate_class_name_is_true(self):
        result_1 = self.validator.validate_class_name("Name")
        result_2 = self.validator.validate_class_name("ClassName")
        self.assertTrue(result_1, "invalid class name")
        self.assertTrue(result_2, "invalid class name")

    def test_validate_class_name_using_special_char(self):
        result = self.validator.validate_class_name("Name$%^&")
        self.assertFalse(result, "valid class name")

    def test_validate_class_name_using_lower(self):
        result = self.validator.validate_class_name("name")
        self.assertFalse(result, "valid class name")

    def test_validate_class_name_start_with_num(self):
        result = self.validator.validate_class_name("123Name")
        self.assertFalse(result, "valid class name")

    def test_validate_class_name_start_with_char(self):
        result = self.validator.validate_class_name("$%^_+Name")
        self.assertFalse(result, "valid class name")

    def test_read_word_file(self):
        actual = self.test_class.read_word_file("test2.docx")
        expect = [
            "@startuml\n", "ToyBox *-- Toy\n", "\n", "class ToyBox {\n",
            "    name : String\n", "}\n", "\n", "class Toy {\n", "}\n",
            "@enduml\n"
        ]
        self.assertEqual(expect, actual, "cannot read word file")

    def test_read_txt_file(self):
        actual = self.test_class.read_txt_file("test2.txt")
        expect = [
            "@startuml\n", "ToyBox *-- Toy\n", "\n", "class ToyBox {\n",
            "    name : String\n", "}\n", "\n", "class Toy {\n", "}\n",
            "@enduml\n"
        ]
        self.assertEqual(expect, actual, "cannot read txt file")

    def test_load_word_file(self):
        actual = self.controller.load_file("test2.docx")
        self.assertTrue(actual, "cannot load word file")

    def test_load_txt_file(self):
        actual = self.controller.load_file("test2.txt")
        self.assertTrue(actual, "cannot load txt file")

    def test_load_file_not_found_exception(self):
        actual = self.controller.load_file("C:\\Users\\Luna\\ICT\\test2.txt")
        self.assertRaises(FileNotFoundError, actual)

    def test_load_incorrect_file_exception(self):
        actual = self.controller.load_file("test2.csv")
        self.assertRaises(NameError, actual)

    def test_get_method_name(self):
        class_item = self.test_class.class_handler("test_method.docx")
        actual_one = self.test_class.get_methods(class_item[0])
        expected_one = ["add_toy", "get_toy"]
        actual_two = self.test_class.get_methods(class_item[1])
        expected_two = ["__str__"]
        self.assertEqual(expected_one, actual_one, "cannot get method name")
        self.assertEqual(expected_two, actual_two, "cannot get method name")

    # Rajan
    def test_validate_method_name_is_false(self):
        validator = Validator()
        result_1 = validator.validate_method_name("Get")
        result_2 = validator.validate_method_name("1_get")
        result_3 = validator.validate_method_name("get_Name")
        self.assertFalse(result_1)
        self.assertFalse(result_2)
        self.assertFalse(result_3)

    def test_validate_method_name_is_true2(self):
        validator = Validator()
        result_1 = validator.validate_method_name("_get")
        result_2 = validator.validate_method_name("get1")
        self.assertTrue(result_1)
        self.assertTrue(result_2)

    def test_validate_method_name_is_true(self):
        validator = Validator()
        result_1 = validator.validate_method_name("method_name")
        result_2 = validator.validate_method_name("get")
        self.assertTrue(result_1)
        self.assertTrue(result_2)

    def test_validate_method_name_is_false2(self):
        validator = Validator()
        result_1 = validator.validate_method_name("Name")
        result_2 = validator.validate_method_name("get_Name")
        result_3 = validator.validate_method_name("1_get")
        self.assertFalse(result_1)
        self.assertFalse(result_2)
        self.assertFalse(result_3)
class TestBadSmells(unittest.TestCase):
    def setUp(self):
        self.command = Command()
        self.printClass = PrintClass()
        if hasattr(self.printClass, 'fileInput'):
            self.printClass.fileProcessor.relationship_list = [
                'Zoo "1" o-- "many" Animal', '"1" *-- "many"', '*--'
            ]
        self.printClass.class_list = [[
            'class Zoo {\n', '    name : String\n', '    location : String\n',
            '    add_animal()\n', '    get_animal()\n', '}\n'
        ],
                                      [
                                          'class Animal {\n',
                                          '    name : String\n',
                                          '    number : Integer\n',
                                          '    __str__()\n', '}\n'
                                      ]]
        self.controller = Controller()
        self.command = Command()

    def test_do_display(self):
        self.command.do_display("/a")
        self.command.do_display("/l")
        self.command.do_display("/p")
        self.command.do_display("/o")
        self.command.do_display("")

    def test_get_class_name(self):
        self.assertEqual(
            self.printClass.get_class_name(
                ['class z {\n', '    n : String\n', '    add()\n', '}\n']),
            'z')
        self.printClass.get_class_name(
            [' z {\n', '    n : String\n', '    add()\n', '}\n'])

    def test_output_classes(self):
        self.printClass.output_classes("")

    def test_output_class_exception_invalid_attribute_name(self):
        try:
            self.printClass.output_class([
                'class zoo {\n', '    Name : String\n',
                '    location : String\n', '    add_animal()\n',
                '    get_animal()\n', '}\n'
            ])
        except NameError:
            self.fail("output_class raised NameError unexpectedly")

    def test_output_class_exception_invalid_method_name(self):
        self.assertEqual(
            self.printClass.output_class([
                'class Zoo {\n', '    name : String\n',
                '    location : String\n', '    Add_animal()\n',
                '    Get_animal()\n', '}\n'
            ]), 'class Zoo:\n'
            '    def __init__(self, name, location):\n'
            '        self.name = name\n'
            '        self.location = location\n'
            '# method name is invalid\n'
            '# method name is invalid\n')

    def test_output_class_test_no_attribute(self):
        self.printClass.output_class([
            'This is a first line', 'Zoo "1" o - - "many" Animal',
            'class Zoo {\n', '    Add_animal()\n', '    Get_animal()\n', '}\n'
        ])

    def test_output_class_test_relationships(self):
        self.printClass.class_handler("uml.txt")

    def test_get_all_num(self):
        self.printClass.get_all_num()

    def test_read_text_file(self):
        self.printClass.read_txt_file("clement.txt")

    def test_identify_r_type(self):
        self.printClass.identify_r_type("o-- <-- <..", "name")
        self.printClass.identify_r_type("<-- <-- <--", "name")
        self.printClass.identify_r_type("o-- o-- o--", "name")
        self.printClass.identify_r_type("<.. <.. <..", "name")
        self.printClass.identify_r_type("<.. <..", "name")
        self.printClass.identify_r_type('"_ #~~ _"', "name")
        self.printClass.identify_r_type('"1" o-- "many"', "name")
        self.printClass.identify_r_type('"1" *-- "many" "1" *-- "many"',
                                        "name")
        self.printClass.identify_r_type("o-- *-- <..", "name")

    def test_class_handler(self):
        self.printClass.class_handler("test3.csv")
        self.printClass.class_handler("test4.txt")

    def test_fileHandler(self):
        self.controller.load_file("uml.txt")
        self.controller.save_file("")
        self.controller.load_file("test.docx")
        self.controller.save_file("")
        self.controller.load_file("test1.docx")
        self.controller.save_file("")
        self.controller.load_file("test2.docx")
        self.controller.save_file("")
        self.controller.load_file("test3.txt")
        self.controller.save_file("")
        self.controller.load_file("test4.txt")
        self.controller.save_file("")