OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # Copyright 2012 Google Inc. All Rights Reserved. |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 """Unit tests for PluralityCheckableIterator.""" |
| 23 |
| 24 from __future__ import absolute_import |
| 25 |
| 26 import sys |
| 27 |
| 28 from gslib.plurality_checkable_iterator import PluralityCheckableIterator |
| 29 import gslib.tests.testcase as testcase |
| 30 |
| 31 |
| 32 class CustomTestException(Exception): |
| 33 pass |
| 34 |
| 35 |
| 36 class PluralityCheckableIteratorTests(testcase.GsUtilUnitTestCase): |
| 37 """Unit tests for PluralityCheckableIterator.""" |
| 38 |
| 39 def testPluralityCheckableIteratorWith0Elems(self): |
| 40 """Tests empty PluralityCheckableIterator.""" |
| 41 input_list = range(0) |
| 42 it = iter(input_list) |
| 43 pcit = PluralityCheckableIterator(it) |
| 44 self.assertTrue(pcit.IsEmpty()) |
| 45 self.assertFalse(pcit.HasPlurality()) |
| 46 output_list = list(pcit) |
| 47 self.assertEqual(input_list, output_list) |
| 48 |
| 49 def testPluralityCheckableIteratorWith1Elem(self): |
| 50 """Tests PluralityCheckableIterator with 1 element.""" |
| 51 input_list = range(1) |
| 52 it = iter(input_list) |
| 53 pcit = PluralityCheckableIterator(it) |
| 54 self.assertFalse(pcit.IsEmpty()) |
| 55 self.assertFalse(pcit.HasPlurality()) |
| 56 output_list = list(pcit) |
| 57 self.assertEqual(input_list, output_list) |
| 58 |
| 59 def testPluralityCheckableIteratorWith2Elems(self): |
| 60 """Tests PluralityCheckableIterator with 2 elements.""" |
| 61 input_list = range(2) |
| 62 it = iter(input_list) |
| 63 pcit = PluralityCheckableIterator(it) |
| 64 self.assertFalse(pcit.IsEmpty()) |
| 65 self.assertTrue(pcit.HasPlurality()) |
| 66 output_list = list(pcit) |
| 67 self.assertEqual(input_list, output_list) |
| 68 |
| 69 def testPluralityCheckableIteratorWith3Elems(self): |
| 70 """Tests PluralityCheckableIterator with 3 elements.""" |
| 71 input_list = range(3) |
| 72 it = iter(input_list) |
| 73 pcit = PluralityCheckableIterator(it) |
| 74 self.assertFalse(pcit.IsEmpty()) |
| 75 self.assertTrue(pcit.HasPlurality()) |
| 76 output_list = list(pcit) |
| 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) |
OLD | NEW |