Skip to content

Commit

Permalink
Bit of refactoring, test for message type
Browse files Browse the repository at this point in the history
  • Loading branch information
quartata committed Dec 6, 2017
1 parent 45d8122 commit 355fd59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
16 changes: 8 additions & 8 deletions chatcommunicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@ def on_msg(msg, client):
room_data.lock.set()
_pickle_run.set()
elif message.parent and message.parent.owner.id == client._br.user_id:
command = message.content.split(" ", 1)[1]
result = dispatch_reply_command(message.parent, message, command)
content = GlobalVars.parser.unescape(message.content).lower()
cmd = content.split(" ", 1)[1]

result = dispatch_reply_command(message.parent, message, cmd)

if result:
message.reply(result, length_check=False)
elif message.content.startswith("sd "):
result = dispatch_shorthand_command(message, message.room)
result = dispatch_shorthand_command(message)

if result:
message.reply(result, length_check=False)
Expand Down Expand Up @@ -366,9 +368,7 @@ def dispatch_command(msg):
return func(*args, original_msg=msg, alias_used=command_name, quiet_action=quiet_action)


def dispatch_reply_command(msg, reply, command_name):
cmd = GlobalVars.parser.unescape(command_name).lower()

def dispatch_reply_command(msg, reply, cmd):
quiet_action = cmd[-1] == "-"
cmd = regex.sub(r"\W*$", "", cmd)

Expand All @@ -382,7 +382,7 @@ def dispatch_reply_command(msg, reply, command_name):
return func(msg, original_msg=reply, alias_used=cmd, quiet_action=quiet_action)


def dispatch_shorthand_command(msg, room):
def dispatch_shorthand_command(msg):
commands = GlobalVars.parser.unescape(msg.content[3:]).split()

output = []
Expand All @@ -396,7 +396,7 @@ def dispatch_shorthand_command(msg, room):

should_return_output = False

for current_command, message in zip(processed_commands, get_last_messages(room, len(processed_commands))):
for current_command, message in zip(processed_commands, get_last_messages(msg.room, len(processed_commands))):
if current_command == "-":
output.append("[:{}] <skipped>".format(message.id))
else:
Expand Down
15 changes: 15 additions & 0 deletions test/test_chatcommunicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,18 @@ def test_on_msg(pickle_rick, get_last_messages):
chatcommunicate.on_msg(msg6, client)

msg6.message.reply.assert_called_once_with("[:0] hi\n[:1] <skipped>\n[:2] hi\n[:3] hi\n[:4] <processed without return value>\n[:5] <processed without return value>\n[:6] <skipped>\n[:7] <skipped>\n[:8] <processed without return value>", length_check=False)


def test_message_type():
fake1 = Fake({}, spec=chatcommunicate.Message)
assert chatcommands.message(fake1) == fake1

fake2 = Fake({})
threw_exception = False

try:
chatcommands.message(fake2)
except AssertionError:
threw_exception = True

assert threw_exception

0 comments on commit 355fd59

Please sign in to comment.