I wrote a couple of extension methods for common guard statements I write a lot. These are extension methods for Object, and allow me to quickly guard against common scenarios.
I'm using .Net 3.5 (Unity3D) so sadly I don't have access to Code Contracts in this project.
My main concern with this code is there's a lot of duplication (primarily so that I can support custom messages for when it is suitable to do so). Is there a good way I can reduce this duplication and still support optional custom messages? I intend to add more common guard methods for the float
, string
and int
classes too, but would rather have a good methodology in place
Any other comments on the code or tests are also welcome.
public static class ObjectExtensions
{
public static void ThrowIfNull(this object value, string argumentName)
{
if (value == null)
throw new ArgumentNullException(argumentName);
}
public static void ThrowIfNotEqual(this object value, string argumentName, object expectedValue)
{
if (!value.Equals(expectedValue))
throw new ArgumentException(argumentName, string.Format("Argument must not equal {0}", expectedValue));
}
public static void ThrowIfEqual(this object value, string argumentName, object expectedValue)
{
if (value.Equals(expectedValue))
throw new ArgumentException(argumentName, string.Format("Argument must equal {0}", expectedValue));
}
public static void ThrowIfNull(this object value, string argumentName, string customMessage)
{
if (value == null)
throw new ArgumentNullException(argumentName, customMessage);
}
public static void ThrowIfNotEqual(this object value, string argumentName, object expectedValue, string customMessage)
{
if (!value.Equals(expectedValue))
throw new ArgumentException(argumentName, customMessage);
}
public static void ThrowIfEqual(this object value, string argumentName, object expectedValue, string customMessage)
{
if (value.Equals(expectedValue))
throw new ArgumentException(argumentName, customMessage);
}
}
NUnit tests (should they be helpful):
[TestFixture()]
public class ObjectExtensionsTests
{
[Test()]
[ExpectedException(typeof(ArgumentNullException))]
public void ThrowIfNullTest_NullInput_ThrowsException()
{
object subject = null;
subject.ThrowIfNull("subject");
}
[Test()]
public void ThrowIfNullTest_NonNullInput()
{
object subject = "Hello";
subject.ThrowIfNull("subject");
}
[Test()]
[ExpectedException(typeof(ArgumentException))]
public void ThrowIfNotEqualTest_NotEqual_ThrowsException()
{
object a = 5f;
object b = 10f;
a.ThrowIfNotEqual("a", b);
}
[Test()]
public void ThrowIfNotEqualTest_Equal()
{
object a = 5f;
object b = 5f;
a.ThrowIfNotEqual("a", b);
}
[Test()]
[ExpectedException(typeof(ArgumentException))]
public void ThrowIfEqualTest_Equal_ThrowsException()
{
object a = 5f;
object b = 5f;
a.ThrowIfEqual("a", b);
}
[Test()]
public void ThrowIfEqualTest_NotEqual()
{
object a = 5f;
object b = 10f;
a.ThrowIfEqual("a", b);
}
[Test()]
[ExpectedException(typeof(ArgumentException))]
public void ThrowIfEqualTest_CustomMessage_Equal_ThrowsException()
{
object a = 5f;
object b = 5f;
a.ThrowIfEqual("a", b, "a is equal to b");
}
[Test()]
public void ThrowIfEqualTest_CustomMessage_NotEqual()
{
object a = 5f;
object b = 10f;
a.ThrowIfEqual("a", b, "a is equal to b");
}
[Test()]
[ExpectedException(typeof(ArgumentNullException))]
public void ThrowIfNullTest_CustomMessage_NullInput_ThrowsException()
{
object subject = null;
subject.ThrowIfNull("subject", "subject is null");
}
[Test()]
public void ThrowIfNullTest_CustomMessage_NonNullInput()
{
object subject = "Hello";
subject.ThrowIfNull("subject", "subject is null");
}
[Test()]
[ExpectedException(typeof(ArgumentException))]
public void ThrowIfNotEqualTest_CustomMessage_NotEqual_ThrowsException()
{
object a = 5f;
object b = 10f;
a.ThrowIfNotEqual("a", b, "a is not equal to b");
}
[Test()]
public void ThrowIfNotEqualTest_CustomMessage_Equal()
{
object a = 5f;
object b = 5f;
a.ThrowIfNotEqual("a", b, "a is not equal to b");
}
}