Ejemplo n.º 1
0
def test_component_subparser():
    """
    test to parse strings with a formula parser and retrieve the following results :
    - "" : {}
    - "--sub toto -b" :  {a:True, sub: {'toto' : {b: True}}}
    - "-b" : BadContextException(b, [toto])

    Parser description :

    - formula subparser toto binded to the argument sub with sub arguments : -b and --name
    """
    parser = MainParser(help_arg=False)

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

    check_parsing_result(parser, '', {})

    check_parsing_result(parser, '--sub toto -b',
                         {'sub': {
                             'toto': {
                                 'b': True
                             }
                         }})

    with pytest.raises(BadContextException):
        check_parsing_result(parser, '-b', None)
Ejemplo n.º 2
0
def test_add_component_subparser_that_aldready_exists():
    """
    Add a component_subparser with no argument 'name'
    test if a SubParserWithoutNameArgumentException is raised
    """
    parser = MainParser(help_arg=False)
    subparser = ComponentSubParser('titi')

    with pytest.raises(SubParserWithoutNameArgumentException):
        parser.add_component_subparser('toto', subparser)
Ejemplo n.º 3
0
def test_add_component_subparser_with_two_name():
    """
    add a component subparser with one short name and one long name
    parse a string and test if the value is only bind to the long name
    """
    parser = MainParser(help_arg=False)
    subparser = ComponentSubParser('titi')
    subparser.add_argument('a', 'aaa', flag=True, action=store_true, default=False)
    subparser.add_argument('n', 'name')
    parser.add_component_subparser('sub', subparser)
    check_parsing_result(parser, '--sub titi -a --name tutu', {'sub': {'titi': {'tutu': {'aaa': True, 'name': 'tutu'}}}})
Ejemplo n.º 4
0
def test_add_component_subparser_that_aldready_exists():
    """
    Add a component_subparser that already exists to a parser and test if an
    AlreadyAddedArgumentException is raised
    """
    parser = MainParser(help_arg=False)
    subparser = ComponentSubParser('titi')
    subparser.add_argument('n', 'name')
    parser.add_component_subparser('toto', subparser)
    subparser2 = ComponentSubParser('titi')
    subparser2.add_argument('n', 'name')
    
    with pytest.raises(AlreadyAddedArgumentException):
        parser.add_component_subparser('toto', subparser2)
Ejemplo n.º 5
0
def test_create_two_component():
    """
    Create two component of the same type with the following cli :
    --sub toto --name titi --sub toto -b --name tutu

    test if the result is :
    {sub:{'toto' : {'titi': {'name': 'titi'}, 'tutu': {'name': 'tutu', 'b':False}}}}

    """
    parser = MainParser(help_arg=False)

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

    check_parsing_result(parser, '--sub toto --name titi --sub toto -b --name tutu', {'sub': {'toto': {'titi': {'name': 'titi'}, 'tutu': {'name': 'tutu', 'b': True}}}})
Ejemplo n.º 6
0
def test_create_component_that_already_exist():
    """
    Create two component with the same name with the following cli
    --sub toto --name titi --sub toto --name titi

    test if an ComponentAlreadyExistException is raised


    """
    parser = MainParser(help_arg=False)

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

    with pytest.raises(ComponentAlreadyExistException):
        check_parsing_result(parser, '--sub toto --name titi --sub toto --name titi', None)
Ejemplo n.º 7
0
def test_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: {'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_component_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': {'toto': {'titi': {'name': 'titi', 'b': True}}}})

    with pytest.raises(BadContextException):
        check_parsing_result(parser, '-b', None)