Skip to content

Commit 4414bf9

Browse files
authored
Merge pull request #1983 from Exirel/irc-test-utils-safe
irc: test sopel.irc.utils.safe function
2 parents 8933fda + 195b57a commit 4414bf9

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

sopel/irc/utils.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,30 @@ def get_cnames(domain):
3737

3838

3939
def safe(string):
40-
"""Remove newlines from a string."""
40+
"""Remove newlines from a string.
41+
42+
:param str string: input text to process
43+
:return: the string without newlines
44+
:rtype: str
45+
:raises TypeError: when ``string`` is ``None``
46+
47+
This function removes newlines from a string and always returns a unicode
48+
string (as in ``str`` on Python 3 and ``unicode`` on Python 2), but doesn't
49+
strip or alter it in any other way::
50+
51+
>>> safe('some text\\r\\n')
52+
'some text'
53+
54+
This is useful to ensure a string can be used in a IRC message.
55+
56+
.. versionchanged:: 7.1
57+
58+
This function now raises a :exc:`TypeError` instead of an unpredictable
59+
behaviour when given ``None``.
60+
61+
"""
62+
if string is None:
63+
raise TypeError('safe function requires a string, not NoneType')
4164
if sys.version_info.major >= 3 and isinstance(string, bytes):
4265
string = string.decode("utf8")
4366
elif sys.version_info.major < 3:

test/irc/test_irc_utils.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# coding=utf-8
2+
"""Tests for core ``sopel.irc.utils``"""
3+
from __future__ import absolute_import, division, print_function, unicode_literals
4+
5+
import pytest
6+
7+
from sopel.irc import utils
8+
9+
10+
def test_safe():
11+
text = 'some text'
12+
assert utils.safe(text + '\r\n') == text
13+
assert utils.safe(text + '\n') == text
14+
assert utils.safe(text + '\r') == text
15+
assert utils.safe('\r\n' + text) == text
16+
assert utils.safe('\n' + text) == text
17+
assert utils.safe('\r' + text) == text
18+
assert utils.safe('some \r\ntext') == text
19+
assert utils.safe('some \ntext') == text
20+
assert utils.safe('some \rtext') == text
21+
22+
23+
def test_safe_empty():
24+
text = ''
25+
assert utils.safe(text) == text
26+
27+
28+
def test_safe_null():
29+
with pytest.raises(TypeError):
30+
utils.safe(None)
31+
32+
33+
def test_safe_bytes():
34+
text = b'some text'
35+
assert utils.safe(text) == text.decode('utf-8')
36+
assert utils.safe(text + b'\r\n') == text.decode('utf-8')
37+
assert utils.safe(text + b'\n') == text.decode('utf-8')
38+
assert utils.safe(text + b'\r') == text.decode('utf-8')
39+
assert utils.safe(b'\r\n' + text) == text.decode('utf-8')
40+
assert utils.safe(b'\n' + text) == text.decode('utf-8')
41+
assert utils.safe(b'\r' + text) == text.decode('utf-8')
42+
assert utils.safe(b'some \r\ntext') == text.decode('utf-8')
43+
assert utils.safe(b'some \ntext') == text.decode('utf-8')
44+
assert utils.safe(b'some \rtext') == text.decode('utf-8')

0 commit comments

Comments
 (0)