ProcessFiles_UnitTests.cs 3.2 KB

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