public void IfDo_ConditionIsFalse_DoNotExecuteTransformation()
        {
            var source            = new Foo();
            var destination       = new Bar();
            var customDestination = new Bar();
            var context           = new SomeTransformationContext();

            var builderInstance = _builder.IfDo((s, d, c) => c == null, (s, d, c) =>
            {
                Assert.AreEqual(source, s);
                Assert.AreEqual(destination, d);
                Assert.IsNull(c);

                return(customDestination);
            });

            Assert.AreEqual(_builder, builderInstance);
            Assert.AreEqual(1, _builder.Transformations.Count);
            var transformer = _builder.Transformations[0] as ConditionalTransformer <Foo, Bar>;

            Assert.IsNotNull(transformer);
            var result = transformer.Transform(source, destination, context);

            Assert.AreEqual(destination, result);
        }
Example #2
0
        public void DoTransform_ConditionReturnTrue_CreateDestinationInstance_CallPassedAction_AndReturnResult()
        {
            var source            = new Foo();
            var destination       = new Bar();
            var context           = new SomeTransformationContext();
            var isActionCalled    = false;
            var customDestination = new Bar();
            var newDestination    = new Bar();
            Func <Foo, Bar, TransformationContext, bool> condition = (_, _, _) => true;
            Func <Foo, Bar, TransformationContext, Bar>  action    = (s, d, c) =>
            {
                Assert.AreEqual(source, s);
                Assert.AreEqual(newDestination, d);
                Assert.AreEqual(context, c);
                isActionCalled = true;
                return(customDestination);
            };
            var transformer = A.Fake <ConditionalTransformer <Foo, Bar> >(o => o.WithArgumentsForConstructor(new object[]
            {
                condition, action, false
            })).AsPartial();

            A.CallTo(() => transformer.GetDestinationInstance(context, destination))
            .Returns(newDestination);

            var result = transformer.Transform(source, destination, context);

            Assert.AreEqual(customDestination, result);
            Assert.IsTrue(isActionCalled);
        }
        public void Do_ActionTransformerWithPassedActionAddedToTransformationList()
        {
            var source      = new Foo();
            var destination = new Bar();
            TransformationContext context = new SomeTransformationContext();
            var customDestination         = new Bar();

            var builderInstance = _builder.Do((s, d, c) =>
            {
                Assert.AreEqual(source, s);
                Assert.AreEqual(destination, d);
                Assert.AreEqual(context, c);

                return(customDestination);
            });

            Assert.AreEqual(_builder, builderInstance);
            Assert.AreEqual(1, _builder.Transformations.Count);
            var transformer = _builder.Transformations[0] as ActionTransformer <Foo, Bar>;

            Assert.IsNotNull(transformer);
            var result = transformer.Transform(source, destination, context);

            Assert.AreEqual(customDestination, result);
        }
        public void IfApplyIsolated_AddConditionalTransformer_WithSpecifiedConditionAndTransformation_AndIsolatedResult()
        {
            var transformation         = A.Fake <MyAbstractTransformation>();
            var source                 = new Foo();
            var destination            = new Bar();
            var context                = new SomeTransformationContext();
            var newDestination         = new Bar();
            var transformedDestination = new Bar();

            _builder.Configuration.InstanceFactory = t =>
            {
                if (t == typeof(Bar))
                {
                    return(newDestination);
                }

                return(null);
            };
            A.CallTo(() => transformation.Transform(source, newDestination, context))
            .Returns(transformedDestination);

            var builderInstance = _builder.IfApplyIsolated((s, d, c) => true, transformation);

            Assert.AreEqual(_builder, builderInstance);
            Assert.AreEqual(1, _builder.Transformations.Count);
            var conditionalTransformer = _builder.Transformations[0] as ConditionalTransformer <Foo, Bar>;

            Assert.IsNotNull(conditionalTransformer);
            Assert.IsTrue(conditionalTransformer.IsIsolatedResult);

            var transformationResult = conditionalTransformer.Transform(source, destination, context);

            Assert.AreEqual(transformedDestination, transformationResult);
        }
Example #5
0
        public void GetDestinationInstance_PassedDestinationNotNull_AndIsNotIsolatedTransformer_ReturnOriginalDestination()
        {
            var context     = new SomeTransformationContext();
            var destination = new Bar();

            _transformator.IsIsolatedResult = false;

            var result = _transformator.GetDestinationInstance(context, destination);

            Assert.AreEqual(destination, result);
        }
        public void Transform_IsNotSupport_AndThrowException()
        {
            var source      = new Foo();
            var destination = new Bar();
            var context     = new SomeTransformationContext();

            var exception = Assert.Throws <NotSupportedException>(() => _transformer.Transform(source, destination, context));

            Assert.IsNotNull(exception);
            Assert.AreEqual($"Only multi-transformation is supported via TransformMulti method", exception.Message);
        }
Example #7
0
        public void GetDestinationInstance_PassedDestinationNotNull_AndIsIsolatedTransformer_NoFactoriesConfigured_ReturnNull()
        {
            var context     = new SomeTransformationContext();
            var destination = new Bar();

            _transformator.IsIsolatedResult = true;
            _builder.Configuration.AutoCreateDestination = false;

            var result = _transformator.GetDestinationInstance(context, destination);

            Assert.IsNull(result);
        }
        public void Transform_ByDefaultItCallsDoTransform_AndReturnDestination()
        {
            var source      = new Foo();
            var destination = new Bar();
            var context     = new SomeTransformationContext();

            var result = _transformer.Transform(source, destination, context);

            Assert.AreEqual(destination, result);
            A.CallTo(() => _transformer.DoTransformPublic(source, destination, context))
            .MustHaveHappenedOnceExactly();
        }
Example #9
0
        public void GetDestinationInstance_InitialDestinationFactoryIsSet_UseItAndReturnCreatedInstance()
        {
            Bar newDestination = new Bar();
            var context        = new SomeTransformationContext();
            var destination    = new Bar();

            _transformator.IsIsolatedResult    = true;
            _builder.InitialDestinationFactory = c => newDestination;

            var result = _transformator.GetDestinationInstance(context, destination);

            Assert.AreEqual(newDestination, result);
        }
        public void Transform_CanTransformReturnTransform_CallDoTransform_AndReturnTransformedDestination()
        {
            var source              = new Foo();
            var destination         = new Bar();
            var context             = new SomeTransformationContext();
            var customerDestination = new Bar();

            _transformer.CustomTransformAction = TransformAction.Transform;
            _transformer.CustomDestination     = customerDestination;

            var result = _transformer.Transform(source, destination, context);

            Assert.AreEqual(customerDestination, result);
        }
Example #11
0
        GetDestinationInstance_AutoCreateDestinationIsTrue_AndConfigurationInstanceFactoryIsSet_CreateInstanceWithInstanceFactoryAndReturn()
        {
            var context        = new SomeTransformationContext();
            var destination    = new Bar();
            var newDestination = new Bar();

            _transformator.IsIsolatedResult = true;
            _builder.Configuration.AutoCreateDestination = true;
            _builder.Configuration.InstanceFactory       = t => t == typeof(Bar) ? newDestination : null;

            var result = _transformator.GetDestinationInstance(context, destination);

            Assert.AreEqual(newDestination, result);
        }
Example #12
0
        public void GetDestinationInstance_NoneFactoryIsConfigured_ReturnNull()
        {
            var context        = new SomeTransformationContext();
            var destination    = new Bar();
            var newDestination = new Bar();

            _transformator.IsIsolatedResult = true;
            _builder.Configuration.AutoCreateDestination = false;
            _builder.Configuration.InstanceFactory       = null;

            var result = _transformator.GetDestinationInstance(context, destination);

            Assert.IsNull(result);
        }
Example #13
0
        public void GetDestinationInstance_AutoCreateDestinationIsTrue_ButConfigurationInstanceFactoryIsNull_CreateInstanceWithActivatorAndReturn()
        {
            var context     = new SomeTransformationContext();
            var destination = new Bar();

            _transformator.IsIsolatedResult = true;
            _builder.Configuration.AutoCreateDestination = true;
            _builder.Configuration.InstanceFactory       = null;

            var result = _transformator.GetDestinationInstance(context, destination);

            Assert.IsNotNull(result);
            Assert.AreNotEqual(destination, result);
        }
        public void TransformMulti_ByDefaultItCallsDoMultiTransform_AndReturnResult()
        {
            var source      = new Foo();
            var destination = new Bar();
            var context     = new SomeTransformationContext();

            var result = _transformer.TransformMulti(source, destination, context);

            Assert.IsNotNull(result);
            var resultItems = result.ToList();

            Assert.AreEqual(1, resultItems.Count);
            Assert.AreEqual(destination, resultItems[0]);
            A.CallTo(() => _transformer.DoMultiTransformPublic(source, destination, context))
            .MustHaveHappenedOnceExactly();
        }
        public void Transform_CanTransformReturnBreakTransformation_DoNotCallDoTransform_AndBreakTransformation()
        {
            var source              = new Foo();
            var destination         = new Bar();
            var context             = new SomeTransformationContext();
            var customerDestination = new Bar();

            _transformer.CustomTransformAction = TransformAction.BreakTransformation;
            _transformer.CustomDestination     = customerDestination;

            var result = _transformer.Transform(source, destination, context);

            Assert.IsNull(result);
            A.CallTo(() => _transformer.DoTransformPublic(source, destination, context))
            .MustNotHaveHappened();
        }
        public void Transform_CanTransformReturnPassThrough_DoNotCallDoTransform_AndReturnUnchangedDestination()
        {
            var source              = new Foo();
            var destination         = new Bar();
            var context             = new SomeTransformationContext();
            var customerDestination = new Bar();

            _transformer.CustomTransformAction = TransformAction.PassThrough;
            _transformer.CustomDestination     = customerDestination;

            var result = _transformer.Transform(source, destination, context);

            Assert.AreEqual(destination, result);
            A.CallTo(() => _transformer.DoTransformPublic(source, destination, context))
            .MustNotHaveHappened();
        }
        public void TransformMulti_CanTransformReturnUnknownAction_DoNotCallDoTransform_AndReturnNullDestination()
        {
            var source               = new Foo();
            var destination          = new Bar();
            var context              = new SomeTransformationContext();
            var customerDestination  = new Bar();
            var customerDestination2 = new Bar();

            _transformer.CustomTransformAction = (TransformAction?)345;
            _transformer.CustomDestinations    = new[] { customerDestination, customerDestination2 };

            var result = _transformer.TransformMulti(source, destination, context);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.ToList().Count);
            A.CallTo(() => _transformer.DoMultiTransformPublic(source, destination, context))
            .MustNotHaveHappened();
        }
Example #18
0
        public void DoTransform_ConditionReturnFalse_PassedActionIsNotCalled_AndReturnDestination()
        {
            var source         = new Foo();
            var destination    = new Bar();
            var context        = new SomeTransformationContext();
            var isActionCalled = false;

            var transformer = new ConditionalTransformer <Foo, Bar>((_, _, _) => false, (_, _, _) =>
            {
                isActionCalled = true;
                return(null);
            });

            var result = transformer.Transform(source, destination, context);

            Assert.IsFalse(isActionCalled);
            Assert.AreEqual(destination, result);
        }
        public void TransformMulti_CanTransformReturnTransform_CallDoMultiTransform_AndReturnResult()
        {
            var source               = new Foo();
            var destination          = new Bar();
            var context              = new SomeTransformationContext();
            var customerDestination  = new Bar();
            var customerDestination2 = new Bar();

            _transformer.CustomTransformAction = TransformAction.Transform;
            _transformer.CustomDestinations    = new [] { customerDestination, customerDestination2 };

            var result = _transformer.TransformMulti(source, destination, context);

            Assert.IsNotNull(result);
            var resultItems = result.ToList();

            Assert.AreEqual(2, resultItems.Count);
            Assert.AreEqual(customerDestination, resultItems[0]);
            Assert.AreEqual(customerDestination2, resultItems[1]);
        }
        public void TransformMulti_CanTransformReturnPassThrough_DoNotCallDoTransform_AndReturnUnchangedDestination()
        {
            var source               = new Foo();
            var destination          = new Bar();
            var context              = new SomeTransformationContext();
            var customerDestination  = new Bar();
            var customerDestination2 = new Bar();

            _transformer.CustomTransformAction = TransformAction.PassThrough;
            _transformer.CustomDestinations    = new[] { customerDestination, customerDestination2 };

            var result = _transformer.TransformMulti(source, destination, context);

            Assert.IsNotNull(result);
            var resultItems = result.ToList();

            Assert.AreEqual(1, resultItems.Count);
            Assert.AreEqual(destination, resultItems[0]);
            A.CallTo(() => _transformer.DoMultiTransformPublic(source, destination, context))
            .MustNotHaveHappened();
        }
        public void IfApply_AddConditionalTransformer_WithSpecifiedConditionAndTransformation_AndNonIsolatedResult()
        {
            var transformation = A.Fake <MyAbstractTransformation>();
            var source         = new Foo();
            var destination    = new Bar();
            var context        = new SomeTransformationContext();
            var newDestination = new Bar();

            A.CallTo(() => transformation.Transform(source, destination, context))
            .Returns(newDestination);

            var builderInstance = _builder.IfApply((s, d, c) => true, transformation);

            Assert.AreEqual(_builder, builderInstance);
            Assert.AreEqual(1, _builder.Transformations.Count);
            var conditionalTransformer = _builder.Transformations[0] as ConditionalTransformer <Foo, Bar>;

            Assert.IsNotNull(conditionalTransformer);
            Assert.IsFalse(conditionalTransformer.IsIsolatedResult);
            var transformationResult = conditionalTransformer.Transform(source, destination, context);

            Assert.AreEqual(newDestination, transformationResult);
        }