argh / test /

Full commit
# -*- coding: utf-8 -*-
Regression tests
from .base import DebugArghParser, run, CmdResult as R

import argh

def test_regression_issue12():
    """Issue #12: @command was broken if there were more than one argument
    to begin with same character (i.e. short option names were inferred

    def cmd(foo=1, fox=2):
        yield 'foo {0}, fox {1}'.format(foo, fox)

    p = DebugArghParser()

    assert run(p, '').out ==  'foo 1, fox 2\n'
    assert run(p, '--foo 3').out == 'foo 3, fox 2\n'
    assert run(p, '--fox 3').out == 'foo 1, fox 3\n'
    assert 'unrecognized' in run(p, '-f 3', exit=True)

def test_regression_issue12_help_flag():
    """Issue #12: if an argument starts with "h", e.g. "--host",
    ArgumentError is raised because "--help" is always added by argh
    without decorators.
    def ddos(host='localhost'):
        return 'so be it, {0}!'.format(host)

    # no help → no conflict
    p = DebugArghParser('PROG', add_help=False)
    assert run(p, '-h').out == 'so be it,!\n'

    # help added → conflict → short name ignored
    p = DebugArghParser('PROG', add_help=True)
    assert None == run(p, '-h', exit=True)

def test_regression_issue27():
    """Issue #27: store_true is not set for inferred bool argument.

    :Reason: when @command was refactored, it stopped using @arg, but it is
    it was there that guesses (choices→type, default→type and
    default→action) were made.
    def parrot(dead=False):
        return 'this parrot is no more' if dead else 'beautiful plumage'

    def grenade(count=3):
        if count == 3:
            return 'Three shall be the number thou shalt count'
            return '{0!r} is right out'.format(count)

    p = DebugArghParser()
    p.add_commands([parrot, grenade])

    # default → type (int)
    assert run(p, 'grenade').out == ('Three shall be the number '
                                     'thou shalt count\n')
    assert run(p, 'grenade --count 5').out == '5 is right out\n'

    # default → action (store_true)
    assert run(p, 'parrot').out == 'beautiful plumage\n'
    assert run(p, 'parrot --dead').out == 'this parrot is no more\n'

def test_regression_issue31():
    """ Issue #31: Argh fails with parameter action type 'count' if a default
    value is provided.

    :Reason: assembling._guess() would infer type from default value without
        regard to the action.  _CountAction does not accept argument "type".

    :Solution: restricted type inferring to actions "store" and "append".

    @argh.arg('-v', '--verbose', dest='verbose', action='count', default=0)
    def cmd(**kwargs):
        yield kwargs.get('verbose', -1)

    p = DebugArghParser()
    assert '0\n' == run(p, '').out
    assert '1\n' == run(p, '-v').out
    assert '2\n' == run(p, '-vv').out