public void Map()
        {
            var sut   = new FixedWidthMapper();
            var lines = new List <string> {
                "a1206", "b0601"
            };
            var columnConfigs = new List <ColumnLayout>
            {
                new ColumnLayout {
                    FieldName = "name", ColumnPosition = 0, FieldLength = 1
                },
                new ColumnLayout {
                    FieldName = "age", ColumnPosition = 1, FieldLength = 2
                },
                new ColumnLayout {
                    FieldName = "grade", ColumnPosition = 3, FieldLength = 2
                },
            };

            var config = new Configuration {
                ColumnLayouts = columnConfigs
            };

            DataTable actualTable = sut.Map(lines, config);

            Assert.Equal(2, actualTable.Rows.Count);
            Assert.Equal("a", actualTable.Rows[0]["name"]);
            Assert.Equal("12", actualTable.Rows[0]["age"]);
            Assert.Equal("06", actualTable.Rows[0]["grade"]);
            Assert.Equal("b", actualTable.Rows[1]["name"]);
            Assert.Equal("06", actualTable.Rows[1]["age"]);
            Assert.Equal("01", actualTable.Rows[1]["grade"]);
        }
Exemplo n.º 2
0
        public void ThrowExceptionWhenConfigInvalid()
        {
            var table = new DataTable();

            DataColumn[] cols =
            {
                new DataColumn("name",  typeof(string)),
                new DataColumn("day",   typeof(string)),
                new DataColumn("month", typeof(string)),
            };
            table.Columns.AddRange(cols);
            object[] rows =
            {
                new object[] { "trang ", "29", "09" },
                new object[] { "phuong", "10", "10" },
            };
            foreach (object[] row in rows)
            {
                table.Rows.Add(row);
            }

            var columnConfigs = new List <ColumnLayout>
            {
                new ColumnLayout {
                    FieldName = "day", ColumnPosition = 6, FieldLength = 2
                },
                new ColumnLayout {
                    FieldName = "month", ColumnPosition = 8, FieldLength = 2
                },
                new ColumnLayout {
                    FieldName = "year", ColumnPosition = 10, FieldLength = 2
                },
                new ColumnLayout {
                    FieldName = "name", ColumnPosition = 0, FieldLength = 6
                },
            };
            var config = new Configuration {
                ColumnLayouts = columnConfigs
            };

            var sut = new FixedWidthMapper();

            Assert.Throws <Exception>(() => sut.Map(table, config));
        }
Exemplo n.º 3
0
        public void MapWhenConfigValidAndInOrder()
        {
            var table = new DataTable();

            DataColumn[] cols =
            {
                new DataColumn("name",  typeof(string)),
                new DataColumn("day",   typeof(string)),
                new DataColumn("month", typeof(string)),
            };
            table.Columns.AddRange(cols);
            object[] rows =
            {
                new object[] { "trang ", "29", "09" },
                new object[] { "phuong", "10", "10" },
            };
            foreach (object[] row in rows)
            {
                table.Rows.Add(row);
            }

            var columnConfigs = new List <ColumnLayout>
            {
                new ColumnLayout {
                    FieldName = "name", ColumnPosition = 0, FieldLength = 6
                },
                new ColumnLayout {
                    FieldName = "day", ColumnPosition = 6, FieldLength = 2
                },
                new ColumnLayout {
                    FieldName = "month", ColumnPosition = 8, FieldLength = 2
                },
            };
            var config = new Configuration {
                ColumnLayouts = columnConfigs
            };

            var sut = new FixedWidthMapper();

            List <string> lines = sut.Map(table, config);

            Assert.Equal("trang 2909", lines[0]);
            Assert.Equal("phuong1010", lines[1]);
        }