ProcessFilesTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Extensions;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Xunit;
  5. namespace ProcessFilesTests
  6. {
  7. public class ProcessFilesTests
  8. {
  9. private readonly string testFolder = "./testFiles";
  10. private readonly string testFile = "./testFiles/test1.txt";
  11. private readonly List<string> expectedInSubFolder = new()
  12. {
  13. "test2.txt",
  14. "test3.txt"
  15. };
  16. private readonly List<string> expectedInSubFolderMultipleExtensions = new()
  17. {
  18. "test.json"
  19. };
  20. private readonly string expectedInFolder = "test1.txt";
  21. public ProcessFilesTests()
  22. {
  23. expectedInSubFolder.Add(expectedInFolder);
  24. expectedInSubFolderMultipleExtensions.AddRange(expectedInSubFolder);
  25. }
  26. [Fact]
  27. public void ProcessFolderTest()
  28. {
  29. var result = string.Empty;
  30. void Callback(string value)
  31. {
  32. result = Path.GetFileName(value);
  33. }
  34. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", Callback);
  35. Assert.Empty(errors);
  36. Assert.Equal(expectedInFolder, result);
  37. }
  38. private static bool CheckResult(List<string> result, List<string> expected)
  39. {
  40. if (!result.Count.Equals(expected.Count))
  41. return false;
  42. foreach (var item in result)
  43. {
  44. if (!expected.Contains(item))
  45. return false;
  46. }
  47. return true;
  48. }
  49. [Fact]
  50. public void ProcessFolderRecursiveTest()
  51. {
  52. var result = new List<string>();
  53. void Callback(string value)
  54. {
  55. result.Add(Path.GetFileName(value));
  56. }
  57. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, "txt", Callback, true);
  58. Assert.Empty(errors);
  59. Assert.True(CheckResult(result, expectedInSubFolder));
  60. }
  61. [Fact]
  62. public void ProcessFolderAndFileTest()
  63. {
  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, expectedInSubFolder));
  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(expectedInFolder, 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(expectedInFolder, 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. [Fact]
  110. public void ProcessFolderRecursiveMultipleExtensionsTest()
  111. {
  112. var result = new List<string>();
  113. void Callback(string value)
  114. {
  115. result.Add(Path.GetFileName(value));
  116. }
  117. var errors = ProcessFiles.ProcessFiles.Process(new[] { testFolder }, new string[] { "txt", "json" }, Callback, true);
  118. Assert.Empty(errors);
  119. Assert.True(CheckResult(result, expectedInSubFolderMultipleExtensions));
  120. }
  121. }
  122. }