Skip to content

Commit

Permalink
Fix implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Dec 2, 2023
1 parent f35b902 commit 73090ee
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/FileParser/Implementations/ParsedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private static ICollection<string> ProcessLine(string original_line, string? sep

if (ignoreEmptyItems)
{
wordsInLine.RemoveAll(string.IsNullOrWhiteSpace); // Probably not needed, but just in case
wordsInLine.RemoveAll(string.IsNullOrWhiteSpace);
}

return wordsInLine;
Expand Down
5 changes: 3 additions & 2 deletions src/FileParser/Utils/StringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ internal static class StringConverter
/// <returns></returns>
public static T Convert<T>(string str, TypeConverter? typeConverter = null)
{
if (str?.Length == 0)
if (str.Length == 0)
{
return (T)(object)default;
return default!;
}

return default(T) switch
{
short => (T)(object)short.Parse(str),
Expand Down
43 changes: 24 additions & 19 deletions tests/FileParser.Test/ParsedFileTest/ExtractTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void ParseCsvFileIntoStrings()
while (!file.Empty)
{
var line = file.NextLine().ToList<string>();
for(int index = 0; index < line.Count; ++index)
for (int index = 0; index < line.Count; ++index)
{
buckets[index].Add(ModifyString(line[index]));
}
Expand All @@ -143,13 +143,15 @@ public void ParseCsvFileIntoStrings()
Assert.Equal(new[] { "age", "66", "1000", "33" }, buckets[2]);
Assert.Equal(new[] { "eyecolor", "brown", "black", "white" }, buckets[3]);
}

[Fact]
public void ParseCsvFile()
{
const string fileName = "ParseCsvFile.txt";
const string line1 = "Name,Age,BirtDate,Score";
const string line4 = "Abraham,10,12/12/1990,9.05";
const string line1 = "Name,Age,Score";
const string line2 = "Abraham,10,9.05";
const string line3 = "Babilon,,64.05";
const string line4 = "Croc,99,";

using (StreamWriter writer = new(fileName))
{
Expand All @@ -161,24 +163,27 @@ public void ParseCsvFile()

IParsedFile file = new ParsedFile(fileName, ",", ignoreEmptyItems: false);

var headerLine = file.NextLine();
var buckets = new List<List<string>>(headerLine.ToList<string>().Select(header => new List<string>(1000) { ModifyString(header) }));
file.NextLine();

while (!file.Empty)
{
var line = file.NextLine().ToList<string>();
for(int index = 0; index < line.Count; ++index)
{
buckets[index].Add(ModifyString(line[index]));
}
}
var line = file.NextLine();

static string ModifyString(string str) => str.ToLowerInvariant();
Assert.Equal("Abraham", line.NextElement<string>());
Assert.Equal(10, line.NextElement<int>());
Assert.Equal(9.05, line.NextElement<double>());

Assert.Equal(new[] { "firstname", "john", "cthulhu", "bugs" }, buckets[0]);
Assert.Equal(new[] { "lastname", "doe", "", "bunny" }, buckets[1]);
Assert.Equal(new[] { "age", "66", "1000", "33" }, buckets[2]);
Assert.Equal(new[] { "eyecolor", "brown", "black", "white" }, buckets[3]);
line = file.NextLine();

Assert.Equal("Babilon", line.NextElement<string>());
Assert.Equal(default, line.NextElement<int>());
Assert.Equal(64.05, line.NextElement<double>());

line = file.NextLine();

Assert.Equal("Croc", line.NextElement<string>());
Assert.Equal(99, line.NextElement<int>());
Assert.Equal(default, line.NextElement<double>());

Assert.True(file.Empty);
}
}
}

0 comments on commit 73090ee

Please sign in to comment.