Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_nested_and_expression(self):
a = ConstantExpression(True)
b = ConstantExpression(False)
c = ConstantExpression(None)
node_type = BinaryExpression.And
expr = BinaryExpression(node_type, BinaryExpression(node_type, a, b), c)
self.assertEquals(expr.node_type, node_type)
self.failUnless(isinstance(expr.lhs, BinaryExpression), "The left-hand side of the binary expression should be a binary expression as well, but is %s" % expr.lhs.__class__)
self.assertEquals(expr.lhs.node_type, node_type)
self.assertEquals(expr.lhs.lhs, a)
self.assertEquals(expr.lhs.rhs, b)
self.assertEquals(expr.rhs, c)
def test_binary_expression_only_accepts_expressions_for_arguments(self):
a = 10
a_expr = ConstantExpression(10)
b = "20"
b_expr = ConstantExpression(20)
node_type = BinaryExpression.Add
self.assertRaisesEx(ValueError, BinaryExpression, node_type, a, b_expr, exc_pattern=re.compile("Lhs must be an expression \(an instance of a class that inherits from pynq.Expression\)"))
self.assertRaisesEx(ValueError, BinaryExpression, node_type, a_expr, b, exc_pattern=re.compile("Rhs must be an expression \(an instance of a class that inherits from pynq.Expression\)"))
self.assertRaisesEx(ValueError, BinaryExpression, None, a_expr, b_expr, exc_pattern=re.compile("The BinaryExpression node type is required"))
def test_nested_multiplication_expression_representation(self):
a = ConstantExpression(10)
b = ConstantExpression(20)
c = ConstantExpression(30)
node_type = BinaryExpression.Multiply
expr = BinaryExpression(node_type, BinaryExpression(node_type, a, b), c)
self.assertEquals("((10 * 20) * 30)", str(expr))
def test_nested_division_expression_representation(self):
a = ConstantExpression(10)
b = ConstantExpression(20)
c = ConstantExpression(30)
node_type = BinaryExpression.Divide
expr = BinaryExpression(node_type, BinaryExpression(node_type, a, b), c)
self.assertEquals("((10 / 20) / 30)", str(expr))
def test_nested_division_expression(self):
a = ConstantExpression(10)
b = ConstantExpression(20)
c = ConstantExpression(30)
node_type = BinaryExpression.Divide
expr = BinaryExpression(node_type, BinaryExpression(node_type, a, b), c)
self.assertEquals(expr.node_type, node_type)
self.failUnless(isinstance(expr.lhs, BinaryExpression), "The left-hand side of the binary expression should be a binary expression as well, but is %s" % expr.lhs.__class__)
self.assertEquals(expr.lhs.node_type, node_type)
self.assertEquals(expr.lhs.lhs, a)
self.assertEquals(expr.lhs.rhs, b)
self.assertEquals(expr.rhs, c)
def test_nested_power_expression(self):
a = ConstantExpression(10)
b = ConstantExpression(20)
c = ConstantExpression(30)
node_type = BinaryExpression.Power
expr = BinaryExpression(node_type, BinaryExpression(node_type, a, b), c)
self.assertEquals(expr.node_type, node_type)
self.failUnless(isinstance(expr.lhs, BinaryExpression), "The left-hand side of the binary expression should be a binary expression as well, but is %s" % expr.lhs.__class__)
self.assertEquals(expr.lhs.node_type, node_type)
self.assertEquals(expr.lhs.lhs, a)
self.assertEquals(expr.lhs.rhs, b)
self.assertEquals(expr.rhs, c)
def test_nested_and_expression_representation(self):
a = ConstantExpression(True)
b = ConstantExpression(False)
c = ConstantExpression(None)
node_type = BinaryExpression.And
expr = BinaryExpression(node_type, BinaryExpression(node_type, a, b), c)
self.assertEquals("((True and False) and None)", str(expr))
def test_nested_or_expression_representation(self):
a = ConstantExpression(True)
b = ConstantExpression(False)
c = ConstantExpression(None)
node_type = BinaryExpression.Or
expr = BinaryExpression(node_type, BinaryExpression(node_type, a, b), c)
self.assertEquals("((True or False) or None)", str(expr))
def led(self, left):
return BinaryExpression(BinaryExpression.Or, left, self.expression(self.lbp-1))
def led(self, left):
return BinaryExpression(BinaryExpression.LessThan, left, self.expression(self.lbp))