forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_iterator.py
More file actions
273 lines (213 loc) · 6.67 KB
/
Copy pathtest_iterator.py
File metadata and controls
273 lines (213 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#####################################################################################
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# This source code is subject to terms and conditions of the Apache License, Version 2.0. A
# copy of the license can be found in the License.html file at the root of this distribution. If
# you cannot locate the Apache License, Version 2.0, please send an email to
# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
# by the terms of the Apache License, Version 2.0.
#
# You must not remove this notice, or any other, from this software.
#
#
#####################################################################################
from iptest.assert_util import *
class It:
x = 0
a = ()
def __init__(self, a):
self.x = 0
self.a = a
def next(self):
if self.x <= 9:
self.x = self.x+1
return self.a[self.x-1]
else:
raise StopIteration
def __iter__(self):
return self
class Iterator:
x = 0
a = (1,2,3,4,5,6,7,8,9,0)
def __iter__(self):
return It(self.a)
class Indexer:
a = (1,2,3,4,5,6,7,8,9,0)
def __getitem__(self, i):
if i < len(self.a):
return self.a[i]
else:
raise IndexError
i = Iterator()
for j in i:
Assert(j in i)
Assert(1 in i)
Assert(2 in i)
Assert(not (10 in i))
i = Indexer()
for j in i:
Assert(j in i)
Assert(1 in i)
Assert(2 in i)
Assert(not (10 in i))
# Testing the iter(o,s) function
class Iter:
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
index = -1
it = Iter()
def f():
it.index += 1
return it.x[it.index]
y = []
for i in iter(f, 14):
y.append(i)
Assert(y == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
y = ['1']
y += Iterator()
Assert(y == ['1', 1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
y = ['1']
y += Indexer()
Assert(y == ['1', 1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
AssertErrorWithMessages(TypeError, "iter() takes at least 1 argument (0 given)",
"iter expected at least 1 arguments, got 0", iter)
def test_itertools_same_value():
from itertools import izip
x = iter(range(4))
AreEqual([(i,j) for i,j in izip(x,x)], [(0, 1), (2, 3)])
def test_itertools_islice_end():
"""islice shouldn't consume values after the limit specified by step"""
from itertools import izip, islice
# create a zipped iterator w/ odd number of values...
it = izip([2,3,4], [4,5,6])
# slice in 2, turn that into a list...
list(islice(it, 2))
# we should still have the last value still present
for x in it:
AreEqual(x, (4,6))
@skip("silverlight")
def test_iterator_for():
"""test various iterable objects with multiple incomplete iterations"""
def generator():
yield 0
yield 1
from cStringIO import StringIO
strO = StringIO()
strO.write('abc\n')
strO.write('def')
strI = StringIO('abc\ndef')
import sys
fi = sys.float_info
d = {2:3, 3:4}
l = [2, 3]
s = set([2, 3, 4])
if not is_silverlight:
f = file('test_file.txt', 'w+')
f.write('abc\n')
f.write('def')
f.close()
f = file('test_file.txt')
import nt
stat = nt.stat(__file__)
class x(object):
abc = 2
bcd = 3
dictproxy = x.__dict__
dictlist = list(x.__dict__)
ba = bytearray(b'abc')
try:
# iterator, first Value, second Value
iterators = [
# objects which when enumerated multiple times continue
(generator(), 0, 1),
(strI, 'abc\n', 'def'),
(strO, 'abc\n', 'def'),
# objects which when enumerated multiple times reset
(xrange(10), 0, 0),
([0, 1], 0, 0),
((0, 1), 0, 0),
(fi, fi[0], fi[0]),
(b'abc', b'a', b'a'),
(ba, ord(b'a'), ord(b'a')),
(u'abc', u'a', u'a'),
(d, list(d)[0], list(d)[0]),
(l, l[0], l[0]),
(s, list(s)[0], list(s)[0]),
(dictproxy, dictlist[0], dictlist[0]),
]
if not is_silverlight:
iterators.append((f, 'abc\n', 'def'))
iterators.append((stat, stat[0], stat[0]))
for iterator, res0, res1 in iterators:
for x in iterator:
AreEqual(x, res0)
break
for x in iterator:
AreEqual(x, res1)
break
finally:
f.close()
nt.unlink('test_file.txt')
def test_iterator_closed_file():
cf = file(__file__)
cf.close()
def f():
for x in cf: pass
AssertError(ValueError, f)
def test_no_return_self_in_iter():
class A(object):
def __iter__(cls):
return 1
def next(cls):
return 2
a = A()
AreEqual(next(a), 2)
def test_no_iter():
class A(object):
def next(cls):
return 2
a = A()
AreEqual(next(a), 2)
def test_with_iter():
class A(object):
def __iter__(cls):
return cls
def next(self):
return 2
a = A()
AreEqual(next(a), 2)
def test_with_iter_next_in_init():
class A(object):
def __init__(cls):
AreEqual(next(cls), 2)
AreEqual(cls.next(), 2)
def __iter__(cls):
return cls
def next(cls):
return 2
a = A()
AreEqual(next(a), 2)
def test_interacting_iterators():
"""This test is similar to how Jinga2 fails."""
class A(object):
def __iter__(cls):
return cls
def next(self):
return 3
class B(object):
def __iter__(cls):
return A()
def next(self):
return 2
b = B()
AreEqual(next(b), 2)
def test_call_to_iter_or_next():
class A(object):
def __iter__(cls):
Assert(False, "__iter__ should not be called.")
return cls
def next(self):
return 2
a = A()
AreEqual(next(a), 2)
run_test(__name__)