public override string Export(bool deformat)
 {
     return(new StringBuilder()
            .Append(Include.HasFlag(Includes.Min) ? "[" : "(")
            .Append(Min.Export(deformat))
            .Append(Export(", ", deformat))
            .Append(Max.Export(deformat))
            .Append(Include.HasFlag(Includes.Max) ? "]" : ")")
            .ToString());
 }
Exemple #2
0
        private static string BuildArgumentExceptionMessage(
            Validation validation,
            string exceptionMessageSuffix,
            Include include          = Include.None,
            Type genericTypeOverride = null)
        {
            if (validation.ApplyBecause == ApplyBecause.InLieuOfDefaultMessage)
            {
                // we force to empty string if null because otherwise when the exception is
                // constructed the framework chooses some generic message like 'An exception of type ArgumentException was thrown'
                return(validation.Because ?? string.Empty);
            }

            var parameterNameQualifier        = validation.ParameterName == null ? string.Empty : Invariant($" '{validation.ParameterName}'");
            var enumerableQualifier           = validation.IsElementInEnumerable ? " contains an element that" : string.Empty;
            var genericTypeQualifier          = include.HasFlag(Include.GenericType) ? ", where T: " + (genericTypeOverride?.GetFriendlyTypeName() ?? validation.ValueType.GetFriendlyTypeName()) : string.Empty;
            var failingValueQualifier         = include.HasFlag(Include.FailingValue) ? (validation.IsElementInEnumerable ? "  Element value" : "  Parameter value") + Invariant($" is '{validation.Value?.ToString() ?? NullValueToString}'.") : string.Empty;
            var validationParameterQualifiers = validation.ValidationParameters == null || !validation.ValidationParameters.Any() ? string.Empty : validation.ValidationParameters.Select(_ => Invariant($"  Specified '{_.Name}' is '{_.Value ?? NullValueToString}'.")).Aggregate((running, current) => running + current);
            var result = Invariant($"Parameter{parameterNameQualifier}{enumerableQualifier} {exceptionMessageSuffix}{genericTypeQualifier}.{failingValueQualifier}{validationParameterQualifiers}");

            if (validation.ApplyBecause == ApplyBecause.PrefixedToDefaultMessage)
            {
                if (!string.IsNullOrWhiteSpace(validation.Because))
                {
                    result = validation.Because + "  " + result;
                }
            }
            else if (validation.ApplyBecause == ApplyBecause.SuffixedToDefaultMessage)
            {
                if (!string.IsNullOrWhiteSpace(validation.Because))
                {
                    result = result + "  " + validation.Because;
                }
            }
            else
            {
                throw new NotSupportedException(Invariant($"This {nameof(ApplyBecause)} is not supported: {validation.ApplyBecause}"));
            }

            return(result);
        }
Exemple #3
0
        private static string BuildVerificationFailedExceptionMessage(
            AssertionTracker assertionTracker,
            Verification verification,
            VerifiableItem verifiableItem,
            string exceptionMessageSuffix,
            Include include        = Include.None,
            string methodologyInfo = null,
            string contextualInfo  = null)
        {
            if (verification.ApplyBecause == ApplyBecause.InLieuOfDefaultMessage)
            {
                // we force to empty string if null because otherwise when the exception is
                // constructed the framework chooses some generic message like 'An exception of type ArgumentException was thrown'
                return(verification.Because ?? string.Empty);
            }

            var subjectNameQualifier = assertionTracker.SubjectName == null ? string.Empty : Invariant($" (name: '{assertionTracker.SubjectName}')");

            var enumerableQualifier = verifiableItem.ItemIsElementInEnumerable ? " contains an element that" : string.Empty;

            var methodologyInfoQualifier = methodologyInfo == null ? null : " " + methodologyInfo;

            var contextualInfoQualifier = contextualInfo == null ? null : "  " + contextualInfo;

            var failingValueQualifier = include.HasFlag(Include.FailingValue) ? (verifiableItem.ItemIsElementInEnumerable ? "  Element value" : "  Provided value") + Invariant($" is {verifiableItem.ItemValue.ToStringInErrorMessage()}.") : string.Empty;

            var verificationParametersQualifier = verification.VerificationParameters == null || !verification.VerificationParameters.Any() ? string.Empty : string.Join(string.Empty, verification.VerificationParameters.Select(_ => _.ToStringInErrorMessage()));

            var result = Invariant($"Provided value{subjectNameQualifier}{enumerableQualifier} {exceptionMessageSuffix}{methodologyInfoQualifier}.{contextualInfoQualifier}{failingValueQualifier}{verificationParametersQualifier}");

            if (verification.ApplyBecause == ApplyBecause.PrefixedToDefaultMessage)
            {
                if (!string.IsNullOrWhiteSpace(verification.Because))
                {
                    result = verification.Because + "  " + result;
                }
            }
            else if (verification.ApplyBecause == ApplyBecause.SuffixedToDefaultMessage)
            {
                if (!string.IsNullOrWhiteSpace(verification.Because))
                {
                    result = result + "  " + verification.Because;
                }
            }
            else
            {
                throw new NotSupportedException(Invariant($"This {nameof(ApplyBecause)} is not supported: {verification.ApplyBecause}"));
            }

            return(result);
        }