Skip to content

Commit

Permalink
update check_palindromes
Browse files Browse the repository at this point in the history
  • Loading branch information
fevziismailsahin committed Jan 13, 2025
1 parent c251df7 commit d0ee4d8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 47 deletions.
10 changes: 5 additions & 5 deletions solutions/check_palindromes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def check_palindromes(words_to_check: List[str]) -> None:
'Test' is not a palindrome.
"""
if not isinstance(words_to_check, list):
raise TypeError("Input must be a list")

for value in words_to_check:
if not isinstance(value, str):
raise TypeError("List must contain only strings")
cleaned_value = "".join(filter(str.isalnum, value)).lower()

if cleaned_value == cleaned_value[::-1]:
print(f"'{value}' is a palindrome.")
else:
print(f"'{value}' is not a palindrome.")


if __name__ == "__main__":
words_to_check_list = ["Radar", "12321", "Hello", "1.232.1", "12345", "aA", "Test"]
check_palindromes(words_to_check_list)
104 changes: 62 additions & 42 deletions solutions/tests/test_check_palindromes.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,71 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Module represents unit test for check_palindromes
Unit test module for checking palindromes.
Created on 01/11/2025
Author fevziismailsahin
Test categories:
- Standard cases: correct inputs for words and numbers;
- Edge cases;
- Defensive tests: wrong input types, assertions;
Author: fevziismailsahin
Created: 01/11/2025
"""

import sys
import unittest
from io import StringIO

from ..check_palindromes import check_palindromes


class TestCheckPalindromes(unittest.TestCase):
"""Test suite for checking palindromes."""

def setUp(self):
"""Set up the test environment."""
self.held_output = StringIO()
sys.stdout = self.held_output

def tearDown(self):
"""Tear down the test environment."""
sys.stdout = sys.__stdout__

# Standard cases: correct inputs
def test_standard_case_1(self):
"""Test with typical valid inputs for words and numbers."""
check_palindromes(["Radar", "12321", "Hello", "1.232.1", "12345", "aA", "Test"])
self.assertIn("'Radar' is a palindrome.", self.held_output.getvalue())
self.assertIn("'12321' is a palindrome.", self.held_output.getvalue())
self.assertIn("'Hello' is not a palindrome.", self.held_output.getvalue())
self.assertIn("'1.232.1' is a palindrome.", self.held_output.getvalue())
self.assertIn("'12345' is not a palindrome.", self.held_output.getvalue())
self.assertIn("'aA' is a palindrome.", self.held_output.getvalue())
self.assertIn("'Test' is not a palindrome.", self.held_output.getvalue())

# Edge cases: testing empty and single-character strings
def test_empty_string(self):
"""Test with an empty string."""
check_palindromes([""])
self.assertIn("'' is a palindrome.", self.held_output.getvalue())

def test_single_character(self):
"""Test with a single character."""
check_palindromes(["a", "1", "!"])
self.assertIn("'a' is a palindrome.", self.held_output.getvalue())
self.assertIn("'1' is a palindrome.", self.held_output.getvalue())
self.assertIn("'!' is a palindrome.", self.held_output.getvalue())

# Defensive tests: wrong input types, assertions
def test_invalid_input_type(self):
"""Test when input is not a list."""
with self.assertRaises(TypeError):
check_palindromes("not a list")

from solutions.check_palindromes import check_palindromes


class TestPalindromeChecker(unittest.TestCase):
def test_palindrome_words(self):
"""
Test: Check palindrome words correctly identified.
"""
with self.assertLogs(level="INFO") as log:
check_palindromes(["Radar", "12321", "aA"])
self.assertIn("'Radar' is a palindrome.", log.output)
self.assertIn("'12321' is a palindrome.", log.output)
self.assertIn("'aA' is a palindrome.", log.output)

def test_non_palindrome_words(self):
"""
Test: Check non-palindrome words correctly identified.
"""
with self.assertLogs(level="INFO") as log:
check_palindromes(["Hello", "12345", "Test"])
self.assertIn("'Hello' is not a palindrome.", log.output)
self.assertIn("'12345' is not a palindrome.", log.output)
self.assertIn("'Test' is not a palindrome.", log.output)

def test_mixed_case(self):
"""
Test: Check case-insensitive palindrome detection.
"""
with self.assertLogs(level="INFO") as log:
check_palindromes(["aA"])
self.assertIn("'aA' is a palindrome.", log.output)

def test_special_characters(self):
"""
Test: Check if special characters are correctly removed and palindrome check works.
"""
with self.assertLogs(level="INFO") as log:
check_palindromes(["1.232.1"])
self.assertIn("'1.232.1' is a palindrome.", log.output)
def test_non_string_elements(self):
"""Test when input list contains non-string elements."""
with self.assertRaises(TypeError):
check_palindromes([123, 456])


if __name__ == "__main__":
Expand Down

0 comments on commit d0ee4d8

Please sign in to comment.