/// <inheritdoc/>
        public ValidatorBuilderContext GetPolymorphicContext(ValidatorBuilderContext validatorContext, Type derivedType)
        {
            if (validatorContext is null)
            {
                throw new ArgumentNullException(nameof(validatorContext));
            }
            if (derivedType is null)
            {
                throw new ArgumentNullException(nameof(derivedType));
            }
            if (!(validatorContext.ManifestValue is IHasPolymorphicTypes polyManifest))
            {
                var message = String.Format(Resources.ExceptionMessages.GetExceptionMessage("MustImplementPolymorphicInterface"),
                                            typeof(IHasPolymorphicTypes).Name,
                                            validatorContext.ManifestValue?.GetType().FullName ?? "<null>");
                throw new ArgumentException(message, nameof(validatorContext));
            }

            ManifestPolymorphicType existingPoly;

            if ((existingPoly = polyManifest.PolymorphicTypes.FirstOrDefault(x => x.ValidatedType == derivedType)) != null)
            {
                return(new ValidatorBuilderContext(existingPoly));
            }

            var polymorphicValue = new ManifestPolymorphicType
            {
                Parent        = validatorContext.ManifestValue.Parent,
                ValidatedType = derivedType,
            };

            polyManifest.PolymorphicTypes.Add(polymorphicValue);
            return(new ValidatorBuilderContext(polymorphicValue));
        }
        public void WhenValueIsShouldAddAPolymorphicTypeToTheManifestValue([Frozen] IGetsValidatorBuilderContext contextFactory,
                                                                           [Frozen, ManifestModel] ValidatorBuilderContext context,
                                                                           ValueAccessorBuilder <object, ValidatedObject> sut,
                                                                           [ManifestModel] ManifestPolymorphicType polymorphicType)
        {
            var derivedContext = new ValidatorBuilderContext(polymorphicType);

            Mock.Get(contextFactory).Setup(x => x.GetPolymorphicContext(context, typeof(DerivedValidatedObject))).Returns(derivedContext);
            sut.WhenValueIs <DerivedValidatedObject>(c => { });

            Assert.Multiple(() =>
            {
                var manifestValue = sut.GetManifestValue();
                Assert.That(manifestValue,
                            Is.InstanceOf <IHasPolymorphicTypes>(),
                            "Manifest value has polymorphic types.");
                Assert.That(manifestValue,
                            Has.Property(nameof(IHasPolymorphicTypes.PolymorphicTypes)).One.SameAs(polymorphicType),
                            "Manifest includes expected polymotphic type");
            });
        }
示例#3
0
        /// <inheritdoc/>
        public IManifestItem GetManifestItem(ModelToManifestConversionContext context)
        {
            if (context.ConversionType != ModelToManifestConversionType.PolymorphicType)
            {
                return(next.GetManifestItem(context));
            }

            var validatedType = Type.GetType(context.PolymorphicTypeName, true);

            var polymorphicType = new ManifestPolymorphicType
            {
                Parent        = context.ParentManifestValue,
                ValidatedType = validatedType,
            };

            if (context.ParentManifestValue is IHasPolymorphicTypes polyParent)
            {
                polyParent.PolymorphicTypes.Add(polymorphicType);
            }

            return(polymorphicType);
        }
        public void GetPolymorphicContextShouldReturnAContextFromAnExistingPolymorphicTypeIfItExists([ManifestModel] ManifestPolymorphicType polymorphicValue,
                                                                                                     [ManifestModel] ManifestValue manifestValue,
                                                                                                     ValidatorBuilderContextFactory sut)
        {
            manifestValue.PolymorphicTypes.Add(polymorphicValue);
            polymorphicValue.ValidatedType = typeof(string);
            var validationContext = new ValidatorBuilderContext(manifestValue);

            Assert.That(() => sut.GetPolymorphicContext(validationContext, typeof(string))?.ManifestValue,
                        Is.SameAs(polymorphicValue));
        }
        public void GetPolymorphicContextShouldThrowIfManifestValueCannotHavePolymorphicTypes([ManifestModel] ManifestPolymorphicType manifestModel,
                                                                                              ValidatorBuilderContextFactory sut)
        {
            var validationContext = new ValidatorBuilderContext(manifestModel);

            Assert.That(() => sut.GetPolymorphicContext(validationContext, typeof(object)),
                        Throws.ArgumentException.And.Message.StartWith("The validation manifest value for the current context must implement IHasPolymorphicTypes"));
        }
        public void GetChildManifestValuesShouldCombineApplicablePolymorphicValuesWithManifestValue([ManifestModel] ManifestValue value,
                                                                                                    [ManifestModel] ManifestPolymorphicType type1,
                                                                                                    [ManifestModel] ManifestPolymorphicType type2,
                                                                                                    [ManifestModel] ManifestPolymorphicType type3,
                                                                                                    [ManifestModel] ManifestValue child1,
                                                                                                    [ManifestModel] ManifestValue child2,
                                                                                                    [ManifestModel] ManifestValue child3,
                                                                                                    [ManifestModel] ManifestValue child4)
        {
            value.ValidatedType = typeof(Person);
            type1.ValidatedType = typeof(Employee);
            type2.ValidatedType = typeof(Manager);
            type3.ValidatedType = typeof(Cleaner);
            value.Children = new[] { child1 };
            type1.Children = new[] { child2 };
            type2.Children = new[] { child3 };
            type3.Children = new[] { child4 };
            value.PolymorphicTypes = new[] { type1, type2, type3 };
            var response = new SuccessfulGetValueToBeValidatedResponse(new Manager());
            var sut = new ValidatedValueBasis(value, response, null);

            Assert.That(() => sut.GetChildManifestValues(), Is.EquivalentTo(new[] { child1, child2, child3 }));
        }