/// <summary> /// Verify that <paramref name="value"/> is not null, not empty and contains other characters than white space. /// </summary> public static void IsNotNullOrWhiteSpace(string value, string propertyName, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); InternalContract.RequireNotNull(propertyName, nameof(propertyName)); var message = customMessage ?? $"Property {propertyName} ({value}) must not be null or empty and it must contain other characters than white space."; GenericAssert <FulcrumAssertionFailedException> .IsNotNullOrWhiteSpace(value, errorLocation, message); }
/// <summary> /// Verify that <paramref name="value"/> is not null. /// </summary> public static void IsNotNull(object value, string propertyName, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); InternalContract.RequireNotNull(propertyName, nameof(propertyName)); var message = customMessage ?? $"Property {propertyName} ({value}) must not be null."; GenericAssert <FulcrumAssertionFailedException> .IsNotNull(value, errorLocation, message); }
/// <summary> /// Verify that <paramref name="value"/> is not the default mustBeTrue for that type. /// </summary> public static void IsNotDefaultValue <T>(T value, string propertyName, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); InternalContract.RequireNotNull(propertyName, nameof(propertyName)); var message = customMessage ?? $"Property {propertyName} ({value}) must not have the default propertyValue ({default(T)}."; GenericAssert <FulcrumAssertionFailedException> .IsNotDefaultValue(value, errorLocation, message); }
/// <summary> /// Verify that <paramref name="actualValue"/> is less than to <paramref name="greaterValue"/>. /// </summary> public static void IsLessThan <T>(T greaterValue, T actualValue, string errorLocation, string customMessage = null) where T : IComparable <T> { InternalContract.RequireNotNull(greaterValue, nameof(greaterValue)); InternalContract.RequireNotNull(actualValue, nameof(actualValue)); InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); GenericAssert <FulcrumAssertionFailedException> .IsLessThan(greaterValue, actualValue, errorLocation, customMessage); }
/// <summary> /// Verify that <paramref name="parameterValue"/> is greater than or equal to <paramref name="lesserOrEqualValue"/>. /// </summary> public static void RequireGreaterThanOrEqualTo <T>(T lesserOrEqualValue, T parameterValue, string parameterName, string customMessage = null) where T : IComparable <T> { InternalContract.RequireNotNull(lesserOrEqualValue, nameof(lesserOrEqualValue)); InternalContract.RequireNotNull(parameterValue, nameof(parameterValue)); InternalContract.RequireNotNull(parameterName, nameof(parameterName)); GenericContract <FulcrumServiceContractException> .RequireGreaterThanOrEqualTo(lesserOrEqualValue, parameterValue, parameterName, customMessage); }
/// <summary> /// Verify that <paramref name="propertyValue"/> is null or not matches the regular expression <paramref name="regularExpression"/>. /// </summary> public static void MatchesNotRegExp(string regularExpression, string propertyValue, string propertyName, string errorLocation, string customMessage = null) { InternalContract.RequireNotNullOrWhitespace(regularExpression, nameof(regularExpression)); InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); var message = customMessage ?? $"Expected property {propertyName} ({propertyValue}) to not match ({regularExpression})."; GenericAssert <FulcrumAssertionFailedException> .MatchesNotRegExp(regularExpression, propertyValue, errorLocation, message); }
/// <summary> /// Verify that <paramref name="propertyValue"/> is equal to <paramref name="expectedValue"/>. /// </summary> public static void AreEqual(object expectedValue, object propertyValue, string propertyName, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); InternalContract.RequireNotNull(propertyName, nameof(propertyName)); var message = customMessage ?? $"Expected property {propertyName} ({propertyValue}) to be equal to ({expectedValue})."; GenericAssert <FulcrumAssertionFailedException> .AreEqual(expectedValue, propertyValue, errorLocation, message); }
/// <summary> /// Verify that <paramref name="propertyValue"/> is greater than or equal to <paramref name="lesserOrEqualValue"/>. /// </summary> public static void IsGreaterThanOrEqualTo <T>(T lesserOrEqualValue, T propertyValue, string propertyName, string errorLocation, string customMessage = null) where T : IComparable <T> { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); InternalContract.RequireNotNull(propertyName, nameof(propertyName)); var message = customMessage ?? $"Expected property {propertyName} ({propertyValue}) to be less than ({lesserOrEqualValue})."; GenericAssert <FulcrumAssertionFailedException> .IsGreaterThanOrEqualTo(lesserOrEqualValue, propertyValue, errorLocation, message); }
/// <summary> /// Verify that <paramref name="parameterValue"/> matches the regular expression <paramref name="regularExpression"/>. /// </summary> public static void RequireMatchesNotRegExp(string regularExpression, string parameterValue, string parameterName, string customMessage = null) { InternalContract.RequireNotNull(regularExpression, nameof(regularExpression)); InternalContract.RequireNotNull(parameterValue, nameof(parameterValue)); InternalContract.RequireNotNull(parameterName, nameof(parameterName)); var message = customMessage ?? $"ContractViolation: {parameterName} ({parameterValue}) must not match regular expression ({regularExpression})."; Require(!Regex.IsMatch(parameterValue, regularExpression), message); }
/// <summary> /// Verify that <paramref name="value"/> is not the default value for that type. /// </summary> public static void IsNotDefaultValue <T>(T value, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); if (!value.Equals(default(T))) { return; } ThrowException(errorLocation, customMessage ?? $"Did not expect value to be default value ({default(T)})."); }
/// <summary> /// Verify that <paramref name="value"/> is null. /// </summary> public static void IsNull(object value, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); if (value == null) { return; } ThrowException(errorLocation, customMessage ?? $"Expected value ({value}) to be null."); }
/// <summary> /// Verify that <paramref name="value"/> is true. /// </summary> public static void IsTrue(bool value, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); if (value) { return; } ThrowException(errorLocation, customMessage ?? "Expected value to be true."); }
/// <summary> /// Verify that <paramref name="actualValue"/> is not equal to <paramref name="expectedValue"/>. /// </summary> public static void AreNotEqual(object expectedValue, object actualValue, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); if (!Equals(expectedValue, actualValue)) { return; } ThrowException(errorLocation, customMessage ?? $"Expected ({actualValue}) to not be equal to ({expectedValue})."); }
/// <summary> /// Verify that <paramref name="value"/> is not null, not empty and has other characters than white space. /// </summary> public static void IsNotNullOrWhiteSpace(string value, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); if (!string.IsNullOrWhiteSpace(value)) { return; } ThrowException(errorLocation, customMessage ?? $"Did not expect value ({value}) to be null, empty or only contain whitespace."); }
/// <summary> /// Verify that <paramref name="actualValue"/> is greater than or equal to <paramref name="lesserOrEqualValue"/>. /// </summary> public static void IsGreaterThanOrEqualTo <T>(T lesserOrEqualValue, T actualValue, string errorLocation, string customMessage = null) where T : IComparable <T> { InternalContract.RequireNotNull(lesserOrEqualValue, nameof(lesserOrEqualValue)); InternalContract.RequireNotNull(actualValue, nameof(actualValue)); InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); var message = customMessage ?? $"Expected ({actualValue}) to be greater than or equal to ({lesserOrEqualValue})."; IsTrue(actualValue.CompareTo(lesserOrEqualValue) >= 0, errorLocation, message); }
/// <summary> /// Verify that <paramref name="actualValue"/> is less than to <paramref name="greaterValue"/>. /// </summary> public static void IsLessThan <T>(T greaterValue, T actualValue, string errorLocation, string customMessage = null) where T : IComparable <T> { InternalContract.RequireNotNull(greaterValue, nameof(greaterValue)); InternalContract.RequireNotNull(actualValue, nameof(actualValue)); InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); var message = customMessage ?? $"Expected ({actualValue}) to be less than ({greaterValue})."; IsTrue(actualValue.CompareTo(greaterValue) < 0, errorLocation, message); }
/// <summary> /// Verify that <paramref name="parameterValue"/> is greater than or equal to <paramref name="lesserOrEqualValue"/>. /// </summary> public static void RequireGreaterThanOrEqualTo <T>(T lesserOrEqualValue, T parameterValue, string parameterName, string customMessage = null) where T : IComparable <T> { InternalContract.RequireNotNull(lesserOrEqualValue, nameof(lesserOrEqualValue)); InternalContract.RequireNotNull(parameterValue, nameof(parameterValue)); InternalContract.RequireNotNull(parameterName, nameof(parameterName)); var message = customMessage ?? $"ContractViolation: {parameterName} ({parameterValue}) must be greater than or equal to ({lesserOrEqualValue})."; Require(parameterValue.CompareTo(lesserOrEqualValue) >= 0, message); }
/// <summary> /// Verify that <paramref name="parameterValue"/> is less than to <paramref name="greaterValue"/>. /// </summary> public static void RequireLessThan <T>(T greaterValue, T parameterValue, string parameterName, string customMessage = null) where T : IComparable <T> { InternalContract.RequireNotNull(greaterValue, nameof(greaterValue)); InternalContract.RequireNotNull(parameterValue, nameof(parameterValue)); InternalContract.RequireNotNull(parameterName, nameof(parameterName)); var message = customMessage ?? $"ContractViolation: {parameterName} ({parameterValue}) must be less than ({greaterValue})."; Require(parameterValue.CompareTo(greaterValue) < 0, message); }
public static void IsTrue(Expression <Func <bool> > expression, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); var value = expression.Compile()(); if (value) { return; } ThrowException(errorLocation, customMessage ?? $"Expected '{expression.Body} to be true."); }
public static void AreEqual(object expectedValue, Expression <Func <string> > expression, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); var actualValue = expression.Compile()(); if (Equals(expectedValue, actualValue)) { return; } ThrowException(errorLocation, customMessage ?? $"Expected '{expression.Body}' ({actualValue}) to be equal to ({expectedValue})."); }
public static void IsNotNullOrWhiteSpace(Expression <Func <string> > expression, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); var value = expression.Compile()(); if (!string.IsNullOrWhiteSpace(value)) { return; } ThrowException(errorLocation, customMessage ?? $"Did not expect '{expression.Body}' ({value}) to be null, empty or only contain whitespace."); }
public static void IsNotNull(Expression <Func <object> > expression, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(expression, nameof(expression)); var value = expression.Compile()(); if (value != null) { return; } ThrowException(errorLocation, customMessage ?? $"Did not expect '{expression.Body}' to be null."); }
/// <summary> /// Verify that <paramref name="value"/> is null or matches the regular expression <paramref name="regularExpression"/>. /// </summary> public static void MatchesNotRegExp(string regularExpression, string value, string errorLocation, string customMessage = null) { if (value == null) { return; } InternalContract.RequireNotNull(regularExpression, nameof(regularExpression)); InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); var message = customMessage ?? $"Expected ({value}) to not match regular expression ({regularExpression})."; IsTrue(!Regex.IsMatch(value, regularExpression), errorLocation, message); }
/// <summary> /// Will always fail. Used in parts of the errorLocation where we should never end up. E.g. a default case in a switch statement where all cases should be covered, so we should never end up in the default case. /// </summary> /// <param name="errorLocation">A unique errorLocation for the part of errorLocation where the validation didn't hold.</param> /// <param name="message">A message that documents/explains this failure. This message should normally start with "Expected ...".</param> public static void Fail(string errorLocation, string message) { InternalContract.RequireNotNull(message, nameof(message)); InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); GenericAssert <FulcrumAssertionFailedException> .Fail(errorLocation, message); }
public static void Require(Expression <Func <bool> > expression, string message) { InternalContract.RequireNotNullOrWhitespace(message, nameof(message)); GenericContract <FulcrumContractException> .Require(expression, message); }
private static string GetErrorMessageIfFalse(bool mustBeTrue, string message) { InternalContract.RequireNotNullOrWhitespace(message, nameof(message)); return(mustBeTrue ? null : message); }
/// <summary> /// Will always fail. Used in parts of the errorLocation where we should never end up. E.g. a default case in a switch statement where all cases should be covered, so we should never end up in the default case. /// </summary> /// <param name="errorLocation">A unique errorLocation for this exact assertion.</param> /// <param name="message">A message that documents/explains this failure. This message should normally start with "Expected ...".</param> public static void Fail(string errorLocation, string message) { InternalContract.RequireNotNullOrWhitespace(message, nameof(message)); InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); ThrowException(errorLocation, message); }
/// <summary> /// Verify that <paramref name="mustBeTrue"/> is true. /// </summary> /// <param name="mustBeTrue">The value that must be true.</param> /// <param name="message">A message that documents/explains this failure. This message should normally start with "Expected ...".</param> /// <param name="errorLocation">A unique errorLocation for the part of errorLocation where the validation didn't hold.</param> public static void IsTrue(bool mustBeTrue, string errorLocation, string message) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); InternalContract.RequireNotNullOrWhitespace(message, nameof(message)); GenericAssert <FulcrumAssertionFailedException> .IsTrue(mustBeTrue, errorLocation, message); }
/// <summary> /// Verify that <paramref name="mustBeTrue"/> really is true. /// </summary> public static void Require(bool mustBeTrue, string message) { InternalContract.RequireNotNullOrWhitespace(message, nameof(message)); GenericContract <FulcrumContractException> .Require(mustBeTrue, message); }
/// <summary> /// Verify that <paramref name="actualValue"/> is not equal to <paramref name="expectedValue"/>. /// </summary> public static void AreNotEqual(object expectedValue, object actualValue, string errorLocation, string customMessage = null) { InternalContract.RequireNotNull(errorLocation, nameof(errorLocation)); GenericAssert <FulcrumAssertionFailedException> .AreNotEqual(expectedValue, actualValue, errorLocation, customMessage); }