예제 #1
0
    def test_eq_and_hash(self):

        def op(x, y):
            return x == y                               # pragma: no cover

        spec1 = Specification("op(a, 1)", candidate_name='a')
        spec2 = Specification("op(a,1)", candidate_name='a')
        spec3 = Specification("a == 1")
        self.assertEqual(spec1, spec2)
        self.assertEqual(hash(spec1), hash(spec2))
        self.assertNotEqual(spec1, spec3)
        self.assertNotEqual(hash(spec1), hash(spec3))
        negspec1 = ~spec1
        negspec2 = ~spec2
        negspec3 = ~spec3
        self.assertEqual(negspec1, negspec2)
        self.assertEqual(hash(negspec1), hash(negspec2))
        self.assertNotEqual(negspec1, negspec3)
        self.assertNotEqual(hash(negspec1), hash(negspec3))
        self.assertNotEqual(spec1, negspec1)
        self.assertNotEqual(hash(spec1), hash(negspec1))
        cspec1 = spec1 & negspec2
        cspec2 = spec2 & negspec1
        cspec3 = spec1 & spec3
        self.assertEqual(cspec1, cspec2)
        self.assertEqual(hash(cspec1), hash(cspec2))
        self.assertNotEqual(cspec1, cspec3)
        self.assertNotEqual(hash(cspec1), hash(cspec3))
        self.assertNotEqual(spec1, cspec1)
        self.assertNotEqual(hash(spec1), hash(cspec1))
예제 #2
0
 def testCompositeSpec(self):
     a, b = 5, 7
     ab = a * b
     tObj = TestObj2(a, b)
     spec1 = Specification('t.b == b', candidate_name='t')
     spec2 = Specification('t.ab < ab', candidate_name='t')
     spec3 = Specification('1 <= t.a <= 7')
     cspec = spec1 & spec2 & spec3
     self.assertFalse(cspec.is_satisfied_by(tObj))
     cspec = spec1 | spec2 | spec3
     self.assertTrue(cspec.is_satisfied_by(tObj))
     cspec = spec1 ^ spec2 ^ spec3
     self.assertFalse(cspec.is_satisfied_by(tObj))
     cspec = spec1 & (spec2 & spec3)
     self.assertFalse(cspec.is_satisfied_by(tObj))
     cspec = (spec1 & spec2) | spec3
     self.assertTrue(cspec.is_satisfied_by(tObj))
     cspec = (spec1 | spec2) & ~spec3
     self.assertFalse(cspec.is_satisfied_by(tObj))
     cspec = (~spec1 | spec2) | ~spec3
     self.assertFalse(cspec.is_satisfied_by(tObj))
     cspec = (spec1 & ~spec2) & spec3
     self.assertTrue(cspec.is_satisfied_by(tObj))
     cspec = (spec1 & ~spec2) ^ spec3
     self.assertFalse(cspec.is_satisfied_by(tObj))
     cspec = (spec1 ^ spec2) & spec3
     self.assertTrue(cspec.is_satisfied_by(tObj))
     cspec = ~(spec1 ^ spec2) ^ spec3
     self.assertTrue(cspec.is_satisfied_by(tObj))
     cspec = ~(spec1 ^ spec2 ^ spec3)
     self.assertTrue(cspec.is_satisfied_by(tObj, ab=ab))
예제 #3
0
 def testCompositeSpec(self):
     spec1 = Specification('b == 2')
     spec2 = Specification('ab > 20')
     spec3 = Specification('2 < a <= 7')
     spec4 = Specification('ab not in range(-12, -7)')
     for op in (operator.and_, operator.or_, operator.xor):
         for spec1, spec2 in combinations((spec1, spec2, spec3, spec4), 2):
             spec = op(spec1, spec2)
             neg_spec = ~spec
             self.assertTrue(isinstance(neg_spec, Specification))
             self.assertEqual(spec, ~neg_spec)
예제 #4
0
 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))
예제 #5
0
 def testSpecFromExpression(self):
     src = "x==1"
     expr = ast.parse(src, mode='eval')
     spec = Specification(expr)
     self.assertIsInstance(spec._ast_expr, ast.Expression)
     self.assertEqual(spec._candidate_name, 'x')
     self.assertEqual(spec._context, {})
예제 #6
0
 def __load_specifications(self):
     """
     Loads Specifications from the configurations and stores them in the
     internal storage.
     """
     try:
         params = rospy.get_param(self.__namespace)
         if isinstance(params, dict):
             specifications = []
             for x in params.values():
                 for y in x:
                     specifications.append(y)
         else:
             specifications = params
         for o in specifications:
             for seuid in o.keys():
                 if SEUID().is_valid(seuid):
                     spec = Specification()
                     spec.seuid = seuid
                     for k in o[seuid].keys():
                         spec.add_tuple((k, o[seuid][k]))
                     self.__specifications[seuid] = spec
                 else:
                     rospy.logdebug("[SpecificationHandler][__load_specifications] %s is not a valid seuid." % seuid)
     except KeyError:
         pass
     rospy.loginfo("[SpecificationHandler] Loaded %s parameters." % str(len(self.__specifications.keys())))
예제 #7
0
 def testAdditionalContext(self):
     year = 1999
     spec = Specification('deadline < date(year, 6, 30)',
                          candidate_name='deadline')
     dt = date(year, 5, 1)
     self.assertTrue(spec(dt))
     self.assertTrue(spec(dt, year=2000))
     self.assertFalse(spec(dt, year=1997))
예제 #8
0
 def testSpecFromStr(self):
     expr = "b == 2"
     spec = Specification(expr)
     self.assertIsInstance(spec._ast_expr, ast.Expression)
     self.assertEqual(spec._candidate_name, 'b')
     self.assertEqual(spec._context, {})
     # nested attribute
     expr = "b.x.y==2"
     spec = Specification(expr)
     self.assertIsInstance(spec._ast_expr, ast.Expression)
     self.assertEqual(spec._candidate_name, 'b')
     self.assertEqual(spec._context, {})
     # nested attribute and context variables
     y = object()
     z = object()
     expr = "y.a > x.b >= z.c"
     spec = Specification(expr, candidate_name='x')
     self.assertIsInstance(spec._ast_expr, ast.Expression)
     self.assertEqual(spec._candidate_name, 'x')
     self.assertEqual(spec._context, {'y': y, 'z': z})
예제 #9
0
 def testReprAndStr(self):
     op = lambda x, y: x == y                        # noqa
     spec = Specification('op(o.a,1)', candidate_name='o')
     self.assertEqual(repr(spec),
                      "Specification('op(o.a, 1)', candidate_name='o')")
     self.assertEqual(str(spec), '<o: op(o.a, 1)>')
     self.assertEqual(repr(~spec),
                      "Specification('not op(o.a, 1)', "
                      "candidate_name='o')")
     self.assertEqual(str(~spec), '<o: not op(o.a, 1)>')
     spec = Specification('x.a == 1')
     self.assertEqual(repr(spec), "Specification('x.a == 1')")
     self.assertEqual(str(spec), '<x: x.a == 1>')
     ispec = Specification('0 <  a.b <=6')
     self.assertEqual(repr(ispec), "Specification('0 < a.b <= 6')")
     self.assertEqual(str(ispec), '<a: 0 < a.b <= 6>')
     cspec = ispec & spec
     self.assertEqual(repr(cspec),
                      "Specification('0 < a.b <= 6 and a.a == 1')")
     self.assertEqual(str(cspec), '<a: 0 < a.b <= 6 and a.a == 1>')
예제 #10
0
 def __init__(self,
              centralised_vcs_server,
              target_test_coverage_per_feature=1.0,
              tests_per_chunk_ratio=1,
              target_dependencies_per_feature=0
              ):
     change_management = ChangeManagement(centralised_vcs_server)
     self.specification = Specification(change_management)
     self.testing = Testing(change_management, target_test_coverage_per_feature, tests_per_chunk_ratio)
     self.implementation = Implementation(change_management)
     self.debugging = Debugging(change_management)
     self.refactoring = Refactoring(change_management, target_dependencies_per_feature)
예제 #11
0
 def testSimpleSpec(self):
     for op1, op2 in [('==', '!='),
                      ('<', '>='),
                      ('>', '<='),
                      ('is', 'is not')]:
         spec1 = Specification(f'a {op1} 1')
         spec2 = Specification(f'a {op2} 1')
         neg_spec1 = ~spec1
         neg_spec2 = ~spec2
         self.assertTrue(isinstance(neg_spec1, Specification))
         self.assertTrue(isinstance(neg_spec2, Specification))
         self.assertEqual(spec1, neg_spec2)
         self.assertEqual(spec2, neg_spec1)
         self.assertEqual(spec1, ~neg_spec1)
         self.assertEqual(spec2, ~neg_spec2)
     # non-predefined operator:
     op = lambda x, y: x == y                        # noqa
     spec = Specification(f'op(a, 1)', candidate_name='a')
     neg_spec = ~spec
     self.assertTrue(isinstance(neg_spec, Specification))
     self.assertEqual(spec, ~neg_spec)
예제 #12
0
 def test_find(self):
     repo = self.repo(Person)
     p1 = Person('Hans', 'Berlinger', date(1982, 12, 3))
     self.assertIsNone(repo.add(p1))
     p2 = Person('Bert', 'Berlinger', date(1962, 2, 14))
     self.assertIsNone(repo.add(p2))
     p3 = Prospect('Hans', 'Berliner', date(1982, 12, 3))
     self.assertIsNone(repo.add(p3))
     spec = Specification('x.date_of_birth >= date(1980, 1, 1)',
                          candidate_name='x')
     res = list(repo.find(spec))
     self.assertEqual(len(res), 2)
     self.assertIn(p1, res)
     self.assertIn(p3, res)
예제 #13
0
 def setUp(self):
     self.x = x = object()                        # noqa
     self.y = y = 5                               # noqa
     self.z = z = 13                              # noqa
     self.spec1 = Specification("y == 2")
     self.spec2 = Specification("ab>20")
     self.spec3 = Specification("x==y", candidate_name='x')
     self.spec4 = Specification("x.ab>=z", candidate_name='x')
     self.spec5 = Specification("x.ab>=z", candidate_name='z')
     self.spec6 = Specification("x.ab>=z", candidate_name='x',
                                namespace = {'z': 7})
예제 #14
0
 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))
예제 #15
0
 def setUp(self):
     from decimal import Decimal                     # noqa
     self.spec = Specification("x == Decimal('5.4')",
                               candidate_name='x')
예제 #16
0
 def testNameConflict(self):
     spec = Specification('dt >= date(2020, 1, 1)', candidate_name='dt')
     # conflict with given keyword
     candidate = date(2021, 8, 1)
     self.assertRaises(ValueError, spec, candidate, date=date,
                       dt=date.today())