Skip to content

Commit

Permalink
Merge branch 'spark-view-tokens' of https://github.com/jmarnold/fubumvc
Browse files Browse the repository at this point in the history
… into jmarnold-spark-view-tokens
  • Loading branch information
RobertTheGrey committed Jan 15, 2011
2 parents b6a805c + 52d93ec commit f6f10cf
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 126 deletions.
Original file line number Original file line Diff line number Diff line change
@@ -1,103 +1,142 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using FubuMVC.Core.Registration.Nodes; using FubuMVC.Core.Registration.Nodes;
using FubuMVC.Core.View; using FubuMVC.Core.View;
using NUnit.Framework; using NUnit.Framework;
using Rhino.Mocks; using Rhino.Mocks;
using Spark.Web.FubuMVC.Registration; using Spark.Web.FubuMVC.Registration;
using Spark.Web.FubuMVC.ViewCreation; using Spark.Web.FubuMVC.ViewCreation;


namespace Spark.Web.FubuMVC.Tests.Registration namespace Spark.Web.FubuMVC.Tests.Registration
{ {
[TestFixture] [TestFixture]
public class when_applying_action_and_view_filter : InteractionContext<ActionAndViewMatchedBySparkViewDescriptors> public class when_applying_action_and_view_filter : InteractionContext<ActionAndViewMatchedBySparkViewDescriptors>
{ {
private ActionCall _call; private ActionCall _call;
private SparkViewDescriptor _desciptor; private SparkViewDescriptor _descriptor;
private ViewBag _views; private ViewBag _views;

private SparkViewToken _token;
protected override void beforeEach()
{ protected override void beforeEach()
_call = ActionCall.For<SampleEndpoint>(e => e.Get(new SampleInput())); {
_desciptor = new SparkViewDescriptor _call = ActionCall.For<SampleEndpoint>(e => e.Get(new SampleInput()));
{ _descriptor = newDescriptor();
Accessors = new List<SparkViewDescriptor.Accessor>(),
Templates = new List<string>() _token = new SparkViewToken(_call, _descriptor, "Sample", "Sample");
}; _token.Descriptors.Add(_descriptor);
var token = new SparkViewToken(_call, _desciptor, "Sample", "Sample");
token.Descriptors.Add(_desciptor); _views = new ViewBag(new List<IViewToken> { _token });

}
_views = new ViewBag(new List<IViewToken> { token });
} protected SparkViewDescriptor newDescriptor()

{
[Test] return new SparkViewDescriptor
public void should_return_empty_collection_when_action_call_returns_a_fubu_continuation() {
{ Accessors = new List<SparkViewDescriptor.Accessor>(),
var call = ActionCall.For<SampleEndpoint>(e => e.Post()); Templates = new List<string>()

};
ClassUnderTest }
.Apply(call, _views)
.ShouldHaveCount(0); [Test]
} public void should_return_empty_collection_when_action_call_returns_a_fubu_continuation()

{
[Test] var call = ActionCall.For<SampleEndpoint>(e => e.Post());
public void should_return_empty_collection_when_resolver_has_no_matching_policies()
{ ClassUnderTest
MockFor<ISparkPolicyResolver>() .Apply(call, _views)
.Expect(resolver => resolver.HasMatchFor(_call)) .ShouldHaveCount(0);
.Return(false); }


ClassUnderTest [Test]
.Apply(_call, _views) public void should_return_empty_collection_when_resolver_has_no_matching_policies()
.ShouldHaveCount(0); {
} MockFor<ISparkPolicyResolver>()

.Expect(resolver => resolver.HasMatchFor(_call))
[Test] .Return(false);
public void should_return_empty_collection_when_no_desciptors_are_matched()
{ ClassUnderTest
MockFor<ISparkPolicyResolver>() .Apply(_call, _views)
.Expect(resolver => resolver.HasMatchFor(_call)) .ShouldHaveCount(0);
.Return(true); }


MockFor<ISparkPolicyResolver>() [Test]
.Expect(resolver => resolver.ResolveViewLocator(_call)) public void should_return_empty_collection_when_no_desciptors_are_matched()
.Return(""); {

MockFor<ISparkPolicyResolver>()
MockFor<ISparkPolicyResolver>() .Expect(resolver => resolver.HasMatchFor(_call))
.Expect(resolver => resolver.ResolveViewName(_call)) .Return(true);
.Return("");

MockFor<ISparkPolicyResolver>()
ClassUnderTest .Expect(resolver => resolver.ResolveViewLocator(_call))
.Apply(_call, _views) .Return("");
.ShouldHaveCount(0);
} MockFor<ISparkPolicyResolver>()

.Expect(resolver => resolver.ResolveViewName(_call))
[Test] .Return("");
public void should_return_single_view_token_when_desciptor_is_matched()
{ ClassUnderTest
const string viewLocator = "TestFolder"; .Apply(_call, _views)
const string viewName = "Sample"; .ShouldHaveCount(0);

}
_desciptor.Templates.Add(viewLocator + "\\" + viewName);

[Test]
MockFor<ISparkPolicyResolver>() public void should_return_single_view_token_when_desciptor_is_matched()
.Expect(resolver => resolver.HasMatchFor(_call)) {
.Return(true); _descriptor.Templates.Add("TestFolder\\Sample.spark");


MockFor<ISparkPolicyResolver>() MockFor<ISparkPolicyResolver>()
.Expect(resolver => resolver.ResolveViewLocator(_call)) .Expect(resolver => resolver.HasMatchFor(_call))
.Return(viewLocator); .Return(true);


MockFor<ISparkPolicyResolver>() MockFor<ISparkPolicyResolver>()
.Expect(resolver => resolver.ResolveViewName(_call)) .Expect(resolver => resolver.ResolveViewLocator(_call))
.Return(viewName); .Return("TestFolder");


var token = ClassUnderTest MockFor<ISparkPolicyResolver>()
.Apply(_call, _views) .Expect(resolver => resolver.ResolveViewName(_call))
.FirstOrDefault(); .Return("Sample");


token.ShouldNotBeNull(); var token = ClassUnderTest
token.Name.ShouldEqual(viewName); .Apply(_call, _views)
} .FirstOrDefault();
}
token.ShouldNotBeNull();
token.Name.ShouldEqual("Sample");
}

[Test]
public void should_return_single_view_token_matched_on_policy_criteria()
{
_descriptor.Templates.Add("Projects\\Dashboard.spark");
_descriptor.Templates.Add("Shared\\Application.spark");

var descriptor2 = newDescriptor();
descriptor2.Templates.Add("Projects\\StoryMap.spark");
descriptor2.Templates.Add("Shared\\Application.spark");
_token.Descriptors.Add(descriptor2);

var descriptor3 = newDescriptor();
descriptor3.Templates.Add("Projects.spark");
descriptor3.Templates.Add("Shared\\Application.spark");
_token.Descriptors.Add(descriptor3);


MockFor<ISparkPolicyResolver>()
.Expect(resolver => resolver.HasMatchFor(_call))
.Return(true);

MockFor<ISparkPolicyResolver>()
.Expect(resolver => resolver.ResolveViewLocator(_call))
.Return("");

MockFor<ISparkPolicyResolver>()
.Expect(resolver => resolver.ResolveViewName(_call))
.Return("Projects");

ClassUnderTest
.Apply(_call, _views)
.FirstOrDefault()
.ShouldNotBeNull();
}
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
using FubuCore; using FubuCore;
using FubuMVC.Core.Continuations; using FubuMVC.Core.Continuations;
using FubuMVC.Core.Registration.Nodes; using FubuMVC.Core.Registration.Nodes;
using FubuMVC.Core.View; using FubuMVC.Core.View;
using Spark.Web.FubuMVC.Extensions;
using Spark.Web.FubuMVC.ViewCreation; using Spark.Web.FubuMVC.ViewCreation;


namespace Spark.Web.FubuMVC.Registration namespace Spark.Web.FubuMVC.Registration
Expand All @@ -24,31 +25,32 @@ public IEnumerable<IViewToken> Apply(ActionCall call, ViewBag views)
return new IViewToken[0]; return new IViewToken[0];
} }


string viewName = _policyResolver.ResolveViewName(call); var viewName = _policyResolver.ResolveViewName(call);
string viewLocatorName = _policyResolver.ResolveViewLocator(call); var viewLocatorName = _policyResolver.ResolveViewLocator(call);
IEnumerable<SparkViewToken> allViewTokens = var allViewTokens = views
views.Views.Where(view => .Views
view.GetType().CanBeCastTo<SparkViewToken>()).Cast<SparkViewToken>(); .Where(view => view.GetType().CanBeCastTo<SparkViewToken>())

.Cast<SparkViewToken>();
SparkViewDescriptor matchedDescriptor = null;
allViewTokens.FirstOrDefault( SparkViewDescriptor matchedDescriptor = null;
token => foreach(var token in allViewTokens)
{ {
matchedDescriptor = token.Descriptors var view = viewName.RemoveSuffix(".spark");
.Where(e => e.Templates var templatePath = !string.IsNullOrEmpty(viewLocatorName) ? "{0}\\{1}".ToFormat(viewLocatorName, view) : view;
.Any(template => template.Contains(viewLocatorName) && template.Contains(viewName))) var descriptor = token
.SingleOrDefault(); .Descriptors
.FirstOrDefault(d => d.Templates.Any(template => template.RemoveSuffix(".spark").Equals(templatePath)));
return matchedDescriptor != null;
}); if(descriptor != null)

{

matchedDescriptor = descriptor;
IEnumerable<IViewToken> viewsBoundToActions = break;
matchedDescriptor != null }
}

return matchedDescriptor != null
? new IViewToken[] { new SparkViewToken(call, matchedDescriptor, viewLocatorName, viewName) } ? new IViewToken[] { new SparkViewToken(call, matchedDescriptor, viewLocatorName, viewName) }
: new IViewToken[0]; : new IViewToken[0];

return viewsBoundToActions;
} }
} }
} }

0 comments on commit f6f10cf

Please sign in to comment.