[selector] allow inplace operator in selector computation
Intended to facilitate overriding of existing selectors chain in view declaration.
It make the code shorter and clearer when you've to import long module pathnames
--- a/appobject.py Thu Apr 22 15:50:45 2010 +0200
+++ b/appobject.py Thu Apr 22 17:04:51 2010 +0200
@@ -98,14 +98,13 @@
def __rand__(self, other):
return AndSelector(other, self)
def __iand__(self, other):
- raise NotImplementedError('cant use inplace & (binary and)')
-
+ return AndSelector(self, other)
def __or__(self, other):
return OrSelector(self, other)
def __ror__(self, other):
return OrSelector(other, self)
def __ior__(self, other):
- raise NotImplementedError('cant use inplace | (binary or)')
+ return OrSelector(self, other)
def __invert__(self):
return NotSelector(self)
--- a/test/unittest_selectors.py Thu Apr 22 15:50:45 2010 +0200
+++ b/test/unittest_selectors.py Thu Apr 22 17:04:51 2010 +0200
@@ -87,6 +87,42 @@
csel = AndSelector(Selector(), sel)
self.assertIs(csel.search_selector(implements), sel)
+ def test_inplace_and(self):
+ selector = _1_()
+ selector &= _1_()
+ selector &= _1_()
+ self.assertEquals(selector(None), 3)
+ selector = _1_()
+ selector &= _0_()
+ selector &= _1_()
+ self.assertEquals(selector(None), 0)
+ selector = _0_()
+ selector &= _1_()
+ selector &= _1_()
+ self.assertEquals(selector(None), 0)
+ selector = _0_()
+ selector &= _0_()
+ selector &= _0_()
+ self.assertEquals(selector(None), 0)
+
+ def test_inplace_or(self):
+ selector = _1_()
+ selector |= _1_()
+ selector |= _1_()
+ self.assertEquals(selector(None), 1)
+ selector = _1_()
+ selector |= _0_()
+ selector |= _1_()
+ self.assertEquals(selector(None), 1)
+ selector = _0_()
+ selector |= _1_()
+ selector |= _1_()
+ self.assertEquals(selector(None), 1)
+ selector = _0_()
+ selector |= _0_()
+ selector |= _0_()
+ self.assertEquals(selector(None), 0)
+
class ImplementsSelectorTC(CubicWebTC):
def test_etype_priority(self):