diff --git a/.github/workflows/run_test.yml b/.github/workflows/run_test.yml new file mode 100644 index 00000000..4b289422 --- /dev/null +++ b/.github/workflows/run_test.yml @@ -0,0 +1,36 @@ +name: simple_calculator unit test + +permissions: + actions: write + contents: write + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.10"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with Ruff + run: | + pip install ruff + ruff --format=github --target-version=py310 . + continue-on-error: true + - name: Test with pytest + run: | + coverage run -m pytest tests/test_1b.py -v -s + - name: Generate Coverage Report + run: | + coverage report -m \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..c9ebf2d2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python-envs.defaultEnvManager": "ms-python.python:system" +} \ No newline at end of file diff --git a/labs/lab_1/lab_1a.py b/labs/lab_1/lab_1a.py index 9d15ec83..cf19cf91 100644 --- a/labs/lab_1/lab_1a.py +++ b/labs/lab_1/lab_1a.py @@ -1,5 +1,6 @@ """ lab_1a.py +This is to simulate a change made on a robot: robot_speed = 5 # m/s The first lab in the BWSI CSS course. To complete this lab, fill out the variable on line 10 with your name. Then, save the code, add it to the staging area, and commit it to the Git tree. @@ -8,9 +9,9 @@ def main(): print("Hello World!") - name = "" # TODO: Insert your name between the double quotes + name = "Yasho" # TODO: Insert your name between the double quotes print(f"{name}, Welcome to the CSS course!") - + print(f"I'm {name} and a fun fact about me is that I like to break my record on solving Rubik's Cube!") if __name__ == "__main__": main() diff --git a/labs/lab_1/lab_1b.py b/labs/lab_1/lab_1b.py index e58dd957..4cf42771 100644 --- a/labs/lab_1/lab_1b.py +++ b/labs/lab_1/lab_1b.py @@ -23,6 +23,10 @@ def simple_calculator(operation: str, num1: float, num2: float) -> float: float: The result of the operation. """ + operation = operation.strip().lower() # Normalize the operation string + + if not isinstance(num1, (int, float)) or not isinstance(num2, (int, float)): + raise ValueError("Both num1 and num2 must be numeric values.") if operation == "add": return num1 + num2 elif operation == "subtract": @@ -39,8 +43,6 @@ def simple_calculator(operation: str, num1: float, num2: float) -> float: def main(): - print(f"===== Simple Calculator =====") - # Ask the user for sample input num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) diff --git a/tests/tests_1b.py b/tests/test_1b.py similarity index 57% rename from tests/tests_1b.py rename to tests/test_1b.py index be6b822d..c53e8338 100644 --- a/tests/tests_1b.py +++ b/tests/test_1b.py @@ -2,6 +2,8 @@ tests_1b.py This module contains unit tests for the simple_calculator function defined in lab_1b.py. +The tests cover various scenarios for addition, subtraction, multiplication, and division operations +including edge cases such as division by zero and invalid operations. The tests are implemented using the pytest framework. Dummy comment to make commit """ import pytest @@ -37,5 +39,23 @@ def test_invalid_operation(): with pytest.raises(ValueError, match="Invalid operation. Please choose from 'add', 'subtract', 'multiply', or 'divide'."): simple_calculator("", 5, 3) # Test for empty operation +def test_operation_case_insensitivity(): + assert simple_calculator("ADD", 5, 3) == 8 # Test for uppercase operation + assert simple_calculator("Subtract", 5, 3) == 2 # Test for mixed case operation + assert simple_calculator("MULTIPLY", 5, 3) == 15 # Test for uppercase operation + assert simple_calculator("DIVIDE", 6, 3) == 2 # Test for uppercase operation + +def test_operation_with_whitespace(): + assert simple_calculator(" add ", 5, 3) == 8 # Test for operation with leading and trailing whitespace + assert simple_calculator(" subtract ", 5, 3) == 2 # Test for operation with leading and trailing whitespace + assert simple_calculator(" multiply ", 5, 3) == 15 # Test for operation with leading and trailing whitespace + assert simple_calculator(" divide ", 6, 3) == 2 # Test for operation with leading and trailing whitespace + +def test_operation_with_non_numeric_input(): + with pytest.raises(ValueError): + simple_calculator("add", "five", 3) # Test for non-numeric input for num1 + with pytest.raises(ValueError): + simple_calculator("subtract", 5, "three") # Test for non-numeric input for num2 + if __name__ == "__main__": pytest.main() \ No newline at end of file