aboutsummaryrefslogtreecommitdiff
path: root/dotnet/Mammoth.Tests/DocumentConverterTests.cs
blob: 8526b04a745ade80864054dadd1ec00467f1f554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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"),
				"<p>Walking on imported air</p>");
		}

		[Fact]
		public void CanReadFilesWithUtf8Bom() {
			AssertSuccessfulConversion(
				ConvertToHtml("utf8-bom.docx"),
				"<p>This XML has a byte order mark.</p>");
		}

		[Fact]
		public void EmptyParagraphsAreIgnoredByDefault() {
			AssertSuccessfulConversion(
				ConvertToHtml("empty.docx"),
				"");
		}

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

        [Fact]
        public void SimpleListIsConvertedToListElements()  {
            AssertSuccessfulConversion(
                ConvertToHtml("simple-list.docx"),
                "<ul><li>Apple</li><li>Banana</li></ul>");
        }

		[Fact]
		public void WordTablesAreConvertedToHtmlTables() {
			AssertSuccessfulConversion(
				ConvertToHtml("tables.docx"),
				"<p>Above</p>" +
				"<table>" +
				"<tr><td><p>Top left</p></td><td><p>Top right</p></td></tr>" +
				"<tr><td><p>Bottom left</p></td><td><p>Bottom right</p></td></tr>" +
				"</table>" +
				"<p>Below</p>");
		}

		[Fact]
		public void InlineImagesAreIncludedInOutput() {
			AssertSuccessfulConversion(
				ConvertToHtml("tiny-picture.docx"),
				"<p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>");
		}

		[Fact]
		public void ImagesStoredOutsideOfDocumentAreIncludedInOutput() {
			AssertSuccessfulConversion(
				ConvertToHtml("external-picture.docx"),
				"<p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>");
        }

        [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"),
                "<p><img src=\"data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>");
        }

        [Fact]
        public void FootnotesAreAppendedToText() {
            AssertSuccessfulConversion(
                ConvertToHtml("footnotes.docx", mammoth => mammoth.IdPrefix("doc-42-")),
                    "<p>Ouch" +
                    "<sup><a href=\"#doc-42-footnote-1\" id=\"doc-42-footnote-ref-1\">[1]</a></sup>." +
                    "<sup><a href=\"#doc-42-footnote-2\" id=\"doc-42-footnote-ref-2\">[2]</a></sup></p>" +
                    "<ol><li id=\"doc-42-footnote-1\"><p> A tachyon walks into a bar. <a href=\"#doc-42-footnote-ref-1\">↑</a></p></li>" +
                    "<li id=\"doc-42-footnote-2\"><p> Fin. <a href=\"#doc-42-footnote-ref-2\">↑</a></p></li></ol>");
        }

        [Fact]
        public void EndNotesAreAppendedToText() {
            AssertSuccessfulConversion(
                ConvertToHtml("endnotes.docx", mammoth => mammoth.IdPrefix("doc-42-")),
                    "<p>Ouch" +
                    "<sup><a href=\"#doc-42-endnote-2\" id=\"doc-42-endnote-ref-2\">[1]</a></sup>." +
                    "<sup><a href=\"#doc-42-endnote-3\" id=\"doc-42-endnote-ref-3\">[2]</a></sup></p>" +
                    "<ol><li id=\"doc-42-endnote-2\"><p> A tachyon walks into a bar. <a href=\"#doc-42-endnote-ref-2\">↑</a></p></li>" +
                    "<li id=\"doc-42-endnote-3\"><p> Fin. <a href=\"#doc-42-endnote-ref-3\">↑</a></p></li></ol>");
        }

        [Fact]
        public void RelationshipsAreReadForEachFileContainingBodyXml() {
            AssertSuccessfulConversion(
                ConvertToHtml("footnote-hyperlink.docx", mammoth => mammoth.IdPrefix("doc-42-")),

                    "<p><sup><a href=\"#doc-42-footnote-1\" id=\"doc-42-footnote-ref-1\">[1]</a></sup></p>" +
                    "<ol><li id=\"doc-42-footnote-1\"><p> <a href=\"http://www.example.com\">Example</a> <a href=\"#doc-42-footnote-ref-1\">↑</a></p></li></ol>");
        }

        [Fact]
        public void TextBoxesAreRead() {
            AssertSuccessfulConversion(
                ConvertToHtml("text-box.docx"),
                "<p>Datum plane</p>");
        }

        [Fact]
        public void CanUseCustomStyleMap() {
            AssertSuccessfulConversion(
                ConvertToHtml("underline.docx", mammoth => mammoth.AddStyleMap("u => em")),
                "<p><strong>The <em>Sunset</em> Tree</strong></p>");
        }

        [Fact]
        public void MostRecentlyAddedStyleMapTakesPrecedence() {
            AssertSuccessfulConversion(
                ConvertToHtml("underline.docx", mammoth => mammoth.AddStyleMap("u => em").AddStyleMap("u => strong")),
                "<p><strong>The <strong>Sunset</strong> Tree</strong></p>");
        }

        [Fact]
        public void RulesFromPreviouslyAddedStyleMapsStillTakeEffectIfNotOverriden() {
            AssertSuccessfulConversion(
                ConvertToHtml("underline.docx", mammoth => mammoth.AddStyleMap("u => em").AddStyleMap("strike => del")),
                "<p><strong>The <em>Sunset</em> Tree</strong></p>");
        }

        [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("<p>Apple</p><p>Banana</p>", 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<string> result, string expectedValue) {
			if (result.Warnings.Count > 0) {
				throw new XunitException("Unexpected warnings: " + string.Join(", ", result.Warnings));
			}
			Assert.Equal(expectedValue, result.Value);
		}

		private IResult<string> ConvertToHtml(string name) {
			return ConvertToHtml(name, converter => converter);
		}

		private IResult<string> ConvertToHtml(string name, Func<DocumentConverter, DocumentConverter> configure) {
			return configure(new DocumentConverter()).ConvertToHtml(TestFilePath(name));
		}

		private string TestFilePath(string name) {
			return Path.Combine("../../TestData", name);
		}
	}
}