Skip to content

Instantly share code, notes, and snippets.

@nkoneko
Last active August 29, 2015 14:07
Show Gist options
  • Save nkoneko/0304cbff5caad8b9fde6 to your computer and use it in GitHub Desktop.
Save nkoneko/0304cbff5caad8b9fde6 to your computer and use it in GitHub Desktop.
public class Program
{
static void Main(string[] args)
{
string str = "Hello work!";
Test(str,
error =>
{
Console.WriteLine(error.Message);
Console.ReadLine();
});
}
static void Test(string str, Action<Exception> a)
{
var error = str
.Assert("Length == 11"
s => s.Length == 11, () => str
.Assert("Starts with \"Hello\"",
s => s.StartsWtih("Hello"), () => str
.Assert("Ends with \"world!\"",
s => s.EndsWith("world!"), () =>
Assertion.End)));
if (error != null) a(error);
}
}
public static class Assertion
{
private Func<string, Exception> fail =
msg => new Exception(msg);
public static Func<Exception> End = () =>
{ return null; }
public static Exception Assert<T>(
this T obj,
string description,
Predicate<T> predicate,
Func<Exception> continuation)
{
return predicate.Invoke(obj)
? continuation()
: fail("\"" + description + "\" failed.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment