Browse files

Merge branch 'spark-view-tokens' of https://github.com/jmarnold/fubumvc

… into jmarnold-spark-view-tokens
  • Loading branch information...
2 parents b6a805c + 52d93ec commit f6f10cf513636b3814e56c618c635e3d6f253b28 @RobertTheGrey committed Jan 15, 2011
View
243 src/Spark.Web.FubuMVC.Tests/Registration/when_applying_action_and_view_filter.cs
@@ -1,103 +1,142 @@
-using System.Collections.Generic;
-using System.Linq;
-using FubuMVC.Core.Registration.Nodes;
-using FubuMVC.Core.View;
-using NUnit.Framework;
-using Rhino.Mocks;
-using Spark.Web.FubuMVC.Registration;
-using Spark.Web.FubuMVC.ViewCreation;
-
-namespace Spark.Web.FubuMVC.Tests.Registration
-{
- [TestFixture]
- public class when_applying_action_and_view_filter : InteractionContext<ActionAndViewMatchedBySparkViewDescriptors>
- {
- private ActionCall _call;
- private SparkViewDescriptor _desciptor;
- private ViewBag _views;
-
- protected override void beforeEach()
- {
- _call = ActionCall.For<SampleEndpoint>(e => e.Get(new SampleInput()));
- _desciptor = new SparkViewDescriptor
- {
- Accessors = new List<SparkViewDescriptor.Accessor>(),
- Templates = new List<string>()
- };
- var token = new SparkViewToken(_call, _desciptor, "Sample", "Sample");
- token.Descriptors.Add(_desciptor);
-
- _views = new ViewBag(new List<IViewToken> { token });
- }
-
- [Test]
- public void should_return_empty_collection_when_action_call_returns_a_fubu_continuation()
- {
- var call = ActionCall.For<SampleEndpoint>(e => e.Post());
-
- ClassUnderTest
- .Apply(call, _views)
- .ShouldHaveCount(0);
- }
-
- [Test]
- public void should_return_empty_collection_when_resolver_has_no_matching_policies()
- {
- MockFor<ISparkPolicyResolver>()
- .Expect(resolver => resolver.HasMatchFor(_call))
- .Return(false);
-
- ClassUnderTest
- .Apply(_call, _views)
- .ShouldHaveCount(0);
- }
-
- [Test]
- public void should_return_empty_collection_when_no_desciptors_are_matched()
- {
- 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("");
-
- ClassUnderTest
- .Apply(_call, _views)
- .ShouldHaveCount(0);
- }
-
- [Test]
- public void should_return_single_view_token_when_desciptor_is_matched()
- {
- const string viewLocator = "TestFolder";
- const string viewName = "Sample";
-
- _desciptor.Templates.Add(viewLocator + "\\" + viewName);
-
- MockFor<ISparkPolicyResolver>()
- .Expect(resolver => resolver.HasMatchFor(_call))
- .Return(true);
-
- MockFor<ISparkPolicyResolver>()
- .Expect(resolver => resolver.ResolveViewLocator(_call))
- .Return(viewLocator);
-
- MockFor<ISparkPolicyResolver>()
- .Expect(resolver => resolver.ResolveViewName(_call))
- .Return(viewName);
-
- var token = ClassUnderTest
- .Apply(_call, _views)
- .FirstOrDefault();
-
- token.ShouldNotBeNull();
- token.Name.ShouldEqual(viewName);
- }
- }
+using System.Collections.Generic;
+using System.Linq;
+using FubuMVC.Core.Registration.Nodes;
+using FubuMVC.Core.View;
+using NUnit.Framework;
+using Rhino.Mocks;
+using Spark.Web.FubuMVC.Registration;
+using Spark.Web.FubuMVC.ViewCreation;
+
+namespace Spark.Web.FubuMVC.Tests.Registration
+{
+ [TestFixture]
+ public class when_applying_action_and_view_filter : InteractionContext<ActionAndViewMatchedBySparkViewDescriptors>
+ {
+ private ActionCall _call;
+ private SparkViewDescriptor _descriptor;
+ private ViewBag _views;
+ private SparkViewToken _token;
+
+ protected override void beforeEach()
+ {
+ _call = ActionCall.For<SampleEndpoint>(e => e.Get(new SampleInput()));
+ _descriptor = newDescriptor();
+
+ _token = new SparkViewToken(_call, _descriptor, "Sample", "Sample");
+ _token.Descriptors.Add(_descriptor);
+
+ _views = new ViewBag(new List<IViewToken> { _token });
+ }
+
+ protected SparkViewDescriptor newDescriptor()
+ {
+ return new SparkViewDescriptor
+ {
+ Accessors = new List<SparkViewDescriptor.Accessor>(),
+ Templates = new List<string>()
+ };
+ }
+
+ [Test]
+ public void should_return_empty_collection_when_action_call_returns_a_fubu_continuation()
+ {
+ var call = ActionCall.For<SampleEndpoint>(e => e.Post());
+
+ ClassUnderTest
+ .Apply(call, _views)
+ .ShouldHaveCount(0);
+ }
+
+ [Test]
+ public void should_return_empty_collection_when_resolver_has_no_matching_policies()
+ {
+ MockFor<ISparkPolicyResolver>()
+ .Expect(resolver => resolver.HasMatchFor(_call))
+ .Return(false);
+
+ ClassUnderTest
+ .Apply(_call, _views)
+ .ShouldHaveCount(0);
+ }
+
+ [Test]
+ public void should_return_empty_collection_when_no_desciptors_are_matched()
+ {
+ 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("");
+
+ ClassUnderTest
+ .Apply(_call, _views)
+ .ShouldHaveCount(0);
+ }
+
+ [Test]
+ public void should_return_single_view_token_when_desciptor_is_matched()
+ {
+ _descriptor.Templates.Add("TestFolder\\Sample.spark");
+
+ MockFor<ISparkPolicyResolver>()
+ .Expect(resolver => resolver.HasMatchFor(_call))
+ .Return(true);
+
+ MockFor<ISparkPolicyResolver>()
+ .Expect(resolver => resolver.ResolveViewLocator(_call))
+ .Return("TestFolder");
+
+ MockFor<ISparkPolicyResolver>()
+ .Expect(resolver => resolver.ResolveViewName(_call))
+ .Return("Sample");
+
+ var token = ClassUnderTest
+ .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();
+ }
+ }
}
View
50 src/Spark.Web.FubuMVC/Registration/ActionAndViewMatchedBySparkViewDescriptors.cs
@@ -3,7 +3,8 @@
using FubuCore;
using FubuMVC.Core.Continuations;
using FubuMVC.Core.Registration.Nodes;
-using FubuMVC.Core.View;
+using FubuMVC.Core.View;
+using Spark.Web.FubuMVC.Extensions;
using Spark.Web.FubuMVC.ViewCreation;
namespace Spark.Web.FubuMVC.Registration
@@ -24,31 +25,32 @@ public IEnumerable<IViewToken> Apply(ActionCall call, ViewBag views)
return new IViewToken[0];
}
- string viewName = _policyResolver.ResolveViewName(call);
- string viewLocatorName = _policyResolver.ResolveViewLocator(call);
- IEnumerable<SparkViewToken> allViewTokens =
- views.Views.Where(view =>
- view.GetType().CanBeCastTo<SparkViewToken>()).Cast<SparkViewToken>();
-
- SparkViewDescriptor matchedDescriptor = null;
- allViewTokens.FirstOrDefault(
- token =>
- {
- matchedDescriptor = token.Descriptors
- .Where(e => e.Templates
- .Any(template => template.Contains(viewLocatorName) && template.Contains(viewName)))
- .SingleOrDefault();
-
- return matchedDescriptor != null;
- });
-
-
- IEnumerable<IViewToken> viewsBoundToActions =
- matchedDescriptor != null
+ var viewName = _policyResolver.ResolveViewName(call);
+ var viewLocatorName = _policyResolver.ResolveViewLocator(call);
+ var allViewTokens = views
+ .Views
+ .Where(view => view.GetType().CanBeCastTo<SparkViewToken>())
+ .Cast<SparkViewToken>();
+
+ SparkViewDescriptor matchedDescriptor = null;
+ foreach(var token in allViewTokens)
+ {
+ var view = viewName.RemoveSuffix(".spark");
+ var templatePath = !string.IsNullOrEmpty(viewLocatorName) ? "{0}\\{1}".ToFormat(viewLocatorName, view) : view;
+ var descriptor = token
+ .Descriptors
+ .FirstOrDefault(d => d.Templates.Any(template => template.RemoveSuffix(".spark").Equals(templatePath)));
+
+ if(descriptor != null)
+ {
+ matchedDescriptor = descriptor;
+ break;
+ }
+ }
+
+ return matchedDescriptor != null
? new IViewToken[] { new SparkViewToken(call, matchedDescriptor, viewLocatorName, viewName) }
: new IViewToken[0];
-
- return viewsBoundToActions;
}
}
}

0 comments on commit f6f10cf

Please sign in to comment.