ProcessFilesTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using Xunit;
  5. namespace ProcessFilesTests
  6. {
  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. void TestAction(string value)
  31. {
  32. result = Path.GetFileName(value);
  33. }
  34. var test = new ProcessFiles.ProcessFiles();
  35. var errors = test.Process([TestFolder], "txt", TestAction);
  36. Assert.Empty(errors);
  37. Assert.Equal(ExpectedInFolder, result);
  38. }
  39. private static bool CheckResult(List<string> result, List<string> expected)
  40. {
  41. return result.Count.Equals(expected.Count) && result.All(expected.Contains);
  42. }
  43. [Fact]
  44. public void ProcessFolderRecursiveTest()
  45. {
  46. var result = new List<string>();
  47. void TestAction(string value)
  48. {
  49. result.Add(Path.GetFileName(value));
  50. }
  51. var test = new ProcessFiles.ProcessFiles();
  52. var errors = test.Process([TestFolder], "txt", TestAction, true);
  53. Assert.Empty(errors);
  54. Assert.True(CheckResult(result, expectedInSubFolder));
  55. }
  56. [Fact]
  57. public void ProcessFolderAndFileTest()
  58. {
  59. var result = new List<string>();
  60. void TestAction(string value)
  61. {
  62. result.Add(Path.GetFileName(value));
  63. }
  64. var test = new ProcessFiles.ProcessFiles();
  65. var errors = test.Process(["./testFiles/subFolder", TestFile], "txt", TestAction);
  66. Assert.Empty(errors);
  67. Assert.True(CheckResult(result, expectedInSubFolder));
  68. }
  69. [Fact]
  70. public void ProcessFileTest()
  71. {
  72. var result = string.Empty;
  73. void TestAction(string value)
  74. {
  75. result = Path.GetFileName(value);
  76. }
  77. var test = new ProcessFiles.ProcessFiles();
  78. var errors = test.Process([TestFile], "txt", TestAction);
  79. Assert.Empty(errors);
  80. Assert.Equal(ExpectedInFolder, result);
  81. }
  82. [Fact]
  83. public void ProcessFileNotExistTest()
  84. {
  85. var result = string.Empty;
  86. void TestAction(string value)
  87. {
  88. result = value;
  89. }
  90. var test = new ProcessFiles.ProcessFiles();
  91. var errors = test.Process(["./testFiles/test.txt"], "txt", TestAction);
  92. Assert.NotEmpty(errors);
  93. Assert.Empty(result);
  94. }
  95. [Fact]
  96. public void ProcessFileNotMatchExtensionTest()
  97. {
  98. var result = string.Empty;
  99. void TestAction(string value)
  100. {
  101. result = value;
  102. }
  103. var test = new ProcessFiles.ProcessFiles();
  104. var errors = test.Process([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 test = new ProcessFiles.ProcessFiles();
  117. var errors = test.Process([TestFolder], ["txt", "json"], TestAction, true);
  118. Assert.Empty(errors);
  119. Assert.True(CheckResult(result, expectedInSubFolderMultipleExtensions));
  120. }
  121. }
  122. }