From 74eb24711522d59cf7a2a890806685d160313d90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Sat, 2 Dec 2023 19:36:53 +0100 Subject: [PATCH] Make `static ParsedFile.Queue ParseFile()` private --- src/FileParser/Implementations/ParsedFile.cs | 15 +++++------ src/FileParser/Implementations/ParsedLine.cs | 2 +- src/FileParser/Utils/TypeValidator.cs | 8 +++--- .../ParsedFileTest/ExtractTests.cs | 27 ++++++++++++------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/FileParser/Implementations/ParsedFile.cs b/src/FileParser/Implementations/ParsedFile.cs index e370e01..4a20ab8 100644 --- a/src/FileParser/Implementations/ParsedFile.cs +++ b/src/FileParser/Implementations/ParsedFile.cs @@ -44,9 +44,7 @@ public ParsedFile(string path, char[] existingSeparator, bool ignoreEmptyItems = /// Word separator (space by default) /// 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)) { } @@ -76,7 +74,7 @@ public IParsedLine LastLine() public List ToList(string? lineSeparatorToAdd = null) { - List list = new(); + List list = []; if (!string.IsNullOrEmpty(lineSeparatorToAdd)) { @@ -132,7 +130,7 @@ public static List> ReadAllGroupsOfLines(string path) { if (currentGroup.Count != 0) { - currentGroup = new List(); + currentGroup = []; result.Add(currentGroup); } } @@ -182,7 +180,7 @@ public static List> ReadAllGroupsOfLines(string path) { if (currentGroup.Count != 0) { - currentGroup = new List(); + currentGroup = []; result.Add(currentGroup); } } @@ -208,16 +206,15 @@ public static List> ReadAllGroupsOfLines(string path) /// Queue<IParsedLine> ~~ Queues of 'words' inside of a queue of lines /// /// - /// Word separator /// + /// Word separator /// /// /// /// /// /// - [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 ParseFile(string path, string? existingSeparator = null, bool ignoreEmptyItems = true) + private static Queue ParseFile(string path, bool ignoreEmptyItems, string? existingSeparator = null) { Queue parsedFile = new(); diff --git a/src/FileParser/Implementations/ParsedLine.cs b/src/FileParser/Implementations/ParsedLine.cs index 288c0a2..8c79062 100644 --- a/src/FileParser/Implementations/ParsedLine.cs +++ b/src/FileParser/Implementations/ParsedLine.cs @@ -62,7 +62,7 @@ public T LastElement() public List ToList() { - List list = new(); + List list = []; while (!Empty) { diff --git a/src/FileParser/Utils/TypeValidator.cs b/src/FileParser/Utils/TypeValidator.cs index 2e3c7b5..3823284 100644 --- a/src/FileParser/Utils/TypeValidator.cs +++ b/src/FileParser/Utils/TypeValidator.cs @@ -5,8 +5,8 @@ internal static class TypeValidator /// /// Supported parsing conversions /// - private static HashSet SupportedTypes { get; } = new HashSet() - { + private static HashSet SupportedTypes { get; } = + [ typeof(bool), typeof(char), typeof(string), @@ -18,13 +18,13 @@ internal static class TypeValidator typeof(long), typeof(double), typeof(object) - }; + ]; internal static void ValidateSupportedType() { 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"); } } } diff --git a/tests/FileParser.Test/ParsedFileTest/ExtractTests.cs b/tests/FileParser.Test/ParsedFileTest/ExtractTests.cs index f9b126b..16ea55a 100644 --- a/tests/FileParser.Test/ParsedFileTest/ExtractTests.cs +++ b/tests/FileParser.Test/ParsedFileTest/ExtractTests.cs @@ -11,7 +11,7 @@ public void CustomLineParse() const string fileName = "CustomLineParse.txt"; const string sampleContent = " 3 1154 508 100 vegetable "; - List expectedList = new() { 1154, 508, 100 }; + List expectedList = [1154, 508, 100]; const string expectedString = "vegetable"; using (StreamWriter writer = new(fileName)) @@ -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())) - 1; for (int iLine = 0; iLine < nLines; ++iLine) { @@ -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(); @@ -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>(headerLine.ToList().Select(header => new List(1000) { ModifyString(header) })); @@ -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] @@ -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();