Skip to content

Commit

Permalink
Merge pull request #478 from conniey/fixObsoleteTests
Browse files Browse the repository at this point in the history
Update Obsolete tests to use RazorEngineService
  • Loading branch information
matthid authored Jul 1, 2017
2 parents 164a888 + 3a1eea0 commit a2a17c5
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,12 @@ public void Dispose()
/// <param name="disposing">true when Dispose() was called manually.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

_disposed = true;
}

#endregion
Expand Down
6 changes: 2 additions & 4 deletions src/test/Test.RazorEngine.Core/ActivatorTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<h1>Hello @Format(Model.Forename)</h1>";
const string expected = "<h1>Hello ttaM</h1>";

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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/Test.RazorEngine.Core/CodeInspectorTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InvalidOperationException>(() => service.Parse(template, null, null, null));
Assert.Throws<InvalidOperationException>(() => service.RunCompile(templateSource: template, name: "template"));
}
}
#endregion
Expand Down
33 changes: 20 additions & 13 deletions src/test/Test.RazorEngine.Core/ConfigurationTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 = "<h1>Hello @Model.String</h1>";
const string expected = "<h1>Hello Matt & World</h1>";

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);
}
}

Expand Down
75 changes: 54 additions & 21 deletions src/test/Test.RazorEngine.Core/Issues/Release_3_0_TestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace RazorEngine.Tests.TestTypes.Issues
{
using System;
using System.IO;
using Microsoft.CSharp.RuntimeBinder;

using NUnit.Framework;
Expand All @@ -12,7 +13,6 @@
/// Provides tests for the Release 3.0
/// </summary>
[TestFixture]
[Obsolete("Needs to be updated to the new API")]
public class Release_3_0_TestFixture
{
#region Tests
Expand All @@ -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 = "<h1>@Model.PageTitle</h1> @RenderSection(\"Child\")";
const string childTemplate = "@{ Layout = \"Parent\"; }@section Child {<h2>@Model.PageDescription</h2>}";
Expand All @@ -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);
}
Expand All @@ -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 = "<h1>@ViewBag.Title</h1>@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("<h1>Test</h1>"));
}
Expand Down Expand Up @@ -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 = "<h1>Hello World</h1>";

service.Compile(template, null, "issue11");
var key = service.GetKey(nameof(template));
var result = service.RunCompile(templateSource: template, key: key);

Assert.AreEqual(template, result);
}
}

Expand All @@ -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 = "<h1>Hello @Model.Person.Forename</h1>";
const string expected = "<h1>Hello </h1>";

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);
}
Expand All @@ -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);
}
Expand All @@ -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 = "<h1>Hello @Model.Number</h1>";
const string expected = "<h1>Hello </h1>";

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);
}
}
Expand All @@ -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<h1>@Model.Forename</h1>@RenderSection(\"Child\")";
service.Compile(parent, null, "Parent");
const string Parent = "@model RazorEngine.Tests.TestTypes.Person\n<h1>@Model.Forename</h1>@RenderSection(\"Child\")";

var key = service.GetKey(nameof(Parent));

service.AddTemplate(key, Parent);
service.Compile(key);

const string child = "@{ Layout = \"Parent\"; }\n@section Child { <h2>@Model.Department</h2> }";
const string expected = "<h1>Matt</h1> <h2>IT</h2> ";
Expand All @@ -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);
}
}
Expand Down
Loading

0 comments on commit a2a17c5

Please sign in to comment.