Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_operator.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | a7ec0a1b0f7c |
| children | 97834382c6cc |
| rev | line source |
|---|---|
| 4777 | 1 import operator |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
2 import unittest |
| 4777 | 3 |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45810
diff
changeset
|
4 from test import support |
| 4777 | 5 |
| 36150 | 6 class Seq1: |
| 7 def __init__(self, lst): | |
| 8 self.lst = lst | |
| 9 def __len__(self): | |
| 10 return len(self.lst) | |
| 11 def __getitem__(self, i): | |
| 12 return self.lst[i] | |
| 13 def __add__(self, other): | |
| 14 return self.lst + other.lst | |
| 15 def __mul__(self, other): | |
| 16 return self.lst * other | |
| 17 def __rmul__(self, other): | |
| 18 return other * self.lst | |
| 19 | |
| 20 class Seq2(object): | |
| 21 def __init__(self, lst): | |
| 22 self.lst = lst | |
| 23 def __len__(self): | |
| 24 return len(self.lst) | |
| 25 def __getitem__(self, i): | |
| 26 return self.lst[i] | |
| 27 def __add__(self, other): | |
| 28 return self.lst + other.lst | |
| 29 def __mul__(self, other): | |
| 30 return self.lst * other | |
| 31 def __rmul__(self, other): | |
| 32 return other * self.lst | |
| 33 | |
| 4777 | 34 |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
35 class OperatorTestCase(unittest.TestCase): |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
36 def test_lt(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
37 self.assertRaises(TypeError, operator.lt) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
38 self.assertRaises(TypeError, operator.lt, 1j, 2j) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
39 self.assertFalse(operator.lt(1, 0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
40 self.assertFalse(operator.lt(1, 0.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
41 self.assertFalse(operator.lt(1, 1)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
42 self.assertFalse(operator.lt(1, 1.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
43 self.assertTrue(operator.lt(1, 2)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
44 self.assertTrue(operator.lt(1, 2.0)) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
45 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
46 def test_le(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
47 self.assertRaises(TypeError, operator.le) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
48 self.assertRaises(TypeError, operator.le, 1j, 2j) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
49 self.assertFalse(operator.le(1, 0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
50 self.assertFalse(operator.le(1, 0.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
51 self.assertTrue(operator.le(1, 1)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
52 self.assertTrue(operator.le(1, 1.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
53 self.assertTrue(operator.le(1, 2)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
54 self.assertTrue(operator.le(1, 2.0)) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
55 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
56 def test_eq(self): |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
57 class C(object): |
|
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
58 def __eq__(self, other): |
|
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
59 raise SyntaxError |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
60 self.assertRaises(TypeError, operator.eq) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
61 self.assertRaises(SyntaxError, operator.eq, C(), C()) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
62 self.assertFalse(operator.eq(1, 0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
63 self.assertFalse(operator.eq(1, 0.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
64 self.assertTrue(operator.eq(1, 1)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
65 self.assertTrue(operator.eq(1, 1.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
66 self.assertFalse(operator.eq(1, 2)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
67 self.assertFalse(operator.eq(1, 2.0)) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
68 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
69 def test_ne(self): |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
70 class C(object): |
|
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
71 def __ne__(self, other): |
|
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
72 raise SyntaxError |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
73 self.assertRaises(TypeError, operator.ne) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
74 self.assertRaises(SyntaxError, operator.ne, C(), C()) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
75 self.assertTrue(operator.ne(1, 0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
76 self.assertTrue(operator.ne(1, 0.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
77 self.assertFalse(operator.ne(1, 1)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
78 self.assertFalse(operator.ne(1, 1.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
79 self.assertTrue(operator.ne(1, 2)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
80 self.assertTrue(operator.ne(1, 2.0)) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
81 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
82 def test_ge(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
83 self.assertRaises(TypeError, operator.ge) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
84 self.assertRaises(TypeError, operator.ge, 1j, 2j) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
85 self.assertTrue(operator.ge(1, 0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
86 self.assertTrue(operator.ge(1, 0.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
87 self.assertTrue(operator.ge(1, 1)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
88 self.assertTrue(operator.ge(1, 1.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
89 self.assertFalse(operator.ge(1, 2)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
90 self.assertFalse(operator.ge(1, 2.0)) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
91 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
92 def test_gt(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
93 self.assertRaises(TypeError, operator.gt) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
94 self.assertRaises(TypeError, operator.gt, 1j, 2j) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
95 self.assertTrue(operator.gt(1, 0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
96 self.assertTrue(operator.gt(1, 0.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
97 self.assertFalse(operator.gt(1, 1)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
98 self.assertFalse(operator.gt(1, 1.0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
99 self.assertFalse(operator.gt(1, 2)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
100 self.assertFalse(operator.gt(1, 2.0)) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
101 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
102 def test_abs(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
103 self.assertRaises(TypeError, operator.abs) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
104 self.assertRaises(TypeError, operator.abs, None) |
|
42188
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
105 self.assertEqual(operator.abs(-1), 1) |
|
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
106 self.assertEqual(operator.abs(1), 1) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
107 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
108 def test_add(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
109 self.assertRaises(TypeError, operator.add) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
110 self.assertRaises(TypeError, operator.add, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
111 self.assertTrue(operator.add(3, 4) == 7) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
112 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
113 def test_bitwise_and(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
114 self.assertRaises(TypeError, operator.and_) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
115 self.assertRaises(TypeError, operator.and_, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
116 self.assertTrue(operator.and_(0xf, 0xa) == 0xa) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
117 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
118 def test_concat(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
119 self.assertRaises(TypeError, operator.concat) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
120 self.assertRaises(TypeError, operator.concat, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
121 self.assertTrue(operator.concat('py', 'thon') == 'python') |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
122 self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4]) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
123 self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7]) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
124 self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7]) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
125 self.assertRaises(TypeError, operator.concat, 13, 29) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
126 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
127 def test_countOf(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
128 self.assertRaises(TypeError, operator.countOf) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
129 self.assertRaises(TypeError, operator.countOf, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
130 self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
131 self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
132 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
133 def test_delitem(self): |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
134 a = [4, 3, 2, 1] |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
135 self.assertRaises(TypeError, operator.delitem, a) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
136 self.assertRaises(TypeError, operator.delitem, a, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
137 self.assertTrue(operator.delitem(a, 1) is None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
138 self.assertTrue(a == [4, 2, 1]) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
139 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
140 def test_floordiv(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
141 self.assertRaises(TypeError, operator.floordiv, 5) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
142 self.assertRaises(TypeError, operator.floordiv, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
143 self.assertTrue(operator.floordiv(5, 2) == 2) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
144 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
145 def test_truediv(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
146 self.assertRaises(TypeError, operator.truediv, 5) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
147 self.assertRaises(TypeError, operator.truediv, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
148 self.assertTrue(operator.truediv(5, 2) == 2.5) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
149 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
150 def test_getitem(self): |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
151 a = range(10) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
152 self.assertRaises(TypeError, operator.getitem) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
153 self.assertRaises(TypeError, operator.getitem, a, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
154 self.assertTrue(operator.getitem(a, 2) == 2) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
155 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
156 def test_indexOf(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
157 self.assertRaises(TypeError, operator.indexOf) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
158 self.assertRaises(TypeError, operator.indexOf, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
159 self.assertTrue(operator.indexOf([4, 3, 2, 1], 3) == 1) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
160 self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0) |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
161 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
162 def test_invert(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
163 self.assertRaises(TypeError, operator.invert) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
164 self.assertRaises(TypeError, operator.invert, None) |
|
42188
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
165 self.assertEqual(operator.inv(4), -5) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
166 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
167 def test_lshift(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
168 self.assertRaises(TypeError, operator.lshift) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
169 self.assertRaises(TypeError, operator.lshift, None, 42) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
170 self.assertTrue(operator.lshift(5, 1) == 10) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
171 self.assertTrue(operator.lshift(5, 0) == 5) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
172 self.assertRaises(ValueError, operator.lshift, 2, -1) |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
173 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
174 def test_mod(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
175 self.assertRaises(TypeError, operator.mod) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
176 self.assertRaises(TypeError, operator.mod, None, 42) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
177 self.assertTrue(operator.mod(5, 2) == 1) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
178 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
179 def test_mul(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
180 self.assertRaises(TypeError, operator.mul) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
181 self.assertRaises(TypeError, operator.mul, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
182 self.assertTrue(operator.mul(5, 2) == 10) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
183 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
184 def test_neg(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
185 self.assertRaises(TypeError, operator.neg) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
186 self.assertRaises(TypeError, operator.neg, None) |
|
42188
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
187 self.assertEqual(operator.neg(5), -5) |
|
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
188 self.assertEqual(operator.neg(-5), 5) |
|
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
189 self.assertEqual(operator.neg(0), 0) |
|
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
190 self.assertEqual(operator.neg(-0), 0) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
191 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
192 def test_bitwise_or(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
193 self.assertRaises(TypeError, operator.or_) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
194 self.assertRaises(TypeError, operator.or_, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
195 self.assertTrue(operator.or_(0xa, 0x5) == 0xf) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
196 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
197 def test_pos(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
198 self.assertRaises(TypeError, operator.pos) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
199 self.assertRaises(TypeError, operator.pos, None) |
|
42188
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
200 self.assertEqual(operator.pos(5), 5) |
|
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
201 self.assertEqual(operator.pos(-5), -5) |
|
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
202 self.assertEqual(operator.pos(0), 0) |
|
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
203 self.assertEqual(operator.pos(-0), 0) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
204 |
|
24887
d797cfa8ca0d
Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
205 def test_pow(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
206 self.assertRaises(TypeError, operator.pow) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
207 self.assertRaises(TypeError, operator.pow, None, None) |
|
42188
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
208 self.assertEqual(operator.pow(3,5), 3**5) |
|
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
209 self.assertEqual(operator.__pow__(3,5), 3**5) |
|
24887
d797cfa8ca0d
Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
210 self.assertRaises(TypeError, operator.pow, 1) |
|
d797cfa8ca0d
Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
211 self.assertRaises(TypeError, operator.pow, 1, 2, 3) |
|
d797cfa8ca0d
Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
212 |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
213 def test_rshift(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
214 self.assertRaises(TypeError, operator.rshift) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
215 self.assertRaises(TypeError, operator.rshift, None, 42) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
216 self.assertTrue(operator.rshift(5, 1) == 2) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
217 self.assertTrue(operator.rshift(5, 0) == 5) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
218 self.assertRaises(ValueError, operator.rshift, 2, -1) |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
219 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
220 def test_contains(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
221 self.assertRaises(TypeError, operator.contains) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
222 self.assertRaises(TypeError, operator.contains, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
223 self.assertTrue(operator.contains(range(4), 2)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
224 self.assertFalse(operator.contains(range(4), 5)) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
225 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
226 def test_setitem(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
227 a = list(range(3)) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
228 self.assertRaises(TypeError, operator.setitem, a) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
229 self.assertRaises(TypeError, operator.setitem, a, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
230 self.assertTrue(operator.setitem(a, 0, 2) is None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
231 self.assertTrue(a == [2, 1, 2]) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
232 self.assertRaises(IndexError, operator.setitem, a, 4, 2) |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
233 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
234 def test_sub(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
235 self.assertRaises(TypeError, operator.sub) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
236 self.assertRaises(TypeError, operator.sub, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
237 self.assertTrue(operator.sub(5, 2) == 3) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
238 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
239 def test_truth(self): |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
240 class C(object): |
|
40401
88133417dfd0
- patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents:
37191
diff
changeset
|
241 def __bool__(self): |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
242 raise SyntaxError |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
243 self.assertRaises(TypeError, operator.truth) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
244 self.assertRaises(SyntaxError, operator.truth, C()) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
245 self.assertTrue(operator.truth(5)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
246 self.assertTrue(operator.truth([0])) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
247 self.assertFalse(operator.truth(0)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
248 self.assertFalse(operator.truth([])) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
249 |
|
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
250 def test_bitwise_xor(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
251 self.assertRaises(TypeError, operator.xor) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
252 self.assertRaises(TypeError, operator.xor, None, None) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
253 self.assertTrue(operator.xor(0xb, 0xc) == 0x7) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
254 |
|
27290
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
24887
diff
changeset
|
255 def test_is(self): |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
24887
diff
changeset
|
256 a = b = 'xyzpdq' |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
24887
diff
changeset
|
257 c = a[:3] + b[3:] |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
258 self.assertRaises(TypeError, operator.is_) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
259 self.assertTrue(operator.is_(a, b)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
260 self.assertFalse(operator.is_(a,c)) |
|
27290
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
24887
diff
changeset
|
261 |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
24887
diff
changeset
|
262 def test_is_not(self): |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
24887
diff
changeset
|
263 a = b = 'xyzpdq' |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
24887
diff
changeset
|
264 c = a[:3] + b[3:] |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
265 self.assertRaises(TypeError, operator.is_not) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
266 self.assertFalse(operator.is_not(a, b)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
267 self.assertTrue(operator.is_not(a,c)) |
|
19163
b160fa63f40c
Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents:
19112
diff
changeset
|
268 |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
269 def test_attrgetter(self): |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
270 class A: |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
271 pass |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
272 a = A() |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
273 a.name = 'arthur' |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
274 f = operator.attrgetter('name') |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
275 self.assertEqual(f(a), 'arthur') |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
276 f = operator.attrgetter('rank') |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
277 self.assertRaises(AttributeError, f, a) |
|
65818
4127ab1f6338
Issue #10160: Speed up operator.attrgetter. Patch by Christos Georgiou.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
278 self.assertRaises(TypeError, operator.attrgetter, 2) |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
279 self.assertRaises(TypeError, operator.attrgetter) |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
280 |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
281 # multiple gets |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
282 record = A() |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
283 record.x = 'X' |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
284 record.y = 'Y' |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
285 record.z = 'Z' |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
286 self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y')) |
|
65818
4127ab1f6338
Issue #10160: Speed up operator.attrgetter. Patch by Christos Georgiou.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
287 self.assertRaises(TypeError, operator.attrgetter, ('x', (), 'y')) |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
288 |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
289 class C(object): |
|
44828
ef7d72d98485
Merged revisions 60124-60142 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42188
diff
changeset
|
290 def __getattr__(self, name): |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
291 raise SyntaxError |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
292 self.assertRaises(SyntaxError, operator.attrgetter('foo'), C()) |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
293 |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
294 # recursive gets |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
295 a = A() |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
296 a.name = 'arthur' |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
297 a.child = A() |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
298 a.child.name = 'thomas' |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
299 f = operator.attrgetter('child.name') |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
300 self.assertEqual(f(a), 'thomas') |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
301 self.assertRaises(AttributeError, f, a.child) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
302 f = operator.attrgetter('name', 'child.name') |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
303 self.assertEqual(f(a), ('arthur', 'thomas')) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
304 f = operator.attrgetter('name', 'child.name', 'child.child.name') |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
305 self.assertRaises(AttributeError, f, a) |
|
65818
4127ab1f6338
Issue #10160: Speed up operator.attrgetter. Patch by Christos Georgiou.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
306 f = operator.attrgetter('child.') |
|
4127ab1f6338
Issue #10160: Speed up operator.attrgetter. Patch by Christos Georgiou.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
307 self.assertRaises(AttributeError, f, a) |
|
4127ab1f6338
Issue #10160: Speed up operator.attrgetter. Patch by Christos Georgiou.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
308 f = operator.attrgetter('.child') |
|
4127ab1f6338
Issue #10160: Speed up operator.attrgetter. Patch by Christos Georgiou.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
309 self.assertRaises(AttributeError, f, a) |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
310 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
311 a.child.child = A() |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
312 a.child.child.name = 'johnson' |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
313 f = operator.attrgetter('child.child.name') |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
314 self.assertEqual(f(a), 'johnson') |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
315 f = operator.attrgetter('name', 'child.name', 'child.child.name') |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
316 self.assertEqual(f(a), ('arthur', 'thomas', 'johnson')) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
317 |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
318 def test_itemgetter(self): |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
319 a = 'ABCDE' |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
320 f = operator.itemgetter(2) |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
321 self.assertEqual(f(a), 'C') |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
322 f = operator.itemgetter(10) |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
323 self.assertRaises(IndexError, f, a) |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
324 |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
325 class C(object): |
|
44828
ef7d72d98485
Merged revisions 60124-60142 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42188
diff
changeset
|
326 def __getitem__(self, name): |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
327 raise SyntaxError |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51064
diff
changeset
|
328 self.assertRaises(SyntaxError, operator.itemgetter(42), C()) |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
31210
diff
changeset
|
329 |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
330 f = operator.itemgetter('name') |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
331 self.assertRaises(TypeError, f, a) |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
332 self.assertRaises(TypeError, operator.itemgetter) |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
333 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
334 d = dict(key='val') |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
335 f = operator.itemgetter('key') |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
336 self.assertEqual(f(d), 'val') |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
337 f = operator.itemgetter('nonkey') |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
338 self.assertRaises(KeyError, f, d) |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
339 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
340 # example used in the docs |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
341 inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
342 getcount = operator.itemgetter(1) |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
343 self.assertEqual(list(map(getcount, inventory)), [3, 2, 5, 1]) |
|
31210
c06b570adf12
Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents:
31052
diff
changeset
|
344 self.assertEqual(sorted(inventory, key=getcount), |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
345 [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]) |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
346 |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
347 # multiple gets |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
348 data = list(map(str, range(20))) |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
349 self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5')) |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
350 self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data) |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19694
diff
changeset
|
351 |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
352 def test_methodcaller(self): |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
353 self.assertRaises(TypeError, operator.methodcaller) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
354 class A: |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
355 def foo(self, *args, **kwds): |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
356 return args[0] + args[1] |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
357 def bar(self, f=42): |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
358 return f |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
359 a = A() |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
360 f = operator.methodcaller('foo') |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
361 self.assertRaises(IndexError, f, a) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
362 f = operator.methodcaller('foo', 1, 2) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
363 self.assertEqual(f(a), 3) |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
364 f = operator.methodcaller('bar') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
365 self.assertEqual(f(a), 42) |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
366 self.assertRaises(TypeError, f, a, a) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
367 f = operator.methodcaller('bar', f=5) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
368 self.assertEqual(f(a), 5) |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44828
diff
changeset
|
369 |
|
36152
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
370 def test_inplace(self): |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
371 class C(object): |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
372 def __iadd__ (self, other): return "iadd" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
373 def __iand__ (self, other): return "iand" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
374 def __ifloordiv__(self, other): return "ifloordiv" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
375 def __ilshift__ (self, other): return "ilshift" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
376 def __imod__ (self, other): return "imod" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
377 def __imul__ (self, other): return "imul" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
378 def __ior__ (self, other): return "ior" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
379 def __ipow__ (self, other): return "ipow" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
380 def __irshift__ (self, other): return "irshift" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
381 def __isub__ (self, other): return "isub" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
382 def __itruediv__ (self, other): return "itruediv" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
383 def __ixor__ (self, other): return "ixor" |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
384 def __getitem__(self, other): return 5 # so that C is a sequence |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
385 c = C() |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
386 self.assertEqual(operator.iadd (c, 5), "iadd") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
387 self.assertEqual(operator.iand (c, 5), "iand") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
388 self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
389 self.assertEqual(operator.ilshift (c, 5), "ilshift") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
390 self.assertEqual(operator.imod (c, 5), "imod") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
391 self.assertEqual(operator.imul (c, 5), "imul") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
392 self.assertEqual(operator.ior (c, 5), "ior") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
393 self.assertEqual(operator.ipow (c, 5), "ipow") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
394 self.assertEqual(operator.irshift (c, 5), "irshift") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
395 self.assertEqual(operator.isub (c, 5), "isub") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
396 self.assertEqual(operator.itruediv (c, 5), "itruediv") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
397 self.assertEqual(operator.ixor (c, 5), "ixor") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
398 self.assertEqual(operator.iconcat (c, c), "iadd") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
399 self.assertEqual(operator.__iadd__ (c, 5), "iadd") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
400 self.assertEqual(operator.__iand__ (c, 5), "iand") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
401 self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
402 self.assertEqual(operator.__ilshift__ (c, 5), "ilshift") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
403 self.assertEqual(operator.__imod__ (c, 5), "imod") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
404 self.assertEqual(operator.__imul__ (c, 5), "imul") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
405 self.assertEqual(operator.__ior__ (c, 5), "ior") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
406 self.assertEqual(operator.__ipow__ (c, 5), "ipow") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
407 self.assertEqual(operator.__irshift__ (c, 5), "irshift") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
408 self.assertEqual(operator.__isub__ (c, 5), "isub") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
409 self.assertEqual(operator.__itruediv__ (c, 5), "itruediv") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
410 self.assertEqual(operator.__ixor__ (c, 5), "ixor") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36150
diff
changeset
|
411 self.assertEqual(operator.__iconcat__ (c, c), "iadd") |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19694
diff
changeset
|
412 |
|
79511
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
413 def test_length_hint(self): |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
414 class X(object): |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
415 def __init__(self, value): |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
416 self.value = value |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
417 |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
418 def __length_hint__(self): |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
419 if type(self.value) is type: |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
420 raise self.value |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
421 else: |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
422 return self.value |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
423 |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
424 self.assertEqual(operator.length_hint([], 2), 0) |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
425 self.assertEqual(operator.length_hint(iter([1, 2, 3])), 3) |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
426 |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
427 self.assertEqual(operator.length_hint(X(2)), 2) |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
428 self.assertEqual(operator.length_hint(X(NotImplemented), 4), 4) |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
429 self.assertEqual(operator.length_hint(X(TypeError), 12), 12) |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
430 with self.assertRaises(TypeError): |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
431 operator.length_hint(X("abc")) |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
432 with self.assertRaises(ValueError): |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
433 operator.length_hint(X(-2)) |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
434 with self.assertRaises(LookupError): |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
435 operator.length_hint(X(LookupError)) |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
436 |
|
a7ec0a1b0f7c
Issue #16148: implemented PEP 424
Armin Ronacher <armin.ronacher@active-4.com>
parents:
68257
diff
changeset
|
437 |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
438 def test_main(verbose=None): |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
439 import sys |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
440 test_classes = ( |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
441 OperatorTestCase, |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
442 ) |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
443 |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45810
diff
changeset
|
444 support.run_unittest(*test_classes) |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
445 |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
446 # verify reference counting |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
447 if verbose and hasattr(sys, "gettotalrefcount"): |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
448 import gc |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
449 counts = [None] * 5 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
450 for i in range(len(counts)): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45810
diff
changeset
|
451 support.run_unittest(*test_classes) |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
452 gc.collect() |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
453 counts[i] = sys.gettotalrefcount() |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
454 print(counts) |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
455 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19694
diff
changeset
|
456 if __name__ == "__main__": |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
33877
diff
changeset
|
457 test_main(verbose=True) |
