Exemplo n.º 1
0
 def testRangeNode(self):
     from peak.rules.indexing import RangeIndex, to_bits
     from peak.rules.predicates import range_node
     from peak.rules.criteria import Range, Value, Min, Max
     ind = RangeIndex(self, 'expr')
     ind.add_case(0, Value(19))
     ind.add_case(1, Value(23))
     ind.add_case(2, Value(23, False))
     ind.add_case(3, Range(lo=(57, 1)))
     ind.add_case(4, Range(lo=(57, -1)))
     dontcare, seeds = ind.seed_bits(to_bits(range(6)))
     exact, ranges = self.build(range_node, dontcare, seeds)
     self.assertEqual(exact, {
         19: to_bits([0, 2, 5]),
         23: to_bits([1, 5]),
         57: to_bits([2, 4, 5])
     })
     self.assertEqual(ranges, [((Min, 57), to_bits([2, 5])),
                               ((57, Max), to_bits([2, 3, 4, 5]))])
Exemplo n.º 2
0
    def testClassBodyRules(self):
        from peak.rules.core import Local, Rule
        from peak.rules.criteria import Signature, Test, Class, Value
        from peak.rules.predicates import IsInstance, Truth
        from peak.util.decorators import enclosing_frame
        im_func = lambda x: getattr(x, 'im_func', x)

        abstract()

        def f1(a):
            pass

        abstract()

        def f2(b):
            pass

        # This is to verify that the rules have sequence numbers by definition
        # order, not reverse definition order, inside a class.
        num = Rule(None).sequence

        class T:
            f1 = enclosing_frame(level=2).f_locals['f1']  # ugh
            when(f1)

            def f1_(a):
                pass

            f2 = enclosing_frame(level=2).f_locals['f2']
            when(f2, 'b')

            def f2_(b):
                pass

        self.assertEqual(list(rules_for(f1)),
                         [Rule(im_func(T.f1_), (T, ), Method, num + 1)])
        self.assertEqual(list(rules_for(f2)), [
            Rule(
                im_func(T.f2_),
                Signature([
                    Test(IsInstance(Local('b')), Class(T)),
                    Test(Truth(Local('b')), Value(True))
                ]), Method, num + 2)
        ])
Exemplo n.º 3
0
    def testParseInequalities(self):
        from peak.rules.predicates import CriteriaBuilder, Comparison, Truth
        from peak.util.assembler import Compare, Local
        from peak.rules.criteria import Inequality, Test, Value
        from peak.rules.ast_builder import parse_expr
        builder = CriteriaBuilder(dict(x=Local('x'), y=Local('y')), locals(),
                                  globals(), __builtins__)
        pe = builder.parse

        x_cmp_y = lambda op, t=True: Test(
            Truth(Compare(Local('x'), ((op, Local('y')), ))), Value(True, t))
        x, y = Comparison(Local('x')), Comparison(Local('y'))

        for op, mirror_op, not_op, stdop, not_stdop in [
            ('>', '<', '<=', '>', '<='),
            ('<', '>', '>=', '<', '>='),
            ('==', '==', '!=', '==', '!='),
            ('<>', '<>', '==', '!=', '=='),
        ]:
            if op == '<>' and sys.version >= "3":
                continue
            fwd_sig = Test(x, Inequality(op, 1))
            self.assertEqual(pe('x %s 1' % op), fwd_sig)
            self.assertEqual(pe('1 %s x' % mirror_op), fwd_sig)

            rev_sig = Test(x, Inequality(mirror_op, 1))
            self.assertEqual(pe('x %s 1' % mirror_op), rev_sig)
            self.assertEqual(pe('1 %s x' % op), rev_sig)

            not_sig = Test(x, Inequality(not_op, 1))
            self.assertEqual(pe('not x %s 1' % op), not_sig)
            self.assertEqual(pe('not x %s 1' % not_op), fwd_sig)

            self.assertEqual(pe('x %s y' % op), x_cmp_y(stdop))
            self.assertEqual(pe('x %s y' % not_op), x_cmp_y(not_stdop))

            self.assertEqual(pe('not x %s y' % op), x_cmp_y(stdop, False))
            self.assertEqual(pe('not x %s y' % not_op),
                             x_cmp_y(not_stdop, False))
Exemplo n.º 4
0
    def testIndexedEngine(self):
        from peak.rules.predicates import IndexedEngine, Comparison
        from peak.rules.criteria import Range, Value, Test, Signature
        from peak.util.assembler import Local
        from peak.util.extremes import Min, Max
        abstract()

        def classify(age):
            pass

        Dispatching(classify).create_engine(IndexedEngine)

        def setup(r, f):
            when(classify, Signature([Test(Comparison(Local('age')), r)]))(f)

        setup(Range(hi=(2, -1)), lambda age: "infant")
        setup(Range(hi=(13, -1)), lambda age: "preteen")
        setup(Range(hi=(5, -1)), lambda age: "preschooler")
        setup(Range(hi=(20, -1)), lambda age: "teenager")
        setup(Range(lo=(20, -1)), lambda age: "adult")
        setup(Range(lo=(55, -1)), lambda age: "senior")
        setup(Value(16), lambda age: "sweet sixteen")

        self.assertEqual(classify(0), "infant")
        self.assertEqual(classify(25), "adult")
        self.assertEqual(classify(17), "teenager")
        self.assertEqual(classify(13), "teenager")
        self.assertEqual(classify(12.99), "preteen")
        self.assertEqual(classify(4), "preschooler")
        self.assertEqual(classify(55), "senior")
        self.assertEqual(classify(54.9), "adult")
        self.assertEqual(classify(14.5), "teenager")
        self.assertEqual(classify(16), "sweet sixteen")
        self.assertEqual(classify(16.5), "teenager")
        self.assertEqual(classify(99), "senior")
        self.assertEqual(classify(Min), "infant")
        self.assertEqual(classify(Max), "senior")