Skip to content

Instantly share code, notes, and snippets.

@biohazard999
Created August 8, 2014 09:17
Show Gist options
  • Save biohazard999/d03f8ce7546ecf02d91b to your computer and use it in GitHub Desktop.
Save biohazard999/d03f8ce7546ecf02d91b to your computer and use it in GitHub Desktop.
[TestClass]
public class SwitchEnumTests
{
[TestMethod]
public void TestSwitcher_WithToString()
{
StringedShape stringedShape = StringedShape.Rectangle;
switch (stringedShape.ToString())
{
case StringedShape.NAME_CIRCLE:
break;
case StringedShape.NAME_RECTANGLE:
Assert.IsTrue(true);
return;
case StringedShape.NAME_SQUARE:
break;
}
Assert.Fail("Rectangle was not hit");
}
[TestMethod]
public void TestSwitcher()
{
Shape shape = Shape.Rectangle;
var wasHit = false;
shape.Switch()
.On(s => s == Shape.Circle, () => { })
.On(s => s == Shape.Rectangle, () => wasHit = true)
.On(s => s == Shape.Square, () => { })
.Evaluate();
Assert.IsTrue(wasHit);
}
}
public static class Switcher_Ext
{
public static Switcher<T> Switch<T>(this T switchable) where T : ISwitchable
{
return new Switcher<T>(switchable);
}
}
public class Switcher<T> where T : ISwitchable
{
private readonly T _switchable;
private readonly List<Case<T>> _cases;
public Switcher(T switchable)
{
_switchable = switchable;
_cases = new List<Case<T>>();
}
public Switcher<T> On(Func<T, bool> predicate, Action action)
{
_cases.Add(new Case<T>(predicate, action));
return this;
}
public void Evaluate()
{
foreach (var @case in _cases)
{
if (@case.Predicate(_switchable))
{
@case.Action();
}
}
}
}
public class Case<T> where T : ISwitchable
{
private readonly Func<T, bool> _predicate;
private readonly Action _action;
public Case(Func<T, bool> predicate, Action action)
{
_predicate = predicate;
_action = action;
}
public Func<T, bool> Predicate
{
get { return _predicate; }
}
public Action Action
{
get { return _action; }
}
}
public interface ISwitchable
{
}
public struct StringedShape
{
public const string NAME_CIRCLE = "Circle";
public const string NAME_SQUARE = "Square";
public const string NAME_RECTANGLE = "Rectangle";
// Define values here.
public static readonly StringedShape Circle = new StringedShape(NAME_CIRCLE, "This shape is round; good for wheels!");
public static readonly StringedShape Square = new StringedShape(NAME_SQUARE, "Definitely more hip than society suggests.");
public static readonly StringedShape Rectangle = new StringedShape(NAME_RECTANGLE, "Straight lines and good times.");
public readonly string Name;
public readonly string Description;
// Constructor is private: values are defined within this class only!
private StringedShape(string name, string description)
{
Name = name;
Description = description;
}
public override string ToString()
{
return Name;
}
}
public struct Shape : ISwitchable
{
// Define values here.
public static readonly Shape Circle = new Shape("Circle", "This shape is round; good for wheels!");
public static readonly Shape Square = new Shape("Square", "Definitely more hip than society suggests.");
public static readonly Shape Rectangle = new Shape("Rectangle", "Straight lines and good times.");
public readonly string Name;
public readonly string Description;
// Constructor is private: values are defined within this class only!
private Shape(string name, string description)
{
Name = name;
Description = description;
}
public static bool operator ==(Shape s1, Shape s2)
{
return s1.Equals(s2);
}
public static bool operator !=(Shape s1, Shape s2)
{
return !s1.Equals(s2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment