Example #1
0
def test_command_matches_patterns(c):
    line = Line(':[email protected] PRIVMSG #Chan :Some cool message', c)
    settings = MockObject(command_prefix='!')

    instance = command(r'^Some', prefix=False)
    assert bool(instance.matches(line, settings)) is True

    instance = command(r'message$', prefix=False)
    assert bool(instance.matches(line, settings)) is True

    instance = command(r'^Some cool message$', prefix=False)
    assert bool(instance.matches(line, settings)) is True

    instance = command(r'mESsagE', prefix=False, flags=re.IGNORECASE)
    assert bool(instance.matches(line, settings)) is True

    instance = command(r'Some')
    assert bool(instance.matches(line, settings)) is False

    settings = MockObject(command_prefix='S')

    instance = command(r'^ome')
    assert bool(instance.matches(line, settings)) is True

    instance = command(r'cool')
    assert bool(instance.matches(line, settings)) is True

    line = Line(':irc.example.net 376 A :End of MOTD command', c)

    instance = command(r'example', prefix=False)
    assert bool(instance.matches(line, settings)) is False

    instance = command(r'example', raw=True, prefix=False)
    assert bool(instance.matches(line, settings)) is True
def test_join_command(c):
    settings = MockObject(command_prefix='!')

    def match(line):
        assert track.join.command.matches(line, settings) is True
        track.join.command.match(line, [], c, settings)

    c.me = User('Pyro')

    line = Line(':Pyro!Hello@world JOIN :#Chan', c)
    assert line.sender is c.me

    match(line)
    assert len(c.me.channels) == 1
    assert c.me.channels[0] is line.channel
    assert c.last == 'WHOIS Pyro'

    channel = c.me.channels[0]
    line = Line(':User1!Hello@world JOIN #Chan', c)
    assert line.command == 'JOIN'
    assert line.channel == channel

    match(line)

    assert c.last == 'WHOIS User1'
    assert channel.users == [c.me, line.sender]
    assert line.sender.channels == [channel]
Example #3
0
def test_command_matches_command(c):
    # A command is a 4 to 5 character all-capital string received from the
    # server. Examples: JOIN, QUIT, NICK, etc.
    settings = MockObject(command_prefix='!')
    instance = command(command='PART')

    line = Line(':[email protected] PART #Chan :"Bye !"', c)
    assert line.command == 'PART'
    assert bool(instance.matches(line, settings)) is True

    line = Line(':[email protected] NICK Paul"', c)
    assert line.command == 'NICK'
    assert bool(instance.matches(line, settings)) is False
Example #4
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)
Example #5
0
def test_command_matches_code():
    with pytest.raises(CommandException):
        command()

    with pytest.raises(CommandException):
        command(code='Foo')

    settings = MockObject(command_prefix='!')
    instance = command(code=376)

    line = Line(':irc.example.net 376 A :End of MOTD command')
    assert line.code == 376
    assert bool(instance.matches(line, settings)) is True

    line = Line(':irc.example.net 375 A :- irc.example.net message of the day')
    assert line.code == 375
    assert bool(instance.matches(line, settings)) is False
Example #6
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)
Example #7
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'
Example #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)
Example #9
0
def test_line_parsing_with_privmsg(c):
    line_str = ':[email protected] PRIVMSG #Chan :Some cool message'
    line = Line(line_str, c)

    assert line.privmsg is True
    assert line.notice is False
    assert line.usermsg is True
    assert line.sender.nick == 'John'
    assert line.target == '#Chan'
    assert line.pm is False
    assert line.full_msg == 'Some cool message'
    assert line[0] == 'Some'
    assert line[1:] == ['cool', 'message']
    assert line[3] == ''