1import os
2import tempfile
3import unittest
4
5from bangaranga import does_the_banga_rang, TheBangaDoesNotRangError
6
7
8class TestDoesTheBangaRang(unittest.TestCase):
9 def create_file(self, content):
10 temp_dir = tempfile.TemporaryDirectory()
11 self.addCleanup(temp_dir.cleanup)
12
13 path = os.path.join(temp_dir.name, "input.txt")
14
15 with open(path, "w", encoding="utf-8") as file:
16 file.write(content)
17
18 return path
19
20 def test_returns_one_when_bangaranga_is_single_word(self):
21 path = self.create_file("This file contains bangaranga inside.")
22
23 self.assertEqual(1, does_the_banga_rang(path))
24
25 def test_returns_two_when_banga_and_ranga_form_bangaranga(self):
26 path = self.create_file("The banga is doing some ranga!")
27
28 self.assertEqual(2, does_the_banga_rang(path))
29
30 def test_returns_three_when_bang_a_ranga_form_bangaranga(self):
31 path = self.create_file("bang a small ranga")
32
33 self.assertEqual(3, does_the_banga_rang(path))
34
35 def test_returns_minimum_number_of_words_when_multiple_solutions_exist(self):
36 path = self.create_file("bang a banga ranga bangaranga")
37
38 self.assertEqual(1, does_the_banga_rang(path))
39
40 def test_is_case_insensitive(self):
41 path = self.create_file("BANG A RANGA")
42
43 self.assertEqual(3, does_the_banga_rang(path))
44
45 def test_words_may_be_separated_by_punctuation(self):
46 path = self.create_file("Well... banga, finally: ranga!")
47
48 self.assertEqual(2, does_the_banga_rang(path))
49
50 def test_returns_zero_when_words_are_in_wrong_order(self):
51 path = self.create_file("ranga is before banga")
52
53 self.assertEqual(0, does_the_banga_rang(path))
54
55 def test_returns_zero_when_concatenation_does_not_match_exactly(self):
56 path = self.create_file("Does the banga rang?")
57
58 self.assertEqual(0, does_the_banga_rang(path))
59
60 def test_returns_zero_when_matching_parts_are_not_whole_words(self):
61 path = self.create_file("xxbanga ranga bangarangaxx")
62
63 self.assertEqual(0, does_the_banga_rang(path))
64
65 def test_raises_custom_error_when_file_cannot_be_read(self):
66 missing_path = os.path.join("directory_that_should_not_exist", "missing.txt")
67
68 with self.assertRaises(TheBangaDoesNotRangError):
69 does_the_banga_rang(missing_path)
70
71
72if __name__ == "__main__":
73 unittest.main()
..........
----------------------------------------------------------------------
Ran 10 tests in 0.081s
OK
31.05.2026 18:06
31.05.2026 18:06