Created
November 15, 2012 15:53
-
-
Save ToJans/4079335 to your computer and use it in GitHub Desktop.
Too much protocol - I need better specs - feel free to fork & adjust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace SimpleMessaging | |
{ | |
public class MessageBox | |
{ | |
Func<dynamic,bool?> Handle; | |
public MessageBox() | |
{ | |
Handle = (msg) => null; | |
} | |
public void Subscribe(Predicate<dynamic> before = null,Predicate<dynamic> after = null) | |
{ | |
var previousHandler = this.Handle; | |
this.Handle = (msg)=> { | |
bool? result= null; | |
if (before!=null) | |
result = before(msg); | |
if (result==false) | |
result=previousHandler(msg); | |
else if (result!=true && after!=null) | |
result=after(msg); | |
return result; | |
}; | |
} | |
public bool Deliver(dynamic message) | |
{ | |
bool? handled = Publish(message); | |
switch(handled) | |
{ | |
case null: | |
throw new InvalidOperationException( | |
"Missing a handler for the following message:", message.ToJson()); | |
default: | |
return handled.Value; | |
} | |
} | |
public bool? Publish(dynamic message) | |
{ | |
return Handle(message); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using Shouldly; | |
namespace SimpleMessaging.Specs | |
{ | |
[TestClass] | |
public class Delivering_a_message_should_fail : | |
Given_a_messagebox_without_subscribers | |
{ | |
[TestInitialize] | |
[ExpectedException(typeof(InvalidOperationException))] | |
public void When_delivering_a_message() | |
{ | |
Succeeded = Sut.Deliver(Message); | |
} | |
} | |
[TestClass] | |
public class Publishing_a_message_should_succeed : | |
Given_a_messagebox_without_subscribers | |
{ | |
[TestInitialize] | |
public void When_publishing_a_message() | |
{ | |
Succeeded = Sut.Publish(Message); | |
} | |
[TestMethod] | |
public void The_publish_command_should_have_returned_null() | |
{ | |
Succeeded.ShouldBe(null); | |
} | |
} | |
[TestClass] | |
public class Delivering_a_message_succeed_if_there_is_a_receiver: | |
Given_a_messagebox_that_has_a_subscriber_for_the_message | |
{ | |
[TestInitialize] | |
public void When_delivering_a_message() | |
{ | |
Succeeded = Sut.Deliver(Message); | |
} | |
[TestMethod] | |
public void The_correct_message_should_be_received() | |
{ | |
ReceivedMessage.ShouldBe(Message); | |
} | |
[TestMethod] | |
public void The_deliver_command_should_have_returned_success() | |
{ | |
Succeeded.ShouldBe(true); | |
} | |
} | |
[TestClass] | |
public class Publishing_a_message_should_succeed_and_the_message_should_match : | |
Given_a_messagebox_that_has_a_subscriber_for_the_message | |
{ | |
[TestInitialize] | |
public void When_publishing_a_message() | |
{ | |
Succeeded = Sut.Publish(Message); | |
} | |
[TestMethod] | |
public void The_publish_command_should_have_returned_true() | |
{ | |
Succeeded.ShouldBe(true); | |
} | |
[TestMethod] | |
public void The_correct_message_should_be_received() | |
{ | |
ReceivedMessage.ShouldBe(Message); | |
} | |
} | |
public class Given_a_messagebox_without_subscribers | |
{ | |
public bool? Succeeded = null; | |
public class SayHello { public string Name = "Tom"; } | |
public SayHello Message = new SayHello(); | |
protected MessageBox Sut = new MessageBox(); | |
} | |
public class Given_a_messagebox_that_has_a_subscriber_for_the_message : | |
Given_a_messagebox_without_subscribers | |
{ | |
public SayHello ReceivedMessage = null; | |
public Given_a_messagebox_that_has_a_subscriber_for_the_message() | |
{ | |
Sut.Subscribe(x => { ReceivedMessage = x; return true; }); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment