コード例 #1
0
        public void TestInverseMappingDoesNothing()
        {
            var foo = new Foo {
                Id = 10
            };

            Expression <Func <Foo, int> > source = _ => _.Id * _.Id;

            var sut = new ExpressionToPropertyAssociation <Foo, Bar, int>(source, _ => _.Id);

            var bar = CreateReader(sut)(foo);

            bar.Should().NotBeNull();
            foo.Id.Should().Be(10);
        }
コード例 #2
0
        public void TestEmptyMapsToEmpty()
        {
            var sut = CreateSut();

            var foo = new Foo
            {
                Bazes = Enumerable.Empty <Baz>()
            };

            var bar = CreateReader(sut)(foo);

            bar.Should().NotBeNull();

            bar.Names.Should().NotBeNull();
            bar.Names.Should().BeEmpty();
        }
コード例 #3
0
        public void TestDirectMappingCreatesTargetComponent()
        {
            var foo = new Foo {
                Component = new Baz {
                    Id = 15
                }
            };

            var sut = new ComponentToComponentAssociation <Foo, Baz, Bar, Qux>(_ => _.Component, _ => _.Component);

            var bar = CreateReader(sut)(foo);

            bar.Should().NotBeNull();
            bar.Component.Should().NotBeNull();
            bar.Component.Id.Should().Be(0);
        }
コード例 #4
0
        public void TestStringMapping()
        {
            var foo = new Foo {
                Name = "123"
            };

            Expression <Func <Foo, int> > source = _ => _.Name.Length;

            var sut = new ExpressionToPropertyAssociation <Foo, Bar, int>(source, _ => _.Id);

            var expected = source.Compile()(foo);

            var bar = CreateReader(sut)(foo);

            bar.Should().NotBeNull();
            bar.Id.Should().Be(expected);
        }
コード例 #5
0
        public void TestDirectMappingAppliesMapper()
        {
            var foo = new Foo {
                Component = new Baz {
                    Id = 15
                }
            };

            var sut       = new ComponentToComponentAssociation <Foo, Baz, Bar, Qux>(_ => _.Component, _ => _.Component);
            var component = new PropertyToPropertyAssociation <Baz, Qux, int>(_ => _.Id, _ => _.Id);

            var bar = CreateReader(sut, component)(foo);

            bar.Should().NotBeNull();
            bar.Component.Should().NotBeNull();
            bar.Component.Id.Should().Be(foo.Component.Id);
        }
コード例 #6
0
        public void TestMappingAssignsDefaultValue()
        {
            var sut       = new ComponentToComponentAssociation <Foo, Baz, Bar, Qux>(_ => _.Component, _ => _.Component);
            var component = new PropertyToPropertyAssociation <Baz, Qux, int>(_ => _.Id, _ => _.Id);

            var foo = new Foo();
            var bar = CreateReader(sut, component)(foo);

            bar.Should().NotBeNull();
            bar.Component.Should().BeNull();

            foo = new Foo {
                Component = new Baz()
            };
            bar = new Bar();
            CreateWriter(sut, component)(bar, foo);
            foo.Component.Should().BeNull();
        }
コード例 #7
0
        public void TestMappingAppliesMapper()
        {
            var sut       = new ComponentCollectionAssociation <Foo, Baz, Bar, Qux>(_ => _.Bazes, _ => _.Quxes);
            var component = new PropertyToPropertyAssociation <Baz, Qux, int>(_ => _.Id, _ => _.Id);

            var foo = new Foo
            {
                Bazes = new[] { new Baz {
                                    Id = 1
                                }, new Baz {
                                    Id = 2
                                } }
            };

            var bar = CreateReader(sut, component)(foo);

            bar.Quxes.Should().NotBeNull();
            bar.Quxes.Should().NotBeEmpty();
            bar.Quxes.Select(_ => _.Id).Should().BeEquivalentTo(foo.Bazes.Select(_ => _.Id));
        }
コード例 #8
0
        public void TestMappingAppliesMapper()
        {
            var sut = CreateSut();

            var foo = new Foo
            {
                Bazes = new[] { new Baz {
                                    Id = 1
                                }, new Baz {
                                    Id = 2
                                } }
            };

            var bar = CreateReader(sut)(foo);

            bar.Should().NotBeNull();
            bar.Names.Should().NotBeNull();
            bar.Names.Should().NotBeEmpty();
            bar.Names.Should().BeEquivalentTo(foo.Bazes.Select(_ => _.Id.ToString()));
        }
コード例 #9
0
        public void TestIntToStringMapping()
        {
            Foo foo;
            Bar bar;

            var sut = new PropertyToPropertyWithConversionAssociation <Foo, int, Bar, string>(_ => _.Id, _ => _.ToString(), _ => _.Name, _ => int.Parse(_));

            foo = new Foo {
                Id = 123
            };
            bar = CreateReader(sut)(foo);
            bar.Should().NotBeNull();
            bar.Name.Should().Be(foo.Id.ToString());

            foo = new Foo {
                Id = 123
            };
            bar = new Bar {
                Name = "555"
            };
            CreateWriter(sut)(bar, foo);
            foo.Id.Should().Be(int.Parse(bar.Name));
        }
コード例 #10
0
        public void TestStringMapping()
        {
            Foo foo;
            Bar bar;

            var sut = new PropertyToPropertyAssociation <Foo, Bar, string>(_ => _.Name, _ => _.Name);

            foo = new Foo {
                Name = "aaa"
            };
            bar = CreateReader(sut)(foo);
            bar.Should().NotBeNull();
            bar.Id.Should().Be(foo.Id);

            foo = new Foo {
                Name = "aaa"
            };
            bar = new Bar {
                Name = "bbb"
            };
            CreateWriter(sut)(bar, foo);
            foo.Id.Should().Be(bar.Id);
        }
コード例 #11
0
        public void TestIntMapping()
        {
            Foo foo;
            Bar bar;

            var sut = new PropertyToPropertyAssociation <Foo, Bar, int>(_ => _.Id, _ => _.Id);

            foo = new Foo {
                Id = 5
            };
            bar = CreateReader(sut)(foo);
            bar.Should().NotBeNull();
            bar.Id.Should().Be(foo.Id);

            foo = new Foo {
                Id = 5
            };
            bar = new Bar {
                Id = 6
            };
            CreateWriter(sut)(bar, foo);
            foo.Id.Should().Be(bar.Id);
        }