Skip to content

Commit 195b57a

Browse files
committed
irc: test sopel.irc.utils.safe function
1 parent 2744f92 commit 195b57a

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
@@ -35,7 +35,30 @@ def get_cnames(domain):
3535

3636

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