From afcd404dfc7e4a740edf2fa62c3e1dd1b624fc00 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R." Date: Sun, 18 Feb 2024 15:57:48 +0100 Subject: [PATCH] Tests for CsvParser --- CsvLib.Tests/ByteArraySearcherTests.cs | 1 + CsvLib.Tests/CsvFieldIndexerTests.cs | 1 + CsvLib.Tests/CsvLib.Tests.csproj | 1 + CsvLib.Tests/CsvParserTest.cs | 134 +++++++++++++++++++++++++ CsvLib.Tests/Usings.cs | 1 + CsvLib/CsvParser.cs | 25 +++-- 6 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 CsvLib.Tests/CsvParserTest.cs diff --git a/CsvLib.Tests/ByteArraySearcherTests.cs b/CsvLib.Tests/ByteArraySearcherTests.cs index e8e2418..96078b2 100644 --- a/CsvLib.Tests/ByteArraySearcherTests.cs +++ b/CsvLib.Tests/ByteArraySearcherTests.cs @@ -2,6 +2,7 @@ using CsvLib; namespace CvsLib; +[TestSubject(typeof(ByteArraySearcher))] public class ByteArraySearcherTests { [Fact] diff --git a/CsvLib.Tests/CsvFieldIndexerTests.cs b/CsvLib.Tests/CsvFieldIndexerTests.cs index 016258b..367deae 100644 --- a/CsvLib.Tests/CsvFieldIndexerTests.cs +++ b/CsvLib.Tests/CsvFieldIndexerTests.cs @@ -3,6 +3,7 @@ using CsvLib; namespace CvsLib; +[TestSubject(typeof(CsvFieldIndexer))] public class CsvFieldIndexerTests { #region GenerateIndex diff --git a/CsvLib.Tests/CsvLib.Tests.csproj b/CsvLib.Tests/CsvLib.Tests.csproj index 089b48f..7c96ec1 100644 --- a/CsvLib.Tests/CsvLib.Tests.csproj +++ b/CsvLib.Tests/CsvLib.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/CsvLib.Tests/CsvParserTest.cs b/CsvLib.Tests/CsvParserTest.cs new file mode 100644 index 0000000..df5a950 --- /dev/null +++ b/CsvLib.Tests/CsvParserTest.cs @@ -0,0 +1,134 @@ +using CsvLib; + +namespace CvsLib; + +[TestSubject(typeof(CsvParser))] +public class CsvParserTest +{ + #region Parse + + [Fact] + public void Parse__Empty__Empty() + { + // --- Arrange + StringReader sr = new(string.Empty); + + // --- Act + CsvParser parser = new(); + parser.Parse(sr); + + // --- Assert + Assert.Empty(parser.Data); + } + + [Fact] + public void Parse__PlainText__OneRowOneColumn() + { + // --- Arrange + StringReader sr = new("Hello World"); + + // --- Act + CsvParser parser = new(); + parser.Parse(sr); + + // --- Assert + Assert.Single(parser.Data); + Assert.Single(parser.Data[0]); + Assert.Equal("Hello World", parser.Data[0][0]); + } + + + [Fact] + public void Parse__TwoLinesOfPainText__TwoRows() + { + // --- Arrange + StringReader sr = new( + """ + Hello World + Hello World + """); + + // --- Act + CsvParser parser = new(); + parser.Parse(sr); + + // --- Assert + Assert.Equal(2, parser.Data.Count); + Assert.Single(parser.Data[0]); + Assert.Equal("Hello World", parser.Data[0][0]); + Assert.Single(parser.Data[1]); + Assert.Equal("Hello World", parser.Data[1][0]); + } + + [Fact] + public void Parse__TwoLinesOfQuotedText__TwoRows() + { + // --- Arrange + StringReader sr = new( + """ + "Hello World" + "Hello World" + """); + + // --- Act + CsvParser parser = new(); + parser.Parse(sr); + + // --- Assert + Assert.Equal(2, parser.Data.Count); + Assert.Single(parser.Data[0]); + Assert.Equal("Hello World", parser.Data[0][0]); + Assert.Single(parser.Data[1]); + Assert.Equal("Hello World", parser.Data[1][0]); + } + + [Fact] + public void Parse__TwoLinesWithTwoQuotedColumns__TwoRowsTwoFields() + { + // --- Arrange + StringReader sr = new( + """ + "Hello","World" + "Hello","World" + """); + + // --- Act + CsvParser parser = new(); + parser.Parse(sr); + + // --- Assert + Assert.Equal(2, parser.Data.Count); + Assert.Equal(2, parser.Data[0].Count); + Assert.Equal("Hello", parser.Data[0][0]); + Assert.Equal("World", parser.Data[0][1]); + Assert.Equal(2, parser.Data[1].Count); + Assert.Equal("Hello", parser.Data[1][0]); + Assert.Equal("World", parser.Data[1][1]); + } + + [Fact] + public void GenerateIndex__TwoLinesWithTwoQuotedColumnsWithUnicode__TwoRowsTwoFields() + { + // --- Arrange + StringReader sr = new( + """ + "Hélló","Wórld" + "Hélló","Wórld" + """); + + // --- Act + CsvParser parser = new(); + parser.Parse(sr); + + // --- Assert + Assert.Equal(2, parser.Data.Count); + Assert.Equal(2, parser.Data[0].Count); + Assert.Equal("Hélló", parser.Data[0][0]); + Assert.Equal("Wórld", parser.Data[0][1]); + Assert.Equal(2, parser.Data[1].Count); + Assert.Equal("Hélló", parser.Data[1][0]); + Assert.Equal("Wórld", parser.Data[1][1]); + } + + #endregion Parse +} diff --git a/CsvLib.Tests/Usings.cs b/CsvLib.Tests/Usings.cs index c802f44..f545ca8 100644 --- a/CsvLib.Tests/Usings.cs +++ b/CsvLib.Tests/Usings.cs @@ -1 +1,2 @@ global using Xunit; +global using JetBrains.Annotations; \ No newline at end of file diff --git a/CsvLib/CsvParser.cs b/CsvLib/CsvParser.cs index b3c9aed..6ed5415 100644 --- a/CsvLib/CsvParser.cs +++ b/CsvLib/CsvParser.cs @@ -76,25 +76,28 @@ public class CsvParser _currentReg = null; } } - - public void ParseFile(string file, long offset = 0, int count = 0) + + public void Parse(TextReader reader, int count = 0) { _insideString = false; _data = new List>(); _currentReg = null; - FileStream stream = new(file, FileMode.Open); - stream.Seek(offset, SeekOrigin.Begin); - using (StreamReader reader = new(stream, Encoding.Default, true, 4096)) + while (reader.ReadLine() is { } currentLine) { - while (reader.ReadLine() is { } currentLine) + ParseLine(currentLine); + if (count > 0 && _data.Count == count) { - ParseLine(currentLine); - if (count > 0 && Data.Count == count) - { - break; - } + break; } } + } + + public void ParseFile(string file, long offset = 0, int count = 0) + { + FileStream stream = new(file, FileMode.Open); + stream.Seek(offset, SeekOrigin.Begin); + using StreamReader reader = new(stream, Encoding.Default, true, 4096); + Parse(reader, count); stream.Close(); }