Completed
Push — master ( 9183d8...5b047d )
by dgw
21s queued 14s
created

test_module.test_action_commands()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 0
1
# coding=utf-8
2
"""Tests for message formatting"""
3
from __future__ import unicode_literals, absolute_import, print_function, division
4
5
import pytest
6
7
from sopel.trigger import PreTrigger, Trigger
8
from sopel.test_tools import MockSopel, MockSopelWrapper
9
from sopel.tools import Identifier
10
from sopel import module
11
12
13
@pytest.fixture
14
def sopel():
15
    bot = MockSopel('Sopel')
16
    bot.config.core.owner = 'Bar'
17
    return bot
18
19
20
@pytest.fixture
21
def bot(sopel, pretrigger):
22
    bot = MockSopelWrapper(sopel, pretrigger)
23
    bot.channels[Identifier('#Sopel')].privileges[Identifier('Foo')] = module.VOICE
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Identifier does not seem to be defined.
Loading history...
24
    return bot
25
26
27
@pytest.fixture
28
def pretrigger():
29
    line = ':[email protected] PRIVMSG #Sopel :Hello, world'
30
    return PreTrigger(Identifier('Foo'), line)
31
32
33
@pytest.fixture
34
def pretrigger_pm():
35
    line = ':[email protected] PRIVMSG Sopel :Hello, world'
36
    return PreTrigger(Identifier('Foo'), line)
37
38
39
@pytest.fixture
40
def trigger_owner(bot):
41
    line = ':[email protected] PRIVMSG #Sopel :Hello, world'
42
    return Trigger(bot.config, PreTrigger(Identifier('Bar'), line), None)
43
44
45
@pytest.fixture
46
def trigger(bot, pretrigger):
47
    return Trigger(bot.config, pretrigger, None)
48
49
50
@pytest.fixture
51
def trigger_pm(bot, pretrigger_pm):
52
    return Trigger(bot.config, pretrigger_pm, None)
53
54
55
def test_unblockable():
56
    @module.unblockable
57
    def mock(bot, trigger, match):
58
        return True
59
    assert mock.unblockable is True
60
61
62
def test_interval():
63
    @module.interval(5)
64
    def mock(bot, trigger, match):
65
        return True
66
    assert mock.interval == [5]
67
68
69
def test_interval_args():
70
    @module.interval(5, 10)
71
    def mock(bot, trigger, match):
72
        return True
73
    assert mock.interval == [5, 10]
74
75
76
def test_interval_multiple():
77
    @module.interval(5, 10)
78
    @module.interval(5)
79
    @module.interval(20)
80
    def mock(bot, trigger, match):
81
        return True
82
    assert mock.interval == [20, 5, 10]
83
84
85
def test_rule():
86
    @module.rule('.*')
87
    def mock(bot, trigger, match):
88
        return True
89
    assert mock.rule == ['.*']
90
91
92
def test_rule_args():
93
    @module.rule('.*', r'\d+')
94
    def mock(bot, trigger, match):
95
        return True
96
    assert mock.rule == ['.*', r'\d+']
97
98
99
def test_rule_multiple():
100
    @module.rule('.*', r'\d+')
101
    @module.rule('.*')
102
    @module.rule(r'\w+')
103
    def mock(bot, trigger, match):
104
        return True
105
    assert mock.rule == [r'\w+', '.*', r'\d+']
106
107
108
def test_thread():
109
    @module.thread(True)
110
    def mock(bot, trigger, match):
111
        return True
112
    assert mock.thread is True
113
114
115
def test_url():
116
    @module.url('pattern')
117
    def mock(bot, trigger, match):
118
        return True
119
    patterns = [regex.pattern for regex in mock.url_regex]
120
    assert len(patterns) == 1
121
    assert 'pattern' in patterns
122
123
124
def test_url_args():
125
    @module.url('first', 'second')
126
    def mock(bot, trigger, match):
127
        return True
128
129
    patterns = [regex.pattern for regex in mock.url_regex]
130
    assert len(patterns) == 2
131
    assert 'first' in patterns
132
    assert 'second' in patterns
133
134
135
def test_url_multiple():
136
    @module.url('first', 'second')
137
    @module.url('second')
138
    @module.url('third')
139
    def mock(bot, trigger, match):
140
        return True
141
142
    patterns = [regex.pattern for regex in mock.url_regex]
143
    assert len(patterns) == 3
144
    assert 'first' in patterns
145
    assert 'second' in patterns
146
    assert 'third' in patterns
147
148
149
def test_echo():
150
    # test decorator with parentheses
151
    @module.echo()
152
    def mock(bot, trigger, match):
153
        return True
154
    assert mock.echo is True
155
156
    # test decorator without parentheses
157
    @module.echo
158
    def mock(bot, trigger, match):
159
        return True
160
    assert mock.echo is True
161
162
    # test without decorator
163
    def mock(bot, trigger, match):
164
        return True
165
    # on undecorated callables, the attr only exists after the loader loads them
166
    # so this cannot `assert mock.echo is False` here
167
    assert not hasattr(mock, 'echo')
168
169
170
def test_commands():
171
    @module.commands('sopel')
172
    def mock(bot, trigger, match):
173
        return True
174
    assert mock.commands == ['sopel']
175
176
177
def test_commands_args():
178
    @module.commands('sopel', 'bot')
179
    def mock(bot, trigger, match):
180
        return True
181
    assert mock.commands == ['sopel', 'bot']
182
183
184
def test_commands_multiple():
185
    @module.commands('sopel', 'bot')
186
    @module.commands('bot')
187
    @module.commands('robot')
188
    def mock(bot, trigger, match):
189
        return True
190
    assert mock.commands == ['robot', 'bot', 'sopel']
191
192
193
def test_nickname_commands():
194
    @module.nickname_commands('sopel')
195
    def mock(bot, trigger, match):
196
        return True
197
    assert mock.nickname_commands == ['sopel']
198
199
200
def test_nickname_commands_args():
201
    @module.nickname_commands('sopel', 'bot')
202
    def mock(bot, trigger, match):
203
        return True
204
    assert mock.nickname_commands == ['sopel', 'bot']
205
206
207
def test_nickname_commands_multiple():
208
    @module.nickname_commands('sopel', 'bot')
209
    @module.nickname_commands('bot')
210
    @module.nickname_commands('robot')
211
    def mock(bot, trigger, match):
212
        return True
213
    assert mock.nickname_commands == ['robot', 'bot', 'sopel']
214
215
216
def test_action_commands():
217
    @module.action_commands('sopel')
218
    def mock(bot, trigger, match):
219
        return True
220
    assert mock.action_commands == ['sopel']
221
    assert mock.intents == ['ACTION']
222
223
224
def test_action_commands_args():
225
    @module.action_commands('sopel', 'bot')
226
    def mock(bot, trigger, match):
227
        return True
228
    assert mock.action_commands == ['sopel', 'bot']
229
    assert mock.intents == ['ACTION']
230
231
232
def test_action_commands_multiple():
233
    @module.action_commands('sopel', 'bot')
234
    @module.action_commands('bot')
235
    @module.action_commands('robot')
236
    def mock(bot, trigger, match):
237
        return True
238
    assert mock.action_commands == ['robot', 'bot', 'sopel']
239
    assert mock.intents == ['ACTION']
240
241
242
def test_priority():
243
    @module.priority('high')
244
    def mock(bot, trigger, match):
245
        return True
246
    assert mock.priority == 'high'
247
248
249
def test_event():
250
    @module.event('301')
251
    def mock(bot, trigger, match):
252
        return True
253
    assert mock.event == ['301']
254
255
256
def test_event_args():
257
    @module.event('301', '302')
258
    def mock(bot, trigger, match):
259
        return True
260
    assert mock.event == ['301', '302']
261
262
263
def test_event_multiple():
264
    @module.event('301', '302')
265
    @module.event('301')
266
    @module.event('466')
267
    def mock(bot, trigger, match):
268
        return True
269
    assert mock.event == ['466', '301', '302']
270
271
272
def test_intent():
273
    @module.intent('ACTION')
274
    def mock(bot, trigger, match):
275
        return True
276
    assert mock.intents == ['ACTION']
277
278
279
def test_intent_args():
280
    @module.intent('ACTION', 'OTHER')
281
    def mock(bot, trigger, match):
282
        return True
283
    assert mock.intents == ['ACTION', 'OTHER']
284
285
286
def test_intent_multiple():
287
    @module.intent('ACTION', 'OTHER')
288
    @module.intent('OTHER')
289
    @module.intent('PING',)
290
    def mock(bot, trigger, match):
291
        return True
292
    assert mock.intents == ['PING', 'OTHER', 'ACTION']
293
294
295
def test_rate():
296
    @module.rate(5)
297
    def mock(bot, trigger, match):
298
        return True
299
    assert mock.rate == 5
300
301
302
def test_require_privmsg(bot, trigger, trigger_pm):
303
    @module.require_privmsg('Try again in a PM')
304
    def mock(bot, trigger, match=None):
305
        return True
306
    assert mock(bot, trigger) is not True
307
    assert mock(bot, trigger_pm) is True
308
309
    @module.require_privmsg
310
    def mock_(bot, trigger, match=None):
311
        return True
312
    assert mock_(bot, trigger) is not True
313
    assert mock_(bot, trigger_pm) is True
314
315
316
def test_require_chanmsg(bot, trigger, trigger_pm):
317
    @module.require_chanmsg('Try again in a channel')
318
    def mock(bot, trigger, match=None):
319
        return True
320
    assert mock(bot, trigger) is True
321
    assert mock(bot, trigger_pm) is not True
322
323
    @module.require_chanmsg
324
    def mock_(bot, trigger, match=None):
325
        return True
326
    assert mock(bot, trigger) is True
327
    assert mock(bot, trigger_pm) is not True
328
329
330
def test_require_privilege(bot, trigger):
331
    @module.require_privilege(module.VOICE)
332
    def mock_v(bot, trigger, match=None):
333
        return True
334
    assert mock_v(bot, trigger) is True
335
336
    @module.require_privilege(module.OP, 'You must be at least opped!')
337
    def mock_o(bot, trigger, match=None):
338
        return True
339
    assert mock_o(bot, trigger) is not True
340
341
342
def test_require_admin(bot, trigger, trigger_owner):
343
    @module.require_admin('You must be an admin')
344
    def mock(bot, trigger, match=None):
345
        return True
346
    assert mock(bot, trigger) is not True
347
348
    @module.require_admin
349
    def mock_(bot, trigger, match=None):
350
        return True
351
    assert mock_(bot, trigger_owner) is True
352
353
354
def test_require_owner(bot, trigger, trigger_owner):
355
    @module.require_owner('You must be an owner')
356
    def mock(bot, trigger, match=None):
357
        return True
358
    assert mock(bot, trigger) is not True
359
360
    @module.require_owner
361
    def mock_(bot, trigger, match=None):
362
        return True
363
    assert mock_(bot, trigger_owner) is True
364
365
366
def test_example(bot, trigger):
367
    @module.commands('mock')
368
    @module.example('.mock', 'True')
369
    def mock(bot, trigger, match=None):
370
        return True
371
    assert mock(bot, trigger) is True
372