aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/Twisted-12.2.0-py2.7-linux-x86_64.egg/twisted/test/test_explorer.py
blob: 2b8fcf0828cf7e2b61f35fe6e13ca703e4dabbd3 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.


"""
Test cases for explorer
"""

from twisted.trial import unittest

from twisted.manhole import explorer

import types

"""
# Tests:

 Get an ObjectLink.  Browse ObjectLink.identifier.  Is it the same?

 Watch Object.  Make sure an ObjectLink is received when:
   Call a method.
   Set an attribute.

 Have an Object with a setattr class.  Watch it.
   Do both the navite setattr and the watcher get called?

 Sequences with circular references.  Does it blow up?
"""

class SomeDohickey:
    def __init__(self, *a):
        self.__dict__['args'] = a

    def bip(self):
        return self.args


class TestBrowser(unittest.TestCase):
    def setUp(self):
        self.pool = explorer.explorerPool
        self.pool.clear()
        self.testThing = ["How many stairs must a man climb down?",
                          SomeDohickey(42)]

    def test_chain(self):
        "Following a chain of Explorers."
        xplorer = self.pool.getExplorer(self.testThing, 'testThing')
        self.assertEqual(xplorer.id, id(self.testThing))
        self.assertEqual(xplorer.identifier, 'testThing')

        dxplorer = xplorer.get_elements()[1]
        self.assertEqual(dxplorer.id, id(self.testThing[1]))

class Watcher:
    zero = 0
    def __init__(self):
        self.links = []

    def receiveBrowserObject(self, olink):
        self.links.append(olink)

    def setZero(self):
        self.zero = len(self.links)

    def len(self):
        return len(self.links) - self.zero


class SetattrDohickey:
    def __setattr__(self, k, v):
        v = list(str(v))
        v.reverse()
        self.__dict__[k] = ''.join(v)

class MiddleMan(SomeDohickey, SetattrDohickey):
    pass

# class TestWatch(unittest.TestCase):
class FIXME_Watch:
    def setUp(self):
        self.globalNS = globals().copy()
        self.localNS = {}
        self.browser = explorer.ObjectBrowser(self.globalNS, self.localNS)
        self.watcher = Watcher()

    def test_setAttrPlain(self):
        "Triggering a watcher response by setting an attribute."

        testThing = SomeDohickey('pencil')
        self.browser.watchObject(testThing, 'testThing',
                                 self.watcher.receiveBrowserObject)
        self.watcher.setZero()

        testThing.someAttr = 'someValue'

        self.assertEqual(testThing.someAttr, 'someValue')
        self.failUnless(self.watcher.len())
        olink = self.watcher.links[-1]
        self.assertEqual(olink.id, id(testThing))

    def test_setAttrChain(self):
        "Setting an attribute on a watched object that has __setattr__"
        testThing = MiddleMan('pencil')

        self.browser.watchObject(testThing, 'testThing',
                                 self.watcher.receiveBrowserObject)
        self.watcher.setZero()

        testThing.someAttr = 'ZORT'

        self.assertEqual(testThing.someAttr, 'TROZ')
        self.failUnless(self.watcher.len())
        olink = self.watcher.links[-1]
        self.assertEqual(olink.id, id(testThing))


    def test_method(self):
        "Triggering a watcher response by invoking a method."

        for testThing in (SomeDohickey('pencil'), MiddleMan('pencil')):
            self.browser.watchObject(testThing, 'testThing',
                                     self.watcher.receiveBrowserObject)
            self.watcher.setZero()

            rval = testThing.bip()
            self.assertEqual(rval, ('pencil',))

            self.failUnless(self.watcher.len())
            olink = self.watcher.links[-1]
            self.assertEqual(olink.id, id(testThing))


def function_noArgs():
    "A function which accepts no arguments at all."
    return

def function_simple(a, b, c):
    "A function which accepts several arguments."
    return a, b, c

def function_variable(*a, **kw):
    "A function which accepts a variable number of args and keywords."
    return a, kw

def function_crazy((alpha, beta), c, d=range(4), **kw):
    "A function with a mad crazy signature."
    return alpha, beta, c, d, kw

class TestBrowseFunction(unittest.TestCase):

    def setUp(self):
        self.pool = explorer.explorerPool
        self.pool.clear()

    def test_sanity(self):
        """Basic checks for browse_function.

        Was the proper type returned?  Does it have the right name and ID?
        """
        for f_name in ('function_noArgs', 'function_simple',
                       'function_variable', 'function_crazy'):
            f = eval(f_name)

            xplorer = self.pool.getExplorer(f, f_name)

            self.assertEqual(xplorer.id, id(f))

            self.failUnless(isinstance(xplorer, explorer.ExplorerFunction))

            self.assertEqual(xplorer.name, f_name)

    def test_signature_noArgs(self):
        """Testing zero-argument function signature.
        """

        xplorer = self.pool.getExplorer(function_noArgs, 'function_noArgs')

        self.assertEqual(len(xplorer.signature), 0)

    def test_signature_simple(self):
        """Testing simple function signature.
        """

        xplorer = self.pool.getExplorer(function_simple, 'function_simple')

        expected_signature = ('a','b','c')

        self.assertEqual(xplorer.signature.name, expected_signature)

    def test_signature_variable(self):
        """Testing variable-argument function signature.
        """

        xplorer = self.pool.getExplorer(function_variable,
                                        'function_variable')

        expected_names = ('a','kw')
        signature = xplorer.signature

        self.assertEqual(signature.name, expected_names)
        self.failUnless(signature.is_varlist(0))
        self.failUnless(signature.is_keyword(1))

    def test_signature_crazy(self):
        """Testing function with crazy signature.
        """
        xplorer = self.pool.getExplorer(function_crazy, 'function_crazy')

        signature = xplorer.signature

        expected_signature = [{'name': 'c'},
                              {'name': 'd',
                               'default': range(4)},
                              {'name': 'kw',
                               'keywords': 1}]

        # The name of the first argument seems to be indecipherable,
        # but make sure it has one (and no default).
        self.failUnless(signature.get_name(0))
        self.failUnless(not signature.get_default(0)[0])

        self.assertEqual(signature.get_name(1), 'c')

        # Get a list of values from a list of ExplorerImmutables.
        arg_2_default = map(lambda l: l.value,
                            signature.get_default(2)[1].get_elements())

        self.assertEqual(signature.get_name(2), 'd')
        self.assertEqual(arg_2_default, range(4))

        self.assertEqual(signature.get_name(3), 'kw')
        self.failUnless(signature.is_keyword(3))

if __name__ == '__main__':
    unittest.main()