Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(480)

Side by Side Diff: gslib/tests/test_plurality_checkable_iterator.py

Issue 698893003: Update checked in version of gsutil to version 4.6 (Closed) Base URL: http://dart.googlecode.com/svn/third_party/gsutil/
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gslib/tests/test_perfdiag.py ('k') | gslib/tests/test_rb.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # -*- coding: utf-8 -*-
1 # Copyright 2012 Google Inc. All Rights Reserved. 2 # Copyright 2012 Google Inc. All Rights Reserved.
2 # 3 #
3 # Permission is hereby granted, free of charge, to any person obtaining a 4 # Permission is hereby granted, free of charge, to any person obtaining a
4 # copy of this software and associated documentation files (the 5 # copy of this software and associated documentation files (the
5 # "Software"), to deal in the Software without restriction, including 6 # "Software"), to deal in the Software without restriction, including
6 # without limitation the rights to use, copy, modify, merge, publish, dis- 7 # without limitation the rights to use, copy, modify, merge, publish, dis-
7 # tribute, sublicense, and/or sell copies of the Software, and to permit 8 # tribute, sublicense, and/or sell copies of the Software, and to permit
8 # persons to whom the Software is furnished to do so, subject to the fol- 9 # persons to whom the Software is furnished to do so, subject to the fol-
9 # lowing conditions: 10 # lowing conditions:
10 # 11 #
11 # The above copyright notice and this permission notice shall be included 12 # The above copyright notice and this permission notice shall be included
12 # in all copies or substantial portions of the Software. 13 # in all copies or substantial portions of the Software.
13 # 14 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 # IN THE SOFTWARE. 21 # IN THE SOFTWARE.
22 """Unit tests for PluralityCheckableIterator."""
21 23
22 """Unit tests for PluralityCheckableIterator""" 24 from __future__ import absolute_import
25
26 import sys
23 27
24 from gslib.plurality_checkable_iterator import PluralityCheckableIterator 28 from gslib.plurality_checkable_iterator import PluralityCheckableIterator
25 import gslib.tests.testcase as testcase 29 import gslib.tests.testcase as testcase
26 30
31
32 class CustomTestException(Exception):
33 pass
34
35
27 class PluralityCheckableIteratorTests(testcase.GsUtilUnitTestCase): 36 class PluralityCheckableIteratorTests(testcase.GsUtilUnitTestCase):
37 """Unit tests for PluralityCheckableIterator."""
28 38
29 def testPluralityCheckableIteratorWith0Elems(self): 39 def testPluralityCheckableIteratorWith0Elems(self):
30 """Tests empty PluralityCheckableIterator.""" 40 """Tests empty PluralityCheckableIterator."""
31 input_list = range(0) 41 input_list = range(0)
32 it = iter(input_list) 42 it = iter(input_list)
33 pcit = PluralityCheckableIterator(it) 43 pcit = PluralityCheckableIterator(it)
34 self.assertTrue(pcit.is_empty()) 44 self.assertTrue(pcit.IsEmpty())
35 self.assertFalse(pcit.has_plurality()) 45 self.assertFalse(pcit.HasPlurality())
36 output_list = list(pcit) 46 output_list = list(pcit)
37 self.assertEqual(input_list, output_list) 47 self.assertEqual(input_list, output_list)
38 48
39 def testPluralityCheckableIteratorWith1Elem(self): 49 def testPluralityCheckableIteratorWith1Elem(self):
40 """Tests PluralityCheckableIterator with 1 element.""" 50 """Tests PluralityCheckableIterator with 1 element."""
41 input_list = range(1) 51 input_list = range(1)
42 it = iter(input_list) 52 it = iter(input_list)
43 pcit = PluralityCheckableIterator(it) 53 pcit = PluralityCheckableIterator(it)
44 self.assertFalse(pcit.is_empty()) 54 self.assertFalse(pcit.IsEmpty())
45 self.assertFalse(pcit.has_plurality()) 55 self.assertFalse(pcit.HasPlurality())
46 output_list = list(pcit) 56 output_list = list(pcit)
47 self.assertEqual(input_list, output_list) 57 self.assertEqual(input_list, output_list)
48 58
49 def testPluralityCheckableIteratorWith2Elems(self): 59 def testPluralityCheckableIteratorWith2Elems(self):
50 """Tests PluralityCheckableIterator with 2 elements.""" 60 """Tests PluralityCheckableIterator with 2 elements."""
51 input_list = range(2) 61 input_list = range(2)
52 it = iter(input_list) 62 it = iter(input_list)
53 pcit = PluralityCheckableIterator(it) 63 pcit = PluralityCheckableIterator(it)
54 self.assertFalse(pcit.is_empty()) 64 self.assertFalse(pcit.IsEmpty())
55 self.assertTrue(pcit.has_plurality()) 65 self.assertTrue(pcit.HasPlurality())
56 output_list = list(pcit) 66 output_list = list(pcit)
57 self.assertEqual(input_list, output_list) 67 self.assertEqual(input_list, output_list)
58 68
59 def testPluralityCheckableIteratorWith3Elems(self): 69 def testPluralityCheckableIteratorWith3Elems(self):
60 """Tests PluralityCheckableIterator with 3 elements.""" 70 """Tests PluralityCheckableIterator with 3 elements."""
61 input_list = range(3) 71 input_list = range(3)
62 it = iter(input_list) 72 it = iter(input_list)
63 pcit = PluralityCheckableIterator(it) 73 pcit = PluralityCheckableIterator(it)
64 self.assertFalse(pcit.is_empty()) 74 self.assertFalse(pcit.IsEmpty())
65 self.assertTrue(pcit.has_plurality()) 75 self.assertTrue(pcit.HasPlurality())
66 output_list = list(pcit) 76 output_list = list(pcit)
67 self.assertEqual(input_list, output_list) 77 self.assertEqual(input_list, output_list)
78
79 def testPluralityCheckableIteratorWith1Elem1Exception(self):
80 """Tests PluralityCheckableIterator with 2 elements.
81
82 The second element raises an exception.
83 """
84
85 class IterTest(object):
86
87 def __init__(self):
88 self.position = 0
89
90 def __iter__(self):
91 return self
92
93 def next(self):
94 if self.position == 0:
95 self.position += 1
96 return 1
97 elif self.position == 1:
98 self.position += 1
99 raise CustomTestException('Test exception')
100 else:
101 raise StopIteration()
102
103 pcit = PluralityCheckableIterator(IterTest())
104 self.assertFalse(pcit.IsEmpty())
105 self.assertTrue(pcit.HasPlurality())
106 iterated_value = None
107 try:
108 for value in pcit:
109 iterated_value = value
110 self.fail('Expected exception from iterator')
111 except CustomTestException:
112 pass
113 self.assertEqual(iterated_value, 1)
114
115 def testPluralityCheckableIteratorWith2Exceptions(self):
116 """Tests PluralityCheckableIterator with 2 elements that both raise."""
117
118 class IterTest(object):
119
120 def __init__(self):
121 self.position = 0
122
123 def __iter__(self):
124 return self
125
126 def next(self):
127 if self.position < 2:
128 self.position += 1
129 raise CustomTestException('Test exception %s' % self.position)
130 else:
131 raise StopIteration()
132
133 pcit = PluralityCheckableIterator(IterTest())
134 try:
135 for _ in pcit:
136 pass
137 self.fail('Expected exception 1 from iterator')
138 except CustomTestException, e:
139 self.assertIn(e.message, 'Test exception 1')
140 try:
141 for _ in pcit:
142 pass
143 self.fail('Expected exception 2 from iterator')
144 except CustomTestException, e:
145 self.assertIn(e.message, 'Test exception 2')
146 for _ in pcit:
147 self.fail('Expected StopIteration')
148
149 def testPluralityCheckableIteratorWithYieldedException(self):
150 """Tests PluralityCheckableIterator an iterator that yields an exception.
151
152 The yielded exception is in the form of a tuple and must also contain a
153 stack trace.
154 """
155
156 class IterTest(object):
157
158 def __init__(self):
159 self.position = 0
160
161 def __iter__(self):
162 return self
163
164 def next(self):
165 if self.position == 0:
166 try:
167 self.position += 1
168 raise CustomTestException('Test exception 0')
169 except CustomTestException, e:
170 return (e, sys.exc_info()[2])
171 elif self.position == 1:
172 self.position += 1
173 return 1
174 else:
175 raise StopIteration()
176
177 pcit = PluralityCheckableIterator(IterTest())
178 try:
179 for _ in pcit:
180 pass
181 self.fail('Expected exception 0 from iterator')
182 except CustomTestException, e:
183 self.assertIn(e.message, 'Test exception 0')
184 for value in pcit:
185 iterated_value = value
186 self.assertEqual(iterated_value, 1)
OLDNEW
« no previous file with comments | « gslib/tests/test_perfdiag.py ('k') | gslib/tests/test_rb.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698