Домашни > Bang the ranga! > Решения > Решението на Гергана Панделиева

Резултати
3 точки от тестове
0 точки от учител

3 точки общо

10 успешни теста
0 неуспешни теста
Код

 1import unittest
 2from unittest.mock import patch, mock_open
 3from bangaranga import does_the_banga_rang, TheBangaDoesNotRangError
 4
 5
 6class TestDoesTheBangaRang(unittest.TestCase):
 7
 8    @patch("builtins.open", new_callable=mock_open, read_data="bangaranga")
 9    def test_single_word_match(self, mock_file):
10        self.assertEqual(does_the_banga_rang("file.txt"), 1)
11
12    @patch("builtins.open", new_callable=mock_open, read_data="banga the ranga")
13    def test_two_words(self, mock_file):
14        self.assertEqual(does_the_banga_rang("file.txt"), 2)
15
16    @patch("builtins.open", new_callable=mock_open, read_data="bang a small ranga")
17    def test_three_words(self, mock_file):
18        self.assertEqual(does_the_banga_rang("file.txt"), 3)
19
20    # Corner cases
21    @patch("builtins.open", new_callable=mock_open, read_data="BaNgA ThE RaNgA")
22    def test_lower_upper_case(self, mock_file):
23        self.assertEqual(does_the_banga_rang("file.txt"), 2)
24
25    @patch("builtins.open", new_callable=mock_open, read_data="bang a banga ranga bangaranga")
26    def test_returns_minimum_word_count(self, mock_file):
27        self.assertEqual(does_the_banga_rang("file.txt"), 1)
28
29    @patch("builtins.open", new_callable=mock_open, read_data="banga-ranga")
30    def test_word_boundaries_with_punctuation(self, mock_file):
31        #\b
32        self.assertEqual(does_the_banga_rang("file.txt"), 2)
33    
34    # Without valid solution
35    @patch("builtins.open", new_callable=mock_open, read_data="ranga is banga")
36    def test_out_of_order_zero(self, mock_file):
37        self.assertEqual(does_the_banga_rang("file.txt"), 0)
38
39    @patch("builtins.open", new_callable=mock_open, read_data="xbangarangax")
40    def test_substring_without_word_boundaries_zero(self, mock_file):
41        #\b
42        self.assertEqual(does_the_banga_rang("file.txt"), 0)
43
44    @patch("builtins.open", new_callable=mock_open, read_data="")
45    def test_empty_file_zero(self, mock_file):
46        self.assertEqual(does_the_banga_rang("file.txt"), 0)
47
48    # Error Handling
49    @patch("builtins.open")
50    def test_exception_on_io_error(self, mock_file):
51        #open/read
52        mock_file.side_effect = OSError("File not found")
53        with self.assertRaises(TheBangaDoesNotRangError):
54            does_the_banga_rang("non_existent_file.txt")
55
56
57if __name__ == "__main__":
58    unittest.main()

..........
----------------------------------------------------------------------
Ran 10 tests in 0.289s

OK

Дискусия
Гергана Панделиева
29.05.2026 12:50

за test_exception_on_io_error си помогнах с интернет
История
Това решение има само една версия.