test_commands   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 9

7 Functions

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