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.

13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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[1:]:
  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 assertSaid(self, msg):
  39. self.assertSent("PRIVMSG #channel :{0}".format(msg))
  40. def assertSaidIn(self, msgs):
  41. msgs = ["PRIVMSG #channel :{0}".format(msg) for msg in msgs]
  42. self.assertSentIn(msgs)
  43. def assertReply(self, msg):
  44. self.assertSaid("\x02Foo\x0F: {0}".format(msg))
  45. def assertReplyIn(self, msgs):
  46. msgs = ["\x02Foo\x0F: {0}".format(msg) for msg in msgs]
  47. self.assertSaidIn(msgs)
  48. def maker(self, line, chan, msg=None):
  49. data = Data(line)
  50. data.nick, data.ident, data.host = self.re_sender.findall(line[0])[0]
  51. if msg is not None:
  52. data.msg = msg
  53. data.chan = chan
  54. data.parse_args()
  55. return data
  56. def make_msg(self, command, *args):
  57. line = ":Foo!bar@example.com PRIVMSG #channel :!{0}".format(command)
  58. line = line.strip().split()
  59. line.extend(args)
  60. return self.maker(line, line[2], " ".join(line[3:])[1:])
  61. def make_join(self):
  62. line = ":Foo!bar@example.com JOIN :#channel".strip().split()
  63. return self.maker(line, line[2][1:])
  64. class FakeConnection(Connection):
  65. def connect(self):
  66. self._buffer = ""
  67. def close(self):
  68. pass
  69. def get(self, size=4096):
  70. data, self._buffer = self._buffer, ""
  71. return data
  72. def send(self, msg):
  73. self._buffer += msg + "\n"