DOCXTests.cs 2.6 KB

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