turner-townsend-backend-ass.../fifth_stack/test_fifth_stack.py

174 lines
3.6 KiB
Python

import pytest
from fifth_stack import FifthStack
# Test the functions
def test_push_and_pop():
stack = FifthStack()
stack.push(10)
stack.push(20)
assert stack.stack == [10, 20]
stack.pop()
assert stack.stack == [10]
stack.pop()
assert stack.stack == []
def test_push_invalid_value(capsys):
stack = FifthStack()
stack.push("abc")
captured = capsys.readouterr()
assert "ERROR: integer required" in captured.out
assert stack.stack == []
def test_pop_empty_stack(capsys):
stack = FifthStack()
stack.pop()
captured = capsys.readouterr()
assert "ERROR: stack empty" in captured.out
def test_swap():
stack = FifthStack()
stack.push(1)
stack.push(2)
stack.swap()
assert stack.stack == [2, 1]
def test_swap_insufficient_elements(capsys):
stack = FifthStack()
stack.push(1)
stack.swap()
captured = capsys.readouterr()
assert "ERROR: two numbers required" in captured.out
assert stack.stack == [1]
def test_dup():
stack = FifthStack()
stack.push(5)
stack.dup()
assert stack.stack == [5, 5]
def test_dup_empty_stack(capsys):
stack = FifthStack()
stack.dup()
captured = capsys.readouterr()
assert "ERROR: stack empty" in captured.out
def test_addition():
stack = FifthStack()
stack.push(2)
stack.push(3)
stack.execute("+")
assert stack.stack == [5]
def test_subtraction():
stack = FifthStack()
stack.push(5)
stack.push(2)
stack.execute("-")
assert stack.stack == [3]
def test_multiplication():
stack = FifthStack()
stack.push(4)
stack.push(3)
stack.execute("*")
assert stack.stack == [12]
def test_division():
stack = FifthStack()
stack.push(8)
stack.push(2)
stack.execute("/")
assert stack.stack == [4]
def test_division_by_zero(capsys):
stack = FifthStack()
stack.push(8)
stack.push(0)
stack.execute("/")
captured = capsys.readouterr()
assert "ERROR: division by zero" in captured.out
assert stack.stack == [8, 0]
def test_binary_op_insufficient_elements(capsys):
stack = FifthStack()
stack.push(1)
stack.execute("+")
captured = capsys.readouterr()
assert "ERROR: two numbers required" in captured.out
assert stack.stack == [1]
# Test executions
def test_execute_unknown_command(capsys):
stack = FifthStack()
stack.execute("foobar")
captured = capsys.readouterr()
assert "ERROR: unknown command" in captured.out
def test_execute_push_command():
stack = FifthStack()
stack.execute("push 42")
assert stack.stack == [42]
def test_execute_pop_command():
stack = FifthStack()
stack.push(99)
stack.execute("pop")
assert stack.stack == []
def test_execute_swap_command():
stack = FifthStack()
stack.push(1)
stack.push(2)
stack.execute("swap")
assert stack.stack == [2, 1]
def test_execute_dup_command():
stack = FifthStack()
stack.push(7)
stack.execute("dup")
assert stack.stack == [7, 7]
# Help and edge cases
def test_help(capsys):
stack = FifthStack()
stack.help()
captured = capsys.readouterr()
assert "Available commands:" in captured.out
assert "Available operations:" in captured.out
def test_execute_empty_command():
stack = FifthStack()
stack.execute("") # Should do nothing
assert stack.stack == []
def test_execute_whitespace_command():
stack = FifthStack()
stack.execute(" ") # Should do nothing
assert stack.stack == []
def test_case_insensitivity():
stack = FifthStack()
stack.execute("PUSH 42")
assert stack.stack == [42]