Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / manhole / test / test_explorer.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for L{twisted.manhole.explorer}.
6 """
7
8 from twisted.trial import unittest
9 from twisted.manhole.explorer import (
10     CRUFT_WatchyThingie,
11     ExplorerImmutable,
12     Pool,
13     _WatchMonkey,
14     )
15
16
17 class Foo:
18     """
19     Test helper.
20     """
21
22
23 class PoolTestCase(unittest.TestCase):
24     """
25     Tests for the Pool class.
26     """
27
28     def test_instanceBuilding(self):
29         """
30         If the object is not in the pool a new instance is created and
31         returned.
32         """
33         p = Pool()
34         e = p.getExplorer(123, 'id')
35         self.assertIsInstance(e, ExplorerImmutable)
36         self.assertEqual(e.value, 123)
37         self.assertEqual(e.identifier, 'id')
38
39
40
41 class CRUFTWatchyThingieTestCase(unittest.TestCase):
42     """
43     Tests for the CRUFT_WatchyThingie class.
44     """
45     def test_watchObjectConstructedClass(self):
46         """
47         L{CRUFT_WatchyThingie.watchObject} changes the class of its
48         first argument to a custom watching class.
49         """
50         foo = Foo()
51         cwt = CRUFT_WatchyThingie()
52         cwt.watchObject(foo, 'id', 'cback')
53
54         # check new constructed class
55         newClassName = foo.__class__.__name__
56         self.assertEqual(newClassName, "WatchingFoo%X" % (id(foo),))
57
58
59     def test_watchObjectConstructedInstanceMethod(self):
60         """
61         L{CRUFT_WatchyThingie.watchingfoo} adds a C{_watchEmitChanged}
62         attribute which refers to a bound method on the instance
63         passed to it.
64         """
65         foo = Foo()
66         cwt = CRUFT_WatchyThingie()
67         cwt.watchObject(foo, 'id', 'cback')
68
69         # check new constructed instance method
70         self.assertIdentical(foo._watchEmitChanged.im_self, foo)
71
72
73
74 class WatchMonkeyTestCase(unittest.TestCase):
75     """
76     Tests for the _WatchMonkey class.
77     """
78     def test_install(self):
79         """
80         When _WatchMonkey is installed on a method, calling that
81         method calls the _WatchMonkey.
82         """
83         class Foo:
84             """
85             Helper.
86             """
87             def someMethod(self):
88                 """
89                 Just a method.
90                 """
91
92         foo = Foo()
93         wm = _WatchMonkey(foo)
94         wm.install('someMethod')
95
96         # patch wm's method to check that the method was exchanged
97         called = []
98         wm.__call__ = lambda s: called.append(True)
99
100         # call and check
101         foo.someMethod()
102         self.assertTrue(called)