public void Test_EmbeddedValue() { var builder = new ValidationBuilder(typeof(FooC)); var ruleSet = builder.LowLevelRules; Assert.AreEqual(1, ruleSet.Rules.Count); object rule = ruleSet.Rules[0]; var property = CollectionUtils.FirstElement((rule as IPropertyBoundRule).Properties); Assert.AreEqual(typeof(FooC).GetProperty("EmbeddedFoo"), property); var foo = new FooC { EmbeddedFoo = null }; // this should pass, because rules on the embedded value class are not evaluated when the property is null Assert.IsTrue(ruleSet.Test(foo).Success); // should pass foo.EmbeddedFoo = new FooA { Name = "Bob" }; Assert.IsTrue(ruleSet.Test(foo).Success); // should fail because Robert is longer than 5 chars foo.EmbeddedFoo.Name = "Robert"; Assert.IsFalse(ruleSet.Test(foo).Success); }
public void ShouldUseSameDependencyDuringMappingOperationOfComposedChildren() { var fooC = new FooC() { Foos = new Foo[] { new Foo(), new Foo(), new Foo() } }; var resolver = new ResolverMock(); resolver.Add(() => new Dependency1()); var mapper = CreateMapper(resolver, builder => { builder.CreateMap <Foo, Bar>() .WithDependencies <Dependency1>() .MapProperty((x, d) => d.Item1.RandomProperty, x => x.Property3); builder.CreateMap <FooC, BarC>() .MapCollection(x => x.Foos, x => x.BarArray); }); var barC = mapper.Map <BarC>(fooC); var randomNumber = barC.BarArray.Select(x => x.Property3).Distinct().ToArray(); Assert.AreEqual(1, randomNumber.Length, "Expected all Bars to have the same Property3 because they should have used the same dependency"); Assert.AreNotEqual(0, randomNumber[0], "Should be a random int - not equal to zero"); }
private void AssertCollectionMap(Expression <Func <BarC, IEnumerable <Bar> > > collectionExpression) { var fooC = new FooC() { Property1 = 18, Foos = new Foo[] { new Foo() { Property1 = 1 }, new Foo() { Property1 = 2 }, new FooX() { Property1 = 3, Property2 = 33 } } }; var mapper = CreateMapper(new ResolverMock(), builder => { builder.CreateMap <Foo, Bar>() .MapProperty(x => x.Property1, x => x.Property3); builder.CreateMap <FooX, BarX>() .MapProperty(x => x.Property2, x => x.Property4); builder.CreateMap <FooC, BarC>() .MapProperty(x => x.Property1, x => x.Property1) .MapCollection(x => x.Foos, collectionExpression); }); var barC = mapper.Map <BarC>(fooC); var func = collectionExpression.Compile(); var enumerable = func(barC); Assert.AreEqual(18, barC.Property1); Assert.AreEqual(3, enumerable.Count()); Assert.AreEqual(1, enumerable.ElementAt(0).Property3); Assert.AreEqual(2, enumerable.ElementAt(1).Property3); Assert.AreEqual(3, enumerable.ElementAt(2).Property3); Assert.IsInstanceOf <BarX>(enumerable.ElementAt(2)); Assert.AreEqual(33, ((BarX)enumerable.ElementAt(2)).Property4); }
public void Test_EmbeddedValue() { var builder = new ValidationBuilder(typeof(FooC)); var ruleSet = builder.LowLevelRules; Assert.AreEqual(1, ruleSet.Rules.Count); object rule = ruleSet.Rules[0]; var property = CollectionUtils.FirstElement((rule as IPropertyBoundRule).Properties); Assert.AreEqual(typeof(FooC).GetProperty("EmbeddedFoo"), property); var foo = new FooC {EmbeddedFoo = null}; // this should pass, because rules on the embedded value class are not evaluated when the property is null Assert.IsTrue(ruleSet.Test(foo).Success); // should pass foo.EmbeddedFoo = new FooA {Name = "Bob"}; Assert.IsTrue(ruleSet.Test(foo).Success); // should fail because Robert is longer than 5 chars foo.EmbeddedFoo.Name = "Robert"; Assert.IsFalse(ruleSet.Test(foo).Success); }