示例#1
0
def test_match_getitem():
    m = re.search(r'(\w+) (\d+)', 'A Hello 2 u!')
    match = Match(m, None, None)

    assert match[0] == 'Hello 2'
    assert match[1] == 'Hello'
    assert match[2] == '2'
    assert match[3] == ''
示例#2
0
def test_command_messaging_return_tuple(c):
    line = Line(':[email protected] PRIVMSG #Chan :Some cool message', c)
    instance = command(r'')
    match = Match(None, line, c)

    for msg, expected in MESSAGES:
        instance.send_messages(msg, match, [])
        assert c.last == 'PRIVMSG #Chan :{}'.format(expected)
示例#3
0
def test_user_get_function(c):
    c.me = User('Test')
    match = Match(None, None, c)

    assert c.users == [c.me]
    assert User.get('Test', c) is c.me
    assert User.get('Test!A@B', c) is c.me
    assert User.get('Test', match) is c.me
    assert User.get('Test2', c) is None
示例#4
0
def test_command_messaging_return_list(c):
    line = Line(':[email protected] PRIVMSG #Chan :Some cool message', c)
    instance = command(r'')
    match = Match(None, line, c)

    instance.send_messages([msg for msg, expected in MESSAGES], match, [])

    for index, (msg, expected) in enumerate(MESSAGES):
        assert c.outbox[index] == 'PRIVMSG #Chan :{}'.format(expected)
示例#5
0
    def match(self, line, timers, connection, settings):
        m = self.matches(line, settings)

        if m:
            match = Match(m, line, connection, settings)
            result = self.function(match)

            if result is not None:
                self.send_messages(result, match, timers)
示例#6
0
def test_match_msg_with_privmsg(c):
    line = Line(':[email protected] PRIVMSG #Chan :Some cool message')
    match = Match(None, line, c)
    match.msg('A {} reply', 'cool')

    assert c.outbox[0] == 'PRIVMSG #Chan :A cool reply'

    line = Line(':[email protected] PRIVMSG TestBot :Some cool message')
    match = Match(None, line, c)
    match.msg('A {} reply', 'cool')

    assert c.outbox[1] == 'PRIVMSG John :A cool reply'

    match.msg('A private message', target='SomeUser')

    assert c.outbox[2] == 'PRIVMSG SomeUser :A private message'

    match.msg('A {}formatted message', 'mis', raw=True)

    assert c.outbox[3] == 'PRIVMSG John :A {}formatted message'
示例#7
0
def test_channel_get_function(c):
    chan = Channel('#test')
    assert c.channels == []

    c.me.channels.append(chan)
    assert c.channels == [chan]

    assert Channel.get('#test', c) is chan
    assert Channel.get('#test2', c) is None

    match = Match(None, None, c)
    assert Channel.get('#test', match) is chan
示例#8
0
def test_command_messaging_yielding(c):
    def mock_command():
        for msg, expected in MESSAGES:
            yield msg

    line = Line(':[email protected] PRIVMSG #Chan :Some cool message', c)
    instance = command(r'')
    match = Match(None, line, c)

    instance.send_messages(mock_command(), match, [])

    for index, (msg, expected) in enumerate(MESSAGES):
        assert c.outbox[index] == 'PRIVMSG #Chan :{}'.format(expected)
示例#9
0
def test_command_appends_timers():
    instance = command(r'')
    match = Match(None, None, None)
    timers = []

    instance.send_messages((datetime.timedelta(seconds=3), 'User', 'Hi'),
                           match, timers)
    assert len(timers) == 1
    assert isinstance(timers[0], Timer)
    assert timers[0].scheduled == datetime.timedelta(seconds=3)
    assert timers[0].msg_tuple == (
        'User',
        'Hi',
        (),
        {},
    )
示例#10
0
def test_messaging_from_timer(c):
    instance = Timer(None)
    match = Match(None, None, c)
    timers = []

    instance.send_messages((datetime.timedelta(seconds=3), 'User', 'Hi'),
                           match, timers)
    assert len(timers) == 1
    assert isinstance(timers[0], Timer)
    assert timers[0].scheduled == datetime.timedelta(seconds=3)
    assert timers[0].msg_tuple == (
        'User',
        'Hi',
        (),
        {},
    )

    instance.send_messages(('User', 'Hello {}', 'world'), match, timers)
    assert c.last == 'PRIVMSG User :Hello world'
示例#11
0
def test_match_msg_with_privmsg(c):
    line = Line(':[email protected] PRIVMSG #Chan :Some cool message', c)
    match = Match(None, line, c)
    match.msg('A {} reply', 'cool')

    assert c.outbox[0] == 'PRIVMSG #Chan :A cool reply'

    line = Line(':[email protected] PRIVMSG TestBot :Some cool message', c)
    match = Match(None, line, c)
    match.msg('A {} reply', 'cool')

    assert c.outbox[1] == 'PRIVMSG John :A cool reply'

    match.msg('A private message', target='SomeUser')

    assert c.outbox[2] == 'PRIVMSG SomeUser :A private message'

    match.msg('A {}formatted message', 'mis', raw=True)

    assert c.outbox[3] == 'PRIVMSG John :A {}formatted message'