Skip to content

Instantly share code, notes, and snippets.

@NikiforovAll
Created June 7, 2019 14:28
Show Gist options
  • Save NikiforovAll/7317a1315a966b82927622fb90c2f7fb to your computer and use it in GitHub Desktop.
Save NikiforovAll/7317a1315a966b82927622fb90c2f7fb to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace Mediator {
/// <summary>
/// The 'Mediator' abstract class
/// </summary>
public abstract class AbstractChatroom {
public abstract void Register (User user);
public abstract void Send (string from, string to, string message);
}
}
namespace Mediator {
/// <summary>
/// The 'ConcreteMediator' class
/// </summary>
public class Chatroom : AbstractChatroom {
private Dictionary<string, User> _users = new Dictionary<string, User> ();
public override void Register (User user) {
if (!_users.ContainsValue (user)) {
_users[user.Name] = user;
}
user.Chatroom = this;
}
public override void Send (string from, string to, string message) {
User participant = _users[to];
if (participant != null) {
participant.Receive (from, message);
} else {
throw new KeyNotFoundException ("User not found");
}
}
}
}
namespace Mediator {
public class User {
public User (string name) {
Name = name;
}
public Chatroom Chatroom { get; set; }
public string Name { get; }
public Stack < (string source, string message) > MessageCache { get; private set; } = new Stack < (string source, string message) > ();
// it is also possible to create interface for recipient aka 'AbstractColleague'
public void Receive (string from, string message) {
MessageCache.Push ((source: from, message: message));
}
}
}
using System;
using System.Collections.Generic;
using Mediator;
public class Program
{
public static void Main()
{
var users = new[]
{
new User(name: "Foo1"),
new User(name: "Foo2"),
new User(name: "Foo3")
};
(string from, string to, string message)[] messagePlan =
{
("Foo1", "Foo2", "msg1"),
("Foo2", "Foo3", "msg2"),
("Foo3", "Foo1", "msg3")
};
Chatroom room = new Chatroom();
foreach (var user in users)
{
room.Register(user);
}
foreach (var message in messagePlan)
{
room.Send(message.from, message.to, message.message);
}
var userUnderTest = users[0];
Assert.Equal(1, userUnderTest.MessageCache.Count);
Assert.Contains("msg3", userUnderTest.MessageCache.Peek().message);
TestRunner.Print();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
public static class Assert {
public static void Equal<T> (T a, T b, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) {
if (!a.Equals (b)) {
// throw new Exception ($"{a} doesn't equal to {b}");
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = false,
Assertion = "Equal<T>",
ErrorMessage = $"{a} doesn't equal to {b}"
});
} else {
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = true,
Assertion = "Single<T>"
});
}
}
public static void Single<T> (IEnumerable<T> collection, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) {
if (collection.Count () != 1) {
// throw new Exception ("Collection doesn't contain 1 element");
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = false,
Assertion = "Single<T>",
ErrorMessage = "Collection doesn't contain 1 element"
});
}
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = true,
Assertion = "Single<T>"
});
}
public static void Contains<T> (T token1, T token2, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) {
if (typeof (T) == typeof (System.String)) {
if (!((token2 as string)).Contains (token1 as string)) {
// throw new Exception ($"{token2} doesn't contain {token1}");
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = false,
Assertion = "Contains<T>",
ErrorMessage = $"{token2} doesn't contain {token1}"
});
} else {
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = true,
Assertion = "Contains<T>"
});
}
} else {
throw new Exception ("It is not possible to use .Contains() method");
}
}
}
public static class TestRunner {
public static List<TestRunResult> Results { get; } = new List<TestRunResult> ();
public static void AddResult (TestRunResult res) {
Results.Add (res);
}
public static void Print () {
System.Console.WriteLine ("TestRunner.Print");
System.Console.WriteLine ("============================================");
foreach (var log in Results) {
System.Console.WriteLine (log);
}
System.Console.WriteLine ("============================================");
}
}
public class TestRunResult {
public string CallerName { get; set; }
public string CallerLine { get; set; }
public string Assertion { get; set; }
public bool Success { get; set; }
public string ErrorMessage { get; set; }
public override string ToString () {
return $"Assertion: {Assertion, 15}; MethodName: {CallerName, 10}; Line: {CallerLine, 3}; " + (Success? $"Success: {Success}": $"ErrorMessage: {ErrorMessage}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment