PageRenderTime 30ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/core/test_feedback.py

https://bitbucket.org/prologic/circuits/
Python | 105 lines | 65 code | 33 blank | 7 comment | 9 complexity | f77eb85bad0b601e6145a6c983b6218b MD5 | raw file
  1# Module:   test_feedback
  2# Date:     11th February 2010
  3# Author:   James Mills, prologic at shortcircuit dot net dot au
  4
  5"""Feedback Channels Tests"""
  6
  7import py
  8
  9from circuits import handler, Event, Component
 10
 11
 12class test(Event):
 13    """test Event"""
 14
 15    success = True
 16    failure = True
 17
 18
 19class App(Component):
 20
 21    def __init__(self):
 22        super(App, self).__init__()
 23
 24        self.e = None
 25        self.error = None
 26        self.value = None
 27        self.success = False
 28        self.failure = False
 29
 30    @handler("*")
 31    def event(self, event, *args, **kwargs):
 32        if kwargs.get("filter", False):
 33            event.stop()
 34
 35    def test(self, error=False):
 36        if error:
 37            raise Exception("Hello World!")
 38
 39        return "Hello World!"
 40
 41    def test_success(self, e, value):
 42        self.e = e
 43        self.value = value
 44        self.success = True
 45
 46    def test_failure(self, e, error):
 47        self.e = e
 48        self.error = error
 49        self.failure = True
 50
 51
 52def reraise(e):
 53    raise e
 54
 55
 56def test_success():
 57    app = App()
 58    while app:
 59        app.flush()
 60
 61    e = test()
 62    value = app.fire(e)
 63
 64    while app:
 65        app.flush()
 66
 67    # The Event
 68    s = value.value
 69    assert s == "Hello World!"
 70
 71    while app:
 72        app.flush()
 73
 74    assert app.e == e
 75    assert app.success
 76    assert app.e.value == value
 77    assert app.value == value.value
 78
 79
 80def test_failure():
 81    app = App()
 82    while app:
 83        app.flush()
 84
 85    e = test(error=True)
 86    x = app.fire(e)
 87
 88    while app:
 89        app.flush()
 90
 91    # The Event
 92    py.test.raises(Exception, lambda x: reraise(x[1]), x.value)
 93
 94    while app:
 95        app.flush()
 96
 97    assert app.e == e
 98
 99    etype, evalue, etraceback = app.error
100    py.test.raises(Exception, lambda x: reraise(x), evalue)
101    assert etype == Exception
102
103    assert app.failure
104    assert not app.success
105    assert app.e.value == x