Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/string_reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def reverse_string(input_string: str) -> str:
"""
Reverses the given input 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")

# Reverse the string using slicing
return input_string[::-1]
37 changes: 37 additions & 0 deletions tests/test_string_reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest
from src.string_reversal import reverse_string

def test_basic_string_reversal():
"""Test reversing a standard string."""
assert reverse_string("hello") == "olleh"

def test_empty_string():
"""Test reversing an empty string."""
assert reverse_string("") == ""

def test_single_character():
"""Test reversing a single character string."""
assert reverse_string("a") == "a"

def test_string_with_spaces():
"""Test reversing a string with spaces."""
assert reverse_string("hello world") == "dlrow olleh"

def test_string_with_special_characters():
"""Test reversing a string with special characters."""
assert reverse_string("a1b2c3!@#") == "#@!3c2b1a"

def test_unicode_string():
"""Test reversing a unicode string."""
assert reverse_string("こんにちは") == "はちにんこ"

def test_invalid_input_type():
"""Test that a 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"])