Ejemplo n.º 1
0
        void Test(String lfdText, params ExpectedLine[] expectedLines)
        {
            //
            // Test using LfdReader
            //
            using (var reader = new LfdTextReader(new StringReader(lfdText)))
            {
                for (int i = 0; i < expectedLines.Length; i++)
                {
                    var line = reader.ReadLine();
                    Assert.AreEqual(expectedLines[i].id, line.id);
                    if (expectedLines[i].fields == null || expectedLines[i].fields.Length == 0)
                    {
                        Assert.IsNull(line.fields);
                    }
                    else
                    {
                        Assert.AreEqual(expectedLines[i].fields.Length, line.fields.Length);
                        for (int fieldIndex = 0; fieldIndex < line.fields.Length; fieldIndex++)
                        {
                            Assert.AreEqual(expectedLines[i].fields[fieldIndex], line.fields[fieldIndex]);
                        }
                    }
                }
                Assert.IsNull(reader.ReadLine());
            }


            Byte[] linesAsBytes = Encoding.UTF8.GetBytes(lfdText);

            //
            // Test using LineParser
            //
            for (uint initialBufferSize = 1; initialBufferSize <= lfdText.Length; initialBufferSize++)
            {
                OffsetLineParser lineParser = new OffsetLineParser(new ByteArrayReference(initialBufferSize));
                lineParser.Add(linesAsBytes, linesAsBytes.Length);

                List <String> fields = new List <String>();
                Byte[]        lineBuffer;
                Int32         lineOffset = 0, lineLength = 0;

                for (int i = 0; i < expectedLines.Length; i++)
                {
                    do
                    {
                        lineBuffer = lineParser.GetLine(ref lineOffset, ref lineLength);
                        Assert.IsNotNull(lineBuffer);
                        fields.Clear();
                        LfdLine.ParseLine(fields, lineBuffer, lineOffset, lineOffset + lineLength);
                    } while (fields.Count == 0);

                    Assert.AreEqual(expectedLines[i].id, fields[0]);
                    if (expectedLines[i].fields == null || expectedLines[i].fields.Length == 0)
                    {
                        Assert.AreEqual(1, fields.Count);
                    }
                    else
                    {
                        Assert.AreEqual(expectedLines[i].fields.Length, fields.Count - 1);
                        for (int fieldIndex = 0; fieldIndex < fields.Count - 1; fieldIndex++)
                        {
                            Assert.AreEqual(expectedLines[i].fields[fieldIndex], fields[fieldIndex + 1]);
                        }
                    }
                }
                Assert.IsNull(lineParser.GetLine(ref lineOffset, ref lineLength));
            }
        }
Ejemplo n.º 2
0
        public void GenericTestMethod()
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine("");
            builder.AppendLine("LineWithNoValues \t  ");
            builder.AppendLine("Line1Fields \t field1  \t\t");
            builder.AppendLine("Line2Fields \t field1  \t\tfield2  ");
            builder.AppendLine("LineFieldWithWhitespace \t\t  \t \"this ; is a long field\"  \t");
            builder.AppendLine("LineWithEmptyFields \t\t\"\"  \"\"  \t");

            using (var reader = new LfdTextReader(new StringReader(builder.ToString())))
            {
                LfdLine line = reader.ReadLine();

                Assert.AreEqual("LineWithNoValues", line.id);

                line = reader.ReadLine();
                Assert.AreEqual("Line1Fields", line.id);
                Assert.AreEqual("field1", line.fields[0]);

                line = reader.ReadLine();
                Assert.AreEqual("Line2Fields", line.id);
                Assert.AreEqual("field1", line.fields[0]);
                Assert.AreEqual("field2", line.fields[1]);

                line = reader.ReadLine();
                Assert.AreEqual("LineFieldWithWhitespace", line.id);
                Assert.AreEqual("this ; is a long field", line.fields[0]);

                line = reader.ReadLine();
                Assert.AreEqual("LineWithEmptyFields", line.id);
                Assert.AreEqual(String.Empty, line.fields[0]);
                Assert.AreEqual("", line.fields[1]);

                Assert.IsNull(reader.ReadLine());
            }
            //
            // Test using line parser
            //
            OffsetLineParser lineParser = new OffsetLineParser(new ByteArrayReference(1));

            Byte[] linesAsBytes = Encoding.UTF8.GetBytes(builder.ToString());
            lineParser.Add(linesAsBytes, linesAsBytes.Length);

            List <String> fields = new List <String>();

            Byte[] lineBuffer;
            Int32  lineOffset = 0, lineLength = 0;

            lineBuffer = lineParser.GetLine(ref lineOffset, ref lineLength);
            LfdLine.ParseLine(fields, lineBuffer, lineOffset, lineOffset + lineLength);
            Assert.AreEqual(0, fields.Count);
            fields.Clear();

            lineBuffer = lineParser.GetLine(ref lineOffset, ref lineLength);
            LfdLine.ParseLine(fields, lineBuffer, lineOffset, lineOffset + lineLength);
            Assert.AreEqual("LineWithNoValues", fields[0]);
            fields.Clear();

            lineBuffer = lineParser.GetLine(ref lineOffset, ref lineLength);
            LfdLine.ParseLine(fields, lineBuffer, lineOffset, lineOffset + lineLength);
            Assert.AreEqual("Line1Fields", fields[0]);
            Assert.AreEqual("field1", fields[1]);
            fields.Clear();

            lineBuffer = lineParser.GetLine(ref lineOffset, ref lineLength);
            LfdLine.ParseLine(fields, lineBuffer, lineOffset, lineOffset + lineLength);
            Assert.AreEqual("Line2Fields", fields[0]);
            Assert.AreEqual("field1", fields[1]);
            Assert.AreEqual("field2", fields[2]);
            fields.Clear();

            lineBuffer = lineParser.GetLine(ref lineOffset, ref lineLength);
            LfdLine.ParseLine(fields, lineBuffer, lineOffset, lineOffset + lineLength);
            Assert.AreEqual("LineFieldWithWhitespace", fields[0]);
            Assert.AreEqual("this ; is a long field", fields[1]);
            fields.Clear();

            lineBuffer = lineParser.GetLine(ref lineOffset, ref lineLength);
            LfdLine.ParseLine(fields, lineBuffer, lineOffset, lineOffset + lineLength);
            Assert.AreEqual("LineWithEmptyFields", fields[0]);
            fields.Clear();
        }