1import os
2import tempfile
3import unittest
4
5from bangaranga import does_the_banga_rang, TheBangaDoesNotRangError
6
7
8class TestDoesTheBangaRang(unittest.TestCase):
9 def make_file(self, content):
10 file = tempfile.NamedTemporaryFile(
11 mode="w",
12 encoding="utf-8",
13 delete=False
14 )
15
16 with file:
17 file.write(content)
18
19 self.addCleanup(lambda: os.path.exists(file.name) and os.remove(file.name))
20 return file.name
21
22 def check_content(self, content, expected):
23 filename = self.make_file(content)
24 self.assertEqual(does_the_banga_rang(filename), expected)
25
26 def test_finds_bangaranga_as_one_whole_word(self):
27 self.check_content("some words before bangaranga and after", 1)
28
29 def test_finds_two_words_in_correct_order(self):
30 self.check_content("The banga is doing some ranga!", 2)
31
32 def test_finds_three_words_with_punctuation_between_them(self):
33 self.check_content("Well... bang, a small ranga appears.", 3)
34
35 def test_returns_minimal_number_of_words_when_multiple_options_exist(self):
36 self.check_content("bang a banga ranga bangaranga", 1)
37
38 def test_is_case_insensitive(self):
39 self.check_content("Something BaNgA something else RaNgA!", 2)
40
41 def test_words_must_be_in_the_correct_order(self):
42 self.check_content("ranga is here before banga", 0)
43
44 def test_does_not_use_parts_inside_bigger_words(self):
45 self.check_content("xxbangaranga bangarangaxx", 0)
46
47 def test_respects_regex_word_boundaries_with_digits_and_underscore(self):
48 self.check_content("bangaranga1 _bangaranga bangaranga_", 0)
49
50 def test_close_but_not_enough_words_returns_zero(self):
51 self.check_content("Does the banga rang?", 0)
52
53 def test_empty_file_returns_zero(self):
54 self.check_content("", 0)
55
56 def test_missing_file_raises_custom_error(self):
57 missing_file = os.path.join(tempfile.gettempdir(), "surely_missing_bangaranga_file.txt")
58
59 if os.path.exists(missing_file):
60 os.remove(missing_file)
61
62 with self.assertRaises(TheBangaDoesNotRangError):
63 does_the_banga_rang(missing_file)
64
65
66if __name__ == "__main__":
67 unittest.main()
..........
----------------------------------------------------------------------
Ran 10 tests in 0.087s
OK
31.05.2026 18:21