From ec5037c01868fecaec2eba89b975f3e53aded89a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 30 Apr 2016 17:20:53 +0100 Subject: Export from java-mammoth 5f538798b147f0e76 --- Mammoth.Tests/DocumentConverterTests.cs | 232 +++++++++++++++++++-- Mammoth.Tests/Mammoth.Tests.csproj | 102 ++++----- Mammoth.Tests/Properties/AssemblyInfo.cs | 51 ++--- Mammoth.Tests/TestData/empty.docx | Bin 0 -> 3306 bytes Mammoth.Tests/TestData/endnotes.docx | Bin 0 -> 4431 bytes Mammoth.Tests/TestData/external-picture.docx | Bin 0 -> 4120 bytes Mammoth.Tests/TestData/footnote-hyperlink.docx | Bin 0 -> 16271 bytes Mammoth.Tests/TestData/footnotes.docx | Bin 0 -> 15936 bytes .../hyperlinks/word/_rels/document.xml.rels | 10 + .../TestData/hyperlinks/word/document.xml | 18 ++ Mammoth.Tests/TestData/simple-list.docx | Bin 0 -> 13887 bytes Mammoth.Tests/TestData/simple/word/document.xml | 18 ++ Mammoth.Tests/TestData/single-paragraph.docx | Bin 0 -> 12773 bytes Mammoth.Tests/TestData/strikethrough.docx | Bin 0 -> 12652 bytes Mammoth.Tests/TestData/tables.docx | Bin 0 -> 13087 bytes Mammoth.Tests/TestData/text-box.docx | Bin 0 -> 15850 bytes .../TestData/tiny-picture-custom-content-type.docx | Bin 0 -> 10876 bytes Mammoth.Tests/TestData/tiny-picture.docx | Bin 0 -> 13699 bytes Mammoth.Tests/TestData/tiny-picture.png | Bin 0 -> 110 bytes Mammoth.Tests/TestData/underline.docx | Bin 0 -> 12797 bytes Mammoth.Tests/TestData/utf8-bom.docx | Bin 0 -> 2836 bytes Mammoth.Tests/TestDocuments/single-paragraph.docx | Bin 12773 -> 0 bytes .../unrecognised-paragraph-styles.docx | Bin 3400 -> 0 bytes Mammoth.Tests/packages.config | 8 +- 24 files changed, 324 insertions(+), 115 deletions(-) create mode 100644 Mammoth.Tests/TestData/empty.docx create mode 100644 Mammoth.Tests/TestData/endnotes.docx create mode 100644 Mammoth.Tests/TestData/external-picture.docx create mode 100755 Mammoth.Tests/TestData/footnote-hyperlink.docx create mode 100755 Mammoth.Tests/TestData/footnotes.docx create mode 100644 Mammoth.Tests/TestData/hyperlinks/word/_rels/document.xml.rels create mode 100644 Mammoth.Tests/TestData/hyperlinks/word/document.xml create mode 100755 Mammoth.Tests/TestData/simple-list.docx create mode 100644 Mammoth.Tests/TestData/simple/word/document.xml create mode 100755 Mammoth.Tests/TestData/single-paragraph.docx create mode 100755 Mammoth.Tests/TestData/strikethrough.docx create mode 100755 Mammoth.Tests/TestData/tables.docx create mode 100755 Mammoth.Tests/TestData/text-box.docx create mode 100644 Mammoth.Tests/TestData/tiny-picture-custom-content-type.docx create mode 100755 Mammoth.Tests/TestData/tiny-picture.docx create mode 100644 Mammoth.Tests/TestData/tiny-picture.png create mode 100755 Mammoth.Tests/TestData/underline.docx create mode 100644 Mammoth.Tests/TestData/utf8-bom.docx delete mode 100644 Mammoth.Tests/TestDocuments/single-paragraph.docx delete mode 100644 Mammoth.Tests/TestDocuments/unrecognised-paragraph-styles.docx (limited to 'Mammoth.Tests') diff --git a/Mammoth.Tests/DocumentConverterTests.cs b/Mammoth.Tests/DocumentConverterTests.cs index 37cec23..92a3adf 100644 --- a/Mammoth.Tests/DocumentConverterTests.cs +++ b/Mammoth.Tests/DocumentConverterTests.cs @@ -1,24 +1,214 @@ -using NUnit.Framework; - -namespace Mammoth.Tests -{ - public class DocumentConverterTests - { - [Test] - public void SingleWordParagraphIsConvertedToSingleHtmlParagraph() - { - var documentConverter = new DocumentConverter(); - var result = documentConverter.ConvertToHtml("TestDocuments/single-paragraph.docx"); - Assert.AreEqual("

Walking on imported air

", result.Value); - Assert.IsEmpty(result.Messages); - } - - [Test] - public void UnrecognisedStylesAreStatedInMessages() - { - var documentConverter = new DocumentConverter(); - var result = documentConverter.ConvertToHtml("TestDocuments/unrecognised-paragraph-styles.docx"); - Assert.AreEqual("Unrecognised paragraph style: 'Funky' (Style ID: style20)", result.Messages[0].Value); +using Xunit; +using System.IO; +using Xunit.Sdk; +using System; +using System.Linq; + +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"), + "

"); + } + + [Fact] + public void WarnIfDocumentHasImagesStoredOutsideOfDocumentWhenPathOfDocumentIsUnknown() { + using (var file = File.OpenRead(TestFilePath("external-picture.docx"))) { + var result = new DocumentConverter().ConvertToHtml(file); + Assert.Equal("", result.Value); + Assert.Equal(new[]{"could not open external image 'tiny-picture.png': path of document is unknown, but is required for relative URI"}, result.Warnings); + } + } + + [Fact] + public void WarnIfImagesStoredOutsideOfDocumentAreNotFound() { + var tempDirectory = Path.Combine(Path.GetTempPath(), "mammoth-" + Guid.NewGuid()); + Directory.CreateDirectory(tempDirectory); + try { + var documentPath = Path.Combine(tempDirectory, "external-picture.docx"); + File.Copy(TestFilePath("external-picture.docx"), documentPath); + var result = new DocumentConverter().ConvertToHtml(documentPath); + Assert.Equal("", result.Value); + Assert.StartsWith("could not open external image 'tiny-picture.png':", result.Warnings.Single()); + } finally { + Directory.Delete(tempDirectory, recursive: true); + } + } + + [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 RelationshipsAreReadForEachFileContainingBodyXml() { + AssertSuccessfulConversion( + ConvertToHtml("footnote-hyperlink.docx", mammoth => mammoth.IdPrefix("doc-42-")), + + "

[1]

" + + "
  1. Example ↑

"); + } + + [Fact] + public void TextBoxesAreRead() { + AssertSuccessfulConversion( + ConvertToHtml("text-box.docx"), + "

Datum plane

"); + } + + [Fact] + public void CanUseCustomStyleMap() { + AssertSuccessfulConversion( + ConvertToHtml("underline.docx", mammoth => mammoth.AddStyleMap("u => em")), + "

The Sunset Tree

"); + } + + [Fact] + public void MostRecentlyAddedStyleMapTakesPrecedence() { + AssertSuccessfulConversion( + ConvertToHtml("underline.docx", mammoth => mammoth.AddStyleMap("u => em").AddStyleMap("u => strong")), + "

The Sunset Tree

"); + } + + [Fact] + public void RulesFromPreviouslyAddedStyleMapsStillTakeEffectIfNotOverridden() { + AssertSuccessfulConversion( + ConvertToHtml("underline.docx", mammoth => mammoth.AddStyleMap("u => em").AddStyleMap("strike => del")), + "

The Sunset Tree

"); + } + + [Fact] + public void ErrorIsRaisedIfStyleMapCannotBeParsed() { + var exception = Assert.ThrowsAny(() => + new DocumentConverter().AddStyleMap("p =>\np[style-name=] =>")); + Assert.Equal( + "error reading style map at line 2, character 14: expected token of type _STRING but was of type _CLOSE_SQUARE_BRACKET\n\n" + + "p[style-name=] =>\n" + + " ^", + exception.Message); + } + + [Fact] + public void CanDisableDefaultStyleMap() { + var result = ConvertToHtml("simple-list.docx", mammoth => mammoth.DisableDefaultStyleMap()); + Assert.Equal(new[]{"Unrecognised paragraph style: List Paragraph (Style ID: ListParagraph)"}, result.Warnings); + Assert.Equal("

Apple

Banana

", result.Value); + } + + [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); } } } + diff --git a/Mammoth.Tests/Mammoth.Tests.csproj b/Mammoth.Tests/Mammoth.Tests.csproj index 212db46..d561dde 100644 --- a/Mammoth.Tests/Mammoth.Tests.csproj +++ b/Mammoth.Tests/Mammoth.Tests.csproj @@ -1,99 +1,77 @@ - - - + + Debug AnyCPU - {ED442448-256E-4574-BB1C-3303EF29B262} + {E9C3EA8B-FF3B-4E3E-8137-1510465200B5} Library - Properties Mammoth.Tests Mammoth.Tests - v4.0 - 512 - ..\ - true + v4.5 true full false - bin\Debug\ - DEBUG;TRACE + bin\Debug + DEBUG; prompt 4 + false - pdbonly + full true - bin\Release\ - TRACE + bin\Release prompt 4 + false - - ..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll - False - - - ..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.interfaces.dll - False + + + ..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll - - ..\packages\NUnit.2.6.4\lib\nunit.framework.dll + + ..\packages\xunit.extensibility.core.2.1.0\lib\portable-net45+win8+wp8+wpa81\xunit.core.dll - - ..\packages\NUnitTestAdapter.2.0.0\lib\nunit.util.dll - False + + ..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll - - ..\packages\NUnitTestAdapter.2.0.0\lib\NUnit.VisualStudio.TestAdapter.dll - False + + ..\packages\xunit.assert.2.1.0\lib\portable-net45+win8+wp8+wpa81\xunit.assert.dll - - - - - - - - + + + - - PreserveNewest - - - PreserveNewest - + + + + + + + + + + + + + + + + + - {7398c88c-cb94-41c3-bef9-755bcf81f373} + {51C5CBD6-8809-4D5E-BABE-1787A16F5ED8} Mammoth - - - - - - - - This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file diff --git a/Mammoth.Tests/Properties/AssemblyInfo.cs b/Mammoth.Tests/Properties/AssemblyInfo.cs index c3c901e..79596a3 100644 --- a/Mammoth.Tests/Properties/AssemblyInfo.cs +++ b/Mammoth.Tests/Properties/AssemblyInfo.cs @@ -1,36 +1,27 @@ using System.Reflection; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("Mammoth.Tests")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Microsoft")] -[assembly: AssemblyProduct("Mammoth.Tests")] -[assembly: AssemblyCopyright("Copyright © Microsoft 2015")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] +[assembly: AssemblyTitle ("Mammoth.Tests")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("Michael Williamson")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("ba4940f2-3760-4a66-b2a7-861164d661cc")] +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Mammoth.Tests/TestData/empty.docx b/Mammoth.Tests/TestData/empty.docx new file mode 100644 index 0000000..e10b869 Binary files /dev/null and b/Mammoth.Tests/TestData/empty.docx differ diff --git a/Mammoth.Tests/TestData/endnotes.docx b/Mammoth.Tests/TestData/endnotes.docx new file mode 100644 index 0000000..1ae4f93 Binary files /dev/null and b/Mammoth.Tests/TestData/endnotes.docx differ diff --git a/Mammoth.Tests/TestData/external-picture.docx b/Mammoth.Tests/TestData/external-picture.docx new file mode 100644 index 0000000..3475626 Binary files /dev/null and b/Mammoth.Tests/TestData/external-picture.docx differ diff --git a/Mammoth.Tests/TestData/footnote-hyperlink.docx b/Mammoth.Tests/TestData/footnote-hyperlink.docx new file mode 100755 index 0000000..208f3dc Binary files /dev/null and b/Mammoth.Tests/TestData/footnote-hyperlink.docx differ diff --git a/Mammoth.Tests/TestData/footnotes.docx b/Mammoth.Tests/TestData/footnotes.docx new file mode 100755 index 0000000..3228e74 Binary files /dev/null and b/Mammoth.Tests/TestData/footnotes.docx differ diff --git a/Mammoth.Tests/TestData/hyperlinks/word/_rels/document.xml.rels b/Mammoth.Tests/TestData/hyperlinks/word/_rels/document.xml.rels new file mode 100644 index 0000000..baa4102 --- /dev/null +++ b/Mammoth.Tests/TestData/hyperlinks/word/_rels/document.xml.rels @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Mammoth.Tests/TestData/hyperlinks/word/document.xml b/Mammoth.Tests/TestData/hyperlinks/word/document.xml new file mode 100644 index 0000000..f1ce575 --- /dev/null +++ b/Mammoth.Tests/TestData/hyperlinks/word/document.xml @@ -0,0 +1,18 @@ + + + + + + + coconuts + + + + + + + + + + + diff --git a/Mammoth.Tests/TestData/simple-list.docx b/Mammoth.Tests/TestData/simple-list.docx new file mode 100755 index 0000000..c2cd5ae Binary files /dev/null and b/Mammoth.Tests/TestData/simple-list.docx differ diff --git a/Mammoth.Tests/TestData/simple/word/document.xml b/Mammoth.Tests/TestData/simple/word/document.xml new file mode 100644 index 0000000..1723398 --- /dev/null +++ b/Mammoth.Tests/TestData/simple/word/document.xml @@ -0,0 +1,18 @@ + + + + + + Hello. + + + + + + + + + + + + diff --git a/Mammoth.Tests/TestData/single-paragraph.docx b/Mammoth.Tests/TestData/single-paragraph.docx new file mode 100755 index 0000000..09caf9a Binary files /dev/null and b/Mammoth.Tests/TestData/single-paragraph.docx differ diff --git a/Mammoth.Tests/TestData/strikethrough.docx b/Mammoth.Tests/TestData/strikethrough.docx new file mode 100755 index 0000000..0d2b713 Binary files /dev/null and b/Mammoth.Tests/TestData/strikethrough.docx differ diff --git a/Mammoth.Tests/TestData/tables.docx b/Mammoth.Tests/TestData/tables.docx new file mode 100755 index 0000000..86009a7 Binary files /dev/null and b/Mammoth.Tests/TestData/tables.docx differ diff --git a/Mammoth.Tests/TestData/text-box.docx b/Mammoth.Tests/TestData/text-box.docx new file mode 100755 index 0000000..2d53703 Binary files /dev/null and b/Mammoth.Tests/TestData/text-box.docx differ diff --git a/Mammoth.Tests/TestData/tiny-picture-custom-content-type.docx b/Mammoth.Tests/TestData/tiny-picture-custom-content-type.docx new file mode 100644 index 0000000..3638f88 Binary files /dev/null and b/Mammoth.Tests/TestData/tiny-picture-custom-content-type.docx differ diff --git a/Mammoth.Tests/TestData/tiny-picture.docx b/Mammoth.Tests/TestData/tiny-picture.docx new file mode 100755 index 0000000..d45bb9e Binary files /dev/null and b/Mammoth.Tests/TestData/tiny-picture.docx differ diff --git a/Mammoth.Tests/TestData/tiny-picture.png b/Mammoth.Tests/TestData/tiny-picture.png new file mode 100644 index 0000000..74cb4ba Binary files /dev/null and b/Mammoth.Tests/TestData/tiny-picture.png differ diff --git a/Mammoth.Tests/TestData/underline.docx b/Mammoth.Tests/TestData/underline.docx new file mode 100755 index 0000000..36604fc Binary files /dev/null and b/Mammoth.Tests/TestData/underline.docx differ diff --git a/Mammoth.Tests/TestData/utf8-bom.docx b/Mammoth.Tests/TestData/utf8-bom.docx new file mode 100644 index 0000000..3881238 Binary files /dev/null and b/Mammoth.Tests/TestData/utf8-bom.docx differ diff --git a/Mammoth.Tests/TestDocuments/single-paragraph.docx b/Mammoth.Tests/TestDocuments/single-paragraph.docx deleted file mode 100644 index 09caf9a..0000000 Binary files a/Mammoth.Tests/TestDocuments/single-paragraph.docx and /dev/null differ diff --git a/Mammoth.Tests/TestDocuments/unrecognised-paragraph-styles.docx b/Mammoth.Tests/TestDocuments/unrecognised-paragraph-styles.docx deleted file mode 100644 index ad4064f..0000000 Binary files a/Mammoth.Tests/TestDocuments/unrecognised-paragraph-styles.docx and /dev/null differ diff --git a/Mammoth.Tests/packages.config b/Mammoth.Tests/packages.config index af221c7..69a4ec4 100644 --- a/Mammoth.Tests/packages.config +++ b/Mammoth.Tests/packages.config @@ -1,5 +1,9 @@  - - + + + + + + \ No newline at end of file -- cgit v1.2.3