Esempio n. 1
0
        public void CanReadFromFile()
        {
            string textFile = Path.GetTempFileName();

            try
            {
                using (StreamWriter writer = new StreamWriter(textFile, false))
                {
                    writer.WriteLine("01732Juan Perez     [email protected]");
                    writer.WriteLine("02310Guillermo Diez [email protected]");
                }

                using (FixedWidthFieldParser parser = new FixedWidthFieldParser(textFile))
                {
                    parser.SetFieldWidths(5, 15, 0);
                    parser.TrimWhiteSpace = true;
                    Assert.AreEqual("01732:Juan Perez:[email protected]", parser.ReadFields().ToString(":"));
                    Assert.AreEqual("02310:Guillermo Diez:[email protected]", parser.ReadFields().ToString(":"));
                }
            }
            finally
            {
                File.Delete(textFile);
            }
        }
Esempio n. 2
0
        public void FailsIfLineIsShorterThanExpected()
        {
            string text = "01732Juan Perez";

            using (StringReader reader = new StringReader(text))
            {
                using (FixedWidthFieldParser parser = new FixedWidthFieldParser(reader))
                {
                    parser.SetFieldWidths(5, 15, 8);
                    parser.ReadFields();
                }
            }
        }
Esempio n. 3
0
        public void CanParseMultipleFields()
        {
            string text = "01732Juan Perez     11052002";

            using (StringReader reader = new StringReader(text))
            {
                using (FixedWidthFieldParser parser = new FixedWidthFieldParser(reader))
                {
                    parser.SetFieldWidths(5, 15, 8);
                    Assert.AreEqual(3, parser.GetFieldWidths().Length);
                    Assert.AreEqual("01732:Juan Perez     :11052002", parser.ReadFields().ToString(":"));
                }
            }
        }
Esempio n. 4
0
        public void CanParseSingleField()
        {
            string text = "single record";

            using (StringReader reader = new StringReader(text))
            {
                using (FixedWidthFieldParser parser = new FixedWidthFieldParser(reader))
                {
                    TextFields fields = parser.ReadFields();
                    Assert.AreEqual(1, fields.Count);
                    Assert.AreEqual(text, fields[0]);
                }
            }
        }
Esempio n. 5
0
 public void DefaultPropertiesTest()
 {
     using (StringReader reader = new StringReader(string.Empty))
     {
         using (FixedWidthFieldParser parser = new FixedWidthFieldParser(reader))
         {
             Assert.IsFalse(parser.IgnoreBlankLines);
             Assert.IsFalse(parser.TrimWhiteSpace);
             Assert.AreEqual(0, parser.LineNumber);
             Assert.AreEqual(1, parser.GetFieldWidths().Length);
             Assert.AreEqual(0, parser.GetFieldWidths()[0]);
         }
     }
 }
Esempio n. 6
0
 public void CanRemeberFieldWidths()
 {
     using (StringReader reader = new StringReader(""))
     {
         using (FixedWidthFieldParser parser = new FixedWidthFieldParser(reader))
         {
             parser.SetFieldWidths(5, 3, 8);
             int[] fieldWidths = parser.GetFieldWidths();
             Assert.AreEqual(3, fieldWidths.Length);
             Assert.AreEqual(5, fieldWidths[0]);
             Assert.AreEqual(3, fieldWidths[1]);
             Assert.AreEqual(8, fieldWidths[2]);
         }
     }
 }
Esempio n. 7
0
        public void ReaderRemainsOpen()
        {
            string text = "01732Juan Perez     11052002";

            StringReader reader = new StringReader(text);

            FixedWidthFieldParser parser = new FixedWidthFieldParser(reader);

            parser.ReadToEnd();
            parser.Close();

            // Not owned reader should remain open after closing parser.
            Assert.AreEqual(-1, reader.Peek());
            reader.Close();
        }
Esempio n. 8
0
        public void VariableLengthField()
        {
            string newLine = "\r\n";
            string text    = "01732Juan Perez     [email protected]" + newLine +
                             "02310Guillermo Diez [email protected]";

            using (StringReader reader = new StringReader(text))
            {
                using (FixedWidthFieldParser parser = new FixedWidthFieldParser(reader))
                {
                    parser.SetFieldWidths(5, 15, 0);
                    parser.TrimWhiteSpace = true;
                    Assert.AreEqual("01732:Juan Perez:[email protected]", parser.ReadFields().ToString(":"));
                    Assert.AreEqual("02310:Guillermo Diez:[email protected]", parser.ReadFields().ToString(":"));
                }
            }
        }
Esempio n. 9
0
        public void CanReadTypedFields()
        {
            string text = "01732Juan Perez     0100.0011/05/2002";

            using (StringReader reader = new StringReader(text))
            {
                using (FixedWidthFieldParser parser = new FixedWidthFieldParser(reader))
                {
                    parser.SetFieldWidths(5, 15, 7, 10);
                    parser.TrimWhiteSpace = true;

                    TextFields fields = parser.ReadFields();
                    Assert.AreEqual(1732, fields.GetInt32(0));
                    Assert.AreEqual("Juan Perez", fields.GetString(1));
                    Assert.AreEqual(100.0f, fields.GetSingle(2));
                    Assert.AreEqual(new DateTime(2002, 11, 05), fields.GetDateTime(3));
                }
            }
        }
Esempio n. 10
0
        public void AllCommentedLines()
        {
            string newLine = "\r\n";
            string text    = "# 01732Juan Perez     [email protected]" + newLine +
                             "# 02310Guillermo Diez [email protected]";

            using (StringReader reader = new StringReader(text))
            {
                using (FixedWidthFieldParser parser = new FixedWidthFieldParser(reader))
                {
                    parser.SetFieldWidths(5, 15, 0);
                    parser.TrimWhiteSpace = true;
                    parser.SetCommentTokens("#");

                    Assert.IsFalse(parser.EndOfFile);
                    string line = parser.ReadLine();
                    Assert.IsNull(line);
                    Assert.IsTrue(parser.EndOfFile);
                }
            }
        }