diff --git a/info4/kapitel-8/Interpreter/test_loop_interpreter.py b/info4/kapitel-8/Interpreter/test_loop_interpreter.py index f8552a8db0e7e79e17539fa86c4b981f742b8449..1cb87c4b3ac9ba649c47e67ecab0ced0fdb8635a 100644 --- a/info4/kapitel-8/Interpreter/test_loop_interpreter.py +++ b/info4/kapitel-8/Interpreter/test_loop_interpreter.py @@ -1,6 +1,17 @@ from interpreter import interpret import unittest +from unittest import mock +def input_exit(prompt): + return "EXIT" + +def input_continue(prompt): + return "" + +def init_without_tokens(self, regex_to_token, program): + self.regex_to_token = {} + self.program = program + self.current_position = 0 class InterpreterTest(unittest.TestCase): def test_assignment_default_zero(self): @@ -207,3 +218,17 @@ class InterpreterTest(unittest.TestCase): self.assertEqual(interpret('''x2:=3; x0:=x2+2'''), 5) self.assertEqual(interpret('x1:=x1-2;\n x0:=x1+2'), 2) + + @mock.patch('interpreter.input', side_effect=input_exit) + def test_break_exit(self, input): + self.assertEqual(interpret('x1:=2; BREAK x0:=2'), -1) + self.assertEqual(interpret('LOOP x1 DO BREAK x2:= 2 END'), -1) + + @mock.patch('interpreter.input', side_effect=input_continue) + def test_break_continue(self, input): + self.assertEqual(interpret('x1:=2; LOOP x1 DO x0:=x0+2 BREAK END'), 4) + + @mock.patch('lexer.Lexer.__init__', init_without_tokens) + def test_unknown_tokens(self): + with self.assertRaises(SyntaxError): + interpret('BLIBLABLUB') \ No newline at end of file