コード例 #1
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_parse_empty_string_default_value():
    parser = MainParser(help_arg=False)
    parser.add_argument('a', default=1)
    result = parser.parse(''.split())
    assert len(result) == 1
    assert 'a' in result
    assert result['a'] == 1
コード例 #2
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_main_parser():
    """
    test to parse strings with a parser and retrieve the following results :

    - "" : {}
    - "-z" : UnknowArgException(z)
    - "-a" : {a: True}
    - "-a --sub toto -b" : UnknowArgException(sub)
    - "-b" : UnknowArgException(b)

    Parser description :

    - base parser arguments : -a
    - subparser toto binded to the argument sub with sub arguments : None
    """
    parser = MainParser(help_arg=False)
    parser.add_argument('a', flag=True, action=store_true)

    check_parsing_result(parser, '', {})

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-z', None)

    check_parsing_result(parser, '-a', {'a': True})

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-a --sub toto -b', None)

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-b', None)
コード例 #3
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_add_two_short_name():
    """
    Add an argument to a parser with two long name and test if the
    parser raise an exception TooManyArgumentNamesException

    """
    parser = MainParser(help_arg=False)
    with pytest.raises(TooManyArgumentNamesException):
        parser.add_argument('coco', 'dodo')
コード例 #4
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_add_argument_long():
    """
    Add a long argument to the parser

    Test if the argument was added to the long_arg list
    """
    parser = MainParser(help_arg=False)
    assert parser.long_arg == []
    parser.add_argument('aaa')
    assert parser.long_arg == ['aaa=']
コード例 #5
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_add_argument_flag():
    """
    Add a short flag to the parser

    Test if the argument was added to the short_arg string
    """
    parser = MainParser(help_arg=False)
    assert parser.short_arg == ''
    parser.add_argument('a', flag=True)
    assert parser.short_arg == 'a'
コード例 #6
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_add_argument_short():
    """
    Add a short argument to the parser

    Test if the argument was added to the short_arg string
    """
    parser = MainParser(help_arg=False)
    assert parser.short_arg == ''
    parser.add_argument('a')
    assert parser.short_arg == 'a:'
コード例 #7
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_cant_convert_to_type():
    """
    add an argument that must catch an int value, Parse a string that
    contains only this argument with a value that is not an int test if an
    """
    parser = MainParser(help_arg=False)
    parser.add_argument('a', type=int)

    with pytest.raises(BadTypeException):
        parser.parse('-a a'.split())
コード例 #8
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_short_and_long_name_val():
    """
    Add an argument to a parser with two name long and short and test if the
    value is only bind to the long name in the parsing result

    """
    parser = MainParser(help_arg=False)
    parser.add_argument('c', 'coco')

    check_parsing_result(parser, '-c 1', {'coco': '1'})
コード例 #9
0
def test_add_two_short_name():
    """
    Parse an argument with a value that doesn't respect the check function of
    this argument. Test if a BadValueException is raised

    """
    parser = MainParser(help_arg=False)
    parser.add_argument('coco', type=int, check=lambda x: x > 2)

    with pytest.raises(BadValueException):
        parser.parse('--coco 1'.split())
コード例 #10
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_add_argument_2_short():
    """
    Add two short argument (an argument and a flag) to the parser

    Test if the arguments was added to the short_arg string
    """
    parser = MainParser(help_arg=False)
    assert parser.short_arg == ''
    parser.add_argument('a', flag=True)
    assert parser.short_arg == 'a'
    parser.add_argument('b')
    assert parser.short_arg == 'ab:'
コード例 #11
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_other_type():
    """
    add an argument that must catch an int value, Parse a string that
    contains only this argument and test if the value contained in the result is
    an int

    """
    parser = MainParser(help_arg=False)
    parser.add_argument('a', type=int)
    result = parser.parse('-a 1'.split())
    assert len(result) == 1
    assert 'a' in result
    assert isinstance(result['a'], int)
コード例 #12
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_default_type():
    """
    add an argument without specifing the type it must catch. Parse a string
    that contains only this argument and test if the value contained in the
    result is a string

    """
    parser = MainParser(help_arg=False)
    parser.add_argument('a')
    result = parser.parse('-a 1'.split())
    assert len(result) == 1
    assert 'a' in result
    assert isinstance(result['a'], str)
コード例 #13
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_actor_subparser():
    """
    test to parse strings with a parser and retrieve the following results :

    - "" : {}
    - "-z" : UnknowArgException(z)
    - "-a" : {a: True}
    - "-a --sub toto -b" : NoNameSpecifiedForComponentException
    - "-a --sub toto -b --name titi" : {a:True, sub: { titi: { 'type': 'toto', b: True}}}
    - "-b" : BadContextException(b, [toto])

    Parser description :

    - base parser arguments : -a
    - subparser toto binded to the argument sub with sub arguments : -b and --name
    """
    parser = MainParser(help_arg=False)
    parser.add_argument('a', flag=True, action=store_true)

    subparser = ComponentSubParser('toto')
    subparser.add_argument('b', flag=True, action=store_true)
    subparser.add_argument('n', 'name')
    parser.add_actor_subparser('sub', subparser)

    check_parsing_result(parser, '', {})

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-z', None)

    check_parsing_result(parser, '-a', {'a': True})

    with pytest.raises(NoNameSpecifiedForComponentException):
        check_parsing_result(parser, '-a --sub toto -b', {})

    check_parsing_result(parser, '-a --sub toto -b --name titi', {
        'a': True,
        'sub': {
            'titi': {
                'type': 'toto',
                'b': True
            }
        }
    })

    with pytest.raises(BadContextException):
        check_parsing_result(parser, '-b', None)
コード例 #14
0
ファイル: test_parser.py プロジェクト: powerapi-ng/powerapi
def test_argument_with_val():
    """
    test to parse strings with a parser and retrieve the following results :

    - "-c" : MissingValue(c)
    - "-c 1" : {c : 1}

    Parser description :

    - base parser arguments : -c (not flag)
    """
    parser = MainParser(help_arg=False)
    parser.add_argument('c')

    with pytest.raises(MissingValueException):
        check_parsing_result(parser, '-c', None)

    check_parsing_result(parser, '-c 1', {'c': '1'})