Домашни > Bang the ranga! > Решения > Решението на Николина Маджарова

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

2 точки общо

10 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

 1import os
 2import tempfile
 3import unittest
 4from bangaranga import does_the_banga_rang, TheBangaDoesNotRangError
 5
 6class DoesTheBangaRangTests(unittest.TestCase):
 7    def create_temp_file(self, content):
 8        temp = tempfile.NamedTemporaryFile(mode="w", delete=False)
 9        self.addCleanup(lambda: os.remove(temp.name))
10        temp.write(content)
11        temp.close()
12
13        return temp.name
14
15    def test_returns_minimum_number_of_words(self):
16        file_name = self.create_temp_file("bang a banga ranga bangaranga")
17
18        self.assertEqual(1, does_the_banga_rang(file_name),
19            "The function should return the minimum number of words forming 'bangaranga'.")
20
21    def test_finds_bangaranga_from_two_words(self):
22        file_name = self.create_temp_file("The banga is doing some ranga!")
23
24        self.assertEqual(2, does_the_banga_rang(file_name),
25            "The function should detect 'bangaranga' formed by two words.")
26
27    def test_finds_bangaranga_from_multiple_words(self):
28        file_name = self.create_temp_file("bang a small ranga")
29
30        self.assertEqual(3, does_the_banga_rang(file_name),
31            "The function should detect 'bangaranga' formed by multiple consecutive words.")
32
33    def test_returns_zero_when_words_are_in_wrong_order(self):
34        file_name = self.create_temp_file("ranga before banga")
35
36        self.assertEqual(0, does_the_banga_rang(file_name),
37            "The function should return 0 when the words are in the wrong order.")
38
39    def test_returns_zero_when_bangaranga_cannot_be_formed(self):
40        file_name = self.create_temp_file("Does the banga rang?")
41
42        self.assertEqual(0, does_the_banga_rang(file_name),
43            "The function should return 0 when 'bangaranga' cannot be formed.")
44
45    def test_is_case_insensitive(self):
46        file_name = self.create_temp_file("BaNgA RAnGa")
47
48        self.assertEqual(2, does_the_banga_rang(file_name),
49            "The function should match words case-insensitively.")
50
51    def test_matches_only_whole_words(self):
52        file_name = self.create_temp_file("xxbangayy rangaxx")
53
54        self.assertEqual(0, does_the_banga_rang(file_name),
55            "The function should match only whole words.")
56
57    def test_ignores_non_letter_characters_around_words(self):
58        file_name = self.create_temp_file("banga...RANGA!")
59
60        self.assertEqual(2, does_the_banga_rang(file_name),
61            "The function should treat punctuation as word boundaries.")
62
63    def test_raises_custom_error_when_file_cannot_be_read(self):
64        with self.assertRaises(
65            TheBangaDoesNotRangError, msg="The function should raise TheBangaDoesNotRangError on read failure."
66        ):
67            does_the_banga_rang("missing_file.txt")
68
69
70if __name__ == "__main__":
71    unittest.main()

..........
----------------------------------------------------------------------
Ran 10 tests in 0.056s

OK

Дискусия
История
Това решение има само една версия.