-
-
Save fcasco/5649478 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import unittest | |
import sys | |
class EmptyStackElement(object): | |
def pop(self): | |
raise StopIteration | |
class StackElement(object): | |
def __init__(self, element, previous): | |
self.element = element | |
self.previous = previous | |
def pop(self): | |
return self.element | |
class Stack(object): | |
def __init__(self): | |
self.top_element = EmptyStackElement() | |
def pop(self): | |
poped = self.top_element.pop() | |
self.top_element = self.top_element.previous | |
return poped | |
def push(self, element): | |
self.top_element = StackElement(element, self.top_element) | |
class StackTestCases(unittest.TestCase): | |
def test_empty(self): | |
s = Stack() | |
try: | |
s.pop() | |
self.fail('No exception raised') | |
except StopIteration: | |
pass | |
else: | |
self.fail('Wrong exception') | |
def test_push(self): | |
s = Stack() | |
self.assertEqual(s.push(42), None) | |
def test_push_pop(self): | |
s = Stack() | |
s.push(42) | |
self.assertEqual(s.pop(), 42) | |
try: | |
s.pop() | |
self.fail('No exception raised') | |
except StopIteration: | |
pass | |
else: | |
self.fail('Wrong exception') | |
if __name__ == '__main__': | |
sys.exit(unittest.main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment