OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2008 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 | |
5 """ | |
6 Tests for L{twisted.python.compat}. | |
7 """ | |
8 | |
9 import types, socket | |
10 | |
11 from twisted.trial import unittest | |
12 | |
13 from twisted.python.compat import set, frozenset | |
14 | |
15 | |
16 | |
17 class IterableCounter: | |
18 def __init__(self, lim=0): | |
19 self.lim = lim | |
20 self.i = -1 | |
21 | |
22 def __iter__(self): | |
23 return self | |
24 | |
25 def next(self): | |
26 self.i += 1 | |
27 if self.i >= self.lim: | |
28 raise StopIteration | |
29 return self.i | |
30 | |
31 class CompatTestCase(unittest.TestCase): | |
32 def testDict(self): | |
33 d1 = {'a': 'b'} | |
34 d2 = dict(d1) | |
35 self.assertEquals(d1, d2) | |
36 d1['a'] = 'c' | |
37 self.assertNotEquals(d1, d2) | |
38 d2 = dict(d1.items()) | |
39 self.assertEquals(d1, d2) | |
40 | |
41 def testBool(self): | |
42 self.assertEquals(bool('hi'), True) | |
43 self.assertEquals(bool(True), True) | |
44 self.assertEquals(bool(''), False) | |
45 self.assertEquals(bool(False), False) | |
46 | |
47 def testIteration(self): | |
48 lst1, lst2 = range(10), [] | |
49 | |
50 for i in iter(lst1): | |
51 lst2.append(i) | |
52 self.assertEquals(lst1, lst2) | |
53 del lst2[:] | |
54 | |
55 try: | |
56 iterable = iter(lst1) | |
57 while 1: | |
58 lst2.append(iterable.next()) | |
59 except StopIteration: | |
60 pass | |
61 self.assertEquals(lst1, lst2) | |
62 del lst2[:] | |
63 | |
64 for i in iter(IterableCounter(10)): | |
65 lst2.append(i) | |
66 self.assertEquals(lst1, lst2) | |
67 del lst2[:] | |
68 | |
69 try: | |
70 iterable = iter(IterableCounter(10)) | |
71 while 1: | |
72 lst2.append(iterable.next()) | |
73 except StopIteration: | |
74 pass | |
75 self.assertEquals(lst1, lst2) | |
76 del lst2[:] | |
77 | |
78 for i in iter(IterableCounter(20).next, 10): | |
79 lst2.append(i) | |
80 self.assertEquals(lst1, lst2) | |
81 | |
82 def testIsinstance(self): | |
83 self.assert_(isinstance(u'hi', types.StringTypes)) | |
84 self.assert_(isinstance(self, unittest.TestCase)) | |
85 # I'm pretty sure it's impossible to implement this | |
86 # without replacing isinstance on 2.2 as well :( | |
87 # self.assert_(isinstance({}, dict)) | |
88 | |
89 def testStrip(self): | |
90 self.assertEquals(' x '.lstrip(' '), 'x ') | |
91 self.assertEquals(' x x'.lstrip(' '), 'x x') | |
92 self.assertEquals(' x '.rstrip(' '), ' x') | |
93 self.assertEquals('x x '.rstrip(' '), 'x x') | |
94 | |
95 self.assertEquals('\t x '.lstrip('\t '), 'x ') | |
96 self.assertEquals(' \tx x'.lstrip('\t '), 'x x') | |
97 self.assertEquals(' x\t '.rstrip(' \t'), ' x') | |
98 self.assertEquals('x x \t'.rstrip(' \t'), 'x x') | |
99 | |
100 self.assertEquals('\t x '.strip('\t '), 'x') | |
101 self.assertEquals(' \tx x'.strip('\t '), 'x x') | |
102 self.assertEquals(' x\t '.strip(' \t'), 'x') | |
103 self.assertEquals('x x \t'.strip(' \t'), 'x x') | |
104 | |
105 def testNToP(self): | |
106 from twisted.python.compat import inet_ntop | |
107 | |
108 f = lambda a: inet_ntop(socket.AF_INET6, a) | |
109 g = lambda a: inet_ntop(socket.AF_INET, a) | |
110 | |
111 self.assertEquals('::', f('\x00' * 16)) | |
112 self.assertEquals('::1', f('\x00' * 15 + '\x01')) | |
113 self.assertEquals( | |
114 'aef:b01:506:1001:ffff:9997:55:170', | |
115 f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70'
)) | |
116 | |
117 self.assertEquals('1.0.1.0', g('\x01\x00\x01\x00')) | |
118 self.assertEquals('170.85.170.85', g('\xaa\x55\xaa\x55')) | |
119 self.assertEquals('255.255.255.255', g('\xff\xff\xff\xff')) | |
120 | |
121 self.assertEquals('100::', f('\x01' + '\x00' * 15)) | |
122 self.assertEquals('100::1', f('\x01' + '\x00' * 14 + '\x01')) | |
123 | |
124 def testPToN(self): | |
125 from twisted.python.compat import inet_pton | |
126 | |
127 f = lambda a: inet_pton(socket.AF_INET6, a) | |
128 g = lambda a: inet_pton(socket.AF_INET, a) | |
129 | |
130 self.assertEquals('\x00\x00\x00\x00', g('0.0.0.0')) | |
131 self.assertEquals('\xff\x00\xff\x00', g('255.0.255.0')) | |
132 self.assertEquals('\xaa\xaa\xaa\xaa', g('170.170.170.170')) | |
133 | |
134 self.assertEquals('\x00' * 16, f('::')) | |
135 self.assertEquals('\x00' * 16, f('0::0')) | |
136 self.assertEquals('\x00\x01' + '\x00' * 14, f('1::')) | |
137 self.assertEquals( | |
138 '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', | |
139 f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')) | |
140 | |
141 self.assertEquals('\x00' * 14 + '\x00\x01', f('::1')) | |
142 self.assertEquals('\x00' * 12 + '\x01\x02\x03\x04', f('::1.2.3.4')) | |
143 self.assertEquals( | |
144 '\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x01\x02\x03\xff', | |
145 f('1:2:3:4:5:6:1.2.3.255')) | |
146 | |
147 for badaddr in ['1:2:3:4:5:6:7:8:', ':1:2:3:4:5:6:7:8', '1::2::3', | |
148 '1:::3', ':::', '1:2', '::1.2', '1.2.3.4::', | |
149 'abcd:1.2.3.4:abcd:abcd:abcd:abcd:abcd', | |
150 '1234:1.2.3.4:1234:1234:1234:1234:1234:1234', | |
151 '1.2.3.4']: | |
152 self.assertRaises(ValueError, f, badaddr) | |
153 | |
154 def test_set(self): | |
155 """ | |
156 L{set} should behave like the expected set interface. | |
157 """ | |
158 a = set() | |
159 a.add('b') | |
160 a.add('c') | |
161 a.add('a') | |
162 b = list(a) | |
163 b.sort() | |
164 self.assertEquals(b, ['a', 'b', 'c']) | |
165 a.remove('b') | |
166 b = list(a) | |
167 b.sort() | |
168 self.assertEquals(b, ['a', 'c']) | |
169 | |
170 a.discard('d') | |
171 | |
172 b = set(['r', 's']) | |
173 d = a.union(b) | |
174 b = list(d) | |
175 b.sort() | |
176 self.assertEquals(b, ['a', 'c', 'r', 's']) | |
177 | |
178 | |
179 def test_frozenset(self): | |
180 """ | |
181 L{frozenset} should behave like the expected frozenset interface. | |
182 """ | |
183 a = frozenset(['a', 'b']) | |
184 self.assertRaises(AttributeError, getattr, a, "add") | |
185 self.assertEquals(list(a), ['a', 'b']) | |
186 | |
187 b = frozenset(['r', 's']) | |
188 d = a.union(b) | |
189 b = list(d) | |
190 b.sort() | |
191 self.assertEquals(b, ['a', 'b', 'r', 's']) | |
192 | |
OLD | NEW |