Passed
Pull Request — master (#3)
by Guibert
49s
created

test_control.test_sequence()   A

Complexity

Conditions 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 23
rs 9.65
c 0
b 0
f 0
cc 1
nop 0
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