11namespace RazorEngine . Tests . TestTypes . Issues
22{
33 using System ;
4+ using System . IO ;
45 using Microsoft . CSharp . RuntimeBinder ;
56
67 using NUnit . Framework ;
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