forked from learnbyexample/Python_Basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunittest_palindrome_main.py
More file actions
34 lines (25 loc) · 1.04 KB
/
unittest_palindrome_main.py
File metadata and controls
34 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/python3
import palindrome
import unittest
from unittest import mock
from io import StringIO
class TestPalindrome(unittest.TestCase):
@mock.patch('sys.stdout', new_callable=StringIO)
def main_op(self, tst_str, mock_stdout):
with mock.patch('builtins.input', side_effect=tst_str):
palindrome.main()
return mock_stdout.getvalue()
def test_valid(self):
for s in ('Malayalam', 'kek'):
self.assertEqual(self.main_op([s]), s + ' is a palindrome\n')
for s in ('zzz', 'cool'):
self.assertEqual(self.main_op([s]), s + ' is NOT a palindrome\n')
def test_error(self):
em1 = 'Error: Characters other than alphabets and punctuations\n'
em2 = 'Error: Less than 3 alphabets\n'
tst1 = em1 + 'Madam is a palindrome\n'
self.assertEqual(self.main_op(['123', 'Madam']), tst1)
tst2 = em2 + em1 + 'Jerry is NOT a palindrome\n'
self.assertEqual(self.main_op(['to', 'a2a', 'Jerry']), tst2)
if __name__ == '__main__':
unittest.main()