ProcessFilesTests.cs 3.6 KB

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