def test_resolve(): context = { 'a': { 'b': 'c', }, } scope = Scope(['a', 'b']) assert scope.resolve(context) == 'c'
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'
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)
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(), }
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)
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)
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)
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'
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...'
def test_from_string_empty_iterable(): scope = Scope.from_string('...') assert scope == Scope(is_iterable=True)
def test_from_string_empty(): scope = Scope.from_string('') assert scope == Scope()
def test_from_string_path(): scope = Scope.from_string('a.b.c') assert scope == Scope(['a', 'b', 'c'])
def test_from_string_single(): scope = Scope.from_string('a') assert scope == Scope(['a'])
def test_from_string_indexes(): scope = Scope.from_string('a.2.c') assert scope == Scope(['a', 2, 'c'])
def test_from_string_iterable(): scope = Scope.from_string('a.2.c...') assert scope == Scope(['a', 2, 'c'], is_iterable=True)
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)"
def test_from_string_invalid(): with pytest.raises(InvalidScope) as error: Scope.from_string('&$#@') assert error.value.scope == '&$#@'
def test_compare_to_different_type(): assert Scope() != 2