Last active
January 15, 2016 19:15
-
-
Save miklund/e9caa07fb82c7888701c to your computer and use it in GitHub Desktop.
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
| # Assert That! | |
| # Author: Mikael Lundin | |
| # Link: http://blog.mikaellundin.name/2008/08/31/assert-that.html |
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
| public static void AssertNonNull(this object argument, string message) | |
| { | |
| argument.AssertThat(arg => arg != null, message); | |
| } | |
| public static void AssertNonNullOrEmpty(this string argument, string message) | |
| { | |
| argument.AssertThat(s => !string.IsNullOrEmpty(s), 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
| public static void AssertThat(this T argument, Predicate<t> condition, string message) | |
| { | |
| if (!condition(argument)) | |
| throw new AssertException(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
| public string Repeat(string arg, int count) | |
| { | |
| arg.AssertThat(s => s != null, | |
| "Repeat was called with arg == null"); | |
| count.AssertThat(i => i > -1, | |
| "Repeat was called with count = (negative number)"); | |
| return count == 0 ? string.Empty : arg + Repeat(arg, count--); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment