From 5872f210c9dcf83d3817910a264bb45875dbc86b Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Thu, 21 Dec 2023 09:44:19 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + pytest.ini | 1 + src/__init__.py | 0 src/calculator.py | 19 +++++++++++++++++++ test_addition.py | 10 ++++++++++ 5 files changed, 31 insertions(+) create mode 100644 .gitignore create mode 100644 pytest.ini create mode 100644 src/__init__.py create mode 100644 src/calculator.py create mode 100644 test_addition.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e3d04c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv* diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..4d11943 --- /dev/null +++ b/pytest.ini @@ -0,0 +1 @@ +junit_family = xunit1 diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/calculator.py b/src/calculator.py new file mode 100644 index 0000000..d545193 --- /dev/null +++ b/src/calculator.py @@ -0,0 +1,19 @@ +def add(a, b): + checkInputs(a, b) + return a + b + +def subtract(a, b): + checkInputs(a, b) + return a - b + +def multiply(a, b): + checkInputs(a, b) + return a * b + +def divide(a, b): + checkInputs(a, b) + return a / b + +def checkInputs(a, b): + if not isinstance(a, (int, float)) or not isinstance(b, (int, float)): + raise TypeError("Inputs must be either int or float!") diff --git a/test_addition.py b/test_addition.py new file mode 100644 index 0000000..7d14170 --- /dev/null +++ b/test_addition.py @@ -0,0 +1,10 @@ +from src.calculator import add +import pytest + +def test_add(): + result = add(3, 4) + assert result == 7 + +def test_add_string(): + with pytest.raises(TypeError): + add("string", 4)