Browse files

added new tracing to the BehaviorNode.Condition methods

  • Loading branch information...
1 parent c98bc4d commit 0fa4a2487d10c174f5db6211afb53b36c48033e1 @jeremydmiller jeremydmiller committed Mar 8, 2012
View
1 src/FubuMVC.Core/Behaviors/Conditional/ConditionalObjectDef.cs
@@ -14,6 +14,7 @@ public static class ConditionalObjectDef
public static ObjectDef ForModel<T>(Func<T, bool> filter) where T : class
{
var def = new ObjectDef(typeof (LambdaConditional<>), typeof (IFubuRequest));
+
Func<IFubuRequest, bool> match = request => filter(request.Get<T>());
def.DependencyByValue(match);
View
3 src/FubuMVC.Core/Registration/Diagnostics/Created.cs
@@ -2,8 +2,5 @@ namespace FubuMVC.Core.Registration.Diagnostics
{
public class Created : NodeEvent
{
- public Created(object subject) : base(subject)
- {
- }
}
}
View
33 src/FubuMVC.Core/Registration/Diagnostics/NodeEvent.cs
@@ -1,19 +1,40 @@
+using System;
+using FubuMVC.Core.Registration.ObjectGraph;
+
namespace FubuMVC.Core.Registration.Diagnostics
{
public abstract class NodeEvent
{
- private readonly object _subject;
+ public object Subject { get; set; }
+
+ public ConfigSource Source { get; set; }
+ }
+
+ public class ConditionAdded : NodeEvent
+ {
+ private readonly string _description;
+ private readonly Type _type;
- public NodeEvent(object subject)
+ public ConditionAdded(string description)
{
- _subject = subject;
+ _description = description;
}
- public object Subject
+ public ConditionAdded(Type type)
{
- get { return _subject; }
+ _type = type;
}
- public ConfigSource Source { get; set; }
+ public string Description
+ {
+ get { return _description; }
+ }
+
+ public Type Type
+ {
+ get {
+ return _type;
+ }
+ }
}
}
View
2 src/FubuMVC.Core/Registration/Diagnostics/Traced.cs
@@ -4,7 +4,7 @@ public class Traced : NodeEvent
{
private readonly string _text;
- public Traced(string text, object subject) : base(subject)
+ public Traced(string text)
{
_text = text;
}
View
9 src/FubuMVC.Core/Registration/Nodes/BehaviorNode.cs
@@ -5,6 +5,7 @@
using FubuCore;
using FubuMVC.Core.Behaviors;
using FubuMVC.Core.Behaviors.Conditional;
+using FubuMVC.Core.Registration.Diagnostics;
namespace FubuMVC.Core.Registration.Nodes
{
@@ -198,8 +199,9 @@ public Wrapper WrapWith(Type behaviorType, params Type[] parameterTypes)
/// Make the behavior *only* execute if the condition is met
/// </summary>
/// <param name = "condition"></param>
- public void Condition(Func<bool> condition)
+ public void Condition(Func<bool> condition, string description = "Anonymous")
{
+ Trace(new ConditionAdded(description));
_conditionalDef = ConditionalObjectDef.For(condition);
}
@@ -211,6 +213,8 @@ public void Condition(Func<bool> condition)
/// <param name = "condition"></param>
public void ConditionByService<T>(Func<T, bool> condition)
{
+ var description = "By Service: Func<{0}, bool>".ToFormat(typeof (T).Name);
+ Trace(new ConditionAdded(description));
_conditionalDef = ConditionalObjectDef.ForService(condition);
}
@@ -220,6 +224,8 @@ public void ConditionByService<T>(Func<T, bool> condition)
/// </summary>
public void ConditionByModel<T>(Func<T, bool> filter) where T : class
{
+ var description = "By Model: Func<{0}, bool>".ToFormat(typeof(T).Name);
+ Trace(new ConditionAdded(description));
_conditionalDef = ConditionalObjectDef.ForModel(filter);
}
@@ -229,6 +235,7 @@ public void ConditionByService<T>(Func<T, bool> condition)
/// </summary>
public void Condition<T>() where T : IConditional
{
+ Trace(new ConditionAdded(typeof (T)));
_conditionalDef = ConditionalObjectDef.For<T>();
}
View
5 src/FubuMVC.Core/Registration/Nodes/TracedNode.cs
@@ -11,17 +11,18 @@ public class TracedNode : ITracedModel
public TracedNode()
{
- Trace(new Created(this));
+ Trace(new Created());
}
public void Trace(NodeEvent @event)
{
_events.Enqueue(@event);
+ @event.Subject = this;
}
public void Trace(string text)
{
- _events.Enqueue(new Traced(text, this));
+ _events.Enqueue(new Traced(text));
}
IEnumerable<NodeEvent> ITracedModel.StagedEvents
View
50 src/FubuMVC.Tests/Registration/Diagnostics/BehaviorNodeTracingMechanicsTester.cs
@@ -1,5 +1,7 @@
+using System;
using System.Collections.Generic;
using FubuCore;
+using FubuMVC.Core.Behaviors.Conditional;
using FubuMVC.Core.Registration.Diagnostics;
using FubuMVC.Core.Registration.Nodes;
using FubuMVC.Core.Security;
@@ -55,5 +57,53 @@ public void recording_events_removes_the_events_from_the_node_and_calls_back()
list.Last().ShouldBeOfType<Traced>().Text.ShouldEqual("something");
}
+
+ [Test]
+ public void add_condition_with_description()
+ {
+ theNode.Condition(() => true, "wacky");
+
+ theTracedNode.StagedEvents.Last().ShouldBeOfType<ConditionAdded>()
+ .Description.ShouldEqual("wacky");
+
+
+ }
+
+ [Test]
+ public void add_condition_by_function_against_service()
+ {
+ theNode.ConditionByService<Something>(x => true);
+
+ theTracedNode.StagedEvents.Last().ShouldBeOfType<ConditionAdded>()
+ .Description.ShouldEqual("By Service: Func<Something, bool>");
+ }
+
+ [Test]
+ public void add_condition_by_function_against_model()
+ {
+ theNode.ConditionByModel<Something>(x => true);
+
+ theTracedNode.StagedEvents.Last().ShouldBeOfType<ConditionAdded>()
+ .Description.ShouldEqual("By Model: Func<Something, bool>");
+ }
+
+ [Test]
+ public void add_condition_by_type()
+ {
+ theNode.Condition<SomethingCondition>();
+
+ theTracedNode.StagedEvents.Last().ShouldBeOfType<ConditionAdded>()
+ .Type.ShouldEqual(typeof (SomethingCondition));
+ }
+
+ public class SomethingCondition : IConditional
+ {
+ public bool ShouldExecute()
+ {
+ throw new NotImplementedException();
+ }
+ }
+
+ public class Something{}
}
}

0 comments on commit 0fa4a24

Please sign in to comment.