ProcessFilesTests.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using Xunit;
  4. namespace ProcessFilesTests
  5. {
  6. public class ProcessFilesTests
  7. {
  8. private readonly string testFolder = "./testFiles";
  9. private readonly string testFile = "./testFiles/test1.txt";
  10. private readonly List<string> expectedInSubFolder = new()
  11. {
  12. "test2.txt",
  13. "test3.txt"
  14. };
  15. private readonly List<string> expectedInSubFolderMultipleExtensions = new()
  16. {
  17. "test.json"
  18. };
  19. private readonly string expectedInFolder = "test1.txt";
  20. public ProcessFilesTests()
  21. {
  22. expectedInSubFolder.Add(expectedInFolder);
  23. expectedInSubFolderMultipleExtensions.AddRange(expectedInSubFolder);
  24. }
  25. [Fact]
  26. public void ProcessFolderTest()
  27. {
  28. var result = string.Empty;
  29. void TestAction(string value)
  30. {
  31. result = Path.GetFileName(value);
  32. }
  33. var test = new ProcessFiles.ProcessFiles();
  34. var errors = test.Process([testFolder], "txt", TestAction);
  35. Assert.Empty(errors);
  36. Assert.Equal(expectedInFolder, result);
  37. }
  38. private static bool CheckResult(List<string> result, List<string> expected)
  39. {
  40. if (!result.Count.Equals(expected.Count))
  41. return false;
  42. foreach (var item in result)
  43. {
  44. if (!expected.Contains(item))
  45. return false;
  46. }
  47. return true;
  48. }
  49. [Fact]
  50. public void ProcessFolderRecursiveTest()
  51. {
  52. var result = new List<string>();
  53. void TestAction(string value)
  54. {
  55. result.Add(Path.GetFileName(value));
  56. }
  57. var test = new ProcessFiles.ProcessFiles();
  58. var errors = test.Process([testFolder], "txt", TestAction, true);
  59. Assert.Empty(errors);
  60. Assert.True(CheckResult(result, expectedInSubFolder));
  61. }
  62. [Fact]
  63. public void ProcessFolderAndFileTest()
  64. {
  65. var result = new List<string>();
  66. void TestAction(string value)
  67. {
  68. result.Add(Path.GetFileName(value));
  69. }
  70. var test = new ProcessFiles.ProcessFiles();
  71. var errors = test.Process(["./testFiles/subFolder", testFile], "txt", TestAction);
  72. Assert.Empty(errors);
  73. Assert.True(CheckResult(result, expectedInSubFolder));
  74. }
  75. [Fact]
  76. public void ProcessFileTest()
  77. {
  78. var result = string.Empty;
  79. void TestAction(string value)
  80. {
  81. result = Path.GetFileName(value);
  82. }
  83. var test = new ProcessFiles.ProcessFiles();
  84. var errors = test.Process([testFile], "txt", TestAction);
  85. Assert.Empty(errors);
  86. Assert.Equal(expectedInFolder, result);
  87. }
  88. [Fact]
  89. public void ProcessFileNotExistTest()
  90. {
  91. var result = string.Empty;
  92. void TestAction(string value)
  93. {
  94. result = value;
  95. }
  96. var test = new ProcessFiles.ProcessFiles();
  97. var errors = test.Process(["./testFiles/test.txt"], "txt", TestAction);
  98. Assert.NotEmpty(errors);
  99. Assert.Empty(result);
  100. }
  101. [Fact]
  102. public void ProcessFileNotMatchExtensionTest()
  103. {
  104. var result = string.Empty;
  105. void TestAction(string value)
  106. {
  107. result = value;
  108. }
  109. var test = new ProcessFiles.ProcessFiles();
  110. var errors = test.Process([testFile], "abc", TestAction);
  111. Assert.NotEmpty(errors);
  112. Assert.Empty(result);
  113. }
  114. [Fact]
  115. public void ProcessFolderRecursiveMultipleExtensionsTest()
  116. {
  117. var result = new List<string>();
  118. void TestAction(string value)
  119. {
  120. result.Add(Path.GetFileName(value));
  121. }
  122. var test = new ProcessFiles.ProcessFiles();
  123. var errors = test.Process([testFolder], ["txt", "json"], TestAction, true);
  124. Assert.Empty(errors);
  125. Assert.True(CheckResult(result, expectedInSubFolderMultipleExtensions));
  126. }
  127. }
  128. }