Exemplo n.º 1
0
        public void CanParseMultipleFields()
        {
            string newLine = "\r\n";
            string text    = "a;bb;ccc;dddd" + newLine + "111;22;3";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.SetDelimiters(';');
                    Assert.AreEqual("a:bb:ccc:dddd", parser.ReadFields().ToString(":"));
                    Assert.AreEqual("111:22:3", parser.ReadFields().ToString(":"));
                }
            }
        }
Exemplo n.º 2
0
        public void CanHandleUnixStyleNewLine()
        {
            string newLine = "\n";
            string text    = "a;bb;ccc;dddd" + newLine + "e;ff;ggg;hhhh";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.SetDelimiters(';');
                    Assert.AreEqual("a:bb:ccc:dddd", parser.ReadFields().ToString(":"));
                    Assert.AreEqual("e:ff:ggg:hhhh", parser.ReadFields().ToString(":"));
                }
            }
        }
Exemplo n.º 3
0
        public void CanParseSingleField()
        {
            string text = "record 1";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    Assert.AreEqual("record 1", parser.ReadFields().ToString(string.Empty));
                }
            }
        }
Exemplo n.º 4
0
        public void LastFieldIsEmpty()
        {
            string text = "a,bb,ccc,";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    Assert.AreEqual("a|bb|ccc|", parser.ReadFields().ToString("|"));
                }
            }
        }
Exemplo n.º 5
0
        public void CanParseQuotedFields()
        {
            string text = "a,\"bb\",ccc";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.HasFieldsEnclosedInQuotes = true;
                    Assert.AreEqual("a|bb|ccc", parser.ReadFields().ToString("|"));
                }
            }
        }
Exemplo n.º 6
0
        public void MultipleDelimiters()
        {
            string text = "a bb\tccc\tdddd";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.SetDelimiters(' ', '\t');
                    Assert.AreEqual("a:bb:ccc:dddd", parser.ReadFields().ToString(":"));
                }
            }
        }
Exemplo n.º 7
0
        public void FailsIfExpectedDelimiterNotFound()
        {
            string text = "a,\"bb\"ccc";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.HasFieldsEnclosedInQuotes = true;
                    parser.ReadFields();
                }
            }
        }
Exemplo n.º 8
0
        public void QuotedFieldIsEmpty()
        {
            string text = "a,bb,ccc,\"\",eeee";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.HasFieldsEnclosedInQuotes = true;
                    Assert.AreEqual("a|bb|ccc||eeee", parser.ReadFields().ToString("|"));
                }
            }
        }
Exemplo n.º 9
0
        public void FailsIfUnmatchedQuotes()
        {
            string text = "\"aaa";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.HasFieldsEnclosedInQuotes = true;
                    Assert.AreEqual("aaa", parser.ReadFields().ToString("|"));
                }
            }
        }
Exemplo n.º 10
0
        public void CanSqueezeDelimiters()
        {
            string text = "a  bb  ccc  dddd";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.SetDelimiters(' ');
                    parser.SqueezeDelimiters = true;
                    Assert.AreEqual("a:bb:ccc:dddd", parser.ReadFields().ToString(":"));
                }
            }
        }
Exemplo n.º 11
0
        public void CanTreatQuotesAsNormalChars()
        {
            string text = "Michael \"Magic\" Jordan";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.HasFieldsEnclosedInQuotes = false;
                    TextFields fields = parser.ReadFields();
                    Assert.AreEqual(1, fields.Count);
                    Assert.AreEqual(text, fields[0]);
                }
            }
        }
Exemplo n.º 12
0
        public void CanTrimWhiteSpace()
        {
            string newLine = "\r\n";
            string text    = "a;bb ; ccc; dddd " + newLine;

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.SetDelimiters(';');
                    parser.TrimWhiteSpace = true;
                    Assert.AreEqual("a:bb:ccc:dddd", parser.ReadFields().ToString(":"));
                }
            }
        }
Exemplo n.º 13
0
        public void CanSkipCommentedLines()
        {
            string newLine = "\r\n";
            string text    = "#Comment line" + newLine + "a;bb;ccc;dddd";

            using (StringReader reader = new StringReader(text))
            {
                using (DelimitedFieldParser parser = new DelimitedFieldParser(reader))
                {
                    parser.SetDelimiters(';');
                    parser.SetCommentTokens("#");
                    Assert.AreEqual("a:bb:ccc:dddd", parser.ReadFields().ToString(":"));
                }
            }
        }
Exemplo n.º 14
0
        public void CanReadFromFile()
        {
            string textFile = Path.GetTempFileName();

            try
            {
                using (StreamWriter writer = new StreamWriter(textFile, false))
                {
                    writer.WriteLine("a,bb,ccc,dddd");
                }

                using (DelimitedFieldParser parser = new DelimitedFieldParser(textFile))
                {
                    parser.SetDelimiters(',');
                    Assert.AreEqual("a:bb:ccc:dddd", parser.ReadFields().ToString(":"));
                }
            }
            finally
            {
                File.Delete(textFile);
            }
        }