예제 #1
0
def test_size_returns_the_number_of_elements_in_the_stack():
    stack = Stack()
    stack.push(42)
    stack.push(42)
    stack.push(42)

    assert stack.size == 3
예제 #2
0
def test_push_increases_the_stack_size():
    stack = Stack()
    previous_size = stack.size

    stack.push(42)

    assert stack.size == (previous_size + 1)
예제 #3
0
 def test_stack_push_pop_with_elements(self):
     stack = Stack()
     stack.push(1)
     stack.push(2)
     stack.push(5)
     assert stack.pop() == 5
     assert stack.pop() == 2
     assert stack.pop() == 1
예제 #4
0
def test_peek_does_not_change_the_stack_size():
    stack = Stack()
    stack.push(42)
    previous_size = stack.size

    stack.peek()

    assert stack.size == previous_size
예제 #5
0
def test_pop_decreases_the_stack_size():
    stack = Stack()
    stack.push(42)
    previous_size = stack.size

    stack.pop()

    assert stack.size == (previous_size - 1)
    def test_is_empty(self):
        stack = Stack()
        assert stack.is_empty() is True

        stack.push(1)
        assert stack.is_empty() is False

        stack.pop()
        assert stack.is_empty() is True
    def test_push_items(self):
        stack = Stack()

        tests = [1, '0', Stack, lambda x: x, {}, [], None]

        for test in tests:
            stack.push(test)

        assert len(tests) == len(stack)
    def test_size(self):
        stack = Stack()
        assert len(stack) == 0

        stack.push(1)
        assert len(stack) == 1

        stack.pop()
        assert len(stack) == 0
    def test_stack_as_string(self):
        stack = Stack()

        assert str(stack) == ''

        stack.push(3)
        stack.push(1)
        stack.push(2)

        assert repr(stack) == '2 -> 1 -> 3'
    def test_pop_items(self):
        stack = Stack()

        with self.assertRaises(StackPopException):
            stack.pop()

        one = 1
        two = 2

        stack.push(one)
        stack.push(two)

        assert len(stack) == 2

        assert stack.pop() == two
        assert stack.pop() == one

        assert len(stack) == 0
예제 #11
0
def test_str_prints_the_stack_and_its_elements():
    stack = Stack()
    stack.push(42)
    stack.push("Second element")
    stack.push(True)

    assert str(stack) == "Stack(True, 'Second element', 42)"
예제 #12
0
def test_is_empty_returns_false_on_stack_with_elements():
    stack = Stack()
    stack.push(42)

    assert not stack.is_empty()
예제 #13
0
def test_push_sets_the_element_on_top():
    stack = Stack()
    
    stack.push(42)
    
    assert stack.peek() == 42
예제 #14
0
def test_peek_returns_the_element_on_top():
    stack = Stack()
    stack.push(42)

    assert stack.peek() == 42
예제 #15
0
def test_pop_returns_the_latest_element_pushed():
    stack = Stack()
    stack.push("First")
    stack.push(42)

    assert stack.pop() == 42