1import unittest
2
3import solution
4
5
6class TestSanity(unittest.TestCase):
7 """Check if the function is present."""
8
9 def test_function(self):
10 self.assertIn('function_that_says_ni', dir(solution), 'Убеди се, че функцията "function_that_says_ni" е налична с точно това име.')
11 self.assertTrue(callable(solution.function_that_says_ni), 'Убеди се, че "function_that_says_ni" е функция.')
12
13
14if __name__ == '__main__':
15 unittest.main()
1import unittest
2
3from solution import *
4
5
6class TestNi(unittest.TestCase):
7 """Test all requirements related to the function that says 'Ni!'."""
8
9 NI = 'Ni!'
10
11 def test_empty(self):
12 """Test with empty input."""
13 self.assertEqual(function_that_says_ni(), self.NI)
14
15 def test_single_with_no_named_args(self):
16 """Test with a single shrub that is passed as positional argument."""
17 self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 3.12}), '3.12лв')
18
19 def test_all_possible_shrub_strings(self):
20 """Test with all possible strings that define a shrub."""
21 self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 1}), '1.00лв')
22 self.assertEqual(function_that_says_ni({'name': 'bush', 'cost': 1}), '1.00лв')
23 self.assertEqual(function_that_says_ni({'name': 'храст', 'cost': 1}), '1.00лв')
24 self.assertEqual(function_that_says_ni({'name': 'SHrub', 'cost': 1}), '1.00лв')
25 self.assertEqual(function_that_says_ni({'name': 'buSH', 'cost': 1}), '1.00лв')
26 self.assertEqual(function_that_says_ni({'name': 'хрАст', 'cost': 1}), '1.00лв')
27
28 def test_with_no_cost(self):
29 """Test with a shrub without defined cost."""
30 self.assertEqual(function_that_says_ni({'name': 'shrub'}), self.NI)
31
32 def test_multiple_shrubs_sumс(self):
33 """Test with a multiple shrubs and cornercase costs."""
34 self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 20.00},
35 {'name': 'shrub', 'cost': 22}), '42.00лв')
36 self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 19.99},
37 {'name': 'shrub', 'cost': 22}), '41.99лв')
38 self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 20.01},
39 {'name': 'shrub', 'cost': 22}), self.NI)
40
41 def test_cost_whole_part_zero(self):
42 """Test with a total cost part equal to zero."""
43 self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 0.1},
44 {'name': 'храст', 'cost': 0.2}), self.NI)
45
46 def test_named_arguments(self):
47 """Test with named arguments."""
48 # Case with whole-part cost exactly
49 # devided by number of unique chars = 7
50 self.assertEqual(function_that_says_ni(_abcde={'name': 'shrub', 'cost': 2.82},
51 _abcf={'name': 'bush', 'cost': 4.51}), '7.33лв')
52 # Case with whole-part cost exactly
53 # devided by number of ALL chars (3),
54 # but not by number of unique chars (2)
55 self.assertEqual(function_that_says_ni(a={'name': 'shrub', 'cost': 1},
56 ab={'name': 'bush', 'cost': 2}), self.NI)
57
58 def test_combination_of_arguments(self):
59 """Test with combination of named and positional arguments."""
60 self.assertEqual(function_that_says_ni({'name': 'bush', 'cost': 1},
61 {'name': 'храст', 'cost': 1},
62 a={'name': 'shrub', 'cost': 0},
63 ab={'name': 'bush', 'cost': 0}), '2.00лв')
64
65 def test_invalid_strings(self):
66 """Test with invalid strings that might be misinterpreted."""
67 self.assertEqual(function_that_says_ni({'no_name': 'bush', 'cost': 1}), self.NI)
68 self.assertEqual(function_that_says_ni({'name': 'not a shrub', 'cost': 1}), self.NI)
69 self.assertEqual(function_that_says_ni({'name': 'shrub', ' cost': 1}), self.NI) # Space before cost
70
71
72 def test_other_than_dicts(self):
73 """Test with inputs other than dicts."""
74 self.assertEqual(function_that_says_ni(1, 3.14, ['some_list'], some_arg={1, 2, 3}), self.NI)
75
76
77if __name__ == '__main__':
78 unittest.main()
Георги Кунчев
16.10.2024 11:42Тъй като сме написали само букви и "_", няма да тестваме с цифри, но едва ли наличието на цифри би повлияло на имплементацията ти.
|
Николай Стоянов
16.10.2024 11:22Здравейте. Имената на именувани аргументи могат ли да съдържат цифри?
|