Beispiel #1
0
        public MappingAccordances <TSource, TDestination> Register <TSourceValue, TDestinationValue>(
            [NotNull] Expression <Func <TSource, TSourceValue> > sourcePropertySelectorExpression,
            [NotNull] Expression <Func <TDestination, TDestinationValue> > destinationPropertySelectorExpression,
            [NotNull] MappingAccordances <TSourceValue, TDestinationValue> innerMappingAccordances)
        {
            Assert.That(sourcePropertySelectorExpression, Is.Not.Null);
            Assert.That(destinationPropertySelectorExpression, Is.Not.Null);
            Assert.That(innerMappingAccordances, Is.Not.Null);

            var sourcePropertySelector      = sourcePropertySelectorExpression.Compile();
            var destinationPropertySelector = destinationPropertySelectorExpression.Compile();

            var sourceExpression      = MappingAccordances.ToReadableString(sourcePropertySelectorExpression);
            var destinationExpression = MappingAccordances.ToReadableString(destinationPropertySelectorExpression);

            _assertions.Add(
                (source, destination, parentFailureMessage) =>
            {
                var sourcePropertyValue      = sourcePropertySelector(source);
                var destinationPropertyValue = destinationPropertySelector(destination);

                var failureMessage = MappingAccordances.CreateDetailedFailureMessage(
                    @"The values are expected to match",
                    sourceExpression,
                    destinationExpression,
                    parentFailureMessage);

                innerMappingAccordances.AssertAllInternal(
                    sourcePropertyValue,
                    destinationPropertyValue,
                    failureMessage);
            });

            return(this);
        }
Beispiel #2
0
        public MappingAccordances <TSource, TDestination> Register <TSourceItem, TDestinationItem>(
            [NotNull] Expression <Func <TSource, IList <TSourceItem> > > sourcePropertySelectorExpression,
            [NotNull] Expression <Func <TDestination, IList <TDestinationItem> > > destinationPropertySelectorExpression,
            [NotNull] MappingAccordances <TSourceItem, TDestinationItem> innerMappingAccordances)
        {
            Assert.That(sourcePropertySelectorExpression, Is.Not.Null);
            Assert.That(destinationPropertySelectorExpression, Is.Not.Null);
            Assert.That(innerMappingAccordances, Is.Not.Null);

            var sourcePropertySelector      = sourcePropertySelectorExpression.Compile();
            var destinationPropertySelector = destinationPropertySelectorExpression.Compile();

            var sourceExpression      = MappingAccordances.ToReadableString(sourcePropertySelectorExpression);
            var destinationExpression = MappingAccordances.ToReadableString(destinationPropertySelectorExpression);

            _assertions.Add(
                (source, destination, parentFailureMessage) =>
            {
                var sourcePropertyValue      = sourcePropertySelector(source);
                var destinationPropertyValue = destinationPropertySelector(destination);

                var isSourcePropertyValueNull      = MappingAccordances.IsNullReference(sourcePropertyValue);
                var isDestinationPropertyValueNull = MappingAccordances.IsNullReference(destinationPropertyValue);

                var nullMismatchMessage = MappingAccordances.CreateDetailedFailureMessage(
                    MappingAccordances.ListValueNullMismatchMessage,
                    sourceExpression,
                    destinationExpression,
                    parentFailureMessage);

                Assert.That(
                    isDestinationPropertyValueNull,
                    Is.EqualTo(isSourcePropertyValueNull),
                    nullMismatchMessage);

                if (isSourcePropertyValueNull && isDestinationPropertyValueNull)
                {
                    return;
                }

                var itemCount = destinationPropertyValue.Count;

                var countMismatchMessage = MappingAccordances.CreateDetailedFailureMessage(
                    MappingAccordances.ListValueCountMismatchMessage,
                    sourceExpression,
                    destinationExpression,
                    parentFailureMessage);

                Assert.That(itemCount, Is.EqualTo(sourcePropertyValue.Count), countMismatchMessage);

                for (var index = 0; index < itemCount; index++)
                {
                    var sourceItem      = sourcePropertyValue[index];
                    var destinationItem = destinationPropertyValue[index];

                    var itemMismatchMessage = MappingAccordances.CreateDetailedFailureMessage(
                        $@"The source and destination must have the matching item at index {index}",
                        sourceExpression,
                        destinationExpression,
                        parentFailureMessage);

                    innerMappingAccordances.AssertAllInternal(sourceItem, destinationItem, itemMismatchMessage);
                }
            });

            return(this);
        }