Beispiel #1
0
def test_resolve():
    context = {
        'a': {
            'b': 'c',
        },
    }
    scope = Scope(['a', 'b'])
    assert scope.resolve(context) == 'c'
Beispiel #2
0
def test_resolve_attributes():
    class Context(object):
        def __init__(self, **kwargs):
            for key, value in kwargs.items():
                setattr(self, key, value)

    context = Context(a=Context(b='c'))
    scope = Scope(['a', 'b'])
    assert scope.resolve(context) == 'c'
Beispiel #3
0
def test_generate_scoped_contexts_iterable_scopes(context):
    target = Target(
        template_name='foo.txt.tpl',
        filename='foo.txt',
        scopes={
            'one': Scope(['a', 'b']),
            'two': Scope(['f']),
            'three': Scope(['h'], is_iterable=True),
            'four': Scope(['l'], is_iterable=True),
        },
    )
    result = target.generate_scoped_contexts(context)
    assert next(result) == {
        'one': 'c',
        'two': 'g',
        'three': 'i',
        'four': 'm',
    }
    assert next(result) == {
        'one': 'c',
        'two': 'g',
        'three': 'j',
        'four': 'm',
    }
    assert next(result) == {
        'one': 'c',
        'two': 'g',
        'three': 'k',
        'four': 'm',
    }
    assert next(result) == {
        'one': 'c',
        'two': 'g',
        'three': 'i',
        'four': 'n',
    }
    assert next(result) == {
        'one': 'c',
        'two': 'g',
        'three': 'j',
        'four': 'n',
    }
    assert next(result) == {
        'one': 'c',
        'two': 'g',
        'three': 'k',
        'four': 'n',
    }

    with raises(StopIteration):
        next(result)
Beispiel #4
0
def test_load_targets(target):
    stream = StringIO()
    yaml.dump(
        {
            'targets': {
                'foo': {
                    'template_name': 'foo.txt.tpl',
                    'filename': 'foo.txt',
                    'scopes': {
                        'path': 'path',
                    },
                },
            },
        }, stream)
    stream.seek(0)
    index = Index.load('', stream)
    target.assert_called_once_with(
        filename='foo.txt',
        template_name='foo.txt.tpl',
        scopes={
            'path': Scope(['path']),
        },
    )
    assert index.targets == {
        'foo': target(),
    }
Beispiel #5
0
def test_generate_scoped_contexts_simple_scopes(context):
    target = Target(
        template_name='foo.txt.tpl',
        filename='foo.txt',
        scopes={
            'one': Scope(['a', 'b']),
            'two': Scope(['f']),
            'three': Scope(['h']),
        },
    )
    result = target.generate_scoped_contexts(context)
    assert next(result) == {
        'one': 'c',
        'two': 'g',
        'three': ['i', 'j', 'k'],
    }

    with raises(StopIteration):
        next(result)
Beispiel #6
0
def test_generate():
    target = Target(
        template_name="foo.txt.tpl", filename="foo{{ x }}.txt", scopes={"x": Scope.from_string("numbers...")}
    )
    environment = Environment(loader=DictLoader({"foo.txt.tpl": "foo {{ x }}"}))
    context = {"numbers": [42, 7]}
    result = target.generate(environment, context)

    assert next(result) == ("foo42.txt", "foo 42")
    assert next(result) == ("foo7.txt", "foo 7")

    with raises(StopIteration):
        next(result)
Beispiel #7
0
def test_generate():
    target = Target(
        template_name='foo.txt.tpl',
        filename='foo{{ x }}.txt',
        scopes={'x': Scope.from_string('numbers...')},
    )
    environment = Environment(loader=DictLoader({
        'foo.txt.tpl': 'foo {{ x }}',
    }))
    context = {'numbers': [42, 7]}
    result = target.generate(environment, context)

    assert next(result) == ('foo42.txt', 'foo 42')
    assert next(result) == ('foo7.txt', 'foo 7')

    with raises(StopIteration):
        next(result)
Beispiel #8
0
def test_resolve_with_attributes():
    class A(object):
        a = 'a'

        def __init__(self):
            self.b = 'b'

    scope = Scope(['a'])
    assert scope.resolve(A()) == 'a'

    scope = Scope(['b'])
    assert scope.resolve(A()) == 'b'
Beispiel #9
0
def test_str():
    assert str(Scope()) == ''
    assert str(Scope(['a', 'b'])) == 'a.b'
    assert str(Scope(is_iterable=True)) == '...'
    assert str(Scope(['a', 'b'], is_iterable=True)) == 'a.b...'
Beispiel #10
0
def test_from_string_empty_iterable():
    scope = Scope.from_string('...')
    assert scope == Scope(is_iterable=True)
Beispiel #11
0
def test_from_string_empty():
    scope = Scope.from_string('')
    assert scope == Scope()
Beispiel #12
0
def test_from_string_path():
    scope = Scope.from_string('a.b.c')
    assert scope == Scope(['a', 'b', 'c'])
Beispiel #13
0
def test_from_string_single():
    scope = Scope.from_string('a')
    assert scope == Scope(['a'])
Beispiel #14
0
def test_from_string_indexes():
    scope = Scope.from_string('a.2.c')
    assert scope == Scope(['a', 2, 'c'])
Beispiel #15
0
def test_from_string_iterable():
    scope = Scope.from_string('a.2.c...')
    assert scope == Scope(['a', 2, 'c'], is_iterable=True)
Beispiel #16
0
def test_repr():
    assert repr(Scope()) == 'Scope()'
    assert repr(Scope(['a', 'b'])) == "Scope(['a', 'b'])"
    assert repr(Scope(is_iterable=True)) == "Scope(is_iterable=True)"
    assert repr(Scope(['a', 'b'], is_iterable=True)) == \
        "Scope(['a', 'b'], is_iterable=True)"
Beispiel #17
0
def test_from_string_invalid():
    with pytest.raises(InvalidScope) as error:
        Scope.from_string('&$#@')

    assert error.value.scope == '&$#@'
Beispiel #18
0
def test_compare_to_different_type():
    assert Scope() != 2