PageRenderTime 17ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/tools/test_tools.py

https://bitbucket.org/prologic/circuits/
Python | 167 lines | 106 code | 54 blank | 7 comment | 25 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
  5"""Tools Test Suite
  6
  7Test all functionality of the tools package.
  8"""
  9
 10import pytest
 11
 12try:
 13    from threading import current_thread
 14except ImportError:
 15    from threading import currentThread as current_thread  # NOQA
 16
 17from circuits import Component, reprhandler
 18from circuits.tools import kill, inspect, findroot, tryimport
 19
 20
 21class A(Component):
 22
 23    def foo(self):
 24        print("A!")
 25
 26
 27class B(Component):
 28
 29    def foo(self):
 30        print("B!")
 31
 32
 33class C(Component):
 34
 35    def foo(self):
 36        print("C!")
 37
 38
 39class D(Component):
 40
 41    def foo(self):
 42        print("D!")
 43
 44
 45class E(Component):
 46
 47    def foo(self):
 48        print("E!")
 49
 50
 51class F(Component):
 52
 53    def foo(self):
 54        print("F!")
 55
 56
 57def test_kill():
 58    a = A()
 59    b = B()
 60    c = C()
 61    d = D()
 62    e = E()
 63    f = F()
 64
 65    a += b
 66    b += c
 67
 68    e += f
 69    d += e
 70    a += d
 71
 72    assert a.parent == a
 73    assert b.parent == a
 74    assert c.parent == b
 75    assert not c.components
 76
 77    assert b in a.components
 78    assert d in a.components
 79
 80    assert d.parent == a
 81    assert e.parent == d
 82    assert f.parent == e
 83
 84    assert f in e.components
 85    assert e in d.components
 86    assert not f.components
 87
 88    assert kill(d) is None
 89    while a:
 90        a.flush()
 91
 92    assert a.parent == a
 93    assert b.parent == a
 94    assert c.parent == b
 95    assert not c.components
 96
 97    assert b in a.components
 98    assert not d in a.components
 99    assert not e in d.components
100    assert not f in e.components
101
102    assert d.parent == d
103    assert e.parent == e
104    assert f.parent == f
105
106    assert not d.components
107    assert not e.components
108    assert not f.components
109
110
111def test_inspect():
112    if pytest.PYVER[:2] == (3, 3):
113        pytest.skip("Broken on Python 3.3")
114
115    a = A()
116    s = inspect(a)
117
118    assert "Components: 0" in s
119    assert "Event Handlers: 2" in s
120    assert "foo; 1" in s
121    assert "<handler[*.foo] (A.foo)>" in s
122    assert "prepare_unregister_complete; 1" in s
123    assert "<handler[<instance of A>.prepare_unregister_complete] (A._on_prepare_unregister_complete)>" in s
124
125
126def test_findroot():
127    a = A()
128    b = B()
129    c = C()
130
131    a += b
132    b += c
133
134    root = findroot(a)
135    assert root == a
136
137    root = findroot(b)
138    assert root == a
139
140    root = findroot(c)
141    assert root == a
142
143
144def test_reprhandler():
145    a = A()
146    s = reprhandler(a.foo)
147    assert s == "<handler[*.foo] (A.foo)>"
148
149    f = lambda: None
150    pytest.raises(AttributeError, reprhandler, f)
151
152
153def test_tryimport():
154    import os
155    m = tryimport("os")
156    assert m is os
157
158
159def test_tryimport_obj():
160    from os import path
161    m = tryimport("os", "path")
162    assert m is path
163
164
165def test_tryimport_fail():
166    m = tryimport("asdf")
167    assert m is None