Skip to content

Commit e1881d9

Browse files
committed
add verify example
1 parent 18cf612 commit e1881d9

19 files changed

+293
-17
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ node_modules
3838
*launchSettings.json
3939
*.orig
4040
nuget
41-
RazorLight.Sandbox/
41+
RazorLight.Sandbox/
42+
43+
*.received.*

README.md

+1-15
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,6 @@ var metadataReference = MetadataReference.CreateFromFile("path-to-your-assembly"
238238
.Build();
239239
````
240240

241-
### I'm getting errors after upgrading to ASP.NET Core 3.0 when using runtime compilation
242-
243-
Please see: https://docs.microsoft.com/en-us/aspnet/core/razor-pages/sdk?view=aspnetcore-3.1#use-the-razor-sdk
244-
245-
> Starting with ASP.NET Core 3.0, MVC Views or Razor Pages aren't served by default if the `RazorCompileOnBuild` or `RazorCompileOnPublish` MSBuild properties in the project file are disabled. Applications must add an explicit reference to the `Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation` package if the app relies on runtime compilation to process .cshtml files.
246-
247241
### I'm getting a Null Reference Exception after upgrading to RazorLight-2.0.0-beta2 or later.
248242

249243
The most common scenario is that some people were using RazorLight's ability to render raw strings as templates. While this is still somewhat supported (you can't use advanced features like partial views), what is not supported (right now) is using the caching provider with raw strings. A workaround is to use a dummy class.
@@ -291,15 +285,7 @@ For more information, see https://github.com/aspnet/AspNetCore/issues/14418#issu
291285

292286
### RazorLight does not work properly on AWS Lambda or Azure Functions
293287

294-
Serverless solutions are not supported yet. However, for Azure Functions, some users have reported success on Azure Functions 3.0.3. As of 6/3/2020, Azure Functions SDK team has acknowledged a [bug in Azure Functions `RemoveRuntimeDependencies` task](https://github.com/toddams/RazorLight/issues/306#issuecomment-636374491), affecting Azure Functions 3.0.4-3.0.6 releases.
295-
296-
For Azure Functions 3.0.4-3.0.5, the known workaround is to disable "Azure Functions dependency trimming". To disable dependency trimming, add the following to your root / entrypoint project:
297-
298-
```xml
299-
<PropertyGroup>
300-
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
301-
</PropertyGroup>
302-
```
288+
Serverless solutions are not supported yet.
303289

304290
## Unsupported Scenarios
305291

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<layout>
2+
@RenderBody()
3+
</layout>
4+
5+
<section>
6+
@RenderSection("header", true)
7+
</section>
8+
9+
<section>
10+
@RenderSection("message", false)
11+
</section>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@using RazorLight
2+
@model RazorLight.Tests.IntegrationTests.TestViewModel
3+
4+
This is a nested partial which attempts to render Partial.cshtml
5+
6+
@{
7+
await IncludeAsync("Partial", Model);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@using RazorLight
2+
@model RazorLight.Tests.IntegrationTests.TestViewModel
3+
4+
This included partial template is used to render data of the passed view model: @Model.Name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@using RazorLight
2+
@model RazorLight.Tests.IntegrationTests.TestViewModel
3+
@{
4+
Layout = "LayoutWithSection.cshtml";
5+
}
6+
7+
@section section {
8+
The content of the section
9+
}
10+
11+
We are rendering this with @Model.Name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@using RazorLight
2+
@model RazorLight.Tests.IntegrationTests.TestViewModel
3+
@{
4+
Layout = "LayoutWithRequiredSection.cshtml";
5+
}
6+
7+
@section header {
8+
Header of a @Model.Name page
9+
}
10+
11+
@section message {
12+
await IncludeAsync("Partial", Model)
13+
}
14+
15+
16+
We are rendering this with @Model.Name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@using RazorLight
2+
@model RazorLight.Tests.IntegrationTests.TestViewModel
3+
@{
4+
Layout = "LayoutWithRequiredSection.cshtml";
5+
}
6+
7+
@section message {
8+
Message
9+
}
10+
11+
We are rendering this with @Model.Name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@using RazorLight
2+
@model RazorLight.Tests.IntegrationTests.TestViewModel
3+
4+
We are rendering this with @Model.Name
5+
6+
@{
7+
await IncludeAsync("Partial", Model);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@using RazorLight
2+
@model RazorLight.Tests.IntegrationTests.TestViewModel
3+
@{
4+
Layout = "LayoutWithRequiredSection.cshtml";
5+
}
6+
7+
@section header {
8+
Header of a @Model.Name page
9+
}
10+
11+
@section message {
12+
await IncludeAsync("NestedPartial", Model);
13+
}
14+
15+
16+
We are rendering this with @Model.Name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@using RazorLight
2+
@model RazorLight.Tests.IntegrationTests.TestViewModel
3+
4+
@{
5+
await IncludeAsync("NestedPartial", Model);
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
Message: 'Section header is not defined',
3+
Data: {},
4+
Source: 'RazorLight'
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
We are rendering this with RazorLight
3+
4+
5+
This included partial template is used to render data of the passed view model: RazorLight
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
This is a nested partial which attempts to render Partial.cshtml
4+
5+
6+
This included partial template is used to render data of the passed view model: RazorLight
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<layout>
2+
3+
4+
5+
6+
We are rendering this with RazorLight
7+
</layout>
8+
9+
<section>
10+
11+
Header of a RazorLight page
12+
13+
</section>
14+
15+
<section>
16+
17+
await IncludeAsync("NestedPartial", Model);
18+
19+
</section>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<layout>
2+
3+
4+
We are rendering this with RazorLight
5+
</layout>
6+
7+
<section>
8+
9+
The content of the section
10+
11+
</section>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<layout>
2+
3+
4+
5+
6+
We are rendering this with RazorLight
7+
</layout>
8+
9+
<section>
10+
11+
Header of a RazorLight page
12+
13+
</section>
14+
15+
<section>
16+
17+
await IncludeAsync("Partial", Model)
18+
19+
</section>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+

2+
using System;
3+
using System.IO;
4+
using System.Reflection;
5+
using System.Threading.Tasks;
6+
using RazorLight.Compilation;
7+
using VerifyXunit;
8+
using Xunit;
9+
10+
namespace RazorLight.Tests.IntegrationTests
11+
{
12+
public class TestViewModel
13+
{
14+
public string Name { get; set; }
15+
16+
public int NumberOfItems { get; set; }
17+
}
18+
19+
[UsesVerify]
20+
public class RendererCommonCasesTests
21+
{
22+
23+
[Fact]
24+
public async Task Should_Render_Section_And_ViewModel()
25+
{
26+
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
27+
28+
var engine = new RazorLightEngineBuilder()
29+
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
30+
.Build();
31+
32+
var model = new TestViewModel
33+
{
34+
Name = "RazorLight",
35+
NumberOfItems = 100
36+
};
37+
var renderedResult = await engine.CompileRenderAsync("template4.cshtml", model);
38+
await Verifier.Verify(renderedResult);
39+
}
40+
41+
[Fact]
42+
public async Task Should_Render_Sections_With_IncludeAsync()
43+
{
44+
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
45+
46+
var engine = new RazorLightEngineBuilder()
47+
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
48+
.Build();
49+
50+
var model = new TestViewModel
51+
{
52+
Name = "RazorLight",
53+
NumberOfItems = 200
54+
};
55+
var renderedResult = await engine.CompileRenderAsync("template5.cshtml", model);
56+
await Verifier.Verify(renderedResult);
57+
}
58+
59+
[Fact]
60+
public async Task Should_Fail_When_Required_Section_Is_Missing()
61+
{
62+
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
63+
64+
var engine = new RazorLightEngineBuilder()
65+
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
66+
.Build();
67+
68+
var model = new TestViewModel
69+
{
70+
Name = "RazorLight",
71+
NumberOfItems = 300
72+
};
73+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () => await engine.CompileRenderAsync("template6.cshtml", model));
74+
await Verifier.Verify(exception);
75+
}
76+
77+
[Fact]
78+
public async Task Should_Render_IncludeAsync()
79+
{
80+
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
81+
82+
var engine = new RazorLightEngineBuilder()
83+
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
84+
.Build();
85+
86+
var model = new TestViewModel
87+
{
88+
Name = "RazorLight",
89+
NumberOfItems = 400
90+
};
91+
var renderedResult = await engine.CompileRenderAsync("template7.cshtml", model);
92+
await Verifier.Verify(renderedResult);
93+
}
94+
95+
[Fact]
96+
public async Task Should_Render_Nested_IncludeAsync()
97+
{
98+
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
99+
100+
var engine = new RazorLightEngineBuilder()
101+
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
102+
.Build();
103+
104+
var model = new TestViewModel
105+
{
106+
Name = "RazorLight",
107+
NumberOfItems = 400
108+
};
109+
var renderedResult = await engine.CompileRenderAsync("template9.cshtml", model);
110+
await Verifier.Verify(renderedResult);
111+
}
112+
113+
[Fact]
114+
public async Task Should_Render_RequiredSections_That_Have_Nested_IncludeAsync()
115+
{
116+
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
117+
118+
var engine = new RazorLightEngineBuilder()
119+
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
120+
.Build();
121+
122+
var model = new TestViewModel
123+
{
124+
Name = "RazorLight",
125+
NumberOfItems = 400
126+
};
127+
var renderedResult = await engine.CompileRenderAsync("template8.cshtml", model);
128+
await Verifier.Verify(renderedResult);
129+
}
130+
}
131+
}

tests/RazorLight.Tests/RazorLight.Tests.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
<PackageReference Include="MarkdownSnippets.MsBuild" Version="18.0.0" />
3232
<PackageReference Include="Moq" Version="4.7.99" />
3333
<PackageReference Include="Pose" Version="1.2.1" />
34-
<PackageReference Include="xunit" Version="2.3.1" />
34+
<PackageReference Include="Verify.Xunit" Version="5.0.2" />
35+
<PackageReference Include="xunit" Version="2.4.1" />
3536
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
3637
</ItemGroup>
3738

0 commit comments

Comments
 (0)