public void LavaHelperRemoveComments_CommentsInIncludeFile_AreRemoved() { var fileProvider = GetFileProviderWithComments(); var input = @" {%- include '_comments.lava' -%} "; var expectedOutput = @" Line 1<br> Line 2<br> Line 3<br> Line 4<br> "; var options = new LavaEngineConfigurationOptions { FileSystem = GetFileProviderWithComments() }; TestHelper.ExecuteForActiveEngines((engine) => { // Create a new engine instance of the same type, but with a test file system configuration. var testEngine = LavaService.NewEngineInstance(engine.GetType(), options); TestHelper.AssertTemplateOutput(testEngine, expectedOutput, input); }); }
public void WebsiteLavaTemplateCacheService_WhitespaceTemplatesWithDifferentLengths_AreCachedIndependently() { var options = new LavaEngineConfigurationOptions(); ILavaTemplateCacheService cacheService = new WebsiteLavaTemplateCacheService(); options.CacheService = cacheService; TestHelper.ExecuteForActiveEngines((defaultEngineInstance) => { if (defaultEngineInstance.GetType() == typeof(RockLiquidEngine)) { Debug.Write("Template caching cannot be tested by this methodology for the RockLiquid implementation."); return; } // Remove all existing items from the cache. cacheService.ClearCache(); var engine = LavaService.NewEngineInstance(defaultEngineInstance.GetType(), options); // Process a zero-length whitespace template - this should be cached separately. var input0 = string.Empty; var key0 = cacheService.GetCacheKeyForTemplate(input0); // Verify that the template does not initially exist in the cache. var exists = cacheService.ContainsKey(key0); Assert.IsFalse(exists, "String-0 Template found in cache unexpectedly."); // Render the template, which will automatically add it to the cache. var output0 = engine.RenderTemplate(input0); // Verify that the template now exists in the cache. exists = cacheService.ContainsKey(key0); Assert.IsTrue(exists, "String-0 Template not found in cache."); // Render a whitespace template of a different length - this should be cached separately from the first template. // If not, the caching mechanism would cause some whitespace to be rendered incorrectly. var input1 = new string( ' ', 1 ); var key1 = engine.TemplateCacheService.GetCacheKeyForTemplate(input1); var output1 = engine.RenderTemplate(input1); // Verify that the 100-character whitespace template now exists in the cache. exists = cacheService.ContainsKey(key1); Assert.IsTrue(exists, "String-1 Template not found in cache."); // Verify that a whitespace template of some other length is not equated with the whitespace templates we have specifically added. var keyX = engine.TemplateCacheService.GetCacheKeyForTemplate(new string( ' ', 9 )); exists = cacheService.ContainsKey(keyX); Assert.IsFalse(exists, "String-9 Template found in cache unexpectedly."); }); }
public void FluidEngineValueConverterPerformance_DictionaryKeyResolution_OutputsPerformanceMeasures() { var totalSets = 10; var totalIterationsPerSet = 1000000; // Create a Fluid engine with template caching disabled. var engineOptions = new LavaEngineConfigurationOptions { CacheService = new MockTemplateCacheService() }; var engine = LavaService.NewEngineInstance(typeof(FluidEngine), engineOptions); var standardLavaDictionary = new Dictionary <string, object>(); var formatString = new string( '0', totalIterationsPerSet.ToString().Length ); for (int i = 0; i < totalIterationsPerSet; i++) { standardLavaDictionary.Add(i.ToString(formatString), i); } var mergeFields = new LavaDataDictionary { { "Dictionary", standardLavaDictionary } }; var totalTime = TestHelper.ExecuteAndGetElapsedTime(() => { engine.ClearTemplateCache(); for (int repeatCount = 1; repeatCount <= totalSets; repeatCount++) { var elapsedTime = TestHelper.ExecuteAndGetElapsedTime(() => { for (int i = 0; i < totalIterationsPerSet; i++) { var template = "{{ Dictionary['" + i.ToString(formatString) + "']}}"; var options = new LavaTestRenderOptions { MergeFields = mergeFields }; var output = TestHelper.GetTemplateOutput(engine, template, mergeFields); // Verify that the correct entry was retrieved. Assert.AreEqual(i.ToString(), output); } }); Debug.Print($"Pass {repeatCount} Elapsed Time: {elapsedTime.TotalMilliseconds} "); } }); Debug.Print($"Average Time: {totalTime.TotalMilliseconds / totalSets} "); }
private static void InitializeRockLiquidLibrary() { _ = LavaService.NewEngineInstance(typeof(RockLiquidEngine)); // Register the set of filters that are compatible with RockLiquid. Template.RegisterFilter(typeof(Rock.Lava.Filters.TemplateFilters)); Template.RegisterFilter(typeof(Rock.Lava.RockFilters)); // Initialize the RockLiquid file system. Template.FileSystem = new LavaFileSystem(); }
private static void AssertTemplateResult(string expected, string template) { // Tests in this class are only compatible with the DotLiquid engine. // If/when these tests are reworked for the Fluid engine, they should be moved to the Rock.Tests.UnitTests.Lava namespace. if (_lavaEngine == null) { _lavaEngine = LavaService.NewEngineInstance(typeof(global::Rock.Lava.DotLiquid.DotLiquidEngine), new LavaEngineConfigurationOptions()); } var result = _lavaEngine.RenderTemplate(template); Assert.That.AreEqual(expected, result.Text); }
public void IncludeStatement_ShouldRenderError_IfFileSystemIsNotConfigured() { var input = @" {% include '_template.lava' %} "; TestHelper.ExecuteForActiveEngines((engine) => { var testEngine = LavaService.NewEngineInstance(engine.GetType(), new LavaEngineConfigurationOptions()); var result = testEngine.RenderTemplate(input); Assert.That.Contains(result.Error.Messages().JoinStrings("//"), "File Load Failed."); }); }
public void Initialize(bool testRockLiquidEngine, bool testDotLiquidEngine, bool testFluidEngine) { // Verify the test environment: RockLiquidEngine and DotLiquidEngine are mutually exclusive test environments. if (testRockLiquidEngine && testDotLiquidEngine) { throw new Exception("RockLiquidEngine/DotLiquidEngine cannot be tested simultaneously because they require different global configurations of the DotLiquid library."); } RockLiquidEngineIsEnabled = testRockLiquidEngine; DotLiquidEngineIsEnabled = testDotLiquidEngine; FluidEngineIsEnabled = testFluidEngine; RegisterLavaEngines(); if (RockLiquidEngineIsEnabled) { // Initialize the Rock variant of the DotLiquid Engine var engineOptions = new LavaEngineConfigurationOptions(); _rockliquidEngine = LavaService.NewEngineInstance(typeof(RockLiquidEngine), engineOptions); // Register the common Rock.Lava filters first, then overwrite with the web-based RockFilters as needed. RegisterFilters(_rockliquidEngine); } if (DotLiquidEngineIsEnabled) { // Initialize the DotLiquid Engine var engineOptions = new LavaEngineConfigurationOptions(); _dotliquidEngine = LavaService.NewEngineInstance(typeof(DotLiquidEngine), engineOptions); RegisterFilters(_dotliquidEngine); } if (FluidEngineIsEnabled) { // Initialize Fluid Engine var engineOptions = new LavaEngineConfigurationOptions(); _fluidEngine = LavaService.NewEngineInstance(typeof(FluidEngine), engineOptions); RegisterFilters(_fluidEngine); } }
public void IncludeStatement_ModifyingLocalVariableSameAsOuterVariable_ModifiesOuterVariable() { var fileSystem = GetMockFileProvider(); var input = @" {% assign a = 'a' %} Outer 'a' = {{ a }} {% include '_assign.lava' %} Outer 'a' = {{ a }} "; if (LavaIntegrationTestHelper.DotLiquidEngineIsEnabled) { var expectedOutputLiquid = @" Outer 'a' = a Included 'a' = b Outer 'a' = b "; var testEngineDotLiquid = LavaService.NewEngineInstance(typeof(DotLiquidEngine), new LavaEngineConfigurationOptions { FileSystem = fileSystem }); TestHelper.AssertTemplateOutput(testEngineDotLiquid, expectedOutputLiquid, input); } if (LavaIntegrationTestHelper.FluidEngineIsEnabled) { // The behavior in Fluid is different from standard Liquid. // The include file maintains a local scope for new variables. var expectedOutputFluid = @" Outer 'a' = a Included 'a' = b Outer 'a' = a "; var testEngineFluid = LavaService.NewEngineInstance(typeof(FluidEngine), new LavaEngineConfigurationOptions { FileSystem = fileSystem }); TestHelper.AssertTemplateOutput(testEngineFluid, expectedOutputFluid, input); } }
public void Configuration_SetDefaultEntityCommandExecute_IsEnabledForNewDefaultContext() { var options = new LavaEngineConfigurationOptions { DefaultEnabledCommands = new List <string> { "Execute" } }; TestHelper.ExecuteForActiveEngines((engine) => { var testEngine = LavaService.NewEngineInstance(engine.GetType(), options); var context = testEngine.NewRenderContext(); var enabledCommands = context.GetEnabledCommands(); Assert.That.Contains(enabledCommands, "Execute"); }); }
public void IncludeStatement_ForFileContainingMergeFields_ReturnsMergedOutput() { var fileSystem = GetMockFileProvider(); var input = @" Name: Ted Decker ** Contact {% include '_contact.lava' %} ** "; var expectedOutput = @" Name: Ted Decker ** Contact Mobile: (623) 555-3323 Home: (623) 555-3322 Work : (623) 555-2444 Email: [email protected] ** "; var mergeValues = new LavaDataDictionary { { "mobilePhone", "(623) 555-3323" }, { "homePhone", "(623) 555-3322" }, { "workPhone", "(623) 555-2444" }, { "email", "*****@*****.**" } }; var options = new LavaTestRenderOptions { MergeFields = mergeValues }; TestHelper.ExecuteForActiveEngines((engine) => { // Create a new engine instance of the same type, but with a test file system configuration. var testEngine = LavaService.NewEngineInstance(engine.GetType(), new LavaEngineConfigurationOptions { FileSystem = fileSystem }); TestHelper.AssertTemplateOutput(testEngine, expectedOutput, input, options); }); }
public void IncludeStatement_ForNonexistentFile_ShouldRenderError() { var fileSystem = GetMockFileProvider(); var input = @" {% include '_unknown.lava' %} "; TestHelper.ExecuteForActiveEngines((engine) => { var testEngine = LavaService.NewEngineInstance(engine.GetType(), new LavaEngineConfigurationOptions { FileSystem = fileSystem }); var result = testEngine.RenderTemplate(input, new LavaRenderParameters { ExceptionHandlingStrategy = ExceptionHandlingStrategySpecifier.RenderToOutput }); TestHelper.DebugWriteRenderResult(engine, input, result.Text); Assert.That.Contains(result.Error.Messages().JoinStrings("//"), "File Load Failed."); }); }
public void WebsiteLavaShortcodeProvider_ModifiedShortcode_ReturnsCorrectVersionAfterModification() { var options = new LavaEngineConfigurationOptions(); ILavaTemplateCacheService cacheService = new WebsiteLavaTemplateCacheService(); options.CacheService = cacheService; TestHelper.ExecuteForActiveEngines((defaultEngineInstance) => { if (defaultEngineInstance.GetType() == typeof(DotLiquidEngine) || defaultEngineInstance.GetType() == typeof(RockLiquidEngine)) { Debug.Write("Shortcode caching is not currently implemented for RockLiquid/DotLiquid."); return; } var engine = LavaService.NewEngineInstance(defaultEngineInstance.GetType(), options); var shortcodeProvider = new TestLavaDynamicShortcodeProvider(); var rockContext = new RockContext(); var lavaShortCodeService = new LavaShortcodeService(rockContext); // Create a new Shortcode. var shortcodeGuid1 = TestGuids.Shortcodes.ShortcodeTest1.AsGuid(); var lavaShortcode = lavaShortCodeService.Queryable().FirstOrDefault(x => x.Guid == shortcodeGuid1); if (lavaShortcode == null) { lavaShortcode = new LavaShortcode(); lavaShortCodeService.Add(lavaShortcode); } lavaShortcode.Guid = shortcodeGuid1; lavaShortcode.TagName = "TestShortcode1"; lavaShortcode.Name = "Test Shortcode 1"; lavaShortcode.IsActive = true; lavaShortcode.Description = "Test shortcode"; lavaShortcode.TagType = TagType.Inline; lavaShortcode.Markup = "Hello!"; rockContext.SaveChanges(); shortcodeProvider.RegisterShortcode(engine, lavaShortcode); // Resolve a template using the new shortcode and verify the result. engine.ClearTemplateCache(); shortcodeProvider.ClearCache(); LavaService.SetCurrentEngine(engine); TestHelper.AssertTemplateOutput(engine, "Hello!", "{[ TestShortcode1 ]}"); lavaShortcode.Markup = "Goodbye!"; rockContext.SaveChanges(); shortcodeProvider.ClearCache(); engine.ClearTemplateCache(); TestHelper.AssertTemplateOutput(engine, "Goodbye!", "{[ TestShortcode1 ]}"); }); }