def test_pop(self): """Empty stack will return indexError when popping""" stack = Stack(5, []) with self.assertRaises(IndexError): stack.pop() """Testing non-empty stacks, item is removed from the stack""" stack.push(-1000) self.assertEqual(stack.__repr__(), "Stack(5, [-1000])") self.assertEqual(stack.pop(), -1000) self.assertEqual(stack.__repr__(), "Stack(5, [])") stack2 = Stack(5, [1, 2, 3, 4, 5]) self.assertEqual(stack2.__repr__(), "Stack(5, [1, 2, 3, 4, 5])") self.assertEqual(stack2.pop(), 5) self.assertEqual(stack2.__repr__(), "Stack(5, [1, 2, 3, 4])")
def test_peek(self): """Cant peek if you're empty *points finger at own head*, peek returns index error""" stack = Stack(5, []) with self.assertRaises(IndexError): stack.peek() """Testing non-empty stacks. Peek returns the item & does not remove item from stack""" stack.push(-1000) self.assertEqual(stack.__repr__(), "Stack(5, [-1000])") self.assertEqual(stack.peek(), -1000) self.assertEqual(stack.__repr__(), "Stack(5, [-1000])") stack2 = Stack(5, [1, 2, 3, 4, 5]) self.assertEqual(stack2.__repr__(), "Stack(5, [1, 2, 3, 4, 5])") self.assertEqual(stack2.peek(), 5) self.assertEqual(stack2.__repr__(), "Stack(5, [1, 2, 3, 4, 5])")
def test_push(self): """Creating non-full stack""" stack = Stack(5, [1, 2, 4]) """Pushing 5 then -1 on non-full stack""" stack.push(5) self.assertEqual(stack.__repr__(), "Stack(5, [1, 2, 4, 5])") self.assertEqual(stack.items[0:4], [1, 2, 4, 5]) self.assertEqual(stack.capacity, 5) stack.push(-1) self.assertEqual(stack.__repr__(), "Stack(5, [1, 2, 4, 5, -1])") self.assertEqual(stack.items[0:5], [1, 2, 4, 5, -1]) self.assertEqual(stack.capacity, 5) """Stack is full, now testing indexError when calling push""" with self.assertRaises(IndexError): stack.push(0)
def test_repr(self): stack = Stack(5, [1, 2]) self.assertEqual(stack.__repr__(), "Stack(5, [1, 2])")