DOCXTests.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 AnonymizeAndDeanonymizeCommentsTest()
  70. {
  71. const string testFile = @"testFiles/testComments.docx";
  72. const string expectedAnonymized = @"testFiles/testCommentsAnonymized.docx";
  73. const string expectedDeanonymized = @"testFiles/testCommentsDeanonymized.docx";
  74. using (var test = new DOCX.Docx(testFile))
  75. {
  76. var result = test.AnonymizeComments();
  77. Assert.True(result.status);
  78. }
  79. Assert.True(FileCompare(expectedAnonymized, testFile));
  80. using (var test = new DOCX.Docx(testFile))
  81. {
  82. var result = test.DeanonymizeComments();
  83. Assert.True(result.status);
  84. }
  85. Assert.True(FileCompare(expectedDeanonymized, testFile));
  86. }
  87. }
  88. }