Beispiel #1
0
    def test_iterable_class(self):
        class Count:
            def __init__(self, start=0):
                self.num = start

            def __iter__(self):
                return self

            def __next__(self):
                if self.num == 5:
                    raise StopIteration

                num = self.num
                self.num += 1
                return num

        iterable_class = Count()
        generator = my_generator(iterable_class, 'odd')
        result = [element for element in generator]
        self.assertEqual(result, [0, 2, 4])

        iterable_class = Count()
        generator = my_generator(iterable_class, 'even')
        result = [element for element in generator]
        self.assertEqual(result, [1, 3])
Beispiel #2
0
 def test_not_iterablle(self):
     generator = my_generator(1, 'even')
     try:
         result = [element for element in generator]
     except BaseException:
         self.assertTrue(True)
     else:
         self.assertFalse(False)
Beispiel #3
0
 def test_wrong_trait(self):
     generator = my_generator('abcdefg', 'wrong_trait')
     try:
         result = [element for element in generator]
     except BaseException:
         self.assertTrue(True)
     else:
         self.assertFalse(False)
Beispiel #4
0
 def test_string_even(self):
     generator = my_generator('abcdefg', 'even')
     result = [element for element in generator]
     self.assertEqual(result, ['b', 'd', 'f'])
Beispiel #5
0
 def test_empty_even(self):
     generator = my_generator([], 'even')
     result = [element for element in generator]
     self.assertEqual(result, [])
Beispiel #6
0
 def test_string_odd(self):
     generator = my_generator('abcdefg', 'odd')
     result = [element for element in generator]
     self.assertEqual(result, ['a', 'c', 'e', 'g'])
Beispiel #7
0
 def test_dict_even(self):
     generator = my_generator({'a': 1, 'b': 2, 'c': 3}, 'even')
     result = [element for element in generator]
     self.assertEqual(result, ['b'])
Beispiel #8
0
 def test_one_element_odd(self):
     generator = my_generator([1], 'odd')
     result = [element for element in generator]
     self.assertEqual(result, [1])
Beispiel #9
0
 def test_dict_odd(self):
     generator = my_generator({'a': 1, 'b': 2, 'c': 3}, 'odd')
     result = [element for element in generator]
     self.assertEqual(result, ['a', 'c'])
Beispiel #10
0
 def test_range_even(self):
     generator = my_generator(range(10), 'even')
     result = [element for element in generator]
     self.assertEqual(result, [1, 3, 5, 7, 9])
Beispiel #11
0
 def test_range_odd(self):
     generator = my_generator(range(10), 'odd')
     result = [element for element in generator]
     self.assertEqual(result, [0, 2, 4, 6, 8])
Beispiel #12
0
 def test_simple_case_even(self):
     generator = my_generator([1, 2, 3, 4, 5], 'even')
     result = [element for element in generator]
     self.assertEqual(result, [2, 4])
Beispiel #13
0
 def test_simple_case_odd(self):
     generator = my_generator([1, 2, 3, 4, 5], 'odd')
     result = [element for element in generator]
     self.assertEqual(result, [1, 3, 5])