-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy pathDocumentAssemblerTests.cs
245 lines (221 loc) · 14.3 KB
/
DocumentAssemblerTests.cs
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using OpenXmlPowerTools;
using Xunit;
#if !ELIDE_XUNIT_TESTS
namespace OxPt
{
public class DaTests
{
[Theory]
[InlineData("DA001-TemplateDocument.docx", "DA-Data.xml", false)]
[InlineData("DA002-TemplateDocument.docx", "DA-DataNotHighValueCust.xml", false)]
[InlineData("DA003-Select-XPathFindsNoData.docx", "DA-Data.xml", true)]
[InlineData("DA004-Select-XPathFindsNoDataOptional.docx", "DA-Data.xml", false)]
[InlineData("DA005-SelectRowData-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA006-SelectTestValue-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA007-SelectRepeatingData-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA008-TableElementWithNoTable.docx", "DA-Data.xml", true)]
[InlineData("DA009-InvalidXPath.docx", "DA-Data.xml", true)]
[InlineData("DA010-InvalidXml.docx", "DA-Data.xml", true)]
[InlineData("DA011-SchemaError.docx", "DA-Data.xml", true)]
[InlineData("DA012-OtherMarkupTypes.docx", "DA-Data.xml", true)]
[InlineData("DA013-Runs.docx", "DA-Data.xml", false)]
[InlineData("DA014-TwoRuns-NoValuesSelected.docx", "DA-Data.xml", true)]
[InlineData("DA015-TwoRunsXmlExceptionInFirst.docx", "DA-Data.xml", true)]
[InlineData("DA016-TwoRunsSchemaErrorInSecond.docx", "DA-Data.xml", true)]
[InlineData("DA017-FiveRuns.docx", "DA-Data.xml", true)]
[InlineData("DA018-SmartQuotes.docx", "DA-Data.xml", false)]
[InlineData("DA019-RunIsEntireParagraph.docx", "DA-Data.xml", false)]
[InlineData("DA020-TwoRunsAndNoOtherContent.docx", "DA-Data.xml", true)]
[InlineData("DA021-NestedRepeat.docx", "DA-DataNestedRepeat.xml", false)]
[InlineData("DA022-InvalidXPath.docx", "DA-Data.xml", true)]
[InlineData("DA023-RepeatWOEndRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA026-InvalidRootXmlElement.docx", "DA-Data.xml", true)]
[InlineData("DA027-XPathErrorInPara.docx", "DA-Data.xml", true)]
[InlineData("DA028-NoPrototypeRow.docx", "DA-Data.xml", true)]
[InlineData("DA029-NoDataForCell.docx", "DA-Data.xml", true)]
[InlineData("DA030-TooMuchDataForCell.docx", "DA-TooMuchDataForCell.xml", true)]
[InlineData("DA031-CellDataInAttributes.docx", "DA-CellDataInAttributes.xml", true)]
[InlineData("DA032-TooMuchDataForConditional.docx", "DA-TooMuchDataForConditional.xml", true)]
[InlineData("DA033-ConditionalOnAttribute.docx", "DA-ConditionalOnAttribute.xml", false)]
[InlineData("DA034-HeaderFooter.docx", "DA-Data.xml", false)]
[InlineData("DA035-SchemaErrorInRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA036-SchemaErrorInConditional.docx", "DA-Data.xml", true)]
[InlineData("DA100-TemplateDocument.docx", "DA-Data.xml", false)]
[InlineData("DA101-TemplateDocument.docx", "DA-Data.xml", true)]
[InlineData("DA102-TemplateDocument.docx", "DA-Data.xml", true)]
[InlineData("DA201-TemplateDocument.docx", "DA-Data.xml", false)]
[InlineData("DA202-TemplateDocument.docx", "DA-DataNotHighValueCust.xml", false)]
[InlineData("DA203-Select-XPathFindsNoData.docx", "DA-Data.xml", true)]
[InlineData("DA204-Select-XPathFindsNoDataOptional.docx", "DA-Data.xml", false)]
[InlineData("DA205-SelectRowData-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA206-SelectTestValue-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA207-SelectRepeatingData-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA209-InvalidXPath.docx", "DA-Data.xml", true)]
[InlineData("DA210-InvalidXml.docx", "DA-Data.xml", true)]
[InlineData("DA211-SchemaError.docx", "DA-Data.xml", true)]
[InlineData("DA212-OtherMarkupTypes.docx", "DA-Data.xml", true)]
[InlineData("DA213-Runs.docx", "DA-Data.xml", false)]
[InlineData("DA214-TwoRuns-NoValuesSelected.docx", "DA-Data.xml", true)]
[InlineData("DA215-TwoRunsXmlExceptionInFirst.docx", "DA-Data.xml", true)]
[InlineData("DA216-TwoRunsSchemaErrorInSecond.docx", "DA-Data.xml", true)]
[InlineData("DA217-FiveRuns.docx", "DA-Data.xml", true)]
[InlineData("DA218-SmartQuotes.docx", "DA-Data.xml", false)]
[InlineData("DA219-RunIsEntireParagraph.docx", "DA-Data.xml", false)]
[InlineData("DA220-TwoRunsAndNoOtherContent.docx", "DA-Data.xml", true)]
[InlineData("DA221-NestedRepeat.docx", "DA-DataNestedRepeat.xml", false)]
[InlineData("DA222-InvalidXPath.docx", "DA-Data.xml", true)]
[InlineData("DA223-RepeatWOEndRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA226-InvalidRootXmlElement.docx", "DA-Data.xml", true)]
[InlineData("DA227-XPathErrorInPara.docx", "DA-Data.xml", true)]
[InlineData("DA228-NoPrototypeRow.docx", "DA-Data.xml", true)]
[InlineData("DA229-NoDataForCell.docx", "DA-Data.xml", true)]
[InlineData("DA230-TooMuchDataForCell.docx", "DA-TooMuchDataForCell.xml", true)]
[InlineData("DA231-CellDataInAttributes.docx", "DA-CellDataInAttributes.xml", true)]
[InlineData("DA232-TooMuchDataForConditional.docx", "DA-TooMuchDataForConditional.xml", true)]
[InlineData("DA233-ConditionalOnAttribute.docx", "DA-ConditionalOnAttribute.xml", false)]
[InlineData("DA234-HeaderFooter.docx", "DA-Data.xml", false)]
[InlineData("DA235-Crashes.docx", "DA-Content-List.xml", false)]
[InlineData("DA236-Page-Num-in-Footer.docx", "DA-Content-List.xml", false)]
[InlineData("DA237-SchemaErrorInRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA238-SchemaErrorInConditional.docx", "DA-Data.xml", true)]
[InlineData("DA239-RunLevelCC-Repeat.docx", "DA-Data.xml", false)]
[InlineData("DA250-ConditionalWithRichXPath.docx", "DA250-Address.xml", false)]
[InlineData("DA251-EnhancedTables.docx", "DA-Data.xml", false)]
[InlineData("DA252-Table-With-Sum.docx", "DA-Data.xml", false)]
[InlineData("DA253-Table-With-Sum-Run-Level-CC.docx", "DA-Data.xml", false)]
[InlineData("DA254-Table-With-XPath-Sum.docx", "DA-Data.xml", false)]
[InlineData("DA255-Table-With-XPath-Sum-Run-Level-CC.docx", "DA-Data.xml", false)]
[InlineData("DA256-NoInvalidDocOnErrorInRun.docx", "DA-Data.xml", true)]
[InlineData("DA257-OptionalRepeat.docx", "DA-Data.xml", false)]
[InlineData("DA258-ContentAcceptsCharsAsXPathResult.docx", "DA-Data.xml", false)]
[InlineData("DA259-MultiLineContents.docx", "DA-Data.xml", false)]
[InlineData("DA260-RunLevelRepeat.docx", "DA-Data.xml", false)]
[InlineData("DA261-RunLevelConditional.docx", "DA-Data.xml", false)]
[InlineData("DA262-ConditionalNotMatch.docx", "DA-Data.xml", false)]
[InlineData("DA263-ConditionalNotMatch.docx", "DA-DataSmallCustomer.xml", false)]
[InlineData("DA264-InvalidRunLevelRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA265-RunLevelRepeatWithWhiteSpaceBefore.docx", "DA-Data.xml", false)]
[InlineData("DA266-RunLevelRepeat-NoData.docx", "DA-Data.xml", true)]
[InlineData("DA300-TableWithContentInCells.docx", "DA-Data.xml", false)]
public void DA101(string name, string data, bool err)
{
DirectoryInfo sourceDir = new DirectoryInfo("../../../../TestFiles/");
FileInfo templateDocx = new FileInfo(Path.Combine(sourceDir.FullName, name));
FileInfo dataFile = new FileInfo(Path.Combine(sourceDir.FullName, data));
WmlDocument wmlTemplate = new WmlDocument(templateDocx.FullName);
XElement xmldata = XElement.Load(dataFile.FullName);
bool returnedTemplateError;
WmlDocument afterAssembling = DocumentAssembler.AssembleDocument(wmlTemplate, xmldata, out returnedTemplateError);
var assembledDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, templateDocx.Name.Replace(".docx", "-processed-by-DocumentAssembler.docx")));
afterAssembling.SaveAs(assembledDocx.FullName);
using (MemoryStream ms = new MemoryStream())
{
ms.Write(afterAssembling.DocumentByteArray, 0, afterAssembling.DocumentByteArray.Length);
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true))
{
OpenXmlValidator v = new OpenXmlValidator();
var valErrors = v.Validate(wDoc).Where(ve => !s_ExpectedErrors.Contains(ve.Description));
#if false
StringBuilder sb = new StringBuilder();
foreach (var item in valErrors.Select(r => r.Description).OrderBy(t => t).Distinct())
{
sb.Append(item).Append(Environment.NewLine);
}
string z = sb.ToString();
Console.WriteLine(z);
#endif
Assert.Empty(valErrors);
}
}
Assert.Equal(err, returnedTemplateError);
}
[Theory]
[InlineData("DA259-MultiLineContents.docx", "DA-Data.xml", false)]
public void DA259(string name, string data, bool err)
{
DA101(name, data, err);
var assembledDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, name.Replace(".docx", "-processed-by-DocumentAssembler.docx")));
WmlDocument afterAssembling = new WmlDocument(assembledDocx.FullName);
int brCount = afterAssembling.MainDocumentPart
.Element(W.body)
.Elements(W.p).ElementAt(1)
.Elements(W.r)
.Elements(W.br).Count();
Assert.Equal(4, brCount);
}
[Theory]
[InlineData("DA024-TrackedRevisions.docx", "DA-Data.xml")]
public void DA102_Throws(string name, string data)
{
DirectoryInfo sourceDir = new DirectoryInfo("../../../../TestFiles/");
FileInfo templateDocx = new FileInfo(Path.Combine(sourceDir.FullName, name));
FileInfo dataFile = new FileInfo(Path.Combine(sourceDir.FullName, data));
WmlDocument wmlTemplate = new WmlDocument(templateDocx.FullName);
XElement xmldata = XElement.Load(dataFile.FullName);
bool returnedTemplateError;
WmlDocument afterAssembling;
Assert.Throws<OpenXmlPowerToolsException>(() =>
{
afterAssembling = DocumentAssembler.AssembleDocument(wmlTemplate, xmldata, out returnedTemplateError);
});
}
[Theory]
[InlineData("DA025-TemplateDocument.docx", "DA-Data.xml", false)]
public void DA103_UseXmlDocument(string name, string data, bool err)
{
DirectoryInfo sourceDir = new DirectoryInfo("../../../../TestFiles/");
FileInfo templateDocx = new FileInfo(Path.Combine(sourceDir.FullName, name));
FileInfo dataFile = new FileInfo(Path.Combine(sourceDir.FullName, data));
WmlDocument wmlTemplate = new WmlDocument(templateDocx.FullName);
XmlDocument xmldata = new XmlDocument();
xmldata.Load(dataFile.FullName);
bool returnedTemplateError;
WmlDocument afterAssembling = DocumentAssembler.AssembleDocument(wmlTemplate, xmldata, out returnedTemplateError);
var assembledDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, templateDocx.Name.Replace(".docx", "-processed-by-DocumentAssembler.docx")));
afterAssembling.SaveAs(assembledDocx.FullName);
using (MemoryStream ms = new MemoryStream())
{
ms.Write(afterAssembling.DocumentByteArray, 0, afterAssembling.DocumentByteArray.Length);
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true))
{
OpenXmlValidator v = new OpenXmlValidator();
var valErrors = v.Validate(wDoc).Where(ve => !s_ExpectedErrors.Contains(ve.Description));
Assert.Empty(valErrors);
}
}
Assert.Equal(err, returnedTemplateError);
}
private static List<string> s_ExpectedErrors = new List<string>()
{
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenVBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRow' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRowFirstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRowLastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRow' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRowFirstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRowLastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:noHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:noVBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:oddHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:oddVBand' attribute is not declared.",
};
}
}
#endif