ProcessFilesTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using Xunit;
  5. using FileOperations;
  6. namespace ProcessFilesTests
  7. {
  8. public class ProcessFilesTests
  9. {
  10. private const string TestFolder = "./testFiles";
  11. private const string TestFile = "./testFiles/test1.txt";
  12. private const string ExpectedInFolder = "test1.txt";
  13. private readonly List<string> expectedInSubFolder =
  14. [
  15. ExpectedInFolder,
  16. "test2.txt",
  17. "test3.txt"
  18. ];
  19. private readonly List<string> expectedInSubFolderMultipleExtensions =
  20. [
  21. "test.json"
  22. ];
  23. public ProcessFilesTests()
  24. {
  25. expectedInSubFolderMultipleExtensions.AddRange(expectedInSubFolder);
  26. }
  27. [Fact]
  28. public void ProcessFolderTest()
  29. {
  30. var result = string.Empty;
  31. void TestAction(string value)
  32. {
  33. result = Path.GetFileName(value);
  34. }
  35. var test = new ProcessFiles();
  36. var errors = test.Process([TestFolder], "txt", TestAction);
  37. Assert.Empty(errors);
  38. Assert.Equal(ExpectedInFolder, result);
  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. void TestAction(string value)
  49. {
  50. result.Add(Path.GetFileName(value));
  51. }
  52. var test = new ProcessFiles();
  53. var errors = test.Process([TestFolder], "txt", TestAction, true);
  54. Assert.Empty(errors);
  55. Assert.True(CheckResult(result, expectedInSubFolder));
  56. }
  57. [Fact]
  58. public void ProcessFolderAndFileTest()
  59. {
  60. var result = new List<string>();
  61. void TestAction(string value)
  62. {
  63. result.Add(Path.GetFileName(value));
  64. }
  65. var test = new ProcessFiles();
  66. var errors = test.Process(["./testFiles/subFolder", TestFile], "txt", TestAction);
  67. Assert.Empty(errors);
  68. Assert.True(CheckResult(result, expectedInSubFolder));
  69. }
  70. [Fact]
  71. public void ProcessFileTest()
  72. {
  73. var result = string.Empty;
  74. void TestAction(string value)
  75. {
  76. result = Path.GetFileName(value);
  77. }
  78. var test = new ProcessFiles();
  79. var errors = test.Process([TestFile], "txt", TestAction);
  80. Assert.Empty(errors);
  81. Assert.Equal(ExpectedInFolder, result);
  82. }
  83. [Fact]
  84. public void ProcessFileNotExistTest()
  85. {
  86. var result = string.Empty;
  87. void TestAction(string value)
  88. {
  89. result = value;
  90. }
  91. var test = new ProcessFiles();
  92. var errors = test.Process(["./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 test = new ProcessFiles();
  105. var errors = test.Process([TestFile], "abc", TestAction);
  106. Assert.NotEmpty(errors);
  107. Assert.Empty(result);
  108. }
  109. [Fact]
  110. public void ProcessFolderRecursiveMultipleExtensionsTest()
  111. {
  112. var result = new List<string>();
  113. void TestAction(string value)
  114. {
  115. result.Add(Path.GetFileName(value));
  116. }
  117. var test = new ProcessFiles();
  118. var errors = test.Process([TestFolder], ["txt", "json"], TestAction, true);
  119. Assert.Empty(errors);
  120. Assert.True(CheckResult(result, expectedInSubFolderMultipleExtensions));
  121. }
  122. }
  123. }