PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/tools/test_tools.py

https://bitbucket.org/prologic/circuits/
Python | 167 lines | 106 code | 54 blank | 7 comment | 4 complexity | 90968a809511bd030bb044394d2bdfaf MD5 | raw file
  1. # Module: test_tools
  2. # Date: 13th March 2009
  3. # Author: James Mills, prologic at shortcircuit dot net dot au
  4. """Tools Test Suite
  5. Test all functionality of the tools package.
  6. """
  7. import pytest
  8. try:
  9. from threading import current_thread
  10. except ImportError:
  11. from threading import currentThread as current_thread # NOQA
  12. from circuits import Component, reprhandler
  13. from circuits.tools import kill, inspect, findroot, tryimport
  14. class A(Component):
  15. def foo(self):
  16. print("A!")
  17. class B(Component):
  18. def foo(self):
  19. print("B!")
  20. class C(Component):
  21. def foo(self):
  22. print("C!")
  23. class D(Component):
  24. def foo(self):
  25. print("D!")
  26. class E(Component):
  27. def foo(self):
  28. print("E!")
  29. class F(Component):
  30. def foo(self):
  31. print("F!")
  32. def test_kill():
  33. a = A()
  34. b = B()
  35. c = C()
  36. d = D()
  37. e = E()
  38. f = F()
  39. a += b
  40. b += c
  41. e += f
  42. d += e
  43. a += d
  44. assert a.parent == a
  45. assert b.parent == a
  46. assert c.parent == b
  47. assert not c.components
  48. assert b in a.components
  49. assert d in a.components
  50. assert d.parent == a
  51. assert e.parent == d
  52. assert f.parent == e
  53. assert f in e.components
  54. assert e in d.components
  55. assert not f.components
  56. assert kill(d) is None
  57. while a:
  58. a.flush()
  59. assert a.parent == a
  60. assert b.parent == a
  61. assert c.parent == b
  62. assert not c.components
  63. assert b in a.components
  64. assert not d in a.components
  65. assert not e in d.components
  66. assert not f in e.components
  67. assert d.parent == d
  68. assert e.parent == e
  69. assert f.parent == f
  70. assert not d.components
  71. assert not e.components
  72. assert not f.components
  73. def test_inspect():
  74. if pytest.PYVER[:2] == (3, 3):
  75. pytest.skip("Broken on Python 3.3")
  76. a = A()
  77. s = inspect(a)
  78. assert "Components: 0" in s
  79. assert "Event Handlers: 2" in s
  80. assert "foo; 1" in s
  81. assert "<handler[*.foo] (A.foo)>" in s
  82. assert "prepare_unregister_complete; 1" in s
  83. assert "<handler[<instance of A>.prepare_unregister_complete] (A._on_prepare_unregister_complete)>" in s
  84. def test_findroot():
  85. a = A()
  86. b = B()
  87. c = C()
  88. a += b
  89. b += c
  90. root = findroot(a)
  91. assert root == a
  92. root = findroot(b)
  93. assert root == a
  94. root = findroot(c)
  95. assert root == a
  96. def test_reprhandler():
  97. a = A()
  98. s = reprhandler(a.foo)
  99. assert s == "<handler[*.foo] (A.foo)>"
  100. f = lambda: None
  101. pytest.raises(AttributeError, reprhandler, f)
  102. def test_tryimport():
  103. import os
  104. m = tryimport("os")
  105. assert m is os
  106. def test_tryimport_obj():
  107. from os import path
  108. m = tryimport("os", "path")
  109. assert m is path
  110. def test_tryimport_fail():
  111. m = tryimport("asdf")
  112. assert m is None