Ejemplo n.º 1
0
        private static void Validate(
            this Parameter parameter,
            Validation validation)
        {
            ParameterValidator.ThrowImproperUseOfFrameworkIfDetected(parameter, ParameterShould.BeMusted);

            validation.ParameterName         = parameter.Name;
            validation.IsElementInEnumerable = parameter.HasBeenEached;

            if (parameter.HasBeenEached)
            {
                // check that the parameter is an IEnumerable and not null
                if (!parameter.HasBeenValidated)
                {
                    var eachValidation = new Validation
                    {
                        ParameterName         = parameter.Name,
                        ValidationName        = nameof(ParameterValidator.Each),
                        Value                 = parameter.Value,
                        ValueType             = parameter.ValueType,
                        IsElementInEnumerable = false,
                    };

                    ThrowIfNotOfType(eachValidation, MustBeEnumerableTypeValidations.Single());
                    NotBeNullInternal(eachValidation);
                }

                var valueAsEnumerable = (IEnumerable)parameter.Value;
                var enumerableType    = GetEnumerableGenericType(parameter.ValueType);
                validation.ValueType = enumerableType;

                foreach (var typeValidation in validation.TypeValidations ?? new TypeValidation[] { })
                {
                    typeValidation.TypeValidationHandler(validation, typeValidation);
                }

                foreach (var element in valueAsEnumerable)
                {
                    validation.Value = element;

                    validation.ValueValidationHandler(validation);
                }
            }
            else
            {
                validation.Value     = parameter.Value;
                validation.ValueType = parameter.ValueType;

                foreach (var typeValidation in validation.TypeValidations ?? new TypeValidation[] { })
                {
                    typeValidation.TypeValidationHandler(validation, typeValidation);
                }

                validation.ValueValidationHandler(validation);
            }

            parameter.HasBeenValidated = true;
        }
Ejemplo n.º 2
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;
        }