def testNestedproperty(self): a, b = 5, 7 ab = a * b tObj = TestObj1(a=3, b=TestObj2(a, b)) spec = Specification(f't.b.ab == {ab}') self.assertTrue(spec.is_satisfied_by(tObj)) spec = ~Specification(f't.b.ab != {ab}') self.assertTrue(spec.is_satisfied_by(tObj)) ispec = Specification('1 <= t.b.a <= 7') self.assertTrue(ispec.is_satisfied_by(tObj)) cspec = spec & ispec self.assertTrue(cspec.is_satisfied_by(tObj))
class TestIssue1(unittest.TestCase): def setUp(self): from decimal import Decimal # noqa self.spec = Specification("x == Decimal('5.4')", candidate_name='x') def testIsSatisfied(self): self.assertFalse(self.spec.is_satisfied_by(5))
def testSimpleSpec(self): val = 5 tObj = TestObj1(b=val) for op1, op2 in [(operator.eq, operator.ne), (operator.lt, operator.ge), (operator.gt, operator.le), (operator.is_, operator.is_not)]: spec1 = Specification(f't.b {op2sym[op1]} {val}') spec2 = Specification(f't.b {op2sym[op2]} {val}') self.assertEqual(spec1.is_satisfied_by(tObj), op1(tObj.b, val)) self.assertFalse(spec1.is_satisfied_by(tObj) and spec2.is_satisfied_by(tObj)) spec1 = Specification(f't.b {op2sym[op1]} {-val}') spec2 = Specification(f't.b {op2sym[op2]} {-val}') self.assertEqual(spec1.is_satisfied_by(tObj), op1(tObj.b, -val)) self.assertFalse(spec1.is_satisfied_by(tObj) and spec2.is_satisfied_by(tObj)) # compare instance of subclass tObj = TestObj2(b=val) spec = Specification(f't.b == {val}') self.assertTrue(spec.is_satisfied_by(tObj)) spec = Specification(f't.b < {val}') self.assertFalse(spec.is_satisfied_by(tObj))