Commits

Dafydd Francis  committed 7308fef

Added an example test to test commands without having to have the bot on the server live

  • Participants
  • Parent commits 8c3e3d0

Comments (0)

Files changed (2)

File test/fake_tbot.py

+"""
+A fake TwistedBot!
+"""
+import re
+
+
+class TestedBot:
+    bot_messages = []
+    functions = []
+
+    def __init__(self):
+        pass
+
+    def msg(self, channel, message):
+        self.bot_messages.append((channel, message))
+
+    def register(self, func):
+        self.functions.append(func)
+
+    def last_message(self):
+        return self.bot_messages[-1]
+
+    def listen(self, usr, channel, message):
+        for func in self.functions:
+            if re.match(func.rule, message):
+                func(self, usr, channel, message)

File test/test_regex.py

+import sys
+sys.path.append("../modules/")
+import regex
+from fake_tbot import TestedBot
+
+#import random
+import unittest
+
+
+class TestRegexModule(unittest.TestCase):
+
+    def setUp(self):
+        self.tbot = TestedBot()
+        self.tbot.register(regex.storemessage)
+        self.tbot.register(regex.substitute)
+        self.tbot.register(regex.directedsubstitute)
+
+    def test_subsitute(self):
+        self.tbot.bot_messages = []
+        self.tbot.listen("Afal", "#42", "test")
+        self.tbot.listen("Afal", "#42", "s/e/oa/")
+        should_be_toast = ("#42", "<Afal> toast")
+        self.assertEqual(self.tbot.last_message(), should_be_toast)
+
+    def test_nothing_has_been_said(self):
+        self.tbot.bot_messages = []
+        self.tbot.listen("Afal", "#42", "s/lol/bums/")
+        rude_error = ("#42", "Uh Afal... you haven't said anything yet")
+        self.assertEqual(self.tbot.last_message(), rude_error)
+
+    def test_person_does_not_exist(self):
+        self.tbot = TestedBot()
+        self.tbot.register(regex.directedsubstitute)
+        self.tbot.listen("Afal", "#42", "Fluttershy: s/yay/woo hoo/")
+        disappointing_result = ("#42", "Afal: Fluttershy doesn't exist! You don't have to correct them!")
+        self.assertEqual(self.tbot.last_message(), disappointing_result)
+
+if __name__ == '__main__':
+    unittest.main()