File: test_tools.py

package info (click to toggle)
circuits 3.1.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,756 kB
  • sloc: python: 15,945; makefile: 130
file content (167 lines) | stat: -rw-r--r-- 2,828 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Module:   test_tools
# Date:     13th March 2009
# Author:   James Mills, prologic at shortcircuit dot net dot au

"""Tools Test Suite

Test all functionality of the tools package.
"""

import pytest

try:
    from threading import current_thread
except ImportError:
    from threading import currentThread as current_thread  # NOQA

from circuits import Component, reprhandler
from circuits.tools import kill, inspect, findroot, tryimport


class A(Component):

    def foo(self):
        print("A!")


class B(Component):

    def foo(self):
        print("B!")


class C(Component):

    def foo(self):
        print("C!")


class D(Component):

    def foo(self):
        print("D!")


class E(Component):

    def foo(self):
        print("E!")


class F(Component):

    def foo(self):
        print("F!")


def test_kill():
    a = A()
    b = B()
    c = C()
    d = D()
    e = E()
    f = F()

    a += b
    b += c

    e += f
    d += e
    a += d

    assert a.parent == a
    assert b.parent == a
    assert c.parent == b
    assert not c.components

    assert b in a.components
    assert d in a.components

    assert d.parent == a
    assert e.parent == d
    assert f.parent == e

    assert f in e.components
    assert e in d.components
    assert not f.components

    assert kill(d) is None
    while a:
        a.flush()

    assert a.parent == a
    assert b.parent == a
    assert c.parent == b
    assert not c.components

    assert b in a.components
    assert not d in a.components
    assert not e in d.components
    assert not f in e.components

    assert d.parent == d
    assert e.parent == e
    assert f.parent == f

    assert not d.components
    assert not e.components
    assert not f.components


def test_inspect():
    if pytest.PYVER[:2] == (3, 3):
        pytest.skip("Broken on Python 3.3")

    a = A()
    s = inspect(a)

    assert "Components: 0" in s
    assert "Event Handlers: 2" in s
    assert "foo; 1" in s
    assert "<handler[*.foo] (A.foo)>" in s
    assert "prepare_unregister_complete; 1" in s
    assert "<handler[<instance of A>.prepare_unregister_complete] (A._on_prepare_unregister_complete)>" in s


def test_findroot():
    a = A()
    b = B()
    c = C()

    a += b
    b += c

    root = findroot(a)
    assert root == a

    root = findroot(b)
    assert root == a

    root = findroot(c)
    assert root == a


def test_reprhandler():
    a = A()
    s = reprhandler(a.foo)
    assert s == "<handler[*.foo] (A.foo)>"

    f = lambda: None
    pytest.raises(AttributeError, reprhandler, f)


def test_tryimport():
    import os
    m = tryimport("os")
    assert m is os


def test_tryimport_obj():
    from os import path
    m = tryimport("os", "path")
    assert m is path


def test_tryimport_fail():
    m = tryimport("asdf")
    assert m is None