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 <TSourceValue, TDestinationValue>(
            [NotNull] Expression <Func <TSource, TSourceValue> > sourcePropertySelectorExpression,
            [NotNull] Expression <Func <TDestination, TDestinationValue> > destinationPropertySelectorExpression,
            [NotNull] ConstraintCreator <TSourceValue> createConstraintFromSourcePropertyValue,
            [NotNull] MappingAccordances.AssertionFailedMessageCreator <TSourceValue, TDestinationValue>
            getAssertionFailureMessage)
        {
            Assert.That(sourcePropertySelectorExpression, Is.Not.Null);
            Assert.That(destinationPropertySelectorExpression, Is.Not.Null);
            Assert.That(createConstraintFromSourcePropertyValue, Is.Not.Null);
            Assert.That(getAssertionFailureMessage, 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 constraint = createConstraintFromSourcePropertyValue(sourcePropertyValue);

                Assert.That(
                    constraint,
                    Is.Not.Null,
                    $@"A factory method specified in the argument '{
                            nameof(createConstraintFromSourcePropertyValue)}' returned an invalid value.");

                var baseFailureMessage = getAssertionFailureMessage(sourcePropertyValue, destinationPropertyValue);

                Assert.That(
                    baseFailureMessage,
                    Is.Not.Null.And.Not.Empty,
                    $@"A factory method specified in the argument '{
                            nameof(getAssertionFailureMessage)}' returned an invalid value.");

                var failureMessage = MappingAccordances.CreateDetailedFailureMessage(
                    baseFailureMessage,
                    sourceExpression,
                    destinationExpression,
                    parentFailureMessage);

                Assert.That(destinationPropertyValue, constraint, failureMessage);
            });

            return(this);
        }
Beispiel #3
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);
        }