private static void Throw(
            Verification verification,
            VerifiableItem verifiableItem,
            TypeValidation typeValidation)
        {
            var verifiableItemTypeReadableString = verifiableItem.ItemType.ToStringReadable();

            WorkflowExtensions.ThrowImproperUseOfFramework(Invariant($"verificationName: {verification.Name}, isElementInEnumerable: {verifiableItem.ItemIsElementInEnumerable}, verifiableItemTypeName: {verifiableItemTypeReadableString}"));
        }
        private static void ThrowSubjectUnexpectedType(
            Verification verification,
            VerifiableItem verifiableItem,
            IReadOnlyList <Type> expectedTypes)
        {
            var expectedTypeStrings = expectedTypes.Select(_ => _.ToStringReadable()).ToArray();

            ThrowSubjectUnexpectedType(verification, verifiableItem, expectedTypeStrings);
        }
 private static void ThrowIfTypeDoesNotHaveWorkingDefaultComparer(
     Verification verification,
     VerifiableItem verifiableItem,
     TypeValidation typeValidation)
 {
     if (!verifiableItem.ItemType.HasWorkingDefaultComparer())
     {
         ThrowSubjectUnexpectedType(verification, verifiableItem, AnyTypeWithWorkingDefaultComparerName);
     }
 }
 private static void ThrowIfValueType(
     Verification verification,
     VerifiableItem verifiableItem,
     TypeValidation typeValidation)
 {
     if (verifiableItem.ItemType.IsValueType)
     {
         ThrowSubjectUnexpectedType(verification, verifiableItem, AnyReferenceTypeName);
     }
 }
        private static void ThrowIfTypeCannotBeAssignedToNull(
            Verification verification,
            VerifiableItem verifiableItem,
            TypeValidation typeValidation)
        {
            var verifiableItemType = verifiableItem.ItemType;

            if (!verifiableItemType.IsClosedTypeAssignableToNull())
            {
                ThrowSubjectUnexpectedType(verification, verifiableItem, AnyReferenceTypeName, NullableGenericTypeName);
            }
        }
Beispiel #6
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);
        }
        private static void ThrowIfNotEqualToType(
            Verification verification,
            VerifiableItem verifiableItem,
            TypeValidation typeValidation)
        {
            var verifiableItemType = verifiableItem.ItemType;

            var validTypes = typeValidation.ReferenceTypes;

            if (validTypes.All(_ => verifiableItemType != _))
            {
                ThrowSubjectUnexpectedType(verification, verifiableItem, validTypes);
            }
        }
        private static void ThrowIfNotAssignableToType(
            Verification verification,
            VerifiableItem verifiableItem,
            TypeValidation typeValidation)
        {
            var verifiableItemType = verifiableItem.ItemType;

            var validTypes = typeValidation.ReferenceTypes;

            if (!validTypes.Any(_ => verifiableItemType.IsAssignableTo(_, treatGenericTypeDefinitionAsAssignableTo: true)))
            {
                ThrowSubjectUnexpectedType(verification, verifiableItem, validTypes);
            }
        }
        private static void ThrowIfDictionaryValueTypeCannotBeAssignedToNull(
            Verification verification,
            VerifiableItem verifiableItem,
            TypeValidation typeValidation)
        {
            var verifiableItemType = verifiableItem.ItemType;

            var dictionaryValueType = verifiableItemType.GetClosedDictionaryValueType();

            if (!dictionaryValueType.IsClosedTypeAssignableToNull())
            {
                ThrowSubjectUnexpectedType(verification, verifiableItem, DictionaryTypeName, DictionaryWithValueOfAnyReferenceTypeName, DictionaryWithValueOfNullableGenericTypeName, ReadOnlyDictionaryWithValueOfAnyReferenceTypeName, ReadOnlyDictionaryWithValueOfNullableGenericTypeName);
            }
        }
        private static void ThrowIfEnumerableElementTypeCannotBeAssignedToNull(
            Verification verification,
            VerifiableItem verifiableItem,
            TypeValidation typeValidation)
        {
            var verifiableItemType = verifiableItem.ItemType;

            var elementType = verifiableItemType.GetClosedEnumerableElementType();

            if (!elementType.IsClosedTypeAssignableToNull())
            {
                ThrowSubjectUnexpectedType(verification, verifiableItem, EnumerableOfAnyReferenceTypeName, EnumerableOfNullableGenericTypeName, EnumerableWhenNotEnumerableOfAnyValueTypeName);
            }
        }
        private static void ThrowIfDictionaryKeyTypeDoesNotEqualAllVerificationParameterTypes(
            Verification verification,
            VerifiableItem verifiableItem,
            TypeValidation typeValidation)
        {
            var keyType = verifiableItem.ItemType.GetClosedDictionaryKeyType();

            foreach (var verificationParameter in verification.VerificationParameters)
            {
                if (verificationParameter.ParameterType != keyType)
                {
                    ThrowVerificationParameterUnexpectedType(verification.Name, verificationParameter.ParameterType, verificationParameter.Name, keyType);
                }
            }
        }
        private static void ThrowIfEnumerableElementTypeDoesNotEqualAllVerificationParameterTypes(
            Verification verification,
            VerifiableItem verifiableItem,
            TypeValidation typeValidation)
        {
            var elementType = verifiableItem.ItemType.GetClosedEnumerableElementType();

            foreach (var verificationParameter in verification.VerificationParameters)
            {
                if (verificationParameter.ParameterType != elementType)
                {
                    ThrowVerificationParameterUnexpectedType(verification.Name, verificationParameter.ParameterType, verificationParameter.Name, elementType);
                }
            }
        }
        private static void ThrowIfTypeIsNotEqualToAllVerificationParameterType(
            Verification verification,
            VerifiableItem verifiableItem,
            TypeValidation typeValidation)
        {
            var verifiableItemType = verifiableItem.ItemType;

            foreach (var verificationParameter in verification.VerificationParameters)
            {
                if (verificationParameter.ParameterType != verifiableItemType)
                {
                    ThrowVerificationParameterUnexpectedType(verification.Name, verificationParameter.ParameterType, verificationParameter.Name, verifiableItemType);
                }
            }
        }
        private static void ThrowSubjectUnexpectedType(
            Verification verification,
            VerifiableItem verifiableItem,
            params string[] expectedTypes)
        {
            var verifiableItemType = verifiableItem.ItemType;

            var verificationName = verification.Name;

            var isElementInEnumerable = verifiableItem.ItemIsElementInEnumerable;

            var expectedTypesMessage = string.Join(", ", expectedTypes.Select(_ => isElementInEnumerable ? Invariant($"IEnumerable<{_}>") : _));

            var valueTypeMessage = isElementInEnumerable ? Invariant($"IEnumerable<{verifiableItemType.ToStringReadable()}>") : verifiableItemType.ToStringReadable();

            var exceptionMessage = string.Format(CultureInfo.InvariantCulture, SubjectUnexpectedTypeErrorMessage, verificationName, valueTypeMessage, expectedTypesMessage);

            WorkflowExtensions.ThrowImproperUseOfFramework(exceptionMessage);
        }
        private static void ThrowIfTypeIsNotEqualToAllVerificationParameterEnumerableElementTypes(
            Verification verification,
            VerifiableItem verifiableItem,
            TypeValidation typeValidation)
        {
            var verifiableItemType = verifiableItem.ItemType;

            foreach (var verificationParameter in verification.VerificationParameters)
            {
                var elementType = verificationParameter.ParameterType.GetClosedEnumerableElementType();

                if (verifiableItemType != elementType)
                {
                    var expectedType = verificationParameter.ParameterType.GetGenericTypeDefinition().MakeGenericType(verifiableItemType);

                    ThrowVerificationParameterUnexpectedType(verification.Name, verificationParameter.ParameterType, verificationParameter.Name, expectedType);
                }
            }
        }
Beispiel #16
0
#pragma warning disable SA1201

        private static void ExecuteVerification(
            this AssertionTracker assertionTracker,
            Verification verification)
        {
            assertionTracker.ThrowImproperUseOfFrameworkIfDetected(AssertionTrackerShould.Exist, AssertionTrackerShould.BeMusted);

            var hasBeenEached = assertionTracker.Actions.HasFlag(Actions.Eached);

            if (hasBeenEached)
            {
                // check that the subject is an IEnumerable and not null
                if (!assertionTracker.Actions.HasFlag(Actions.VerifiedAtLeastOnce))
                {
                    var eachVerification = new Verification
                    {
                        Name = nameof(WorkflowExtensions.Each),
                    };

                    var eachVerifiableItem = new VerifiableItem
                    {
                        ItemValue = assertionTracker.SubjectValue,
                        ItemType  = assertionTracker.SubjectType,
                        ItemIsElementInEnumerable = false,
                    };

                    ThrowIfNotAssignableToType(eachVerification, eachVerifiableItem, MustBeEnumerableTypeValidations.Single());

                    NotBeNullInternal(assertionTracker, eachVerification, eachVerifiableItem);
                }

                var valueAsEnumerable = (IEnumerable)assertionTracker.SubjectValue;

                var enumerableElementType = assertionTracker.SubjectType.GetClosedEnumerableElementType();

                var verifiableItem = new VerifiableItem
                {
                    ItemIsElementInEnumerable = true,
                    ItemType = enumerableElementType,
                };

                foreach (var typeValidation in verification.TypeValidations ?? new TypeValidation[] { })
                {
                    typeValidation.Handler(verification, verifiableItem, typeValidation);
                }

                foreach (var element in valueAsEnumerable)
                {
                    verifiableItem.ItemValue = element;

                    verification.Handler(assertionTracker, verification, verifiableItem);
                }
            }
            else
            {
                var verifiableItem = new VerifiableItem
                {
                    ItemIsElementInEnumerable = false,
                    ItemValue = assertionTracker.SubjectValue,
                    ItemType  = assertionTracker.SubjectType,
                };

                foreach (var typeValidation in verification.TypeValidations ?? new TypeValidation[] { })
                {
                    typeValidation.Handler(verification, verifiableItem, typeValidation);
                }

                verification.Handler(assertionTracker, verification, verifiableItem);
            }

            assertionTracker.Actions |= Actions.VerifiedAtLeastOnce;
        }