A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

__init__.py 3.6 KiB

13 年前
13 年前
13 年前
13 年前
13 年前
13 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. """
  23. EarwigBot's Unit Tests
  24. This package __init__ file provides some support code for unit tests.
  25. CommandTestCase is a subclass of unittest.TestCase that provides setUp() for
  26. creating a fake connection and some other helpful methods. It uses
  27. FakeConnection, a subclass of classes.Connection, but with an internal string
  28. instead of a socket for data.
  29. """
  30. import re
  31. from unittest import TestCase
  32. from earwigbot.irc import IRCConnection, Data
  33. class CommandTestCase(TestCase):
  34. re_sender = re.compile(":(.*?)!(.*?)@(.*?)\Z")
  35. def setUp(self, command):
  36. self.connection = FakeConnection()
  37. self.connection._connect()
  38. self.command = command(self.connection)
  39. def get_single(self):
  40. data = self.connection._get().split("\n")
  41. line = data.pop(0)
  42. for remaining in data[1:]:
  43. self.connection.send(remaining)
  44. return line
  45. def assertSent(self, msg):
  46. line = self.get_single()
  47. self.assertEqual(line, msg)
  48. def assertSentIn(self, msgs):
  49. line = self.get_single()
  50. self.assertIn(line, msgs)
  51. def assertSaid(self, msg):
  52. self.assertSent("PRIVMSG #channel :{0}".format(msg))
  53. def assertSaidIn(self, msgs):
  54. msgs = ["PRIVMSG #channel :{0}".format(msg) for msg in msgs]
  55. self.assertSentIn(msgs)
  56. def assertReply(self, msg):
  57. self.assertSaid("\x02Foo\x0F: {0}".format(msg))
  58. def assertReplyIn(self, msgs):
  59. msgs = ["\x02Foo\x0F: {0}".format(msg) for msg in msgs]
  60. self.assertSaidIn(msgs)
  61. def maker(self, line, chan, msg=None):
  62. data = Data(line)
  63. data.nick, data.ident, data.host = self.re_sender.findall(line[0])[0]
  64. if msg is not None:
  65. data.msg = msg
  66. data.chan = chan
  67. data.parse_args()
  68. return data
  69. def make_msg(self, command, *args):
  70. line = ":Foo!bar@example.com PRIVMSG #channel :!{0}".format(command)
  71. line = line.strip().split()
  72. line.extend(args)
  73. return self.maker(line, line[2], " ".join(line[3:])[1:])
  74. def make_join(self):
  75. line = ":Foo!bar@example.com JOIN :#channel".strip().split()
  76. return self.maker(line, line[2][1:])
  77. class FakeConnection(IRCConnection):
  78. def __init__(self):
  79. pass
  80. def _connect(self):
  81. self._buffer = ""
  82. def _close(self):
  83. pass
  84. def _get(self, size=4096):
  85. data, self._buffer = self._buffer, ""
  86. return data
  87. def _send(self, msg):
  88. self._buffer += msg + "\n"