Skip to content

Commit 77c69b6

Browse files
committed
Improve test coverage
Signed-off-by: Moritz Schubotz (physikerwelt) <wiki@physikerwelt.de>
1 parent 62cbf91 commit 77c69b6

4 files changed

Lines changed: 52 additions & 11 deletions

File tree

mathml-core/src/main/java/com/formulasearchengine/mathmltools/mml/CIdentifier.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ public Integer getOrdinal() {
1414

1515
private int ordinal = -1;
1616

17-
public CIdentifier(Element n) {
18-
this.n = n;
19-
}
20-
2117
public CIdentifier(Element n, int i) {
2218
this.n = n;
2319
this.ordinal = i;

mathml-core/src/main/java/com/formulasearchengine/mathmltools/mml/MathDoc.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.xml.transform.stream.StreamSource;
2424
import javax.xml.validation.SchemaFactory;
2525
import javax.xml.xpath.XPath;
26+
import javax.xml.xpath.XPathExpression;
2627
import javax.xml.xpath.XPathExpressionException;
2728
import org.apache.commons.lang3.NotImplementedException;
2829
import org.apache.logging.log4j.LogManager;
@@ -254,22 +255,28 @@ public List<CSymbol> getCSymbols() {
254255
public List<CIdentifier> getIdentifiers() {
255256
if (cIdentifiers == null) {
256257
final IterableNodeList nodeList;
257-
try {
258-
XPath xpath = XMLHelper.namespaceAwareXpath("m", NS_MATHML);
259-
nodeList = new IterableNodeList(getElementsB(dom, xpath.compile("//m:ci")));
258+
nodeList = getXNodes("//m:ci");
260259
cIdentifiers = new ArrayList<>();
261260
int i = 0;
262261
for (Node node : nodeList) {
263262
cIdentifiers.add(new CIdentifier((Element) node, i));
264263
i++;
265264
}
266-
} catch (XPathExpressionException e) {
267-
e.printStackTrace();
268-
}
269265
}
270266
return cIdentifiers;
271267
}
272268

269+
private IterableNodeList getXNodes(String xPath) {
270+
XPath xpath = XMLHelper.namespaceAwareXpath("m", NS_MATHML);
271+
try {
272+
final XPathExpression pattern = xpath.compile(xPath);
273+
final NodeList elements = getElementsB(dom, pattern);
274+
return new IterableNodeList(elements);
275+
} catch (XPathExpressionException e) {
276+
throw new RuntimeException(e);
277+
}
278+
}
279+
273280
public Document getDom() {
274281
return dom;
275282
}
@@ -320,7 +327,7 @@ private void highlightIdentifier(CIdentifier identifier) {
320327
try {
321328
final Element identifierPresentation = identifier.getPresentation();
322329
if (identifierPresentation.hasAttribute("class")) {
323-
log.warn("Presentation node " + identifier.getXref() + "has already class attribute. Cannot highlight!");
330+
log.warn("Presentation node " + identifier.getXref() + " has already class attribute. Cannot highlight!");
324331
} else {
325332
identifierPresentation.setAttribute("class", "highlightedIdentifier");
326333
}

mathml-core/src/test/java/com/formulasearchengine/mathmltools/mml/CIdentifierTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ void equalsTo() throws Exception {
5555
final CIdentifier E2 = getE();
5656
assertEquals(E2, E);
5757
}
58+
@Test
59+
void equalsTo2() throws Exception {
60+
final CIdentifier E = getE();
61+
assertNotEquals("E", E);
62+
}
5863
}

mathml-core/src/test/java/com/formulasearchengine/mathmltools/mml/MathTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.hamcrest.MatcherAssert.assertThat;
2222
import static org.hamcrest.core.IsEqual.equalTo;
2323
import static org.junit.Assert.assertFalse;
24+
import static org.junit.Assert.assertTrue;
2425
import static org.junit.jupiter.api.Assertions.assertEquals;
2526
import static org.junit.jupiter.api.Assertions.assertThrows;
2627

@@ -201,6 +202,38 @@ void HighlightMTestFirst2k() throws Exception {
201202
isHighlighted(mml, PID_k2);
202203
isNotHighlighted(mml, PID_k3);
203204
}
205+
206+
@Test
207+
void HighlightMTestFirstk2() throws Exception {
208+
final String sampleMML = getFileContents(TEST_DIR + "Van_der_Waerden_CI.mml");
209+
final MathDoc mml = new MathDoc(sampleMML);
210+
final int WHash = "Q7913892".hashCode();
211+
final int kHash = "Q12503".hashCode();
212+
final ArrayList<Integer> toHighlight = new ArrayList<>();
213+
toHighlight.add(kHash);
214+
toHighlight.add(WHash);
215+
mml.highlightConsecutiveIdentifiers(toHighlight, false);
216+
isHighlighted(mml, PID_k1);
217+
isNotHighlighted(mml, PID_k2);
218+
isNotHighlighted(mml, PID_k3);
219+
}
220+
221+
@Test
222+
void HighlightTestDefun() throws Exception {
223+
final String sampleMML = getFileContents(TEST_DIR + "Van_der_Waerden_CI.mml");
224+
final MathDoc mml = new MathDoc(sampleMML);
225+
final int kHash = "Q12503".hashCode();
226+
final ArrayList<Integer> toHighlight = new ArrayList<>();
227+
toHighlight.add(kHash);
228+
final CIdentifier k1 = mml.getIdentifiers().get(1);
229+
k1.getPresentation().setAttribute("class","other");
230+
mml.highlightConsecutiveIdentifiers(toHighlight, false);
231+
assertTrue(k1.getPresentation().hasAttribute("class"));
232+
assertEquals("other",k1.getPresentation().getAttribute("class"));
233+
234+
isNotHighlighted(mml, PID_k2);
235+
isNotHighlighted(mml, PID_k3);
236+
}
204237
}
205238

206239
/*

0 commit comments

Comments
 (0)