Домашни > Bang the ranga! > Решения > Решението на Емилияна Атанасова

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

2 точки общо

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

 1import unittest
 2from pathlib import Path
 3from tempfile import TemporaryDirectory
 4
 5from bangaranga import does_the_banga_rang, TheBangaDoesNotRangError
 6
 7class DoesTheBangaRangTests(unittest.TestCase):
 8        def create_file(self, directory, name, content):
 9            path = Path(directory) / name
10            path.write_text(content)
11            return path
12
13        def test_returns_minimum_number_of_words(self):
14            with TemporaryDirectory() as directory:
15                file_path = self.create_file(
16                    directory,
17                    "multiple.txt",
18                    "bang a banga ranga bangaranga",
19                )
20
21                self.assertEqual(1, does_the_banga_rang(file_path))
22
23        def test_finds_bangaranga_from_two_words(self):
24            with TemporaryDirectory() as directory:
25                file_path = self.create_file(
26                    directory,
27                    "two_words.txt",
28                    "The banga is doing some ranga!",
29                )
30
31                self.assertEqual(2, does_the_banga_rang(file_path))
32
33        def test_finds_bangaranga_from_multiple_words(self):
34            with TemporaryDirectory() as directory:
35                file_path = self.create_file(
36                    directory,
37                    "three_words.txt",
38                    "bang a small ranga",
39                )
40
41                self.assertEqual(3, does_the_banga_rang(file_path))
42
43        def test_returns_zero_when_words_cannot_form_bangaranga(self):
44            with TemporaryDirectory() as directory:
45                file_path = self.create_file(
46                    directory,
47                    "invalid.txt",
48                    "Does the banga rang?",
49                )
50
51                self.assertEqual(0, does_the_banga_rang(file_path))
52
53        def test_words_must_be_in_correct_order(self):
54            with TemporaryDirectory() as directory:
55                file_path = self.create_file(
56                    directory,
57                    "wrong_order.txt",
58                    "ranga comes before banga",
59                )
60
61                self.assertEqual(0, does_the_banga_rang(file_path))
62
63        def test_matches_only_whole_words(self):
64            with TemporaryDirectory() as directory:
65                file_path = self.create_file(
66                    directory,
67                    "whole_words.txt",
68                    "xxbanga rangaxx",
69                )
70
71                self.assertEqual(0, does_the_banga_rang(file_path))
72
73        def test_returns_zero_when_file_contains_no_matching_words(self):
74            with TemporaryDirectory() as directory:
75                file_path = self.create_file(
76                    directory,
77                    "no_match.txt",
78                    "nothing useful here",
79                )
80
81                self.assertEqual(0, does_the_banga_rang(file_path))
82
83        def test_raises_custom_error_when_file_cannot_be_read(self):
84            with self.assertRaises(TheBangaDoesNotRangError):
85                does_the_banga_rang("missing_file.txt")
86
87if __name__ == "__main__":
88    unittest.main()

.F........
======================================================================
FAIL: test_student_tests_catch_bad_implementation_case_sensitive (test.StudentTestSuiteEvaluationTests.test_student_tests_catch_bad_implementation_case_sensitive)
----------------------------------------------------------------------
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_case_sensitive'.
| Output:
| ----------------------------------------------------------------------
| Ran 8 tests in 0.002s
|
| OK

----------------------------------------------------------------------
Ran 10 tests in 0.069s

FAILED (failures=1)

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