Exemple #1
0
def test_traject_no_duplicate_variables():
    traject = TrajectRegistry()

    with pytest.raises(TrajectError):
        traject.add_pattern('{foo}-{foo}', 'value')
    with pytest.raises(TrajectError):
        traject.add_pattern('{foo}/{foo}', 'value')
Exemple #2
0
def test_traject_no_concecutive_variables():
    traject = TrajectRegistry()

    def f():
        pass

    with pytest.raises(TrajectError):
        traject.add_pattern('{foo}{bar}', f)
Exemple #3
0
def test_traject_no_concecutive_variables():
    traject = TrajectRegistry()

    def f():
        pass

    with pytest.raises(TrajectError):
        traject.add_pattern('{foo}{bar}', f)
Exemple #4
0
def test_traject_multiple_steps_with_variables():
    traject = TrajectRegistry()

    class xy(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y

    traject.add_pattern('{x}/{y}', xy)
    obj = traject.consume(req('x/y'))
    assert obj.x == 'x'
    assert obj.y == 'y'
Exemple #5
0
def test_traject_multiple_steps_with_variables():
    traject = TrajectRegistry()

    class xy(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y

    traject.add_pattern('{x}/{y}', xy)
    obj = traject.consume(req('x/y'))
    assert obj.x == 'x'
    assert obj.y == 'y'
Exemple #6
0
def test_traject_no_type_conflict_middle():
    traject = TrajectRegistry()

    class int_f(object):
        def __init__(self, x):
            self.x = x

    class int_f2(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('a/{x}/y', int_f, converters=dict(x=Converter(int)))
    traject.add_pattern('a/{x}/z', int_f2, converters=dict(x=Converter(int)))
Exemple #7
0
def test_traject_with_converter():
    traject = TrajectRegistry()

    class found(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('{x}', found, converters=dict(x=Converter(int)))

    obj = traject.consume(req('1'))
    assert obj.x == 1

    assert traject.consume(req('foo')) is None
Exemple #8
0
def test_traject_variable_specific_first():
    traject = TrajectRegistry()
    traject.add_pattern('a/{x}/b', 'axb')
    traject.add_pattern('a/prefix{x}/b', 'aprefixxb')
    assert traject.consume(['b', 'lah', 'a']) == ('axb', [], {'x': 'lah'})
    assert traject.consume(['b', 'prefixlah', 'a']) == (
        'aprefixxb', [], {'x': 'lah'})
Exemple #9
0
def test_traject_type_conflict_middle_end():
    traject = TrajectRegistry()

    class int_f(object):
        def __init__(self, x):
            self.x = x

    class str_f(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('a/{x}/y', int_f, converters=dict(x=Converter(int)))
    with pytest.raises(TrajectError):
        traject.add_pattern('a/{x}', str_f)
Exemple #10
0
def test_traject_type_conflict_default_type():
    traject = TrajectRegistry()

    class found_str(object):
        def __init__(self, x):
            self.x = x

    class found_int(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('{x}', found_str)
    with pytest.raises(TrajectError):
        traject.add_pattern('{x}', found_int,
                            converters=dict(x=Converter(int)))
Exemple #11
0
def test_traject_type_conflict_explicit_default():
    traject = TrajectRegistry()

    class found_explicit(object):
        def __init__(self, x):
            self.x = x

    class found_implicit(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('{x}', found_explicit,
                        converters=dict(x=IDENTITY_CONVERTER))
    traject.add_pattern('{x}', found_implicit)
    # these add_pattern calls are equivalent so will not result in an error
    assert True
Exemple #12
0
def test_traject_greedy_middle_prefix():
    traject = TrajectRegistry()
    traject.add_pattern('a/prefix{x}/y', 'prefix')
    traject.add_pattern('a/{x}/z', 'no_prefix')

    assert traject.consume(['y', 'prefixX', 'a']) == ('prefix', [], {'x': 'X'})
    assert traject.consume(['z', 'prefixX', 'a']) == (None, ['z'], {'x': 'X'})
    assert traject.consume(['z', 'blah', 'a']) == (
        'no_prefix', [], {'x': 'blah'})
Exemple #13
0
def test_traject_variable_specific_first():
    traject = TrajectRegistry()
    traject.add_pattern('a/{x}/b', 'axb')
    traject.add_pattern('a/prefix{x}/b', 'aprefixxb')
    assert traject.consume(['b', 'lah', 'a']) == ('axb', [], {'x': 'lah'})
    assert traject.consume(['b', 'prefixlah', 'a']) == (
        'aprefixxb', [], {'x': 'lah'})
Exemple #14
0
def test_traject_greedy_middle_prefix():
    traject = TrajectRegistry()
    traject.add_pattern('a/prefix{x}/y', 'prefix')
    traject.add_pattern('a/{x}/z', 'no_prefix')

    assert traject.consume(['y', 'prefixX', 'a']) == ('prefix', [], {'x': 'X'})
    assert traject.consume(['z', 'prefixX', 'a']) == (None, ['z'], {'x': 'X'})
    assert traject.consume(['z', 'blah', 'a']) == (
        'no_prefix', [], {'x': 'blah'})
Exemple #15
0
def test_traject_no_duplicate_variables():
    traject = TrajectRegistry()

    with pytest.raises(TrajectError):
        traject.add_pattern('{foo}-{foo}', 'value')
    with pytest.raises(TrajectError):
        traject.add_pattern('{foo}/{foo}', 'value')
Exemple #16
0
def test_traject_no_duplicate_variables():
    traject = TrajectRegistry()

    def f():
        pass

    with pytest.raises(TrajectError):
        traject.add_pattern("{foo}-{foo}", f)
    with pytest.raises(TrajectError):
        traject.add_pattern("{foo}/{foo}", f)
Exemple #17
0
def test_traject_greedy_middle_prefix():
    traject = TrajectRegistry()

    class prefix(object):
        def __init__(self, x):
            self.x = x

    class no_prefix(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('a/prefix{x}/y', prefix)
    traject.add_pattern('a/{x}/z', no_prefix)

    obj = traject.consume(req('a/prefixX/y'))
    assert obj.x == 'X'
    assert isinstance(obj, prefix)

    assert traject.consume(req('a/prefixX/z')) is None

    obj = traject.consume(req('a/blah/z'))
    assert obj.x == 'blah'
    assert isinstance(obj, no_prefix)
Exemple #18
0
def test_traject_no_type_conflict_middle():
    traject = TrajectRegistry()

    class int_f(object):
        def __init__(self, x):
            self.x = x

    class int_f2(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('a/{x}/y', int_f, converters=dict(x=Converter(int)))
    traject.add_pattern('a/{x}/z', int_f2, converters=dict(x=Converter(int)))
Exemple #19
0
def test_traject_with_converter():
    traject = TrajectRegistry()

    class found(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('{x}', found, converters=dict(x=Converter(int)))

    obj = traject.consume(req('1'))
    assert obj.x == 1

    assert traject.consume(req('foo')) is None
Exemple #20
0
def test_traject_variable_specific_first():
    traject = TrajectRegistry()

    class axb(object):
        def __init__(self, x):
            self.x = x

    class aprefixxb(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('a/{x}/b', axb)
    traject.add_pattern('a/prefix{x}/b', aprefixxb)

    obj = traject.consume(req('a/lah/b'))
    assert isinstance(obj, axb)
    assert obj.x == 'lah'

    obj = traject.consume(req('a/prefixlah/b'))
    assert isinstance(obj, aprefixxb)
    assert obj.x == 'lah'
Exemple #21
0
def test_traject_type_conflict_middle_end():
    traject = TrajectRegistry()

    class int_f(object):
        def __init__(self, x):
            self.x = x

    class str_f(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('a/{x}/y', int_f, converters=dict(x=Converter(int)))
    with pytest.raises(TrajectError):
        traject.add_pattern('a/{x}', str_f)
Exemple #22
0
def test_traject_greedy_middle_prefix():
    traject = TrajectRegistry()

    class prefix(object):
        def __init__(self, x):
            self.x = x

    class no_prefix(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('a/prefix{x}/y', prefix)
    traject.add_pattern('a/{x}/z', no_prefix)

    obj = traject.consume(req('a/prefixX/y'))
    assert obj.x == 'X'
    assert isinstance(obj, prefix)

    assert traject.consume(req('a/prefixX/z')) is None

    obj = traject.consume(req('a/blah/z'))
    assert obj.x == 'blah'
    assert isinstance(obj, no_prefix)
Exemple #23
0
def test_traject_type_conflict_default_type():
    traject = TrajectRegistry()

    class found_str(object):
        def __init__(self, x):
            self.x = x

    class found_int(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('{x}', found_str)
    with pytest.raises(TrajectError):
        traject.add_pattern('{x}',
                            found_int,
                            converters=dict(x=Converter(int)))
Exemple #24
0
def test_traject_type_conflict_explicit_default():
    traject = TrajectRegistry()

    class found_explicit(object):
        def __init__(self, x):
            self.x = x

    class found_implicit(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('{x}',
                        found_explicit,
                        converters=dict(x=IDENTITY_CONVERTER))
    traject.add_pattern('{x}', found_implicit)
    # these add_pattern calls are equivalent so will not result in an error
    assert True
Exemple #25
0
def test_traject_variable_specific_first():
    traject = TrajectRegistry()

    class axb(object):
        def __init__(self, x):
            self.x = x

    class aprefixxb(object):
        def __init__(self, x):
            self.x = x

    traject.add_pattern('a/{x}/b', axb)
    traject.add_pattern('a/prefix{x}/b', aprefixxb)

    obj = traject.consume(req('a/lah/b'))
    assert isinstance(obj, axb)
    assert obj.x == 'lah'

    obj = traject.consume(req('a/prefixlah/b'))
    assert isinstance(obj, aprefixxb)
    assert obj.x == 'lah'
Exemple #26
0
def test_traject_simple():
    traject = TrajectRegistry()
    traject.add_pattern('a/b/c', 'abc')
    traject.add_pattern('a/b/d', 'abd')
    traject.add_pattern('x/y', 'xy')
    traject.add_pattern('x/z', 'xz')

    assert traject.consume(['c', 'b', 'a']) == ('abc', [], {})
    assert traject.consume(['d', 'b', 'a']) == ('abd', [], {})
    assert traject.consume(['y', 'x']) == ('xy', [], {})
    assert traject.consume(['z', 'x']) == ('xz', [], {})
    assert traject.consume(['d', 'c', 'b', 'a']) == ('abc', ['d'], {})
    assert traject.consume(['d', 'd', 'b', 'a']) == ('abd', ['d'], {})
    assert traject.consume(['3', '2', '1', 'y', 'x']) == (
        'xy', ['3', '2', '1'], {})
    assert traject.consume(['3', '2', '1']) == (None, ['3', '2', '1'], {})
    assert traject.consume(['b', 'a']) == (None, [], {})
Exemple #27
0
def test_traject_simple():
    traject = TrajectRegistry()
    traject.add_pattern('a/b/c', 'abc')
    traject.add_pattern('a/b/d', 'abd')
    traject.add_pattern('x/y', 'xy')
    traject.add_pattern('x/z', 'xz')

    assert traject.consume(['c', 'b', 'a']) == ('abc', [], {})
    assert traject.consume(['d', 'b', 'a']) == ('abd', [], {})
    assert traject.consume(['y', 'x']) == ('xy', [], {})
    assert traject.consume(['z', 'x']) == ('xz', [], {})
    assert traject.consume(['d', 'c', 'b', 'a']) == ('abc', ['d'], {})
    assert traject.consume(['d', 'd', 'b', 'a']) == ('abd', ['d'], {})
    assert traject.consume(['3', '2', '1', 'y', 'x']) == (
        'xy', ['3', '2', '1'], {})
    assert traject.consume(['3', '2', '1']) == (None, ['3', '2', '1'], {})
    assert traject.consume(['b', 'a']) == (None, [], {})
Exemple #28
0
def test_traject_multiple_steps_with_variables():
    traject = TrajectRegistry()
    traject.add_pattern('{x}/{y}', 'xy')
    assert traject.consume(['y', 'x']) == ('xy', [], {'x': 'x', 'y': 'y'})
Exemple #29
0
def test_traject_type_conflict_default_type():
    traject = TrajectRegistry()
    traject.add_pattern('{x}', 'found_str')
    with pytest.raises(TrajectError):
        traject.add_pattern('{x}', 'found_int', dict(x=Converter(int)))
Exemple #30
0
def test_traject_type_conflict_default_type():
    traject = TrajectRegistry()
    traject.add_pattern('{x}', 'found_str')
    with pytest.raises(TrajectError):
        traject.add_pattern('{x}', 'found_int', dict(x=Converter(int)))
Exemple #31
0
def test_traject_with_converter():
    traject = TrajectRegistry()
    traject.add_pattern('{x}', 'found', dict(x=Converter(int)))
    assert traject.consume(['1']) == ('found', [], {'x': 1})
    assert traject.consume(['foo']) == (None, ['foo'], {})
Exemple #32
0
def test_traject_simple():
    traject = TrajectRegistry()

    class abc(object):
        pass

    class abd(object):
        pass

    class xy():
        pass

    class xz():
        pass

    traject.add_pattern('a/b/c', abc)
    traject.add_pattern('a/b/d', abd)
    traject.add_pattern('x/y', xy)
    traject.add_pattern('x/z', xz)

    r = req('a/b/c')
    assert isinstance(traject.consume(r), abc)
    assert r.unconsumed == []

    assert isinstance(traject.consume(req('a/b/d')), abd)
    assert isinstance(traject.consume(req('x/y')), xy)
    assert isinstance(traject.consume(req('x/z')), xz)

    r = req('a/b/c/d')
    assert isinstance(traject.consume(r), abc)
    assert r.unconsumed == ['d']

    r = req('a/b/d/d')
    assert isinstance(traject.consume(r), abd)
    assert r.unconsumed == ['d']

    r = req('x/y/1/2/3')
    assert isinstance(traject.consume(r), xy)
    assert r.unconsumed == ['3', '2', '1']

    r = req('1/2/3')
    assert traject.consume(r) is None
    assert r.unconsumed == ['3', '2', '1']

    r = req('a/b')
    assert traject.consume(r) is None
    assert r.unconsumed == []
Exemple #33
0
def test_traject_simple():
    traject = TrajectRegistry()

    class abc(object):
        pass

    class abd(object):
        pass

    class xy:
        pass

    class xz:
        pass

    traject.add_pattern("a/b/c", abc)
    traject.add_pattern("a/b/d", abd)
    traject.add_pattern("x/y", xy)
    traject.add_pattern("x/z", xz)

    r = req("a/b/c")
    assert isinstance(traject.consume(r), abc)
    assert r.unconsumed == []

    assert isinstance(traject.consume(req("a/b/d")), abd)
    assert isinstance(traject.consume(req("x/y")), xy)
    assert isinstance(traject.consume(req("x/z")), xz)

    r = req("a/b/c/d")
    assert isinstance(traject.consume(r), abc)
    assert r.unconsumed == ["d"]

    r = req("a/b/d/d")
    assert isinstance(traject.consume(r), abd)
    assert r.unconsumed == ["d"]

    r = req("x/y/1/2/3")
    assert isinstance(traject.consume(r), xy)
    assert r.unconsumed == ["3", "2", "1"]

    r = req("1/2/3")
    assert traject.consume(r) is None
    assert r.unconsumed == ["3", "2", "1"]

    r = req("a/b")
    assert traject.consume(r) is None
    assert r.unconsumed == []
Exemple #34
0
def test_traject_multiple_steps_with_variables():
    traject = TrajectRegistry()
    traject.add_pattern('{x}/{y}', 'xy')
    assert traject.consume(['y', 'x']) == ('xy', [], {'x': 'x', 'y': 'y'})
Exemple #35
0
def test_traject_with_converter():
    traject = TrajectRegistry()
    traject.add_pattern('{x}', 'found', dict(x=Converter(int)))
    assert traject.consume(['1']) == ('found', [], {'x': 1})
    assert traject.consume(['foo']) == (None, ['foo'], {})
Exemple #36
0
def test_traject_no_type_conflict_middle_end():
    traject = TrajectRegistry()
    traject.add_pattern('a/{x}/y', 'int', dict(x=Converter(int)))
    traject.add_pattern('a/{x}', 'int2', dict(x=Converter(int)))
    assert True
Exemple #37
0
def test_traject_type_conflict_explicit_default():
    traject = TrajectRegistry()
    traject.add_pattern('{x}', 'found_explicit', dict(x=IDENTITY_CONVERTER))
    traject.add_pattern('{x}', 'found_implicit')
    # these add_pattern calls are equivalent so will not result in an error
    assert True
Exemple #38
0
def test_traject_type_conflict_middle_end():
    traject = TrajectRegistry()
    traject.add_pattern('a/{x}/y', 'int', dict(x=Converter(int)))
    with pytest.raises(TrajectError):
        traject.add_pattern('a/{x}', 'str')
Exemple #39
0
def test_traject_type_conflict_middle_end():
    traject = TrajectRegistry()
    traject.add_pattern('a/{x}/y', 'int', dict(x=Converter(int)))
    with pytest.raises(TrajectError):
        traject.add_pattern('a/{x}', 'str')
Exemple #40
0
def test_traject_no_type_conflict_middle_end():
    traject = TrajectRegistry()
    traject.add_pattern('a/{x}/y', 'int', dict(x=Converter(int)))
    traject.add_pattern('a/{x}', 'int2', dict(x=Converter(int)))
    assert True
Exemple #41
0
def test_traject_simple():
    traject = TrajectRegistry()

    class abc(object):
        pass

    class abd(object):
        pass

    class xy():
        pass

    class xz():
        pass

    traject.add_pattern('a/b/c', abc)
    traject.add_pattern('a/b/d', abd)
    traject.add_pattern('x/y', xy)
    traject.add_pattern('x/z', xz)

    r = req('a/b/c')
    assert isinstance(traject.consume(r), abc)
    assert r.unconsumed == []

    assert isinstance(traject.consume(req('a/b/d')), abd)
    assert isinstance(traject.consume(req('x/y')), xy)
    assert isinstance(traject.consume(req('x/z')), xz)

    r = req('a/b/c/d')
    assert isinstance(traject.consume(r), abc)
    assert r.unconsumed == ['d']

    r = req('a/b/d/d')
    assert isinstance(traject.consume(r), abd)
    assert r.unconsumed == ['d']

    r = req('x/y/1/2/3')
    assert isinstance(traject.consume(r), xy)
    assert r.unconsumed == ['3', '2', '1']

    r = req('1/2/3')
    assert traject.consume(r) is None
    assert r.unconsumed == ['3', '2', '1']

    r = req('a/b')
    assert traject.consume(r) is None
    assert r.unconsumed == []
Exemple #42
0
def test_traject_type_conflict_explicit_default():
    traject = TrajectRegistry()
    traject.add_pattern('{x}', 'found_explicit', dict(x=IDENTITY_CONVERTER))
    traject.add_pattern('{x}', 'found_implicit')
    # these add_pattern calls are equivalent so will not result in an error
    assert True