From faa58feba4608c6c429b0725374b146245dae828 Mon Sep 17 00:00:00 2001 From: Connie Yau Date: Wed, 28 Jun 2017 18:34:04 -0700 Subject: [PATCH 1/2] Replacing TemplateService with RazorEngineService.Create() --- .../ActivatorTestFixture.cs | 6 +- .../CodeInspectorTestFixture.cs | 4 +- .../ConfigurationTestFixture.cs | 33 +++--- .../Issues/Release_3_0_TestFixture.cs | 75 +++++++++---- .../Issues/Release_3_6_TestFixture.cs | 69 ++++++++---- .../RazorEngineHostTestFixture.cs | 20 +++- .../TemplateBaseTestFixture.cs | 100 +++++++++++++----- 7 files changed, 212 insertions(+), 95 deletions(-) diff --git a/src/test/Test.RazorEngine.Core/ActivatorTestFixture.cs b/src/test/Test.RazorEngine.Core/ActivatorTestFixture.cs index 39f3f012..7975d215 100644 --- a/src/test/Test.RazorEngine.Core/ActivatorTestFixture.cs +++ b/src/test/Test.RazorEngine.Core/ActivatorTestFixture.cs @@ -46,15 +46,13 @@ public void TemplateService_CanSupportCustomActivator_WithUnity() BaseTemplateType = typeof(CustomTemplateBase<>) }; -#pragma warning disable 0618 // Fine because we still want to test if - using (var service = new TemplateService(config)) -#pragma warning restore 0618 // Fine because we still want to test if + using (var service = RazorEngineService.Create(config)) { const string template = "

Hello @Format(Model.Forename)

"; const string expected = "

Hello ttaM

"; var model = new Person { Forename = "Matt" }; - string result = service.Parse(template, model, null, null); + string result = service.RunCompile(templateSource: template, name: "template", model: model); Assert.That(result == expected, "Result does not match expected: " + result); } diff --git a/src/test/Test.RazorEngine.Core/CodeInspectorTestFixture.cs b/src/test/Test.RazorEngine.Core/CodeInspectorTestFixture.cs index 217b6289..d6905328 100644 --- a/src/test/Test.RazorEngine.Core/CodeInspectorTestFixture.cs +++ b/src/test/Test.RazorEngine.Core/CodeInspectorTestFixture.cs @@ -27,11 +27,11 @@ public void CodeInspector_SupportsAddingCustomInspector() var config = new TemplateServiceConfiguration(); config.CodeInspectors.Add(new ThrowExceptionCodeInspector()); - using (var service = new TemplateService(config)) + using (var service = RazorEngineService.Create(config)) { const string template = "Hello World"; - Assert.Throws(() => service.Parse(template, null, null, null)); + Assert.Throws(() => service.RunCompile(templateSource: template, name: "template")); } } #endregion diff --git a/src/test/Test.RazorEngine.Core/ConfigurationTestFixture.cs b/src/test/Test.RazorEngine.Core/ConfigurationTestFixture.cs index b82164c8..0be9e640 100644 --- a/src/test/Test.RazorEngine.Core/ConfigurationTestFixture.cs +++ b/src/test/Test.RazorEngine.Core/ConfigurationTestFixture.cs @@ -38,15 +38,19 @@ public void FluentTemplateServiceConfiguration_CanConfigureTemplateService_WithA { var config = new FluentTemplateServiceConfiguration( c => c.IncludeNamespaces("System.IO")); -#pragma warning disable 0618 // TODO: Update test. - using (var service = new TemplateService(config)) -#pragma warning restore 0618 // TODO: Update test. + using (var service = RazorEngineService.Create(config)) + using (var writer = new StringWriter()) { const string template = @"@Directory.GetFiles(Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Personal)), ""*.*"").Length"; int expected = Directory.GetFiles(Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Personal)), "*.*").Length; - string result = service.Parse(template, null, null, null); + var key = service.GetKey("testTemplate"); + var source = new LoadedTemplateSource(template); + service.AddTemplate(key, source); + service.Compile(key); + service.Run(key, writer); + var result = writer.ToString(); Assert.AreEqual(expected.ToString(), result); } } @@ -74,14 +78,12 @@ public void FluentTemplateServiceConfiguration_CanConfigureTemplateService_WithS var config = new FluentTemplateServiceConfiguration( c => c.WithCodeLanguage(Language.VisualBasic)); -#pragma warning disable 0618 // TODO: Update test. - using (var service = new TemplateService(config)) -#pragma warning restore 0618 // TODO: Update test. + using (var service = RazorEngineService.Create(config)) { const string template = "@Code Dim name As String = \"Matt\" End Code\n@name"; const string expected = "\nMatt"; - string result = service.Parse(template, null, null, null); + string result = service.RunCompile(templateSource: template, name: "template"); Assert.That(result == expected, "Result does not match expected: " + result); } @@ -97,17 +99,22 @@ public void FluentTemplateServiceConfiguration_CanConfigureTemplateService_WithS var config = new FluentTemplateServiceConfiguration( c => c.WithEncoding(Encoding.Raw)); -#pragma warning disable 0618 // TODO: Update test. - using (var service = new TemplateService(config)) -#pragma warning restore 0618 // TODO: Update test. + using (var service = RazorEngineService.Create(config)) + using (var writer = new StringWriter()) { const string template = "

Hello @Model.String

"; const string expected = "

Hello Matt & World

"; var model = new { String = "Matt & World" }; - string result = service.Parse(template, model, null, null); + var source = new LoadedTemplateSource(template); + var key = service.GetKey("testTemplate"); - Assert.That(result == expected, "Result does not match expected: " + result); + service.Compile(source, key); + service.Run(key, writer, model: model); + + var contents = writer.ToString(); + + Assert.AreEqual(expected, contents); } } diff --git a/src/test/Test.RazorEngine.Core/Issues/Release_3_0_TestFixture.cs b/src/test/Test.RazorEngine.Core/Issues/Release_3_0_TestFixture.cs index 92dffc07..a434a702 100644 --- a/src/test/Test.RazorEngine.Core/Issues/Release_3_0_TestFixture.cs +++ b/src/test/Test.RazorEngine.Core/Issues/Release_3_0_TestFixture.cs @@ -1,6 +1,7 @@ namespace RazorEngine.Tests.TestTypes.Issues { using System; + using System.IO; using Microsoft.CSharp.RuntimeBinder; using NUnit.Framework; @@ -12,7 +13,6 @@ /// Provides tests for the Release 3.0 /// [TestFixture] - [Obsolete("Needs to be updated to the new API")] public class Release_3_0_TestFixture { #region Tests @@ -24,7 +24,7 @@ public class Release_3_0_TestFixture [Test] public void Issue6_ModelShouldBePassedToLayout() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) { const string layoutTemplate = "

@Model.PageTitle

@RenderSection(\"Child\")"; const string childTemplate = "@{ Layout = \"Parent\"; }@section Child {

@Model.PageDescription

}"; @@ -35,11 +35,13 @@ public void Issue6_ModelShouldBePassedToLayout() PageDescription = "Test Page Description" }; - var type = model.GetType(); + var key = service.GetKey("Parent"); + var childKey = service.GetKey("Child"); - service.Compile(layoutTemplate, type, "Parent"); + service.AddTemplate(key, layoutTemplate); + service.Compile(key); - string result = service.Parse(childTemplate, model, null, null); + string result = service.RunCompile(templateSource: childTemplate, key: childKey, model: model); Assert.That(result == expected, "Result does not match expected: " + result); } @@ -54,14 +56,22 @@ public void Issue6_ModelShouldBePassedToLayout() [Test] public void Issue7_ViewBagShouldPersistThroughLayout() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) + using (var writer = new StringWriter()) { const string layoutTemplate = "

@ViewBag.Title

@RenderSection(\"Child\")"; const string childTemplate = "@{ Layout = \"Parent\"; ViewBag.Title = \"Test\"; }@section Child {}"; - service.Compile(layoutTemplate, null, "Parent"); + var key = service.GetKey("Parent"); + var childKey = service.GetKey(nameof(childTemplate)); - string result = service.Parse(childTemplate, null, null, null); + service.AddTemplate(key, layoutTemplate); + service.AddTemplate(childKey, childTemplate); + + service.Compile(key); + service.Compile(childKey); + service.Run(childKey, writer); + string result = writer.ToString(); Assert.That(result.StartsWith("

Test

")); } @@ -134,11 +144,14 @@ public void Issue7_ViewBagShouldNotPersistThroughInclude_UsingVB() [Test] public void Issue11_TemplateServiceShouldCompileModellessTemplate() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) { const string template = "

Hello World

"; - service.Compile(template, null, "issue11"); + var key = service.GetKey(nameof(template)); + var result = service.RunCompile(templateSource: template, key: key); + + Assert.AreEqual(template, result); } } @@ -151,13 +164,14 @@ public void Issue11_TemplateServiceShouldCompileModellessTemplate() [Test] public void Issue16_LastNullValueShouldReturnEmptyString() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) { const string template = "

Hello @Model.Person.Forename

"; const string expected = "

Hello

"; var model = new { Person = new Person { Forename = null } }; - string result = service.Parse(template, model, null, null); + var key = service.GetKey(nameof(template)); + var result = service.RunCompile(templateSource: template, key: key, model: model); Assert.That(result == expected, "Result does not match expected: " + result); } @@ -171,15 +185,20 @@ public void Issue16_LastNullValueShouldReturnEmptyString() [Test] public void TemplateService_ShouldAllowTypeOverrideForNonGenericCompile() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) + using (var writer = new StringWriter()) { const string template = "@Model.Name"; const string expected = "Matt"; object model = new { Name = "Matt" }; - Type modelType = model.GetType(); - string result = service.Parse(template, model, null, null); + var key = service.GetKey(nameof(template)); + + service.AddTemplate(key, template); + service.RunCompile(key, writer, model: model); + + string result = writer.ToString(); Assert.That(result == expected, "Result does not match expected: " + result); } @@ -194,14 +213,19 @@ public void TemplateService_ShouldAllowTypeOverrideForNonGenericCompile() [Test] public void TemplateService_ShouldEnableNullableValueTypes() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) + using (var writer = new StringWriter()) { const string template = "

Hello @Model.Number

"; const string expected = "

Hello

"; var model = new { Number = (int?)null }; - string result = service.Parse(template, model, null, null); + var key = service.GetKey(nameof(template)); + service.AddTemplate(key, template); + service.RunCompile(key, writer, model: model); + + string result = writer.ToString(); Assert.That(result == expected, "Result does not match expected: " + result); } } @@ -214,10 +238,15 @@ public void TemplateService_ShouldEnableNullableValueTypes() [Test] public void Issue21_SubclassModelShouldBeSupportedInLayout() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) + using (var writer = new StringWriter()) { - const string parent = "@model RazorEngine.Tests.TestTypes.Person\n

@Model.Forename

@RenderSection(\"Child\")"; - service.Compile(parent, null, "Parent"); + const string Parent = "@model RazorEngine.Tests.TestTypes.Person\n

@Model.Forename

@RenderSection(\"Child\")"; + + var key = service.GetKey(nameof(Parent)); + + service.AddTemplate(key, Parent); + service.Compile(key); const string child = "@{ Layout = \"Parent\"; }\n@section Child {

@Model.Department

}"; const string expected = "

Matt

IT

"; @@ -230,8 +259,12 @@ public void Issue21_SubclassModelShouldBeSupportedInLayout() Surname = "Abbott" }; - string result = service.Parse(child, model, null, null); + var childKey = service.GetKey(nameof(child)); + + service.AddTemplate(childKey, child); + service.RunCompile(childKey, writer, model.GetType(), model: model); + string result = writer.ToString(); Assert.That(result == expected, "Result does not match expected: " + result); } } diff --git a/src/test/Test.RazorEngine.Core/Issues/Release_3_6_TestFixture.cs b/src/test/Test.RazorEngine.Core/Issues/Release_3_6_TestFixture.cs index b1203069..f6126230 100644 --- a/src/test/Test.RazorEngine.Core/Issues/Release_3_6_TestFixture.cs +++ b/src/test/Test.RazorEngine.Core/Issues/Release_3_6_TestFixture.cs @@ -1,22 +1,18 @@ namespace RazorEngine.Tests.TestTypes.Issues { - using System; + using System.IO; using System.Linq; using System.Collections.Generic; - using Microsoft.CSharp.RuntimeBinder; using NUnit.Framework; - using Configuration; using Templating; - using System.Dynamic; using Test.RazorEngine; /// /// Provides tests for the Release 3.6 /// [TestFixture] - [Obsolete("Needs to be updated to RazorEngineService")] public class Release_3_6_TestFixture { /// @@ -114,7 +110,7 @@ public void Issue67_CollectionOfAnonymous() [Test] public void Issue181_RawDoesntWork() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) { const string link = "http://home.mysite.com/?a=b&c=1&new=1&d=3&e=0&g=1"; const string template_1 = "@Raw(Model.Data)"; @@ -123,10 +119,16 @@ public void Issue181_RawDoesntWork() string expected_2 = string.Format("{0}", link); var model = new { Data = link }; + var template1Key = service.GetKey(nameof(template_1)); + var template2Key = service.GetKey(nameof(template_2)); + + service.AddTemplate(template1Key, template_1); + service.AddTemplate(template2Key, template_2); + + var result_1 = service.RunCompile(template1Key, model: model); + var result_2 = service.RunCompile(template2Key, model: model); - var result_1 = service.Parse(template_1, model, null, null); Assert.AreEqual(expected_1, result_1); - var result_2 = service.Parse(template_2, model, null, null); Assert.AreEqual(expected_2, result_2); } } @@ -141,7 +143,7 @@ public void Issue181_RawDoesntWork() [Test] public void Issue93_SectionParsing() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) { string parent = @"@RenderSection(""MySection"", false)"; string section_test_1 = "@{ Layout = \"ParentLayout\"; }@section MySection { My section content }"; @@ -150,11 +152,12 @@ public void Issue93_SectionParsing() string expected_1 = " My section content "; string expected_2 = "\nMy section content\n"; string expected_3 = "\nMy section content\na"; - service.Compile(parent, null, "ParentLayout"); - - var result_1 = service.Parse(section_test_1, null, null, null); - var result_2 = service.Parse(section_test_2, null, null, null); - var result_3 = service.Parse(section_test_3, null, null, null); + var parentKey = service.GetKey("ParentLayout"); + service.AddTemplate(parentKey, parent); + + var result_1 = service.RunCompile(section_test_1, nameof(section_test_1)); + var result_2 = service.RunCompile(section_test_2, nameof(section_test_2)); + var result_3 = service.RunCompile(section_test_3, nameof(section_test_3)); Assert.AreEqual(expected_1, result_1); Assert.AreEqual(expected_2, result_2); @@ -171,27 +174,51 @@ public void Issue93_SectionParsing() [Test] public void Issue163_SectionRedefinition() { - TemplateServiceTestFixture.RunTestHelper(service => + RazorEngineServiceTestFixture.RunTestHelper(service => { string parentLayoutTemplate = @"@RenderSection(""Scripts"", false)"; string childLayoutTemplate = @"@{ Layout = ""ParentLayout""; }@section Scripts {@RenderSection(""Scripts"", false)}"; - service.Compile(parentLayoutTemplate, null, "ParentLayout"); - service.Compile(childLayoutTemplate, null, "ChildLayout"); + + var parentKey = service.GetKey("ParentLayout"); + var childKey = service.GetKey("ChildLayout"); + + service.AddTemplate(parentKey, parentLayoutTemplate); + service.AddTemplate(childKey, childLayoutTemplate); + + service.Compile(parentKey); + service.Compile(childKey); // Page with no section defined (e.g. page has no own scripts) string pageWithoutOwnScriptsTemplate = @"@{ Layout = ""ChildLayout""; }"; string expectedPageWithoutOwnScriptsResult = @""; - string actualPageWithoutOwnScriptsResult = service.Parse(pageWithoutOwnScriptsTemplate, null, null, null); - Assert.AreEqual(expectedPageWithoutOwnScriptsResult, actualPageWithoutOwnScriptsResult); + + using (var writer = new StringWriter()) + { + var pageKey = service.GetKey(nameof(pageWithoutOwnScriptsTemplate)); + + service.AddTemplate(pageKey, pageWithoutOwnScriptsTemplate); + string actualPageWithoutOwnScriptsResult = service.RunCompile(pageKey); + + Assert.AreEqual(expectedPageWithoutOwnScriptsResult, actualPageWithoutOwnScriptsResult); + } // Page with section redefenition (page has own additional scripts) string pageWithOwnScriptsTemplate = @"@{ Layout = ""ChildLayout""; }@section Scripts {}"; string expectedPageWithOwnScriptsResult = @""; - string actualPageWithOwnScriptsResult = service.Parse(pageWithOwnScriptsTemplate, null, null, null); - Assert.AreEqual(expectedPageWithOwnScriptsResult, actualPageWithOwnScriptsResult); + + using (var writer = new StringWriter()) + { + var pageKey2 = service.GetKey(nameof(pageWithOwnScriptsTemplate)); + + service.AddTemplate(pageKey2, pageWithOwnScriptsTemplate); + service.RunCompile(pageKey2, writer); + + string actualPageWithOwnScriptsResult = writer.ToString(); + Assert.AreEqual(expectedPageWithOwnScriptsResult, actualPageWithOwnScriptsResult); + } }); } diff --git a/src/test/Test.RazorEngine.Core/RazorEngineHostTestFixture.cs b/src/test/Test.RazorEngine.Core/RazorEngineHostTestFixture.cs index 10a97b66..7d0a51cb 100644 --- a/src/test/Test.RazorEngine.Core/RazorEngineHostTestFixture.cs +++ b/src/test/Test.RazorEngine.Core/RazorEngineHostTestFixture.cs @@ -4,6 +4,7 @@ namespace RazorEngine.Tests { using System.Collections.Generic; using System.Dynamic; + using System.IO; using System.Linq; using NUnit.Framework; @@ -18,7 +19,6 @@ namespace RazorEngine.Tests /// Defines a test fixture that provides tests for the type. /// [TestFixture] - [System.Obsolete("Needs to be updated to RazorEngineService")] public class RazorEngineHostTestFixture { #region Tests @@ -33,13 +33,19 @@ public class RazorEngineHostTestFixture [Test] public void RazorEngineHost_SupportsModelSpan_UsingCSharpCodeParser() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) + using (var writer = new StringWriter()) { const string template = "@model List\n@Model.Count"; const string expected = "1"; var model = new List { new Person() { Forename = "Matt", Age = 27 } }; - string result = service.Parse(template, (object)model, null, null); + var key = service.GetKey(nameof(template)); + + service.AddTemplate(key, template); + service.RunCompile(key, writer, modelType: model.GetType(), model: model); + + string result = writer.ToString(); Assert.That(result == expected, "Result does not match expected: " + result); } @@ -63,13 +69,17 @@ public void RazorEngineHost_SupportsModelSpan_UsingVBCodeParser() Language = Language.VisualBasic }; - using (var service = new TemplateService(config)) + using (var service = RazorEngineService.Create(config)) { const string template = "@ModelType List(Of RazorEngine.Tests.TestTypes.Person)\n@Model.Count"; const string expected = "1"; var model = new List { new Person() { Forename = "Matt", Age = 27 } }; - string result = service.Parse(template, (object)model, null, null); + var key = service.GetKey("template"); + + service.AddTemplate(key, new LoadedTemplateSource(template)); + + string result = service.RunCompile(key, modelType: typeof(object), model: model); Assert.That(result == expected, "Result does not match expected: " + result); } diff --git a/src/test/Test.RazorEngine.Core/TemplateBaseTestFixture.cs b/src/test/Test.RazorEngine.Core/TemplateBaseTestFixture.cs index d8ebdac0..6f9faa1f 100644 --- a/src/test/Test.RazorEngine.Core/TemplateBaseTestFixture.cs +++ b/src/test/Test.RazorEngine.Core/TemplateBaseTestFixture.cs @@ -2,6 +2,7 @@ { using System.Collections.Generic; using System.Dynamic; + using System.IO; using System.Linq; using NUnit.Framework; @@ -15,7 +16,6 @@ /// Defines a test fixture that provides tests for the type. /// [TestFixture] - [System.Obsolete("Needs to be updated/replaced with RazorEngineService.")] public class TemplateBaseTestFixture { #region Tests @@ -29,15 +29,21 @@ public class TemplateBaseTestFixture [Test] public void TemplateBase_CanUseRawOutput_WithHtmlEncoding() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) + using (var writer = new StringWriter()) { const string template = "

Hello @Raw(Model.Name)

"; const string expected = "

Hello <

"; var model = new { Name = "<" }; - string result = service.Parse(template, model, null, null); + var key = service.GetKey("test"); - Assert.That(result == expected, "Result does not match expected: " + result); + service.AddTemplate(key, template); + service.RunCompile(key, writer, model: model); + + string result = writer.ToString(); + + Assert.AreEqual(expected, result); } } @@ -47,7 +53,8 @@ public void TemplateBase_CanUseRawOutput_WithHtmlEncoding() [Test] public void TemplateBase_CanRenderWithLayout_WithSimpleLayout() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) + using (var writer = new StringWriter()) { const string parent = @"
@RenderSection(""TestSection"")
@RenderBody()"; const string template = @"@{Layout = ""Parent"";}@section TestSection {Hello}

Hello World

"; @@ -55,10 +62,20 @@ public void TemplateBase_CanRenderWithLayout_WithSimpleLayout() /* GetTemplate is the simplest method for compiling and caching a template without using a * resolver to locate the layout template at a later time in exection. */ - service.GetTemplate(parent, null, "Parent"); - string result = service.Parse(template, null, null, null); + var key = service.GetKey("Parent"); + var key2 = service.GetKey("Child"); + var parentTemplate = new LoadedTemplateSource(parent); + var childTemplate = new LoadedTemplateSource(template); - Assert.That(result == expected, "Result does not match expected: " + result); + service.AddTemplate(key, parentTemplate); + service.AddTemplate(key2, childTemplate); + service.Compile(key); + service.Compile(key2); + + service.Run(key2, writer); + string result = writer.ToString(); + + Assert.AreEqual(expected, result); } } @@ -104,16 +121,19 @@ public void TemplateBase_CanRenderWithLayout_WithSimpleLayout() [Test] public void TemplateBase_CanRenderWithLayout_WithComplexLayout() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) { - const string grandparent = @"
Message from Child Template (section): @RenderSection(""ChildMessage"")
Message from Parent Template (section): @RenderSection(""ParentMessage"")
Content from Parent Template (body): @RenderBody()
"; - const string parent = @"@{Layout = ""GrandParent"";}@section ParentMessage {Hello from Parent}

Child content: @RenderBody()

"; + const string GrandParent = @"
Message from Child Template (section): @RenderSection(""ChildMessage"")
Message from Parent Template (section): @RenderSection(""ParentMessage"")
Content from Parent Template (body): @RenderBody()
"; + const string Parent = @"@{Layout = ""GrandParent"";}@section ParentMessage {Hello from Parent}

Child content: @RenderBody()

"; const string template = @"@{Layout = ""Parent"";}@section ChildMessage {Hello from Child}

This is child content

"; const string expected = "
Message from Child Template (section): Hello from Child
Message from Parent Template (section): Hello from Parent
Content from Parent Template (body):

Child content:

This is child content

"; - service.GetTemplate(parent, null, "Parent"); - service.GetTemplate(grandparent, null, "GrandParent"); - string result = service.Parse(template, null, null, null); + var keyGrandparent = service.GetKey(nameof(GrandParent)); + var keyParent = service.GetKey(nameof(Parent)); + service.AddTemplate(keyGrandparent, new LoadedTemplateSource(GrandParent)); + service.AddTemplate(keyParent, new LoadedTemplateSource(Parent)); + + string result = service.RunCompile(templateSource: template, name: nameof(template)); Assert.That(result == expected, "Result does not match expected: " + result); } @@ -125,14 +145,20 @@ public void TemplateBase_CanRenderWithLayout_WithComplexLayout() [Test] public void TemplateBase_CanRenderWithInclude_SimpleInclude() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) + using (var writer = new StringWriter()) { const string child = "
Content from child
"; const string template = "@Include(\"Child\")"; const string expected = "
Content from child
"; - service.GetTemplate(child, null, "Child"); - string result = service.Parse(template, null, null, null); + var keyChild = service.GetKey("Child"); + var key = service.GetKey(nameof(template)); + + service.AddTemplate(keyChild, child); + service.AddTemplate(key, template); + service.RunCompile(key, writer); + string result = writer.ToString(); Assert.That(result == expected, "Result does not match expected: " + result); } @@ -144,34 +170,43 @@ public void TemplateBase_CanRenderWithInclude_SimpleInclude() [Test] public void TemplateBase_CanRenderWithInclude_WithCurrentModel() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) + using (var writer = new StringWriter()) { const string child = "@model RazorEngine.Tests.TestTypes.Person\n
Content from child for @Model.Forename
"; const string template = "@model RazorEngine.Tests.TestTypes.Person\n@Include(\"Child\")"; const string expected = "
Content from child for Test
"; var person = new Person { Forename = "Test" }; - service.GetTemplate(child, person, "Child"); - string result = service.Parse(template, person, null, null); + var childKey = service.GetKey("Child"); + service.AddTemplate(childKey, child); + service.RunCompile(childKey, model: person); + //string result = service.Parse(template, person, null, null); + string result = service.RunCompile(templateSource: template, name: nameof(template), model: person); Assert.That(result == expected, "Result does not match expected: " + result); } - } - + } + /// /// Tests that a template service can include another template with current templete model if it was not specified. /// [Test] public void TemplateBase_CanRenderWithInclude_WithCustomModel() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) { - const string child = "@model RazorEngine.Tests.TestTypes.Person\n
Content from child for @Model.Forename
"; + const string Child = "@model RazorEngine.Tests.TestTypes.Person\n
Content from child for @Model.Forename
"; const string template = "@Include(\"Child\", new RazorEngine.Tests.TestTypes.Person { Forename = \"Test\" })"; const string expected = "
Content from child for Test
"; - service.GetTemplate(child, null, "Child"); - string result = service.Parse(template, null, null, null); + var childKey = service.GetKey(nameof(Child)); + var templateKey = service.GetKey(nameof(template)); + + service.AddTemplate(childKey, Child); + service.AddTemplate(templateKey, template); + + string result = service.RunCompile(templateSource: template, key: templateKey); Assert.That(result == expected, "Result does not match expected: " + result); } @@ -184,7 +219,8 @@ public void TemplateBase_CanRenderWithInclude_WithCustomModel() [Test] public void TemplateBase_CanRenderInclude_WithInlineTemplate() { - using (var service = new TemplateService()) + using (var service = RazorEngineService.Create()) + using (var writer = new StringWriter()) { const string child = "@model RazorEngine.Tests.TestTypes.InlineTemplateModel\n@Model.InlineTemplate(Model)"; const string template = "@model RazorEngine.Tests.TestTypes.InlineTemplateModel\n@{ Model.InlineTemplate = @

@ViewBag.Name

; }@Include(\"Child\", Model)"; @@ -193,8 +229,14 @@ public void TemplateBase_CanRenderInclude_WithInlineTemplate() dynamic bag = new DynamicViewBag(); bag.Name = "Matt"; - service.GetTemplate(child, null, "Child"); - string result = service.Parse(template, new InlineTemplateModel(), bag, null); + var childKey = service.GetKey("Child"); + var templateKey = service.GetKey(nameof(template)); + service.AddTemplate(childKey, child); + service.AddTemplate(templateKey, template); + + service.RunCompile(templateKey, writer, model: new InlineTemplateModel(), viewBag: bag); + string result = writer.ToString(); + Assert.That(result == expected, "Result does not match expected: " + result); } } From 3a1eea0dff6da6ee6222ec09c0f84b91969c1bf1 Mon Sep 17 00:00:00 2001 From: Connie Yau Date: Wed, 28 Jun 2017 14:06:48 -0700 Subject: [PATCH 2/2] Fix variable _disposed is unused warning by implementing its Dispose pattern --- .../RazorEngine.Core/Compilation/CompilerServiceBase.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs b/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs index 418bd32b..63041358 100644 --- a/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs +++ b/src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs @@ -417,7 +417,12 @@ public void Dispose() /// true when Dispose() was called manually. protected virtual void Dispose(bool disposing) { + if (_disposed) + { + return; + } + _disposed = true; } #endregion