diff options
author | Eric Lin <anselor@gmail.com> | 2018-04-11 11:44:18 -0400 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-04-11 11:44:18 -0400 |
commit | 52bf16c412eb7933eac159ed0fc6363ccc37a82c (patch) | |
tree | 70a496ff7eca30f6e0234b8083a41c5fc7dd1199 /tests/test_cmd2.py | |
parent | 2ffd342e7523e36f2e0536beca6cf36db5070362 (diff) | |
download | cmd2-git-52bf16c412eb7933eac159ed0fc6363ccc37a82c.tar.gz |
Fixed issue where categorization is skipped when there's a help_<command> function provided.
In verbose help, added check for argparse usage block (starting with 'usage: '), to skip that block and move to the next comment block
Added unit tests for new categorization code
Updated example to demonstrate skipping of argparse usage statement
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r-- | tests/test_cmd2.py | 103 |
1 files changed, 101 insertions, 2 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 545cf1aa..0861c073 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -21,7 +21,8 @@ from optparse import make_option import six.moves as sm import cmd2 -from conftest import run_cmd, normalize, BASE_HELP, HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG, StdOut +from conftest import run_cmd, normalize, BASE_HELP, BASE_HELP_VERBOSE, \ + HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG, StdOut def test_ver(): @@ -38,6 +39,13 @@ def test_base_help(base_app): expected = normalize(BASE_HELP) assert out == expected +def test_base_help_verbose(base_app): + out = run_cmd(base_app, 'help -v') + expected = normalize(BASE_HELP_VERBOSE) + assert out == expected + + out = run_cmd(base_app, 'help --verbose') + assert out == expected def test_base_help_history(base_app): out = run_cmd(base_app, 'help history') @@ -47,7 +55,7 @@ def test_base_argparse_help(base_app, capsys): # Verify that "set -h" gives the same output as "help set" and that it starts in a way that makes sense run_cmd(base_app, 'set -h') out, err = capsys.readouterr() - out1 = out.splitlines() + out1 = normalize(out) out2 = run_cmd(base_app, 'help set') @@ -1066,6 +1074,97 @@ def test_help_overridden_method(help_app): assert out == expected +class HelpCategoriesApp(cmd2.Cmd): + """Class for testing custom help_* methods which override docstring help.""" + def __init__(self, *args, **kwargs): + # Need to use this older form of invoking super class constructor to support Python 2.x and Python 3.x + cmd2.Cmd.__init__(self, *args, **kwargs) + + def do_diddly(self, arg): + """This command does diddly""" + pass + + cmd2.categorize(do_diddly, "Some Category") + + def do_squat(self, arg): + """This docstring help will never be shown because the help_squat method overrides it.""" + pass + + def help_squat(self): + self.stdout.write('This command does diddly squat...\n') + + def do_edit(self, arg): + """This overrides the edit command and does nothing.""" + pass + + cmd2.categorize((do_squat, do_edit), 'Custom Category') + + # This command will be in the "undocumented" section of the help menu + def do_undoc(self, arg): + pass + +@pytest.fixture +def helpcat_app(): + app = HelpCategoriesApp() + app.stdout = StdOut() + return app + +def test_help_cat_base(helpcat_app): + out = run_cmd(helpcat_app, 'help') + expected = normalize("""Documented commands (type help <topic>): + +Custom Category +=============== +edit squat + +Some Category +============= +diddly + +Other +===== +alias help history load py pyscript quit set shell shortcuts unalias + +Undocumented commands: +====================== +undoc +""") + assert out == expected + +def test_help_cat_verbose(helpcat_app): + out = run_cmd(helpcat_app, 'help --verbose') + expected = normalize("""Documented commands (type help <topic>): + +Custom Category +================================================================================ +edit This overrides the edit command and does nothing. +squat This docstring help will never be shown because the help_squat method overrides it. + +Some Category +================================================================================ +diddly This command does diddly + +Other +================================================================================ +alias Define or display aliases +help List available commands with "help" or detailed help with "help cmd". +history View, run, edit, and save previously entered commands. +load Runs commands in script file that is encoded as either ASCII or UTF-8 text. +py Invoke python command, shell, or script +pyscript Runs a python script file inside the console +quit Exits this application. +set Sets a settable parameter or shows current settings of parameters. +shell Execute a command as if at the OS prompt. +shortcuts Lists shortcuts (aliases) available. +unalias Unsets aliases + +Undocumented commands: +====================== +undoc +""") + assert out == expected + + class SelectApp(cmd2.Cmd): def do_eat(self, arg): """Eat something, with a selection of sauces to choose from.""" |