1import unittest
2from unittest.mock import patch, mock_open
3from bangaranga import does_the_banga_rang, TheBangaDoesNotRangError
4
5class TestDoesTheBangaRang(unittest.TestCase):
6 @patch('builtins.open', new_callable=mock_open, read_data="bangaranga")
7 def test_single_word_match(self, mock_file):
8 result = does_the_banga_rang("dummy.txt")
9 self.assertEqual(result, 1)
10
11 @patch('builtins.open', new_callable=mock_open, read_data="The banga is doing some ranga!")
12 def test_two_words_in_sentence(self, mock_file):
13 result = does_the_banga_rang("dummy.txt")
14 self.assertEqual(result, 2)
15
16 @patch('builtins.open', new_callable=mock_open, read_data="The baNGa is doing some RAngA!")
17 def test_case_insensitivity(self, mock_file):
18 result = does_the_banga_rang("dummy.txt")
19 self.assertEqual(result, 2)
20
21 @patch('builtins.open', new_callable=mock_open, read_data="bang a banga ranga bangaranga")
22 def test_correct_search_length(self, mock_file):
23 result = does_the_banga_rang("dummy.txt")
24 self.assertEqual(result, 1)
25
26 @patch('builtins.open', new_callable=mock_open, read_data="b a n g a r a n g a")
27 def test_with_letters(self, mock_file):
28 result = does_the_banga_rang("dummy.txt")
29 self.assertEqual(result, 10)
30
31 @patch('builtins.open', new_callable=mock_open, read_data="banga,ranga.")
32 def test_regex_b(self, mock_file):
33 result = does_the_banga_rang("dummy.txt")
34 self.assertEqual(result, 2)
35
36 @patch('builtins.open', new_callable=mock_open, read_data="ranga is banga")
37 def test_order_correctness(self, mock_file):
38 result = does_the_banga_rang("dummy.txt")
39 self.assertEqual(result, 0)
40
41 @patch('builtins.open', new_callable=mock_open, read_data="bang a rang")
42 def test_particle_match(self, mock_file):
43 result = does_the_banga_rang("dummy.txt")
44 self.assertEqual(result, 0)
45
46 @patch('builtins.open', new_callable=mock_open, read_data="spam_banga ranga_eggs")
47 def test_subwords(self, mock_file):
48 result = does_the_banga_rang("dummy.txt")
49 self.assertEqual(result, 0)
50
51 @patch('builtins.open', new_callable=mock_open, read_data="")
52 def test_empty_file(self, mock_file):
53 result = does_the_banga_rang("dummy.txt")
54 self.assertEqual(result, 0)
55
56 @patch('builtins.open')
57 def test_error(self, mock_file):
58 mock_file.side_effect = OSError("Mocked OS Error")
59 with self.assertRaises(TheBangaDoesNotRangError):
60 does_the_banga_rang("missing_file.txt")
61
62if __name__ == '__main__':
63 unittest.main()
..........
----------------------------------------------------------------------
Ran 10 tests in 1.826s
OK
Виктор Бечев
31.05.2026 18:03Чудесно, нямам коментари.
|