class TestLoopStack: def setUp(self): self.stack = LoopStack() self.bottom = "spam" self.stack.stack = [self.bottom] def test_enter(self): iterable = "ham" s = self.stack._enter(iterable) assert s is self.stack.stack[-1], ( "Calling the stack with an iterable returns " "the stack" ) assert iterable == self.stack.stack[-1]._iterable, ( "and pushes the " "iterable on the top of the stack" ) def test__top(self): assert self.bottom == self.stack._top, ( "_top returns the last item " "on the stack" ) def test__pop(self): assert len(self.stack.stack) == 1 top = self.stack._pop() assert top == self.bottom assert len(self.stack.stack) == 0 def test__push(self): assert len(self.stack.stack) == 1 iterable = "ham" self.stack._push(iterable) assert len(self.stack.stack) == 2 assert iterable is self.stack._top._iterable def test_exit(self): iterable = "ham" self.stack._enter(iterable) before = len(self.stack.stack) self.stack._exit() after = len(self.stack.stack) assert before == (after + 1), "Exiting a context pops the stack"
class TestLoopStack(unittest.TestCase): def setUp(self): self.stack = LoopStack() self.bottom = 'spam' self.stack.stack = [self.bottom] def test_enter(self): iterable = 'ham' s = self.stack._enter(iterable) assert s is self.stack.stack[-1], "Calling the stack with an iterable returns "\ "the stack" assert iterable == self.stack.stack[-1]._iterable, "and pushes the "\ "iterable on the top of the stack" def test__top(self): assert self.bottom == self.stack._top, "_top returns the last item "\ "on the stack" def test__pop(self): assert len(self.stack.stack) == 1 top = self.stack._pop() assert top == self.bottom assert len(self.stack.stack) == 0 def test__push(self): assert len(self.stack.stack) == 1 iterable = 'ham' self.stack._push(iterable) assert len(self.stack.stack) == 2 assert iterable is self.stack._top._iterable def test_exit(self): iterable = 'ham' self.stack._enter(iterable) before = len(self.stack.stack) self.stack._exit() after = len(self.stack.stack) assert before == (after + 1), "Exiting a context pops the stack"