Пример #1
0
    def test_list_parameters(self):
        model = UMLModel()
        model.create_class("class1")
        
        # variables used for testing equality
        message = model.list_parameters("class2", "test")[1]
        # test output equality with using a non-existent class 
        self.assertEqual(message, "class2 does not exist")

        # variables used for testing equality
        message = model.list_parameters("class1", "test")[1]
        # test output equality without inserting methods 
        self.assertEqual(message, "class1 does not have method, test")

        # add some methods to class1
        model.create_method("class1", "public", "int", "getYear")
        # add some params to getYear
        model.create_parameter("class1", "getYear", "string", "calendarName")
        model.create_parameter("class1", "getYear", "int", "year")

        # variables used for testing equality
        message = model.list_parameters("class1", "getYear")[1]
        outString = "".join(("Parameters for getYear\n",
                            "(string):calendarName\n",  
                            "(int):year"))
        # test output equality            
        self.assertEqual(message,outString)
Пример #2
0
    def test_rename_parameter(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_method("class1", "public", "string", "method1")
        model.create_parameter("class1", "method1", "param_type", "param_name")

        # Ensure we get correct output when class doesn't exist
        status, msg = model.rename_parameter("class9", "method1", "param_name", "new_param_name")
        self.assertFalse(status)
        self.assertEqual(msg, "class9 does not exist")

        # Ensure we get correct output when method doesn't exist
        status, msg = model.rename_parameter("class1", "method3", "param_name", "new_param_name")
        self.assertFalse(status)
        self.assertEqual(msg, "class1 does not have method, method3")

        # Ensure we get correct output when the parameter we wish to rename doesn't exist
        status, msg = model.rename_parameter("class1", "method1", "param_name7", "new_param_name")
        self.assertFalse(status)
        self.assertEqual(msg, " param_name7 does not exists in method1")

        # Ensure we get correct output when the parameter we wish to rename already exists
        status, msg = model.rename_parameter("class1", "method1", "param_name", "param_name")
        self.assertFalse(status)
        self.assertEqual(msg, " param_name already exists in method1")
        
        #ensure parameter is renamed
        model.rename_parameter("class1", "method1", "param_name", "new_param_name")
        
        # Ensure duplicate parameter is not created
        self.assertEqual(model.classes["class1"].methods[model.classes["class1"].method_index("method1")].parameters[0].name, "new_param_name")
Пример #3
0
    def test_delete_parameter(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_method("class1", "public", "string", "method1")
        model.create_parameter("class1", "method1", "param_type", "parameter_name")
        testClass = model.classes["class1"]

        # Ensure we get correct output when class doesn't exist
        status, msg = model.delete_parameter("class9", "method1", "parameter_name")
        self.assertFalse(status)
        self.assertEqual(msg, "class9 does not exist")

        # Ensure we get correct output when method doesn't exist
        status, msg = model.delete_parameter("class1", "method6", "parameter_name")
        self.assertFalse(status)
        self.assertEqual(msg, "class1 does not have method, method6")
        
        # Ensure parameter is deleted
        status, msg = model.delete_parameter("class1", "method1", "parameter_name")
        self.assertFalse(testClass.methods[testClass.method_index("method1")].has_parameter("parameter_name"))

        # Ensure we get correct output after parameter is deleted
        self.assertTrue(status)
        self.assertEqual(msg, "parameter 'parameter_name' has been removed from 'method1'")

        # Ensure we get correct output if the parameter we wish to delete doesn't exist
        status, msg = model.delete_parameter("class1", "method1", "parameter_name")
        self.assertFalse(status)
        self.assertEqual(msg, " parameter_name does not exists in method1")
Пример #4
0
    def test_move_down_method(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_method("class1", "public", "string", "method1")
        model.create_method("class1", "public", "string", "method2")
        model.create_method("class1", "public", "string", "method3")
        testClass = model.classes["class1"]

        # Ensure the methods'position 
        self.assertEqual(testClass.methods[0].name, "method1")
        self.assertEqual(testClass.methods[1].name, "method2")
        self.assertEqual(testClass.methods[2].name, "method3")

        # Move down the method one position from the list
        model.move_down_method("class1", "method2") 
        
        # Ensure the method has a right position after moved
        self.assertEqual(testClass.method_index("method1"), 0)
        self.assertEqual(testClass.method_index("method3"), 1)
        self.assertEqual(testClass.method_index("method2"), 2)
        
        # Ensure the method don't move down further
        status, msg = model.move_down_method("class1", "method2")
        self.assertFalse(status)

        # Ensure correct response is outputted when class doesn't exist
        status, msg = model.move_down_method("class7", "method2")
        self.assertFalse(status)
        self.assertEqual(msg, "class7 does not exist")
        
        # Ensure correct response is outputted when method doesn't exist
        status, msg = model.move_down_method("class1", "method9")
        self.assertFalse(status)
        self.assertEqual(msg, "method9 does not exist in class1")
    def test_fetch_from_method(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_method("class1", "public", "int", "method1")
        model.create_parameter("class1", "method1", "int", "num")
        model.create_parameter("class1", "method1", "bool", "isTrue")
        model.create_method("class1", "public", "bool", "method2")

        # Test 1: get parameters
        params = fetch_from_method(model, "class1", "method1")
        self.assertEqual(params, ["num", "isTrue"])

        # Test 2: no parameters
        params = fetch_from_method(model, "class1", "method2")
        self.assertEqual(params, [])
Пример #6
0
    def test_delete_method(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_method("class1", "public", "void", "add")
        testClass = model.classes["class1"]

        # Ensure we get correct output when class doesn't exist
        status, msg = model.delete_method("class4", "add")
        self.assertFalse(status)
        self.assertEqual(msg, "'class4' does not exist")

        # Ensure we get correct output when the method we wish to delete doesn't exist
        status, msg = model.delete_method("class1", "subtract")
        self.assertFalse(status)
        self.assertEqual(msg, "'subtract' does not exist in 'class1'")

        # Ensure method is deleted
        model.delete_method("class1", "add")
        self.assertFalse(testClass.has_method("add"))
Пример #7
0
    def test_create_method(self):
        model = UMLModel()
        model.create_class("class1")
        testClass = model.classes["class1"]

        # Ensure method is created
        model.create_method("class1", "public", "void", "add")
        self.assertTrue(testClass.has_method("add"))

        # Ensure we get correct output when class doesn't exist
        status, msg = model.create_method("class3", "public", "void", "subtract")
        self.assertFalse(status)
        self.assertEqual(msg, "class3 does not exist")

        # Ensure duplicate method is not created
        status, msg = model.create_method("class1", "public", "void", "add")
        # ensure it failed
        self.assertFalse(status)
        self.assertEqual(msg, "method add already exists in class1")
    def test_fetch_from_class(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_class("class2")
        model.create_field("class1", "public", "string", "field1")
        model.create_field("class1", "public", "int", "field2")
        model.create_method("class1", "public", "int", "method1")
        model.create_method("class1", "public", "bool", "method2")
        model.create_relationship("inheritance", "class1", "class2")

        # Test 1: fetch fields
        fields = fetch_from_class(model, "class1", "fields")
        self.assertEqual(fields, ["field1", "field2"])

        # Test 2: fetch methods
        methods = fetch_from_class(model, "class1", "methods")
        self.assertEqual(methods, ["method1", "method2"])

        # Test 3: empty list
        fields = fetch_from_class(model, "class2", "fields")
        self.assertEqual(fields, [])
Пример #9
0
    def test_list_methods(self):
        model = UMLModel()
        model.create_class("class1")

        # variables used for testing equality
        message = model.list_methods("class3")[1]
        # test output equality with a non-existent class
        self.assertEqual(message, "class3 does not exist")
        
        # variables used for testing equality
        message = model.list_methods("class1")[1]
        # test output equality without inserting methods 
        self.assertEqual(message, "class1 has no methods")

        # add some methods to class1
        model.create_method("class1", "public", "int", "getYear")
        model.create_method("class1", "private", "int", "getSalary")
        model.create_method("class1", "protected", "string", "getSSN")

        # variables used for testing equality
        message = model.list_methods("class1")[1]
        outString = "".join(("Methods for class1\n",
                            "PUBLIC getYear() : int\n", 
                            "PRIVATE getSalary() : int\n", 
                            "PROTECTED getSSN() : string"))
        # test output equality            
        self.assertEqual(message,outString) 
Пример #10
0
    def test_list_class(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_class("class2")
        model.create_class("class3")

        # variables used for testing equality
        message = model.list_class("class5")[1]
        # test output equality while using the wrong class name
        self.assertEqual(message, "'class5' is not a valid class")
        
        # add fields to class1
        model.create_field("class1", "public", "void", "a1")
        model.create_field("class1", "private", "int", "size")
        # add methods to class1 along with some parameters
        model.create_method("class1", "protected", "void", "setSpeed")
        model.create_method("class1", "public", "void", "walk")
        model.create_parameter("class1", "setSpeed", "int", "speed")
        model.create_parameter("class1", "walk", "int", "speed")
        model.create_parameter("class1", "walk", "double", "direction")
        # add relationships to class1
        model.create_relationship("inheritance", "class1", "class2")
        model.create_relationship("aggregation", "class3", "class1")

        # variables used for testing equality
        message = model.list_class("class1")[1]
        outString = "".join(("Class: class1\n", 
                    "=== Fields ======================\n", 
                    "public a1: void\n", 
                    "private size: int\n", 
                    "=== Methods =====================\n", 
                    "protected setSpeed(int speed): void\n", 
                    "public walk(int speed, double direction): void\n", 
                    "=== Relationships ===============\n", 
                    "class1 ---------> class2\n", 
                    "class1 <<>>------ class3\n", 
                    "================================="))
        # test output equality            
        self.assertEqual(message,outString)
Пример #11
0
    def test_create_parameter(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_method("class1", "public", "string", "method1")
        model.create_parameter("class1", "method1", "param_type", "param_name")

        self.assertTrue(model.classes["class1"].methods[model.classes["class1"].method_index("method1")].has_parameter("param_name"))

        # Ensure we get correct output when class doesn't exist
        status, msg = model.create_parameter("class9", "method1", "param_type", "param_name")
        self.assertFalse(status)
        self.assertEqual(msg, "class9 does not exist")

        # Ensure we get correct output when method doesn't exist
        status, msg = model.create_parameter("class1", "method3", "param_type", "param_name")
        self.assertFalse(status)
        self.assertEqual(msg, "method method3 does not exist in class1")

        # Ensure duplicate parameter is not created
        status, msg = model.create_parameter("class1", "method1", "param_type", "param_name")

        # ensure it failed
        self.assertFalse(status)
Пример #12
0
    def test_rename_method(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_method("class1", "public", "void", "add")

        # Ensure we get correct output when class doesn't exist
        status, msg = model.rename_method("class3", "add", "subtract")
        self.assertFalse(status)
        self.assertEqual(msg, "class3 does not exist")

        # Ensure we get correct output when method doesn't exist
        status, msg = model.rename_method("class1", "multiply", "subtract")
        self.assertFalse(status)
        self.assertEqual(msg, "method multiply does not exist in class1")

        # Ensure we get correct output when new method name already exists
        status, msg = model.rename_method("class1", "add", "add")
        self.assertFalse(status)
        self.assertEqual(msg, "method 'add' already exists in 'class1'")

        # Ensure method is renamed
        model.rename_method("class1", "add", "subtract")
        self.assertEqual(model.classes["class1"].methods[0].name, "subtract")
    def test_complete_rename_parameter(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_method("class1", "public", "int", "method1")
        model.create_parameter("class1", "method1", "int", "a")
        model.create_parameter("class1", "method1", "int", "b")
        model.create_parameter("class1", "method1", "int", "abba")
        model.create_method("class1", "public", "int", "method2")
        model.create_method("class1", "public", "int", "mymethod")
        model.create_class("myclass")
        repl = REPL(model, CommandHistory(HISTORY_LIMIT))

        # Test 1: Tab before classname
        classnames = repl.complete_rename_parameter("", "rename_parameter ", 0,
                                                    0)
        self.assertEqual(classnames, ["class1", "myclass"])

        # Test 2: Tab in the middle of classname - only one match
        classnames = repl.complete_rename_parameter("cl",
                                                    "rename_parameter cl", 0,
                                                    0)
        self.assertEqual(classnames, ["class1"])

        # Test 3: Tab before methodname
        classnames = repl.complete_rename_parameter(
            "", "rename_parameter class1 ", 0, 0)
        self.assertEqual(classnames, ["method1", "method2", "mymethod"])

        # Test 4: Tab during methodname
        classnames = repl.complete_rename_parameter(
            "met", "rename_parameter class1 met", 0, 0)
        self.assertEqual(classnames, ["method1", "method2"])

        # Test 5: Tab before old param name
        classnames = repl.complete_rename_parameter(
            "", "rename_parameter class1 method1 ", 0, 0)
        self.assertEqual(classnames, ["a", "b", "abba"])

        # Test 6: Tab during old param name
        classnames = repl.complete_rename_parameter(
            "a", "rename_parameter class1 method1 a", 0, 0)
        self.assertEqual(classnames, ["a", "abba"])

        # Test 7: Tab after old param name
        classnames = repl.complete_rename_parameter(
            "", "rename_parameter class1 method1 abba ", 0, 0)
        self.assertEqual(classnames, [])

        # Test 8: Tab after old param name
        classnames = repl.complete_rename_parameter(
            "iLikePie", "rename_parameter class1 method1 abba iLikePie", 0, 0)
        self.assertEqual(classnames, [])
Пример #14
0
    def test_save_model(self):
        model = UMLModel()
        model.create_class('class1')
        model.create_class('class2')
        model.create_class('class3')
        model.create_field('class1', "protected", "int", "number")
        model.create_method('class2', "public", "string", "getMsg")
        model.create_parameter('class2', 'getMsg', 'string', 'msg')

        # Test 1: Normal save
        model.save_model("test-save.json")
        # ensure data is in the json file
        data = None
        with open("code/server-data/test-save.json", 'r') as file:
            data = json.loads(file.read())
        expectedData = {
            "class1" : {
                "name" : "class1",
                "fields" : [{
                    "visibility" : "protected",
                    "type" : "int",
                    "name" : "number"
                }],
                "methods" : [],
                "relationships" : [],
                "x" : 200,
                "y" : 0,
                "zindex" : 0
            },
            "class2" : {
                "name" : "class2",
                "fields" : [],
                "methods" : [{
                    "visibility" : "public",
                    "type" : "string",
                    "name" : "getMsg",
                    "parameters" : [{
                        'type' : 'string',
                        'name' : 'msg'
                    }]
                }],
                "relationships" : [],
                "x" : 200,
                "y" : 0,
                "zindex" : 0
            },
            "class3" : {
                "name" : "class3",
                "fields" : [],
                "methods" : [],
                "relationships" : [],
                "x" : 200,
                "y" : 0,
                "zindex" : 0
            }
        }
        self.assertEqual(data, expectedData)

        # Test 2: save to different directory
        directory = os.getcwd() + '/'
        model.save_model("test-save2.json", directory=directory)
        data = None
        passes = True
        try:
            with open("test-save2.json", 'r') as file:
                data = json.loads(file.read())
        except FileNotFoundError:
            passes = False
        self.assertTrue(passes)
        self.assertEqual(data, expectedData)