Represents a column, and contains data about where that column is located in a row and what type of data it contains. This class contains some error checking, but it can be used in some very weird ways (for example, it doesn't care if columns overlap with each other). In general, it's your responsibility to use it sensibly.
예제 #1
0
 public FixedWidthField(FixedWidthField old)
 {
     Start        = old.Start;
     End          = old.End;
     Strict       = old.Strict;
     DefaultValue = old.DefaultValue;
     Type         = old.Type;
     Seeker       = old.Seeker;
     Terminator   = old.Terminator;
 }
예제 #2
0
 public FixedWidthField(FixedWidthField old)
 {
     Start = old.Start;
     End = old.End;
     Strict = old.Strict;
     DefaultValue = old.DefaultValue;
     Type = old.Type;
     Seeker = old.Seeker;
     Terminator = old.Terminator;
 }
        public void FixedWidthInputTest()
        {
            #region Setup

            FixedWidthColumnReader reader = new FixedWidthColumnReader();
            string filename = Path.Combine(InputDirectory, InputFile);
            reader.LoadFile(filename);

            var expectedStrings = new List<string[]>();
            var expectedObjects = new List<object[]>();
            var fields = new List<List<FixedWidthField>>();
            List<FixedWidthField> row;
            FixedWidthField temp, template;

            /***********************************************************************************
             * First row tests Seeker = FROM_START, Terminator = LENGTH, types int and float
             * Data = "this is some text25      13373.133712/20/18342:32 PM12 November 1978 15:38:24"
             ***********************************************************************************/
            expectedStrings.Add(new string[] {
                "this is some text25      ",
                "1337",
                "3.1337",
                "12/20/1834",
                "2:32 PM",
                "12 November 1978 15:38:24"
                });

            expectedObjects.Add(new object[] {
                "this is some text25      ",
                (int)1337,
                (float)3.1337,
                DateTime.Parse("12/20/1834"),
                DateTime.Parse("2:32 PM"),
                DateTime.Parse("12 November 1978 15:38:24")
                });

            row = new List<FixedWidthField>();
            template = temp = new FixedWidthField(0, 25, FixedWidthTypes.STRING, FixedWidthPositions.FROM_START, FixedWidthTerminators.LENGTH);
            row.Add(temp);

            temp = new FixedWidthField(template);
            temp.Start = 25;
            temp.End = 4;
            temp.Type = FixedWidthTypes.INT;
            row.Add(temp);

            temp = new FixedWidthField(template);
            temp.Start = 29;
            temp.End = 6;
            temp.Type = FixedWidthTypes.FLOAT;
            row.Add(temp);

            temp = new FixedWidthField(template);
            temp.Start = 35;
            temp.End = 10;
            temp.Type = FixedWidthTypes.DATETIME;
            row.Add(temp);

            temp = new FixedWidthField(template);
            temp.Start = 45;
            temp.End = 7;
            temp.Type = FixedWidthTypes.DATETIME;
            row.Add(temp);

            temp = new FixedWidthField(template);
            temp.Start = 52;
            temp.End = 25;
            temp.Type = FixedWidthTypes.DATETIME;
            row.Add(temp);

            fields.Add(row);
            row = new List<FixedWidthField>();

            /****************************************************************************************/

            /***********************************************************************************
             * Second row tests Seeker = FROM_CURRENT, otherwise same as first
             * Data = "this is some text25      13373.133712/20/18342:32 PM12 November 1978 15:38:24"
             **********************************************************************************/
            expectedStrings.Add(expectedStrings[0]);
            expectedObjects.Add(expectedObjects[0]);

            foreach (FixedWidthField f in fields[0])
            {
                temp = new FixedWidthField(f);
                temp.Seeker = FixedWidthPositions.FROM_CURRENT;
                temp.Start = 0;
                row.Add(temp);
            }

            fields.Add(row);
            row = new List<FixedWidthField>();

            /****************************************************************************************/

            /***********************************************************************************
             * Third row tests Seeker = FROM_START, Terminator = INDEX, types long and double
             * Data = "this is some text25      13373.133712/20/18342:32 PM12 November 1978 15:38:24"
             **********************************************************************************/
            expectedStrings.Add(expectedStrings[0]);

            expectedObjects.Add(new object[] {
                "this is some text25      ",
                (long)1337,
                (double)3.1337,
                DateTime.Parse("12/20/1834"),
                DateTime.Parse("2:32 PM"),
                DateTime.Parse("12 November 1978 15:38:24")
                });

            foreach (FixedWidthField f in fields[0])
            {
                temp = new FixedWidthField(f);
                temp.Terminator = FixedWidthTerminators.INDEX;

               //Used to be start index and length, converting to start index and end index
                temp.End = temp.Start + temp.End;
                if (temp.Type == FixedWidthTypes.FLOAT) temp.Type = FixedWidthTypes.DOUBLE;
                if (temp.Type == FixedWidthTypes.INT) temp.Type = FixedWidthTypes.LONG;
                row.Add(temp);
            }

            fields.Add(row);
            row = new List<FixedWidthField>();

            /****************************************************************************************/

            /***********************************************************************************
             * Fourth row tests Seeker = FROM_END, Terminator = NEWLINE, and overlapping records
             * Data = "0123456789"
             **********************************************************************************/

            expectedStrings.Add(new string[] { "34", "0123456789" });
            expectedObjects.Add(expectedStrings[expectedStrings.Count - 1] as object[]);

            row.Add(new FixedWidthField(7, 2, FixedWidthTypes.STRING, FixedWidthPositions.FROM_END, FixedWidthTerminators.LENGTH));
            row.Add(new FixedWidthField(0, 43847283, FixedWidthTypes.STRING, FixedWidthPositions.FROM_START, FixedWidthTerminators.NEWLINE));

            fields.Add(row);
            row = null;

            /****************************************************************************************/

            #endregion Setup

            Assert.AreEqual(expectedObjects.Count, expectedStrings.Count);
            Assert.AreEqual(expectedObjects.Count, fields.Count);

            //eString and eObject refer to the same object!
            var eObject = (reader as IEnumerable).GetEnumerator();
            var eString = eObject as IEnumerator<List<string>>;

            int count = expectedObjects.Count;

            for (int i = 0; i < count; i++)
            {
                if (!eString.MoveNext())
                {
                    Assert.Fail("Ran out of lines in input file {0}", InputFile);
                }
                reader.Columns = fields[i];
                List<string> myRowStrings = eString.Current;
                List<object> myRowObjects = eObject.Current as List<object>;

                //Make sure same number of elements for current row in myRowStrings, myRowObjects,
                //expectedObjects[i], expectedStrings[i], and fields[i]
                Assert.AreEqual(myRowStrings.Count, myRowObjects.Count);
                Assert.AreEqual(myRowObjects.Count, expectedObjects[i].Count());
                Assert.AreEqual(expectedObjects[i].Count(), expectedStrings[i].Count());
                Assert.AreEqual(expectedStrings[i].Count(), fields[i].Count());

                int fieldCount = myRowStrings.Count;
                for (int j = 0; j < fieldCount; j++)
                {
                    Assert.AreEqual(expectedObjects[i][j], myRowObjects[j]);
                    Assert.AreEqual(expectedStrings[i][j], myRowStrings[j]);
                }
            }
        }