Exemplo n.º 1
0
    def testCmdProcess_Unknown(self):
        mock = mock_engine()
        lap = LinotArgParser('testcmd', self.parser, mock.cmdProcess, None)
        lap.add_argument('-a', action='store_true')
        self.cmd_server = LineCmdServer(mock, self.parser)

        def cmdChecker(args, sender, done):
            ok_(sender)
            ok_(args.a)
            cmdChecker.testpass = True

        def msgChecker(recv, msg, done):
            ok_(recv == 'sender')
            ok_('Unknown' in msg)
            msgChecker.passtime += 1
            if msgChecker.passtime == 2:
                done()

        cmdChecker.testpass = False
        msgChecker.passtime = 0
        fake_cmd = [
            ['sender', None, fakeMessage('some_unknown_cmds')],
            [True, None, fakeMessage('testcmd -a')],
            ['sender', None, fakeMessage('some_unknown_cmds')],
        ]
        mock.addTest(fake_cmd, cmdChecker, msgChecker)
        self.cmd_server.start()
        mock.test_finished.wait(10)
        ok_(cmdChecker.testpass)
        self.cmd_server.stop()
        self.cmd_server.join(10)
        ok_(self.cmd_server.stopped())
        ok_(not self.cmd_server.isAlive())
Exemplo n.º 2
0
    def testAddArgument_PositionalArgs(self):

        def cmdProcess(args, sender):
            ok_(False)

        lap = LinotArgParser('testcmd', self.parser, cmdProcess, None)
        lap.add_argument('abc', action='store_true')
Exemplo n.º 3
0
 def testAddArgument_Exclusiveness(self):
     def cmdProcess(args, sender):
         assert_equal(args.a is True and args.b is True, False)
     lap = LinotArgParser('testcmd', self.parser, cmdProcess, None)
     lap.add_argument('-a', action='store_true')
     lap.add_argument('-b', action='store_true')
     with assert_raises(SystemExit) as e:
         args, unknown_args = self.parser.parse_known_args('testcmd -a -b'.split())
         check_str = 'not allowed with'
         ok_(check_str in e.msg)
Exemplo n.º 4
0
    def testAddArgument_MultiArgs(self):
        def cmdProcess(args, sender):
            ok_(False)

        def custFunc(value, sender):
            ok_(value)
        lap = LinotArgParser('testcmd', self.parser, cmdProcess, None)
        lap.add_argument('-a', '-b', '-c', action='store_true', func=custFunc)
        args, unkown_args = self.parser.parse_known_args('testcmd -a'.split())
        args, unkown_args = self.parser.parse_known_args('testcmd -b'.split())
        args, unkown_args = self.parser.parse_known_args('testcmd -c'.split())
Exemplo n.º 5
0
    def test_direct_command(self):
        def cmd_checker1(match_list, cmd, sender):
            cmd_checker1.runned = True
            cmd_checker1.cmd = cmd
            ok_('1234' in match_list)

        def cmd_checker2(match_list, cmd, sender):
            cmd_checker2.runned = True
            cmd_checker2.cmd = cmd
            ok_('1234' in match_list)

        ap = LinotArgParser('testcmd', self.parser, None, None)
        ap.add_direct_command(cmd_checker1, '[cxyz]+([0-9]+)', re.IGNORECASE)
        ap = LinotArgParser('testcmd2', self.parser, None, None)
        ap.add_direct_command(cmd_checker2, '[abc]+([0-9]+)', re.IGNORECASE)
        cmd_checker1.runned = False
        cmd_checker2.runned = False
        self.parser.process_direct_commands('1234', None)
        ok_(cmd_checker1.runned is False)
        ok_(cmd_checker2.runned is False)
        self.parser.process_direct_commands('xyz1234', None)
        ok_(cmd_checker1.runned is True)
        ok_(cmd_checker2.runned is False)
        cmd_checker1.runned = False
        self.parser.process_direct_commands('ab1234', None)
        ok_(cmd_checker1.runned is False)
        ok_(cmd_checker2.runned is True)
        cmd_checker2.runned = False
        self.parser.process_direct_commands('c1234', None)
        ok_(cmd_checker1.runned is True)
        ok_(cmd_checker2.runned is True)
        ok_(cmd_checker1.cmd == 'c1234')
        ok_(cmd_checker2.cmd == 'c1234')
Exemplo n.º 6
0
    def testPrintHelp(self):
        def cmdProcess(args, sender):
            ok_(False)

        test_str = 'testtesttest'
        line = MockLine()
        lap = LinotArgParser('testcmd', self.parser, cmdProcess, line)
        lap.add_argument('-a', action='store_true', help=test_str)
        lap.add_argument('-noshow', action='store_true', help=argparse.SUPPRESS)
        lap.add_argument('-showme', action='store_true', help='')
        args, unknown_args = self.parser.parse_known_args('testcmd -h'.split())
        args.proc(args, 'test_sender')
        cap_text = line.msg
        ok_(test_str in cap_text, True)
        ok_('-nowshow' not in cap_text, True)
        ok_('-showme' in cap_text, True)
        ok_('test_sender' == line.recv)

        # Test help suppress if sender not found (for coverage)
        args, unknown_args = self.parser.parse_known_args('testcmd -h'.split())
        line.msg = None
        line.recv = None
        args.proc(args, None)
        ok_(line.msg is None)
        ok_(line.recv is None)
Exemplo n.º 7
0
 def setUp(self):
     self.line = MockLine()
     self.twitch_engine = MockTwitchEngine()
     self.plugin = Plugin(self.line)
     self.plugin._twitch = self.twitch_engine
     self.parser = LinotParser(usage=argparse.SUPPRESS, add_help=False)
     self.cmd_group = LinotArgParser('testcmd', self.parser, None, self.line)
Exemplo n.º 8
0
    def testSubcmdDefault(self):
        def cmdProcess(args, sender):
            if args is None:
                ok_(True)
                return
            ok_(False)

        LinotArgParser('testcmd', self.parser, cmdProcess, None)
        args, unknown_args = self.parser.parse_known_args('testcmd'.split())
        args.proc(args, None)
Exemplo n.º 9
0
    def test_direct_command(self):
        mock = mock_engine()
        lap = LinotArgParser('testcmd', self.parser, mock.cmdProcess, None)

        def cmd_checker(match_list, cmd, sender):
            ok_('somechannel' in match_list)
            ok_(len(match_list) == 1)
            ok_(cmd == 'www.twitch.tv/somechannel')
            cmd_checker.runned = True
        cmd_checker.runned = False
        lap.add_direct_command(cmd_checker, 'twitch\.tv/(\w+)[\s\t,]*', re.IGNORECASE)
        self.cmd_server = LineCmdServer(mock, self.parser)
        fake_cmd = [
            ['sender', None, fakeMessage('www.twitch.tv/somechannel')],
        ]
        mock.addTest(fake_cmd, None, None)
        self.cmd_server.start()
        mock.test_finished.wait(10)
        ok_(cmd_checker.runned)
        self.cmd_server.stop()
Exemplo n.º 10
0
    def testCmdProcess_Normal(self):
        mock = mock_engine()
        lap = LinotArgParser('testcmd', self.parser, mock.cmdProcess, None)
        lap.add_argument('-a', action='store_true')
        lap.add_argument('-b', action='store_true')
        self.cmd_server = LineCmdServer(mock, self.parser)

        # Test 1 cmd in op_list
        fake_cmd = [
            [True, None, fakeMessage('testcmd -a')]
        ]

        def cmdChecker_1(args, sender, done):
            ok_(args.a)
            ok_(sender)
            done()
        mock.addTest(fake_cmd, cmdChecker_1, None)

        # Test 2 cmds in op_list
        fake_cmd = [
            [True, None, fakeMessage('testcmd -a')],
            [True, None, fakeMessage('testcmd -b')],
        ]

        def cmdChecker_2(args, sender, done):
            ok_(args.a or args.b)
            if args.a:
                cmdChecker_2.var += 'a'
            if args.b:
                cmdChecker_2.var += 'b'
            if cmdChecker_2.var == 'ab':
                done()
            elif len(cmdChecker_2.var) >= 2:
                ok_(False)
        cmdChecker_2.var = ''
        mock.addTest(fake_cmd, cmdChecker_2, None)
        self.cmd_server.start()
        mock.test_finished.wait(10)
        self.cmd_server.stop()
        self.cmd_server.join(10)
        ok_(not self.cmd_server.isAlive())
Exemplo n.º 11
0
 def testInit(self):
     def cmdProcess(args, sender):
         pass
     line = MockLine()
     LinotArgParser('testcmd', self.parser, cmdProcess, line)
     # test -h and --help goes to print_help
     args, unknown_args = self.parser.parse_known_args('testcmd -h'.split())
     eq_(len(unknown_args), 0)
     args.proc(args, 'test_sender')
     ok_('command list' in line.msg)
     ok_('test_sender' == line.recv)
     args, unknown_args = self.parser.parse_known_args('testcmd --help'.split())
     eq_(len(unknown_args), 0)
     line.msg = ''
     line.recv = ''
     args.proc(args, 'test_sender')
     ok_('command list' in line.msg)
     ok_('test_sender' == line.recv)
Exemplo n.º 12
0
    def testAddArgument_Func(self):
        def cmdProcess(args, sender):
            if args.b:
                ok_(True)
            else:
                ok_(False)

        def custFunc(value, sender):
            ok_(value and sender)

        lap = LinotArgParser('testcmd', self.parser, cmdProcess, None)
        lap.add_argument('-a', action='store_true', func=custFunc)
        lap.add_argument('-b', action='store_true')  # default proc
        args, unknown_args = self.parser.parse_known_args('testcmd -a'.split())
        args.proc(args, True)
        args, unknown_args = self.parser.parse_known_args('testcmd -b'.split())
        args.proc(args, False)
Exemplo n.º 13
0
 def testAddArgument_DestException(self):
     def cmdProcess(args, sender):
         pass
     lap = LinotArgParser('testcmd', self.parser, cmdProcess, None)
     with assert_raises(ValueError):
         lap.add_argument('-a', dest='b', action='store_true')