annotate Lib/test/test_operator.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4777
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
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
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
3
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
4 from test import test_support
4777
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
5
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
6 class Seq1:
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
7 def __init__(self, lst):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
8 self.lst = lst
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
9 def __len__(self):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
10 return len(self.lst)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
11 def __getitem__(self, i):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
12 return self.lst[i]
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
13 def __add__(self, other):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
14 return self.lst + other.lst
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
15 def __mul__(self, other):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
16 return self.lst * other
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
17 def __rmul__(self, other):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
18 return other * self.lst
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
19
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
20 class Seq2(object):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
21 def __init__(self, lst):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
22 self.lst = lst
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
23 def __len__(self):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
24 return len(self.lst)
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
25 def __getitem__(self, i):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
26 return self.lst[i]
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
27 def __add__(self, other):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
28 return self.lst + other.lst
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
29 def __mul__(self, other):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
30 return self.lst * other
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
31 def __rmul__(self, other):
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
32 return other * self.lst
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
33
4777
50d6dc458aa2 Test of the operator module
Barry Warsaw <barry@python.org>
parents:
diff changeset
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):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
37 self.assertRaises(TypeError, operator.lt)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
38 self.assertRaises(TypeError, operator.lt, 1j, 2j)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
39 self.assertFalse(operator.lt(1, 0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
40 self.assertFalse(operator.lt(1, 0.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
41 self.assertFalse(operator.lt(1, 1))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
42 self.assertFalse(operator.lt(1, 1.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
43 self.assertTrue(operator.lt(1, 2))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
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):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
47 self.assertRaises(TypeError, operator.le)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
48 self.assertRaises(TypeError, operator.le, 1j, 2j)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
49 self.assertFalse(operator.le(1, 0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
50 self.assertFalse(operator.le(1, 0.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
51 self.assertTrue(operator.le(1, 1))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
52 self.assertTrue(operator.le(1, 1.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
53 self.assertTrue(operator.le(1, 2))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
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
48213
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 45488
diff changeset
60 __hash__ = None # Silence Py3k warning
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
61 self.assertRaises(TypeError, operator.eq)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
62 self.assertRaises(SyntaxError, operator.eq, C(), C())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
63 self.assertFalse(operator.eq(1, 0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
64 self.assertFalse(operator.eq(1, 0.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
65 self.assertTrue(operator.eq(1, 1))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
66 self.assertTrue(operator.eq(1, 1.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
67 self.assertFalse(operator.eq(1, 2))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
68 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
69
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
70 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
71 class C(object):
a8c979cc4fa6 Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents: 31210
diff changeset
72 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
73 raise SyntaxError
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
74 self.assertRaises(TypeError, operator.ne)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
75 self.assertRaises(SyntaxError, operator.ne, C(), C())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
76 self.assertTrue(operator.ne(1, 0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
77 self.assertTrue(operator.ne(1, 0.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
78 self.assertFalse(operator.ne(1, 1))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
79 self.assertFalse(operator.ne(1, 1.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
80 self.assertTrue(operator.ne(1, 2))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
81 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
82
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
83 def test_ge(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
84 self.assertRaises(TypeError, operator.ge)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
85 self.assertRaises(TypeError, operator.ge, 1j, 2j)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
86 self.assertTrue(operator.ge(1, 0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
87 self.assertTrue(operator.ge(1, 0.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
88 self.assertTrue(operator.ge(1, 1))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
89 self.assertTrue(operator.ge(1, 1.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
90 self.assertFalse(operator.ge(1, 2))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
91 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
92
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
93 def test_gt(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
94 self.assertRaises(TypeError, operator.gt)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
95 self.assertRaises(TypeError, operator.gt, 1j, 2j)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
96 self.assertTrue(operator.gt(1, 0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
97 self.assertTrue(operator.gt(1, 0.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
98 self.assertFalse(operator.gt(1, 1))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
99 self.assertFalse(operator.gt(1, 1.0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
100 self.assertFalse(operator.gt(1, 2))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
101 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
102
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
103 def test_abs(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
104 self.assertRaises(TypeError, operator.abs)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
105 self.assertRaises(TypeError, operator.abs, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
106 self.assertTrue(operator.abs(-1) == 1)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
107 self.assertTrue(operator.abs(1) == 1)
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
108
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
109 def test_add(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
110 self.assertRaises(TypeError, operator.add)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
111 self.assertRaises(TypeError, operator.add, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
112 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
113
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
114 def test_bitwise_and(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
115 self.assertRaises(TypeError, operator.and_)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
116 self.assertRaises(TypeError, operator.and_, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
117 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
118
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
119 def test_concat(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
120 self.assertRaises(TypeError, operator.concat)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
121 self.assertRaises(TypeError, operator.concat, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
122 self.assertTrue(operator.concat('py', 'thon') == 'python')
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
123 self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
124 self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
125 self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
126 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
127
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
128 def test_countOf(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
129 self.assertRaises(TypeError, operator.countOf)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
130 self.assertRaises(TypeError, operator.countOf, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
131 self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
132 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
133
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
134 def test_delitem(self):
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
135 a = [4, 3, 2, 1]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
136 self.assertRaises(TypeError, operator.delitem, a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
137 self.assertRaises(TypeError, operator.delitem, a, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
138 self.assertTrue(operator.delitem(a, 1) is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
139 self.assertTrue(a == [4, 2, 1])
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
140
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
141 def test_delslice(self):
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
142 a = range(10)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
143 self.assertRaises(TypeError, operator.delslice, a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
144 self.assertRaises(TypeError, operator.delslice, a, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
145 self.assertTrue(operator.delslice(a, 2, 8) is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
146 self.assertTrue(a == [0, 1, 8, 9])
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 40762
diff changeset
147 operator.delslice(a, 0, test_support.MAX_Py_ssize_t)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
148 self.assertTrue(a == [])
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_div(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
151 self.assertRaises(TypeError, operator.div, 5)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
152 self.assertRaises(TypeError, operator.div, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
153 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
154
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
155 def test_floordiv(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
156 self.assertRaises(TypeError, operator.floordiv, 5)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
157 self.assertRaises(TypeError, operator.floordiv, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
158 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
159
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
160 def test_truediv(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
161 self.assertRaises(TypeError, operator.truediv, 5)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
162 self.assertRaises(TypeError, operator.truediv, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
163 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
164
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
165 def test_getitem(self):
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
166 a = range(10)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
167 self.assertRaises(TypeError, operator.getitem)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
168 self.assertRaises(TypeError, operator.getitem, a, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
169 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
170
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
171 def test_getslice(self):
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
172 a = range(10)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
173 self.assertRaises(TypeError, operator.getslice)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
174 self.assertRaises(TypeError, operator.getslice, a, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
175 self.assertTrue(operator.getslice(a, 4, 6) == [4, 5])
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 40762
diff changeset
176 b = operator.getslice(a, 0, test_support.MAX_Py_ssize_t)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
177 self.assertTrue(b == a)
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_indexOf(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
180 self.assertRaises(TypeError, operator.indexOf)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
181 self.assertRaises(TypeError, operator.indexOf, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
182 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
183 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
184
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
185 def test_invert(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
186 self.assertRaises(TypeError, operator.invert)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
187 self.assertRaises(TypeError, operator.invert, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
188 self.assertTrue(operator.inv(4) == -5)
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
189
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
190 def test_isCallable(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
191 self.assertRaises(TypeError, operator.isCallable)
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
192 class C:
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
193 pass
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
194 def check(self, o, v):
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58198
diff changeset
195 with test_support.check_py3k_warnings():
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58198
diff changeset
196 self.assertEqual(operator.isCallable(o), v)
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
197 self.assertEqual(callable(o), v)
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
198 check(self, 4, 0)
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
199 check(self, operator.isCallable, 1)
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
200 check(self, C, 1)
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
201 check(self, C(), 0)
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
202
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
203 def test_isMappingType(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
204 self.assertRaises(TypeError, operator.isMappingType)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
205 self.assertFalse(operator.isMappingType(1))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
206 self.assertFalse(operator.isMappingType(operator.isMappingType))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
207 self.assertTrue(operator.isMappingType(operator.__dict__))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
208 self.assertTrue(operator.isMappingType({}))
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
209
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
210 def test_isNumberType(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
211 self.assertRaises(TypeError, operator.isNumberType)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
212 self.assertTrue(operator.isNumberType(8))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
213 self.assertTrue(operator.isNumberType(8j))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
214 self.assertTrue(operator.isNumberType(8L))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
215 self.assertTrue(operator.isNumberType(8.3))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
216 self.assertFalse(operator.isNumberType(dir()))
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
217
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
218 def test_isSequenceType(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
219 self.assertRaises(TypeError, operator.isSequenceType)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
220 self.assertTrue(operator.isSequenceType(dir()))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
221 self.assertTrue(operator.isSequenceType(()))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
222 self.assertTrue(operator.isSequenceType(xrange(10)))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
223 self.assertTrue(operator.isSequenceType('yeahbuddy'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
224 self.assertFalse(operator.isSequenceType(3))
40762
ffaaae02c662 Bug #1575169: operator.isSequenceType() now returns False for subclasses of dict.
Raymond Hettinger <python@rcn.com>
parents: 36152
diff changeset
225 class Dict(dict): pass
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
226 self.assertFalse(operator.isSequenceType(Dict()))
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
227
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
228 def test_lshift(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
229 self.assertRaises(TypeError, operator.lshift)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
230 self.assertRaises(TypeError, operator.lshift, None, 42)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
231 self.assertTrue(operator.lshift(5, 1) == 10)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
232 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
233 self.assertRaises(ValueError, operator.lshift, 2, -1)
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
234
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
235 def test_mod(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
236 self.assertRaises(TypeError, operator.mod)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
237 self.assertRaises(TypeError, operator.mod, None, 42)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
238 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
239
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
240 def test_mul(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
241 self.assertRaises(TypeError, operator.mul)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
242 self.assertRaises(TypeError, operator.mul, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
243 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
244
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
245 def test_neg(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
246 self.assertRaises(TypeError, operator.neg)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
247 self.assertRaises(TypeError, operator.neg, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
248 self.assertTrue(operator.neg(5) == -5)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
249 self.assertTrue(operator.neg(-5) == 5)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
250 self.assertTrue(operator.neg(0) == 0)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
251 self.assertTrue(operator.neg(-0) == 0)
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
252
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
253 def test_bitwise_or(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
254 self.assertRaises(TypeError, operator.or_)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
255 self.assertRaises(TypeError, operator.or_, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
256 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
257
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
258 def test_pos(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
259 self.assertRaises(TypeError, operator.pos)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
260 self.assertRaises(TypeError, operator.pos, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
261 self.assertTrue(operator.pos(5) == 5)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
262 self.assertTrue(operator.pos(-5) == -5)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
263 self.assertTrue(operator.pos(0) == 0)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
264 self.assertTrue(operator.pos(-0) == 0)
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
265
24887
d797cfa8ca0d Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
266 def test_pow(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
267 self.assertRaises(TypeError, operator.pow)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
268 self.assertRaises(TypeError, operator.pow, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
269 self.assertTrue(operator.pow(3,5) == 3**5)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
270 self.assertTrue(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
271 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
272 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
273
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
274 def test_repeat(self):
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
275 a = range(3)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
276 self.assertRaises(TypeError, operator.repeat)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
277 self.assertRaises(TypeError, operator.repeat, a, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
278 self.assertTrue(operator.repeat(a, 2) == a+a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
279 self.assertTrue(operator.repeat(a, 1) == a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
280 self.assertTrue(operator.repeat(a, 0) == [])
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
281 a = (1, 2, 3)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
282 self.assertTrue(operator.repeat(a, 2) == a+a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
283 self.assertTrue(operator.repeat(a, 1) == a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
284 self.assertTrue(operator.repeat(a, 0) == ())
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
285 a = '123'
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
286 self.assertTrue(operator.repeat(a, 2) == a+a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
287 self.assertTrue(operator.repeat(a, 1) == a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
288 self.assertTrue(operator.repeat(a, 0) == '')
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
289 a = Seq1([4, 5, 6])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
290 self.assertTrue(operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
291 self.assertTrue(operator.repeat(a, 1) == [4, 5, 6])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
292 self.assertTrue(operator.repeat(a, 0) == [])
36150
2eb78f5d1450 SF patch #1390657:
Armin Rigo <arigo@tunes.org>
parents: 34716
diff changeset
293 a = Seq2([4, 5, 6])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
294 self.assertTrue(operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
295 self.assertTrue(operator.repeat(a, 1) == [4, 5, 6])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
296 self.assertTrue(operator.repeat(a, 0) == [])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
297 self.assertRaises(TypeError, operator.repeat, 6, 7)
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
298
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
299 def test_rshift(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
300 self.assertRaises(TypeError, operator.rshift)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
301 self.assertRaises(TypeError, operator.rshift, None, 42)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
302 self.assertTrue(operator.rshift(5, 1) == 2)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
303 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
304 self.assertRaises(ValueError, operator.rshift, 2, -1)
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
305
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
306 def test_contains(self):
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
307 self.assertRaises(TypeError, operator.contains)
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
308 self.assertRaises(TypeError, operator.contains, None, None)
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
309 self.assertTrue(operator.contains(range(4), 2))
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
310 self.assertFalse(operator.contains(range(4), 5))
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58198
diff changeset
311 with test_support.check_py3k_warnings():
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58198
diff changeset
312 self.assertTrue(operator.sequenceIncludes(range(4), 2))
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58198
diff changeset
313 self.assertFalse(operator.sequenceIncludes(range(4), 5))
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
314
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
315 def test_setitem(self):
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
316 a = range(3)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
317 self.assertRaises(TypeError, operator.setitem, a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
318 self.assertRaises(TypeError, operator.setitem, a, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
319 self.assertTrue(operator.setitem(a, 0, 2) is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
320 self.assertTrue(a == [2, 1, 2])
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
321 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
322
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
323 def test_setslice(self):
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
324 a = range(4)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
325 self.assertRaises(TypeError, operator.setslice, a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
326 self.assertRaises(TypeError, operator.setslice, a, None, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
327 self.assertTrue(operator.setslice(a, 1, 3, [2, 1]) is None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
328 self.assertTrue(a == [0, 2, 1, 3])
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 40762
diff changeset
329 operator.setslice(a, 0, test_support.MAX_Py_ssize_t, [])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
330 self.assertTrue(a == [])
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
331
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
332 def test_sub(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
333 self.assertRaises(TypeError, operator.sub)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
334 self.assertRaises(TypeError, operator.sub, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
335 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
336
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
337 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
338 class C(object):
a8c979cc4fa6 Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents: 31210
diff changeset
339 def __nonzero__(self):
a8c979cc4fa6 Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents: 31210
diff changeset
340 raise SyntaxError
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
341 self.assertRaises(TypeError, operator.truth)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
342 self.assertRaises(SyntaxError, operator.truth, C())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
343 self.assertTrue(operator.truth(5))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
344 self.assertTrue(operator.truth([0]))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
345 self.assertFalse(operator.truth(0))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
346 self.assertFalse(operator.truth([]))
19163
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
347
b160fa63f40c Added tests for rich comparison operator functions.
Fred Drake <fdrake@acm.org>
parents: 19112
diff changeset
348 def test_bitwise_xor(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
349 self.assertRaises(TypeError, operator.xor)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
350 self.assertRaises(TypeError, operator.xor, None, None)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
351 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
352
27290
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
353 def test_is(self):
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
354 a = b = 'xyzpdq'
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
355 c = a[:3] + b[3:]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
356 self.assertRaises(TypeError, operator.is_)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
357 self.assertTrue(operator.is_(a, b))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
358 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
359
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
360 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
361 a = b = 'xyzpdq'
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
362 c = a[:3] + b[3:]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
363 self.assertRaises(TypeError, operator.is_not)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
364 self.assertFalse(operator.is_not(a, b))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
365 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
366
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
367 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
368 class A:
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
369 pass
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
370 a = A()
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
371 a.name = 'arthur'
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
372 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
373 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
374 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
375 self.assertRaises(AttributeError, f, a)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
376 f = operator.attrgetter(2)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
377 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
378 self.assertRaises(TypeError, operator.attrgetter)
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
379
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
380 # multiple gets
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
381 record = A()
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
382 record.x = 'X'
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
383 record.y = 'Y'
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
384 record.z = 'Z'
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
385 self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y'))
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
386 self.assertRaises(TypeError, operator.attrgetter('x', (), 'y'), record)
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
387
33877
a8c979cc4fa6 Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents: 31210
diff changeset
388 class C(object):
44826
ea389a268b54 #1876: fix typos in test_operator.
Georg Brandl <georg@python.org>
parents: 41036
diff changeset
389 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
390 raise SyntaxError
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
391 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
392
45487
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
393 # recursive gets
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
394 a = A()
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
395 a.name = 'arthur'
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
396 a.child = A()
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
397 a.child.name = 'thomas'
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
398 f = operator.attrgetter('child.name')
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
399 self.assertEqual(f(a), 'thomas')
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
400 self.assertRaises(AttributeError, f, a.child)
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
401 f = operator.attrgetter('name', 'child.name')
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
402 self.assertEqual(f(a), ('arthur', 'thomas'))
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
403 f = operator.attrgetter('name', 'child.name', 'child.child.name')
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
404 self.assertRaises(AttributeError, f, a)
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
405
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
406 a.child.child = A()
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
407 a.child.child.name = 'johnson'
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
408 f = operator.attrgetter('child.child.name')
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
409 self.assertEqual(f(a), 'johnson')
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
410 f = operator.attrgetter('name', 'child.name', 'child.child.name')
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
411 self.assertEqual(f(a), ('arthur', 'thomas', 'johnson'))
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 44826
diff changeset
412
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
413 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
414 a = 'ABCDE'
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
415 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
416 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
417 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
418 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
419
33877
a8c979cc4fa6 Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents: 31210
diff changeset
420 class C(object):
44826
ea389a268b54 #1876: fix typos in test_operator.
Georg Brandl <georg@python.org>
parents: 41036
diff changeset
421 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
422 raise SyntaxError
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 48213
diff changeset
423 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
424
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
425 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
426 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
427 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
428
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
429 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
430 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
431 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
432 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
433 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
434
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
435 # 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
436 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
437 getcount = operator.itemgetter(1)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
438 self.assertEqual(map(getcount, inventory), [3, 2, 5, 1])
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31052
diff changeset
439 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
440 [('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
441
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
442 # multiple gets
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
443 data = map(str, range(20))
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
444 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
445 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
446
45488
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
447 def test_methodcaller(self):
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
448 self.assertRaises(TypeError, operator.methodcaller)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
449 class A:
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
450 def foo(self, *args, **kwds):
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
451 return args[0] + args[1]
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
452 def bar(self, f=42):
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
453 return f
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
454 a = A()
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
455 f = operator.methodcaller('foo')
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
456 self.assertRaises(IndexError, f, a)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
457 f = operator.methodcaller('foo', 1, 2)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
458 self.assertEqual(f(a), 3)
45488
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
459 f = operator.methodcaller('bar')
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
460 self.assertEqual(f(a), 42)
45488
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
461 self.assertRaises(TypeError, f, a, a)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
462 f = operator.methodcaller('bar', f=5)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59783
diff changeset
463 self.assertEqual(f(a), 5)
45488
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
464
36152
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
465 def test_inplace(self):
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
466 class C(object):
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
467 def __iadd__ (self, other): return "iadd"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
468 def __iand__ (self, other): return "iand"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
469 def __idiv__ (self, other): return "idiv"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
470 def __ifloordiv__(self, other): return "ifloordiv"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
471 def __ilshift__ (self, other): return "ilshift"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
472 def __imod__ (self, other): return "imod"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
473 def __imul__ (self, other): return "imul"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
474 def __ior__ (self, other): return "ior"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
475 def __ipow__ (self, other): return "ipow"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
476 def __irshift__ (self, other): return "irshift"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
477 def __isub__ (self, other): return "isub"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
478 def __itruediv__ (self, other): return "itruediv"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
479 def __ixor__ (self, other): return "ixor"
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
480 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
481 c = C()
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
482 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
483 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
484 self.assertEqual(operator.idiv (c, 5), "idiv")
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
485 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
486 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
487 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
488 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
489 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
490 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
491 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
492 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
493 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
494 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
495 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
496 self.assertEqual(operator.irepeat (c, 5), "imul")
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
497 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
498 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
499 self.assertEqual(operator.__idiv__ (c, 5), "idiv")
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36150
diff changeset
500 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
501 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
502 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
503 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
504 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
505 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
506 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
507 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
508 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
509 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
510 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
511 self.assertEqual(operator.__irepeat__ (c, 5), "imul")
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19694
diff changeset
512
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
513 def test_main(verbose=None):
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
514 import sys
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
515 test_classes = (
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
516 OperatorTestCase,
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
517 )
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
518
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
519 test_support.run_unittest(*test_classes)
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
520
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
521 # verify reference counting
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
522 if verbose and hasattr(sys, "gettotalrefcount"):
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
523 import gc
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
524 counts = [None] * 5
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
525 for i in xrange(len(counts)):
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
526 test_support.run_unittest(*test_classes)
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
527 gc.collect()
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
528 counts[i] = sys.gettotalrefcount()
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
529 print counts
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
530
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19694
diff changeset
531 if __name__ == "__main__":
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 33877
diff changeset
532 test_main(verbose=True)