Ejemplo n.º 1
0
            public void Must_assign_all_settable_properties_picked_up_by_convention()
            {
                var sourceFoo = new Foo();
                var targetBar = new Bar();

                sourceFoo.A             = "SourceA";
                sourceFoo.B             = "SourceB";
                sourceFoo.C             = 1;
                sourceFoo.Color         = Color.Red;
                sourceFoo.NullableColor = null;
                sourceFoo.RefType       = new SimpleRefType
                {
                    Id = Guid.NewGuid()
                };

                targetBar.A             = "WrongA";
                targetBar.B             = "WrongB";
                targetBar.C             = 0;
                targetBar.Color         = ColorType.Blue;
                targetBar.NullableColor = ColorType.Green;
                targetBar.RefType       = null;

                var systemUnderTest = new DefaultMapper <Foo, Bar>();

                systemUnderTest.Map(sourceFoo, targetBar);

                Assert.That(targetBar.A, Is.EqualTo(sourceFoo.A));
                Assert.That(targetBar.B, Is.EqualTo(sourceFoo.B));
                Assert.That(targetBar.C, Is.EqualTo(sourceFoo.C));
                Assert.That(targetBar.Color, Is.EqualTo(ColorType.Red));
                Assert.That(targetBar.NullableColor, Is.EqualTo(null));
                Assert.That(targetBar.RefType, Is.Not.Null);
                Assert.That(targetBar.RefType.Id, Is.Not.Null);
                Assert.That(targetBar.RefType.Id, Is.EqualTo(sourceFoo.RefType.Id));
            }
Ejemplo n.º 2
0
        public void Map_Nested_List()
        {
	        var item = new
	        {
		        value = new
		        {
			        Items = new []
			        {
						new { Value = "one"},
						new { Value = "two"}
					}
		        }
	        };

	        var mapper = new DefaultMapper();
	        var snapshot = mapper.Map(item, SnapshotOptions.Default);

	        var sb = string.Join(Environment.NewLine, new[]
	        {
		        "value:",
		        "  Items:",
		        "    Value: one",
		        "    Value: two"
	        });

	        snapshot.ToString().Should().BeEquivalentTo(sb);
        }
Ejemplo n.º 3
0
        public void Map_ValueType_Deep()
        {
	        var item = new
	        {
		        value = new
		        {
			        Id = 1,
			        Name = "one",
			        Dbl = 2.2,
			        KeyValue = new KeyValuePair<string, string>("key", "value")
		        }
	        };

	        var mapper = new DefaultMapper();
	        var snapshot = mapper.Map(item, SnapshotOptions.Default);

	        var sb = string.Join(Environment.NewLine, new[]
	        {
				"value:",
		        "  Dbl: 2.2",
		        "  Id: 1",
		        "  KeyValue:",
				"    Key: key",
				"    Value: value",
				"  Name: one"
			});

	        snapshot.ToString().Should().BeEquivalentTo(sb);
        }
Ejemplo n.º 4
0
            public void Must_perform_acceptably()
            {
                var sourceFoo = new Foo();
                var targetBar = new Bar();

                sourceFoo.A       = "SourceA";
                sourceFoo.B       = "SourceB";
                sourceFoo.C       = 1;
                sourceFoo.RefType = new SimpleRefType
                {
                    Id = Guid.NewGuid()
                };

                targetBar.A       = "WrongA";
                targetBar.B       = "WrongB";
                targetBar.C       = 0;
                targetBar.RefType = null;

                const int numberOfMappings = 10000000;
                var       systemUnderTest  = new DefaultMapper <Foo, Bar>();
                var       milliseconds     = (long)StopwatchContext.Timed(() =>
                {
                    for (int i = 0; i < numberOfMappings; i++)
                    {
                        systemUnderTest.Map(sourceFoo, targetBar);
                    }
                }).TotalMilliseconds;

                Console.WriteLine("{0:0.000000000000000}", milliseconds / numberOfMappings);
            }
Ejemplo n.º 5
0
        public void MapMethodThrowsIfOptionsAreNull()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                var mapper = new DefaultMapper();

                mapper.Map(Enumerable.Empty <string>(), null);
            });
        }
Ejemplo n.º 6
0
        public void MapMethodThrowsIfArgumentsAreNull()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                var mapper = new DefaultMapper();

                mapper.Map(null);
            });
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Maps the passed arguments to the specified type
        /// </summary>
        /// <param name="arguments">
        ///    The arguments mapped to the specified type
        /// </param>
        /// <typeparam name="T">
        ///    The type to whom the arguments are mapped
        /// </typeparam>
        /// <returns>
        ///    An instance of the specified type with all
        ///     properties mapped to the passed arguments
        /// </returns>
        private static IMapperResult <T> MapArgumentsToType <T>(string arguments) where T : class, new()
        {
            var mapper = new DefaultMapper();

            mapper.RegistrationService.Register <T>();

            mapper.Map(arguments.SimpleSplitArguments());

            var result = mapper.GetMapperResult <T>();

            return(result);
        }
Ejemplo n.º 8
0
        public void DefaultMapper_Map_NoSetter()
        {
            var mapper = new DefaultMapper();

            var context = new MapperContext(mapper, new Snapshot(), new SnapshotOptions(), 0);

            var item = new OnlyGetter();

            mapper.Map(context, item);

            Assert.AreEqual(1, context.Snapshot.Count);
        }
Ejemplo n.º 9
0
        public void DefaultMapper_Map_IgnoreIndexers()
        {
            var mapper = new DefaultMapper();

            var context = new MapperContext(mapper, new Snapshot(), new SnapshotOptions(), 0);

            var item = new WithIndexer();

            mapper.Map(context, item);

            Assert.AreEqual(0, context.Snapshot.Count);
        }
Ejemplo n.º 10
0
        public void SingleObjectPropertyIsNotFound()
        {
            var expectedUnmappedArg = new Argument("-", "-foo", "200");

            var mapper = new DefaultMapper();

            mapper.RegistrationService.Register <IntegratedTypesOptions>();
            mapper.Map("-stringProperty C:\\some\\file\\path -foo 200".SimpleSplitArguments());

            var unmapped = mapper.UnmappedArguments.ToList()[0];

            Assert.AreEqual(expectedUnmappedArg, unmapped);
        }
Ejemplo n.º 11
0
        /// <summary>
        ///     Maps the passed arguments to the specified types
        /// </summary>
        /// <param name="arguments">
        ///    The arguments to map
        /// </param>
        /// <typeparam name="TFirst">
        ///    The first type to map
        /// </typeparam>
        /// <typeparam name="TSecond">
        ///    The second type to map
        /// </typeparam>
        /// <returns>
        ///    The mapper that contains the result of the operation
        /// </returns>
        private static ICommandLineMapper MapArgumentsToTwoTypes <TFirst, TSecond>(string arguments)
            where TFirst : class, new()
            where TSecond : class, new()
        {
            var mapper = new DefaultMapper();

            mapper.RegistrationService.Register <TFirst>();
            mapper.RegistrationService.Register <TSecond>();

            mapper.Map(arguments.SimpleSplitArguments());

            return(mapper);
        }
Ejemplo n.º 12
0
        public void Map_ComplexObject()
        {
            var item = new
            {
                value = 1,
                obj = new
                {
                    sub = new
                    {
                        value = "sum"
                    }
                },
                data = "this is a test",
                list = new[]
                {
                    new
                    {
                        value = "1"
                    },
                    new
                    {
                        value = "2"
                    },
                },
                strings = new List<string>
                {
                    "value 1",
                    "value 2"
                }
            };

            var mapper = new DefaultMapper();
            var snapshot = mapper.Map(item, SnapshotOptions.Default);

            var sb = string.Join(Environment.NewLine, new[]
            {
                "data: this is a test",
                "list:",
                "  value: 1",
                "  value: 2",
                "obj:",
                "  sub:",
                "    value: sum",
                "strings:",
                "  value 1",
                "  value 2",
                "value: 1"
            });

            snapshot.ToString().Should().BeEquivalentTo(sb);
        }
Ejemplo n.º 13
0
        public void Map_IEnumerable()
        {
	        var list = new List<MapItem>
	        {
		        new MapItem {Value = "one"},
		        new MapItem {Value = "two"},
		        new MapItem {Value = "three"}
	        };

	        var mapper = new DefaultMapper();
	        var snapshot = mapper.Map(list.Select(i => new { i.Value }), SnapshotOptions.Default);

	        var sb = string.Join(Environment.NewLine, new[]
	        {
		        "Value: one",
		        "Value: two",
		        "Value: three"
	        });

	        snapshot.ToString().Should().BeEquivalentTo(sb);
		}
Ejemplo n.º 14
0
        public void Map_ValueType_Top()
        {
	        var item = new
	        {
		        Id = 1,
		        Name = "one",
		        Dbl = 2.2
	        };

	        var mapper = new DefaultMapper();
	        var snapshot = mapper.Map(item, SnapshotOptions.Default);

	        var sb = string.Join(Environment.NewLine, new[]
	        {
		        "Dbl: 2.2",
		        "Id: 1",
		        "Name: one"
	        });

	        snapshot.ToString().Should().BeEquivalentTo(sb);
        }
        private IEnumerable <T> GetDatabaseObjects <T>(string sqlCommandText)
            where T : new()
        {
            IList <T> objs = new List <T>();

            using (SqlConnection connection = new SqlConnection(_dbContext.ConnectionString))
                using (SqlCommand command = new SqlCommand(sqlCommandText, connection))
                {
                    connection.Open();

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            objs.Add(DefaultMapper.Map <T>(reader));
                        }
                    }
                }

            return(objs);
        }
Ejemplo n.º 16
0
            public void Must_map_the_nested_type_too_by_straight_assignment_when_settable_and_types_are_the_same()
            {
                var person = new Person
                {
                    Name     = "Joe",
                    Employer = new Employer
                    {
                        Name    = "Microsoft",
                        Address = new Address
                        {
                            Street = "1 Microsoft Way",
                            City   = "Redmond"
                        }
                    }
                };

                person.Address = new PersonAddress(person)
                {
                    Street = "123 Main",
                    City   = "Dallas"
                };

                var systemUnderTest = new DefaultMapper <Person, Person>();

                systemUnderTest.Validate();

                var person2 = new Person();

                systemUnderTest.Map(person, person2);

                Assert.That(person2.Name, Is.EqualTo(person.Name));
                Assert.That(person2.Employer, Is.SameAs(person.Employer));
                Assert.That(person2.Employer.Name, Is.EqualTo(person.Employer.Name));
                Assert.That(person2.Employer.Address, Is.SameAs(person.Employer.Address));
                Assert.That(person2.Employer.Address.Street, Is.EqualTo(person.Employer.Address.Street));
                Assert.That(person2.Employer.Address.City, Is.EqualTo(person.Employer.Address.City));
                Assert.That(person2.Address, Is.SameAs(person.Address));
                Assert.That(person2.Address.Street, Is.EqualTo(person.Address.Street));
                Assert.That(person2.Address.City, Is.EqualTo(person.Address.City));
            }
Ejemplo n.º 17
0
        public void DefaultMapper_NoPipeConverters_Map_Success()
        {
            // Use Case : I want to first combine a date and time field.  Then pipe
            // that result into the UTC converter along with the original data point.
            // Because PipedData is set to false then the end result should be two entries.
            // 1. Combined Date and Time
            // 2. Combined Date and Time converted to UTC.

            var converters = CreateConverters();

            var collector = new MockCollector(new MockLogger());

            collector.Configure(new CollectorConfiguration()
            {
                Id      = "1",
                Version = 2.0
            });

            var mapper = new DefaultMapper(new MockLogger(), collector);

            var leftSideMap1 = new Dictionary <string, List <string> >()
            {
                { "Date", new List <string>()
                  {
                      "DateTime"
                  } }
            };
            var leftSideMap2 = new Dictionary <string, List <string> >()
            {
                { "DateTime", new List <string>()
                  {
                      "DateTimeUTC"
                  } }
            };

            // Create the converters we are targeting
            var targetConverters = new List <SourceTargetConverter>();

            targetConverters.Add(new SourceTargetConverter()
            {
                Id = ID_CONVERTER_1, LeftSideMap = leftSideMap1, CombineInputOutput = false
            });
            targetConverters.Add(new SourceTargetConverter()
            {
                Id = ID_CONVERTER_2, LeftSideMap = leftSideMap2, CombineInputOutput = true
            });
            // Now create the targeted mapping
            var targetMappings = new List <SourceTargetMapping>();

            targetMappings.Add(new SourceTargetMapping()
            {
                PrimaryKey       = "Date",
                TargetConverters = targetConverters
            });
            // Finally create the mapper config and configure the mapper
            var mapperConfig = new MapperConfiguration()
            {
                Id                   = "1234",
                TransformerId        = "6678",
                SourceTargetMappings = targetMappings
            };

            mapper.Configure(mapperConfig, converters);

            mapper.Id.Should().Be("1234");
            mapper.TransformerId.Should().Be("6678");

            var dataRow = new EntityCollection();

            dataRow.Entities.Add("Date", "1/17/2018");
            dataRow.Entities.Add("Time", "15:46:07.000");

            var data = new List <IEntityCollection>();

            data.Add(dataRow);

            var convertedData = mapper.Map(data);

            convertedData.Should().NotBeNull();
            convertedData.Count.Should().Be(1);

            var entities = convertedData[0] as IEntityCollection;

            entities.Entities.Count.Should().Be(2);
            entities.Entities.ContainsKey("DateTimeUTC").Should().BeTrue();

            var dateTime         = DateTime.Parse(entities.Entities["DateTimeUTC"].ToString());
            var expectedDateTime = DateTime.Parse("1/17/2018 15:46:07.000");

            dateTime.Month.Should().Be(expectedDateTime.Month);
            dateTime.Day.Should().Be(expectedDateTime.Day);
            dateTime.Year.Should().Be(expectedDateTime.Year);
            dateTime.Hour.Should().Be(expectedDateTime.Hour);
            dateTime.Minute.Should().Be(expectedDateTime.Minute);
        }