ProcessFilesTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", TestAction);
  34. Assert.Empty(errors);
  35. Assert.Equal(expectedInFolder, result);
  36. }
  37. private static bool CheckResult(List<string> result, List<string> expected)
  38. {
  39. if (!result.Count.Equals(expected.Count))
  40. return false;
  41. foreach (var item in result)
  42. {
  43. if (!expected.Contains(item))
  44. return false;
  45. }
  46. return true;
  47. }
  48. [Fact]
  49. public void ProcessFolderRecursiveTest()
  50. {
  51. var result = new List<string>();
  52. void TestAction(string value)
  53. {
  54. result.Add(Path.GetFileName(value));
  55. }
  56. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", TestAction, true);
  57. Assert.Empty(errors);
  58. Assert.True(CheckResult(result, expectedInSubFolder));
  59. }
  60. [Fact]
  61. public void ProcessFolderAndFileTest()
  62. {
  63. var result = new List<string>();
  64. void TestAction(string value)
  65. {
  66. result.Add(Path.GetFileName(value));
  67. }
  68. var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/subFolder", testFile }, "txt", TestAction);
  69. Assert.Empty(errors);
  70. Assert.True(CheckResult(result, expectedInSubFolder));
  71. }
  72. [Fact]
  73. public void ProcessFileTest()
  74. {
  75. var result = string.Empty;
  76. void TestAction(string value)
  77. {
  78. result = Path.GetFileName(value);
  79. }
  80. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "txt", TestAction);
  81. Assert.Empty(errors);
  82. Assert.Equal(expectedInFolder, result);
  83. }
  84. [Fact]
  85. public void ProcessFileNotExistTest()
  86. {
  87. var result = string.Empty;
  88. void TestAction(string value)
  89. {
  90. result = value;
  91. }
  92. var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/test.txt" }, "txt", TestAction);
  93. Assert.NotEmpty(errors);
  94. Assert.Empty(result);
  95. }
  96. [Fact]
  97. public void ProcessFileNotMatchExtensionTest()
  98. {
  99. var result = string.Empty;
  100. void TestAction(string value)
  101. {
  102. result = value;
  103. }
  104. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "abc", TestAction);
  105. Assert.NotEmpty(errors);
  106. Assert.Empty(result);
  107. }
  108. [Fact]
  109. public void ProcessFolderRecursiveMultipleExtensionsTest()
  110. {
  111. var result = new List<string>();
  112. void TestAction(string value)
  113. {
  114. result.Add(Path.GetFileName(value));
  115. }
  116. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, new[] { "txt", "json" }, TestAction, true);
  117. Assert.Empty(errors);
  118. Assert.True(CheckResult(result, expectedInSubFolderMultipleExtensions));
  119. }
  120. }
  121. }