/tests/core/test_utils.py

https://bitbucket.org/prologic/circuits/ · Python · 104 lines · 64 code · 38 blank · 2 comment · 5 complexity · 7ec063980cdb22e66ee69086cd121d7e MD5 · raw file

  1. #!/usr/bin/env python
  2. import sys
  3. from types import ModuleType
  4. from circuits import Component
  5. from circuits.core.utils import findchannel, findroot, findtype
  6. FOO = """\
  7. def foo():
  8. return "Hello World!"
  9. """
  10. FOOBAR = """\
  11. def foo();
  12. return "Hello World!'
  13. """
  14. class Base(Component):
  15. """Base"""
  16. class App(Base):
  17. def hello(self):
  18. return "Hello World!"
  19. class A(Component):
  20. channel = "a"
  21. class B(Component):
  22. channel = "b"
  23. def test_safeimport(tmpdir):
  24. from circuits.core.utils import safeimport
  25. sys.path.insert(0, str(tmpdir))
  26. foo_path = tmpdir.ensure("foo.py")
  27. foo_path.write(FOO)
  28. foo = safeimport("foo")
  29. assert foo is not None
  30. assert type(foo) is ModuleType
  31. s = foo.foo()
  32. assert s == "Hello World!"
  33. pyc = foo_path.new(ext="pyc")
  34. if pyc.check(file=1):
  35. pyc.remove(ignore_errors=True)
  36. pyd = foo_path.dirpath('__pycache__')
  37. if pyd.check(dir=1):
  38. pyd.remove(ignore_errors=True)
  39. foo_path.write(FOOBAR)
  40. foo = safeimport("foo")
  41. assert foo is None
  42. assert foo not in sys.modules
  43. def test_findroot():
  44. app = App()
  45. a = A()
  46. b = B()
  47. b.register(a)
  48. a.register(app)
  49. while app:
  50. app.flush()
  51. root = findroot(b)
  52. assert root == app
  53. def test_findchannel():
  54. app = App()
  55. (A() + B()).register(app)
  56. while app:
  57. app.flush()
  58. a = findchannel(app, "a")
  59. assert a.channel == "a"
  60. def test_findtype():
  61. app = App()
  62. (A() + B()).register(app)
  63. while app:
  64. app.flush()
  65. a = findtype(app, A)
  66. assert isinstance(a, A)