public async Task TestStringConversion() { var template = "{{data.ExpectInt()}}"; var data = new Dictionary <string, object>() { { "data", 123 } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts, options => { options.Formatters.AddFromType(typeof(StringFormatter)); }); Assert.That(result, Is.EqualTo(123.ToString("X2"))); }
public async Task TemplateShouldProcessVariablesInInvertedGroup() { var data = new Dictionary <string, object> { { "not_here", false }, { "placeholder", "a placeholder value" } }; var template = "{{^SCOPE not_here}}{{../placeholder}}{{/SCOPE}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo("a placeholder value")); }
public async Task TemplateCanExecuteNestedIfs() { var template = @"{{#IF data}}SHOULD PRINT{{#IF alum}}!{{/IF}}{{#ELSE}}SHOULD NOT PRINT{{/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("SHOULD PRINT")); }
public async Task TemplateRendersWithScopeWithAliasPath() { var template = @"{{#SCOPE data AS test}}{{#SCOPE ~root AS rootTest}}{{test}},{{rootTest}}{{/SCOPE}}{{rootTest}}{{/SCOPE}}{{test}}"; var data = new Dictionary <string, object>() { { "data", "test" }, { "root", "tset" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(data["data"] + "," + data["root"])); }
public async Task TestEveryKeywordOnObject() { var template = "{{#each this.?}}{{Key}}:\"{{Value}}\"{{^IF $last}},{{/IF}}{{/each}}"; var data = new EveryObjectTest() { TestA = "Du", TestB = "Hast" }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo($"{nameof(EveryObjectTest.TestA)}:\"Du\"," + $"{nameof(EveryObjectTest.TestB)}:\"Hast\"," + $"{nameof(EveryObjectTest.ObjectTest)}:\"\"")); }
public async Task TemplateRendersWithComplexRootScopePathInIf() { var template = @"{{#IF data}}{{root}}{{/if}}"; var data = new Dictionary <string, object>() { { "data", "test" }, { "root", "tset" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(data["root"])); }
public async Task TemplateInvertedIfElseCombined() { var template = @"{{^IF data}}{{data}}{{#else}}{{root}}{{/else}}{{/IF}}"; var data = new Dictionary <string, object>() { { "data", "false" }, { "root", "true" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options | ParserOptionTypes.NoRerenderingTest); Assert.That(result, Is.EqualTo(data["root"])); }
public async Task TestCanAcceptExternalServiceFactory() { var template = "{{data.Formatter()}}"; var data = new Dictionary <string, object>() { { "data", 123 } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts, options => { options.Formatters.AddFromType(typeof(ExternalDataFormatter)); options.Formatters.Services.AddService(() => new ExternalDataFormatter.ExternalDataService()); }); Assert.That(result, Is.EqualTo("123|B6B747D4-02E4-4CBE-8CD2-013B64C1399A")); }
public async Task TestSingleNamed() { var template = "{{data.reverse()}}"; var data = new Dictionary <string, object>() { { "data", "Test" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts, options => { options.Formatters.AddFromType(typeof(StringFormatter)); }); Assert.That(result, Is.EqualTo("tseT")); }
public async Task TemplateIfDoesNotScope() { var template = @"{{#IF data}}{{this}}{{/IF}}"; var data = new Dictionary <string, object>() { { "data", "test" }, { "root", "tset" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(data.ToString())); }
public async Task ParserCanPrintNested() { var data = new Dictionary <string, object>(); //declare TestPartial -> Print Recursion -> If Recursion is smaller then 10 -> Print TestPartial //Print TestPartial var template = @"{{#declare TestPartial}}{{$recursion}}{{#SCOPE $recursion.Self() as rec}}{{#IMPORT 'TestPartial'}}{{/SCOPE}}{{/declare}}{{#IMPORT 'TestPartial'}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, options => { options.Formatters.AddSingle <int, bool>(e => { return(e < 9); }, "Self"); }); Assert.That(result, Is.EqualTo("123456789")); }
public async Task FormatterCanFormatObjectTwice() { var template = "{{Plus(B, B)}}"; var data = new Dictionary <string, object>() { { "A", 5 }, { "B", 6 } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts, options => { options.Formatters.AddFromType(typeof(StringFormatter)); }); Assert.That(result, Is.EqualTo("12")); }
public async Task ParserCanFormatMultipleUnnamedParams() { var template = "{{#SCOPE data}}{{Self( 'arg', 'arg, arg', ' spaced ', [testArgument]'test', ' 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 Func <int, string, object[], string>(UnnamedParamsFormatter), "Self"); }); Assert.That(result, Is.EqualTo("123123123|test|arg|arg, arg| spaced | spaced with quote ' |123123123")); }
public async Task ParserCanPartialsOneUp() { var data = new Dictionary <string, object>(); data["Data"] = new List <object> { new Dictionary <string, object> { { "self", new Dictionary <string, object> { { "Test", 1 } } } }, new Dictionary <string, object> { { "self", new Dictionary <string, object> { { "Test", 2 } } } } }; data["DataOneUp"] = new Dictionary <string, object> { { "self", new Dictionary <string, object> { { "Test", "Is:" } } } }; var template = "{{#DECLARE TestPartial}}{{../../DataOneUp.self.Test}}{{self.Test}}{{/DECLARE}}" + "{{#EACH Data}}" + " {{#IMPORT 'TestPartial'}}"+ "{{/EACH}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo(" Is:1 Is:2")); }
public async Task TestNullCoalescingOperatorFromParaValues() { var template = "{{((A) ?? (B))}}"; var data = new Dictionary <string, object>() { { "A", "VALA" }, { "B", "VALB" }, }; Assert.That(await ParserFixture.CreateAndParseWithOptions(template, data, _opts), Is.EqualTo(data["A"])); data = new Dictionary <string, object>() { { "A", null }, { "B", "VALB" }, }; Assert.That(await ParserFixture.CreateAndParseWithOptions(template, data, _opts), Is.EqualTo(data["B"])); }
public async Task ParserCanLoadFileStore() { var tempPath = Path.Combine(Path.GetTempPath(), "MorestachioTesting", Environment.Version.ToString(), _options.GetHashCode().ToString()); Directory.CreateDirectory(tempPath); var data = new Dictionary <string, object>() { { "data", new Dictionary <string, object>() { { "name", "Bond" } } } }; try { File.WriteAllText(Path.Combine(tempPath, "content.html"), "Hello World", ParserFixture.DefaultEncoding); File.WriteAllText(Path.Combine(tempPath, "instruction.html"), "Hello mr {{data.name}}", ParserFixture.DefaultEncoding); Directory.CreateDirectory(Path.Combine(tempPath, "sub")); File.WriteAllText(Path.Combine(tempPath, "sub", "base.html"), "Sub Path", ParserFixture.DefaultEncoding); var template = @"Blank {{#IMPORT 'File/content'}} {{#IMPORT 'File/instruction'}} {{#IMPORT 'File/base'}} "; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, options => { options.PartialsStore = new FileSystemPartialStore(tempPath, "*.html", true, true, "File/"); }); Assert.That(result, Is.EqualTo(@"Blank Hello World Hello mr Bond Sub Path ")); } finally { Directory.Delete(tempPath, true); } }
public async Task ParserCanIncludePartialsWithExplicitScopeFromFormatter() { var data = new Dictionary <string, object>(); data["Data"] = new List <object> { new Dictionary <string, object> { { "self", new Dictionary <string, object> { { "Test", 1 } } } }, new Dictionary <string, object> { { "self", new Dictionary <string, object> { { "Test", 2 } } } }, }; var template = @"{{#DECLARE TestPartial}}{{ExportedValue.ElementAt(1).self.Test}}{{/DECLARE}}{{#IMPORT 'TestPartial' #WITH Data.Self($name)}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, options => { options.Formatters.AddFromType(typeof(DynamicLinq)); options.Formatters.AddSingle(new Func <object, string, object>((sourceObject, name) => { return(new Dictionary <string, object>() { { "ExportedValue", sourceObject }, { "XNAME", name } }); }), "Self"); }); Assert.That(result, Is.EqualTo("2")); }
public async Task TestCanLocalizeData() { var data = new Dictionary <string, object>() { { "testKey", "test" } }; var translationResult = "TestFixture"; var template = "{{#LOC testKey}}"; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, parserOptions => { parserOptions.RegisterLocalizationService(() => { return(new MorestachioLocalizationService() .AddResource(new MemoryTranslationResource() .Add("test", CultureInfo.GetCultureInfo("EN-US"), translationResult + " en-US") .Add("test", CultureInfo.GetCultureInfo("DE-DE"), translationResult + " de-DE")) .Load(new[] { CultureInfo.GetCultureInfo("EN-US"), CultureInfo.GetCultureInfo("DE-DE") })); }); parserOptions.CultureInfo = CultureInfo.GetCultureInfo("EN-US"); }); Assert.That(result, Is.EqualTo(translationResult + " en-US")); result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, parserOptions => { parserOptions.RegisterLocalizationService(() => { return(new MorestachioLocalizationService() .AddResource(new MemoryTranslationResource() .Add("test", CultureInfo.GetCultureInfo("EN-US"), translationResult + " en-US") .Add("test", CultureInfo.GetCultureInfo("DE-DE"), translationResult + " de-DE")) .Load(new[] { CultureInfo.GetCultureInfo("EN-US"), CultureInfo.GetCultureInfo("DE-DE") })); }); parserOptions.CultureInfo = CultureInfo.GetCultureInfo("DE-DE"); }); Assert.That(result, Is.EqualTo(translationResult + " de-DE")); }
public async Task TemplateInvalidContentBetweenIfAndElse() { var template = @"{{^IF data}}{{data}}{{/IF}}{{data}}{{#else}}{{root}}{{/else}}"; var data = new Dictionary <string, object>() { { "data", "false" }, { "root", "true" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, null, info => { Assert.That(info.Errors .OfType <MorestachioSyntaxError>() .FirstOrDefault(e => e.Location.Equals(CharacterLocation.FromFormatString("1:38,38"))), Is.Not.Null); }); }
public async Task ParserCanCheckCanFormat() { 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, string>((s, inv, inva) => throw new Exception("A")), "Self"); options.Formatters.AddSingle(new Func <string, string>(s => throw new Exception("Wrong Ordering")), "Self"); options.Formatters.AddSingle(new Action <string>(s => throw new Exception("Wrong Return Ordering")), "Self"); options.Formatters.AddSingle(new Func <string, string, string>((s, inv) => inv), "Self"); });
public async Task ParserCanChainFormatWithLineBreak() { var template = @"{{#SCOPE data}}{{this . ToString ( 'd' ) . Self () }}{{/SCOPE}}"; var data = new Dictionary <string, object>() { { "data", DateTime.UtcNow } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts | ParserOptionTypes.NoRerenderingTest, options => { options.Formatters.AddSingle(new Func <string, string>(s => "TEST"), "Self"); }); Assert.That(result, Is.EqualTo("TEST")); }
public async Task TestCanTransformValueWithAttribute() { var template = "{{data.ReturnValueExplicitConverter()}}"; var data = new Dictionary <string, object>() { { "data", new CustomConverterFormatter.TestObject() { No = 123 } } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _opts, options => { options.Formatters.AddFromType(typeof(CustomConverterFormatter)); }); Assert.That(result, Is.EqualTo("123")); }
public async Task ParserCanPartials() { var template = @"{{#DECLARE TestPartial}}{{self.Test}}{{/DECLARE}}{{#EACH Data}}{{#IMPORT 'TestPartial'}}{{/EACH}}"; var data = new Dictionary <string, object>(); data["Data"] = new List <object> { new Dictionary <string, object> { { "self", new Dictionary <string, object> { { "Test", 1 } } } }, new Dictionary <string, object> { { "self", new Dictionary <string, object> { { "Test", 2 } } } }, new Dictionary <string, object> { { "self", new Dictionary <string, object> { { "Test", 3 } } } } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, options => { options.Formatters.AddFromType(typeof(ParserFixture.NumberFormatter)); }); Assert.That(result, Is.EqualTo("123")); }
public async Task TestCollectionFormatting() { var template = "{{#each data.OrderBy()}}{{this}},{{/each}}"; var collection = new[] { 0, 1, 2, 3, 5, 4, 6, 7 }; var data = new Dictionary <string, object> { { "data", collection } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, options => { options.Formatters.AddFromType(typeof(DynamicLinq)); }); Assert.That(result, Is.EqualTo(collection.OrderBy(e => e).Select(e => e.ToString()).Aggregate((e, f) => e + "," + f) + ",")); }
public async Task TestCollectionSpecialKeyFormatting() { var template = "{{#each data}}{{$index.PlusOne()}},{{/each}}"; var collection = new[] { 10, 11, 12, 14 }; var data = new Dictionary <string, object> { { "data", collection } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, options => { options.Formatters.AddSingle(new Func <long, long>((value) => value + 1), "PlusOne"); }); Assert.That(result, Is.EqualTo(Enumerable.Range(1, collection.Length).Select(e => e.ToString()).Aggregate((e, f) => e + "," + f) + ",")); }
public async Task TestCanChangeLocale() { var template = "{{#LOC 'Texts.Welcome'}} " + "{{#LOCCULTURE 'de-AT'}}" + "{{#LOC 'Texts.Welcome'}} " + "{{/LOCCULTURE}}" + "{{#LOC 'Texts.Welcome'}} " + "{{#LOCCULTURE 'de-DE'}}" + "{{#LOC 'Texts.Welcome'}} " + "{{#LOCCULTURE 'de-AT'}}" + "{{#LOC 'Texts.Welcome'}} " + "{{/LOCCULTURE}}" + "{{#LOC 'Texts.Welcome'}}" + "{{/LOCCULTURE}}"; var data = new Dictionary <string, object>(); var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options, parserOptions => { parserOptions.RegisterLocalizationService(() => { var morestachioLocalizationService = new MorestachioLocalizationService() .AddResource(new MemoryTranslationResource() .Add("Texts.Welcome", CultureInfo.GetCultureInfo("EN-US"), "Welcome") .Add("Texts.Welcome", CultureInfo.GetCultureInfo("DE-DE"), "Moin") .Add("Texts.Welcome", CultureInfo.GetCultureInfo("DE-AT"), "Grützli")) .Load(new[] { CultureInfo.GetCultureInfo("EN-US"), CultureInfo.GetCultureInfo("DE-DE"), CultureInfo.GetCultureInfo("DE-AT") }); return(morestachioLocalizationService); }); parserOptions.CultureInfo = CultureInfo.GetCultureInfo("EN-US"); }); Assert.That(result, Is.EqualTo("Welcome Grützli Welcome Moin Grützli Moin")); }
public void TestGetCurrent() { var morestachioDocumentInfo = GenerateTemplate("{{test}}content{{#if test}}data{{/if}}"); var api = morestachioDocumentInfo.Fluent(); api.Current(f => Assert.That(f, Is.TypeOf <MorestachioDocument>())) .FindNext <PathDocumentItem>() .IfNotSuccess(f => Assert.Fail("Could not find PathDocumentItem")) .Current(f => Assert.That(f, Is.TypeOf <PathDocumentItem>())) .FindNext <ContentDocumentItem>() .IfNotSuccess(f => Assert.Fail("Could not find ContentDocumentItem")) .Current(f => Assert.That(f, Is.TypeOf <ContentDocumentItem>())) .FindNext <IfExpressionScopeDocumentItem>() .IfNotSuccess(f => Assert.Fail("Could not find IfExpressionScopeDocumentItem")) .Current(f => Assert.That(f, Is.TypeOf <IfExpressionScopeDocumentItem>())) .FindNext <PathDocumentItem>() .IfSuccess(f => Assert.Fail("Found PathDocumentItem but should have not")) .Current(f => Assert.That(f, Is.TypeOf <IfExpressionScopeDocumentItem>())); ParserFixture.TestLocationsInOrder(morestachioDocumentInfo); }
public async Task TemplateCanRenderSwitchCaseWithScopeing() { var template = "{{#SWITCH data #SCOPE}}" + "{{#CASE 'tset'}}FAIL-{{this}}{{/CASE}}" + "{{#CASE 123}}FAIL-{{this}}{{/CASE}}" + "{{#CASE root}}FAIL-{{this}}{{/CASE}}" + "{{#CASE 'test'}}SUCCESS-{{this}}{{/CASE}}" + "{{/SWITCH}}"; var data = new Dictionary <string, object>() { { "data", "test" }, { "root", "tset" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo("SUCCESS-test")); }
public async Task TemplateCanRenderDefaultSwitchCase() { var template = @"{{#SWITCH data}} {{#CASE 'tset'}}FAIL{{/CASE}} {{#CASE 123}}FAIL{{/CASE}} {{#CASE root}}FAIL{{/CASE}} {{#DEFAULT}}SUCCESS{{/DEFAULT}} {{/SWITCH}}" ; var data = new Dictionary <string, object>() { { "data", "test" }, { "root", "tset" } }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options); Assert.That(result, Is.EqualTo("SUCCESS")); }
public async Task TemplateRendersWithEachWithAliasPath() { var template = @"{{#each data AS dd}}{{dd}}{{/each}}"; var value = new List <int>() { 1, 2, 3, 4, 5 }; var data = new Dictionary <string, object>() { { "data", value }, }; var result = await ParserFixture.CreateAndParseWithOptions(template, data, _options | ParserOptionTypes.NoRerenderingTest); Assert.That(result, Is.EqualTo(value.Select(e => e.ToString()).Aggregate((e, f) => e + "" + f))); }