Skip to content

Instantly share code, notes, and snippets.

@miklund
Last active January 15, 2016 19:15
Show Gist options
  • Select an option

  • Save miklund/e9caa07fb82c7888701c to your computer and use it in GitHub Desktop.

Select an option

Save miklund/e9caa07fb82c7888701c to your computer and use it in GitHub Desktop.
# Assert That!
# Author: Mikael Lundin
# Link: http://blog.mikaellundin.name/2008/08/31/assert-that.html
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);
}
public static void AssertThat(this T argument, Predicate<t> condition, string message)
{
if (!condition(argument))
throw new AssertException(message);
}
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