ProcessFiles_UnitTests.cs 3.3 KB

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