/// <summary>
        /// Converts all of the hierarchy of <see cref="Value"/> instances within the specified context into
        /// an equivalent hierarchy of <see cref="CSF.Validation.Manifest.ManifestValue"/>, which are returned
        /// as a result object.
        /// </summary>
        /// <param name="context">A conversion context.</param>
        /// <returns>A result object containing the converted manifest values.</returns>
        /// <exception cref="ValidatorBuildingException">If the input value(s) are not valid for creating a validation manifest.</exception>
        /// <exception cref="System.ArgumentNullException">If the <paramref name="context"/> is <see langword="null"/>.</exception>
        public ModelToManifestValueConversionResult ConvertAllValues(ModelToManifestConversionContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var openList = new Queue <ModelToManifestConversionContext>(new[] { context });
            var result   = new ModelToManifestValueConversionResult();

            while (openList.Count != 0)
            {
                var current = openList.Dequeue();
                var value   = contextToItemConverter.GetManifestItem(current);

                if (result.RootValue is null && value is ManifestValue manifestValue)
                {
                    result.RootValue = manifestValue;
                }

                FindAndAddChildrenToOpenList(openList, current, value);

                result.ConvertedValues.Add(new ModelAndManifestValuePair
                {
                    ModelValue    = current.CurrentValue,
                    ManifestValue = value,
                });
            }

            return(result);
        }
예제 #2
0
        public void GetValidationManifestShouldGetAValidationManifestUsingServices([Frozen] IConvertsModelValuesToManifestValues valueConverter,
                                                                                   [Frozen] IConvertsModelRulesToManifestRules ruleConverter,
                                                                                   ValidationManifestFromModelConverter sut,
                                                                                   [ManifestModel] Value rootValue,
                                                                                   Type validatedType,
                                                                                   [ManifestModel] ModelToManifestValueConversionResult conversionResult)
        {
            Mock.Get(valueConverter)
            .Setup(x => x.ConvertAllValues(It.Is <ModelToManifestConversionContext>(c => c.CurrentValue == rootValue && c.ValidatedType == validatedType)))
            .Returns(conversionResult);

            var result = sut.GetValidationManifest(rootValue, validatedType);

            Assert.Multiple(() =>
            {
                Assert.That(result, Has.Property(nameof(ValidationManifest.RootValue)).SameAs(conversionResult.RootValue), "Correct root value");
                Assert.That(result, Has.Property(nameof(ValidationManifest.ValidatedType)).SameAs(validatedType), "Correct validated type");
            });

            Mock.Get(ruleConverter).Verify(x => x.ConvertAllRulesAndAddToManifestValues(conversionResult.ConvertedValues), Times.Once, "Rule converter was used correctly");
        }