From 158f5b25dec0fa60a2cd11229d0ab5df79ffaacd Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 22 May 2025 13:38:33 -0300 Subject: [PATCH 1/3] Start draft PR From 9ab159c70f68be8cf5c20a9f01e5b0e2109dfbf0 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 22 May 2025 13:38:53 -0300 Subject: [PATCH 2/3] Add string reversal function with type checking --- src/string_reversal.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/string_reversal.py diff --git a/src/string_reversal.py b/src/string_reversal.py new file mode 100644 index 0000000..ca3c8c0 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,19 @@ +def reverse_string(input_string): + """ + Reverse a given string. + + Args: + input_string (str): The string to be reversed. + + Returns: + str: The reversed string. + + Raises: + TypeError: If the input is not a string. + """ + # Check if input is a string + if not isinstance(input_string, str): + raise TypeError("Input must be a string") + + # Return the reversed string + return input_string[::-1] \ No newline at end of file From b915a96ca3ebc287cdff584c7ca2cc372bba5d0e Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 22 May 2025 13:39:00 -0300 Subject: [PATCH 3/3] Add comprehensive tests for string reversal function --- tests/test_string_reversal.py | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/test_string_reversal.py diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py new file mode 100644 index 0000000..dfeea21 --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,38 @@ +import pytest +from src.string_reversal import reverse_string + +def test_reverse_normal_string(): + """Test reversing a normal string.""" + assert reverse_string("hello") == "olleh" + assert reverse_string("python") == "nohtyp" + +def test_reverse_empty_string(): + """Test reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_single_char(): + """Test reversing a single character string.""" + assert reverse_string("a") == "a" + +def test_reverse_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_with_special_chars(): + """Test reversing a string with special characters.""" + assert reverse_string("hello, world!") == "!dlrow ,olleh" + +def test_reverse_with_unicode(): + """Test reversing a string with unicode characters.""" + assert reverse_string("こんにちは") == "はちにんこ" + +def test_invalid_input_type(): + """Test that TypeError is raised for non-string inputs.""" + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(123) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(None) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(["hello"]) \ No newline at end of file