forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
A module for counting vowels in a string. | ||
Module contents: | ||
- count_vowels: Counts the number of vowels in a given string. | ||
It supports both uppercase and lowercase vowels. | ||
Created on: 2025-07-06 | ||
@author: Fatima | ||
""" | ||
|
||
|
||
def count_vowels(text: str) -> int: | ||
"""Returns the number of vowels (both uppercase and lowercase) in a string. | ||
Parameters: | ||
text: str, the input string to check | ||
Returns: | ||
int: Number of vowels in the text. | ||
Raises: | ||
AssertionError: if the argument is not a string | ||
>>> count_vowels("hello") | ||
2 | ||
>>> count_vowels("APPLE") | ||
2 | ||
>>> count_vowels("why") | ||
0 | ||
""" | ||
|
||
assert isinstance(text, str), "input must be a string" | ||
|
||
vowels = "aeiouAEIOU" | ||
count = 0 | ||
|
||
for letter in text: | ||
if letter in vowels: | ||
count += 1 | ||
|
||
return count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
Unit tests for validating the count_vowels function. | ||
Test Categories: | ||
- Standard Cases: Regular words with varying vowel counts. | ||
- Edge Cases: Empty strings, repeated vowels, and mixed casing. | ||
- Special Cases: Strings with numbers, special characters, and whitespace. | ||
- Defensive Cases: Ensuring proper handling of non-string inputs. | ||
Created on: 2025-07-06 | ||
@author: Fatima | ||
""" | ||
|
||
import unittest | ||
from solutions.count_vowels import count_vowels | ||
|
||
|
||
class TestCountVowels(unittest.TestCase): | ||
"""Test cases for count_vowels function.""" | ||
|
||
# Standard Cases | ||
def test_typical_word(self): | ||
"""It should count vowels in a regular word with both vowels and consonants.""" | ||
self.assertEqual(count_vowels("hello"), 2) | ||
|
||
def test_mixed_case_vowels(self): | ||
"""It should count both uppercase and lowercase vowels in a string.""" | ||
self.assertEqual(count_vowels("hellOWorld"), 3) | ||
|
||
def test_uppercase_vowels(self): | ||
"""It should count uppercase vowels in the string.""" | ||
self.assertEqual(count_vowels("SMALL"), 1) | ||
|
||
def test_lowercase_vowels(self): | ||
"""It should count lowercase vowels in the string.""" | ||
self.assertEqual(count_vowels("small"), 1) | ||
|
||
def test_repeated_vowels(self): | ||
"""It should count all repeated vowels in a string.""" | ||
self.assertEqual(count_vowels("aaeeiioouuAAEEIIOOUU"), 20) | ||
|
||
# Edge Cases | ||
def test_empty_string(self): | ||
"""It should return 0 for an empty string.""" | ||
self.assertEqual(count_vowels(""), 0) | ||
|
||
def test_only_whitespace(self): | ||
"""It should return 0 for strings with only whitespace..""" | ||
self.assertEqual(count_vowels(" "), 0) | ||
|
||
def test_no_vowels(self): | ||
"""It should return 0 for a string with no vowels.""" | ||
self.assertEqual(count_vowels("fly"), 0) | ||
|
||
def test_long_string(self): | ||
"""It should correctly count vowels in a long mixed-case string.""" | ||
text = ("aAeEiIoOuU" * 500) + "bcdfghjklmnpqrstvwxyz" * 100 | ||
self.assertEqual(count_vowels(text), 5000) | ||
|
||
# Special Cases | ||
def test_alphanumeric_string(self): | ||
"""It should count vowels in alphanumeric strings, ignoring digits.""" | ||
self.assertEqual(count_vowels("fatima123Malik"), 5) | ||
|
||
def test_special_characters(self): | ||
"""It should ignore special characters and only count vowels.""" | ||
self.assertEqual(count_vowels("!@#$%^&*()aeiOu"), 5) | ||
|
||
def test_string_with_whitespace(self): | ||
"""It should count vowels even if the string contains spaces.""" | ||
self.assertEqual(count_vowels("hello world"), 3) | ||
|
||
# Defensive Cases | ||
def test_invalid_input(self): | ||
"""Test that raises assertion error for non-string input.""" | ||
with self.assertRaises(AssertionError): | ||
count_vowels(None) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |