Skip to content

Added tests for null, anonymous and iterator models. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/WebApiContrib.Formatting.Razor/RazorViewParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class RazorViewParser : IViewParser
{
private readonly ITemplateService _templateService;

public RazorViewParser(ITemplateService templateService)
{
if (templateService == null)
throw new ArgumentNullException("templateService");
public RazorViewParser(ITemplateService templateService)
{
if (templateService == null)
throw new ArgumentNullException("templateService");

_templateService = templateService;
}
_templateService = templateService;
}

public RazorViewParser()
{
Expand All @@ -27,13 +27,13 @@ public RazorViewParser()


public RazorViewParser(ITemplateResolver resolver)
{
if (resolver == null)
throw new ArgumentNullException("resolver");
{
if (resolver == null)
throw new ArgumentNullException("resolver");

var config = new TemplateServiceConfiguration { Resolver = resolver };
_templateService = new TemplateService(config);
}
var config = new TemplateServiceConfiguration { Resolver = resolver };
_templateService = new TemplateService(config);
}


public byte[] ParseView(IView view, string viewTemplate, System.Text.Encoding encoding)
Expand All @@ -47,8 +47,8 @@ private string GetParsedView(IView view, string viewTemplate)
{
if (view.ModelType == null)
{
_templateService.Compile(viewTemplate, view.ViewName);
return _templateService.Run(view.ViewName);
_templateService.Compile(viewTemplate, null, view.ViewName);
return _templateService.Run(view.ViewName, view.Model);
}

_templateService.Compile(viewTemplate, view.ModelType, view.ViewName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace WebApiContrib.Formatting.RazorViewEngine.Tests
{
using System.Collections.Generic;

[TestFixture]
public class ViewEngineTests
{
Expand Down Expand Up @@ -54,5 +56,48 @@ public void render_template_with_specified_resolver()

Assert.AreEqual("<html>Hello foo! Welcome to Razor!</html>", output);
}

[Test]
public void render_anonymous_model_type()
{
var view = new View("Test1", new { Name = "foo" });
var content = new ObjectContent<View>(view, _formatter);

var output = content.ReadAsStringAsync().Result;

Assert.AreEqual("Hello foo! Welcome to Razor!", output);
}

[Test]
public void render_iterator_model_type()
{
var view = new View("IteratorTest", SomeValues);
var content = new ObjectContent<View>(view, _formatter);

var output = content.ReadAsStringAsync().Result;

Assert.AreEqual("first, second, third", output);
}

private IEnumerable<string> SomeValues
{
get
{
yield return "first";
yield return "second";
yield return "third";
}
}

[Test]
public void render_null_model()
{
var view = new View("NullModelTest", null, typeof(object));
var content = new ObjectContent<View>(view, _formatter);

var output = content.ReadAsStringAsync().Result;

Assert.AreEqual("Hello Anonymous! Welcome to Razor!", output);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@{ var count = 0; }
@foreach (var item in Model) { if (count++ > 0) {<text>, </text>}@item}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello @(Model != null ? Model : "Anonymous")! Welcome to Razor!
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@{
_Layout = "~/Embed.cshtml";
_Layout = "~/Embed.cshtml";
}
Hello @Model.Name! Welcome to Razor!
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
<EmbeddedResource Include="Views\Test1.cshtml" />
<EmbeddedResource Include="Views\Test2.cshtml" />
<None Include="packages.config" />
<EmbeddedResource Include="Views\NullModelTest.cshtml" />
<EmbeddedResource Include="Views\IteratorTest.cshtml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\WebApiContrib.Formatting.Razor\WebApiContrib.Formatting.Razor.csproj">
Expand Down