ProcessFiles_UnitTests.cs 3.0 KB

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