Skip to content

Commit

Permalink
Merge remote branch 'nieve/tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
chadmyers committed May 25, 2010
2 parents 6a0497d + a04cd43 commit 3e65b1b
Show file tree
Hide file tree
Showing 14 changed files with 439 additions and 18 deletions.
11 changes: 11 additions & 0 deletions src/FubuCore.Testing/GenericEnumerableExtensionsTester.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;

Expand Down Expand Up @@ -72,5 +73,15 @@ public void add_many_and_range()

list.ShouldHaveTheSameElementsAs("a", "b", "c");
}

[Test]
public void remove_all()
{
var list = new List<string> { "a", "c", "b" };
list.ShouldHaveCount(3);
Func<string, bool> whereEvaluator = item => item.CompareTo("c") < 0;
list.RemoveAll(whereEvaluator);
list.ShouldHaveCount(1).ShouldContain("c");
}
}
}
1 change: 0 additions & 1 deletion src/FubuMVC.Core/View/WebForms/WebFormsEndpointPolicy.cs
Expand Up @@ -6,7 +6,6 @@

namespace FubuMVC.Core.View.WebForms
{
// TODO: Test for this turkey
// Find any action w/o an output, look for the WebFormEndpoint att
public class WebFormsEndpointPolicy : IConfigurationAction
{
Expand Down
40 changes: 40 additions & 0 deletions src/FubuMVC.Tests/AjaxExtensionsTester.cs
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Web;
using NUnit.Framework;
using FubuMVC.Core;

namespace FubuMVC.Tests
{
[TestFixture]
public class AjaxExtensionsTester
{
private HttpContext _ajaxContext;
private HttpContext _nonAjaxContext;
private IDictionary<string, object> _ajaxRequestInput = new Dictionary<string, object> { { "X-Requested-With", "XMLHttpRequest" } };
private IDictionary<string, object> _nonAjaxRequestInput = new Dictionary<string, object> { { "X-Requested-With", "some_value" } };

[SetUp]
public void SetUp()
{
var request = new HttpRequest("foo.txt", "http://test", "X-Requested-With=XMLHttpRequest");
_ajaxContext = new HttpContext(request, new HttpResponse(Console.Out));
var nonAjaxRequest = new HttpRequest("foo.txt", "http://test", "X-Requested-With=some_value");
_nonAjaxContext = new HttpContext(nonAjaxRequest, new HttpResponse(Console.Out));
}

[Test]
public void is_http_context_an_ajax_request()
{
_ajaxContext.IsAjaxRequest().ShouldBeTrue();
_nonAjaxContext.IsAjaxRequest().ShouldBeFalse();
}

[Test]
public void is_dictionary_input_an_ajax_request()
{
_ajaxRequestInput.IsAjaxRequest().ShouldBeTrue();
_nonAjaxRequestInput.IsAjaxRequest().ShouldBeFalse();
}
}
}
80 changes: 80 additions & 0 deletions src/FubuMVC.Tests/Behaviors/BasicBehaviorTester.cs
@@ -0,0 +1,80 @@
using FubuMVC.Core;
using FubuMVC.Core.Behaviors;
using FubuMVC.Core.Runtime;
using FubuMVC.Core.View;
using FubuMVC.UI;
using NUnit.Framework;
using Rhino.Mocks;

namespace FubuMVC.Tests.Behaviors
{
[TestFixture]
public class BasicBehaviorTester
{
public static IPartialInvokingHandler PartialInvokingHandler;
private IFubuPage _page;
private IPartialFactory _partialFactory;
private BasicBehavior _behavior;
private IFubuRequest _request;

public class PartialHandlingBehavior : IActionBehavior
{
public void Invoke()
{

}

public void InvokePartial()
{
PartialInvokingHandler.Invoke();
}
}
public interface IPartialInvokingHandler{void Invoke();}
public class FakeController
{
public void SomeAction() { }
}
public class WrappingBehavior : BasicBehavior
{
public WrappingBehavior()
: base(PartialBehavior.Ignored)
{
}

public DoNext PerformInvoke()
{
return base.performInvoke();
}
}
public class InputModel { }

[SetUp]
public void SetUp()
{
_page = MockRepository.GenerateStub<IFubuPage>();
_partialFactory = MockRepository.GenerateStub<IPartialFactory>();
_behavior = new WrappingBehavior();
PartialInvokingHandler = MockRepository.GenerateStub<IPartialInvokingHandler>();
_behavior.InsideBehavior = new PartialHandlingBehavior();

_partialFactory.Stub(f => f.BuildPartial(typeof(InputModel))).Return(_behavior);
_page.Stub(p => p.Get<IPartialFactory>()).Return(_partialFactory);

_request = MockRepository.GenerateStub<IFubuRequest>();
_page.Stub(p => p.Get<IFubuRequest>()).Return(_request);
}

[Test]
public void should_invoke_partial_inside_behavior_by_default_even_when_partial_behavior_does_not_execute()
{
_page.Partial<InputModel>();
PartialInvokingHandler.AssertWasCalled(h=>h.Invoke());
}

[Test]
public void perform_invoke_should_return_continue()
{
new WrappingBehavior().PerformInvoke().ShouldEqual(DoNext.Continue);
}
}
}
15 changes: 15 additions & 0 deletions src/FubuMVC.Tests/Behaviors/LoadCurrentPrincipalTester.cs
Expand Up @@ -58,5 +58,20 @@ public void should_not_execute_in_partial_invoke()

_context.AssertWasNotCalled(x => x.IsAuthenticated());
}

[Test]
public void invoke_should_attach_principal_and_continue()
{
IActionBehavior insideBehavior = MockRepository.GenerateStub<IActionBehavior>();
_behavior.InsideBehavior = insideBehavior;
var expectedUser = MockRepository.GenerateStub<IPrincipal>();
_context.Stub(c => c.IsAuthenticated()).Return(true);
_factory.Stub(f => f.CreatePrincipal(_identity)).Return(expectedUser);

_behavior.Invoke();

_context.CurrentUser.ShouldBeTheSameAs(expectedUser);
insideBehavior.AssertWasCalled(b=>b.Invoke());
}
}
}
28 changes: 28 additions & 0 deletions src/FubuMVC.Tests/Behaviors/RenderTextBehaviorTester.cs
Expand Up @@ -35,6 +35,34 @@ public void should_write_the_to_string_render_of_the_type_to_the_output_writer()
writer.AssertWasCalled(x => x.Write(MimeType.Html.ToString(), "some text"));
}
}
[TestFixture]
public class RenderHtmlBehaviorTester
{
#region Setup/Teardown

[SetUp]
public void SetUp()
{
request = new InMemoryFubuRequest();
writer = MockRepository.GenerateMock<IOutputWriter>();

request.Set("some html");

var behavior = new RenderHtmlBehavior(writer, request);
behavior.Invoke();
}

#endregion

private InMemoryFubuRequest request;
private IOutputWriter writer;

[Test]
public void should_write_the_to_string_render_of_the_type_to_the_output_writer()
{
writer.AssertWasCalled(x => x.Write(MimeType.Html.ToString(), "some html"));
}
}

[TestFixture]
public class IntegratedTestOfRenderTextBehavior
Expand Down
92 changes: 79 additions & 13 deletions src/FubuMVC.Tests/Continuations/ContinuationTester.cs
@@ -1,7 +1,10 @@
using System;
using FubuMVC.Core;
using FubuMVC.Core.Behaviors;
using FubuMVC.Core.Continuations;
using FubuMVC.Core.Registration.Nodes;
using FubuMVC.Core.Runtime;
using FubuMVC.Core.Urls;
using FubuMVC.Tests.Registration;
using NUnit.Framework;
using Rhino.Mocks;
Expand All @@ -28,6 +31,16 @@ private void shouldFail(Action action)
Exception<FubuAssertionException>.ShouldBeThrownBy(action);
}

[Test]
public void Continue_just_continues()
{
FubuContinuation continuation = FubuContinuation.NextBehavior();
continuation.Type.ShouldEqual(ContinuationType.NextBehavior);

continuation.Process(director);
director.AssertWasCalled(x => x.InvokeNextBehavior());
}


[Test]
public void assert_continued()
Expand Down Expand Up @@ -59,7 +72,7 @@ public void assert_redirect_to_a_method()
[Test]
public void assert_redirect_to_a_target()
{
var input = new InputModelWithEquals{Name = "Luke"};
var input = new InputModelWithEquals {Name = "Luke"};
FubuContinuation continuation = FubuContinuation.RedirectTo(input);

continuation.AssertWasRedirectedTo(new InputModelWithEquals {Name = "Luke"});
Expand All @@ -72,6 +85,20 @@ public void assert_redirect_to_a_target()
shouldFail(() => continuation.AssertWasRedirectedTo<ControllerTarget>(x => x.OneInOneOut(null)));
}

[Test]
public void when_destination_null_redirect_to_throws()
{
var argNullEx = typeof (ArgumentNullException).ShouldBeThrownBy(() => FubuContinuation.RedirectTo(null));
argNullEx.Message.ShouldContain("destination");
}

[Test]
public void when_destination_null_transfer_to_throws()
{
var argNullEx = typeof (ArgumentNullException).ShouldBeThrownBy(() => FubuContinuation.TransferTo(null));
argNullEx.Message.ShouldContain("destination");
}

[Test]
public void assert_transfer_to_a_method()
{
Expand All @@ -90,10 +117,10 @@ public void assert_transfer_to_a_method()
[Test]
public void assert_transfer_to_a_target()
{
var input = new InputModelWithEquals{Name="Luke"};
var input = new InputModelWithEquals {Name = "Luke"};
FubuContinuation continuation = FubuContinuation.TransferTo(input);

continuation.AssertWasTransferedTo(new InputModelWithEquals { Name = "Luke" });
continuation.AssertWasTransferedTo(new InputModelWithEquals {Name = "Luke"});

shouldFail(() => continuation.AssertWasTransferedTo(new InputModelWithEquals()));
shouldFail(() => continuation.AssertWasRedirectedTo(input));
Expand All @@ -103,16 +130,6 @@ public void assert_transfer_to_a_target()
shouldFail(() => continuation.AssertWasRedirectedTo<ControllerTarget>(x => x.OneInOneOut(null)));
}

[Test]
public void Continue_just_continues()
{
FubuContinuation continuation = FubuContinuation.NextBehavior();
continuation.Type.ShouldEqual(ContinuationType.NextBehavior);

continuation.Process(director);
director.AssertWasCalled(x => x.InvokeNextBehavior());
}

[Test]
public void redirect_to_a_method()
{
Expand Down Expand Up @@ -167,5 +184,54 @@ public void transfer_to_a_target()

director.AssertWasCalled(x => x.TransferTo(input));
}

[Test]
public void transfer_to_null_throws()
{
var urlRegistry = MockRepository.GenerateStub<IUrlRegistry>();
var outputWriter = MockRepository.GenerateStub<IOutputWriter>();
var fubuRequest = MockRepository.GenerateStub<IFubuRequest>();
var partialFactory = MockRepository.GenerateStub<IPartialFactory>();
var handler = new ContinuationHandler(urlRegistry, outputWriter, fubuRequest, partialFactory);

var exception =
typeof (ArgumentNullException).ShouldBeThrownBy(() => handler.TransferTo(null)) as ArgumentNullException;
exception.ShouldNotBeNull();
exception.ParamName.ShouldEqual("input");
}

[Test]
public void perform_invoke_processes_handler()
{
//Arrange
var urlRegistry = MockRepository.GenerateStub<IUrlRegistry>();
var outputWriter = MockRepository.GenerateStub<IOutputWriter>();
var fubuRequest = MockRepository.GenerateStub<IFubuRequest>();
var continuation = FubuContinuation.TransferTo(new object());
fubuRequest.Stub(r => r.Get<FubuContinuation>()).Return(continuation);
var partialFactory = MockRepository.GenerateStub<IPartialFactory>();
var partialBehavior = MockRepository.GenerateStub<IActionBehavior>();
partialFactory.Stub(f => f.BuildPartial(typeof(object))).Return(partialBehavior);
var handler = new ContinuationHandler(urlRegistry, outputWriter, fubuRequest, partialFactory);
var insideBehavior = MockRepository.GenerateStub<IActionBehavior>();
handler.InsideBehavior = insideBehavior;

//Act
handler.Invoke();

//Assert TransferTo was called by _request.Get<FubuContinuation>().Process(this);
partialFactory.AssertWasCalled(f=>f.BuildPartial(typeof(object)));
partialBehavior.AssertWasCalled(p=>p.InvokePartial());
//Assert performInvoke() returned Stop
insideBehavior.AssertWasNotCalled(b=>b.Invoke());
}

[Test]
public void destination_returns_destination_object()
{
var destination = new object();
var continuation = FubuContinuation.TransferTo(destination);
continuation.Destination<object>().ShouldEqual(destination);
}
}
}
8 changes: 6 additions & 2 deletions src/FubuMVC.Tests/FubuBootstrapperIntegrationTester.cs
Expand Up @@ -37,6 +37,9 @@ public void SetUp()
x.Route<InputModel>("area/sub3/{Name}/{Age}")
.Calls<TestController>(c => c.AnotherAction(null)).OutputToJson();
x.Route("area/sub4/some_pattern")
.Calls<TestController>(c => c.AnotherAction(null)).OutputToJson();
});

container = new Container();
Expand Down Expand Up @@ -71,21 +74,22 @@ public void each_route_can_resolve_its_behavior()
[Test]
public void should_have_a_route_in_the_RouteCollection_with_a_Fubu_RouteHandler_for_each_route_in_the_registry()
{
routes.Count.ShouldEqual(5);
routes.Count.ShouldEqual(6);
routes.Each(x => x.ShouldBeOfType<Route>().RouteHandler.ShouldBeOfType<FubuRouteHandler>());
}

[Test]
public void should_have_registered_behaviors_in_the_container()
{
container.GetAllInstances<IActionBehavior>().Count.ShouldEqual(5);
container.GetAllInstances<IActionBehavior>().Count.ShouldEqual(6);
}

[Test]
public void should_register_routes_in_order_of_the_number_of_their_inputs()
{
routes.OfType<Route>().Select(r => r.Url).ShouldHaveTheSameElementsAs(
"area/sub2/prop",
"area/sub4/some_pattern",
"area/sub2/{Name}",
"area/sub/{Name}/{Age}",
"area/sub2/{Name}/{Age}",
Expand Down

0 comments on commit 3e65b1b

Please sign in to comment.