Exemplo n.º 1
0
class InterpreterTest(unittest.TestCase):

    def setUp(self):
        self.interpreter = Interpreter()

    def test_basic(self):
        """Check if interpreting with the initial version works."""

        self.check_result(BASIC_CODE, BASIC_RESULT_SHOULD)

    def test_comparison_operators(self):
        """
        Check if the following comparison operators work:
            - <
            - >
            - ==
            - <=
            - >=
            - !=

        This test fails until you add support for this operators in the
        parser and the evaluator.
        """
        self.check_result(COMPARISON_OPERATORS_CODE, COMPARISON_RESULT_SHOULD)

    def test_negative_integers(self):
        """
        Check if code containing negative integers work., e.g. '1 + -2'
        This test fails until you add support for negative integers in the
        parser, javali_ast and the evaluator.
        """
        self.check_result(NEGATIVE_INTEGERS_CODE, NEGATIVES_RESULT_SHOULD)

    def test_if_stmt(self):
        """
        Check if if statements of the following form work:

        if (1 < 2) {
            put(1)
        } else {
            put(2)
        }

        The else block is optional:

        if (1 < 2) {
            put(1)
        }

        This test fails until you add support for if-statements in the
        parser, javali_ast and the evaluator.
        """
        self.check_result(IF_STMT_CODE, IF_STMT_RESULT_SHOULD)

    def check_result(self, code, result_should):
        """
        Helper for interpreting javali 'code' and comparing the result of the
        interpreter to 'result_should'.
        """
        self.interpreter.interprete_code(code)
        self.assertEqual(self.interpreter.get_result(), result_should)