Add '^' exponentiation alias to calculate and tests
This commit is contained in:
+4
-2
@@ -1,7 +1,7 @@
|
||||
"""A small arithmetic calculator.
|
||||
|
||||
Provides the :func:`calculate` function which performs basic arithmetic
|
||||
operations (+, -, *, /, **, //, %) with proper error handling.
|
||||
operations (+, -, *, /, **, ^, //, %) with proper error handling.
|
||||
"""
|
||||
|
||||
__all__ = ["calculate"]
|
||||
@@ -17,7 +17,7 @@ def calculate(a: float, op: str, b: float) -> int | float:
|
||||
|
||||
Args:
|
||||
a: First operand
|
||||
op: Operation (+, -, *, /, **, //, %)
|
||||
op: Operation (+, -, *, /, **, ^, //, %)
|
||||
b: Second operand
|
||||
|
||||
Returns:
|
||||
@@ -50,6 +50,8 @@ def calculate(a: float, op: str, b: float) -> int | float:
|
||||
return a / b
|
||||
case "**":
|
||||
return a ** b
|
||||
case "^":
|
||||
return a ** b
|
||||
case "//":
|
||||
return a // b
|
||||
case "%":
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Tests for the pipeline_test calculator."""
|
||||
|
||||
import pytest
|
||||
|
||||
from pipeline_test import calculate
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("a", "op", "b", "expected"),
|
||||
[
|
||||
(2, "+", 3, 5),
|
||||
(7, "-", 2, 5),
|
||||
(4, "*", 3, 12),
|
||||
(8, "/", 2, 4),
|
||||
(2, "**", 3, 8),
|
||||
(2, "^", 3, 8),
|
||||
(7, "//", 2, 3),
|
||||
(7, "%", 2, 1),
|
||||
],
|
||||
)
|
||||
def test_operations(a, op, b, expected):
|
||||
assert calculate(a, op, b) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("op", ["/", "//", "%"])
|
||||
def test_division_by_zero(op):
|
||||
with pytest.raises(ZeroDivisionError):
|
||||
calculate(1, op, 0)
|
||||
|
||||
|
||||
def test_unknown_operation():
|
||||
with pytest.raises(ValueError):
|
||||
calculate(1, "!", 2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("a", "op", "b"),
|
||||
[
|
||||
("1", "+", 2),
|
||||
(1, "+", "2"),
|
||||
],
|
||||
)
|
||||
def test_operand_not_a_number(a, op, b):
|
||||
with pytest.raises(TypeError):
|
||||
calculate(a, op, b)
|
||||
|
||||
|
||||
def test_op_not_a_string():
|
||||
with pytest.raises(TypeError):
|
||||
calculate(1, 2, 3)
|
||||
Reference in New Issue
Block a user