Exemple #1
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'
Exemple #2
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)
Exemple #3
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(),
    }
Exemple #4
0
def test_resolve():
    context = {
        'a': {
            'b': 'c',
        },
    }
    scope = Scope(['a', 'b'])
    assert scope.resolve(context) == 'c'
Exemple #5
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'
Exemple #6
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)
Exemple #7
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)"
Exemple #8
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...'
Exemple #9
0
def test_compare_to_different_type():
    assert Scope() != 2
Exemple #10
0
def test_from_string_iterable():
    scope = Scope.from_string('a.2.c...')
    assert scope == Scope(['a', 2, 'c'], is_iterable=True)
Exemple #11
0
def test_from_string_indexes():
    scope = Scope.from_string('a.2.c')
    assert scope == Scope(['a', 2, 'c'])
Exemple #12
0
def test_from_string_path():
    scope = Scope.from_string('a.b.c')
    assert scope == Scope(['a', 'b', 'c'])
Exemple #13
0
def test_from_string_single():
    scope = Scope.from_string('a')
    assert scope == Scope(['a'])
Exemple #14
0
def test_from_string_empty_iterable():
    scope = Scope.from_string('...')
    assert scope == Scope(is_iterable=True)
Exemple #15
0
def test_from_string_empty():
    scope = Scope.from_string('')
    assert scope == Scope()