示例#1
0
def test_pattern_flat():
    assert Required(OneOrMore(Argument('N')),
                    Option('-a'), Argument('M')).flat() == \
                            [Argument('N'), Option('-a'), Argument('M')]
    assert Required(Optional(OptionsShortcut()),
                    Optional(Option('-a', None))).flat(OptionsShortcut) == \
                            [OptionsShortcut()]
示例#2
0
def test_one_or_more_match():
    assert OneOrMore(Argument('N')).match([Argument(None, 9)]) == \
            (True, [], [Argument('N', 9)])
    assert OneOrMore(Argument('N')).match([]) == (False, [], [])
    assert OneOrMore(Argument('N')).match([Option('-x')]) == \
            (False, [Option('-x')], [])
    assert OneOrMore(Argument('N')).match([
        Argument(None, 9), Argument(None, 8)
    ]) == (True, [], [Argument('N', 9), Argument('N', 8)])
    assert OneOrMore(Argument('N')).match([
        Argument(None, 9), Option('-x'),
        Argument(None, 8)
    ]) == (True, [Option('-x')], [Argument('N', 9),
                                  Argument('N', 8)])
    assert OneOrMore(Option('-a')).match(
            [Option('-a'), Argument(None, 8), Option('-a')]) == \
                    (True, [Argument(None, 8)], [Option('-a'), Option('-a')])
    assert OneOrMore(Option('-a')).match([Argument(None, 8),
                                          Option('-x')]) == \
                    (False, [Argument(None, 8), Option('-x')], [])
    assert OneOrMore(Required(Option('-a'), Argument('N'))).match(
            [Option('-a'), Argument(None, 1), Option('-x'),
             Option('-a'), Argument(None, 2)]) == \
             (True, [Option('-x')],
              [Option('-a'), Argument('N', 1), Option('-a'), Argument('N', 2)])
    assert OneOrMore(Optional(Argument('N'))).match([Argument(None, 9)]) == \
                    (True, [], [Argument('N', 9)])
示例#3
0
def test_one_or_more_match():
    assert OneOrMore(Argument('N')).match([Argument(None, 9)
                                           ]) == (True, [], [Argument('N', 9)])
    assert OneOrMore(Argument('N')).match([]) == (False, [], [])
    assert OneOrMore(Argument('N')).match([Option('-x')]) == \
            (False, [Option('-x')], [])
    assert OneOrMore(Argument('N')).match([
        Argument(None, 9), Argument(None, 8)
    ]) == (True, [], [Argument('N', 9), Argument('N', 8)])
    assert OneOrMore(Argument('N')).match([
        Argument(None, 9), Option('-x'),
        Argument(None, 8)
    ]) == (True, [Option('-x')], [Argument('N', 9),
                                  Argument('N', 8)])
    assert OneOrMore(Option('-a')).match(
        [Option('-a'), Argument(None, 8),
         Option('-a')]) == (True, [Argument(None, 8)], [])
    assert OneOrMore(Option('-a')).match([Argument(
        None, 8), Option('-x')]) == (False, [Argument(None, 8),
                                             Option('-x')], [])
    #   NOTE, Option is greedy, nothing to match second time
    #   assert OneOrMore(Required(Option('-a'), Argument('N'))).match(
    #           [Option('-a'), Argument(None, 1), Option('-x'),
    #            Option('-a'), Argument(None, 2)]) == \
    #                    (True, [Option('-x')], [Argument('N', 1), Argument('N', 2)])
    assert OneOrMore(Optional(Argument('N'))).match([Argument(None, 9)]) == \
                    (True, [], [Argument('N', 9)])
示例#4
0
def test_required_match():
    assert Required(Option('-a')).match([Option('-a')]) == (True, [], [])
    assert Required(Option('-a')).match([]) == (False, [], [])
    assert Required(Option('-a')).match([Option('-x')]) == (
            False, [Option('-x')], [])
    assert Required(Option('-a'), Option('-b')).match([Option('-a')]) == (
            False, [Option('-a')], [])
    assert Optional(Option('-a'), Option('-b')).match(
            [Option('-b'), Option('-x'), Option('-a')]) == (
                    True, [Option('-x')], [])
示例#5
0
def test_optional_match():
    assert Optional(Option('-a')).match([Option('-a')]) == (True, [], [])
    assert Optional(Option('-a')).match([]) == (True, [], [])
    assert Optional(Option('-a')).match([Option('-x')]) == (
            True, [Option('-x')], [])
    assert Optional(Option('-a'), Option('-b')).match([Option('-a')]) == (
            True, [], [])
    assert Optional(Option('-a'), Option('-b')).match([Option('-b')]) == (
            True, [], [])
    assert Optional(Option('-a'), Option('-b')).match([Option('-x')]) == (
            True, [Option('-x')], [])
    assert Optional(Argument('N')).match([Argument(None, 9)]) == (
            True, [], [Argument('N', 9)])
示例#6
0
def test_pattern_either():
    assert Option('-a').either == Either(Required(Option('-a')))
    assert Argument('A').either == Either(Required(Argument('A')))
    assert Required(Either(Option('-a'), Option('-b')), Option('-c')).either ==\
            Either(Required(Option('-a'), Option('-c')),
                   Required(Option('-b'), Option('-c')))
    assert Optional(Option('-a'), Either(Option('-b'), Option('-c'))).either ==\
            Either(Required(Option('-b'), Option('-a')),
                   Required(Option('-c'), Option('-a')))
    assert Either(Option('-x'), Either(Option('-y'), Option('-z'))).either == \
            Either(Required(Option('-x')),
                   Required(Option('-y')),
                   Required(Option('-z')))
    assert OneOrMore(Argument('N'), Argument('M')).either == \
            Either(Required(Argument('N'), Argument('M'),
                            Argument('N'), Argument('M')))
示例#7
0
def test_basic_pattern_matching():
    # ( -a N [ -x Z ] )
    pattern = Required(Option('-a'), Argument('N'),
                       Optional(Option('-x'), Argument('Z')))
    # -a N
    assert pattern.match([Option('-a'), Argument(None, 9)]) == \
            (True, [], [Option('-a'), Argument('N', 9)])
    # -a -x N Z
    assert pattern.match([Option('-a'), Option('-x'),
                          Argument(None, 9), Argument(None, 5)]) == \
            (True, [], [Option('-a'), Argument('N', 9),
                        Option('-x'), Argument('Z', 5)])
    # -x N Z  # BZZ!
    assert pattern.match([Option('-x'),
                          Argument(None, 9),
                          Argument(None, 5)]) == \
            (False, [Option('-x'), Argument(None, 9), Argument(None, 5)], [])
示例#8
0
def test_pattern_either():
    assert transform(Option('-a')) == Either(Required(Option('-a')))
    assert transform(Argument('A')) == Either(Required(Argument('A')))
    assert transform(Required(Either(Option('-a'), Option('-b')),
                    Option('-c'))) == \
            Either(Required(Option('-a'), Option('-c')),
                   Required(Option('-b'), Option('-c')))
    assert transform(Optional(Option('-a'), Either(Option('-b'),
                                                   Option('-c')))) == \
            Either(Required(Option('-b'), Option('-a')),
                   Required(Option('-c'), Option('-a')))
    assert transform(Either(Option('-x'),
                            Either(Option('-y'), Option('-z')))) == \
            Either(Required(Option('-x')),
                   Required(Option('-y')),
                   Required(Option('-z')))
    assert transform(OneOrMore(Argument('N'), Argument('M'))) == \
            Either(Required(Argument('N'), Argument('M'),
                            Argument('N'), Argument('M')))
示例#9
0
def test_pattern_fix_identities_2():
    pattern = Required(Optional(Argument('X'), Argument('N')), Argument('N'))
    assert pattern.children[0].children[1] == pattern.children[1]
    assert pattern.children[0].children[1] is not pattern.children[1]
    pattern.fix_identities()
    assert pattern.children[0].children[1] is pattern.children[1]
示例#10
0
def test_parse_pattern():
    o = [Option('-h'), Option('-v', '--verbose'), Option('-f', '--file', 1)]
    assert parse_pattern('[ -h ]', options=o) == \
               Required(Optional(Option('-h')))
    assert parse_pattern('[ ARG ... ]', options=o) == \
               Required(Optional(OneOrMore(Argument('ARG'))))
    assert parse_pattern('[ -h | -v ]', options=o) == \
               Required(Optional(Either(Option('-h'),
                                Option('-v', '--verbose'))))
    assert parse_pattern('( -h | -v [ --file <f> ] )', options=o) == \
               Required(Required(
                   Either(Option('-h'),
                          Required(Option('-v', '--verbose'),
                               Optional(Option('-f', '--file', 1, None))))))
    assert parse_pattern('(-h|-v[--file=<f>]N...)', options=o) == \
               Required(Required(Either(Option('-h'),
                              Required(Option('-v', '--verbose'),
                                  Optional(Option('-f', '--file', 1, None)),
                                     OneOrMore(Argument('N'))))))
    assert parse_pattern('(N [M | (K | L)] | O P)', options=[]) == \
               Required(Required(Either(
                   Required(Argument('N'),
                            Optional(Either(Argument('M'),
                                            Required(Either(Argument('K'),
                                                            Argument('L')))))),
                   Required(Argument('O'), Argument('P')))))
    assert parse_pattern('[ -h ] [N]', options=o) == \
               Required(Optional(Option('-h')),
                        Optional(Argument('N')))
    assert parse_pattern('[options]', options=o) == \
            Required(Optional(OptionsShortcut()))
    assert parse_pattern('[options] A', options=o) == \
            Required(Optional(OptionsShortcut()),
                     Argument('A'))
    assert parse_pattern('-v [options]', options=o) == \
            Required(Option('-v', '--verbose'),
                     Optional(OptionsShortcut()))
    assert parse_pattern('ADD', options=o) == Required(Argument('ADD'))
    assert parse_pattern('<add>', options=o) == Required(Argument('<add>'))
    assert parse_pattern('add', options=o) == Required(Command('add'))