PageRenderTime 41ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/core/test_value.py

https://bitbucket.org/prologic/circuits/
Python | 134 lines | 83 code | 50 blank | 1 comment | 0 complexity | bf2e14b07997377dc3f2b8a916ea082f MD5 | raw file
  1. #!/usr/bin/python -i
  2. import pytest
  3. from circuits import handler, Event, Component
  4. class hello(Event):
  5. "Hhllo Event"
  6. class test(Event):
  7. "test Event"
  8. class foo(Event):
  9. "foo Event"
  10. class values(Event):
  11. "values Event"
  12. complete = True
  13. class App(Component):
  14. def hello(self):
  15. return "Hello World!"
  16. def test(self):
  17. return self.fire(hello())
  18. def foo(self):
  19. raise Exception("ERROR")
  20. @handler("hello_value_changed")
  21. def _on_hello_value_changed(self, value):
  22. self.value = value
  23. @handler("test_value_changed")
  24. def _on_test_value_changed(self, value):
  25. self.value = value
  26. @handler("values", priority=2.0)
  27. def _value1(self):
  28. return "foo"
  29. @handler("values", priority=1.0)
  30. def _value2(self):
  31. return "bar"
  32. @handler("values", priority=0.0)
  33. def _value3(self):
  34. return self.fire(hello())
  35. @pytest.fixture
  36. def app(request, manager, watcher):
  37. app = App().register(manager)
  38. watcher.wait("registered")
  39. def finalizer():
  40. app.unregister()
  41. watcher.wait("unregistered")
  42. request.addfinalizer(finalizer)
  43. return app
  44. def test_value(app, watcher):
  45. x = app.fire(hello())
  46. watcher.wait("hello")
  47. assert "Hello World!" in x
  48. assert x.value == "Hello World!"
  49. def test_nested_value(app, watcher):
  50. x = app.fire(test())
  51. watcher.wait("test")
  52. assert x.value == "Hello World!"
  53. assert str(x) == "Hello World!"
  54. def test_value_notify(app, watcher):
  55. x = app.fire(hello())
  56. x.notify = True
  57. watcher.wait("hello_value_changed")
  58. assert "Hello World!" in x
  59. assert x.value == "Hello World!"
  60. assert app.value is x
  61. def test_nested_value_notify(app, watcher):
  62. x = app.fire(test())
  63. x.notify = True
  64. watcher.wait("hello_value_changed")
  65. assert x.value == "Hello World!"
  66. assert str(x) == "Hello World!"
  67. assert app.value is x
  68. def test_error_value(app, watcher):
  69. x = app.fire(foo())
  70. watcher.wait("foo")
  71. etype, evalue, etraceback = x
  72. assert etype is Exception
  73. assert str(evalue) == "ERROR"
  74. assert isinstance(etraceback, list)
  75. def test_multiple_values(app, watcher):
  76. v = app.fire(values())
  77. watcher.wait("values_complete")
  78. assert isinstance(v.value, list)
  79. x = list(v)
  80. assert "foo" in v
  81. assert x == ["foo", "bar", "Hello World!"]
  82. assert x[0] == "foo"
  83. assert x[1] == "bar"
  84. assert x[2] == "Hello World!"