Exemplo n.º 1
0
        public void TestElifDeep()
        {
            var elifs          = string.Join("\n", Enumerable.Range(1, 999).Select(index => string.Format(CultureInfo.InvariantCulture, "{{% elif a == {0} %}}{0}", index)));
            var templateString = string.Format(CultureInfo.InvariantCulture, "{{% if a == 0 %}}0{0}{{% else %}}x{{% endif %}}", elifs);

            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString(templateString)
                );

            foreach (var x in new[] { 0, 10, 999 })
            {
                MyAssert.AreEqual(x.ToString(CultureInfo.InvariantCulture), template.Render(a: x).Trim());
            }
            MyAssert.AreEqual("x", template.Render(a: 1000).Trim());
        }
Exemplo n.º 2
0
        public void TestElseScopingItem()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{% for item in [] %}{% else %}{{ item }}{% endfor %}")
                );

            MyAssert.AreEqual("42", template.Render(item: 42));
        }
Exemplo n.º 3
0
        public void TestSimple()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{% for item in seq %}{{ item }}{% endfor %}")
                );

            MyAssert.AreEqual("0123456789", template.Render(seq: Enumerable.Range(0, 10).ToList()));
        }
Exemplo n.º 4
0
        public void TestCenter()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{{ \"foo\"|center(9) }}")
                );

            MyAssert.AreEqual("   foo   ", template.Render());
        }
Exemplo n.º 5
0
        public void TestCapitalize()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{{ \"foo bar\"|capitalize }}")
                );

            MyAssert.AreEqual("Foo bar", template.Render());
        }
Exemplo n.º 6
0
        public void TestBoolSelect()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{{ [none, false, 0, 1, 2, 3, 4, 5]|select|join(\" | \") }}")
                );

            MyAssert.AreEqual("1|2|3|4|5", template.Render());
        }
Exemplo n.º 7
0
        public void TestBoolReject()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{{ [none, false, 0, 1, 2, 3, 4, 5]|reject|join(\" | \") }}")
                );

            MyAssert.AreEqual("None|False|0", template.Render());
        }
Exemplo n.º 8
0
        public void TestBlock()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{{ \"foo bar baz\"|wordcount }}")
                );

            MyAssert.AreEqual("3", template.Render());
        }
Exemplo n.º 9
0
        public void TestAbs()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{{ -1|abs }}|{{ 1|abs }}")
                );

            MyAssert.AreEqual("1|1", template.Render());
        }
Exemplo n.º 10
0
        public void TestEmptyBlocks()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("<{% for item in seq %}{% else %}{% endfor %}>")
                );

            MyAssert.AreEqual("<>", template.Render());
        }
Exemplo n.º 11
0
        public void TestComplete()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{% if a %}A{% elif b %}B{% elif c == d %}", "C{% else %}D{% endif %}")
                );

            MyAssert.AreEqual("C", template.Render(a: 0, b: false, c: 42, d: 42.0));
        }
Exemplo n.º 12
0
        public void TestCallerUndefined()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString(@"{% set caller = 42 %}{% macro test() %}{{ caller is not defined }}{% endmacro %}{{ test() }}")
                );

            MyAssert.AreEqual("True", template.Render());
        }
Exemplo n.º 13
0
        public void TestScoping()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString(@"{% macro level1(data1) %}{% macro level2(data2) %}{{ data1 }}|{{ data2 }}{% endmacro %}{{ level2('bar') }}{% endmacro %}{{ level1('foo') }}")
                );

            MyAssert.AreEqual("foo|bar", template.Render());
        }
Exemplo n.º 14
0
        public void TestVarArgs()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString(@"{% macro test() %}{{ varargs|join('|') }}{% endmacro %}{{ test(1, 2, 3) }}")
                );

            MyAssert.AreEqual("1|2|3", template.Render());
        }
Exemplo n.º 15
0
        public void TestSimple()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString(@"{% macro say_hello(name) %}Hello {{ name }}!{% endmacro %}{{ say_hello('Peter') }}")
                );

            MyAssert.AreEqual("Hello Peter!", template.Render());
        }
Exemplo n.º 16
0
        public void TestElse()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{% if false %}XXX{% else %}...{% endif %}")
                );

            MyAssert.AreEqual("...", template.Render());
        }
Exemplo n.º 17
0
        public void TestNamespaceLoop()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString(
                    "{% set ns = namespace(found=false) %}",
                    "{% for x in range(4) %}",
                    "{% if x == v %}",
                    "{% set ns.found = true %}",
                    "{% endif %}",
                    "{% endfor %}",
                    "{{ ns.found }}"
                    )
                );

            MyAssert.AreEqual("True", template.Render(v: 3));
            MyAssert.AreEqual("False", template.Render(v: 4));
        }
Exemplo n.º 18
0
        public void TestBlockEscaping()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{% set foo %}<em>{{ test }}</em>{% endset %}foo: {{ foo }}")
                );

            MyAssert.AreEqual("foo: <em>&lt;unsafe&gt;</em>", template.Render(test: "<unsafe>"));
        }
Exemplo n.º 19
0
        public void TestNamespaceBlock()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{% set ns = namespace() %}{% set ns.bar %}42{% endset %}{{ ns.bar }}")
                );

            MyAssert.AreEqual("42", template.Render());
        }
Exemplo n.º 20
0
        public void TestNormal()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{% set foo = 1 %}{{ foo }}")
                );

            MyAssert.AreEqual("1", template.Render());
        }
Exemplo n.º 21
0
        public void TestBlock()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{% set foo %}42{% endset %}{{ foo }}")
                );

            MyAssert.AreEqual("42", template.Render());
        }
Exemplo n.º 22
0
        public void TestComplexCall()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString(@"{% macro test() %}[[{{ caller('data') }}]]{% endmacro %}{% call(data) test() %}{{ data }}{% endcall %}")
                );

            MyAssert.AreEqual("[[data]]", template.Render());
        }
Exemplo n.º 23
0
        public void TestDefault()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{{ missing|default('no') }}|{{ false|default('no') }}|{{ false|default('no', true) }}|{{ given|default('no') }}")
                );

            MyAssert.AreEqual("no|False|no|yes", template.Render(given: "yes"));
        }
Exemplo n.º 24
0
        public void TestEmpty()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("[{% if true %}{% else %}{% endif %}]")
                );

            MyAssert.AreEqual("[]", template.Render());
        }
Exemplo n.º 25
0
        public void TestArguments()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString(@"{% macro m(a, b, c='c', d='d') %}{{ a }}|{{ b }}|{{ c }}|{{ d }}{% endmacro %}{{ m() }}|{{ m('a') }}|{{ m('a', 'b') }}|{{ m(1, 2, 3) }}")
                );

            MyAssert.AreEqual("||c|d|a||c|d|a|b|c|d|1|2|3|d", template.Render());
        }
Exemplo n.º 26
0
        public void TestChaining()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{{ ['<foo>', '<bar>']|first|upper|escape }}")
                );

            MyAssert.AreEqual("&lt;FOO&gt", template.Render());
        }
Exemplo n.º 27
0
        public void TestBatch()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{{ foo|batch(3)|list }}|{{ foo|batch(3, 'X')|list }}")
                );
            var output = template.Render(foo: Enumerable.Range(0, 10));

            MyAssert.AreEqual("[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]|[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]", output);
        }
Exemplo n.º 28
0
        public void TestCallSelf()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString(@"{% macro foo(x) %}{{ x }}{% if x > 1 %}|",
                                        "{{ foo(x - 1) }}{% endif %}{% endmacro %}",
                                        "{{ foo(5) }}")
                );

            MyAssert.AreEqual("5|4|3|2|1", template.Render());
        }
Exemplo n.º 29
0
        public void TestInitNamespace()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString("{% set ns = namespace(d, self=37) %}{% set ns.b = 42 %}{{ ns.a }}|{{ ns.self }}|{{ ns.b }}")
                );

            MyAssert.AreEqual("13|37|42", template.Render(d: new Dictionary <string, object?>
            {
                { "a", 13 }
            }));
        }
Exemplo n.º 30
0
        public void TestBlockFiltered()
        {
            dynamic template = new DynamicTemplateRenderer(
                _Environment.FromString(
                    "{% set foo | trim | length | string %} 42    {% endset %}",
                    "{{ foo }}"
                    )
                );

            MyAssert.AreEqual("2", template.Render());
        }