ProcessFilesTests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 string exptectedInFolder = "test1.txt";
  16. [Fact]
  17. public void ProcessFolderTest()
  18. {
  19. var result = string.Empty;
  20. void Callback(string value)
  21. {
  22. result = Path.GetFileName(value);
  23. }
  24. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", Callback);
  25. Assert.Empty(errors);
  26. Assert.Equal(exptectedInFolder, result);
  27. }
  28. private bool CheckResult(List<string> result, List<string> expected)
  29. {
  30. if (!result.Count.Equals(expected.Count))
  31. return false;
  32. foreach (var item in result)
  33. {
  34. if (!expected.Contains(item))
  35. return false;
  36. }
  37. return true;
  38. }
  39. [Fact]
  40. public void ProcessFolderRecursiveTest()
  41. {
  42. var expected = new List<string>
  43. {
  44. exptectedInFolder
  45. };
  46. expected.AddRange(expectedInSubFolder);
  47. var result = new List<string>();
  48. void Callback(string value)
  49. {
  50. result.Add(Path.GetFileName(value));
  51. }
  52. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", Callback, true);
  53. Assert.Empty(errors);
  54. Assert.True(CheckResult(result, expected));
  55. }
  56. [Fact]
  57. public void ProcessFolderAndFileTest()
  58. {
  59. var expected = new List<string>
  60. {
  61. exptectedInFolder
  62. };
  63. expected.AddRange(expectedInSubFolder);
  64. var result = new List<string>();
  65. void Callback(string value)
  66. {
  67. result.Add(Path.GetFileName(value));
  68. }
  69. var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/subFolder", testFile }, "txt", Callback);
  70. Assert.Empty(errors);
  71. Assert.True(CheckResult(result, expected));
  72. }
  73. [Fact]
  74. public void ProcessFileTest()
  75. {
  76. var result = string.Empty;
  77. void Callback(string value)
  78. {
  79. result = Path.GetFileName(value);
  80. }
  81. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "txt", Callback);
  82. Assert.Empty(errors);
  83. Assert.Equal(exptectedInFolder, result);
  84. }
  85. [Fact]
  86. public void ProcessFileNotExistTest()
  87. {
  88. var result = string.Empty;
  89. void Callback(string value)
  90. {
  91. result = value;
  92. }
  93. var errors = ProcessFiles.ProcessFiles.Process(new[] { "./testFiles/test.txt" }, "txt", Callback);
  94. Assert.NotEmpty(errors);
  95. Assert.NotEqual(exptectedInFolder, result);
  96. }
  97. [Fact]
  98. public void ProcessFileNotMatchExtensionTest()
  99. {
  100. var result = string.Empty;
  101. void Callback(string value)
  102. {
  103. result = value;
  104. }
  105. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFile }, "abc", Callback);
  106. Assert.NotEmpty(errors);
  107. Assert.Empty(result);
  108. }
  109. }
  110. }