Skip to content

Commit 9e1709f

Browse files
author
Matthew Shirtliffe
committed
anagram checker with tests
1 parent 05d81c4 commit 9e1709f

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

anagram_checker/app.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
3+
def print_header():
4+
""" Print header
5+
"""
6+
header_text = ''
7+
TEXT = f' Anagram Checker \n'
8+
line = '-' * len(TEXT)
9+
line += '\n'
10+
11+
header_text += line
12+
header_text += TEXT
13+
header_text += line
14+
print(header_text)
15+
16+
17+
def prompt_get_strings():
18+
print('Enter two strings and I\'ll tell you if they\nare anagrams: ')
19+
20+
21+
def get_first_string():
22+
23+
while True:
24+
first_string = input('Enter the first string: ')
25+
26+
if len(first_string) >=3:
27+
return first_string
28+
else:
29+
print('A valid input is required')
30+
31+
32+
def get_second_string():
33+
34+
while True:
35+
second_string = input('Enter the second string: ')
36+
37+
if len(second_string) >=3:
38+
return second_string
39+
else:
40+
print('A valid input is required')
41+
42+
43+
def is_anagram(first_string, second_string):
44+
45+
return sorted(first_string) == sorted(second_string)
46+
47+
48+
def print_is_anagram(first_string, second_string):
49+
if(is_anagram(first_string, second_string)):
50+
print(f'{first_string} and {second_string} are anagrams.')
51+
52+
if __name__ == "__main__":
53+
54+
print_header()
55+
prompt_get_strings()
56+
first_string = get_first_string()
57+
second_string = get_second_string()
58+
print_is_anagram(first_string, second_string)

anagram_checker/tests/__init__.py

Whitespace-only changes.

anagram_checker/tests/system/__init__.py

Whitespace-only changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
import app
4+
5+
class AppTest(TestCase):
6+
7+
def test_print_header(self):
8+
9+
expected = '------------------\n Anagram Checker \n------------------\n'
10+
with patch('builtins.print') as mocked_print:
11+
app.print_header()
12+
mocked_print.assert_called_with(expected)
13+
14+
def test_prompt_get_strings(self):
15+
16+
expected = 'Enter two strings and I\'ll tell you if they\nare anagrams: '
17+
with patch('builtins.print') as mocked_print:
18+
app.prompt_get_strings()
19+
mocked_print.assert_called_with(expected)
20+
21+
def test_get_first_string(self):
22+
23+
expected = 'Enter the first string: '
24+
with patch('builtins.input') as mocked_input:
25+
with patch('builtins.print') as mocked_print:
26+
mocked_input.side_effect = ('' , 'note')
27+
first_string = app.get_first_string()
28+
mocked_print.assert_called_with('A valid input is required')
29+
mocked_input.assert_called_with(expected)
30+
self.assertEqual(first_string, 'note')
31+
32+
def test_get_first_string(self):
33+
34+
expected = 'Enter the second string: '
35+
with patch('builtins.input') as mocked_input:
36+
with patch('builtins.print') as mocked_print:
37+
mocked_input.side_effect = ('' , 'tone')
38+
second_string = app.get_second_string()
39+
mocked_print.assert_called_with('A valid input is required')
40+
mocked_input.assert_called_with(expected)
41+
self.assertEqual(second_string, 'tone')
42+
43+
44+
def test_print_is_anagram(self):
45+
46+
first_string = 'note'
47+
second_string = 'tone'
48+
49+
expected = f'{first_string} and {second_string} are anagrams.'
50+
with patch('builtins.print') as mocked_print:
51+
app.print_is_anagram(first_string, second_string)
52+
mocked_print.assert_called_with(expected)

anagram_checker/tests/unit/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from unittest import TestCase
2+
import app
3+
4+
class AnagramTest(TestCase):
5+
6+
def test_is_anagram(self):
7+
is_anagram = app.is_anagram('tone', 'note')
8+
self.assertTrue(is_anagram)

0 commit comments

Comments
 (0)