Beschreibung:
Hierbei handelt es sich um einfache Hilfsklasse welche Debug.Assert
, bwz. Debug.Fail
kapselt und deren Interface um einige praktische Debug Methoden erweitert.
Die Klasse ist weder technisch oder wissenschaftlich (noch sonstwie) anspruchsvoll, aber einfach praktisch 😉. Ich verwende sie mittlerweile in mehreren Projekten.
Da alle Methoden mit dem System.Diagnostics.ConditionalAttribute
und der Kondition "DEBUG" markiert sind, werden alle Aufrufe beim Release-Build aus dem produktiven Code entfernt.
using System;
using System.Diagnostics;
using System.Globalization;
namespace ClassLibrary1.Diagnostics {
/// <summary>
/// Helper class for debug assertions.
/// </summary>
/// <remarks>
/// This class represents a wrapper of <see cref="System.Diagnostics.Debug"/> class
/// which provides a richer interface and enables a simplified debug assertion.
/// <p/>
/// All methods are marked with <see cref="System.Diagnostics.ConditionalAttribute"/>
/// with "DEBUG" condition string. This causes the method calls to be removed from production
/// source code when compiling in "Release" mode.
/// </remarks>
public static class Assert {
/// <summary>
/// Asserts two specified argiments to be equal to each other.
/// </summary>
/// <param name="arg1">The first argument to be compared for equality.</param>
/// <param name="arg2">The first argument to be compared for equality.</param>
[Conditional("DEBUG")]
public static void AreEqual(object arg1, object arg2) {
if (arg1 == null && arg2 == null)
return;
if (arg1 == null)
Debug.Assert(false);
Debug.Assert(arg1.Equals(arg2));
}
/// <summary>
/// Asserts two specified argiments to be equal to each other.
/// </summary>
/// <param name="arg1">The first argument to be compared for equality.</param>
/// <param name="arg2">The first argument to be compared for equality.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void AreEqual(object arg1, object arg2, string message, params object[] args) {
if (arg1 == null && arg2 == null)
return;
if (arg1 == null)
Debug.Assert(false, GetMessage(message, args));
Debug.Assert(arg1.Equals(arg2), GetMessage(message, args));
}
/// <summary>
/// Asserts two specified argiments to be equal to each other.
/// </summary>
/// <param name="arg1">The first argument to be compared for equality.</param>
/// <param name="arg2">The first argument to be compared for equality.</param>
[Conditional("DEBUG")]
public static void AreEqual<T>(T arg1, T arg2) {
if (arg1 == null && arg2 == null)
return;
if (arg1 == null)
Debug.Assert(false);
Debug.Assert(arg1.Equals(arg2));
}
/// <summary>
/// Asserts two specified argiments to be equal to each other.
/// </summary>
/// <param name="arg1">The first argument to be compared for equality.</param>
/// <param name="arg2">The first argument to be compared for equality.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void AreEqual<T>(T arg1, T arg2, string message, params object[] args) {
if (arg1 == null && arg2 == null)
return;
if (arg1 == null)
Debug.Assert(false, GetMessage(message, args));
Debug.Assert(arg1.Equals(arg2), GetMessage(message, args));
}
/// <summary>
/// Asserts two specified argiments to be not equal to each other.
/// </summary>
/// <param name="arg1">The first argument to be compared for not being equal.</param>
/// <param name="arg2">The first argument to be compared for not being equal.</param>
[Conditional("DEBUG")]
public static void AreNotEqual(object arg1, object arg2) {
if (arg1 == null && arg2 == null)
Debug.Assert(false);
if (arg1 == null)
return;
Debug.Assert(!arg1.Equals(arg2));
}
/// <summary>
/// Asserts two specified argiments to be not equal to each other.
/// </summary>
/// <param name="arg1">The first argument to be compared for not being equal.</param>
/// <param name="arg2">The first argument to be compared for not being equal.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void AreNotEqual(object arg1, object arg2, string message, params object[] args) {
if (arg1 == null && arg2 == null)
Debug.Assert(false, GetMessage(message, args));
if (arg1 == null)
return;
Debug.Assert(!arg1.Equals(arg2), GetMessage(message, args));
}
/// <summary>
/// Asserts two specified argiments to be not equal to each other.
/// </summary>
/// <param name="arg1">The first argument to be compared for not being equal.</param>
/// <param name="arg2">The first argument to be compared for not being equal.</param>
[Conditional("DEBUG")]
public static void AreNotEqual<T>(T arg1, T arg2) {
if (arg1 == null && arg2 == null)
Debug.Assert(false);
if (arg1 == null)
return;
Debug.Assert(!arg1.Equals(arg2));
}
/// <summary>
/// Asserts two specified argiments to be not equal to each other.
/// </summary>
/// <param name="arg1">The first argument to be compared for not being equal.</param>
/// <param name="arg2">The first argument to be compared for not being equal.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void AreNotEqual<T>(T arg1, T arg2, string message, params object[] args) {
if (arg1 == null && arg2 == null)
Debug.Assert(false, GetMessage(message, args));
if (arg1 == null)
return;
Debug.Assert(!arg1.Equals(arg2), GetMessage(message, args));
}
/// <summary>
/// Generates a failing assertion.
/// </summary>
[Conditional("DEBUG")]
public static void Fail() {
Debug.Assert(false);
}
/// <summary>
/// Generates a failing assertion.
/// </summary>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void Fail(string message, params object[] args) {
Debug.Assert(false, GetMessage(message, args));
}
/// <summary>
/// Asserts a specified value not to be null.
/// </summary>
/// <param name="value">The value to be asserted not to be null.</param>
[Conditional("DEBUG")]
public static void IsNull(object value) {
Debug.Assert(value == null);
}
/// <summary>
/// Asserts a specified value not to be null.
/// </summary>
/// <param name="value">The value to be asserted not to be null.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
public static void IsNull(object value, string message, params object[] args) {
Debug.Assert(value == null, GetMessage(message, args));
}
/// <summary>
/// Asserts a specified value to be null.
/// </summary>
/// <param name="value">The value to be asserted to be null.</param>
[Conditional("DEBUG")]
public static void IsNotNull(object value) {
Debug.Assert(value != null);
}
/// <summary>
/// Asserts a specified value to be null.
/// </summary>
/// <param name="value">The value to be asserted to be null.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void IsNotNull(object value, string message, params object[] args) {
Debug.Assert(value != null, GetMessage(message, args));
}
/// <summary>
/// Asserts a specified parameter to be null.
/// </summary>
/// <param name="value">The parameter to be asserted to be null.</param>
/// <param name="paramName">The name of the parameter not to be null.</param>
/// <remarks>
/// The difference between <see cref="NullArg"/> and IsNotNull
/// is that <see cref="NullArg"/> should be used for parameters provided to a
/// method. IsNotNull should be used for other use cases like
/// method return values or conditional variable initializations.
/// </remarks>
[Conditional("DEBUG")]
public static void NullArg(object value, string paramName) {
if (value == null)
Debug.Fail(string.Concat(paramName + " cannot be null"));
}
/// <summary>
/// Asserts a specified value to be assignable by a specified type.
/// </summary>
/// <param name="value">The value to be asserted.</param>
/// <param name="expectedType">The type to validate the specified value to be assignable from.</param>
[Conditional("DEBUG")]
public static void IsOfType(object value, Type expectedType) {
if (value == null)
Debug.Assert(false);
Debug.Assert(expectedType.IsAssignableFrom(value.GetType()));
}
/// <summary>
/// Asserts a specified value to be assignable by a specified type.
/// </summary>
/// <param name="value">The value to be asserted.</param>
/// <param name="expectedType">The type to validate the specified value to be assignable from.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void IsOfType(object value, Type expectedType, string message, params object[] args) {
if (value == null)
Debug.Assert(false, GetMessage(message, args));
Debug.Assert(expectedType.IsAssignableFrom(value.GetType()), GetMessage(message, args));
}
/// <summary>
/// Asserts a specified value not to be assignable by a specified type.
/// </summary>
/// <param name="value">The value to be asserted.</param>
/// <param name="expectedType">The type to validate the specified value not to be assignable from.</param>
[Conditional("DEBUG")]
public static void IsNotOfType(object value, Type expectedType) {
if (value == null)
Debug.Assert(false);
Debug.Assert(!expectedType.IsAssignableFrom(value.GetType()));
}
/// <summary>
/// Asserts a specified value not to be assignable by a specified type.
/// </summary>
/// <param name="value">The value to be asserted.</param>
/// <param name="expectedType">The type to validate the specified value not to be assignable from.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void IsNotOfType(object value, Type expectedType, string message, params object[] args) {
if (value == null)
Debug.Assert(false, GetMessage(message, args));
Debug.Assert(!expectedType.IsAssignableFrom(value.GetType()), GetMessage(message, args));
}
/// <summary>
/// Asserts a target type to be assignable by a expected type.
/// </summary>
/// <param name="targetType">The target type to be assignable.</param>
/// <param name="expectedType">The expected type to be assignable by specified target type.</param>
[Conditional("DEBUG")]
public static void IsTypeOf(Type targetType, Type expectedType) {
Debug.Assert(expectedType.IsAssignableFrom(targetType));
}
/// <summary>
/// Asserts a target type to be assignable by a expected type.
/// </summary>
/// <param name="targetType">The target type to be assignable.</param>
/// <param name="expectedType">The expected type to be assignable by specified target type.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void IsTypeOf(Type targetType, Type expectedType, string message, params object[] args) {
Debug.Assert(expectedType.IsAssignableFrom(targetType), GetMessage(message, args));
}
/// <summary>
/// Asserts a specified condition to be true.
/// </summary>
/// <param name="condition">The condition to be asserted for being true.</param>
[Conditional("DEBUG")]
public static void IsTrue(bool condition) {
Debug.Assert(condition);
}
/// <summary>
/// Asserts a specified condition to be true.
/// </summary>
/// <param name="condition">The condition to be asserted for being true.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void IsTrue(bool condition, string message, params object[] args) {
Debug.Assert(condition, GetMessage(message, args));
}
/// <summary>
/// Asserts a specified condition not to be true.
/// </summary>
/// <param name="condition">The condition to be asserted for not being true.</param>
[Conditional("DEBUG")]
public static void IsFalse(bool condition) {
Debug.Assert(!condition);
}
/// <summary>
/// Asserts a specified condition not to be true.
/// </summary>
/// <param name="condition">The condition to be asserted for not being true.</param>
/// <param name="message">The message to be shown when assertion fails.</param>
/// <param name="args">Optional formatting arguments for specified message.</param>
[Conditional("DEBUG")]
public static void IsFalse(bool condition, string message, params object[] args) {
Debug.Assert(!condition, GetMessage(message, args));
}
private static string GetMessage(string message, params object[] args) {
if (args != null && args.Length != 0)
return string.Format(CultureInfo.InvariantCulture, message, args);
return message;
}
}
}
Schlagwörter: <Debug, Assert, Test, Testing, Debug.Assert>
Blog: Things about Software Architecture, .NET development and SQL Server
Twitter
Google+
Je mehr ich weiß, desto mehr weiß ich was ich noch nicht weiß.