Exemple #1
0
    public ConstraintResult Test(string caption, T value)
    {
        var message = new StringBuilder();

        if (value is IComparable comparableValue)
        {
            if (lowerBound != null)
            {
                var lowerResult = lowerBound.CompareTo(comparableValue);
                if (lowerResult < 0 || (!isLowerBoundIncluded && lowerResult == 0))
                {
                    var includedText = isLowerBoundIncluded ? "or equal to " : "";
                    message.Append($"{caption} must be greater than {includedText}{lowerResult}");
                }
            }
            if (upperBound != null)
            {
                var upperResult = upperBound.CompareTo(value);
                if (upperResult < 0 || (!isUpperBoundIncluded && upperResult == 0))
                {
                    var includedText = isUpperBoundIncluded ? "or equal to " : "";
                    message.Append($"{caption} must be less than {includedText}{upperResult}");
                }
            }
        }
        return(message.Length > 0
            ? ConstraintResult.Failed(message.ToString())
            : ConstraintResult.Passed());
    }
 public ConstraintResult Test(string friendlyName, string value)
 => string.IsNullOrWhiteSpace(value)
         ? ConstraintResult.Failed($"{friendlyName} must not be blank")
         : ConstraintResult.Passed();