Completed
Branch master (ef56ad)
by Lambda
01:27
created

test_next_match()   C

Complexity

Conditions 11

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 11
dl 18
loc 18
rs 5.5714
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like test_next_match() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
from unittest.mock import MagicMock, patch
2
from neovim_prompt.history import History
3
import pytest
4
5
6
@pytest.fixture
7
def prompt(nvim, context):
8
    history_candidates = (
9
        'foo',
10
        'bar',
11
        'foobar',
12
        'hoge',
13
        'barhoge',
14
        'foobarhoge',
15
    )
16
17
    def histnr(histname):
18
        return len(history_candidates)
19
20
    def histget(histname, index):
21
        if index == 0:
22
            return ''
23
        return history_candidates[-(index+1)]
24
25
    def call(fname, *args):
26
        if fname == 'histnr':
27
            return histnr(*args)
28
        elif fname == 'histget':
29
            return histget(*args)
30
31
    Prompt = MagicMock(spec='neovim_prompt.prompt.Prompt')
32
    prompt = Prompt(nvim, context)
33
34
    prompt.nvim.call = MagicMock()
35
    prompt.nvim.call.side_effect = call
36
    return prompt
37
38
39
def test_current(prompt):
40
    prompt.text = 'fool'
41
    prompt.caret.get_backward_text.return_value = 'fo'
42
    history = History(prompt)
43
    assert history.current() == 'fool'
44
45
46
def test_previous(prompt):
47
    prompt.text = 'fool'
48
    prompt.caret.get_backward_text.return_value = 'fo'
49
    history = History(prompt)
50
    assert history.current() == 'fool'
51
52
    assert history.previous() == 'foo'
53
    assert history.current() == 'foo'
54
55
    assert history.previous() == 'bar'
56
    assert history.current() == 'bar'
57
58
    assert history.previous() == 'foobar'
59
    assert history.current() == 'foobar'
60
61
    assert history.previous() == 'hoge'
62
    assert history.current() == 'hoge'
63
64
    assert history.previous() == 'barhoge'
65
    assert history.current() == 'barhoge'
66
67
    assert history.previous() == 'foobarhoge'
68
    assert history.current() == 'foobarhoge'
69
70
    assert history.previous() == 'foobarhoge'
71
    assert history.current() == 'foobarhoge'
72
73
74
def test_next(prompt):
75
    prompt.text = 'fool'
76
    prompt.caret.get_backward_text.return_value = 'fo'
77
    history = History(prompt)
78
    [history.previous() for i in range(6)]
79
    assert history.current() == 'foobarhoge'
80
81
    assert history.next() == 'barhoge'
82
    assert history.current() == 'barhoge'
83
84
    assert history.next() == 'hoge'
85
    assert history.current() == 'hoge'
86
87
    assert history.next() == 'foobar'
88
    assert history.current() == 'foobar'
89
90
    assert history.next() == 'bar'
91
    assert history.current() == 'bar'
92
93
    assert history.next() == 'foo'
94
    assert history.current() == 'foo'
95
96
    assert history.next() == 'fool'
97
    assert history.current() == 'fool'
98
99
    assert history.next() == 'fool'
100
    assert history.current() == 'fool'
101
102
103
def test_previous_match(prompt):
104
    prompt.text = 'fool'
105
    prompt.caret.get_backward_text.return_value = 'fo'
106
    history = History(prompt)
107
    assert history.current() == 'fool'
108
109
    assert history.previous_match() == 'foo'
110
    assert history.current() == 'foo'
111
112
    assert history.previous_match() == 'foobar'
113
    assert history.current() == 'foobar'
114
115
    assert history.previous_match() == 'foobarhoge'
116
    assert history.current() == 'foobarhoge'
117
118
    assert history.previous_match() == 'foobarhoge'
119
    assert history.current() == 'foobarhoge'
120
121
122
def test_previous_match_without_match(prompt):
123
    prompt.text = 'fool'
124
    prompt.caret.get_backward_text.return_value = 'Hello'
125
    history = History(prompt)
126
    assert history.current() == 'fool'
127
128
    assert history.previous_match() == 'fool'
129
    assert history.current() == 'fool'
130
131
132
def test_next_match(prompt):
133
    prompt.text = 'fool'
134
    prompt.caret.get_backward_text.return_value = 'fo'
135
    history = History(prompt)
136
    [history.previous() for i in range(6)]
137
    assert history.current() == 'foobarhoge'
138
139
    assert history.next_match() == 'foobar'
140
    assert history.current() == 'foobar'
141
142
    assert history.next_match() == 'foo'
143
    assert history.current() == 'foo'
144
145
    assert history.next_match() == 'fool'
146
    assert history.current() == 'fool'
147
148
    assert history.next_match() == 'fool'
149
    assert history.current() == 'fool'
150
151
152
def test_next_match_without_match(prompt):
153
    prompt.text = 'fool'
154
    prompt.caret.get_backward_text.return_value = 'Hello'
155
    history = History(prompt)
156
    assert history.current() == 'fool'
157
158
    assert history.next_match() == 'fool'
159
    assert history.current() == 'fool'
160