예제 #1
0
파일: test_target.py 프로젝트: ereOn/pygen
def test_generate_scoped_contexts_no_scopes(context):
    target = Target(template_name="foo.txt.tpl", filename="foo.txt", scopes={})
    result = target.generate_scoped_contexts(context)
    assert next(result) == context

    with raises(StopIteration):
        next(result)
예제 #2
0
파일: test_target.py 프로젝트: ereOn/pygen
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)
예제 #3
0
def test_generate_scoped_contexts_no_scopes(context):
    target = Target(
        template_name='foo.txt.tpl',
        filename='foo.txt',
        scopes={},
    )
    result = target.generate_scoped_contexts(context)
    assert next(result) == context

    with raises(StopIteration):
        next(result)
예제 #4
0
def test_get_template():
    target = Target(
        template_name='foo.txt.tpl',
        filename='foo{{ x }}.txt',
        scopes={},
    )
    environment = Environment(loader=DictLoader({
        'foo.txt.tpl': 'foo {{ x }}',
    }))
    target_template = target.get_template(environment)
    assert target_template.template.render({'x': 42}) == 'foo 42'
    assert target_template.filename_template.render({'x': 42}) == 'foo42.txt'
예제 #5
0
파일: test_target.py 프로젝트: ereOn/pygen
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)
예제 #6
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)
예제 #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)
예제 #8
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)
예제 #9
0
파일: test_target.py 프로젝트: ereOn/pygen
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)
예제 #10
0
파일: test_target.py 프로젝트: ereOn/pygen
def test_get_template():
    target = Target(template_name="foo.txt.tpl", filename="foo{{ x }}.txt", scopes={})
    environment = Environment(loader=DictLoader({"foo.txt.tpl": "foo {{ x }}"}))
    target_template = target.get_template(environment)
    assert target_template.template.render({"x": 42}) == "foo 42"
    assert target_template.filename_template.render({"x": 42}) == "foo42.txt"