示例#1
0
    def test_add_number_and_string(self):
        binary = Binary(Literal(1), new_token(t.PLUS), Literal("kitty"))

        with self.assertRaises(InterpreterException) as context:
            result = self.interpreter.visit_binary_expression(binary)

            self.assertEqual(context.expression, binary)
示例#2
0
    def test_equality_bools(self):
        binary = Binary(Literal(True), new_token(t.EQUAL_EQUAL),
                        Literal(False))

        result = self.interpreter.visit_binary_expression(binary)

        self.assertEqual(result, False)
示例#3
0
    def test_double_equality(self):
        # 1 == 1 == 1
        comparison = Binary(
            Binary(Literal(1), new_token(t.EQUAL_EQUAL), Literal(1)),
            new_token(t.EQUAL_EQUAL), Literal(1))

        result = self.interpreter.visit_binary_expression(comparison)

        self.assertEqual(result, True)
示例#4
0
    def test_grouping_should_equal_inner_value(self):
        inner_expression = Literal(1)
        grouping = Grouping(inner_expression)

        result = self.interpreter.visit_grouping_expression(grouping)

        self.assertEqual(result, 1)
示例#5
0
    def test_add_numbers(self):
        binary = Binary(Literal(1), new_token(t.PLUS), Literal(1))

        result = self.interpreter.visit_binary_expression(binary)

        self.assertEqual(result, 2)
示例#6
0
    def test_multiply(self):
        binary = Binary(Literal(2), new_token(t.STAR), Literal(4))

        result = self.interpreter.visit_binary_expression(binary)

        self.assertEqual(result, 8)
示例#7
0
    def test_equality_numbers(self):
        binary = Binary(Literal(2), new_token(t.EQUAL_EQUAL), Literal(2))

        result = self.interpreter.visit_binary_expression(binary)

        self.assertEqual(result, True)
示例#8
0
    def test_unary_minus(self):
        unary = Unary(new_token(t.MINUS), Literal(2))

        result = self.interpreter.visit_unary_expression(unary)

        self.assertEqual(result, -2)
示例#9
0
    def test_unary_not(self):
        unary = Unary(new_token(t.BANG), Literal(False))

        result = self.interpreter.visit_unary_expression(unary)

        self.assertEqual(result, True)
示例#10
0
    def test_literal_should_equal_value(self):
        literal = Literal(2)

        result = self.interpreter.visit_literal_expression(literal)

        self.assertEqual(result, 2)
示例#11
0
    def test_comparison(self):
        comparison = Binary(Literal(1), new_token(t.LESS), Literal(2))

        result = self.interpreter.visit_binary_expression(comparison)

        self.assertEqual(result, True)
示例#12
0
    def test_add_strings(self):
        binary = Binary(Literal("hello "), new_token(t.PLUS), Literal("kitty"))

        result = self.interpreter.visit_binary_expression(binary)

        self.assertEqual(result, "hello kitty")