Skip to content

Commit 4e65b06

Browse files
committed
Make Nodes.find throw when not found
1 parent 06aa3e5 commit 4e65b06

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

commonmark/src/test/java/org/commonmark/test/ListBlockParserTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public void testOrderedListIndents() {
6060
private void assertListItemIndents(String input, int expectedMarkerIndent, int expectedContentIndent) {
6161
Node doc = PARSER.parse(input);
6262
ListItem listItem = Nodes.find(doc, ListItem.class);
63-
assertNotNull(listItem);
6463
assertEquals(expectedMarkerIndent, listItem.getMarkerIndent());
6564
assertEquals(expectedContentIndent, listItem.getContentIndent());
6665
}

commonmark/src/test/java/org/commonmark/test/Nodes.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,32 @@ public static List<Node> getChildren(Node parent) {
2121
* @param parent The node to get children from (node itself will not be checked)
2222
* @param nodeClass The type of node to find
2323
*/
24-
public static <T> T find(Node parent, Class<T> nodeClass) {
24+
public static <T> T tryFind(Node parent, Class<T> nodeClass) {
2525
Node node = parent.getFirstChild();
2626
while (node != null) {
2727
Node next = node.getNext();
2828
if (nodeClass.isInstance(node)) {
2929
//noinspection unchecked
3030
return (T) node;
3131
}
32-
T result = find(node, nodeClass);
32+
T result = tryFind(node, nodeClass);
3333
if (result != null) {
3434
return result;
3535
}
3636
node = next;
3737
}
3838
return null;
3939
}
40+
41+
/**
42+
* Recursively try to find a node with the given type within the children of the specified node. Throw if node
43+
* could not be found.
44+
*/
45+
public static <T> T find(Node parent, Class<T> nodeClass) {
46+
var node = tryFind(parent, nodeClass);
47+
if (node == null) {
48+
throw new IllegalArgumentException("Could not find a " + nodeClass.getSimpleName() + " node in " + parent);
49+
}
50+
return node;
51+
}
4052
}

commonmark/src/test/java/org/commonmark/test/ThematicBreakParserTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import org.commonmark.node.ThematicBreak;
44
import org.commonmark.parser.Parser;
5-
import org.commonmark.renderer.html.HtmlRenderer;
6-
import org.commonmark.testutil.RenderingTestCase;
75
import org.junit.Test;
86

97
import static org.junit.Assert.assertEquals;
@@ -23,7 +21,6 @@ public void testLiteral() {
2321

2422
private static void assertLiteral(String expected, String input) {
2523
var tb = Nodes.find(PARSER.parse(input), ThematicBreak.class);
26-
assertNotNull(tb);
2724
assertEquals(expected, tb.getLiteral());
2825
}
2926
}

0 commit comments

Comments
 (0)