class TestLoopContext: def setUp(self): self.iterable = [1, 2, 3] self.ctx = LoopContext(self.iterable) def test___len__(self): assert len(self.iterable) == len(self.ctx), ( "The LoopContext is the " "same length as the iterable" ) def test_index(self): expected = tuple(range(len(self.iterable))) actual = tuple(self.ctx.index for i in self.ctx) assert expected == actual, ( "The index is consistent with the current " "iteration count" ) def test_reverse_index(self): length = len(self.iterable) expected = tuple(length - i - 1 for i in range(length)) actual = tuple(self.ctx.reverse_index for i in self.ctx) print(expected, actual) assert expected == actual, ( "The reverse_index is the number of " "iterations until the end" ) def test_first(self): expected = (True, False, False) actual = tuple(self.ctx.first for i in self.ctx) assert expected == actual, "first is only true on the first iteration" def test_last(self): expected = (False, False, True) actual = tuple(self.ctx.last for i in self.ctx) assert expected == actual, "last is only true on the last iteration" def test_even(self): expected = (True, False, True) actual = tuple(self.ctx.even for i in self.ctx) assert expected == actual, "even is true on even iterations" def test_odd(self): expected = (False, True, False) actual = tuple(self.ctx.odd for i in self.ctx) assert expected == actual, "odd is true on odd iterations" def test_cycle(self): expected = ("a", "b", "a") actual = tuple(self.ctx.cycle("a", "b") for i in self.ctx) assert expected == actual, "cycle endlessly cycles through the values"
class TestLoopContext(unittest.TestCase): def setUp(self): self.iterable = [1, 2, 3] self.ctx = LoopContext(self.iterable) def test___len__(self): assert len(self.iterable) == len(self.ctx), "The LoopContext is the "\ "same length as the iterable" def test_index(self): expected = tuple(range(len(self.iterable))) actual = tuple(self.ctx.index for i in self.ctx) assert expected == actual, "The index is consistent with the current "\ "iteration count" def test_reverse_index(self): length = len(self.iterable) expected = tuple([length-i-1 for i in range(length)]) actual = tuple(self.ctx.reverse_index for i in self.ctx) print(expected, actual) assert expected == actual, "The reverse_index is the number of "\ "iterations until the end" def test_first(self): expected = (True, False, False) actual = tuple(self.ctx.first for i in self.ctx) assert expected == actual, "first is only true on the first iteration" def test_last(self): expected = (False, False, True) actual = tuple(self.ctx.last for i in self.ctx) assert expected == actual, "last is only true on the last iteration" def test_even(self): expected = (True, False, True) actual = tuple(self.ctx.even for i in self.ctx) assert expected == actual, "even is true on even iterations" def test_odd(self): expected = (False, True, False) actual = tuple(self.ctx.odd for i in self.ctx) assert expected == actual, "odd is true on odd iterations" def test_cycle(self): expected = ('a', 'b', 'a') actual = tuple(self.ctx.cycle('a', 'b') for i in self.ctx) assert expected == actual, "cycle endlessly cycles through the values"
def setUp(self): self.iterable = [1, 2, 3] self.ctx = LoopContext(self.iterable)