A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.2 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. EarwigBot's Unit Test Support
  4. This module provides some support code for unit tests.
  5. Importing this module will "fix" your path so that EarwigBot code from bot/ can
  6. be imported normally.
  7. CommandTestCase is a subclass of unittest.TestCase that provides setUp() for
  8. creating a fake connection and some other helpful methods. It uses
  9. FakeConnection, a subclass of classes.Connection, but with an internal string
  10. instead of a socket for data.
  11. """
  12. from os import path
  13. import re
  14. import sys
  15. from unittest import TestCase
  16. root_dir = path.split(path.dirname(path.abspath(__file__)))[0]
  17. code_dir = path.join(root_dir, "bot")
  18. sys.path.insert(0, code_dir)
  19. from classes import Connection, Data
  20. class CommandTestCase(TestCase):
  21. re_sender = re.compile(":(.*?)!(.*?)@(.*?)\Z")
  22. def setUp(self, command):
  23. self.connection = FakeConnection()
  24. self.connection.connect()
  25. self.command = command(self.connection)
  26. def get_single(self):
  27. data = self.connection.get().split("\n")
  28. line = data.pop(0)
  29. for remaining in data:
  30. self.connection.send(remaining)
  31. return line
  32. def assertSent(self, msg):
  33. line = self.get_single()
  34. self.assertEqual(line, msg)
  35. def assertSentIn(self, msgs):
  36. line = self.get_single()
  37. self.assertIn(line, msgs)
  38. def maker(self, line, chan, msg=None):
  39. data = Data(line)
  40. data.nick, data.ident, data.host = self.re_sender.findall(line[0])[0]
  41. if msg is not None:
  42. data.msg = msg
  43. data.chan = chan
  44. data.parse_args()
  45. return data
  46. def make_msg(self, command, *args):
  47. line = ":Foo!bar@example.com PRIVMSG #channel :!{0}".format(command)
  48. line = line.strip().split()
  49. line.extend(args)
  50. return self.maker(line, line[2], " ".join(line[3:])[1:])
  51. def make_join(self):
  52. line = ":Foo!bar@example.com JOIN :#channel".strip().split()
  53. return self.maker(line, line[2][1:])
  54. class FakeConnection(Connection):
  55. def connect(self):
  56. self._buffer = ""
  57. def close(self):
  58. pass
  59. def get(self, size=4096):
  60. data, self._buffer = self._buffer, ""
  61. return data
  62. def send(self, msg):
  63. self._buffer += msg + "\n"