aboutsummaryrefslogtreecommitdiff
blob: ffd6d36300ef18d5f47f2c6300df5b1010d3ead3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from snakeoil import currying
from snakeoil import dependant_methods as dm


def func(self, seq, data, val=True):
    seq.append(data)
    return val


class TestDependantMethods:

    @staticmethod
    def generate_instance(methods, dependencies):
        class Class(metaclass=dm.ForcedDepends):
            stage_depends = dict(dependencies)

            locals().update(list(methods.items()))

        return Class()

    def test_no_dependant_methods(self):
        assert self.generate_instance({}, {})

    def test_return_checking(self):
        results = []
        o = self.generate_instance(
            {str(x): currying.post_curry(func, results, x) for x in range(10)},
            {str(x): str(x - 1) for x in range(1, 10)})
        getattr(o, "9")()
        assert results == list(range(10))
        results = []
        o = self.generate_instance(
            {str(x): currying.post_curry(func, results, x, False) for x in range(10)},
            {str(x): str(x - 1) for x in range(1, 10)})
        getattr(o, "9")()
        assert results == [0]
        getattr(o, "9")()
        assert results == [0, 0]

    def test_stage_awareness(self):
        results = []
        o = self.generate_instance(
            {str(x): currying.post_curry(func, results, x) for x in range(10)},
            {str(x): str(x - 1) for x in range(1, 10)})
        getattr(o, "1")()
        assert results == [0, 1]
        getattr(o, "2")()
        assert results == [0, 1, 2]
        getattr(o, "2")()
        assert results == [0, 1, 2]
        o.__set_stage_state__(["0", "1"])
        l = []
        o.__stage_step_callback__ = l.append
        getattr(o, "2")()
        assert results == [0, 1, 2, 2]
        assert l == ["2"]

    def test_stage_depends(self):
        results = []
        methods = {str(x): currying.post_curry(func, results, x) for x in range(10)}
        deps = {str(x): str(x - 1) for x in range(1, 10)}
        deps["1"] = ["0", "a"]
        methods["a"] = currying.post_curry(func, results, "a")
        o = self.generate_instance(methods, deps)
        getattr(o, "1")()
        assert results == [0, "a", 1]
        getattr(o, "2")()
        assert results == [0, "a", 1, 2]

    def test_ignore_deps(self):
        results = []
        o = self.generate_instance(
            {str(x): currying.post_curry(func, results, x) for x in range(10)},
            {str(x): str(x - 1) for x in range(1, 10)})
        getattr(o, '2')(ignore_deps=True)
        assert [2] == results

    def test_no_deps(self):
        results = []
        o = self.generate_instance(
            {str(x): currying.post_curry(func, results, x) for x in range(10)},
            {})
        getattr(o, '2')()
        assert [2] == results