ProcessFiles_UnitTests.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using NSubstitute;
  2. using FileOperations.Interfaces;
  3. using Xunit;
  4. namespace ProcessFilesTests
  5. {
  6. public class ProcessFiles_UnitTests
  7. {
  8. [Fact]
  9. public void ProcessFileNotExistTest()
  10. {
  11. var result = string.Empty;
  12. void TestAction(string value)
  13. {
  14. result = value;
  15. }
  16. var fakeFileSystem = Substitute.For<IFileSystem>();
  17. fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(false);
  18. var test = new FileOperations.ProcessFiles(fakeFileSystem);
  19. var errors = test.Process(["./imaginaryFolder/imaginaryTest.txt"], "txt", TestAction);
  20. Assert.NotEmpty(errors);
  21. Assert.Empty(result);
  22. fakeFileSystem.File.Received().Exists(Arg.Any<string>());
  23. }
  24. [Fact]
  25. public void ProcessFileNoExtensionTest()
  26. {
  27. var result = string.Empty;
  28. void TestAction(string value)
  29. {
  30. result = value;
  31. }
  32. var fakeFileSystem = Substitute.For<IFileSystem>();
  33. fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(true);
  34. fakeFileSystem.Path.GetExtension(Arg.Any<string>()).Returns(string.Empty);
  35. var test = new FileOperations.ProcessFiles(fakeFileSystem);
  36. var errors = test.Process(["imaginaryNoExtension"], "abc", TestAction);
  37. Assert.NotEmpty(errors);
  38. Assert.Empty(result);
  39. fakeFileSystem.File.Received().Exists(Arg.Any<string>());
  40. fakeFileSystem.Path.Received().GetExtension(Arg.Any<string>());
  41. }
  42. [Fact]
  43. public void ProcessFileNotMatchExtensionTest()
  44. {
  45. var result = string.Empty;
  46. void TestAction(string value)
  47. {
  48. result = value;
  49. }
  50. var fakeFileSystem = Substitute.For<IFileSystem>();
  51. fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(true);
  52. fakeFileSystem.Path.GetExtension(Arg.Any<string>()).Returns("def");
  53. var test = new FileOperations.ProcessFiles(fakeFileSystem);
  54. var errors = test.Process(["imaginaryFile"], "abc", TestAction);
  55. Assert.NotEmpty(errors);
  56. Assert.Empty(result);
  57. fakeFileSystem.File.Received().Exists(Arg.Any<string>());
  58. fakeFileSystem.Path.Received().GetExtension(Arg.Any<string>());
  59. }
  60. [Fact]
  61. public void ProcessFileTest()
  62. {
  63. var result = string.Empty;
  64. void TestAction(string value)
  65. {
  66. result = value;
  67. }
  68. var fakeFileSystem = Substitute.For<IFileSystem>();
  69. fakeFileSystem.File.Exists(Arg.Any<string>()).Returns(true);
  70. fakeFileSystem.Path.GetExtension(Arg.Any<string>()).Returns("txt");
  71. const string expectedValue = "imaginary.txt";
  72. var test = new FileOperations.ProcessFiles(fakeFileSystem);
  73. var errors = test.Process([expectedValue], "txt", TestAction);
  74. Assert.Empty(errors);
  75. Assert.Equal(expectedValue, result);
  76. fakeFileSystem.File.Received().Exists(Arg.Any<string>());
  77. fakeFileSystem.Path.Received().GetExtension(Arg.Any<string>());
  78. }
  79. }
  80. }