Skip to content

Commit a2a17c5

Browse files
authored
Merge pull request #478 from conniey/fixObsoleteTests
Update Obsolete tests to use RazorEngineService
2 parents 164a888 + 3a1eea0 commit a2a17c5

8 files changed

Lines changed: 217 additions & 95 deletions

File tree

src/source/RazorEngine.Core/Compilation/CompilerServiceBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,12 @@ public void Dispose()
417417
/// <param name="disposing">true when Dispose() was called manually.</param>
418418
protected virtual void Dispose(bool disposing)
419419
{
420+
if (_disposed)
421+
{
422+
return;
423+
}
420424

425+
_disposed = true;
421426
}
422427

423428
#endregion

src/test/Test.RazorEngine.Core/ActivatorTestFixture.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@ public void TemplateService_CanSupportCustomActivator_WithUnity()
4646
BaseTemplateType = typeof(CustomTemplateBase<>)
4747
};
4848

49-
#pragma warning disable 0618 // Fine because we still want to test if
50-
using (var service = new TemplateService(config))
51-
#pragma warning restore 0618 // Fine because we still want to test if
49+
using (var service = RazorEngineService.Create(config))
5250
{
5351
const string template = "<h1>Hello @Format(Model.Forename)</h1>";
5452
const string expected = "<h1>Hello ttaM</h1>";
5553

5654
var model = new Person { Forename = "Matt" };
57-
string result = service.Parse(template, model, null, null);
55+
string result = service.RunCompile(templateSource: template, name: "template", model: model);
5856

5957
Assert.That(result == expected, "Result does not match expected: " + result);
6058
}

src/test/Test.RazorEngine.Core/CodeInspectorTestFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public void CodeInspector_SupportsAddingCustomInspector()
2727
var config = new TemplateServiceConfiguration();
2828
config.CodeInspectors.Add(new ThrowExceptionCodeInspector());
2929

30-
using (var service = new TemplateService(config))
30+
using (var service = RazorEngineService.Create(config))
3131
{
3232
const string template = "Hello World";
3333

34-
Assert.Throws<InvalidOperationException>(() => service.Parse(template, null, null, null));
34+
Assert.Throws<InvalidOperationException>(() => service.RunCompile(templateSource: template, name: "template"));
3535
}
3636
}
3737
#endregion

src/test/Test.RazorEngine.Core/ConfigurationTestFixture.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@ public void FluentTemplateServiceConfiguration_CanConfigureTemplateService_WithA
3838
{
3939
var config = new FluentTemplateServiceConfiguration(
4040
c => c.IncludeNamespaces("System.IO"));
41-
#pragma warning disable 0618 // TODO: Update test.
42-
using (var service = new TemplateService(config))
43-
#pragma warning restore 0618 // TODO: Update test.
41+
using (var service = RazorEngineService.Create(config))
42+
using (var writer = new StringWriter())
4443
{
4544
const string template = @"@Directory.GetFiles(Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Personal)), ""*.*"").Length";
4645

4746
int expected = Directory.GetFiles(Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Personal)), "*.*").Length;
48-
string result = service.Parse(template, null, null, null);
4947

48+
var key = service.GetKey("testTemplate");
49+
var source = new LoadedTemplateSource(template);
50+
service.AddTemplate(key, source);
51+
service.Compile(key);
52+
service.Run(key, writer);
53+
var result = writer.ToString();
5054
Assert.AreEqual(expected.ToString(), result);
5155
}
5256
}
@@ -74,14 +78,12 @@ public void FluentTemplateServiceConfiguration_CanConfigureTemplateService_WithS
7478
var config = new FluentTemplateServiceConfiguration(
7579
c => c.WithCodeLanguage(Language.VisualBasic));
7680

77-
#pragma warning disable 0618 // TODO: Update test.
78-
using (var service = new TemplateService(config))
79-
#pragma warning restore 0618 // TODO: Update test.
81+
using (var service = RazorEngineService.Create(config))
8082
{
8183
const string template = "@Code Dim name As String = \"Matt\" End Code\n@name";
8284
const string expected = "\nMatt";
8385

84-
string result = service.Parse(template, null, null, null);
86+
string result = service.RunCompile(templateSource: template, name: "template");
8587

8688
Assert.That(result == expected, "Result does not match expected: " + result);
8789
}
@@ -97,17 +99,22 @@ public void FluentTemplateServiceConfiguration_CanConfigureTemplateService_WithS
9799
var config = new FluentTemplateServiceConfiguration(
98100
c => c.WithEncoding(Encoding.Raw));
99101

100-
#pragma warning disable 0618 // TODO: Update test.
101-
using (var service = new TemplateService(config))
102-
#pragma warning restore 0618 // TODO: Update test.
102+
using (var service = RazorEngineService.Create(config))
103+
using (var writer = new StringWriter())
103104
{
104105
const string template = "<h1>Hello @Model.String</h1>";
105106
const string expected = "<h1>Hello Matt & World</h1>";
106107

107108
var model = new { String = "Matt & World" };
108-
string result = service.Parse(template, model, null, null);
109+
var source = new LoadedTemplateSource(template);
110+
var key = service.GetKey("testTemplate");
109111

110-
Assert.That(result == expected, "Result does not match expected: " + result);
112+
service.Compile(source, key);
113+
service.Run(key, writer, model: model);
114+
115+
var contents = writer.ToString();
116+
117+
Assert.AreEqual(expected, contents);
111118
}
112119
}
113120

src/test/Test.RazorEngine.Core/Issues/Release_3_0_TestFixture.cs

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace RazorEngine.Tests.TestTypes.Issues
22
{
33
using System;
4+
using System.IO;
45
using Microsoft.CSharp.RuntimeBinder;
56

67
using NUnit.Framework;
@@ -12,7 +13,6 @@
1213
/// Provides tests for the Release 3.0
1314
/// </summary>
1415
[TestFixture]
15-
[Obsolete("Needs to be updated to the new API")]
1616
public class Release_3_0_TestFixture
1717
{
1818
#region Tests
@@ -24,7 +24,7 @@ public class Release_3_0_TestFixture
2424
[Test]
2525
public void Issue6_ModelShouldBePassedToLayout()
2626
{
27-
using (var service = new TemplateService())
27+
using (var service = RazorEngineService.Create())
2828
{
2929
const string layoutTemplate = "<h1>@Model.PageTitle</h1> @RenderSection(\"Child\")";
3030
const string childTemplate = "@{ Layout = \"Parent\"; }@section Child {<h2>@Model.PageDescription</h2>}";
@@ -35,11 +35,13 @@ public void Issue6_ModelShouldBePassedToLayout()
3535
PageDescription = "Test Page Description"
3636
};
3737

38-
var type = model.GetType();
38+
var key = service.GetKey("Parent");
39+
var childKey = service.GetKey("Child");
3940

40-
service.Compile(layoutTemplate, type, "Parent");
41+
service.AddTemplate(key, layoutTemplate);
42+
service.Compile(key);
4143

42-
string result = service.Parse(childTemplate, model, null, null);
44+
string result = service.RunCompile(templateSource: childTemplate, key: childKey, model: model);
4345

4446
Assert.That(result == expected, "Result does not match expected: " + result);
4547
}
@@ -54,14 +56,22 @@ public void Issue6_ModelShouldBePassedToLayout()
5456
[Test]
5557
public void Issue7_ViewBagShouldPersistThroughLayout()
5658
{
57-
using (var service = new TemplateService())
59+
using (var service = RazorEngineService.Create())
60+
using (var writer = new StringWriter())
5861
{
5962
const string layoutTemplate = "<h1>@ViewBag.Title</h1>@RenderSection(\"Child\")";
6063
const string childTemplate = "@{ Layout = \"Parent\"; ViewBag.Title = \"Test\"; }@section Child {}";
6164

62-
service.Compile(layoutTemplate, null, "Parent");
65+
var key = service.GetKey("Parent");
66+
var childKey = service.GetKey(nameof(childTemplate));
6367

64-
string result = service.Parse(childTemplate, null, null, null);
68+
service.AddTemplate(key, layoutTemplate);
69+
service.AddTemplate(childKey, childTemplate);
70+
71+
service.Compile(key);
72+
service.Compile(childKey);
73+
service.Run(childKey, writer);
74+
string result = writer.ToString();
6575

6676
Assert.That(result.StartsWith("<h1>Test</h1>"));
6777
}
@@ -134,11 +144,14 @@ public void Issue7_ViewBagShouldNotPersistThroughInclude_UsingVB()
134144
[Test]
135145
public void Issue11_TemplateServiceShouldCompileModellessTemplate()
136146
{
137-
using (var service = new TemplateService())
147+
using (var service = RazorEngineService.Create())
138148
{
139149
const string template = "<h1>Hello World</h1>";
140150

141-
service.Compile(template, null, "issue11");
151+
var key = service.GetKey(nameof(template));
152+
var result = service.RunCompile(templateSource: template, key: key);
153+
154+
Assert.AreEqual(template, result);
142155
}
143156
}
144157

@@ -151,13 +164,14 @@ public void Issue11_TemplateServiceShouldCompileModellessTemplate()
151164
[Test]
152165
public void Issue16_LastNullValueShouldReturnEmptyString()
153166
{
154-
using (var service = new TemplateService())
167+
using (var service = RazorEngineService.Create())
155168
{
156169
const string template = "<h1>Hello @Model.Person.Forename</h1>";
157170
const string expected = "<h1>Hello </h1>";
158171

159172
var model = new { Person = new Person { Forename = null } };
160-
string result = service.Parse(template, model, null, null);
173+
var key = service.GetKey(nameof(template));
174+
var result = service.RunCompile(templateSource: template, key: key, model: model);
161175

162176
Assert.That(result == expected, "Result does not match expected: " + result);
163177
}
@@ -171,15 +185,20 @@ public void Issue16_LastNullValueShouldReturnEmptyString()
171185
[Test]
172186
public void TemplateService_ShouldAllowTypeOverrideForNonGenericCompile()
173187
{
174-
using (var service = new TemplateService())
188+
using (var service = RazorEngineService.Create())
189+
using (var writer = new StringWriter())
175190
{
176191
const string template = "@Model.Name";
177192
const string expected = "Matt";
178193

179194
object model = new { Name = "Matt" };
180-
Type modelType = model.GetType();
181195

182-
string result = service.Parse(template, model, null, null);
196+
var key = service.GetKey(nameof(template));
197+
198+
service.AddTemplate(key, template);
199+
service.RunCompile(key, writer, model: model);
200+
201+
string result = writer.ToString();
183202

184203
Assert.That(result == expected, "Result does not match expected: " + result);
185204
}
@@ -194,14 +213,19 @@ public void TemplateService_ShouldAllowTypeOverrideForNonGenericCompile()
194213
[Test]
195214
public void TemplateService_ShouldEnableNullableValueTypes()
196215
{
197-
using (var service = new TemplateService())
216+
using (var service = RazorEngineService.Create())
217+
using (var writer = new StringWriter())
198218
{
199219
const string template = "<h1>Hello @Model.Number</h1>";
200220
const string expected = "<h1>Hello </h1>";
201221

202222
var model = new { Number = (int?)null };
203-
string result = service.Parse(template, model, null, null);
223+
var key = service.GetKey(nameof(template));
204224

225+
service.AddTemplate(key, template);
226+
service.RunCompile(key, writer, model: model);
227+
228+
string result = writer.ToString();
205229
Assert.That(result == expected, "Result does not match expected: " + result);
206230
}
207231
}
@@ -214,10 +238,15 @@ public void TemplateService_ShouldEnableNullableValueTypes()
214238
[Test]
215239
public void Issue21_SubclassModelShouldBeSupportedInLayout()
216240
{
217-
using (var service = new TemplateService())
241+
using (var service = RazorEngineService.Create())
242+
using (var writer = new StringWriter())
218243
{
219-
const string parent = "@model RazorEngine.Tests.TestTypes.Person\n<h1>@Model.Forename</h1>@RenderSection(\"Child\")";
220-
service.Compile(parent, null, "Parent");
244+
const string Parent = "@model RazorEngine.Tests.TestTypes.Person\n<h1>@Model.Forename</h1>@RenderSection(\"Child\")";
245+
246+
var key = service.GetKey(nameof(Parent));
247+
248+
service.AddTemplate(key, Parent);
249+
service.Compile(key);
221250

222251
const string child = "@{ Layout = \"Parent\"; }\n@section Child { <h2>@Model.Department</h2> }";
223252
const string expected = "<h1>Matt</h1> <h2>IT</h2> ";
@@ -230,8 +259,12 @@ public void Issue21_SubclassModelShouldBeSupportedInLayout()
230259
Surname = "Abbott"
231260
};
232261

233-
string result = service.Parse(child, model, null, null);
262+
var childKey = service.GetKey(nameof(child));
263+
264+
service.AddTemplate(childKey, child);
265+
service.RunCompile(childKey, writer, model.GetType(), model: model);
234266

267+
string result = writer.ToString();
235268
Assert.That(result == expected, "Result does not match expected: " + result);
236269
}
237270
}

0 commit comments

Comments
 (0)