DOCXTests.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.IO;
  2. using Xunit;
  3. namespace DOCXTests
  4. {
  5. public class DocxTests
  6. {
  7. private static bool FileCompare(string file1, string file2)
  8. {
  9. // Borrowed from https://stackoverflow.com/questions/7931304/comparing-two-files-in-c-sharp#7931353
  10. int file1Byte;
  11. int file2Byte;
  12. // Determine if the same file was referenced two times.
  13. if (file1 == file2)
  14. {
  15. // Return true to indicate that the files are the same.
  16. return true;
  17. }
  18. // Open the two files.
  19. var fs1 = new FileStream(file1, FileMode.Open, FileAccess.Read);
  20. var fs2 = new FileStream(file2, FileMode.Open, FileAccess.Read);
  21. // Check the file sizes. If they are not the same, the files
  22. // are not the same.
  23. if (fs1.Length != fs2.Length)
  24. {
  25. // Close the file
  26. fs1.Close();
  27. fs2.Close();
  28. // Return false to indicate files are different
  29. return false;
  30. }
  31. // Read and compare a byte from each file until either a
  32. // non-matching set of bytes is found or until the end of
  33. // file1 is reached.
  34. do
  35. {
  36. // Read one byte from each file.
  37. file1Byte = fs1.ReadByte();
  38. file2Byte = fs2.ReadByte();
  39. }
  40. while (file1Byte == file2Byte && file1Byte != -1);
  41. // Close the files.
  42. fs1.Close();
  43. fs2.Close();
  44. // Return the success of the comparison. "file1byte" is
  45. // equal to "file2byte" at this point only if the files are
  46. // the same.
  47. return file1Byte - file2Byte == 0;
  48. }
  49. [Fact]
  50. public void EnableTrackedChangesTest()
  51. {
  52. const string testFile = @"testFiles/notTracked.docx";
  53. const string expectedFile = @"testFiles/tracked.docx";
  54. using (var test = new DOCX.Docx(testFile))
  55. {
  56. var result = test.EnableTrackedChanges();
  57. Assert.True(result.status);
  58. }
  59. Assert.True(FileCompare(expectedFile, testFile));
  60. // Test for no duplication
  61. using (var test = new DOCX.Docx(expectedFile))
  62. {
  63. var result = test.EnableTrackedChanges();
  64. Assert.True(result.status);
  65. }
  66. Assert.True(FileCompare(expectedFile, testFile));
  67. }
  68. [Fact]
  69. public void AnonymizeAuthorsTest()
  70. {
  71. const string testFile = @"testFiles/testComments.docx";
  72. using (var test = new DOCX.Docx(testFile))
  73. {
  74. var result = test.AnonymizeComments();
  75. Assert.True(result.status);
  76. }
  77. }
  78. }
  79. }