test_water_jars.Tests.check_solution()   F
last analyzed

Complexity

Conditions 16

Size

Total Lines 30
Code Lines 27

Duplication

Lines 29
Ratio 96.67 %

Importance

Changes 0
Metric Value
cc 16
eloc 27
nop 4
dl 29
loc 30
rs 2.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like test_water_jars.Tests.check_solution() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import unittest
2
3
from water_jars import checkio
4
5
6
class Tests(unittest.TestCase):
7
    TESTS = {
8
        "Basics": [
9
            {
10
                "input": [5, 7, 6],
11
                "answer": ['02', '21', '10', '21', '02', '21', '10', '21', '02', '21'],
12
            },
13
            {"input": [3, 4, 1], "answer": ['02', '21']},
14
            {"input": [2, 1, 1], "answer": ['02']},
15
            {"input": [8, 5, 2], "answer": ['02', '21', '02', '21']},
16
            {"input": [9, 8, 7], "answer": ['02', '21', '02', '21']},
17
            {"input": [8, 10, 4], "answer": ['02', '21', '10', '21', '02', '21']},
18
            {"input": [2, 7, 1], "answer": ['02', '21', '10', '21', '10', '21']},
19
            {
20
                "input": [5, 8, 7],
21
                "answer": ['01', '12', '01', '12', '20', '12', '01', '12'],
22
            },
23
        ],
24
        "Extra": [
25
            {"input": [9, 1, 6], "answer": ['01', '12', '20', '12', '20', '12']},
26
            {"input": [7, 2, 4], "answer": ['02', '21', '02', '21']},
27
            {
28
                "input": [8, 1, 4],
29
                "answer": ['01', '12', '20', '12', '20', '12', '20', '12'],
30
            },
31
        ],
32
    }
33
34 View Code Duplication
    def check_solution(self, func, initial_data, max_steps):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
35
        first_volume, second_volume, goal = initial_data
36
        actions = {
37
            "01": lambda f, s: (first_volume, s),
38
            "02": lambda f, s: (f, second_volume),
39
            "12": lambda f, s: (
40
                f - (second_volume - s if f > second_volume - s else f),
41
                second_volume if f > second_volume - s else s + f,
42
            ),
43
            "21": lambda f, s: (
44
                first_volume if s > first_volume - f else s + f,
45
                s - (first_volume - f if s > first_volume - f else s),
46
            ),
47
            "10": lambda f, s: (0, s),
48
            "20": lambda f, s: (f, 0),
49
        }
50
        first, second = 0, 0
51
        result = func(*initial_data)
52
        if len(result) > max_steps:
53
            print("You answer contains too many steps. It can be shorter.")
54
            return False
55
        for act in result:
56
            if act not in actions.keys():
57
                print("I don't know this action {0}".format(act))
58
                return False
59
            first, second = actions[act](first, second)
60
        if goal == first or goal == second:
61
            return True
62
        print("You did not reach the goal.")
63
        return False
64
65
    def test_Basics(self):
66
        for i in self.TESTS['Basics']:
67
            assert self.check_solution(checkio, i['input'], len(i['answer']))
68
69
    def test_Extra(self):
70
        for i in self.TESTS['Extra']:
71
            assert self.check_solution(checkio, i['input'], len(i['answer']))
72
73
74
if __name__ == "__main__":  # pragma: no cover
75
    unittest.main()
76