public async Task TemplateIfElseIfElseMultipleTailingElse() { var template = @" {{--| #IF data == 'ABC'}} {{data}} {{#ELSEIF data == 'a'}} {{data}} {{/ELSEIF}} {{#ELSEIF data == 'c'}} {{data}} {{/ELSEIF}} {{#ELSEIF data == 'true'}} {{data}} {{/ELSEIF}} {{#ELSE}} {{--| root |--}} {{/ELSE}} {{/IF}}"; var data = new Dictionary <string, object>() { { "data", "false" }, { "root", "true" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(data["root"])); }
public async Task TemplatePartialCanTrim() { var template = @" {{--| #DECLARE PartialA |--}} {{#IF otherother |--}} {{otherother}} {{--| #ELSE |--}} Invalid Context {{--| /ELSE |--}} {{--| /IF |--}} {{--| /DECLARE |--}} This is an partial included: ({{#IMPORT 'PartialA'}}) within the current context.<br/> This is an partial included: ({{#IMPORT 'PartialA' #WITH other}}) with an set context."; var subData = new Dictionary <string, object>(); subData["otherother"] = "Test"; var data = new Dictionary <string, object>(); data["other"] = subData; data["navigateUp"] = "Test 2"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.Not.Null); }
public async Task ParserCanChainFormatSubExpressionFromEach() { var template = "{{#each d.r}}{{d.Format('t').Format('t', f)}}{{/each}}"; var expectedValue = "formatter data value"; var data = new Dictionary <string, object> { { "d", new Dictionary <string, object>() { { "field", "field value" }, { "r", new List <Dictionary <string, object> >() { new Dictionary <string, object> { { "d", "display data value" }, { "f", expectedValue } } } } } }, }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts, options => { options.Formatters.AddSingle( new Func <object, object, object, object>((source, tempValue, reference) => reference), "Format"); options.Formatters.AddSingle( new Func <object, object, object>((source, tempValue) => source), "Format"); }); Assert.That(result, Is.EqualTo(expectedValue)); }
public async Task TemplateRendersWithComplexScopePath() { var template = @"{{#SCOPE Company.ceo}}{{#each products}}<li>{{name}} and {{version}} and has a CEO: {{../../last_name}}</li>{{/each}}{{/SCOPE}}"; var data = new Dictionary <string, object>() { { "Company", new Dictionary <string, object>() { { "ceo", new Dictionary <string, object>() { { "last_name", "Smith" }, { "products", Enumerable.Range(0, 3).Select(k => { var r = new Dictionary <string, object>(); r["name"] = "name " + k; r["version"] = "version " + k; return(r); }).ToArray() } } } } } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo("<li>name 0 and version 0 and has a CEO: Smith</li>" + "<li>name 1 and version 1 and has a CEO: Smith</li>" + "<li>name 2 and version 2 and has a CEO: Smith</li>")); }
public async Task ParserCanChainFormatSubExpression() { var template = "{{#SCOPE data}}{{Self('V').Self('V', r.d)}}{{/SCOPE}}"; var referenceDataValue = "reference data value"; var data = new Dictionary <string, object> { { "data", new { field = "field value", r = new { d = referenceDataValue } } }, }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts, options => { options.Formatters.AddSingle( new Func <object, object, object, object>((source, tempValue, reference) => reference), "Self"); options.Formatters.AddSingle( new Func <object, object, object>((source, tempValue) => source), "Self"); }); Assert.That(result, Is.EqualTo(referenceDataValue)); }
public async Task ParserCanFormatMultipleUnnamedWithoutResult() { var formatterResult = ""; var template = "{{#SCOPE data}}{{Self('test', 'arg', 'arg, arg', ' spaced ', ' spaced with quote \\' ' , this )}}{{/SCOPE}}"; var data = new Dictionary <string, object>() { { "data", 123123123 } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts | ParserOptionTypes.NoRerenderingTest, options => { options.DisableContentEscaping = true; options.Formatters.AddSingle(new Action <int, string[]>( (self, test) => { Assert.Fail("Should not be invoked"); }), "Self"); options.Formatters.AddSingle(new Action <int, string, string, string, string, string, int>( (self, test, arg, argarg, spacedArg, spacedWithQuote, refSelf) => { formatterResult = string.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}", self, test, arg, argarg, spacedArg, spacedWithQuote, refSelf); }), "Self"); }); Assert.That(result, Is.Empty); Assert.That(formatterResult, Is.EqualTo(@"123123123|test|arg|arg, arg| spaced | spaced with quote ' |123123123")); }
public async Task TemplateRendersWithComplexUpScopePathWithFormatting() { var template = @"{{#SCOPE d.d.n}}{{../../../r.ToString('c')}}{{/SCOPE}}"; var data = new Dictionary <string, object>() { { "d", new Dictionary <string, object>() { { "d", new Dictionary <string, object>() { { "n", (int?)1 } } } } }, { "r", "tset" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(data["r"])); }
public async Task TemplateRendersWithComplexRootScopePathWithFormatting() { var template = @"{{#SCOPE Data1.Data2.NullableInit}}{{~root}}{{/SCOPE}}"; var data = new Dictionary <string, object>() { { "Data1", new Dictionary <string, object>() { { "Data2", new Dictionary <string, object>() { { "NullableInit", (int?)1 } } } } }, { "root", "tset" } }; //1.ToString("E") var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(data["root"])); }
public async Task TemplateRendersContentWithNoVariables() { var data = new Dictionary <string, object>(); var template = @"ASDF"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo("ASDF")); }
public async Task NegationGroupRendersContentWhenValueNotSet() { var data = new Dictionary <string, object>(); var template = @"{{^SCOPE stuff}}No Stuff Here.{{/SCOPE}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo("No Stuff Here.")); }
public async Task TestEscapingBlocksAreExcluded() { var data = new Dictionary <string, object>(); var template = @"{{!?}} Dies ist ein Escaped block with {{Some}} {{Invalid {{/!?}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options | ParserOptionTypes.NoRerenderingTest); Assert.That(result, Is.EqualTo(" Dies ist ein Escaped block with {{Some}} {{Invalid ")); }
public async Task TestMultiLineExcludesAreExcludedFromOutput() { var data = new Dictionary <string, object>(); var template = @"A{{!?}}ZZZ{{/!?}}B{{!?}} {{123}} {{'{{'}} {{'}} }} {{/!?}}C {{!= angularjs}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options | ParserOptionTypes.NoRerenderingTest); Assert.That(result, Is.EqualTo("AZZZB {{123}} {{'{{'}} {{'}} }} C {{ angularjs}}")); }
public async Task TestCanFormatSourceObjectLessFormatter() { var template = "{{DateTimeNow().ToString('D')}}"; var data = new Dictionary <string, object>(); var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts); Assert.That(result, Is.EqualTo(DateTime.Now.ToString("D", ParserFixture.DefaultCulture))); }
public async Task TestCanFormatSourceObjectLessFormatterAsArgumentAsync() { var template = "{{TimeSpanFromDays(DateTimeNow().Day).ToString('g')}}"; var data = new Dictionary <string, object>(); var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts); Assert.That(result, Is.EqualTo(TimeSpan.FromDays(DateTime.Now.Day).ToString("g"))); }
public async Task CommentsAreExcludedFromOutput() { var data = new Dictionary <string, object>(); var template = @"as{{!stu ff}}df" ; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options | ParserOptionTypes.NoRerenderingTest); Assert.That(result, Is.EqualTo("asdf")); }
public async Task ParserCanCreateNestedPartials() { var data = new Dictionary <string, object>(); var template = @"{{#declare TestPartial}}{{#declare InnerPartial}}1{{/declare}}2{{/declare}}{{#IMPORT 'TestPartial'}}{{#IMPORT 'InnerPartial'}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo("21")); }
public async Task TestCanLocalizeDataWithArguments() { var template = "{{#LOCP 'WelcomeText'}}" + "{{#LOCPARAM ','}}" + "{{#LOC 'WelcomeDefine'}}" + "{{/LOCP}}"; var data = new Dictionary <string, object>() { }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, parserOptions => { parserOptions.RegisterLocalizationService(() => { return(new MorestachioLocalizationService() .AddResource(new MemoryTranslationResource() .Add("WelcomeText", CultureInfo.GetCultureInfo("EN-US"), "Hello{0} {1}") .Add("WelcomeText", CultureInfo.GetCultureInfo("DE-DE"), "Hallo{0} {1}") .Add("WelcomeDefine", CultureInfo.GetCultureInfo("EN-US"), "World") .Add("WelcomeDefine", CultureInfo.GetCultureInfo("DE-DE"), "Welt") ) .Load(new[] { CultureInfo.GetCultureInfo("EN-US"), CultureInfo.GetCultureInfo("DE-DE") })); }); parserOptions.CultureInfo = CultureInfo.GetCultureInfo("EN-US"); }); Assert.That(result, Is.EqualTo("Hello, World")); result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, parserOptions => { parserOptions.RegisterLocalizationService(() => { return(new MorestachioLocalizationService() .AddResource(new MemoryTranslationResource() .Add("WelcomeText", CultureInfo.GetCultureInfo("EN-US"), "Hello{0} {1}") .Add("WelcomeText", CultureInfo.GetCultureInfo("DE-DE"), "Hallo{0} {1}") .Add("WelcomeDefine", CultureInfo.GetCultureInfo("EN-US"), "World") .Add("WelcomeDefine", CultureInfo.GetCultureInfo("DE-DE"), "Welt") ) .Load(new[] { CultureInfo.GetCultureInfo("EN-US"), CultureInfo.GetCultureInfo("DE-DE") })); }); parserOptions.CultureInfo = CultureInfo.GetCultureInfo("DE-DE"); }); Assert.That(result, Is.EqualTo("Hallo, Welt")); }
public async Task TestInvertOperator() { var template = "{{(!A && B).ToString().ToLower()}}"; var data = new Dictionary <string, object>() { { "A", false }, { "B", true }, }; Assert.That(await ParserFixture.CreateAndParseWithOptions(template, data, _opts), Is.EqualTo("true")); }
public async Task CanUseNumberFunctionsOnData(object realData, string templateData, string operation, string expected) { var template = $"{{{{data.{operation}({templateData})}}}}"; var data = new { data = realData }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(expected)); }
public async Task CommentsAreOptionalTokenized() { var data = new Dictionary <string, object>(); var template = @"as{{!stu ff}}df" ; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, e => { e.TokenizeComments = true; }); Assert.That(result, Is.EqualTo("asdf")); }
public async Task HtmlIsEscapedByDefault() { var data = new Dictionary <string, object>() { { "stuff", "<b>inner</b>" } }; var template = @"{{stuff}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo("<b>inner</b>")); }
public async Task HtmlIsNotEscapedWhenUsingUnsafeSyntaxes() { var data = new Dictionary <string, object>() { { "stuff", "<b>inner</b>" } }; var template = @"{{&stuff}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo("<b>inner</b>")); }
public async Task CanUseNumberOperationsOnData(object realData, object templateData, string operation, object expected) { var template = $"{{{{data {operation} templateData}}}}"; var data = new { data = realData, templateData = templateData }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(expected.ToString())); }
public async Task TemplateShouldTreatFalseyValuesAsEmptyArray(object falseyModelValue) { var data = new Dictionary <string, object> { { "locations", falseyModelValue } }; var template = "{{#each locations}}Shouldn't be rendered!{{/each}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(string.Empty)); }
public async Task TemplatesShoudlNotRenderFalseyComplexStructures(object falseyModelValue) { var data = new Dictionary <string, object> { { "outer_level", falseyModelValue } }; var template = "{{#SCOPE outer_level}}Shouldn't be rendered!{{inner_level}}{{/SCOPE}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(string.Empty)); }
public async Task TemplateShouldRenderFalseValue() { var data = new Dictionary <string, object> { { "times_won", false } }; var template = "You've won {{times_won}} times!"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo("You've won False times!")); }
public async Task CanUseBooleanOperationsOnData(object realData, object templateData, string operation, object expected) { var template = $"{{{{data {operation} templateData}}}}"; var data = new { data = realData, templateData = templateData }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts); //var parsingOptions = new ParserOptions(template, null, ParserFixture.DefaultEncoding); //var result = await Parser.ParseWithOptions(parsingOptions).CreateAndStringifyAsync(data); Assert.That(result, Is.EqualTo(expected.ToString())); }
public async Task ParserCanTransferChains() { var template = @"{{#SCOPE data}}{{Self('(d(a))')}}{{/SCOPE}}"; var data = new Dictionary <string, object>() { { "data", "d" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts, options => { options.Formatters.AddSingle(new Func <string, string, string>((s, s1) => s1), "Self"); }); Assert.That(result, Is.EqualTo("(d(a))")); }
public async Task ParserCanChainFormat() { var template = "{{#SCOPE data}}{{ToString('d').fnc()}}{{/SCOPE}}"; var data = new Dictionary <string, object>() { { "data", DateTime.UtcNow } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts, options => { options.Formatters.AddSingle(new Func <string, string>(s => "TEST"), "fnc"); }); Assert.That(result, Is.EqualTo("TEST")); }
public async Task GenericsTest() { var template = "{{data.fod()}}"; var data = new Dictionary <string, object>() { { "data", new string[] { "TEST", "test" } } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts, options => { options.Formatters.AddFromType(typeof(StringFormatter)); }); Assert.That(result, Is.EqualTo("TEST")); }