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.

108 lines
3.5 KiB

  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 module __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.classes import Connection, 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(Connection):
  78. def connect(self):
  79. self._buffer = ""
  80. def close(self):
  81. pass
  82. def get(self, size=4096):
  83. data, self._buffer = self._buffer, ""
  84. return data
  85. def send(self, msg):
  86. self._buffer += msg + "\n"