Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #771 from phillip-haydon/defaultviewlocation

re-ordered default view location and added new module path conventions
  • Loading branch information...
commit 9509e4dd0552c7f69a9c418b4e7f70a868d964cf 2 parents 28b7688 + c130f04
@thecodejunkie thecodejunkie authored
View
84 src/Nancy.Tests/Unit/Conventions/DefaultViewLocationConventionsFixture.cs
@@ -122,7 +122,7 @@ public void Should_define_convention_that_returns_viewname()
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
- var convention = this.conventions.ViewLocationConventions[5];
+ var convention = this.conventions.ViewLocationConventions[7];
// When
var result = convention.Invoke(
@@ -139,7 +139,7 @@ public void Should_define_convention_that_returns_viewname_in_views_folder()
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
- var convention = this.conventions.ViewLocationConventions[4];
+ var convention = this.conventions.ViewLocationConventions[6];
// When
var result = convention.Invoke(
@@ -156,7 +156,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_subfold
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
- var convention = this.conventions.ViewLocationConventions[0];
+ var convention = this.conventions.ViewLocationConventions[2];
// When
var result = convention.Invoke(
@@ -173,7 +173,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_subfold
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
- var convention = this.conventions.ViewLocationConventions[0];
+ var convention = this.conventions.ViewLocationConventions[2];
// When
var result = convention.Invoke(
@@ -224,7 +224,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_folder(
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
- var convention = this.conventions.ViewLocationConventions[2];
+ var convention = this.conventions.ViewLocationConventions[3];
// When
var result = convention.Invoke(
@@ -275,7 +275,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_folder_
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
- var convention = this.conventions.ViewLocationConventions[2];
+ var convention = this.conventions.ViewLocationConventions[3];
// When
var result = convention.Invoke(
@@ -292,7 +292,7 @@ public void Should_define_convention_that_returns_viewname_in_modulename_subfold
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
- var convention = this.conventions.ViewLocationConventions[1];
+ var convention = this.conventions.ViewLocationConventions[4];
// When
var result = convention.Invoke(
@@ -309,7 +309,7 @@ public void Should_define_convention_that_returns_viewname_in_modulename_folder(
{
// Given
this.viewLocationConventions.Initialise(this.conventions);
- var convention = this.conventions.ViewLocationConventions[3];
+ var convention = this.conventions.ViewLocationConventions[5];
// When
var result = convention.Invoke(
@@ -320,5 +320,73 @@ public void Should_define_convention_that_returns_viewname_in_modulename_folder(
// Then
result.ShouldEqual("modulename/viewname");
}
+
+ [Fact]
+ public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder()
+ {
+ // Given
+ this.viewLocationConventions.Initialise(this.conventions);
+ var convention = this.conventions.ViewLocationConventions[1];
+
+ // When
+ var result = convention.Invoke(
+ "viewname",
+ null,
+ new ViewLocationContext { ModuleName = "modulename", ModulePath = "modulepath" });
+
+ // Then
+ result.ShouldEqual("modulepath/modulename/viewname");
+ }
+
+ [Fact]
+ public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder_in_views_folder()
+ {
+ // Given
+ this.viewLocationConventions.Initialise(this.conventions);
+ var convention = this.conventions.ViewLocationConventions[0];
+
+ // When
+ var result = convention.Invoke(
+ "viewname",
+ null,
+ new ViewLocationContext { ModuleName = "modulename", ModulePath = "modulepath" });
+
+ // Then
+ result.ShouldEqual("views/modulepath/modulename/viewname");
+ }
+
+ [Fact]
+ public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder_when_modulepath_contains_leading_slash()
+ {
+ // Given
+ this.viewLocationConventions.Initialise(this.conventions);
+ var convention = this.conventions.ViewLocationConventions[1];
+
+ // When
+ var result = convention.Invoke(
+ "viewname",
+ null,
+ new ViewLocationContext { ModuleName = "modulename", ModulePath = "/modulepath" });
+
+ // Then
+ result.ShouldEqual("modulepath/modulename/viewname");
+ }
+
+ [Fact]
+ public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder_in_views_folder_when_modulepath_contains_leading_slash()
+ {
+ // Given
+ this.viewLocationConventions.Initialise(this.conventions);
+ var convention = this.conventions.ViewLocationConventions[0];
+
+ // When
+ var result = convention.Invoke(
+ "viewname",
+ null,
+ new ViewLocationContext { ModuleName = "modulename", ModulePath = "/modulepath" });
+
+ // Then
+ result.ShouldEqual("views/modulepath/modulename/viewname");
+ }
}
}
View
38 src/Nancy/Conventions/DefaultViewLocationConventions.cs
@@ -39,29 +39,59 @@ private static void ConfigureViewLocationConventions(NancyConventions convention
{
conventions.ViewLocationConventions = new List<Func<string, object, ViewLocationContext, string>>
{
+ // 0 Handles: views / *modulepath* / *modulename* / *viewname*
(viewName, model, viewLocationContext) =>{
- return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat("views/", viewLocationContext.ModulePath.TrimStart(new[] {'/'}), "/", viewName);
+ if (string.IsNullOrEmpty(viewLocationContext.ModulePath))
+ {
+ return string.Empty;
+ }
+
+ var path = viewLocationContext.ModulePath.TrimStart(new[] { '/' });
+
+ return string.Concat("views/", path, "/", viewLocationContext.ModuleName, "/", viewName);
},
- (viewName, model, viewLocationContext) => {
- return string.Concat("views/", viewLocationContext.ModuleName, "/", viewName);
+ // 1 Handles: *modulepath* / *modulename* / *viewname*
+ (viewName, model, viewLocationContext) =>{
+ if (string.IsNullOrEmpty(viewLocationContext.ModulePath))
+ {
+ return string.Empty;
+ }
+
+ var path = viewLocationContext.ModulePath.TrimStart(new[] { '/' });
+
+ return string.Concat(path, "/", viewLocationContext.ModuleName, "/", viewName);
},
+ // 2 Handles: views / *modulepath* / *viewname*
+ (viewName, model, viewLocationContext) =>{
+ return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat("views/", viewLocationContext.ModulePath.TrimStart(new[] {'/'}), "/", viewName);
+ },
+
+ // 3 Handles: *modulepath* / *viewname*
(viewName, model, viewLocationContext) =>{
return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat(viewLocationContext.ModulePath.TrimStart(new[] { '/' }), "/", viewName);
},
+ // 4 Handles: views / *modulename* / *viewname*
+ (viewName, model, viewLocationContext) => {
+ return string.Concat("views/", viewLocationContext.ModuleName, "/", viewName);
+ },
+
+ // 5 Handles: *modulename* / *viewname*
(viewName, model, viewLocationContext) => {
return string.Concat(viewLocationContext.ModuleName, "/", viewName);
},
+ // 6 Handles: views / *viewname*
(viewName, model, viewLocationContext) => {
return string.Concat("views/", viewName);
},
+ // 7 Handles: *viewname*
(viewName, model, viewLocationContext) => {
return viewName;
- },
+ }
};
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.