using Xunit; using System.IO; using Xunit.Sdk; using System; namespace Mammoth.Tests { public class DocumentConverterTests { [Fact] public void DocxContainingOneParagraphIsConvertedToSingleParagraphElement() { AssertSuccessfulConversion( ConvertToHtml("single-paragraph.docx"), "

Walking on imported air

"); } [Fact] public void CanReadFilesWithUtf8Bom() { AssertSuccessfulConversion( ConvertToHtml("utf8-bom.docx"), "

This XML has a byte order mark.

"); } [Fact] public void EmptyParagraphsAreIgnoredByDefault() { AssertSuccessfulConversion( ConvertToHtml("empty.docx"), ""); } [Fact] public void EmptyParagraphsArePreservedIfIgnoreEmptyParagraphsIsFalse() { AssertSuccessfulConversion( ConvertToHtml("empty.docx", converter => converter.PreserveEmptyParagraphs()), "

"); } [Fact] public void SimpleListIsConvertedToListElements() { AssertSuccessfulConversion( ConvertToHtml("simple-list.docx"), ""); } [Fact] public void WordTablesAreConvertedToHtmlTables() { AssertSuccessfulConversion( ConvertToHtml("tables.docx"), "

Above

" + "" + "" + "" + "

Top left

Top right

Bottom left

Bottom right

" + "

Below

"); } [Fact] public void InlineImagesAreIncludedInOutput() { AssertSuccessfulConversion( ConvertToHtml("tiny-picture.docx"), "

"); } [Fact] public void ImagesStoredOutsideOfDocumentAreIncludedInOutput() { AssertSuccessfulConversion( ConvertToHtml("external-picture.docx"), "

"); } // TODO: image warnings [Fact] public void ContentTypesAreRead() { AssertSuccessfulConversion( ConvertToHtml("tiny-picture-custom-content-type.docx"), "

"); } [Fact] public void FootnotesAreAppendedToText() { AssertSuccessfulConversion( ConvertToHtml("footnotes.docx", mammoth => mammoth.IdPrefix("doc-42-")), "

Ouch" + "[1]." + "[2]

" + "
  1. A tachyon walks into a bar. ↑

  2. " + "
  3. Fin. ↑

"); } [Fact] public void EndNotesAreAppendedToText() { AssertSuccessfulConversion( ConvertToHtml("endnotes.docx", mammoth => mammoth.IdPrefix("doc-42-")), "

Ouch" + "[1]." + "[2]

" + "
  1. A tachyon walks into a bar. ↑

  2. " + "
  3. Fin. ↑

"); } [Fact] public void CanExtractRawTextFromFile() { AssertSuccessfulConversion( new DocumentConverter().ExtractRawText(TestFilePath("simple-list.docx")), "Apple\n\nBanana\n\n"); } [Fact] public void CanExtractRawTextFromStream() { using (var file = File.OpenRead(TestFilePath("simple-list.docx"))) { AssertSuccessfulConversion( new DocumentConverter().ExtractRawText(file), "Apple\n\nBanana\n\n"); } } private void AssertSuccessfulConversion(IResult result, string expectedValue) { if (result.Warnings.Count > 0) { throw new XunitException("Unexpected warnings: " + string.Join(", ", result.Warnings)); } Assert.Equal(expectedValue, result.Value); } private IResult ConvertToHtml(string name) { return ConvertToHtml(name, converter => converter); } private IResult ConvertToHtml(string name, Func configure) { return configure(new DocumentConverter()).ConvertToHtml(TestFilePath(name)); } private string TestFilePath(string name) { return Path.Combine("../../TestData", name); } } }