Coverage for tests\test_stack.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-03 22:23 -0500

1import pytest 

2from src.stack import Stack 

3 

4 

5def test_is_empty(): 

6 stack = Stack() 

7 assert stack.is_empty() # La pila recien creada debe estar vacia 

8 stack.push(5) 

9 # Despues de agregar un elemento, la pila no debe estar vacia 

10 assert not stack.is_empty() 

11 

12 

13def test_push(): 

14 stack = Stack() 

15 stack.push(1) 

16 assert stack.peek() == 1 # El valor recien agregado debe estar en la parte superior 

17 stack.push(2) 

18 # Despues de otro push, el valor superior debe ser el ultimo agregado 

19 assert stack.peek() == 2 

20 

21 

22def test_pop(): 

23 stack = Stack() 

24 stack.push(1) 

25 stack.push(2) 

26 assert stack.pop() == 2 # El valor superior (2) debe eliminarse y devolverse 

27 assert stack.peek() == 1 # Despues de pop(), el valor superior debe ser 1 

28 stack.pop() 

29 # Despues de eliminar todos los elementos, la pila debe estar vacia 

30 assert stack.is_empty() 

31 

32 

33def test_peek(): 

34 stack = Stack() 

35 stack.push(1) 

36 stack.push(2) 

37 assert stack.peek() == 2 # El valor superior debe ser el ultimo agregado (2) 

38 assert stack.peek() == 2 # La pila no debe cambiar despues de peek() 

39 

40 

41# Prueba adicional para manejar casos de error 

42def test_pop_empty(): 

43 stack = Stack() 

44 with pytest.raises(IndexError, match="La pila esta vacia"): 

45 stack.pop() 

46 

47 

48def test_peek_empty(): 

49 stack = Stack() 

50 with pytest.raises(IndexError, match="La pila esta vacia"): 

51 stack.peek()