Esempio n. 1
0
 def test_process_calls_custom_runner(self):
     cmd = Command('foo', aliases=('bar', 'baz'), help='foo command')
     cmd.run = lambda client, chan, nick, msg, cmd, args: 'runner'
     assert 'runner' == cmd.process(self.client, '#bots', 'me', 'helga foo')
Esempio n. 2
0
 def setup(self):
     self.cmd = Command('foo', aliases=('bar', 'baz'), help='foo cmd')
     self.client = Mock(nickname='helga')
Esempio n. 3
0
class TestCommand(object):
    def setup(self):
        self.cmd = Command('foo', aliases=('bar', 'baz'), help='foo cmd')
        self.client = Mock(nickname='helga')

    def test_init_does_not_overwrite_things(self):
        class MyCommand(Command):
            command = 'dothis'
            aliases = ('foo', 'bar', 'baz', 'f')
            help = 'my command'

        cmd = MyCommand()
        assert cmd.command == 'dothis'
        assert cmd.aliases == ('foo', 'bar', 'baz', 'f')
        assert cmd.help == 'my command'

    def test_parse_handles_main_command(self):
        assert 'foo' == self.cmd.parse('helga', 'helga foo')[0]

    @patch('helga.plugins.settings')
    def test_parse_handles_char_prefix(self, settings):
        settings.COMMAND_PREFIX_CHAR = '#'
        assert 'foo' == self.cmd.parse('helga', '#foo')[0]

    @patch('helga.plugins.settings')
    def test_parse_handles_botnick_string(self, settings):
        settings.COMMAND_PREFIX_BOTNICK = '@helga'
        settings.COMMAND_PREFIX_CHAR = ''
        assert 'foo' == self.cmd.parse('helga', '@helga foo')[0]

    def test_parse_handles_aliases(self):
        assert 'bar' == self.cmd.parse('helga', 'helga bar')[0]
        assert 'baz' == self.cmd.parse('helga', 'helga baz')[0]

    def test_parse_with_punctuation(self):
        assert 'foo' == self.cmd.parse('helga', 'helga: foo')[0]
        assert 'foo' == self.cmd.parse('helga', 'helga, foo')[0]
        assert 'foo' == self.cmd.parse('helga', 'helga ----> foo')[0]

    def test_parse_does_not_handle(self):
        assert '' == self.cmd.parse('helga', 'helga qux')[0]

    def test_parse_returns_args(self):
        assert ['1', '2', '3'] == self.cmd.parse('helga', 'helga foo 1 2 3')[1]

    def test_parse_handles_longest_command_first(self):
        with patch.object(self.cmd, 'aliases', ['b', 'bar']):
            for check in ('b', 'bar'):
                cmd, args = self.cmd.parse('helga', 'helga %s baz' % check)
                assert cmd == check
                assert args == ['baz']

    def test_parse_does_not_handle_something_else(self):
        assert ('', []) == self.cmd.parse('helga', 'helga fun')

    def test_parse_handles_unicode(self):
        snowman = u'☃'
        disapproval = u'ಠ_ಠ'
        cmd = u'helga {0} {1}'.format(snowman, disapproval)
        self.cmd.command = snowman
        assert (snowman, [disapproval]) == self.cmd.parse('helga', cmd)

    @pytest.mark.parametrize('argstr,expected', [
        ('foo bar "baz qux"', ['foo', 'bar', 'baz qux']),
        (u'foo "bar ☃"', ['foo', u'bar ☃']),
    ])
    def test_parse_argstr_shlex(self, argstr, expected):
        with patch('helga.plugins.settings') as settings:
            settings.COMMAND_ARGS_SHLEX = True
            assert self.cmd._parse_argstr(argstr) == expected

    @pytest.mark.parametrize('argstr,expected', [
        ('foo bar "baz qux"', ['foo', 'bar', '"baz', 'qux"']),
        (u'foo "bar ☃"', ['foo', '"bar', u'☃"']),
    ])
    def test_parse_argstr_whitespace(self, argstr, expected):
        with patch('helga.plugins.settings') as settings:
            settings.COMMAND_ARGS_SHLEX = False
            assert self.cmd._parse_argstr(argstr) == expected

    def test_process_for_different_command_returns_none(self):
        assert self.cmd.process(self.client, '#bots', 'me',
                                'helga qux') is None

    def test_process_calls_class_run_method(self):
        self.cmd.run = lambda client, chan, nick, msg, cmd, args: 'run'
        assert 'run' == self.cmd.process(self.client, '#bots', 'me',
                                         'helga foo')

    def test_process_calls_custom_runner(self):
        cmd = Command('foo', aliases=('bar', 'baz'), help='foo command')
        cmd.run = lambda client, chan, nick, msg, cmd, args: 'runner'
        assert 'runner' == cmd.process(self.client, '#bots', 'me', 'helga foo')

    def test_process_is_case_insensitive(self):
        with patch.object(self.cmd, 'run'):
            with patch.object(settings, 'COMMAND_IGNORECASE', True):
                settings.COMMAND_IGNORECASE = True
                self.cmd.run.return_value = 'run'
                assert 'run' == self.cmd.process(self.client, '#bots', 'me',
                                                 'HELGA FOO')

    def test_multiple_decorators(self):
        @command('foo')
        @command('bar')
        def foobar(*args):
            return args[-2]

        assert len(foobar._plugins) == 2
        assert 'bar' == foobar._plugins[0](self.client, '#bots', 'me',
                                           'helga bar')
        assert 'foo' == foobar._plugins[1](self.client, '#bots', 'me',
                                           'helga foo')

    def test_decorator_using_command(self):
        @command('foo')
        def foo(client, chan, nick, msg, cmd, args):
            return 'bar'

        assert 'bar' == foo._plugins[0](self.client, '#bots', 'me',
                                        'helga foo')

    def test_decorator_using_alias(self):
        @command('foo', aliases=['baz'])
        def foo(client, chan, nick, msg, cmd, args):
            return 'bar'

        assert 'bar' == foo._plugins[0](self.client, '#bots', 'me',
                                        'helga baz')

    def test_decorator_with_shlex(self):
        @command('foo', shlex=True)
        def foo(client, chan, nick, msg, cmd, args):
            return args

        with patch('helga.plugins.settings') as settings:
            settings.COMMAND_ARGS_SHLEX = False
            settings.COMMAND_PREFIX_BOTNICK = True
            settings.COMMAND_PREFIX_CHAR = '!'
            message = 'helga foo bar "baz qux"'
            assert foo._plugins[0](self.client, '#bots', 'me',
                                   message) == ['bar', 'baz qux']
Esempio n. 4
0
 def test_process_calls_custom_runner(self):
     cmd = Command('foo', aliases=('bar', 'baz'), help='foo command')
     cmd.run = lambda client, chan, nick, msg, cmd, args: 'runner'
     assert 'runner' == cmd.process(self.client, '#bots', 'me', 'helga foo')
Esempio n. 5
0
 def setup(self):
     self.cmd = Command('foo', aliases=('bar', 'baz'), help='foo cmd')
     self.client = Mock(nickname='helga')
Esempio n. 6
0
class TestCommand(object):

    def setup(self):
        self.cmd = Command('foo', aliases=('bar', 'baz'), help='foo cmd')
        self.client = Mock(nickname='helga')

    def test_init_does_not_overwrite_things(self):
        class MyCommand(Command):
            command = 'dothis'
            aliases = ('foo', 'bar', 'baz', 'f')
            help = 'my command'

        cmd = MyCommand()
        assert cmd.command == 'dothis'
        assert cmd.aliases == ('foo', 'bar', 'baz', 'f')
        assert cmd.help == 'my command'

    def test_parse_handles_main_command(self):
        assert 'foo' == self.cmd.parse('helga', 'helga foo')[0]

    @patch('helga.plugins.settings')
    def test_parse_handles_char_prefix(self, settings):
        settings.COMMAND_PREFIX_CHAR = '#'
        assert 'foo' == self.cmd.parse('helga', '#foo')[0]

    def test_parse_handles_aliases(self):
        assert 'bar' == self.cmd.parse('helga', 'helga bar')[0]
        assert 'baz' == self.cmd.parse('helga', 'helga baz')[0]

    def test_parse_with_punctuation(self):
        assert 'foo' == self.cmd.parse('helga', 'helga: foo')[0]
        assert 'foo' == self.cmd.parse('helga', 'helga, foo')[0]
        assert 'foo' == self.cmd.parse('helga', 'helga ----> foo')[0]

    def test_parse_does_not_handle(self):
        assert '' == self.cmd.parse('helga', 'helga qux')[0]

    def test_parse_returns_args(self):
        assert ['1', '2', '3'] == self.cmd.parse('helga', 'helga foo 1 2 3')[1]

    def test_parse_handles_longest_command_first(self):
        with patch.object(self.cmd, 'aliases', ['b', 'bar']):
            for check in ('b', 'bar'):
                cmd, args = self.cmd.parse('helga', 'helga %s baz' % check)
                assert cmd == check
                assert args == ['baz']

    def test_parse_does_not_handle_something_else(self):
        assert ('', []) == self.cmd.parse('helga', 'helga fun')

    def test_parse_handles_unicode(self):
        snowman = u'☃'
        disapproval = u'ಠ_ಠ'
        cmd = u'helga {0} {1}'.format(snowman, disapproval)
        self.cmd.command = snowman
        assert (snowman, [disapproval]) == self.cmd.parse('helga', cmd)

    @pytest.mark.parametrize('argstr,expected', [
        ('foo bar "baz qux"', ['foo', 'bar', 'baz qux']),
        (u'foo "bar ☃"', ['foo', u'bar ☃']),
    ])
    def test_parse_argstr_shlex(self, argstr, expected):
        with patch('helga.plugins.settings') as settings:
            settings.COMMAND_ARGS_SHLEX = True
            assert self.cmd._parse_argstr(argstr) == expected

    @pytest.mark.parametrize('argstr,expected', [
        ('foo bar "baz qux"', ['foo', 'bar', '"baz', 'qux"']),
        (u'foo "bar ☃"', ['foo', '"bar', u'☃"']),
    ])
    def test_parse_argstr_whitespace(self, argstr, expected):
        with patch('helga.plugins.settings') as settings:
            settings.COMMAND_ARGS_SHLEX = False
            assert self.cmd._parse_argstr(argstr) == expected

    def test_process_for_different_command_returns_none(self):
        assert self.cmd.process(self.client, '#bots', 'me', 'helga qux') is None

    def test_process_calls_class_run_method(self):
        self.cmd.run = lambda client, chan, nick, msg, cmd, args: 'run'
        assert 'run' == self.cmd.process(self.client, '#bots', 'me', 'helga foo')

    def test_process_calls_custom_runner(self):
        cmd = Command('foo', aliases=('bar', 'baz'), help='foo command')
        cmd.run = lambda client, chan, nick, msg, cmd, args: 'runner'
        assert 'runner' == cmd.process(self.client, '#bots', 'me', 'helga foo')

    def test_process_is_case_insensitive(self):
        with patch.object(self.cmd, 'run'):
            with patch.object(settings, 'COMMAND_IGNORECASE', True):
                settings.COMMAND_IGNORECASE = True
                self.cmd.run.return_value = 'run'
                assert 'run' == self.cmd.process(self.client, '#bots', 'me', 'HELGA FOO')

    def test_multiple_decorators(self):
        @command('foo')
        @command('bar')
        def foobar(*args):
            return args[-2]

        assert len(foobar._plugins) == 2
        assert 'bar' == foobar._plugins[0](self.client, '#bots', 'me', 'helga bar')
        assert 'foo' == foobar._plugins[1](self.client, '#bots', 'me', 'helga foo')

    def test_decorator_using_command(self):
        @command('foo')
        def foo(client, chan, nick, msg, cmd, args):
            return 'bar'

        assert 'bar' == foo._plugins[0](self.client, '#bots', 'me', 'helga foo')

    def test_decorator_using_alias(self):
        @command('foo', aliases=['baz'])
        def foo(client, chan, nick, msg, cmd, args):
            return 'bar'

        assert 'bar' == foo._plugins[0](self.client, '#bots', 'me', 'helga baz')

    def test_decorator_with_shlex(self):
        @command('foo', shlex=True)
        def foo(client, chan, nick, msg, cmd, args):
            return args

        with patch('helga.plugins.settings') as settings:
            settings.COMMAND_ARGS_SHLEX = False
            settings.COMMAND_PREFIX_BOTNICK = True
            settings.COMMAND_PREFIX_CHAR = '!'
            message = 'helga foo bar "baz qux"'
            assert foo._plugins[0](self.client, '#bots', 'me', message) == ['bar', 'baz qux']