1from unittest.mock import mock_open, patch
2import unittest
3from bangaranga import does_the_banga_rang, TheBangaDoesNotRangError
4
5
6def bang_the_ranga(content):
7 with patch("builtins.open", mock_open(read_data=content)):
8 return does_the_banga_rang("any.txt")
9
10
11class TestDoesTheBangaRang(unittest.TestCase):
12 def banga_the_ranga_ception(self, exception):
13 with patch("builtins.open", side_effect=exception):
14 with self.assertRaises(TheBangaDoesNotRangError):
15 does_the_banga_rang("missing.txt")
16
17 def test_multiple_words_form_bangaranga(self):
18 self.assertEqual(bang_the_ranga("ban ga ran ga"), 4)
19
20 def test_multiple_words_with_noise(self):
21 self.assertEqual(bang_the_ranga("hello i will bang my a small ranga ranga danga"), 3)
22
23 def test_single_word_with_noise(self):
24 self.assertEqual(bang_the_ranga("fangaranga bangaranga dangadanga mangaranga"), 1)
25
26 def test_minimum_word_match(self):
27 self.assertEqual(bang_the_ranga("bang a banga ranga bangaranga"), 1)
28
29 def test_word_parts_in_order(self):
30 self.assertEqual(bang_the_ranga("ga ga ran banga"), 0)
31
32 def test_partial_match(self):
33 self.assertEqual(bang_the_ranga("Does a banga rang?"), 0)
34
35 def test_with_empty_file(self):
36 self.assertEqual(bang_the_ranga(""), 0)
37
38 def test_case_insensitive(self):
39 self.assertEqual(bang_the_ranga("BaNga rANgA"), 2)
40
41 def test_punctuation_splits_words_on_different_separators(self):
42 self.assertEqual(bang_the_ranga("b,a$n!g%a r'a\"n@g;()*&-_=\\|~`a"), 10)
43
44 def test_repeated_words_still_finds_minimum(self):
45 self.assertEqual(bang_the_ranga("banga ranga banga ranga banga ranga banga banga ranga banga"), 2)
46
47 def test_os_error_raises_custom_exception(self):
48 self.banga_the_ranga_ception(OSError)
49
50 def test_io_error_raises_custom_exception(self):
51 self.banga_the_ranga_ception(IOError)
52
53
54if __name__ == "__main__":
55 unittest.main()
....F.....
======================================================================
FAIL: test_student_tests_catch_bad_implementation_matches_partial_words (test.StudentTestSuiteEvaluationTests.test_student_tests_catch_bad_implementation_matches_partial_words)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 98, in test
self.assertTrue(
~~~~~~~~~~~~~~~^
bad_result["failed_test_ids"],
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
f"Student tests passed against incorrect implementation: {implementation_name!r}.\n{format_student_output(bad_result['output'])}",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
AssertionError: set() is not true : Student tests passed against incorrect implementation: 'bad_implementation_matches_partial_words'.
| Output:
| ----------------------------------------------------------------------
| Ran 12 tests in 0.014s
|
| OK
----------------------------------------------------------------------
Ran 10 tests in 1.704s
FAILED (failures=1)
Виктор Бечев
31.05.2026 18:11Важи, нямам забележки.
|