Passed
Pull Request — master (#3)
by Guibert
01:15
created

test_control   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 55
dl 0
loc 89
rs 10
c 0
b 0
f 0

10 Functions

Rating   Name   Duplication   Size   Complexity  
A exception_func() 0 2 1
A test_fallback() 0 4 1
A a_func() 0 2 1
A b_func() 0 2 1
A test_selector() 0 4 1
A failure_func() 0 2 1
A test_decision() 0 6 1
A success_func() 0 2 1
A test_repeat_until() 0 14 3
A test_sequence() 0 23 1
1
from contextvars import ContextVar
2
3
from curio import run
4
5
from libellule.control_flow.common import *
6
from libellule.control_flow.control import *
7
8
9
async def a_func():
10
    return "a"
11
12
13
async def b_func():
14
    return "b"
15
16
17
async def failure_func():
18
    return FAILURE
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable FAILURE does not seem to be defined.
Loading history...
19
20
21
async def success_func():
22
    return SUCCESS
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SUCCESS does not seem to be defined.
Loading history...
23
24
25
async def exception_func():
26
    raise RuntimeError()
27
28
29
def test_sequence():
30
    assert not run(
31
        sequence(children=[a_func, failure_func, success_func])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sequence does not seem to be defined.
Loading history...
32
    ), "default behaviour fail of one failed"
33
34
    assert run(
35
        sequence(children=[a_func, failure_func, success_func], succes_threshold=2)
36
    )
37
    assert run(
38
        sequence(children=[a_func, success_func, failure_func], succes_threshold=2)
39
    )
40
    assert run(
41
        sequence(children=[failure_func, a_func, success_func], succes_threshold=2)
42
    )
43
44
    assert run(
45
        sequence(children=[exception_func, failure_func, a_func], succes_threshold=1)
46
    )
47
    assert not run(
48
        sequence(children=[exception_func, failure_func], succes_threshold=1)
49
    )
50
51
    assert not run(sequence(children=[]))
52
53
54
def test_fallback():
55
    assert run(fallback(children=[exception_func, failure_func, a_func]))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable fallback does not seem to be defined.
Loading history...
56
    assert not run(fallback(children=[exception_func, failure_func]))
57
    assert not run(fallback(children=[]))
58
59
60
def test_selector():
61
    assert run(selector(children=[exception_func, failure_func, a_func]))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable selector does not seem to be defined.
Loading history...
62
    assert not run(selector(children=[exception_func, failure_func]))
63
    assert selector(children=[]).__node_metadata.name == "selector"
64
65
66
def test_decision():
67
    assert run(decision(condition=success_func, success_tree=a_func)) == "a"
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable decision does not seem to be defined.
Loading history...
68
    assert not run(decision(condition=failure_func, success_tree=a_func))
69
    assert (
70
        run(decision(condition=failure_func, success_tree=a_func, failure_tree=b_func))
71
        == "b"
72
    )
73
74
75
def test_repeat_until():
76
77
    counter = ContextVar("counter", default=5)
78
79
    async def tick():
80
        value = counter.get()
81
        counter.set(value - 1)
82
        if value <= 0:
83
            return FAILURE
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable FAILURE does not seem to be defined.
Loading history...
84
        if value == 3:
85
            raise RuntimeError("3")
86
        return SUCCESS
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SUCCESS does not seem to be defined.
Loading history...
87
88
    assert run(repeat_until(condition=tick, child=a_func)) == "a"
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable repeat_until does not seem to be defined.
Loading history...
89