Piotr Czajkowski преди 4 дни
родител
ревизия
99f5d10e07
променени са 2 файла, в които са добавени 30 реда и са изтрити 28 реда
  1. 11 13
      ProcessFiles/ProcessFiles.cs
  2. 19 15
      ProcessFilesTests/ProcessFilesTests.cs

+ 11 - 13
ProcessFiles/ProcessFiles.cs

@@ -12,11 +12,11 @@ namespace ProcessFiles
         Failure
     }
 
-    public static class ProcessFiles
+    public class ProcessFiles
     {
-        private static List<string> errors = new();
+        private List<string> errors = [];
 
-        private static Result WhatIsIt(string path)
+        private Result WhatIsIt(string path)
         {
             try
             {
@@ -30,7 +30,7 @@ namespace ProcessFiles
             }
         }
 
-        private static string? GetExtension(string path)
+        private string? GetExtension(string path)
         {
             var extension = Path.GetExtension(path).TrimStart('.');
             if (!string.IsNullOrWhiteSpace(extension)) return extension;
@@ -39,12 +39,12 @@ namespace ProcessFiles
             return null;
         }
 
-        private static bool CheckExtension(string extension, string[] validExtensions)
+        private bool CheckExtension(string extension, string[] validExtensions)
         {
             return validExtensions.Any(validExtension => extension.Equals(validExtension, StringComparison.InvariantCultureIgnoreCase));
         }
 
-        private static bool IsValid(string path, string[] fileExtensions)
+        private bool IsValid(string path, string[] fileExtensions)
         {
             if (!File.Exists(path))
             {
@@ -63,7 +63,7 @@ namespace ProcessFiles
 
         }
 
-        private static void PerformAction(string path, Action<string> action)
+        private void PerformAction(string path, Action<string> action)
         {
             try
             {
@@ -75,7 +75,7 @@ namespace ProcessFiles
             }
         }
 
-        private static void ProcessFile(string path, string[] fileExtensions, Action<string> action)
+        private void ProcessFile(string path, string[] fileExtensions, Action<string> action)
         {
             if (!IsValid(path, fileExtensions))
                 return;
@@ -83,7 +83,7 @@ namespace ProcessFiles
             PerformAction(path, action);
         }
 
-        private static void ProcessDir(string path, string[] fileExtensions, Action<string> action, bool recursive = false)
+        private void ProcessDir(string path, string[] fileExtensions, Action<string> action, bool recursive = false)
         {
             if (!Directory.Exists(path))
             {
@@ -113,15 +113,13 @@ namespace ProcessFiles
                 ProcessFile(file, fileExtensions, action);
         }
 
-        public static IEnumerable<string> Process(IEnumerable<string> arguments, string fileExtension, Action<string> action, bool recursive = false)
+        public IEnumerable<string> Process(IEnumerable<string> arguments, string fileExtension, Action<string> action, bool recursive = false)
         {
             return Process(arguments, [fileExtension], action, recursive);
         }
 
-        public static IEnumerable<string> Process(IEnumerable<string> arguments, string[] fileExtensions, Action<string> action, bool recursive = false)
+        public IEnumerable<string> Process(IEnumerable<string> arguments, string[] fileExtensions, Action<string> action, bool recursive = false)
         {
-            errors = new List<string>();
-
             foreach (var argument in arguments)
             {
                 switch (WhatIsIt(argument))

+ 19 - 15
ProcessFilesTests/ProcessFilesTests.cs

@@ -9,18 +9,15 @@ namespace ProcessFilesTests
         private readonly string testFolder = "./testFiles";
         private readonly string testFile = "./testFiles/test1.txt";
 
-        private readonly List<string> expectedInSubFolder = new()
-        {
+        private readonly List<string> expectedInSubFolder =
+        [
             "test2.txt",
             "test3.txt"
-        };
+        ];
 
-        private readonly List<string> expectedInSubFolderMultipleExtensions = new()
-        {
-            "test.json"
-        };
+        private readonly List<string> expectedInSubFolderMultipleExtensions = ["test.json"];
 
-        private readonly string expectedInFolder = "test1.txt";
+        private const string expectedInFolder = "test1.txt";
 
         public ProcessFilesTests()
         {
@@ -37,7 +34,8 @@ namespace ProcessFilesTests
                 result = Path.GetFileName(value);
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", TestAction);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process([testFolder], "txt", TestAction);
             Assert.Empty(errors);
             Assert.Equal(expectedInFolder, result);
         }
@@ -65,7 +63,8 @@ namespace ProcessFilesTests
                 result.Add(Path.GetFileName(value));
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", TestAction, true);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process([testFolder], "txt", TestAction, true);
             Assert.Empty(errors);
             Assert.True(CheckResult(result, expectedInSubFolder));
         }
@@ -79,7 +78,8 @@ namespace ProcessFilesTests
                 result.Add(Path.GetFileName(value));
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/subFolder", testFile }, "txt", TestAction);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process(["./testFiles/subFolder", testFile], "txt", TestAction);
             Assert.Empty(errors);
             Assert.True(CheckResult(result, expectedInSubFolder));
         }
@@ -93,7 +93,8 @@ namespace ProcessFilesTests
                 result = Path.GetFileName(value);
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "txt", TestAction);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process([testFile], "txt", TestAction);
             Assert.Empty(errors);
             Assert.Equal(expectedInFolder, result);
         }
@@ -107,7 +108,8 @@ namespace ProcessFilesTests
                 result = value;
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/test.txt" }, "txt", TestAction);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process(["./testFiles/test.txt"], "txt", TestAction);
             Assert.NotEmpty(errors);
             Assert.Empty(result);
         }
@@ -121,7 +123,8 @@ namespace ProcessFilesTests
                 result = value;
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "abc", TestAction);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process([testFile], "abc", TestAction);
             Assert.NotEmpty(errors);
             Assert.Empty(result);
         }
@@ -135,7 +138,8 @@ namespace ProcessFilesTests
                 result.Add(Path.GetFileName(value));
             }
 
-            var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, new[] { "txt", "json" }, TestAction, true);
+            var test = new ProcessFiles.ProcessFiles();
+            var errors = test.Process([testFolder], ["txt", "json"], TestAction, true);
             Assert.Empty(errors);
             Assert.True(CheckResult(result, expectedInSubFolderMultipleExtensions));
         }