Skip to content

Instantly share code, notes, and snippets.

@JoeRobich
Created March 13, 2014 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeRobich/9529079 to your computer and use it in GitHub Desktop.
Save JoeRobich/9529079 to your computer and use it in GitHub Desktop.
Port of AsGuard to C#.
using System;
using System.Collections.Generic;
namespace TheDevStop.CsGuard
{
public class Contracts
{
public enum Kind
{
PreCondition,
PostCondition
}
public static event EventHandler<ContractViolationEventArgs> Violation;
static Contracts()
{
Requires = new Assertions(Kind.PreCondition);
Ensures = new Assertions(Kind.PostCondition);
}
public static Assertions Requires { get; private set; }
public static Assertions Ensures {get; private set; }
internal static void OnViolation(ContractViolationEventArgs args)
{
if (Violation != null)
Violation(null, args);
}
}
public class Assertions
{
Contracts.Kind _kind;
public Assertions(Contracts.Kind kind)
{
_kind = kind;
}
public Conditions<T> CreateConditions<T>()
{
return new Conditions<T>(this);
}
public bool IsTrue(bool condition, string message=null)
{
if (condition)
return true;
message = message ?? "Expected condition to be true.";
ContractCheckFailed(message);
return false;
}
public bool IsFalse(bool condition, string message=null)
{
message = message ?? "Expected condition to be false.";
return IsTrue(!condition, message);
}
public bool IsEqual(object expected, object actual, string message=null)
{
message = message ?? "Expected value: <" + expected.ToString() + "> actual value: <" + actual.ToString() + ">.";
return IsTrue(expected.Equals(actual) || expected == actual);
}
public bool IsNull(object value, string message=null)
{
message = message ?? "Expected value to be null.";
return IsTrue(value == null, message);
}
public bool IsNotNull(object value, string message=null)
{
message = message ?? "Expected value to be not null.";
return IsTrue(value != null, message);
}
void ContractCheckFailed(string message)
{
#if DEBUG
throw new ContractViolationException(_kind, message);
#else
Contracts.OnViolation(new ContractViolationEventArgs(_kind, message));
#endif
}
}
public class Conditions<T>
{
Assertions _asserter;
List<Func<T, bool>> _conditions = new List<Func<T, bool>>();
bool _checkNull;
bool _checkNotNull;
public Conditions(Assertions asserter)
{
_asserter = asserter;
}
public Conditions<T> IsTrue(Func<T, bool> condition)
{
_conditions.Add(t => _asserter.IsTrue(condition(t)));
return this;
}
public Conditions<T> IsFalse(Func<T, bool> condition)
{
_conditions.Add(t => _asserter.IsFalse(condition(t)));
return this;
}
public Conditions<T> IsEqual(T expected)
{
_conditions.Add(t => _asserter.IsEqual(expected, t));
return this;
}
public Conditions<T> IsNull()
{
_checkNull = true;
_checkNotNull = false;
return this;
}
public Conditions<T> IsNotNull()
{
_checkNull = false;
_checkNotNull = true;
return this;
}
public T Validate(T value)
{
if (_checkNull)
_asserter.IsNull(value);
else if (_checkNotNull)
_asserter.IsNotNull(value);
_conditions.ForEach(c => c(value));
return value;
}
}
public class ContractViolationException : Exception
{
public Contracts.Kind Kind { get; private set; }
public ContractViolationException(Contracts.Kind kind, string message)
: base(message)
{
Kind = kind;
}
}
public class ContractViolationEventArgs : EventArgs
{
public string Message { get; private set; }
public Contracts.Kind Kind { get; private set; }
public ContractViolationEventArgs(Contracts.Kind kind, string message)
{
Kind = kind;
Message = message;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment