Skip to content

Instantly share code, notes, and snippets.

Created May 25, 2013 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5649471 to your computer and use it in GitHub Desktop.
Save anonymous/5649471 to your computer and use it in GitHub Desktop.
look ma' no ifs!
#!/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