/
test_paginator.py
324 lines (229 loc) · 8.75 KB
/
test_paginator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# -*- coding: utf-8 -*-
"""
jishaku converter test
~~~~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2020 Devon (Gorialis) R
:license: MIT, see LICENSE for more details.
"""
import asyncio
import inspect
from io import BytesIO
import discord
import pytest
from discord.ext import commands
import utils
from jishaku.paginators import FilePaginator, PaginatorEmbedInterface, PaginatorInterface, WrappedPaginator
def test_file_paginator():
base_text = inspect.cleandoc("""
#!/usr/bin/env python
# -*- coding: utf-8 -*-
pass # \u3088\u308d\u3057\u304f
""")
# test standard encoding
pages = FilePaginator(BytesIO(base_text.encode("utf-8"))).pages
assert len(pages) == 1
assert pages[0] == f"```python\n{base_text}\n```"
# test linespan
pages = FilePaginator(BytesIO(base_text.encode("utf-8")), line_span=(2, 2)).pages
assert len(pages) == 1
assert pages[0] == f"```python\n# -*- coding: utf-8 -*-\n```"
# test reception to encoding hint
base_text = inspect.cleandoc("""
#!/usr/bin/env python
# -*- coding: cp932 -*-
pass # \u3088\u308d\u3057\u304f
""")
pages = FilePaginator(BytesIO(base_text.encode("cp932"))).pages
assert len(pages) == 1
assert pages[0] == f"```python\n{base_text}\n```"
# test without encoding hint
with pytest.raises(UnicodeDecodeError):
FilePaginator(BytesIO("\u3088\u308d\u3057\u304f".encode("cp932")))
# test with wrong encoding hint
with pytest.raises(UnicodeDecodeError):
FilePaginator(BytesIO("-*- coding: utf-8 -*-\n\u3088\u308d\u3057\u304f".encode("cp932")))
# test OOB
with pytest.raises(ValueError):
FilePaginator(BytesIO("one\ntwo\nthree\nfour".encode('utf-8')), line_span=(-1, 20))
def test_wrapped_paginator():
paginator = WrappedPaginator(max_size=200)
paginator.add_line("abcde " * 50)
assert len(paginator.pages) == 2
paginator = WrappedPaginator(max_size=200, include_wrapped=False)
paginator.add_line("abcde " * 50)
assert len(paginator.pages) == 2
@utils.run_async
async def test_paginator_interface():
bot = commands.Bot('?')
with open(__file__, 'rb') as file:
paginator = FilePaginator(file, max_size=200)
interface = PaginatorInterface(bot, paginator)
assert interface.pages == paginator.pages
assert interface.page_count == len(paginator.pages)
assert interface.page_size > 200
assert interface.page_size < interface.max_page_size
send_kwargs = interface.send_kwargs
assert isinstance(send_kwargs, dict)
assert 'content' in send_kwargs
content = send_kwargs['content']
assert isinstance(content, str)
assert len(content) <= interface.page_size
assert interface.display_page == 0
# pages have been closed, so adding a line should make a new page
old_page_count = interface.page_count
await interface.add_line('a' * 150)
assert interface.page_count > old_page_count
# push the page to the end (rounded into bounds)
interface.display_page = 999
old_display_page = interface.display_page
assert interface.pages == paginator.pages
# page closed, so create new page
await interface.add_line('b' * 150)
# ensure page has followed tail
assert interface.display_page > old_display_page
# testing with embed interface
embed_interface = PaginatorEmbedInterface(bot, paginator)
assert embed_interface.pages[0] == interface.pages[0]
send_kwargs = embed_interface.send_kwargs
assert isinstance(send_kwargs, dict)
assert 'embed' in send_kwargs
embed = send_kwargs['embed']
assert isinstance(embed, discord.Embed)
description = embed.description
assert content.startswith(description)
# check for raise on too large page size
with pytest.raises(ValueError):
PaginatorInterface(None, commands.Paginator(max_size=2000))
# check for raise on not-paginator
with pytest.raises(TypeError):
PaginatorInterface(None, 4)
paginator = commands.Paginator(max_size=100)
for _ in range(100):
paginator.add_line("test text")
# test interfacing
with utils.mock_ctx(bot) as ctx:
interface = PaginatorInterface(bot, paginator)
assert not interface.closed
await interface.send_to(ctx)
await asyncio.sleep(0.1)
await interface.add_line("test text")
assert interface.page_count > 1
assert not interface.closed
interface.message.id = utils.sentinel()
current_page = interface.display_page
payload = {
'message_id': interface.message.id,
'user_id': ctx.author.id,
'channel_id': ctx.channel.id,
'guild_id': ctx.guild.id
}
# push right button
emoji = discord.PartialEmoji(
animated=False,
name="\N{BLACK RIGHT-POINTING TRIANGLE}",
id=None
)
bot.dispatch(
'raw_reaction_add',
discord.RawReactionActionEvent(payload, emoji, 'REACTION_ADD')
if discord.version_info >= (1, 3) else
discord.RawReactionActionEvent(payload, emoji)
)
await asyncio.sleep(0.1)
assert interface.display_page > current_page
assert not interface.closed
current_page = interface.display_page
# push left button
emoji = discord.PartialEmoji(
animated=False,
name="\N{BLACK LEFT-POINTING TRIANGLE}",
id=None
)
bot.dispatch(
'raw_reaction_add',
discord.RawReactionActionEvent(payload, emoji, 'REACTION_ADD')
if discord.version_info >= (1, 3) else
discord.RawReactionActionEvent(payload, emoji)
)
await asyncio.sleep(0.1)
assert interface.display_page < current_page
assert not interface.closed
# push last page button
emoji = discord.PartialEmoji(
animated=False,
name="\N{BLACK RIGHT-POINTING DOUBLE TRIANGLE WITH VERTICAL BAR}",
id=None
)
bot.dispatch(
'raw_reaction_add',
discord.RawReactionActionEvent(payload, emoji, 'REACTION_ADD')
if discord.version_info >= (1, 3) else
discord.RawReactionActionEvent(payload, emoji)
)
await asyncio.sleep(0.1)
assert interface.display_page == interface.page_count - 1
assert not interface.closed
# push first page button
emoji = discord.PartialEmoji(
animated=False,
name="\N{BLACK LEFT-POINTING DOUBLE TRIANGLE WITH VERTICAL BAR}",
id=None
)
bot.dispatch(
'raw_reaction_add',
discord.RawReactionActionEvent(payload, emoji, 'REACTION_ADD')
if discord.version_info >= (1, 3) else
discord.RawReactionActionEvent(payload, emoji)
)
await asyncio.sleep(0.1)
assert interface.display_page == 0
assert not interface.closed
# push close button
emoji = discord.PartialEmoji(
animated=False,
name="\N{BLACK SQUARE FOR STOP}",
id=None
)
bot.dispatch(
'raw_reaction_add',
discord.RawReactionActionEvent(payload, emoji, 'REACTION_ADD')
if discord.version_info >= (1, 3) else
discord.RawReactionActionEvent(payload, emoji)
)
await asyncio.sleep(0.1)
assert interface.closed
ctx.send.coro.return_value.delete.assert_called_once()
# test resend, no delete
with utils.mock_ctx(bot) as ctx:
interface = PaginatorInterface(bot, paginator)
assert not interface.closed
await interface.send_to(ctx)
await asyncio.sleep(0.1)
await interface.add_line("test text")
assert interface.page_count > 1
assert not interface.closed
# resend
await interface.send_to(ctx)
await asyncio.sleep(0.1)
await interface.add_line("test text")
ctx.send.coro.return_value.delete.assert_not_called()
interface.task.cancel()
await asyncio.sleep(0.1)
assert interface.closed
# test resend, delete
with utils.mock_ctx(bot) as ctx:
interface = PaginatorInterface(bot, paginator, delete_message=True)
assert not interface.closed
await interface.send_to(ctx)
await asyncio.sleep(0.1)
await interface.add_line("test text")
assert interface.page_count > 1
assert not interface.closed
# resend
await interface.send_to(ctx)
await asyncio.sleep(0.1)
await interface.add_line("test text")
ctx.send.coro.return_value.delete.assert_called_once()
interface.task.cancel()
await asyncio.sleep(0.1)
assert interface.closed