def testHelp(self):
        def TestCmd(console_, first, second, option=1, option_no=False):
            '''
            This is a test.

            :param first: This is the first parameter.
            :param second: This is the second and last parameter.
            :param option: This must be a number.
            :param option_no: If set, says nop.
            '''

        app = App('test', color=False, buffered_console=True)
        app.Add(TestCmd)

        self._TestMain(
            app, '',
            Dedent('''

            Usage:
                test <subcommand> [options]

            Commands:
                test-cmd   This is a test.

            '''))

        self._TestMain(
            app, '--help',
            Dedent('''

            Usage:
                test <subcommand> [options]

            Commands:
                test-cmd   This is a test.

            '''))

        self._TestMain(
            app, 'test-cmd --help',
            Dedent('''
                    This is a test.

                    Usage:
                        test-cmd <first> <second> [--option=1],[--option-no]

                    Parameters:
                        first   This is the first parameter.
                        second   This is the second and last parameter.

                    Options:
                        --option   This must be a number. [default: 1]
                        --option-no   If set, says nop.


                '''))

        self._TestMain(
            app, 'test-cmd',
            Dedent('''
                    ERROR: Too few arguments.

                    This is a test.

                    Usage:
                        test-cmd <first> <second> [--option=1],[--option-no]

                    Parameters:
                        first   This is the first parameter.
                        second   This is the second and last parameter.

                    Options:
                        --option   This must be a number. [default: 1]
                        --option-no   If set, says nop.


                '''), app.RETCODE_ERROR)
Exemple #2
0
    def testPageWidthParameter(self):
        oss = TextOutput()
        oss.c_page_width = 80
        oss.RegisterKeyword('yes', 'This will be shown')
        oss.RegisterKeyword('no', 'This will not be shown')
        oss.SetKeyword('yes', True)

        stream = StringIO()
        oss.SetOutputStream(stream)
        try:
            raise RuntimeError('This is an exception')
        except Exception:
            oss.EXCEPTION()
        assert stream.getvalue() == Dedent('''
            ********************************************************************************
            RuntimeError:
                This is an exception
            --------------------------------------------------------------------------------
            %s
                raise RuntimeError('This is an exception')
            ********************************************************************************

            ''') % '\n'.join(textwrap.wrap(__file__ + ':52:', 80))

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.P('alpha bravo charlie delta echo foxtrot')
        oss.P('No show', verbose=4)
        oss.P('No show', keywords=('no', ))
        assert stream.getvalue() == Dedent('''
            alpha bravo charlie delta echo foxtrot

            ''')

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.P('alpha bravo charlie delta echo foxtrot', page_width=12)
        assert stream.getvalue() == Dedent('''
            alpha bravo
            charlie
            delta echo
            foxtrot

            ''')

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.I('alpha')
        oss.I('bravo')
        oss.I('charlie')
        oss.I('No show', verbose=4)
        oss.I('No show', keywords=('no', ))
        assert stream.getvalue() == Dedent('''
            - alpha
            - bravo
            - charlie

            ''')

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.TABLE([7, 8, 9], ['alpha', 'bravo', 'charlie'],
                  [[1, 2, 3], [4, 5, 6]])
        assert 'x\n' + stream.getvalue() == Dedent('''
            x
                  alpha   bravo  charlie
                      1       2        3
                      4       5        6

            ''')

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.HEADER("This is header")
        oss.HEADER('No show', verbose=4)
        oss.HEADER('No show', keywords=('no', ))
        assert stream.getvalue() == Dedent('''
            --------------------------------------------------------------------------------
            This is header
            --------------------------------------------------------------------------------

            ''')

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.ERROR('Oops!')
        oss.ERROR('Oops!', verbose=4)
        oss.ERROR('Oops!', keywords=('no', ))
        assert stream.getvalue() == Dedent('''
            ********************************************************************************
            ERROR
              Oops!
            ********************************************************************************

            ''')

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.PROCESSING('...', 'Doing')
        oss.PROCESSING('Done')
        oss.PROCESSING('...', 'Multi\nline\ndoing')
        oss.PROCESSING('Done')
        oss.flat_output = True
        oss.PROCESSING('...', 'Doing')
        oss.PROCESSING('Done')
        oss.PROCESSING('No show', verbose=4)
        oss.PROCESSING('No show', keywords=('no', ))
        assert stream.getvalue() == Dedent('''
            ...Doing\\rDone
            ...Multi
               line
               doing
            \\rDone
            Doing...Done

            ''')

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.DT('caption', 'value')
        oss.DT('No show', verbose=4)
        oss.DT('No show', keywords=('no', ))
        assert (stream.getvalue() == 'caption value:\n')

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.DD('caption', 'value')
        oss.DD('No show', 'No show', verbose=4)
        oss.DD('No show', 'No show', keywords=('no', ))
        assert (stream.getvalue() == 'caption:\n    value\n')

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.LINE('-')
        oss.LINE('X', verbose=4)
        oss.LINE('X', keywords=('no', ))
        assert (stream.getvalue() == '-' * 80 + '\n')

        stream = StringIO()
        oss.SetOutputStream(stream)
        oss.Indent()
        oss.Indent()
        oss.P('alpha')
        oss.Dedent()
        oss.P('bravo')
        oss.ResetIndentation()
        oss.P('charlie')
        assert (stream.getvalue() == '        alpha\n    bravo\ncharlie\n')
    def testApp(self):
        '''
        Tests App usage and features.
        '''
        def Case1(console_):
            '''
            A "hello" message from case 1
            '''
            console_.Print('Hello from case 1')

        def Case2(console_):
            '''
            A "hello" message from case 2

            Additional help for this function is available.
            '''
            console_.Print('Hello from case 2')

        def Case3(console_):
            console_.Print('Hello from case 3')

        def Case4(console_):
            console_.Print('Hello from case 4 (AKA: four)')

        app = App('test', color=False, buffered_console=True)
        app.Add(Case1, alias='cs')
        app.Add(Case2)
        case3_cmd = app.Add(Case3, alias=('c3', 'cs3'))
        app.Add(Case4, name='four')

        # Test duplicate name
        with pytest.raises(ValueError):
            app.Add(case3_cmd.func, alias='cs')

        # Test commands listing
        assert app.ListAllCommandNames() == [
            'case1', 'cs', 'case2', 'case3', 'c3', 'cs3', 'four'
        ]

        # Tests all commands output
        self._TestMain(app, 'case1', 'Hello from case 1\n')
        self._TestMain(app, 'cs', 'Hello from case 1\n')
        self._TestMain(app, 'case2', 'Hello from case 2\n')
        self._TestMain(app, 'case3', 'Hello from case 3\n')
        self._TestMain(app, 'c3', 'Hello from case 3\n')
        self._TestMain(app, 'cs3', 'Hello from case 3\n')

        # Tests output when an invalid command is requested
        self._TestMain(
            app, 'INVALID',
            Dedent('''
            ERROR: Unknown command 'INVALID'

            Usage:
                test <subcommand> [options]

            Commands:
                case1, cs        A "hello" message from case 1
                case2            A "hello" message from case 2
                case3, c3, cs3   (no description)
                four             (no description)

            '''), app.RETCODE_ERROR)