public async Task TestForWithContinue() { var assigns = Hash.FromAnonymousObject(new { array = new { items = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } } }); string markup = "{% for i in array.items %}{% if i == 3 %}{% continue %}{% endif %}{{ i }}{% endfor %}"; string expected = "1245678910"; await Helper.AssertTemplateResultAsync(expected, markup, assigns); }
public async Task TestHasABlockWhichDoesNothing() { await Helper.AssertTemplateResultAsync("the comment block should be removed .. right?", "the comment block should be removed {%comment%} be gone.. {%endcomment%} .. right?"); await Helper.AssertTemplateResultAsync("", "{%comment%}{%endcomment%}"); await Helper.AssertTemplateResultAsync("", "{%comment%}{% endcomment %}"); await Helper.AssertTemplateResultAsync("", "{% comment %}{%endcomment%}"); await Helper.AssertTemplateResultAsync("", "{% comment %}{% endcomment %}"); await Helper.AssertTemplateResultAsync("", "{%comment%}comment{%endcomment%}"); await Helper.AssertTemplateResultAsync("", "{% comment %}comment{% endcomment %}"); await Helper.AssertTemplateResultAsync("foobar", "foo{%comment%}comment{%endcomment%}bar"); await Helper.AssertTemplateResultAsync("foobar", "foo{% comment %}comment{% endcomment %}bar"); await Helper.AssertTemplateResultAsync("foobar", "foo{%comment%} comment {%endcomment%}bar"); await Helper.AssertTemplateResultAsync("foobar", "foo{% comment %} comment {% endcomment %}bar"); await Helper.AssertTemplateResultAsync("foo bar", "foo {%comment%} {%endcomment%} bar"); await Helper.AssertTemplateResultAsync("foo bar", "foo {%comment%}comment{%endcomment%} bar"); await Helper.AssertTemplateResultAsync("foo bar", "foo {%comment%} comment {%endcomment%} bar"); await Helper.AssertTemplateResultAsync("foobar", @"foo{%comment%} {%endcomment%}bar"); }
public async Task TestForWithBreak() { var assigns = Hash.FromAnonymousObject(new { array = new { items = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } } }); var markup = "{% for i in array.items %}{{ i }}{% if i > 3 %}{% break %}{% endif %}{% endfor %}"; var expected = "1234"; await Helper.AssertTemplateResultAsync(expected, markup, assigns); }
public async Task TestAssignWithColonAndSpaces() { var assigns = Hash.FromAnonymousObject(new { var = new Dictionary <string, object> { { "a:b c", new { paged = 1 } } } }); await Helper.AssertTemplateResultAsync("var2: 1", "{%assign var2 = var['a:b c'].paged %}var2: {{var2}}", assigns); }
public async Task TestMultipleNamedCyclesWithNamesFromContext() { var assigns = Hash.FromAnonymousObject(new { var1 = 1, var2 = 2 }); await Helper.AssertTemplateResultAsync("one one two two one one", "{%cycle var1: 'one', 'two' %} {%cycle var2: 'one', 'two' %} {%cycle var1: 'one', 'two' %} {%cycle var2: 'one', 'two' %} {%cycle var1: 'one', 'two' %} {%cycle var2: 'one', 'two' %}", assigns); }
public async Task TestHyphenatedAssign() { var assigns = Hash.FromDictionary(new Dictionary <string, object> { { "a-b", "1" } }); await Helper.AssertTemplateResultAsync("a-b:1 a-b:2", "a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}", assigns); }
public async Task TestUnless() { await Helper.AssertTemplateResultAsync(" ", " {% unless true %} this text should not go into the output {% endunless %} "); await Helper.AssertTemplateResultAsync(" this text should go into the output ", " {% unless false %} this text should go into the output {% endunless %} "); await Helper.AssertTemplateResultAsync(" you rock ?", "{% unless true %} you suck {% endunless %} {% unless false %} you rock {% endunless %}?"); }
public async Task TestUnlessElse() { await Helper.AssertTemplateResultAsync(" YES ", "{% unless true %} NO {% else %} YES {% endunless %}"); await Helper.AssertTemplateResultAsync(" YES ", "{% unless false %} YES {% else %} NO {% endunless %}"); await Helper.AssertTemplateResultAsync(" YES ", "{% unless 'foo' %} NO {% else %} YES {% endunless %}"); }
public async Task TestAssignDecimalInlineWithInvariantDecimalSeparatorInFrenchCulture() { using (CultureHelper.SetCulture("fr-FR")) { await Helper.AssertTemplateResultAsync(string.Format("2{0}5", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator), "{% assign foo = 2.5 %}{{ foo }}"); } }
public async Task TestAssignDecimalInlineWithEnglishGroupSeparator() { using (CultureHelper.SetCulture("en-GB")) { await Helper.AssertTemplateResultAsync("2500", "{% assign foo = 2,500 %}{{ foo }}"); } }
public async Task TestAssignDecimalInlineWithEnglishDecimalSeparator() { using (CultureHelper.SetCulture("en-GB")) { await Helper.AssertTemplateResultAsync(string.Format("2{0}5", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator), "{% assign foo = 2.5 %}{{ foo }}"); } }
public async Task TestIfAnd() { await Helper.AssertTemplateResultAsync(" YES ", "{% if true and true %} YES {% endif %}"); await Helper.AssertTemplateResultAsync("", "{% if false and true %} YES {% endif %}"); await Helper.AssertTemplateResultAsync("", "{% if false and true %} YES {% endif %}"); }
public void TestIfMaxConditions() { var se = Assert.Throws <SyntaxException>(() => Helper.AssertTemplateResultAsync("", "{% if 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 and 1 %}too many conditions{% endif %}").GetAwaiter().GetResult()); StringAssert.Contains("'if'", se.Message); StringAssert.Contains("tag", se.Message); StringAssert.Contains("500", se.Message); }
public async Task TestIfChanged() { var assigns = Hash.FromAnonymousObject(new { array = new[] { 1, 1, 2, 2, 3, 3 } }); await Helper.AssertTemplateResultAsync("123", "{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}", assigns); assigns = Hash.FromAnonymousObject(new { array = new[] { 1, 1, 1, 1 } }); await Helper.AssertTemplateResultAsync("1", "{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}", assigns); }
public async Task TestIfElse() { await Helper.AssertTemplateResultAsync(" YES ", "{% if false %} NO {% else %} YES {% endif %}"); await Helper.AssertTemplateResultAsync(" YES ", "{% if true %} YES {% else %} NO {% endif %}"); await Helper.AssertTemplateResultAsync(" YES ", "{% if 'foo' %} YES {% else %} NO {% endif %}"); }
public async Task TestAssignedVariable() { await Helper.AssertTemplateResultAsync(".foo.", "{% assign foo = values %}.{{ foo[0] }}.", Hash.FromAnonymousObject(new { values = new[] { "foo", "bar", "baz" } })); await Helper.AssertTemplateResultAsync(".bar.", "{% assign foo = values %}.{{ foo[1] }}.", Hash.FromAnonymousObject(new { values = new[] { "foo", "bar", "baz" } })); }
public async Task TestDynamicVariableLimiting() { var assigns = Hash.FromAnonymousObject(new { array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } }); assigns["limit"] = 2; assigns["offset"] = 2; await Helper.AssertTemplateResultAsync("34", "{%for i in array limit: limit offset: offset %}{{ i }}{%endfor%}", assigns); }
public void TestComparisonOfStringsContainingAndOrOr() { Assert.DoesNotThrow(() => { const string awfulMarkup = "a == 'and' and b == 'or' and c == 'foo and bar' and d == 'bar or baz' and e == 'foo' and foo and bar"; Hash assigns = Hash.FromAnonymousObject(new { a = "and", b = "or", c = "foo and bar", d = "bar or baz", e = "foo", foo = true, bar = true }); Helper.AssertTemplateResultAsync(" YES ", "{% if " + awfulMarkup + " %} YES {% endif %}", assigns).GetAwaiter().GetResult(); }); }
public async Task TestForWithDictionary() { var dictionary = new Dictionary <string, string> { { "Graham Greene", "English" }, { "F. Scott Fitzgerald", "American" } }; await Helper.AssertTemplateResultAsync(" English American ", "{%for item in authors%} {{ item }} {%endfor%}", Hash.FromAnonymousObject(new { authors = dictionary.Values })); }
public async Task TestElseIf() { await Helper.AssertTemplateResultAsync("0", "{% if 0 == 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}"); await Helper.AssertTemplateResultAsync("1", "{% if 0 != 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}"); await Helper.AssertTemplateResultAsync("2", "{% if 0 != 0 %}0{% elsif 1 != 1%}1{% else %}2{% endif %}"); await Helper.AssertTemplateResultAsync("elsif", "{% if false %}if{% elsif true %}elsif{% endif %}"); }
public async Task TestIllegalSymbols() { await Helper.AssertTemplateResultAsync("", "{% if true == empty %}?{% endif %}", new Hash()); await Helper.AssertTemplateResultAsync("", "{% if true == null %}?{% endif %}", new Hash()); await Helper.AssertTemplateResultAsync("", "{% if empty == true %}?{% endif %}", new Hash()); await Helper.AssertTemplateResultAsync("", "{% if null == true %}?{% endif %}", new Hash()); }
public async Task TestCaseWithElse() { var assigns = Hash.FromAnonymousObject(new { condition = 5 }); await Helper.AssertTemplateResultAsync(" hit ", "{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}", assigns); assigns = Hash.FromAnonymousObject(new { condition = 6 }); await Helper.AssertTemplateResultAsync(" else ", "{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}", assigns); assigns = Hash.FromAnonymousObject(new { condition = 6 }); await Helper.AssertTemplateResultAsync(" else ", "{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}", assigns); }
public async Task TestIfOrWithOperators() { await Helper.AssertTemplateResultAsync(" YES ", "{% if a == true or b == true %} YES {% endif %}", Hash.FromAnonymousObject(new { a = true, b = true })); await Helper.AssertTemplateResultAsync(" YES ", "{% if a == true or b == false %} YES {% endif %}", Hash.FromAnonymousObject(new { a = true, b = true })); await Helper.AssertTemplateResultAsync("", "{% if a == false or b == false %} YES {% endif %}", Hash.FromAnonymousObject(new { a = true, b = true })); }
public async Task TestLimiting() { var assigns = Hash.FromAnonymousObject(new { array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } }); await Helper.AssertTemplateResultAsync("12", "{%for i in array limit:2 %}{{ i }}{%endfor%}", assigns); await Helper.AssertTemplateResultAsync("1234", "{%for i in array limit:4 %}{{ i }}{%endfor%}", assigns); await Helper.AssertTemplateResultAsync("3456", "{%for i in array limit:4 offset:2 %}{{ i }}{%endfor%}", assigns); await Helper.AssertTemplateResultAsync("3456", "{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}", assigns); }
public async Task TestCycle() { await Helper.AssertTemplateResultAsync("one", "{%cycle 'one', 'two'%}"); await Helper.AssertTemplateResultAsync("one two", "{%cycle 'one', 'two'%} {%cycle 'one', 'two'%}"); await Helper.AssertTemplateResultAsync(" two", "{%cycle '', 'two'%} {%cycle '', 'two'%}"); await Helper.AssertTemplateResultAsync("one two one", "{%cycle 'one', 'two'%} {%cycle 'one', 'two'%} {%cycle 'one', 'two'%}"); await Helper.AssertTemplateResultAsync("text-align: left text-align: right", "{%cycle 'text-align: left', 'text-align: right' %} {%cycle 'text-align: left', 'text-align: right'%}"); }
public async Task TestCaseOnLengthWithElse() { await Helper.AssertTemplateResultAsync("else", "{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}", new Hash()); await Helper.AssertTemplateResultAsync("false", "{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}", new Hash()); await Helper.AssertTemplateResultAsync("true", "{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}", new Hash()); await Helper.AssertTemplateResultAsync("else", "{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}", new Hash()); }
public async Task TestPauseResumeLimit() { var assigns = Hash.FromAnonymousObject(new { array = Hash.FromAnonymousObject(new { items = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } }) }); const string markup = @"{%for i in array.items limit:3 %}{{i}}{%endfor%} next {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%} next {%for i in array.items offset:continue limit:1 %}{{i}}{%endfor%}"; const string expected = @"123 next 456 next 7"; await Helper.AssertTemplateResultAsync(expected, markup, assigns); }
public async Task TestIfWithCustomCondition() { DotLiquid.ConditionOperatorDelegate oldCondition = Condition.Operators["contains"]; Condition.Operators["contains"] = (left, right) => (left is IList) ? ((IList)left).Contains(right) : ((left is string) ? ((string)left).Contains((string)right) : false); try { await Helper.AssertTemplateResultAsync("yes", "{% if 'bob' contains 'o' %}yes{% endif %}"); await Helper.AssertTemplateResultAsync("no", "{% if 'bob' contains 'f' %}yes{% else %}no{% endif %}"); } finally { Condition.Operators["contains"] = oldCondition; } }
public async Task TestIfOr() { await Helper.AssertTemplateResultAsync(" YES ", "{% if a or b %} YES {% endif %}", Hash.FromAnonymousObject(new { a = true, b = true })); await Helper.AssertTemplateResultAsync(" YES ", "{% if a or b %} YES {% endif %}", Hash.FromAnonymousObject(new { a = true, b = false })); await Helper.AssertTemplateResultAsync(" YES ", "{% if a or b %} YES {% endif %}", Hash.FromAnonymousObject(new { a = false, b = true })); await Helper.AssertTemplateResultAsync("", "{% if a or b %} YES {% endif %}", Hash.FromAnonymousObject(new { a = false, b = false })); await Helper.AssertTemplateResultAsync(" YES ", "{% if a or b or c %} YES {% endif %}", Hash.FromAnonymousObject(new { a = false, b = false, c = true })); await Helper.AssertTemplateResultAsync("", "{% if a or b or c %} YES {% endif %}", Hash.FromAnonymousObject(new { a = false, b = false, c = false })); }
public async Task TestNoTransform() { await Helper.AssertTemplateResultAsync("this text should come out of the template without change...", "this text should come out of the template without change..."); await Helper.AssertTemplateResultAsync("blah", "blah"); await Helper.AssertTemplateResultAsync("<blah>", "<blah>"); await Helper.AssertTemplateResultAsync("|,.:", "|,.:"); await Helper.AssertTemplateResultAsync("", ""); const string text = @"this shouldnt see any transformation either but has multiple lines as you can clearly see here ..."; await Helper.AssertTemplateResultAsync(text, text); }