Skip to content

Instantly share code, notes, and snippets.

@atruskie
Last active May 14, 2017 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atruskie/1708ec79c02de950447cef7976325ede to your computer and use it in GitHub Desktop.
Save atruskie/1708ec79c02de950447cef7976325ede to your computer and use it in GitHub Desktop.
A dummy implementation of CodeContracts designed to be a signature-wise drop in replacement for common use cases
/// MIT License
/// Copyright (c) 2017 Anthony Truskinger
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
/// SOFTWARE.
namespace System.Diagnostics.Contracts
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
/// <summary>
/// This is a minimal implementation of the CodeContracts API that represents 90%
/// of our use cases. It doesn't do anything fancy and ends up being sugar for
/// standard exception throwing.
/// </summary>
public class Contract
{
/// <summary>
/// Require the supplied boolean to be true, otherwise throw an exception.
/// </summary>
/// <typeparam name="T">The type of exception to throw</typeparam>
/// <param name="result">Whether or not the exception should be thrown</param>
/// <param name="message">The message to add to the exception if the check fails</param>
[DebuggerHidden]
public static void Requires<T>(bool result, string message = "Precondition failed")
where T : Exception
{
if (!result)
{
throw (T)Activator.CreateInstance(typeof(T), message);
}
}
/// <summary>
/// Require the supplied boolean to be true, otherwise throw an exception.
/// This is a mirror of <see cref="Requires{T}"/> and behaves identically.
/// If you wish to check a condition at the end of your method, move the <see cref="Ensures{T}"/> call there.
/// </summary>
/// <typeparam name="T">The type of exception to throw</typeparam>
/// <param name="result">Whether or not the exception should be thrown</param>
/// <param name="message">The message to add to the exception if the check fails</param>
[DebuggerHidden]
public static void Ensures<T>(bool result, string message = "Postcondition failed")
where T : Exception
{
if (!result)
{
throw (T)Activator.CreateInstance(typeof(T), message);
}
}
/// <summary>
/// Require the supplied boolean to be true, otherwise throw an <see cref="ArgumentException"/>.
/// </summary>
/// <param name="result">Whether or not the exception should be thrown</param>
/// <param name="message">The message to add to the exception if the check fails</param>
[DebuggerHidden]
public static void Requires(bool result, string message = "Precondition failed")
{
if (!result)
{
throw new ArgumentException(message);
}
}
/// <summary>
/// Require the supplied boolean to be true, otherwise throw an exception.
/// This is a mirror of <see cref="Requires"/> and behaves identically.
/// If you wish to check a condition at the end of your method, move the <see cref="Ensures"/> call there.
/// </summary>
/// <param name="result">Whether or not the exception should be thrown</param>
/// <param name="message">The message to add to the exception if the check fails</param>
[DebuggerHidden]
public static void Ensures(bool result, string message = "Postcondition failed")
{
if (!result)
{
throw new InvalidOperationException(message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment