Skip to content

Commit

Permalink
Add tests for is_action and parse_action in util
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmaguire committed Jan 14, 2021
1 parent 7c914b3 commit b95130e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
27 changes: 27 additions & 0 deletions cardinal/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@
from cardinal import util


@pytest.mark.parametrize('message,expected', (
('\x01ACTION tests\x01', True),
('\x01ACTION tests', True),
('\x01ACTION', True),
('ACTION tests', False),
))
def test_is_action(message, expected):
assert util.is_action(message) == expected


@pytest.mark.parametrize('nick,message,expected', (
('Cardinal', '\x01ACTION tests\x01', '* Cardinal tests'),
('Cardinal', '\x01ACTION tests', '* Cardinal tests'),
('Cardinal', '\x01ACTION \x01', '* Cardinal'),
('Cardinal', '\x01ACTION ', '* Cardinal'),
('Cardinal', '\x01ACTION\x01', '* Cardinal'),
('Cardinal', '\x01ACTION', '* Cardinal'),
))
def test_parse_action(nick, message, expected):
assert util.parse_action(nick, message) == expected


def test_parse_action_raises():
with pytest.raises(ValueError):
util.parse_action('Cardinal', 'this is not an action!')


@pytest.mark.parametrize("input_,expected", (
('\x02bold\x02', 'bold'),
('\x0309colored\x03', 'colored'),
Expand Down
7 changes: 4 additions & 3 deletions cardinal/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ def parse_action(nick, message):
raise ValueError("This message is not an ACTION message")

message = message[len("\x01ACTION "):]
if message[-1] == "\x01":
if message and message[-1] == "\x01":
message = message[:-1]

return "* {} {}".format(
return "* {}{}".format(
nick,
message,
" " + message if message else "",
)


Expand Down Expand Up @@ -111,5 +111,6 @@ def light_grey(text):

reset = "\x03"


# alias as this will be used commonly
F = formatting

0 comments on commit b95130e

Please sign in to comment.