def __init__(self):
        Cmd.__init__(self)
        self.prompt = ">>> "
        self.db = SqliteDB("unittest.db")
        self.analyser = JSAnalyser()
        self.grapher = Grapher()

        self.db.setup()
Exemple #2
0
    def build_menu(self):
        # Menu
        menu = Menu()
        # Database
        database = Database("unittest.db")
        database.set_connect_behaviour(SqliteConnect())
        database.set_setup_behaviour(SqliteSetup())
        database.set_query_behaviour(SqliteQuery())
        database.set_fetch_behaviour(SqliteFetch())
        menu.set_database(database)
        # Grapher
        grapher = Grapher()
        menu.set_grapher(grapher)
        # Analyser
        analyser = JSAnalyser()
        menu.set_analyser(analyser)

        self.menu = menu
Exemple #3
0
    def build_menu(self):
        # Menu
        menu = Menu()
        # Database
        database = Database("bcde321_assignment", "127.0.0.1", "root",
                            "password")
        database.set_connect_behaviour(MySQLConnect())
        database.set_setup_behaviour(MySQLSetup())
        database.set_query_behaviour(MySQLQuery())
        database.set_fetch_behaviour(MySQLFetch())
        menu.set_database(database)
        # Grapher
        grapher = Grapher()
        menu.set_grapher(grapher)
        # Analyser
        analyser = JSAnalyser()
        menu.set_analyser(analyser)

        self.menu = menu
 def setUp(self):
     self.analyser = JSAnalyser(path="package/sample-es6/src/js")
     self.analyser.get_filenames()
     self.analyser.read_files()
class TestAnalyser(unittest.TestCase):
    def setUp(self):
        self.analyser = JSAnalyser(path="package/sample-es6/src/js")
        self.analyser.get_filenames()
        self.analyser.read_files()

    def tearDown(self):
        self.analyser = None

    def test_analyser_set_path(self):
        self.analyser.set_path("test/")
        result = self.analyser.path
        expected = "test/"
        self.assertEqual(result, expected)

    def test_analyser_get_filenames(self):
        self.analyser.get_filenames()
        result = self.analyser.filenames
        expected = [
            "package/sample-es6/src/js\\drag.js",
            "package/sample-es6/src/js\\html.js",
            "package/sample-es6/src/js\\main.js",
            "package/sample-es6/src/js\\utils.js",
        ]
        self.assertEqual(result, expected)

    def test_analyser_get_filenames_with_file(self):
        self.analyser.set_path("package/sample-es6/src/js/drag.js")
        self.analyser.get_filenames()
        result = self.analyser.filenames
        expected = ["package/sample-es6/src/js/drag.js"]
        self.assertEqual(result, expected)

    def test_analyser_get_filenames_incorrect(self):
        self.analyser.set_path("incorrect path")
        self.analyser.get_filenames()
        result = self.analyser.filenames
        expected = []
        self.assertEqual(result, expected)

    def test_analyser_read_files_fail(self):
        self.analyser.filenames.append("incorrect file name")
        result = self.analyser.read_files()
        self.assertFalse(result)

    def test_analyser_get_files(self):
        result = len(self.analyser.get_files())
        expected = 4
        self.assertEqual(result, expected)

    def test_analyser_get_classes(self):
        result = []
        for aClass in self.analyser.get_classes():
            result.append(aClass.get_name())
        expected = ["Drag", "Html", "Base"]
        self.assertEqual(result, expected)

    def test_analyser_file_count(self):
        result = self.analyser.file_count()
        expected = 4
        self.assertEqual(result, expected)

    def test_analyser_class_count(self):
        result = self.analyser.class_count()
        expected = 3
        self.assertEqual(result, expected)

    def test_analyser_method_count(self):
        result = self.analyser.method_count()
        expected = 6
        self.assertEqual(result, expected)

    def test_analyser_attribute_count(self):
        result = self.analyser.attribute_count()
        expected = 2
        self.assertEqual(result, expected)

    def test_analyser_to_string(self):
        result = str(self.analyser)
        expected = type("string")
        self.assertEqual(type(result), expected)

    def test_class_model_get_attributes(self):
        result = []
        for aClass in self.analyser.get_classes():
            if len(aClass.get_attributes()) > 0:
                result.append(aClass.get_attributes())
        expected = [["Base"], ["container"]]
        self.assertEqual(result, expected)

    def test_class_model_get_methods(self):
        result = []
        for aClass in self.analyser.get_classes():
            for aMethod in aClass.get_methods():
                result.append(aMethod.get_name())
        expected = [
            "constructor(base)",
            "constructor(base)",
            "createContainer()",
            "htmlTest()",
            "constructor()",
            "baseTest()",
        ]
        self.assertEqual(result, expected)

    def test_file_model_multiple_classes(self):
        self.analyser.set_path(
            "package/unit-tests/test_files/single_file_multiple_class.js"
        )
        self.analyser.get_filenames()
        self.analyser.read_files()
        result = []
        for aClass in self.analyser.get_classes():
            result.append(aClass.get_name())
        expected = ["Animal", "Insect"]
        self.assertEqual(result, expected)

    def test_method_model_get_parameters(self):
        result = []
        for aClass in self.analyser.get_classes():
            for aMethod in aClass.get_methods():
                for aParameter in aMethod.get_parameters():
                    result.append(aParameter)
        expected = ["base", "base"]
        self.assertEqual(result, expected)
class Menu(Cmd):
    def __init__(self):
        Cmd.__init__(self)
        self.prompt = ">>> "
        self.db = SqliteDB("unittest.db")
        self.analyser = JSAnalyser()
        self.grapher = Grapher()

        self.db.setup()

    def do_add_record(self, path: str):
        """
        Syntax: add_record [path]
        Run an analysis on a file / folder and create a record in the database
        storing file, class, attribute and method counts.
        :param path: a string representing the path to the file / folder for
        analysis
        :return: None
        """
        self.analyser.set_path(path)
        self.analyser.get_filenames()
        self.analyser.read_files()

        file_count = self.analyser.file_count()
        class_count = self.analyser.class_count()
        attribute_count = self.analyser.attribute_count()
        method_count = self.analyser.method_count()

        sql = (f"insert into analysis (path, fileCount, classCount,"
               f" attributeCount, methodCount) values "
               f'("{path}", {file_count}, {class_count},'
               f" {attribute_count}, {method_count})")

        result = self.db.query(sql)
        return result

    def do_get_record(self, path: str):
        """
        Syntax: get_record [path]
        Get an analysis record from the database
        :param path: a string representing the path to the file / folder
        record in the DB
        :return: None
        """
        sql = (f"select path, fileCount, classCount, attributeCount,"
               f' methodCount from analysis where path="{path}"')
        results = self.db.fetch(sql)
        if len(results) == 0:
            print(f"No records found for path: {path}...")
        else:
            for aResult in results:
                print(f"Path: {aResult[0]}\n"
                      f"File Count: {aResult[1]}\n"
                      f"Class Count: {aResult[2]}\n"
                      f"Attribute Count: {aResult[3]}\n"
                      f"Method Count: {aResult[4]}")
        return results

    def do_delete_record(self, path: str):
        """
        Syntax: delete_record [path]
        Remove an analysis record from the database
        :param path: a string representing the path of the
        file / folder record in the DB
        :return: None
        """
        sql = f'delete from analysis where path="{path}"'
        result = self.db.query(sql)

        return result

    def do_list_records(self, line):
        """
        Syntax: list_records
        Lists all stored analysis records in the database
        :return: None
        """
        sql = f"select path from analysis"
        results = self.db.fetch(sql)
        if len(results) == 0:
            print("No records in the database...")
        else:
            for aResult in results:
                print(aResult)

        return results

    def do_analyse(self, path: str):
        """
        Syntax: analyse [path]
        Run an analysis on a file / folder
        :param path: a string representing the path to the
        file / folder for analysis
        :return: None
        """
        self.analyser.set_path(path)
        self.analyser.get_filenames()
        self.analyser.read_files()
        print(self.analyser)

        return True

    def do_draw_class_diagram(self, path: str):
        """
        Syntax: draw_class_diagram [path]
        Render a PDF class diagram of an es6 file / folder
        :param path: a string representing the path to the
        file / folder for analysis
        :return: None
        """
        self.analyser.set_path(path)
        self.analyser.get_filenames()
        self.analyser.read_files()

        classes = self.analyser.get_classes()

        self.grapher.set_classes(classes)
        self.grapher.configure_labels()
        self.grapher.add_nodes()
        self.grapher.render()

        return True

    def do_quit(self, line):
        print("Quitting...")
        return True