def test_direct_operator_use(self): x, y, z = 423, 324, 321 res = "(((423 + 324) + (324 + 321)) - (423 + 324)) + (324 + 321)" f1 = FieldExpression(x, y, ops.add) f2 = FieldExpression(y, z, ops.add) f3 = ((f1 + f2) - f1) + f2 self.assertTrue(f3.eval() == 1290) self.assertTrue(f3.to_string() == res)
def test_partly_evaluation(self): x, y = 123, "varname" f1 = FieldExpression(x, y, ops.add) f2 = f1.eval() self.assertTrue(isinstance(f1.eval(), FieldExpression)) self.assertTrue(f1.to_string() == "123 + varname") f1.context["varname"] = 313 self.assertTrue(isinstance(f1.eval(), int)) self.assertTrue(f1.eval() == 436)
def test_multi_level(self): x, y, z = 423, 324, 321 f1 = FieldExpression(x, y, ops.add) f2 = FieldExpression(y, z, ops.add) f3 = FieldExpression(f1, f2, ops.sub) self.assertTrue(f3.eval() == ops.sub(ops.add(x, y), ops.add(y, z))) self.assertTrue(f3.to_string() == "(423 + 324) - (324 + 321)")
def test_simple_exp_add(self): x, y = 1, 2 for o in [ops.add, ops.sub, ops.and_, ops.or_, ops.eq, ops.lt, ops.le]: f1 = FieldExpression(x, y, o) self.assertTrue(f1.eval() == o(x, y), "failed: {}, ref: {}". \ format(o.__name__, o(x, y)))
def __mul__(self, other): return FieldExpression(self, other, operator.mul)
def __div__(self, other): return FieldExpression(self, other, operator.div)
def __add__(self, other): return FieldExpression(self, other, operator.add)
def __sub__(self, other): return FieldExpression(self, other, operator.sub)
def __or__(self, other): return FieldExpression(self, other, operator.or_)
def __invert__(self): return FieldExpression(self, None, operator.inv)
def __len__(self): return FieldExpression(self, None, len)
def __contains__(self, other): return FieldExpression(self, other, operator.contains)
def __ge__(self, other): return FieldExpression(self, other, operator.ge)
def __eq__(self, other): return FieldExpression(self, other, operator.eq)
def __lt__(self, other): return FieldExpression(self, other, operator.lt)