public void GetCandidateMessageProviderTypesShouldAlwaysReturnWrappedResults([Frozen] IGetsCandidateMessageTypes wrapped,
                                                                              RuleWithMessageCandidateTypeDecorator sut,
                                                                              MessageProviderTypeInfo typeInfo1,
                                                                              MessageProviderTypeInfo typeInfo2,
                                                                              MessageProviderTypeInfo typeInfo3,
                                                                              [RuleResult] ValidationRuleResult result)
 {
     Mock.Get(wrapped).Setup(x => x.GetCandidateMessageProviderTypes(result)).Returns(new[] { typeInfo1, typeInfo2, typeInfo3 });
     Assert.That(() => sut.GetCandidateMessageProviderTypes(result), Is.SupersetOf(new[] { typeInfo1, typeInfo2, typeInfo3 }));
 }
 public void GetCandidateMessageProviderTypeShouldNotAddAResultIfTheRuleObjectDoesNotMatchTheRuleWithMessageInterface([Frozen] IGetsCandidateMessageTypes wrapped,
                                                                                                                      RuleWithMessageCandidateTypeDecorator sut,
                                                                                                                      [RuleResult(RuleInterface = typeof(IRule <string>))] ValidationRuleResult result)
 {
     Mock.Get(wrapped).Setup(x => x.GetCandidateMessageProviderTypes(result)).Returns(Array.Empty <MessageProviderTypeInfo>());
     Mock.Get(result.ValidationLogic).SetupGet(x => x.RuleObject).Returns(new DoubleGenericRuleWithMessage());
     Assert.That(() => sut.GetCandidateMessageProviderTypes(result), Is.Empty);
 }
 public void GetCandidateMessageProviderTypeShouldAddAResultForASingleGenericRuleWithMessageWhenItMatches([Frozen] IGetsCandidateMessageTypes wrapped,
                                                                                                          RuleWithMessageCandidateTypeDecorator sut,
                                                                                                          [RuleResult(RuleInterface = typeof(IRule <string>))] ValidationRuleResult result)
 {
     Mock.Get(wrapped).Setup(x => x.GetCandidateMessageProviderTypes(result)).Returns(Array.Empty <MessageProviderTypeInfo>());
     Mock.Get(result.ValidationLogic).SetupGet(x => x.RuleObject).Returns(new SingleGenericRuleWithMessage());
     Assert.That(() => sut.GetCandidateMessageProviderTypes(result), Has.One.InstanceOf <InstanceMessageProviderInfo>());
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initialises a new instance of <see cref="RuleWithMessageCandidateTypeDecorator"/>.
 /// </summary>
 /// <param name="wrapped">The wrapped candidate message type provider.</param>
 /// <exception cref="System.ArgumentNullException">If <paramref name="wrapped"/> is <see langword="null" />.</exception>
 public RuleWithMessageCandidateTypeDecorator(IGetsCandidateMessageTypes wrapped)
 {
     this.wrapped = wrapped ?? throw new System.ArgumentNullException(nameof(wrapped));
 }
 /// <summary>
 /// Initialises a new instance of <see cref="MessageProviderInfoFactory"/>.
 /// </summary>
 /// <param name="typeRegistry">A provider type registry.</param>
 /// <param name="factoryStrategySelector">A service that gets strategies for message provider factories.</param>
 /// <exception cref="ArgumentNullException">If any parameter is <see langword="null" />.</exception>
 public MessageProviderInfoFactory(IGetsCandidateMessageTypes typeRegistry,
                                   IGetsMessageProviderFactoryStrategy factoryStrategySelector)
 {
     this.typeRegistry            = typeRegistry ?? throw new ArgumentNullException(nameof(typeRegistry));
     this.factoryStrategySelector = factoryStrategySelector ?? throw new ArgumentNullException(nameof(factoryStrategySelector));
 }
        public void GetMessageProviderInfoShouldCreateProviderUsingLazyResolutionFromTheFactory([Frozen] IGetsCandidateMessageTypes typeRegistry,
                                                                                                [Frozen] IGetsMessageProviderFactoryStrategy factoryStrategySelector,
                                                                                                MessageProviderInfoFactory sut,
                                                                                                int irrelevantPriority,
                                                                                                [RuleResult] ValidationRuleResult ruleResult,
                                                                                                IGetsNonGenericMessageProvider factory,
                                                                                                IGetsFailureMessage provider)
        {
            MessageProviderTypeInfo
                type = new MessageProviderTypeInfo(typeof(string), irrelevantPriority);

            Mock.Get(typeRegistry).Setup(x => x.GetCandidateMessageProviderTypes(ruleResult)).Returns(new[] { type });
            Mock.Get(factoryStrategySelector).Setup(x => x.GetMessageProviderFactory(type, ruleResult.RuleInterface)).Returns(factory);
            Mock.Get(factory).Setup(x => x.GetNonGenericFailureMessageProvider(type, ruleResult.RuleInterface)).Returns(provider);

            var result = sut.GetMessageProviderInfo(ruleResult);

            Assert.Multiple(() =>
            {
                Mock.Get(factory).Verify(x => x.GetNonGenericFailureMessageProvider(type, It.IsAny <Type>()),
                                         Times.Never,
                                         "Because the provider hasn't been accessed, the factory should not have yet been used");
                Assert.That(result.Single().MessageProvider,
                            Is.SameAs(provider),
                            "The returned message provider is the same as the expected one.");
                Mock.Get(factory).Verify(x => x.GetNonGenericFailureMessageProvider(type, ruleResult.RuleInterface),
                                         Times.Once,
                                         "Now that the provider has been accessed, the factory should have been executed");
            });
        }
        public void GetMessageProviderInfoShouldReturnProvidersCreatedFromRegistryTypeInfoExcludingThoseForWhichThereIsNoFactory([Frozen] IGetsCandidateMessageTypes typeRegistry,
                                                                                                                                 [Frozen] IGetsMessageProviderFactoryStrategy factoryStrategySelector,
                                                                                                                                 MessageProviderInfoFactory sut,
                                                                                                                                 int irrelevantPriority,
                                                                                                                                 [RuleResult] ValidationRuleResult ruleResult,
                                                                                                                                 IGetsNonGenericMessageProvider factory)
        {
            MessageProviderTypeInfo
                type1 = new MessageProviderTypeInfo(typeof(string), irrelevantPriority),
                type2 = new MessageProviderTypeInfo(typeof(int), irrelevantPriority),
                type3 = new MessageProviderTypeInfo(typeof(bool), irrelevantPriority);

            Mock.Get(typeRegistry).Setup(x => x.GetCandidateMessageProviderTypes(ruleResult)).Returns(new[] { type1, type2, type3 });
            Mock.Get(factoryStrategySelector).Setup(x => x.GetMessageProviderFactory(type1, ruleResult.RuleInterface)).Returns(factory);
            Mock.Get(factoryStrategySelector).Setup(x => x.GetMessageProviderFactory(type2, ruleResult.RuleInterface)).Returns(() => null);
            Mock.Get(factoryStrategySelector).Setup(x => x.GetMessageProviderFactory(type3, ruleResult.RuleInterface)).Returns(factory);

            var result = sut.GetMessageProviderInfo(ruleResult);

            Assert.That(result.Select(x => x.ProviderType), Is.EquivalentTo(new[] { typeof(string), typeof(bool) }));
        }