ProcessFilesTests.cs 3.8 KB

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