def testOnceInDiamondWithTransparent(self): class MyObserver(Observable): def methodOnlyCalledOnce(self, aList): aList.append('once') return yield t1 = Transparent(name="A") t2 = Transparent(name="B") t3 = Transparent(name="C") once = MyObserver("D") diamond = \ (t1, (t2, (once,) ), (t3, (once,) ) ) root = be(diamond) collector = [] methodOnlyCalledOnce = root.once.methodOnlyCalledOnce consume(methodOnlyCalledOnce(collector)) self.assertTrue(once in methodOnlyCalledOnce.seen) self.assertTrue(t2 in methodOnlyCalledOnce.seen) self.assertTrue(t3 in methodOnlyCalledOnce.seen) self.assertEquals(3, len(methodOnlyCalledOnce.seen)) self.assertEquals(set([once]), methodOnlyCalledOnce.called) self.assertEquals(['once'], collector)
def xxtestRelativeSpeedOfAll(self): from time import time class A(Observable): def f(self): raise StopIteration(None) yield root = Observable() connector = Transparent() root.addObserver(connector) connector.addObserver(A()) connector.addObserver(A()) connector.addObserver(A()) connector.addObserver(A()) connector.addObserver(A()) connector.addObserver(A()) connector.addObserver(A()) t = 0.0 for _ in xrange(10000): g = compose(root.all.f()) t0 = time() for _ in g: g.next() t1 = time() t += t1 - t0 print t def f(): for _ in xrange(10000): g = compose(root.all.f()) for _ in g: g.next()
def testTransparentUnknownImplementationIsVisibleOnTraceback(self): class Leaf(Observable): def aCall(self): raise RuntimeError('trace') def aAny(self): def lookBusy(): raise RuntimeError('trace') return yield ignored = yield lookBusy() def aAll(self): raise RuntimeError('trace') yield 'ignored' def aDo(self): raise RuntimeError('trace') root = be(( Observable(), ( Transparent(), (Leaf(), ), ), )) try: root.call.aCall() self.fail('Should not happen') except RuntimeError: self.assertFunctionsOnTraceback( 'testTransparentUnknownImplementationIsVisibleOnTraceback', 'call_unknown', 'aCall') try: compose(root.any.aAny()).next() self.fail('Should not happen') except RuntimeError: self.assertFunctionsOnTraceback( 'testTransparentUnknownImplementationIsVisibleOnTraceback', 'any_unknown', 'aAny', 'lookBusy') try: compose(root.all.aAll()).next() self.fail('Should not happen') except RuntimeError: self.assertFunctionsOnTraceback( 'testTransparentUnknownImplementationIsVisibleOnTraceback', 'all_unknown', 'aAll') try: root.do.aDo() self.fail('Should not happen') except RuntimeError: self.assertFunctionsOnTraceback( 'testTransparentUnknownImplementationIsVisibleOnTraceback', 'do_unknown', 'aDo')
def testTransparentInCaseNoneOfTheObserversRespond(self): root = be(( Observable(), (Transparent(), ), ( Transparent(), (Transparent(), (object(), )), ), (Responder(42), ), )) self.assertEquals(42, root.call.m()) try: g = compose(root.any.m()) self.assertEquals(42, g.next()) g.next() self.fail("Should have raised StopIteration") except StopIteration, e: self.assertEquals((42, ), e.args)
def testOnceInDiamondWithTransparent(self): class MyObserver(Observable): def methodOnlyCalledOnce(self, aList): aList.append('once') return yield once = MyObserver() diamond = \ (Transparent(), (Transparent(), (once,) ), (Transparent(), (once,) ) ) root = be(diamond) collector = [] list(compose(root.once.methodOnlyCalledOnce(collector))) self.assertEquals(['once'], collector)
def testNoLeakingGeneratorsInMultiTransparents(self): class Responder(Observable): def message(self): return 'response' obs = Observable() t1 = Transparent() t2 = Transparent() obs.addObserver(t1) t1.addObserver(t2) t2.addObserver(Responder()) result = obs.call.message() self.assertEquals('response', result) del obs, t1, t2, result
def testObserverAttributeErrorNotIgnored(self): class GetAttr(object): def __init__(self, attrName): self.attrName = attrName def call_unknown(self, message, *args, **kwargs): return getattr(self, self.attrName) def any_unknown(self, message, *args, **kwargs): raise StopIteration(getattr(self, self.attrName)) yield root = be((Observable(), (Transparent(), (GetAttr('doesnotexist'), )), (GetAttr('__class__'), ))) try: result = root.call.someMessage() self.fail("should not get here: %s" % result) except AttributeError, e: self.assertEquals( "'GetAttr' object has no attribute 'doesnotexist'", str(e))
def testAllAssertsResultOfCallIsGeneratorOrComposed(self): class A(object): def f(self): return 42 def all_unknown(self, message, *args, **kwargs): return 42 root = be((Observable(), (Transparent(), (A(), )))) g = compose(root.all.f()) try: g.next() self.fail("Should not happen") except AssertionError, e: self.assertTrue( "<bound method A.f of <core.observabletest.A object at 0x" in str(e), str(e)) self.assertTrue( ">> should have resulted in a generator." in str(e), str(e)) self.assertFunctionsOnTraceback( "testAllAssertsResultOfCallIsGeneratorOrComposed", "all_unknown", "verifyMethodResult")
def testNonGeneratorMethodMayNeverRaiseGeneratorExceptionsOnMessages(self): # any, all, do, call and once class OddObject(object): def stopIter(self): raise StopIteration('Stop!') def genExit(self): raise GeneratorExit('Exit!') dna = (Observable(), ( Transparent(), (OddObject(), ), )) root = be(dna) # Verify traceback's and wrapped-exception text is ok try: root.call.stopIter() except AssertionError, e: self.assertTrue( str(e).startswith( 'Non-Generator <bound method OddObject.stopIter of <core.observabletest.OddObject object at 0x' ), str(e)) expected = ignoreLineNumbers( '''>> should not have raised Generator-Exception: Traceback (most recent call last): File "%(__file__)s", line [#], in stopIter raise StopIteration('Stop!') StopIteration: Stop! ''' % fileDict) self.assertTrue( ignoreLineNumbers(str(e)).endswith(expected), str(e)) self.assertFunctionsOnTraceback( 'testNonGeneratorMethodMayNeverRaiseGeneratorExceptionsOnMessages', 'call_unknown', # From Transparent, is supposed to be on the stack to aid retracing the path taken for a messages. 'handleNonGeneratorGeneratorExceptions')
def __init__(self, unqualifiedTermFields, luceneSettings, ignoreStemmingForWords=None): Transparent.__init__(self) self._unqualifiedTermFields = unqualifiedTermFields self._analyzer = luceneSettings.createAnalyzer() self._fieldRegistry = luceneSettings.fieldRegistry self._ignoreStemmingForWords = set(ignoreStemmingForWords or [])
def __init__(self, name=None): Transparent_orig.__init__(self, name=name) self.ctx = Context()