Beispiel #1
0
    def test_dividemultiply(self):
        with io.StringIO() as buf, redirect_stdout(buf):
            with FileManager("Testing/Final_Test_Cases/dividemultiply.txt"
                             ) as file_manager:
                tokeniser = Tokeniser(file_manager)
                analysed_tokens = []
                analysed_tokens.append(Token("START_OF_FILE", "", 0))
                while (analysed_tokens[-1].type is not "END_OF_FILE"):
                    analysed_tokens.append(tokeniser.get_token())

                analysed_tokens = analysed_tokens[1:]

                parsed = ProgramNode(analysed_tokens[::-1], file_manager)
                parsed.execute()

                solution = """1.25
20.0"""

                for line_sol, line_test in zip(solution.rstrip('\r'),
                                               buf.getvalue().rstrip('\r')):
                    self.compare_output(line_sol, line_test)
    def test2(self):
        with FileManager("Testing/Lexer_Test_Cases/test2.txt") as file_manager:
            tokens = self.loop_test(file_manager)

            expected = [("PLAYERS", "PLAYERS"), ("NATURAL_NUMBER", "2"),
                        ("GAME", "GAME"), ("COLON", ":"),
                        ("IDENTIFIER", "tokens"), ("ASSIGNMENT", "="),
                        ("NATURAL_NUMBER", "10"), ("SEMICOLON", ";"),
                        ("IDENTIFIER", "finished"), ("ASSIGNMENT", "="),
                        ("REAL_NUMBER", "0"), ("SEMICOLON", ";"),
                        ("INDIVIDUAL", "INDIVIDUAL"), ("COLON", ":"),
                        ("IDENTIFIER", "score"), ("ASSIGNMENT", "="),
                        ("REAL_NUMBER", "0"), ("SEMICOLON", ";"),
                        ("FUNCTIONS", "FUNCTIONS"), ("COLON", ":"),
                        ("FUNCTION_DEF", "def"), ("IDENTIFIER", "getScore"),
                        ("LEFT_ROUND", "("), ("IDENTIFIER", "playerscore"),
                        ("COMMA", ","), ("IDENTIFIER", "gametokens"),
                        ("RIGHT_ROUND", ")"), ("LEFT_CURLY", "{"),
                        ("VARIABLE_ASSIGN", "var"), ("IDENTIFIER", "retScore"),
                        ("SEMICOLON", ";"), ("IDENTIFIER", "retScore"),
                        ("ASSIGNMENT", "="), ("NATURAL_NUMBER", "2"),
                        ("MULTIPLICATION", "*"), ("IDENTIFIER", "playerscore"),
                        ("MINUS", "-"), ("IDENTIFIER", "gametokens"),
                        ("SEMICOLON", ";"), ("RETURN", "return"),
                        ("IDENTIFIER", "retScore"), ("SEMICOLON", ";"),
                        ("RIGHT_CURLY", "}"), ("RUN", "RUN"), ("COLON", ":"),
                        ("FOR_EACH_PLAYER", "forEachPlayer"),
                        ("LEFT_ROUND", "("), ("NATURAL_NUMBER", "1"),
                        ("RIGHT_ROUND", ")"), ("LEFT_CURLY", "{"),
                        ("IDENTIFIER", "player.won"), ("ASSIGNMENT", "="),
                        ("NATURAL_NUMBER", "1"), ("SEMICOLON", ";"),
                        ("BREAK", "break"), ("SEMICOLON", ";"),
                        ("RIGHT_CURLY", "}"), ("WIN", "WIN"), ("COLON", ":"),
                        ("LEFT_SQUARE", "["), ("IDENTIFIER", "won"),
                        ("EQUAL_TO", "=="), ("NATURAL_NUMBER", "1"),
                        ("RIGHT_SQUARE", "]"), ("END_OF_FILE", "")]

            self.compare_tokens(expected, tokens)
 def test_realnumber_too_long(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(error_manager.ErrorManager,
                           Tokeniser(fm).get_token)
         self.assertTrue(mock_error.called)
 def assert_equal_tokens(self, expected):
     with FileManager("") as fm:
         for _type, _value in expected:
             token = Tokeniser(fm).get_token()
             self.assertEqual(_type, token.type)
             self.assertEqual(_value, token.value)
 def test_unknown_char(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(error_manager.ErrorManager,
                           Tokeniser(fm).get_token)
         self.assertTrue(mock_error.called)
 def test_number_with_comma_but_no_decimal(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(error_manager.ErrorManager,
                           lambda: loop_test(fm))
         self.assertTrue(mock_error.called)
 def test_number_with_2_separators(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(error_manager.ErrorManager,
                           lambda: loop_test(fm))
         self.assertTrue(mock_error.called)
 def test_number_starting_with_0(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(error_manager.ErrorManager,
                           Tokeniser(fm).get_token)
         self.assertTrue(mock_error.called)
Beispiel #9
0
 def test_function_no_body(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(Parser.error_manager.ErrorManager, lambda: run_program(fm))
Beispiel #10
0
 def test_rolled_inited(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(Parser.error_manager.ErrorManager, lambda: run_program(fm))
Beispiel #11
0
 def test_assign_chosen_variable_not_defined(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(Parser.error_manager.ErrorManager, lambda: run_program(fm))
Beispiel #12
0
 def test_print_wrong_char(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(Parser.error_manager.ErrorManager, lambda: run_program(fm))
Beispiel #13
0
 def test_math_with_string2(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(Parser.error_manager.ErrorManager, lambda: run_program(fm))
Beispiel #14
0
 def test_too_many_playerheaders(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(Parser.error_manager.ErrorManager, lambda: run_program(fm))
Beispiel #15
0
 def test_access_existing_without_value(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(Parser.error_manager.ErrorManager, lambda: run_program(fm))
Beispiel #16
0
 def test_minus_without_number(self, mock_error, mock_open):
     with FileManager("") as fm:
         self.assertRaises(Parser.error_manager.ErrorManager, lambda: run_program(fm))