public void OverflowTests() { FixedWidthField[] fields = new FixedWidthField[] { new FixedWidthField(2), new FixedWidthField(2), }; List <string[]> values = new() { new string[] { "abc", "def" }, new string[] { "ghi", "jkl" }, }; List <string[]> expected = new() { new string[] { "ab", "de" }, new string[] { "gh", "jk" }, }; Assert.Throws <FixedWidthOverflowException>(() => { WriteReadValues(fields, values, out List <string[]> results, new FixedWidthOptions { ThrowOverflowException = true }); }); WriteReadValues(fields, values, out List <string[]> results, new FixedWidthOptions { ThrowOverflowException = false }); CollectionAssert.AreEqual(expected, results); }
public void FieldMismatchTest() { FixedWidthField[] fields = new FixedWidthField[] { new FixedWidthField(10), new FixedWidthField(10), new FixedWidthField(10), new FixedWidthField(10), }; List <string[]> values = new() { new string[] { "abc" }, new string[] { "abc", "def" }, new string[] { "abc", "def", "ghi" }, new string[] { "abc", "def", "ghi", "jkl" }, new string[] { "abc", "def", "ghi", "jkl", "mno" }, new string[] { "abc", "def", "ghi", "jkl", "mno", "pqr" }, }; List <string[]> expected = new() { new string[] { "abc", string.Empty, string.Empty, string.Empty }, new string[] { "abc", "def", string.Empty, string.Empty }, new string[] { "abc", "def", "ghi", string.Empty }, new string[] { "abc", "def", "ghi", "jkl" }, new string[] { "abc", "def", "ghi", "jkl" }, new string[] { "abc", "def", "ghi", "jkl" }, }; WriteReadValues(fields, values, out List <string[]> results); CollectionAssert.AreEqual(expected, results); }
public void SkipTests() { FixedWidthField[] fields = new FixedWidthField[] { new FixedWidthField(5), new FixedWidthField(5) { Skip = 4 }, new FixedWidthField(5), }; FixedWidthField[] fields2 = new FixedWidthField[] { new FixedWidthField(5), new FixedWidthField(4), new FixedWidthField(5), new FixedWidthField(5), }; List <string[]> values = new() { new string[] { "abc", "def", "ghi" }, new string[] { "def", "ghi", "jkl" }, new string[] { "ghi", "jkl", "mno" }, }; List <string[]> expected = new() { new string[] { "abc ", "def ", "ghi " }, new string[] { "def ", "ghi ", "jkl " }, new string[] { "ghi ", "jkl ", "mno " }, }; List <string[]> expected2 = new() { new string[] { "abc ", " ", "def ", "ghi " }, new string[] { "def ", " ", "ghi ", "jkl " }, new string[] { "ghi ", " ", "jkl ", "mno " }, }; WriteReadValues(fields, values, out List <string[]> results); CollectionAssert.AreEqual(values, results); WriteReadValues(fields, values, out results, new FixedWidthOptions { TrimFields = false }); CollectionAssert.AreEqual(expected, results); WriteReadValues(fields, fields2, values, out results, new FixedWidthOptions { TrimFields = false }); CollectionAssert.AreEqual(expected2, results); }
public void OutOfRangeTests() { FixedWidthField[] writeFields = new FixedWidthField[] { new FixedWidthField(4), new FixedWidthField(4), }; FixedWidthField[] readFields = new FixedWidthField[] { new FixedWidthField(4), new FixedWidthField(4), new FixedWidthField(4), }; List <string[]> values = new() { new string[] { "abc", "def" }, new string[] { "ghi", "jkl" }, }; List <string[]> expected = new() { new string[] { "abc", "def", string.Empty }, new string[] { "ghi", "jkl", string.Empty }, }; Assert.Throws <FixedWidthOutOfRangeException>(() => { WriteReadValues(writeFields, readFields, values, out List <string[]> results, new FixedWidthOptions { ThrowOutOfRangeException = true }); }); WriteReadValues(writeFields, readFields, values, out List <string[]> results, new FixedWidthOptions { ThrowOutOfRangeException = false }); CollectionAssert.AreEqual(expected, results); }
public void FieldOverrideTests() { FixedWidthField[] fields = new FixedWidthField[] { new FixedWidthField(10, FieldAlignment.Left, ' ', false), new FixedWidthField(10, FieldAlignment.Right, '~', false), new FixedWidthField(10, alignment: FieldAlignment.Left, padCharacter: '@', trimField: false), new FixedWidthField(10, alignment: FieldAlignment.Right, padCharacter: '!', trimField: false), new FixedWidthField(10) { Alignment = FieldAlignment.Left, PadCharacter = '=', TrimField = true, }, new FixedWidthField(10) { Alignment = FieldAlignment.Right, PadCharacter = '=', TrimField = true, }, }; List <string[]> values = new() { new string[] { "abc", "def", "ghi", "jkl", "mno", "pqr" }, new string[] { "def", "ghi", "jkl", "mno", "pqr", "stu" }, new string[] { "ghi", "jkl", "mno", "pqr", "stu", "vwx" }, }; List <string[]> expected = new() { new string[] { "abc ", "~~~~~~~def", "ghi@@@@@@@", "!!!!!!!jkl", "mno", "pqr" }, new string[] { "def ", "~~~~~~~ghi", "jkl@@@@@@@", "!!!!!!!mno", "pqr", "stu" }, new string[] { "ghi ", "~~~~~~~jkl", "mno@@@@@@@", "!!!!!!!pqr", "stu", "vwx" }, }; WriteReadValues(fields, values, out List <string[]> results); CollectionAssert.AreEqual(expected, results); }
public void BasicTest() { FixedWidthField[] fields = new FixedWidthField[] { new FixedWidthField(26), new FixedWidthField(26), new FixedWidthField(26), new FixedWidthField(26), }; List <string[]> values = new() { new string[] { "abcdefghijklmnopqrstuvwxyz", "1234567890", "1010101010", "" }, new string[] { "1234567890", "1010101010", "", "abcdefghijklmnopqrstuvwxyz" }, new string[] { "1010101010", "", "abcdefghijklmnopqrstuvwxyz", "1234567890" }, new string[] { "", "abcdefghijklmnopqrstuvwxyz", "1234567890", "1010101010" }, }; List <string[]> leftPadded = new() { new string[] { "abcdefghijklmnopqrstuvwxyz", "1234567890 ", "1010101010 ", " " }, new string[] { "1234567890 ", "1010101010 ", " ", "abcdefghijklmnopqrstuvwxyz" }, new string[] { "1010101010 ", " ", "abcdefghijklmnopqrstuvwxyz", "1234567890 " }, new string[] { " ", "abcdefghijklmnopqrstuvwxyz", "1234567890 ", "1010101010 " }, }; List <string[]> rightPadded = new() { new string[] { "abcdefghijklmnopqrstuvwxyz", " 1234567890", " 1010101010", " " }, new string[] { " 1234567890", " 1010101010", " ", "abcdefghijklmnopqrstuvwxyz" }, new string[] { " 1010101010", " ", "abcdefghijklmnopqrstuvwxyz", " 1234567890" }, new string[] { " ", "abcdefghijklmnopqrstuvwxyz", " 1234567890", " 1010101010" }, }; List <string[]> tildePadded = new() { new string[] { "abcdefghijklmnopqrstuvwxyz", "1234567890~~~~~~~~~~~~~~~~", "1010101010~~~~~~~~~~~~~~~~", "~~~~~~~~~~~~~~~~~~~~~~~~~~" }, new string[] { "1234567890~~~~~~~~~~~~~~~~", "1010101010~~~~~~~~~~~~~~~~", "~~~~~~~~~~~~~~~~~~~~~~~~~~", "abcdefghijklmnopqrstuvwxyz" }, new string[] { "1010101010~~~~~~~~~~~~~~~~", "~~~~~~~~~~~~~~~~~~~~~~~~~~", "abcdefghijklmnopqrstuvwxyz", "1234567890~~~~~~~~~~~~~~~~" }, new string[] { "~~~~~~~~~~~~~~~~~~~~~~~~~~", "abcdefghijklmnopqrstuvwxyz", "1234567890~~~~~~~~~~~~~~~~", "1010101010~~~~~~~~~~~~~~~~" }, }; WriteReadValues(fields, values, out List <string[]> results, new FixedWidthOptions { TrimFields = true }); CollectionAssert.AreEqual(values, results); WriteReadValues(fields, values, out results, new FixedWidthOptions { DefaultPadCharacter = '~', TrimFields = true }); CollectionAssert.AreEqual(values, results); WriteReadValues(fields, values, out results, new FixedWidthOptions { DefaultAlignment = FieldAlignment.Left, TrimFields = false }); CollectionAssert.AreEqual(leftPadded, results); WriteReadValues(fields, values, out results, new FixedWidthOptions { DefaultAlignment = FieldAlignment.Right, TrimFields = false }); CollectionAssert.AreEqual(rightPadded, results); WriteReadValues(fields, values, out results, new FixedWidthOptions { DefaultPadCharacter = '~', TrimFields = false }); CollectionAssert.AreEqual(tildePadded, results); }
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]); } } }