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

/tests/core/test_utils.py

https://bitbucket.org/prologic/circuits/
Python | 104 lines | 100 code | 3 blank | 1 comment | 0 complexity | 7ec063980cdb22e66ee69086cd121d7e MD5 | raw file
  1#!/usr/bin/env python
  2
  3import sys
  4from types import ModuleType
  5
  6from circuits import Component
  7from circuits.core.utils import findchannel, findroot, findtype
  8
  9FOO = """\
 10def foo():
 11    return "Hello World!"
 12"""
 13
 14FOOBAR = """\
 15def foo();
 16    return "Hello World!'
 17"""
 18
 19
 20class Base(Component):
 21    """Base"""
 22
 23
 24class App(Base):
 25
 26    def hello(self):
 27        return "Hello World!"
 28
 29
 30class A(Component):
 31
 32    channel = "a"
 33
 34
 35class B(Component):
 36
 37    channel = "b"
 38
 39
 40def test_safeimport(tmpdir):
 41    from circuits.core.utils import safeimport
 42
 43    sys.path.insert(0, str(tmpdir))
 44
 45    foo_path = tmpdir.ensure("foo.py")
 46    foo_path.write(FOO)
 47
 48    foo = safeimport("foo")
 49    assert foo is not None
 50    assert type(foo) is ModuleType
 51
 52    s = foo.foo()
 53    assert s == "Hello World!"
 54    pyc = foo_path.new(ext="pyc")
 55    if pyc.check(file=1):
 56        pyc.remove(ignore_errors=True)
 57    pyd = foo_path.dirpath('__pycache__')
 58    if pyd.check(dir=1):
 59        pyd.remove(ignore_errors=True)
 60    foo_path.write(FOOBAR)
 61
 62    foo = safeimport("foo")
 63    assert foo is None
 64    assert foo not in sys.modules
 65
 66
 67def test_findroot():
 68    app = App()
 69    a = A()
 70    b = B()
 71
 72    b.register(a)
 73    a.register(app)
 74
 75    while app:
 76        app.flush()
 77
 78    root = findroot(b)
 79
 80    assert root == app
 81
 82
 83def test_findchannel():
 84    app = App()
 85    (A() + B()).register(app)
 86
 87    while app:
 88        app.flush()
 89
 90    a = findchannel(app, "a")
 91
 92    assert a.channel == "a"
 93
 94
 95def test_findtype():
 96    app = App()
 97    (A() + B()).register(app)
 98
 99    while app:
100        app.flush()
101
102    a = findtype(app, A)
103
104    assert isinstance(a, A)