PageRenderTime 20ms CodeModel.GetById 9ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/core/test_value.py

https://bitbucket.org/prologic/circuits/
Python | 134 lines | 83 code | 50 blank | 1 comment | 3 complexity | bf2e14b07997377dc3f2b8a916ea082f MD5 | raw file
  1#!/usr/bin/python -i
  2
  3
  4import pytest
  5
  6
  7from circuits import handler, Event, Component
  8
  9
 10class hello(Event):
 11    "Hhllo Event"
 12
 13
 14class test(Event):
 15    "test Event"
 16
 17
 18class foo(Event):
 19    "foo Event"
 20
 21
 22class values(Event):
 23    "values Event"
 24
 25    complete = True
 26
 27
 28class App(Component):
 29
 30    def hello(self):
 31        return "Hello World!"
 32
 33    def test(self):
 34        return self.fire(hello())
 35
 36    def foo(self):
 37        raise Exception("ERROR")
 38
 39    @handler("hello_value_changed")
 40    def _on_hello_value_changed(self, value):
 41        self.value = value
 42
 43    @handler("test_value_changed")
 44    def _on_test_value_changed(self, value):
 45        self.value = value
 46
 47    @handler("values", priority=2.0)
 48    def _value1(self):
 49        return "foo"
 50
 51    @handler("values", priority=1.0)
 52    def _value2(self):
 53        return "bar"
 54
 55    @handler("values", priority=0.0)
 56    def _value3(self):
 57        return self.fire(hello())
 58
 59
 60@pytest.fixture
 61def app(request, manager, watcher):
 62    app = App().register(manager)
 63    watcher.wait("registered")
 64
 65    def finalizer():
 66        app.unregister()
 67        watcher.wait("unregistered")
 68
 69    request.addfinalizer(finalizer)
 70
 71    return app
 72
 73
 74def test_value(app, watcher):
 75    x = app.fire(hello())
 76    watcher.wait("hello")
 77
 78    assert "Hello World!" in x
 79    assert x.value == "Hello World!"
 80
 81
 82def test_nested_value(app, watcher):
 83    x = app.fire(test())
 84    watcher.wait("test")
 85
 86    assert x.value == "Hello World!"
 87    assert str(x) == "Hello World!"
 88
 89
 90def test_value_notify(app, watcher):
 91    x = app.fire(hello())
 92    x.notify = True
 93
 94    watcher.wait("hello_value_changed")
 95
 96    assert "Hello World!" in x
 97    assert x.value == "Hello World!"
 98    assert app.value is x
 99
100
101def test_nested_value_notify(app, watcher):
102    x = app.fire(test())
103    x.notify = True
104
105    watcher.wait("hello_value_changed")
106
107    assert x.value == "Hello World!"
108    assert str(x) == "Hello World!"
109    assert app.value is x
110
111
112def test_error_value(app, watcher):
113    x = app.fire(foo())
114    watcher.wait("foo")
115
116    etype, evalue, etraceback = x
117    assert etype is Exception
118    assert str(evalue) == "ERROR"
119    assert isinstance(etraceback, list)
120
121
122def test_multiple_values(app, watcher):
123    v = app.fire(values())
124    watcher.wait("values_complete")
125
126    assert isinstance(v.value, list)
127
128    x = list(v)
129
130    assert "foo" in v
131    assert x == ["foo", "bar", "Hello World!"]
132    assert x[0] == "foo"
133    assert x[1] == "bar"
134    assert x[2] == "Hello World!"