Passed
Push — dev ( 3c73d5...40dcc8 )
by Konstantinos
01:24
created

test_commands   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 62
dl 0
loc 84
rs 10
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A test_wrong_command() 0 9 2
A invoker() 0 4 1
A command_class() 0 4 1
A test_invoker() 0 31 1
A test_wrong_interface_usage() 0 5 2
A test_correct_command() 0 11 1
A command_interface() 0 4 1
1
import pytest
2
3
4
@pytest.fixture
5
def command_interface():
6
    from green_magic.utils.commands import CommandInterface
7
    return CommandInterface
8
9
10
@pytest.fixture
11
def command_class():
12
    from green_magic.utils.commands import Command
13
    return Command
14
15
16
@pytest.fixture
17
def invoker():
18
    from green_magic.utils.commands import Invoker, CommandHistory
19
    return Invoker(CommandHistory())
20
21
22
def test_wrong_interface_usage(command_interface):
23
    class WrongChildClass(command_interface): pass
24
    with pytest.raises(TypeError) as e:
25
        a = WrongChildClass()
26
        assert "Can't instantiate abstract class WrongChildClass with abstract methods execute" == str(e)
27
28
29
def test_correct_command(command_class):
30
    def add(a_list, *args):
31
        return a_list.extend([_ for _ in args])
32
33
    input_list = [1]
34
    cmd = command_class(add, '__call__', input_list, 20, 100)
35
    cmd.execute()
36
    assert input_list == [1, 20, 100]
37
    cmd.append_arg(-1)
38
    cmd.execute()
39
    assert input_list == [1, 20, 100, 20, 100, -1]
40
41
42
def test_wrong_command(command_class):
43
    def add(a_list, *args):
44
        return a_list.gg([_ for _ in args])
45
46
    input_list = [1]
47
    cmd = command_class(add, '__call__', input_list, 22)
48
    with pytest.raises(AttributeError) as e:
49
        cmd.execute()
50
        assert "'list' object has no attribute 'gg'" == str(e)
51
52
53
def test_invoker(invoker, command_class):
54
    import copy
55
    class A:
56
        def b(self, x):
57
            res = x + 1
58
            print(res)
59
    a = A()
60
61
    cmd1 = command_class(a, 'b', 2)
62
    invoker.execute_command(cmd1)
63
    assert invoker.history.stack == []
64
65
    class A:
66
        def b(self, x):
67
            return x + 1
68
69
    a = A()
70
    cmd2 = copy.copy(cmd1)
71
    cmd2.args = [12]
72
    invoker.execute_command(cmd2)
73
    assert invoker.history.stack == []
74
75
    cmd3 = command_class(a, 'b', -1)
76
    invoker.execute_command(cmd3)
77
    assert invoker.history.stack == [cmd3]
78
79
    del cmd2
80
    invoker.execute_command(cmd1)
81
    assert invoker.history.stack == [cmd3]
82
83
    assert cmd3 == invoker.history.stack.pop()
84