import unittest, sys |
import unittest, sys |
from peak.rules.core import * |
from peak.rules.core import * |
|
from peak.rules.core import CODE |
|
|
x2 = lambda a: a*2 |
x2 = lambda a: a*2 |
x3 = lambda next_method, a: next_method(a)*3 |
x3 = lambda next_method, a: next_method(a)*3 |
self.assertEqual(f(1), 42) |
self.assertEqual(f(1), 42) |
self.assertEqual(f('x'), 'x') |
self.assertEqual(f('x'), 'x') |
|
|
|
|
class MiscTests(unittest.TestCase): |
class MiscTests(unittest.TestCase): |
|
|
def testPointers(self): |
def testPointers(self): |
from peak.rules.indexing import IsObject |
from peak.rules.indexing import IsObject |
from sys import maxint |
try: from sys import maxint |
|
except ImportError: maxint = (2<<32)-1 |
anOb = object() |
anOb = object() |
ptr = IsObject(anOb) |
ptr = IsObject(anOb) |
self.assertEqual(id(anOb)&maxint,ptr) |
self.assertEqual(id(anOb)&maxint,ptr) |
d.rules.default_action = lambda *args: log.append(args) |
d.rules.default_action = lambda *args: log.append(args) |
d.request_regeneration() |
d.request_regeneration() |
f1 = abstract(f1) |
f1 = abstract(f1) |
self.assertNotEqual(d.backup, f1.func_code) |
self.assertNotEqual(d.backup, getattr(f1, CODE)) |
self.assertEqual(f1.func_code, d._regen) |
self.assertEqual(getattr(f1, CODE), d._regen) |
f1.func_code = d.backup |
setattr(f1, CODE, d.backup) |
f1(27,42) |
f1(27,42) |
self.assertEqual(log, [(27,42)]) |
self.assertEqual(log, [(27,42)]) |
|
|
|
|
|
|
def testIndexClassicMRO(self): |
def testIndexClassicMRO(self): |
|
try: from types import InstanceType |
|
except ImportError: return # No classic types on Python 3 |
class MyEngine: pass |
class MyEngine: pass |
eng = MyEngine() |
eng = MyEngine() |
from peak.rules.indexing import TypeIndex |
from peak.rules.indexing import TypeIndex |
from peak.rules.criteria import Class |
from peak.rules.criteria import Class |
from types import InstanceType |
|
ind = TypeIndex(eng, 'classes') |
ind = TypeIndex(eng, 'classes') |
ind.add_case(0, Class(MyEngine)) |
ind.add_case(0, Class(MyEngine)) |
ind.add_case(1, Class(object)) |
ind.add_case(1, Class(object)) |
self.assertEqual( |
self.assertEqual( |
argnames(lambda a,b,c=None: None), list('abc') |
argnames(lambda a,b,c=None: None), list('abc') |
) |
) |
|
if sys.version<"3": |
self.assertEqual( |
self.assertEqual( |
argnames(lambda a,(b,(c,d)), e: None), list('abcde') |
argnames(eval("lambda a,(b,(c,d)), e: None")), list('abcde') |
) |
) |
|
|
def test_istype_implies(self): |
def test_istype_implies(self): |
self.failIf(implies(istype(object, False), object)) |
self.failIf(implies(istype(object, False), object)) |
self.failIf(implies(istype(int, False), object)) |
self.failIf(implies(istype(int, False), object)) |
|
|
|
|
|
|
def testIndexedEngine(self): |
def testIndexedEngine(self): |
from peak.rules.predicates import IndexedEngine, Comparison |
from peak.rules.predicates import IndexedEngine, Comparison |
from peak.rules.criteria import Range, Value, Test, Signature |
from peak.rules.criteria import Range, Value, Test, Signature |
from peak.rules.core import Local, Rule |
from peak.rules.core import Local, Rule |
from peak.rules.criteria import Signature, Test, Class, Value |
from peak.rules.criteria import Signature, Test, Class, Value |
from peak.rules.predicates import IsInstance, Truth |
from peak.rules.predicates import IsInstance, Truth |
|
from peak.util.decorators import enclosing_frame |
|
im_func = lambda x: getattr(x, 'im_func', x) |
|
|
abstract() |
abstract() |
def f1(a): pass |
def f1(a): pass |
num = Rule(None).sequence |
num = Rule(None).sequence |
|
|
class T: |
class T: |
f1=sys._getframe(1).f_locals['f1'] # ugh |
f1 = enclosing_frame(level=2).f_locals['f1'] # ugh |
when(f1) |
when(f1) |
def f1_(a): pass |
def f1_(a): pass |
|
|
f2=sys._getframe(1).f_locals['f2'] |
f2 = enclosing_frame(level=2).f_locals['f2'] |
when(f2, 'b') |
when(f2, 'b') |
def f2_(b): pass |
def f2_(b): pass |
|
|
self.assertEqual( |
self.assertEqual( |
list(rules_for(f1)), [Rule(T.f1_.im_func, (T,), Method, num+1)] |
list(rules_for(f1)), [Rule(im_func(T.f1_), (T,), Method, num+1)] |
) |
) |
self.assertEqual( |
self.assertEqual( |
list(rules_for(f2)), [Rule( |
list(rules_for(f2)), [Rule( |
T.f2_.im_func, Signature([ |
im_func(T.f2_), Signature([ |
Test(IsInstance(Local('b')), Class(T)), |
Test(IsInstance(Local('b')), Class(T)), |
Test(Truth(Local('b')), Value(True)) |
Test(Truth(Local('b')), Value(True)) |
]), Method, num+2) |
]), Method, num+2) |
|
|
|
|
|
|
|
|
|
|
def testParseInequalities(self): |
def testParseInequalities(self): |
from peak.rules.predicates import CriteriaBuilder, Comparison, Truth |
from peak.rules.predicates import CriteriaBuilder, Comparison, Truth |
from peak.util.assembler import Compare, Local |
from peak.util.assembler import Compare, Local |
('==','==','!=','==','!='), |
('==','==','!=','==','!='), |
('<>','<>','==','!=','=='), |
('<>','<>','==','!=','=='), |
]: |
]: |
|
if op=='<>' and sys.version>="3": |
|
continue |
fwd_sig = Test(x, Inequality(op, 1)) |
fwd_sig = Test(x, Inequality(op, 1)) |
self.assertEqual(pe('x %s 1' % op), fwd_sig) |
self.assertEqual(pe('x %s 1' % op), fwd_sig) |
self.assertEqual(pe('1 %s x' % mirror_op), fwd_sig) |
self.assertEqual(pe('1 %s x' % mirror_op), fwd_sig) |
self.assertEqual(pe('not x %s y' % op),x_cmp_y(stdop,False)) |
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)) |
self.assertEqual(pe('not x %s y' % not_op),x_cmp_y(not_stdop,False)) |
|
|
|
|
|
|
def testInheritance(self): |
def testInheritance(self): |
class X: pass |
class X: pass |
class Y(X): pass |
class Y(X): pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def additional_tests(): |
def additional_tests(): |
import doctest |
import doctest |
files = [ |
files = [ |
*files |
*files |
) |
) |
|
|
|
try: |
|
reduce |
|
except NameError: |
|
from functools import reduce |
|
|
|
|
|
|