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
246
|
package org.zwobble.mammoth.tests.docx;
import com.natpryce.makeiteasy.Maker;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.zwobble.mammoth.results.Result;
import org.zwobble.mammoth.documents.*;
import org.zwobble.mammoth.docx.BodyXmlReader;
import org.zwobble.mammoth.docx.Numbering;
import org.zwobble.mammoth.docx.Styles;
import org.zwobble.mammoth.results.Warning;
import org.zwobble.mammoth.tests.DeepReflectionMatcher;
import org.zwobble.mammoth.xml.XmlElement;
import org.zwobble.mammoth.xml.XmlNode;
import org.zwobble.mammoth.xml.XmlNodes;
import java.util.List;
import java.util.Optional;
import static com.natpryce.makeiteasy.MakeItEasy.*;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasProperty;
import static org.zwobble.mammoth.results.Warning.warning;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.CHILDREN;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.PARAGRAPH;
import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.*;
import static org.zwobble.mammoth.util.MammothLists.list;
import static org.zwobble.mammoth.util.MammothMaps.map;
import static org.zwobble.mammoth.xml.XmlNodes.element;
public class BodyXmlTests {
@Test
public void textFromTextElementIsRead() {
XmlElement element = textXml("Hello!");
assertThat(readSuccess(a(bodyReader), element), isTextElement("Hello!"));
}
@Test
public void canReadTextWithinRun() {
XmlElement element = runXml(list(textXml("Hello!")));
assertThat(
readSuccess(a(bodyReader), element),
isRun(run(text("Hello!"))));
}
@Test
public void canReadTextWithinParagraph() {
XmlElement element = paragraphXml(list(runXml(list(textXml("Hello!")))));
assertThat(
readSuccess(a(bodyReader), element),
isParagraph(make(a(PARAGRAPH, with(CHILDREN, list(run(text("Hello!"))))))));
}
@Test
public void paragraphHasNoStyleIfItHasNoProperties() {
XmlElement element = paragraphXml();
assertThat(
readSuccess(a(bodyReader), element),
hasStyle(Optional.empty()));
}
@Test
public void whenParagraphHasStyleIdInStylesThenStyleNameIsReadFromStyles() {
XmlElement element = paragraphXml(list(
element("w:pPr", list(
element("w:pStyle", map("w:val", "Heading1"))))));
Style style = new Style("Heading1", Optional.of("Heading 1"));
Styles styles = new Styles(
map("Heading1", style),
map());
assertThat(
readSuccess(a(bodyReader, with(STYLES, styles)), element),
hasStyle(Optional.of(style)));
}
@Test
public void warningIsEmittedWhenParagraphStyleCannotBeFound() {
XmlElement element = paragraphXml(list(
element("w:pPr", list(
element("w:pStyle", map("w:val", "Heading1"))))));
assertThat(
read(a(bodyReader), element),
isResult(
hasStyle(Optional.of(new Style("Heading1", Optional.empty()))),
list(warning("Paragraph style with ID Heading1 was referenced but not defined in the document"))));
}
@Test
public void paragraphHasNoNumberingIfItHasNoNumberingProperties() {
XmlElement element = paragraphXml();
assertThat(
readSuccess(a(bodyReader), element),
hasNumbering(Optional.empty()));
}
@Test
public void paragraphHasNumberingPropertiesFromParagraphPropertiesIfPresent() {
XmlElement element = paragraphXml(list(
element("w:pPr", list(
element("w:numPr", map(), list(
element("w:ilvl", map("w:val", "1")),
element("w:numId", map("w:val", "42"))))))));
Numbering numbering = new Numbering(map("42", map("1", NumberingLevel.ordered("1"))));
assertThat(
readSuccess(a(bodyReader, with(NUMBERING, numbering)), element),
hasNumbering(NumberingLevel.ordered("1")));
}
@Test
public void numberingPropertiesAreIgnoredIfLevelIsMissing() {
// TODO: emit warning
XmlElement element = paragraphXml(list(
element("w:pPr", list(
element("w:numPr", map(), list(
element("w:numId", map("w:val", "42"))))))));
Numbering numbering = new Numbering(map("42", map("1", NumberingLevel.ordered("1"))));
assertThat(
readSuccess(a(bodyReader, with(NUMBERING, numbering)), element),
hasNumbering(Optional.empty()));
}
@Test
public void numberingPropertiesAreIgnoredIfNumIdIsMissing() {
// TODO: emit warning
XmlElement element = paragraphXml(list(
element("w:pPr", list(
element("w:numPr", map(), list(
element("w:ilvl", map("w:val", "1"))))))));
Numbering numbering = new Numbering(map("42", map("1", NumberingLevel.ordered("1"))));
assertThat(
readSuccess(a(bodyReader, with(NUMBERING, numbering)), element),
hasNumbering(Optional.empty()));
}
@Test
public void runHasNoStyleIfItHasNoProperties() {
XmlElement element = runXml(list());
assertThat(
readSuccess(a(bodyReader), element),
hasStyle(Optional.empty()));
}
@Test
public void whenRunHasStyleIdInStylesThenStyleNameIsReadFromStyles() {
XmlElement element = runXml(list(
element("w:rPr", list(
element("w:rStyle", map("w:val", "Heading1Char"))))));
Style style = new Style("Heading1Char", Optional.of("Heading 1 Char"));
Styles styles = new Styles(
map(),
map("Heading1Char", style));
assertThat(
readSuccess(a(bodyReader, with(STYLES, styles)), element),
hasStyle(Optional.of(style)));
}
@Test
public void warningIsEmittedWhenRunStyleCannotBeFound() {
XmlElement element = runXml(list(
element("w:rPr", list(
element("w:rStyle", map("w:val", "Heading1Char"))))));
assertThat(
read(a(bodyReader), element),
isResult(
hasStyle(Optional.of(new Style("Heading1Char", Optional.empty()))),
list(warning("Run style with ID Heading1Char was referenced but not defined in the document"))));
}
private static DocumentElement readSuccess(Maker<BodyXmlReader> reader, XmlElement element) {
Result<DocumentElement> result = read(reader, element);
assertThat(result.getWarnings(), deepEquals(list()));
return result.getValue();
}
private static Result<DocumentElement> read(Maker<BodyXmlReader> reader, XmlElement element) {
return reader.make().readElement(element).toResult()
.map(elements -> elements.get(0));
}
private XmlElement paragraphXml() {
return paragraphXml(list());
}
private XmlElement paragraphXml(List<XmlNode> children) {
return element("w:p", children);
}
private XmlElement runXml(List<XmlNode> children) {
return element("w:r", children);
}
private XmlElement textXml(String value) {
return element("w:t", list(XmlNodes.text(value)));
}
private Matcher<DocumentElement> isParagraph(Paragraph expected) {
return new DeepReflectionMatcher<>(expected);
}
private Matcher<DocumentElement> isRun(Run expected) {
return new DeepReflectionMatcher<>(expected);
}
private Matcher<DocumentElement> isTextElement(String value) {
return new DeepReflectionMatcher<>(new Text(value));
}
private Matcher<? super DocumentElement> hasStyle(Optional<Style> expected) {
return hasProperty("style", deepEquals(expected));
}
private Matcher<? super DocumentElement> hasNumbering(NumberingLevel expected) {
return hasNumbering(Optional.of(expected));
}
private Matcher<? super DocumentElement> hasNumbering(Optional<NumberingLevel> expected) {
return hasProperty("numbering", deepEquals(expected));
}
private <T> Matcher<Result<? extends T>> isResult(Matcher<T> valueMatcher, List<Warning> warnings) {
return Matchers.allOf(
hasProperty("value", valueMatcher),
hasProperty("warnings", deepEquals(warnings)));
}
private Run run(DocumentElement... children) {
return new Run(Optional.empty(), asList(children));
}
private Text text(String value) {
return new Text(value);
}
}
|