Beispiel #1
0
def test_run_Command():
    def fn(name: str):
        '''
        :n name: give the program a name
        '''
        assert name == 'joe'
    cmd = Command(fn)
    cmd.run(['--name', 'joe'])
    cmd.run(['-n', 'joe'])
    cmd.run(['--name=joe'])
    cmd.run(['-n=joe'])

    @command
    def fn2(name: str):
        '''
        :n name: give the program a name
        '''
        assert name == 'joe'
        assert len(fn2.flags) == 1
    fn2(['--name', 'joe'])
    fn2(['-n', 'joe'])
    fn2(['--name=joe'])
    fn2(['-n=joe'])

    @command()
    def fn3(multi_word_flag, bool_flag: bool):
        assert multi_word_flag
        assert not bool_flag
    fn3(['--multi-word-flag'])  # pylint: disable=no-value-for-parameter
Beispiel #2
0
def testDocParsing():
    def fn(verbose=False, pi=3.14159, tag: str = ''):
        '''fn is a function
        that has a multi-line description.
        :v verbose:
        : pi  : this is the value of pi




        :      t              tag :this is a tag

     :return: None
        '''
    parsed = _parse_flags_doc(fn.__doc__)
    assert 'verbose' in parsed
    assert parsed['verbose']['shorthand'] == 'v'
    assert 'pi' in parsed
    assert parsed['pi']['shorthand'] is None
    assert parsed['tag']['doc'] == "this is a tag"

    cmd = Command(fn)
    assert "fn is a function" in cmd._help
    assert "that has a multi-line description." in cmd._help
    assert len(cmd.flags) == 3
    f = cmd.flags['verbose']
    assert f.name == 'verbose'
    assert f.shorthand == 'v'
    assert f.type is bool
    assert f.value is False
    assert cmd.flags['pi'].type is float
    assert cmd.flags['pi'].value == 3.14159
    assert cmd.flags['pi'].help == 'this is the value of pi'
    assert cmd.flags['tag'].type is str
    assert cmd.flags['tag'].help == 'this is a tag'
    assert cmd.flags['tag'].shorthand == 't'
    assert cmd.flags['tag'].value == ''
    assert cmd.flags['t'].name == 'tag'
    assert cmd.flags['t'].type is str
    assert cmd.flags['t'].help == 'this is a tag'
    assert cmd.flags['t'].shorthand == 't'
    assert cmd.flags['t'].value == ''
    assert cmd.flags['t'] == cmd.flags['tag']
    assert id(cmd.flags['t']) == id(cmd.flags['tag'])

    assert cmd.flags['verbose'].has_default
    assert cmd.flags['verbose'].value == False
    assert cmd.flags['pi'].has_default
    assert cmd.flags['pi'].value == 3.14159
    assert cmd.flags['tag'].has_default
    assert cmd.flags['tag'].value == ''

    hlp = cmd.helptext()
    assert 'return' not in hlp
    assert '--return' not in hlp
    for name, flag in cmd.flags.items():
        assert name in hlp
        assert flag.help in hlp
Beispiel #3
0
def test_multi_name_flag_docs():
    def f(flag_name, another_flag):
        '''this is a cli

        :f flag-name: a flag's name
        :a another_flag: another flag
        '''
    c = Command(f)
    assert '-f, --flag-name' in c.helptext()
    assert '-a, --another-flag' in c.helptext()
Beispiel #4
0
def test_incomplete_doc_parsing():
    def fn(verbose: bool, hello):
        ''':l hello: say hello'''
        pass
    cmd = Command(fn)
    assert cmd.flags['l'] is not None
    assert cmd.flags['l'].name == 'hello'
    assert '-l, --hello' in cmd.helptext()
    assert 'say hello' in cmd.helptext()

    @command
    def fn2(verbose: bool, hello):
        ''':l hello: say hello'''
    assert '-l, --hello' in fn2.helptext()
    assert 'say hello' in fn2.helptext()
Beispiel #5
0
def test_Command():
    def fn(arg1, arg2): pass
    cmd = Command(fn)
    assert cmd._meta.name == 'fn'
    assert cmd.flags['arg1'].name == 'arg1'
    assert cmd.flags['arg2'].name == 'arg2'
    assert cmd.flags['arg1'].type is bool
    assert cmd.flags['arg1'].type is bool
    assert cmd.name == 'fn'
    cmd.name = 'not_fn'
    assert cmd.name == 'not_fn'

    def fn(): pass
    cmd = Command(fn)
    assert len(cmd.flags) == 0
    assert cmd._meta.name == 'fn'
Beispiel #6
0
def testOptionTypes():
    def fn(a: str, b: int, c: bool, d: FlagType, pi=3.14159): pass
    cmd = Command(fn)
    flags = cmd.flags
    assert len(cmd.flags) == 5
    assert flags['a'].type is str
    assert flags['b'].type is int
    assert flags['c'].type is bool
    assert flags['d'].type is FlagType
    assert flags['pi'].type is float
Beispiel #7
0
def test_bad_doc():
    def f1(verbose: bool): pass
    cmd = Command(f1)
    htext = cmd.helptext()
    assert 'Usage:\n    f1 [options]' in htext
    assert '--verbose' in htext
    assert '-h, --help' in  htext
    assert 'Get help.' in htext
    expected = 'Usage:\n    f1 [options]\n\nOptions:\n        --verbose   \n    -h, --help      Get help.' # noqa
    assert htext and len(htext) > 5
    got = cmd.helptext()
    assert expected.replace(' ', '') == got.replace(' ', '')

    def f2(): pass
    cmd = Command(f2)
    htext = cmd.helptext()
    assert htext and len(htext) > 5
    assert EMPTY_HELP.format(name='f2') == htext
Beispiel #8
0
def test_none_command():
    with raises(DeveloperException):
        c = Command(None)