internal static void EnsureCodeContract(ICodeContract c) { if (c == null) { throw new NullReferenceException($"{nameof(c)} is null"); } }
public static void IsTrue(this ICodeContract c, bool o, string message = null, Type exceptionType = null) { CodeContract.EnsureCodeContract(c); if (!o) { throw c.Encapsulate(new ArgumentException($"Code Contract harmed, false is not allowed. {message}"), exceptionType); } }
/// <summary> /// Extends <see cref="ICodeContract"/> with null check capability. /// </summary> /// <param name="c">Extends <see cref="ICodeContract"/></param> /// <param name="a">Pass the to be checked <see cref="object"/></param> /// <param name="message">Pass an additional message to be add to exception in case of harming the contract</param> /// <param name="exeptionType">Pass an additional exception type the thrown exception should be wrapped into in case of harming the contract</param> /// <param name="exeptionName">Pass an additional exception type name the thrown exception should be wrapped into in case of harming the contract</param> public static void IsNotNull(this ICodeContract c, object a, string message = null, Type exeptionType = null) { CodeContract.EnsureCodeContract(c); if (a == null) { throw c.Encapsulate(new NoNullAllowedException($"Code Contract harmed, null is not allowed. {message}"), exeptionType); } }
public static void ExceedsMax(this ICodeContract c, object o, object limit, string message = null, Type exceptionType = null) { CodeContract.EnsureCodeContract(c); bool harmed = false; if (o is double d && limit is double dLim && d > dLim) { harmed = true; }
public static void IsNull(this ICodeContract c, object o, string message = null, Type exceptionType = null ) { CodeContract.EnsureCodeContract(c); if (o != null) { throw c.Encapsulate(new ArgumentException($"Code Contract harmed, not null is not allowed. {message}"), exceptionType); } }
public static void HasItems(this ICodeContract c, IEnumerable o, string message = null, Type exceptionType = null) { CodeContract.EnsureCodeContract(c); c.IsNotNull(o, message, exceptionType); IEnumerator e = o.GetEnumerator(); c.IsNotNull(e, message, exceptionType); if (e.Current == null) { e.Reset(); throw c.Encapsulate(new ArgumentOutOfRangeException($"Code Contract harmed, {nameof(o)} has no items. {message}"), exceptionType); } e.Reset(); }
public static void HasAtLeast(this ICodeContract c, IEnumerable o, int items, string message = null, Type exceptionType = null) { CodeContract.EnsureCodeContract(c); c.IsNotNull(o, message, exceptionType); var e = o.GetEnumerator(); int idx = 0; while (e.Current != null && e.MoveNext()) { if (idx >= items) { return; } idx++; } throw c.Encapsulate( new ArgumentOutOfRangeException( $"Code Contract harmed, has less items than {idx} < {items}. {message}"), exceptionType ); }
public void Teardown() { _contract = null; }
public void Setup() { _contract = new CodeContract(); }