Last active
August 29, 2015 14:07
-
-
Save nkoneko/0304cbff5caad8b9fde6 to your computer and use it in GitHub Desktop.
This file contains 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
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