Skip to content

Commit

Permalink
Merge pull request #346 from SimonCropp/verify
Browse files Browse the repository at this point in the history
add verify example
  • Loading branch information
jzabroski authored Jun 21, 2020
2 parents 18cf612 + 0b724b5 commit 35d0e5a
Show file tree
Hide file tree
Showing 20 changed files with 295 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ node_modules
*launchSettings.json
*.orig
nuget
RazorLight.Sandbox/
RazorLight.Sandbox/

*.received.*
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,6 @@ var metadataReference = MetadataReference.CreateFromFile("path-to-your-assembly"
.Build();
````

### I'm getting errors after upgrading to ASP.NET Core 3.0 when using runtime compilation

Please see: https://docs.microsoft.com/en-us/aspnet/core/razor-pages/sdk?view=aspnetcore-3.1#use-the-razor-sdk

> 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.
### I'm getting a Null Reference Exception after upgrading to RazorLight-2.0.0-beta2 or later.

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.
Expand Down Expand Up @@ -291,15 +285,7 @@ For more information, see https://github.com/aspnet/AspNetCore/issues/14418#issu

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

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.

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:

```xml
<PropertyGroup>
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>
```
Serverless solutions are not supported yet.

## Unsupported Scenarios

Expand Down
3 changes: 2 additions & 1 deletion src/RazorLight/DefaultRazorEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public static RazorEngine Instance
NamespaceDirective.Register(builder);
FunctionsDirective.Register(builder);
InheritsDirective.Register(builder);
SectionDirective.Register(builder);

}
SectionDirective.Register(builder);

builder.Features.Add(new ModelExpressionPass());
builder.Features.Add(new RazorLightTemplateDocumentClassifierPass());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<layout>
@RenderBody()
</layout>

<section>
@RenderSection("header", true)
</section>

<section>
@RenderSection("message", false)
</section>
8 changes: 8 additions & 0 deletions tests/RazorLight.Tests/Assets/Files/NestedPartial.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@using RazorLight
@model RazorLight.Tests.IntegrationTests.TestViewModel

This is a nested partial which attempts to render Partial.cshtml

@{
await IncludeAsync("Partial", Model);
}
4 changes: 4 additions & 0 deletions tests/RazorLight.Tests/Assets/Files/Partial.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@using RazorLight
@model RazorLight.Tests.IntegrationTests.TestViewModel

This included partial template is used to render data of the passed view model: @Model.Name
11 changes: 11 additions & 0 deletions tests/RazorLight.Tests/Assets/Files/template4.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@using RazorLight
@model RazorLight.Tests.IntegrationTests.TestViewModel
@{
Layout = "LayoutWithSection.cshtml";
}

@section section {
The content of the section
}

We are rendering this with @Model.Name
16 changes: 16 additions & 0 deletions tests/RazorLight.Tests/Assets/Files/template5.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@using RazorLight
@model RazorLight.Tests.IntegrationTests.TestViewModel
@{
Layout = "LayoutWithRequiredSection.cshtml";
}

@section header {
Header of a @Model.Name page
}

@section message {
await IncludeAsync("Partial", Model)
}


We are rendering this with @Model.Name
11 changes: 11 additions & 0 deletions tests/RazorLight.Tests/Assets/Files/template6.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@using RazorLight
@model RazorLight.Tests.IntegrationTests.TestViewModel
@{
Layout = "LayoutWithRequiredSection.cshtml";
}

@section message {
Message
}

We are rendering this with @Model.Name
8 changes: 8 additions & 0 deletions tests/RazorLight.Tests/Assets/Files/template7.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@using RazorLight
@model RazorLight.Tests.IntegrationTests.TestViewModel

We are rendering this with @Model.Name

@{
await IncludeAsync("Partial", Model);
}
16 changes: 16 additions & 0 deletions tests/RazorLight.Tests/Assets/Files/template8.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@using RazorLight
@model RazorLight.Tests.IntegrationTests.TestViewModel
@{
Layout = "LayoutWithRequiredSection.cshtml";
}

@section header {
Header of a @Model.Name page
}

@section message {
await IncludeAsync("NestedPartial", Model);
}


We are rendering this with @Model.Name
6 changes: 6 additions & 0 deletions tests/RazorLight.Tests/Assets/Files/template9.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@using RazorLight
@model RazorLight.Tests.IntegrationTests.TestViewModel

@{
await IncludeAsync("NestedPartial", Model);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
Message: 'Section header is not defined',
Data: {},
Source: 'RazorLight'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

We are rendering this with RazorLight


This included partial template is used to render data of the passed view model: RazorLight
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


This is a nested partial which attempts to render Partial.cshtml


This included partial template is used to render data of the passed view model: RazorLight
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<layout>




We are rendering this with RazorLight
</layout>

<section>

Header of a RazorLight page

</section>

<section>

await IncludeAsync("NestedPartial", Model);

</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<layout>


We are rendering this with RazorLight
</layout>

<section>

The content of the section

</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<layout>




We are rendering this with RazorLight
</layout>

<section>

Header of a RazorLight page

</section>

<section>

await IncludeAsync("Partial", Model)

</section>
131 changes: 131 additions & 0 deletions tests/RazorLight.Tests/Integration/RendererCommonCasesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using RazorLight.Compilation;
using VerifyXunit;
using Xunit;

namespace RazorLight.Tests.IntegrationTests
{
public class TestViewModel
{
public string Name { get; set; }

public int NumberOfItems { get; set; }
}

[UsesVerify]
public class RendererCommonCasesTests
{

[Fact()]
public async Task Should_Render_Section_And_ViewModel()
{
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

var engine = new RazorLightEngineBuilder()
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
.Build();

var model = new TestViewModel
{
Name = "RazorLight",
NumberOfItems = 100
};
var renderedResult = await engine.CompileRenderAsync("template4.cshtml", model);
await Verifier.Verify(renderedResult);
}

[Fact()]
public async Task Should_Render_Sections_With_IncludeAsync()
{
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

var engine = new RazorLightEngineBuilder()
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
.Build();

var model = new TestViewModel
{
Name = "RazorLight",
NumberOfItems = 200
};
var renderedResult = await engine.CompileRenderAsync("template5.cshtml", model);
await Verifier.Verify(renderedResult);
}

[Fact()]
public async Task Should_Fail_When_Required_Section_Is_Missing()
{
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

var engine = new RazorLightEngineBuilder()
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
.Build();

var model = new TestViewModel
{
Name = "RazorLight",
NumberOfItems = 300
};
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () => await engine.CompileRenderAsync("template6.cshtml", model));
await Verifier.Verify(exception);
}

[Fact]
public async Task Should_Render_IncludeAsync()
{
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

var engine = new RazorLightEngineBuilder()
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
.Build();

var model = new TestViewModel
{
Name = "RazorLight",
NumberOfItems = 400
};
var renderedResult = await engine.CompileRenderAsync("template7.cshtml", model);
await Verifier.Verify(renderedResult);
}

[Fact]
public async Task Should_Render_Nested_IncludeAsync()
{
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

var engine = new RazorLightEngineBuilder()
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
.Build();

var model = new TestViewModel
{
Name = "RazorLight",
NumberOfItems = 400
};
var renderedResult = await engine.CompileRenderAsync("template9.cshtml", model);
await Verifier.Verify(renderedResult);
}

[Fact()]
public async Task Should_Render_RequiredSections_That_Have_Nested_IncludeAsync()
{
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

var engine = new RazorLightEngineBuilder()
.UseFileSystemProject(Path.Combine(path, "Assets", "Files"))
.Build();

var model = new TestViewModel
{
Name = "RazorLight",
NumberOfItems = 400
};
var renderedResult = await engine.CompileRenderAsync("template8.cshtml", model);
await Verifier.Verify(renderedResult);
}
}
}
3 changes: 2 additions & 1 deletion tests/RazorLight.Tests/RazorLight.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
<PackageReference Include="MarkdownSnippets.MsBuild" Version="18.0.0" />
<PackageReference Include="Moq" Version="4.7.99" />
<PackageReference Include="Pose" Version="1.2.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="Verify.Xunit" Version="5.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>

Expand Down

0 comments on commit 35d0e5a

Please sign in to comment.