From 9799df1e7f41428ffdf04b38ae76e9f1ece7abb0 Mon Sep 17 00:00:00 2001 From: NimahMasuud Date: Sat, 11 Jan 2025 15:55:00 +0100 Subject: [PATCH] Add to_uppercase module and tests with Ruff formatting --- solutions/multiply.py | 38 --------------------- solutions/tests/test_multiply.py | 49 ---------------------------- solutions/tests/test_to_uppercase.py | 40 +++++++++++++++++++++++ solutions/to_uppercase.py | 37 +++++++++++++++++++++ 4 files changed, 77 insertions(+), 87 deletions(-) delete mode 100644 solutions/multiply.py delete mode 100644 solutions/tests/test_multiply.py create mode 100644 solutions/tests/test_to_uppercase.py create mode 100644 solutions/to_uppercase.py diff --git a/solutions/multiply.py b/solutions/multiply.py deleted file mode 100644 index 6f8f92add..000000000 --- a/solutions/multiply.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python3 -# -- coding: utf-8 -- -""" -This module contains unit tests for the `multiply` function in `multiply.py`. -The tests cover various scenarios including valid inputs, defensive assertions, and boundary cases. -""" - - -def multiply(a: float, b: float) -> float: - """ - Multiplies two numbers and returns the result. - - Args: - a (float): The first number to be multiplied. - Must be a real number (no specific range constraints). - b (float): The second number to be multiplied. - Must be a real number (no specific range constraints). - - Returns: - float: The product of the two input numbers. - - Raises: - TypeError: If either `a` or `b` is not a number. - - Examples: - >>> multiply(3, 4) - 12 - >>> multiply(-1.5, 2) - -3.0 - >>> multiply(0, 5) - 0 - """ - if not isinstance(a, (int, float)): - raise TypeError("The first argument must be a number.") - if not isinstance(b, (int, float)): - raise TypeError("The second argument must be a number.") - - return a * b diff --git a/solutions/tests/test_multiply.py b/solutions/tests/test_multiply.py deleted file mode 100644 index 4733a031e..000000000 --- a/solutions/tests/test_multiply.py +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env python3 -# -- coding: utf-8 -- -# File: tests/test_multiply_function.py -""" -This module contains unit tests for the `multiply` function in `multiply_function.py`. -The tests cover various scenarios including valid inputs, defensive assertions, and boundary cases. -""" - -import unittest -from solutions.multiply import multiply - - -class TestMultiplyFunction(unittest.TestCase): - """Unit tests for the `multiply` function.""" - - def test_multiply_positive_numbers(self): - """Test multiplying two positive numbers.""" - self.assertEqual(multiply(3, 4), 12) - - def test_multiply_negative_numbers(self): - """Test multiplying two negative numbers.""" - self.assertEqual(multiply(-2, -5), 10) - - def test_multiply_positive_and_negative(self): - """Test multiplying a positive and a negative number.""" - self.assertEqual(multiply(-1.5, 2), -3.0) - - def test_multiply_with_zero(self): - """Test multiplying any number by zero.""" - self.assertEqual(multiply(0, 5), 0) - self.assertEqual(multiply(7, 0), 0) - - def test_multiply_float_numbers(self): - """Test multiplying two floating-point numbers.""" - self.assertAlmostEqual(multiply(2.5, 4.2), 10.5) - - def test_multiply_invalid_first_argument(self): - """Test error when the first argument is not a number.""" - with self.assertRaises(TypeError): - multiply("a", 5) - - def test_multiply_invalid_second_argument(self): - """Test error when the second argument is not a number.""" - with self.assertRaises(TypeError): - multiply(3, None) - - -if __name__ == "__main__": - unittest.main() diff --git a/solutions/tests/test_to_uppercase.py b/solutions/tests/test_to_uppercase.py new file mode 100644 index 000000000..9723cdbd8 --- /dev/null +++ b/solutions/tests/test_to_uppercase.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Unit tests for the to_uppercase function. + +Created on XX XX XX +@author: Your Name +""" + +import unittest +from solutions.to_uppercase import to_uppercase + + +class TestToUppercase(unittest.TestCase): + """Tests for the to_uppercase function.""" + + def test_valid_string(self): + """It should convert lowercase to uppercase.""" + self.assertEqual(to_uppercase("hello"), "HELLO") + + def test_mixed_case_string(self): + """It should convert mixed case to uppercase.""" + self.assertEqual(to_uppercase("Python"), "PYTHON") + + def test_alphanumeric_string(self): + """It should handle alphanumeric strings.""" + self.assertEqual(to_uppercase("123abc"), "123ABC") + + def test_empty_string(self): + """It should return an empty string.""" + self.assertEqual(to_uppercase(""), "") + + def test_non_string_input(self): + """It should raise TypeError for non-string inputs.""" + with self.assertRaises(TypeError): + to_uppercase(123) + + +if __name__ == "__main__": + unittest.main() diff --git a/solutions/to_uppercase.py b/solutions/to_uppercase.py new file mode 100644 index 000000000..3662d167c --- /dev/null +++ b/solutions/to_uppercase.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Module for converting strings to uppercase. + +Module contents: + - to_uppercase: Converts a given string to uppercase. + +Created on XX XX XX +@author: Your Name +""" + + +def to_uppercase(s: str) -> str: + """Converts a string to uppercase. + + Parameters: + s (str): The string to be converted. + + Returns: + str: The string with all characters in uppercase. + + Raises: + TypeError: If the input is not a string. + + >>> to_uppercase("hello") + 'HELLO' + + >>> to_uppercase("Python") + 'PYTHON' + + >>> to_uppercase("123abc") + '123ABC' + """ + if not isinstance(s, str): + raise TypeError("Input must be a string.") + return s.upper()