This repository has been archived by the owner on Oct 25, 2019. It is now read-only.
forked from sbp/phenny
-
Notifications
You must be signed in to change notification settings - Fork 19
/
test_irc.py
85 lines (65 loc) · 2.46 KB
/
test_irc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Tests for phenny's irc.py
"""
import unittest
from mock import call, patch, Mock
import irc
class OriginTest(unittest.TestCase):
def setUp(self):
self.bot = Mock()
def test_server(self):
source = "foobar.example.com"
origin = irc.Origin(self.bot, source, [])
self.assertEqual(origin.host, '')
def test_privmsg(self):
source = "Foobar!foo@bar.example.com"
args = ['PRIVMSG', '#phenny']
origin = irc.Origin(self.bot, source, args)
self.assertEqual(origin.nick, 'Foobar')
self.assertEqual(origin.user, 'foo')
self.assertEqual(origin.host, 'bar.example.com')
self.assertEqual(origin.sender, '#phenny')
class BotTest(unittest.TestCase):
@patch('threading.RLock')
@patch('asynchat.async_chat')
def setUp(self, mock_async, mock_thread):
self.nick = 'foo'
self.name = 'Phenny'
self.bot = irc.Bot(self.nick, self.name, '#phenny')
@patch('irc.Bot.write')
def test_login(self, mock_write):
self.bot.verbose = False
self.bot.handle_connect()
mock_write.assert_has_calls([
call(('NICK', self.nick)),
call(('USER', self.nick, '+iw', self.nick), self.name)
])
@patch('irc.Bot.write')
def test_ping(self, mock_write):
self.bot.buffer = b"PING"
self.bot.found_terminator()
mock_write.assert_called_once_with(('PONG', ''))
@patch('irc.Bot.push')
def test_msg(self, mock_push):
self.bot.msg('#phenny', 'hi')
mock_push.assert_called_once_with(b'PRIVMSG #phenny :hi\r\n')
@patch('time.sleep') # patch sleep so test runs faster
@patch('irc.Bot.push')
def test_msgflood(self, mock_push, mock_sleep):
self.bot.msg('#phenny', 'flood')
self.bot.msg('#phenny', 'flood')
self.bot.msg('#phenny', 'flood')
self.bot.msg('#phenny', 'flood')
self.bot.msg('#phenny', 'flood')
self.bot.msg('#phenny', 'flood')
mock_push.assert_called_with(b'PRIVMSG #phenny :...\r\n')
self.assertEqual(mock_sleep.call_count, 5)
@patch('irc.Bot.msg')
def test_action(self, mock_msg):
self.bot.action('foo', 'is')
mock_msg.assert_called_once_with('foo', '\x01ACTION is\x01')
@patch('irc.Bot.write')
def test_notice(self, mock_write):
notice = "This is a notice!"
self.bot.notice('jqh', notice)
mock_write.assert_called_once_with(('NOTICE', 'jqh'), notice)