Esempio n. 1
0
        public void GetManifestRuleShouldReturnAManifestRuleWithCorrectDependencies([Frozen] IGetsManifestRuleIdentifierFromRelativeIdentifier relativeToManifestIdentityConverter,
                                                                                    [Frozen] IGetsManifestRuleIdentifier identifierFactory,
                                                                                    [Frozen, ManifestModel] ValidatorBuilderContext context,
                                                                                    RuleBuilder <SampleRule> sut,
                                                                                    [ManifestModel] ManifestRuleIdentifier identifier,
                                                                                    RelativeRuleIdentifier relativeId1,
                                                                                    RelativeRuleIdentifier relativeId2,
                                                                                    RelativeRuleIdentifier relativeId3,
                                                                                    [ManifestModel] ManifestRuleIdentifier manifestId1,
                                                                                    [ManifestModel] ManifestRuleIdentifier manifestId2,
                                                                                    [ManifestModel] ManifestRuleIdentifier manifestId3)
        {
            Mock.Get(identifierFactory)
            .Setup(x => x.GetManifestRuleIdentifier(typeof(SampleRule), It.IsAny <ValidatorBuilderContext>(), It.IsAny <string>()))
            .Returns(identifier);
            Mock.Get(relativeToManifestIdentityConverter).Setup(x => x.GetManifestRuleIdentifier(context.ManifestValue, relativeId1)).Returns(manifestId1);
            Mock.Get(relativeToManifestIdentityConverter).Setup(x => x.GetManifestRuleIdentifier(context.ManifestValue, relativeId2)).Returns(manifestId2);
            Mock.Get(relativeToManifestIdentityConverter).Setup(x => x.GetManifestRuleIdentifier(context.ManifestValue, relativeId3)).Returns(manifestId3);

            sut.Dependencies = new[] { relativeId1, relativeId2, relativeId3 };

            var result = sut.GetManifestValue().Rules.Single();

            Assert.That(result, Has.Property(nameof(ManifestRule.DependencyRules)).EquivalentTo(new[] { manifestId1, manifestId2, manifestId3 }));
        }
        static IManifestItem GetManifestValue(IManifestItem baseValue, RelativeRuleIdentifier relativeIdentifier)
        {
            var current = baseValue;

            for (var i = relativeIdentifier.AncestorLevels; i > 0; i--)
            {
                current = current?.Parent ?? throw GetInsufficientParentsException(relativeIdentifier.AncestorLevels, nameof(relativeIdentifier));
            }

            var memberName = relativeIdentifier.MemberName;

            if (!String.IsNullOrEmpty(memberName))
            {
                current = current.Children.SingleOrDefault(x => x.MemberName == memberName) ?? throw GetCannotFindMemberException(memberName, nameof(relativeIdentifier));
            }

            return(current);
        }
        /// <summary>
        /// Gets the manifest rule identifier from a relative rule identifier.
        /// </summary>
        /// <param name="currentValue">The current manifest value from which the <paramref name="relativeIdentifier"/> should be derived.</param>
        /// <param name="relativeIdentifier">The relative rule identifier.</param>
        /// <returns>A manifest rule identifier.</returns>
        public ManifestRuleIdentifier GetManifestRuleIdentifier(IManifestItem currentValue, RelativeRuleIdentifier relativeIdentifier)
        {
            if (currentValue is null)
            {
                throw new ArgumentNullException(nameof(currentValue));
            }
            if (relativeIdentifier is null)
            {
                throw new ArgumentNullException(nameof(relativeIdentifier));
            }

            var manifestValue = GetManifestValue(currentValue, relativeIdentifier);

            return(new ManifestRuleIdentifier(manifestValue, relativeIdentifier.RuleType, relativeIdentifier.RuleName));
        }