1import unittest
2import io
3from enum import Enum
4from secret import validate_recipe, RuinedNikuldenDinnerError
5import string
6import random
7
8
9def generate_random_string(length):
10 valid_chars = string.ascii_letters + string.digits + string.punctuation
11 return "".join([random.choice(valid_chars) for _ in range(length)])
12
13
14class ValidStuff(Enum):
15 RIBA = "риба"
16 RIBENA = "рибена"
17 ШARAN = "шаран"
18 SYONGA = "сьонга"
19
20
21def permutify(value):
22 lower_c = value[0].lower()
23 upper_c = value[0].upper()
24
25 if len(value) == 1:
26 return [
27 lower_c,
28 upper_c
29 ]
30
31 permutations = []
32
33 for p in permutify(value[1:]):
34 permutified = "".join(p)
35 permutations.append(f"{lower_c}{permutified}")
36 permutations.append(f"{upper_c}{permutified}")
37
38 return permutations
39
40
41class TestNikuldenValidator(unittest.TestCase):
42
43 def test_valid_recipe(self):
44 for valid_ingredient in ValidStuff:
45 for valid_test_case in permutify(valid_ingredient):
46 self.assertTrue(validate_recipe(io.StringIO(valid_test_case)))
47
48 def test_invalid_recipe(self):
49 self.assertFalse(validate_recipe(io.StringIO("")))
50 for valid_ingredient in ValidStuff:
51 for valid_test_case in permutify(valid_ingredient):
52 self.assertFalse(validate_recipe(io.StringIO(valid_test_case[1:])))
53 self.assertFalse(validate_recipe(io.StringIO(valid_test_case[:-1])))
54 self.assertFalse(validate_recipe(io.StringIO(valid_test_case[::2])))
55 self.assertFalse(validate_recipe(io.StringIO(generate_random_string(1000))))
56
57 def test_bad_recipe_file(self):
58 with self.assertRaises(RuinedNikuldenDinnerError):
59 validate_recipe()
60 with self.assertRaises(RuinedNikuldenDinnerError):
61 validate_recipe(3)
62 with self.assertRaises(RuinedNikuldenDinnerError):
63 validate_recipe(type.__annotations__)
64 with self.assertRaises(RuinedNikuldenDinnerError):
65 validate_recipe('nematakuvfail.txt')
FFFF
======================================================================
FAIL: test_naive_in_validator (test.TestTestNikuldenValidator.test_naive_in_validator)
Test with implementation missing word splits.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 95, in test_naive_in_validator
self.assertTrue(result.wasSuccessful(),
AssertionError: False is not true : Expecting test_bad_recipe_file to pass with an implementation missing word splits.
======================================================================
FAIL: test_no_error_handling_validator (test.TestTestNikuldenValidator.test_no_error_handling_validator)
Test with implementation missing error handling.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 80, in test_no_error_handling_validator
self.assertTrue(result.wasSuccessful(),
AssertionError: False is not true : Expecting test_invalid_recipe to pass with an implementation with error handling.
======================================================================
FAIL: test_no_lower_validator (test.TestTestNikuldenValidator.test_no_lower_validator)
Test with implementation missing lower.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 65, in test_no_lower_validator
self.assertTrue(result.wasSuccessful(),
AssertionError: False is not true : Expecting test_invalid_recipe to pass with an implementation missing lower.
======================================================================
FAIL: test_valid_validator (test.TestTestNikuldenValidator.test_valid_validator)
Test with a valid implementation.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 50, in test_valid_validator
self.assertTrue(result.wasSuccessful(),
AssertionError: False is not true : Expecting test_invalid_recipe to pass with valid implementation.
----------------------------------------------------------------------
Ran 4 tests in 0.009s
FAILED (failures=4)
Виктор Бечев
07.12.2024 12:09Стъпка едно е "`validate_recipe` приема **име** на файл". Оттам и тестовете фейлват.
|