Skip to content

Commit

Permalink
Messages shouldn't be modified by responding (#30)
Browse files Browse the repository at this point in the history
* Test if messages are affected by responding

* Messages are copied when responding
  • Loading branch information
jacobtomlinson committed Aug 12, 2016
1 parent 5916a40 commit 24cfea8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 5 additions & 2 deletions opsdroid/message.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Class to encapsulate a message."""

from copy import copy


class Message:
# pylint: disable=too-few-public-methods
Expand All @@ -15,5 +17,6 @@ def __init__(self, text, user, room, connector):

def respond(self, text):
"""Respond to this message using the connector it was created by."""
self.text = text
self.connector.respond(self)
response = copy(self)
response.text = text
self.connector.respond(response)
9 changes: 8 additions & 1 deletion tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ def test_message(self):
message.respond("Goodbye world")

self.assertEqual(len(mock_connector.mock_calls), 1)
self.assertEqual(message.text, "Goodbye world")

def test_response_effects(self):
"""Responding to a message shouldn't change the message."""
mock_connector = mock.MagicMock()
message_text = "Hello world"
message = Message(message_text, "user", "default", mock_connector)
message.respond("Goodbye world")
self.assertEqual(message_text, message.text)

0 comments on commit 24cfea8

Please sign in to comment.