def setUp(self):
     self.file_reader = FileReader(JSFileReader())
     self.current_dir = getcwd() + "/tests"
class TestFileReader(TestCase):

    def setUp(self):
        self.file_reader = FileReader(JSFileReader())
        self.current_dir = getcwd() + "/tests"

    def test_read_txt_file(self):
        """This test simply tests the basic reader - whether it can correctly
         read a regular .txt file"""
        file_dir = str.format("{}/testing_files/not_a_js_file.txt",
                              self.current_dir).replace("\\", "/")
        file_contents = "This is not a JS file!"
        self.assertEqual(self.file_reader.get_file_contents(file_dir),
                         file_contents)

    def test_read_js_file(self):
        """This tests to ensure it can correctly read a .js file contents"""
        file_dir = str.format("{}/testing_files/test_file_4.js",
                              self.current_dir).replace("\\", "/")
        expected_file_contents = 'class TestClass {\n' \
                        '    constructor() {\n' \
                        '    this.attribute1 = "";\n' \
                        '    this.attribute2 = "";\n' \
                        '    }\n' \
                        '}'
        actual_file_contents = self.file_reader.get_file_contents(file_dir)
        self.assertEqual(expected_file_contents, actual_file_contents)

    def test_reject_wrong_file(self):
        """This tests to ensure the class will reject a file that's not
        a js file, when using that particular checker."""
        file_dir = str.format("{}/testing_files/not_a_js_file.txt",
                              self.current_dir).replace("\\", "/")
        self.assertFalse(self.file_reader.check_if_valid_file(file_dir))

    def test_accept_right_file(self):
        """This tests to ensure the class will accept a file that's
                a js file, when using that particular checker."""
        file_dir = str.format("{}/testing_files/test_file_1.js",
                              self.current_dir).replace("\\", "/")
        self.assertTrue(self.file_reader.check_if_valid_file(file_dir))

    def test_no_file(self):
        """This tests to ensure the class will reject a file that doesn't
        exist!"""
        file_dir = str.format("{}/testing_files/no_file",
                              self.current_dir).replace("\\", "/")
        self.assertFalse(self.file_reader.check_if_valid_file(file_dir))

    def test_empty_file(self):
        """This tests to ensure the class will read a class that's empty.
        It's not an issue if an empty file makes it through as it makes no
        difference to the program."""
        file_dir = str.format("{}/testing_files/test_file_empty.js",
                              self.current_dir).replace("\\", "/")
        self.assertTrue(self.file_reader.check_if_valid_file(file_dir))

    def test_is_valid_file_dir(self):
        """This tests to see whether it correctly returns true on a valid
        file directory."""
        file_dir = str.format("{}/testing_files/test_file_empty.js",
                              self.current_dir).replace("\\", "/")
        self.assertTrue(self.file_reader.check_if_valid_dir(file_dir))

    def test_is_invalid_file_dir(self):
        """This tests to see whether it correctly returns false on an invalid
        file directory."""
        file_dir = "Bad times! No."
        self.assertFalse(self.file_reader.check_if_valid_dir(file_dir))
 def setUp(self):
     self.parser = None
     self.file_reader = FileReader(PYFileReader())
     self.dir_reader = DirectoryReader()
     self.current_dir = getcwd() + '/tests'
class TestJsParser(TestCase):

    def setUp(self):
        self.parser = None
        self.file_reader = FileReader(PYFileReader())
        self.dir_reader = DirectoryReader()
        self.current_dir = getcwd() + '/tests'

    def test_basic_class(self):
        """Tests to see whether it can correctly read a py file with a
        basic class."""

        expected_class = {
            'attributes': ['attribute1', 'attribute2', 'attribute3'],
            'methods': ['__init__'],
            'name': 'TestClass'}
        file_dir = str.format("{}/testing_files/py_files/test_file_1.py",
                              self.current_dir).replace("\\", "/")
        py_file = self.file_reader.get_file_contents(file_dir)
        parser_director = ParserDirector(PYParserBuilder())
        self.parser = parser_director.make_parser(py_file)
        actual_class = self.parser.get_classes()[0]
        self.assertEqual(expected_class, actual_class)

    def test_multiple_classes(self):
        """Tests to see whether it can correctly read a py file with multiple
        classes."""
        expected_classes = [{
            'attributes': ['attribute1', 'attribute2', 'attribute3'],
            'methods': ['__init__'],
            'name': 'TestClass1'},
            {
            'attributes': ['attribute1', 'attribute2', 'attribute3'],
            'methods': ['__init__'],
            'name': 'TestClass2'},
            {
            'attributes': ['attribute1', 'attribute2', 'attribute3'],
            'methods': ['__init__'],
            'name': 'TestClass3'}]
        file_dir = str.format("{}/testing_files/py_files/test_file_2.py",
                              self.current_dir).replace("\\", "/")
        js_file = self.file_reader.get_file_contents(file_dir)
        parser_director = ParserDirector(PYParserBuilder())
        self.parser = parser_director.make_parser(js_file)
        actual_classes = self.parser.get_classes()
        self.assertEqual(expected_classes, actual_classes)

    def test_no_classes(self):
        """Tests to see whether it returns an empty list when there are
        no classes."""
        expected_classes = []
        file_dir = str.format("{}/testing_files/test_file_empty.py",
                              self.current_dir).replace("\\", "/")
        js_file = self.file_reader.get_file_contents(file_dir)
        parser_director = ParserDirector(PYParserBuilder())
        self.parser = parser_director.make_parser(js_file)
        actual_classes = self.parser.get_classes()
        self.assertEqual(expected_classes, actual_classes)

    def test_entire_class(self):
        """Tests to see whether it returns an empty list when the provided
        file is not compatible."""
        expected_classes = []
        file_dir = str.format("{}/testing_files/test_file_bad.js",
                              self.current_dir).replace("\\", "/")
        js_file = self.file_reader.get_file_contents(file_dir)
        parser_director = ParserDirector(PYParserBuilder())
        self.parser = parser_director.make_parser(js_file)
        actual_classes = self.parser.get_classes()
        self.assertEqual(expected_classes, actual_classes)
Example #5
0
class TestJsParser(TestCase):
    def setUp(self):
        self.parser = None
        self.file_reader = FileReader(JSFileReader())
        self.dir_reader = DirectoryReader()
        self.current_dir = getcwd() + "/tests"

    def test_basic_class(self):
        """Tests to see whether it can correctly read a js file with a
        basic class."""
        expected_class = {
            'name': 'TestClass',
            'attributes': ['name', 'allMySports'],
            'methods': ['constructor', 'testFunction']
        }
        file_dir = str.format("{}/testing_files/test_file_1.js",
                              self.current_dir).replace("\\", "/")
        js_file = self.file_reader.get_file_contents(file_dir)
        parser_director = ParserDirector(JSParserBuilder())
        self.parser = parser_director.make_parser(js_file)
        actual_class = self.parser.get_classes()[0]
        self.assertEqual(expected_class, actual_class)

    def test_class_no_methods_attributes(self):
        """Tests to see whether it can correctly read a js file with a class
        with no methods or attributes"""
        expected_class = {'name': 'TestClass', 'attributes': [], 'methods': []}
        file_dir = str.format("{}/testing_files/test_file_3.js",
                              self.current_dir).replace("\\", "/")
        js_file = self.file_reader.get_file_contents(file_dir)
        parser_director = ParserDirector(JSParserBuilder())
        self.parser = parser_director.make_parser(js_file)
        actual_class = self.parser.get_classes()[0]
        self.assertEqual(expected_class, actual_class)

    def test_class_no_methods(self):
        """Tests to see whether it can correctly read a js file with an empty
        constructor."""
        expected_class = {'name': 'TestClass', 'attributes': [], 'methods': []}
        file_dir = str.format("{}/testing_files/test_file_6.js",
                              self.current_dir).replace("\\", "/")
        js_file = self.file_reader.get_file_contents(file_dir)
        parser_director = ParserDirector(JSParserBuilder())
        self.parser = parser_director.make_parser(js_file)
        actual_class = self.parser.get_classes()[0]
        self.assertEqual(expected_class, actual_class)

    def test_multiple_classes(self):
        """Tests to see whether it can correctly read a js file with multiple
        classes."""
        expected_classes = [{
            'name': 'TestClass1',
            'attributes': ['attribute1', 'attribute2'],
            'methods': ['constructor']
        }, {
            'name': 'TestClass2',
            'attributes': ['attribute1', 'attribute2'],
            'methods': ['constructor']
        }, {
            'name': 'TestClass3',
            'attributes': ['attribute1', 'attribute2'],
            'methods': ['constructor']
        }]
        file_dir = str.format("{}/testing_files/test_file_2.js",
                              self.current_dir).replace("\\", "/")
        js_file = self.file_reader.get_file_contents(file_dir)
        parser_director = ParserDirector(JSParserBuilder())
        self.parser = parser_director.make_parser(js_file)
        actual_classes = self.parser.get_classes()
        self.assertEqual(expected_classes, actual_classes)

    def test_no_classes(self):
        """Tests to see whether it returns an empty list when there are
        no classes."""
        expected_classes = []
        file_dir = str.format("{}/testing_files/test_file_empty.js",
                              self.current_dir).replace("\\", "/")
        js_file = self.file_reader.get_file_contents(file_dir)
        parser_director = ParserDirector(JSParserBuilder())
        self.parser = parser_director.make_parser(js_file)
        actual_classes = self.parser.get_classes()
        self.assertEqual(expected_classes, actual_classes)

    def test_entire_class(self):
        """Tests to see whether it returns an empty list when the provided
        file is not compatible."""
        expected_classes = []
        file_dir = str.format("{}/testing_files/test_file_bad.js",
                              self.current_dir).replace("\\", "/")
        js_file = self.file_reader.get_file_contents(file_dir)
        parser_director = ParserDirector(JSParserBuilder())
        self.parser = parser_director.make_parser(js_file)
        actual_classes = self.parser.get_classes()
        self.assertEqual(expected_classes, actual_classes)

    def test_entire_dir(self):
        """Tests to see whether it can read an entire directory and extract
        all the classes."""
        expected_class_count = 4
        dir = str.format("{}/testing_files/test_four_classes",
                         self.current_dir).replace("\\", "/")
        self.dir_reader.set_directory(dir)
        for aDir in self.dir_reader.get_file_dirs():
            js_file = self.file_reader.get_file_contents(aDir)
            parser_director = ParserDirector(JSParserBuilder())
            self.parser = parser_director.make_parser(js_file)
        actual_class_count = len(self.parser.get_classes())
        self.assertEqual(expected_class_count, actual_class_count)