Example #1
0
        public void GetManifestRulesShouldIterateOverEveryRuleAdded([Frozen, ManifestModel] ValidatorBuilderContext context,
                                                                    [Frozen] IGetsValidatorBuilderContext ruleContextFactory,
                                                                    [Frozen] IGetsRuleBuilder ruleBuilderFactory,
                                                                    ValidatorBuilder <ValidatedObject> sut,
                                                                    [ManifestModel] ManifestValue value)
        {
            Mock.Get(ruleBuilderFactory)
            .Setup(x => x.GetRuleBuilder <ObjectRule>(It.IsAny <ValidatorBuilderContext>(), It.IsAny <Action <IConfiguresRule <ObjectRule> > >()))
            .Returns(() => {
                var ruleBuilder = new Mock <IBuildsRule <ObjectRule> >();
                ruleBuilder
                .Setup(x => x.GetManifestValue())
                .Returns(() => value);
                return(ruleBuilder.Object);
            });

            sut.AddRule <ObjectRule>();
            sut.AddRule <ObjectRule>();

            sut.GetManifestValue();

            Mock.Get(ruleBuilderFactory)
            .Verify(x => x.GetRuleBuilder <ObjectRule>(context, It.IsAny <Action <IConfiguresRule <ObjectRule> > >()),
                    Times.Exactly(2));
        }
Example #2
0
        public void ForValuesShouldPassConfigurationActionToBuilder([Frozen, ManifestModel] ValidatorBuilderContext context,
                                                                    [Frozen] IGetsValidatorBuilderContext ruleContextFactory,
                                                                    [Frozen] IGetsValueAccessorBuilder valueBuilderFactory,
                                                                    ValidatorBuilder <ValidatedObject> sut,
                                                                    [ManifestModel] ValidatorBuilderContext ruleContext,
                                                                    IBuildsValueAccessor <ValidatedObject, char> valueBuilder,
                                                                    [ManifestModel] ManifestValue value)
        {
            Mock.Get(ruleContextFactory)
            .Setup(x => x.GetContextForValue(It.IsAny <Func <ValidatedObject, IEnumerable <char> > >(), context, false))
            .Returns(ruleContext);
            Mock.Get(ruleContextFactory)
            .Setup(x => x.GetContextForValue <ValidatedObject, char>(null, ruleContext, true))
            .Returns(ruleContext);
            Mock.Get(valueBuilderFactory)
            .Setup(x => x.GetValueAccessorBuilder <ValidatedObject, char>(ruleContext, It.IsAny <Action <IConfiguresValueAccessor <ValidatedObject, char> > >()))
            .Returns(valueBuilder);
            Mock.Get(valueBuilder)
            .Setup(x => x.GetManifestValue())
            .Returns(() => value);

            Action <IConfiguresValueAccessor <ValidatedObject, char> > configAction = c => c.AddRuleWithParent <CharValueRule>();

            sut.ForValues(x => x.AProperty, configAction);

            Mock.Get(valueBuilderFactory)
            .Verify(x => x.GetValueAccessorBuilder <ValidatedObject, char>(ruleContext, configAction), Times.Once);
        }
Example #3
0
        public void ForValuesShouldAddBuilderReturnedFromValueBuilderFactory([Frozen, ManifestModel] ValidatorBuilderContext context,
                                                                             [Frozen] IGetsValidatorBuilderContext ruleContextFactory,
                                                                             [Frozen] IGetsValueAccessorBuilder valueBuilderFactory,
                                                                             ValidatorBuilder <ValidatedObject> sut,
                                                                             [ManifestModel] ValidatorBuilderContext ruleContext,
                                                                             IBuildsValueAccessor <ValidatedObject, char> valueBuilder,
                                                                             [ManifestModel] ManifestValue value)
        {
            Mock.Get(ruleContextFactory)
            .Setup(x => x.GetContextForValue(It.IsAny <Func <ValidatedObject, IEnumerable <char> > >(), context, false))
            .Returns(ruleContext);
            Mock.Get(ruleContextFactory)
            .Setup(x => x.GetContextForValue <ValidatedObject, char>(null, ruleContext, true))
            .Returns(ruleContext);
            Mock.Get(valueBuilderFactory)
            .Setup(x => x.GetValueAccessorBuilder <ValidatedObject, char>(ruleContext, It.IsAny <Action <IConfiguresValueAccessor <ValidatedObject, char> > >()))
            .Returns(valueBuilder);
            Mock.Get(valueBuilder)
            .Setup(x => x.GetManifestValue())
            .Returns(() => value);

            sut.ForValues(x => x.AProperty, c => {});

            Assert.That(sut.GetManifestValue().Children.Single(), Is.SameAs(value));
        }
Example #4
0
        public void GetValueAccessorBuilderShouldReturnAnAccessorBuilder([ManifestModel] ValidatorBuilderContext context,
                                                                         IGetsRuleBuilder ruleBuilderFactory,
                                                                         IGetsValidatorManifest validatorManifestFactory,
                                                                         IGetsValidatorBuilderContext contextFactory)
        {
            var sut = new ValueAccessorBuilderFactory(() => ruleBuilderFactory, () => validatorManifestFactory, () => contextFactory);

            Assert.That(() => sut.GetValueAccessorBuilder <ValidatedObject, string>(context, c => { }), Is.Not.Null);
        }
Example #5
0
 /// <summary>
 /// Initializes a new instance of <see cref="ValueAccessorBuilder{TValidated, TValue}"/>.
 /// </summary>
 /// <param name="context">The context which should be used for newly-added rule-builders.</param>
 /// <param name="ruleBuilderFactory">A factory for rule-builder instances.</param>
 /// <param name="validatorManifestFactory">A factory for validator manifest instances.</param>
 /// <param name="builderContextFactory">A factory for validator builder contexts.</param>
 public ValueAccessorBuilder(ValidatorBuilderContext context,
                             IGetsRuleBuilder ruleBuilderFactory,
                             IGetsValidatorManifest validatorManifestFactory,
                             IGetsValidatorBuilderContext builderContextFactory)
 {
     this.validatorManifestFactory = validatorManifestFactory ?? throw new ArgumentNullException(nameof(validatorManifestFactory));
     this.builderContextFactory    = builderContextFactory ?? throw new ArgumentNullException(nameof(builderContextFactory));
     this.context            = context ?? throw new ArgumentNullException(nameof(context));
     this.ruleBuilderFactory = ruleBuilderFactory ?? throw new ArgumentNullException(nameof(ruleBuilderFactory));
 }
Example #6
0
        public void GetValueAccessorBuilderShouldExecuteConfigurationUponBuilder([ManifestModel] ValidatorBuilderContext context,
                                                                                 IGetsRuleBuilder ruleBuilderFactory,
                                                                                 IGetsValidatorManifest validatorManifestFactory,
                                                                                 IGetsValidatorBuilderContext contextFactory)
        {
            var sut = new ValueAccessorBuilderFactory(() => ruleBuilderFactory, () => validatorManifestFactory, () => contextFactory);

            sut.GetValueAccessorBuilder <ValidatedObject, string>(context, c => c.AddRuleWithParent <StringValueRule>());
            Mock.Get(ruleBuilderFactory)
            .Verify(x => x.GetRuleBuilder <StringValueRule>(context, It.IsAny <Action <IConfiguresRule <StringValueRule> > >()), Times.Once);
        }
Example #7
0
        public void AddRuleShouldAddRuleCreatedFromFactoryUsingContext([Frozen, ManifestModel] ValidatorBuilderContext context,
                                                                       [Frozen] IGetsValidatorBuilderContext ruleContextFactory,
                                                                       [Frozen] IGetsRuleBuilder ruleBuilderFactory,
                                                                       IBuildsRule <ObjectRule> ruleBuilder,
                                                                       ValidatorBuilder <ValidatedObject> sut,
                                                                       [ManifestModel] ManifestValue value)
        {
            Mock.Get(ruleBuilderFactory)
            .Setup(x => x.GetRuleBuilder <ObjectRule>(context, It.IsAny <Action <IConfiguresRule <ObjectRule> > >()))
            .Returns(ruleBuilder);
            Mock.Get(ruleBuilder)
            .Setup(x => x.GetManifestValue())
            .Returns(() => value);

            sut.AddRule <ObjectRule>();

            Assert.That(() => sut.GetManifestValue().Children.Single(), Is.SameAs(value));
        }
Example #8
0
        public void GetValidatorManifestWithRuleContextShouldReturnAValidatorBuilder([Frozen] IServiceProvider serviceProvider,
                                                                                     ImportedValidatorBuilderManifestFactory sut,
                                                                                     IGetsValidatorBuilderContext ruleContextFactory,
                                                                                     IGetsRuleBuilder ruleBuilderFactory,
                                                                                     IGetsValueAccessorBuilder valueBuilderFactory,
                                                                                     IGetsValidatorManifest validatorManifestFactory,
                                                                                     IResolvesServices resolver,
                                                                                     [ManifestModel] ValidatorBuilderContext context)
        {
            Mock.Get(serviceProvider).Setup(x => x.GetService(typeof(IGetsValidatorBuilderContext))).Returns(ruleContextFactory);
            Mock.Get(serviceProvider).Setup(x => x.GetService(typeof(IGetsRuleBuilder))).Returns(ruleBuilderFactory);
            Mock.Get(serviceProvider).Setup(x => x.GetService(typeof(IGetsValueAccessorBuilder))).Returns(valueBuilderFactory);
            Mock.Get(serviceProvider).Setup(x => x.GetService(typeof(IGetsValidatorManifest))).Returns(validatorManifestFactory);
            Mock.Get(serviceProvider).Setup(x => x.GetService(typeof(IResolvesServices))).Returns(resolver);
            Mock.Get(resolver).Setup(x => x.ResolveService <object>(typeof(GenericValidatorDefinition <object>))).Returns(() => new GenericValidatorDefinition <object>());

            var result = sut.GetValidatorManifest(typeof(GenericValidatorDefinition <object>), context);

            Assert.That(result, Is.InstanceOf <ValidatorBuilder <object> >());
        }
        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");
            });
        }
Example #10
0
        public void ValidateAsAncestorShouldThrowIfForMemberHasAlreadyBeenUsed([Frozen, ManifestModel] ValidatorBuilderContext context,
                                                                               [Frozen] IGetsValidatorBuilderContext ruleContextFactory,
                                                                               [Frozen] IGetsValueAccessorBuilder valueBuilderFactory,
                                                                               ValidatorBuilder <ValidatedObject> sut,
                                                                               ValidatorBuilderContext ruleContext,
                                                                               IBuildsValueAccessor <ValidatedObject, string> valueBuilder,
                                                                               [ManifestModel] ManifestValue value)
        {
            Mock.Get(ruleContextFactory)
            .Setup(x => x.GetContextForMember(It.IsAny <Expression <Func <ValidatedObject, string> > >(), context, false))
            .Returns(ruleContext);
            Mock.Get(valueBuilderFactory)
            .Setup(x => x.GetValueAccessorBuilder <ValidatedObject, string>(ruleContext, It.IsAny <Action <IConfiguresValueAccessor <ValidatedObject, string> > >()))
            .Returns(valueBuilder);
            Mock.Get(valueBuilder)
            .Setup(x => x.GetManifestValue())
            .Returns(() => value);

            sut.ForMember(x => x.AProperty, v => { });
            Assert.That(() => sut.ValidateAsAncestor(1), Throws.InvalidOperationException);
        }
Example #11
0
        public void GetValidatorManifestShouldExecuteConfigureValidatorFromDefinitionUponBuilder([Frozen] IServiceProvider serviceProvider,
                                                                                                 ImportedValidatorBuilderManifestFactory sut,
                                                                                                 IGetsValidatorBuilderContext ruleContextFactory,
                                                                                                 IGetsRuleBuilder ruleBuilderFactory,
                                                                                                 IGetsValueAccessorBuilder valueBuilderFactory,
                                                                                                 IGetsValidatorManifest validatorManifestFactory,
                                                                                                 IResolvesServices resolver,
                                                                                                 [ManifestModel] ValidatorBuilderContext context,
                                                                                                 ValidatedObject obj)
        {
            Mock.Get(serviceProvider).Setup(x => x.GetService(typeof(IGetsValidatorBuilderContext))).Returns(ruleContextFactory);
            Mock.Get(serviceProvider).Setup(x => x.GetService(typeof(IGetsRuleBuilder))).Returns(ruleBuilderFactory);
            Mock.Get(serviceProvider).Setup(x => x.GetService(typeof(IGetsValueAccessorBuilder))).Returns(valueBuilderFactory);
            Mock.Get(serviceProvider).Setup(x => x.GetService(typeof(IGetsValidatorManifest))).Returns(validatorManifestFactory);
            Mock.Get(serviceProvider).Setup(x => x.GetService(typeof(IResolvesServices))).Returns(resolver);
            Mock.Get(resolver).Setup(x => x.ResolveService <object>(typeof(ValidatedObjectDefinition))).Returns(() => new ValidatedObjectDefinition());

            sut.GetValidatorManifest(typeof(ValidatedObjectDefinition), context);

            Assert.That(() => context.ManifestValue.IdentityAccessor(obj),
                        Is.EqualTo(obj.Identity),
                        $@"Because the definition sets up the context to use the {nameof(ValidatedObject.Identity)} property
for identity, asserting that they are equal proves that the configuration function was executed.");
        }