Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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```"
# -*- 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))
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))
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))
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']
super().__init__(prefix=f'```{language}', suffix='```', **kwargs)
if line_span:
line_span = sorted(line_span)
if min(line_span) < 1 or max(line_span) > len(lines):
raise ValueError("Linespan goes out of bounds.")
lines = lines[line_span[0] - 1:line_span[1]]
for line in lines:
self.add_line(line)
class WrappedFilePaginator(FilePaginator, WrappedPaginator):
"""
Combination of FilePaginator and WrappedPaginator.