Skip to content

Commit

Permalink
Make static ParsedFile.Queue<IParsedLine> ParseFile() private
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Dec 2, 2023
1 parent 806b9ff commit 74eb247
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
15 changes: 6 additions & 9 deletions src/FileParser/Implementations/ParsedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public ParsedFile(string path, char[] existingSeparator, bool ignoreEmptyItems =
/// <param name="existingSeparator">Word separator (space by default)</param>
/// <param name="ignoreEmptyItems"></param>
public ParsedFile(string path, string? existingSeparator = null, bool ignoreEmptyItems = true)
#pragma warning disable CS0618 // Type or member is obsolete - will keep it as private
: base(ParseFile(path, existingSeparator, ignoreEmptyItems))
#pragma warning restore CS0618 // Type or member is obsolete
: base(ParseFile(path, ignoreEmptyItems, existingSeparator))
{
}

Expand Down Expand Up @@ -76,7 +74,7 @@ public IParsedLine LastLine()

public List<T> ToList<T>(string? lineSeparatorToAdd = null)
{
List<T> list = new();
List<T> list = [];

if (!string.IsNullOrEmpty(lineSeparatorToAdd))
{
Expand Down Expand Up @@ -132,7 +130,7 @@ public static List<List<string>> ReadAllGroupsOfLines(string path)
{
if (currentGroup.Count != 0)
{
currentGroup = new List<string>();
currentGroup = [];
result.Add(currentGroup);
}
}
Expand Down Expand Up @@ -182,7 +180,7 @@ public static List<List<T>> ReadAllGroupsOfLines<T>(string path)
{
if (currentGroup.Count != 0)
{
currentGroup = new List<T>();
currentGroup = [];
result.Add(currentGroup);
}
}
Expand All @@ -208,16 +206,15 @@ public static List<List<T>> ReadAllGroupsOfLines<T>(string path)
/// Queue&lt;IParsedLine&gt; ~~ Queues of 'words' inside of a queue of lines
/// </summary>
/// <param name="path"></param>
/// <param name="existingSeparator">Word separator</param>
/// <param name="ignoreEmptyItems"></param>
/// <param name="existingSeparator">Word separator</param>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="IOException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
[Obsolete("This method exposes internal functionality and was made public accidentally. It will be removed in next major release, please used ParsedFile constructor instead.")]
public static Queue<IParsedLine> ParseFile(string path, string? existingSeparator = null, bool ignoreEmptyItems = true)
private static Queue<IParsedLine> ParseFile(string path, bool ignoreEmptyItems, string? existingSeparator = null)
{
Queue<IParsedLine> parsedFile = new();

Expand Down
2 changes: 1 addition & 1 deletion src/FileParser/Implementations/ParsedLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public T LastElement<T>()

public List<T> ToList<T>()
{
List<T> list = new();
List<T> list = [];

while (!Empty)
{
Expand Down
8 changes: 4 additions & 4 deletions src/FileParser/Utils/TypeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ internal static class TypeValidator
/// <summary>
/// Supported parsing conversions
/// </summary>
private static HashSet<Type> SupportedTypes { get; } = new HashSet<Type>()
{
private static HashSet<Type> SupportedTypes { get; } =
[
typeof(bool),
typeof(char),
typeof(string),
Expand All @@ -18,13 +18,13 @@ internal static class TypeValidator
typeof(long),
typeof(double),
typeof(object)
};
];

internal static void ValidateSupportedType<T>()
{
if (!SupportedTypes.Contains(typeof(T)))
{
throw new NotSupportedException("Parsing to " + typeof(T).ToString() + "is not supported yet");
throw new NotSupportedException("Parsing to " + typeof(T) + "is not supported yet");
}
}
}
27 changes: 17 additions & 10 deletions tests/FileParser.Test/ParsedFileTest/ExtractTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void CustomLineParse()
const string fileName = "CustomLineParse.txt";
const string sampleContent = " 3 1154 508 100 vegetable ";

List<long> expectedList = new() { 1154, 508, 100 };
List<long> expectedList = [1154, 508, 100];
const string expectedString = "vegetable";

using (StreamWriter writer = new(fileName))
Expand Down Expand Up @@ -54,8 +54,8 @@ public void ExtractChar()
writer.WriteLine(line2);
}

IParsedFile file = new ParsedFile(fileName);
IParsedLine firstParsedLine = file.NextLine();
var file = new ParsedFile(fileName);
var firstParsedLine = file.NextLine();
int nLines = Convert.ToInt32(Math.Floor(firstParsedLine.NextElement<double>())) - 1;
for (int iLine = 0; iLine < nLines; ++iLine)
{
Expand Down Expand Up @@ -89,7 +89,7 @@ public void MultipleCharWordSeparator()
writer.WriteLine(line2);
}

IParsedFile file = new ParsedFile(fileName, "||");
var file = new ParsedFile(fileName, "||");

int index = 0;
var line = file.NextLine();
Expand Down Expand Up @@ -122,7 +122,7 @@ public void ParseCsvFileIntoStrings()
writer.WriteLine(line4);
}

IParsedFile file = new ParsedFile(fileName, ",", ignoreEmptyItems: false);
var 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) }));
Expand All @@ -138,10 +138,17 @@ public void ParseCsvFileIntoStrings()

static string ModifyString(string str) => str.ToLowerInvariant();

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]);
string[] expected = ["firstname", "john", "cthulhu", "bugs"];
Assert.Equal(expected, buckets[0]);

string[] expected1 = ["lastname", "doe", "", "bunny"];
Assert.Equal(expected1, buckets[1]);

string[] expected2 = ["age", "66", "1000", "33"];
Assert.Equal(expected2, buckets[2]);

string[] expected3 = ["eyecolor", "brown", "black", "white"];
Assert.Equal(expected3, buckets[3]);
}

[Fact]
Expand All @@ -161,7 +168,7 @@ public void ParseCsvFile()
writer.WriteLine(line4);
}

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

file.NextLine();

Expand Down

0 comments on commit 74eb247

Please sign in to comment.