Adding support for RazorView.IsSectionDefined

This commit is contained in:
Pranav K 2014-04-03 15:48:58 -07:00
parent c0d06f1fbc
commit 94c028a5df
4 changed files with 113 additions and 3 deletions

View File

@ -170,6 +170,22 @@ namespace Microsoft.AspNet.Mvc.Razor
return string.Format(CultureInfo.CurrentCulture, GetString("ViewEngine_ViewNotFound"), p0, p1); return string.Format(CultureInfo.CurrentCulture, GetString("ViewEngine_ViewNotFound"), p0, p1);
} }
/// <summary>
/// The method '{0}' cannot be invoked by this view.
/// </summary>
internal static string View_MethodCannotBeCalled
{
get { return GetString("View_MethodCannotBeCalled"); }
}
/// <summary>
/// The method '{0}' cannot be invoked by this view.
/// </summary>
internal static string FormatView_MethodCannotBeCalled(object p0)
{
return string.Format(CultureInfo.CurrentCulture, GetString("View_MethodCannotBeCalled"), p0);
}
private static string GetString(string name, params string[] formatterNames) private static string GetString(string name, params string[] formatterNames)
{ {
var value = _resourceManager.GetString(name); var value = _resourceManager.GetString(name);

View File

@ -259,6 +259,12 @@ namespace Microsoft.AspNet.Mvc.Razor
SectionWriters[name] = action; SectionWriters[name] = action;
} }
public bool IsSectionDefined([NotNull] string name)
{
EnsureMethodCanBeInvoked("IsSectionDefined");
return PreviousSectionWriters.ContainsKey(name);
}
public HelperResult RenderSection([NotNull] string name) public HelperResult RenderSection([NotNull] string name)
{ {
return RenderSection(name, required: true); return RenderSection(name, required: true);
@ -266,9 +272,10 @@ namespace Microsoft.AspNet.Mvc.Razor
public HelperResult RenderSection([NotNull] string name, bool required) public HelperResult RenderSection([NotNull] string name, bool required)
{ {
EnsureMethodCanBeInvoked("RenderSection");
HelperResult action; HelperResult action;
if (PreviousSectionWriters != null && if (PreviousSectionWriters.TryGetValue(name, out action))
PreviousSectionWriters.TryGetValue(name, out action))
{ {
return action; return action;
} }
@ -283,5 +290,13 @@ namespace Microsoft.AspNet.Mvc.Razor
return null; return null;
} }
} }
private void EnsureMethodCanBeInvoked(string methodName)
{
if (PreviousSectionWriters == null)
{
throw new InvalidOperationException(Resources.FormatView_MethodCannotBeCalled(methodName));
}
}
} }
} }

View File

@ -147,4 +147,7 @@
<data name="ViewEngine_ViewNotFound" xml:space="preserve"> <data name="ViewEngine_ViewNotFound" xml:space="preserve">
<value>The view '{0}' was not found. The following locations were searched:{1}.</value> <value>The view '{0}' was not found. The following locations were searched:{1}.</value>
</data> </data>
<data name="View_MethodCannotBeCalled" xml:space="preserve">
<value>The method '{0}' cannot be invoked by this view.</value>
</data>
</root> </root>

View File

@ -2,6 +2,7 @@
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Testing;
using Moq; using Moq;
using Xunit; using Xunit;
@ -56,6 +57,25 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
Assert.Same(actual, expected); Assert.Same(actual, expected);
} }
[Fact]
public async Task RenderSection_ThrowsIfNoPreviousPage()
{
// Arrange
Exception ex = null;
var view = CreateView(v =>
{
ex = Assert.Throws<InvalidOperationException>(() => v.RenderSection("bar"));
});
var viewContext = CreateViewContext(layoutView: null);
// Act
await view.RenderAsync(viewContext);
// Assert
Assert.Equal("The method 'RenderSection' cannot be invoked by this view.",
ex.Message);
}
[Fact] [Fact]
public async Task RenderSection_ThrowsIfRequiredSectionIsNotFound() public async Task RenderSection_ThrowsIfRequiredSectionIsNotFound()
{ {
@ -80,7 +100,63 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
// Assert // Assert
Assert.Equal("Section 'bar' is not defined.", ex.Message); Assert.Equal("Section 'bar' is not defined.", ex.Message);
} }
[Fact]
public void IsSectionDefined_ThrowsIfNoPreviousExecutingPage()
{
// Arrange
var view = CreateView(v => { });
var viewContext = CreateViewContext(layoutView: null);
// Act and Assert
ExceptionAssert.Throws<InvalidOperationException>(() => view.IsSectionDefined("foo"),
"The method 'IsSectionDefined' cannot be invoked by this view.");
}
[Fact]
public async Task IsSectionDefined_ReturnsFalseIfSectionNotDefined()
{
// Arrange
bool? actual = null;
var view = CreateView(v =>
{
v.DefineSection("baz", new HelperResult(writer => { }));
v.Layout = LayoutPath;
});
var layoutView = CreateView(v =>
{
actual = v.IsSectionDefined("foo");
});
// Act
await view.RenderAsync(CreateViewContext(layoutView));
// Assert
Assert.Equal(false, actual);
}
[Fact]
public async Task IsSectionDefined_ReturnsTrueIfSectionDefined()
{
// Arrange
bool? actual = null;
var view = CreateView(v =>
{
v.DefineSection("foo", new HelperResult(writer => { }));
v.Layout = LayoutPath;
});
var layoutView = CreateView(v =>
{
actual = v.IsSectionDefined("foo");
});
// Act
await view.RenderAsync(CreateViewContext(layoutView));
// Assert
Assert.Equal(true, actual);
}
public static RazorView CreateView(Action<RazorView> executeAction) public static RazorView CreateView(Action<RazorView> executeAction)
{ {
var view = new Mock<RazorView> { CallBase = true }; var view = new Mock<RazorView> { CallBase = true };