1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.xpath.iterators;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.internal.utils.XpathIteratorUtil.findNode;
24
25 import org.junit.jupiter.api.Test;
26
27 import net.sf.saxon.om.NodeInfo;
28
29 public class DescendantIteratorTest {
30
31 @Test
32 public void testIncludeSelf() {
33 final NodeInfo startNode = findNode("CLASS_DEF");
34
35 try (DescendantIterator iterator = new DescendantIterator(startNode,
36 DescendantIterator.StartWith.CURRENT_NODE)) {
37 assertWithMessage("Invalid node")
38 .that(iterator.next())
39 .isEqualTo(startNode);
40 assertWithMessage("Invalid node")
41 .that(iterator.next())
42 .isEqualTo(findNode("OBJBLOCK"));
43 assertWithMessage("Invalid node")
44 .that(iterator.next())
45 .isEqualTo(findNode("LCURLY"));
46 assertWithMessage("Invalid node")
47 .that(iterator.next())
48 .isEqualTo(findNode("METHOD_DEF"));
49 assertWithMessage("Invalid node")
50 .that(iterator.next())
51 .isEqualTo(findNode("RCURLY"));
52 assertWithMessage("Invalid node")
53 .that(iterator.next())
54 .isEqualTo(findNode("PARAMETERS"));
55 assertWithMessage("Invalid node")
56 .that(iterator.next())
57 .isEqualTo(findNode("SLIST"));
58 assertWithMessage("Node should be null")
59 .that(iterator.next())
60 .isNull();
61 assertWithMessage("Node should be null")
62 .that(iterator.next())
63 .isNull();
64 }
65 }
66
67 @Test
68 public void testWithoutSelf() {
69 final NodeInfo startNode = findNode("CLASS_DEF");
70
71 try (DescendantIterator iterator = new DescendantIterator(startNode,
72 DescendantIterator.StartWith.CHILDREN)) {
73 assertWithMessage("Invalid node")
74 .that(iterator.next())
75 .isEqualTo(findNode("OBJBLOCK"));
76 assertWithMessage("Invalid node")
77 .that(iterator.next())
78 .isEqualTo(findNode("LCURLY"));
79 assertWithMessage("Invalid node")
80 .that(iterator.next())
81 .isEqualTo(findNode("METHOD_DEF"));
82 assertWithMessage("Invalid node")
83 .that(iterator.next())
84 .isEqualTo(findNode("RCURLY"));
85 assertWithMessage("Invalid node")
86 .that(iterator.next())
87 .isEqualTo(findNode("PARAMETERS"));
88 assertWithMessage("Invalid node")
89 .that(iterator.next())
90 .isEqualTo(findNode("SLIST"));
91 assertWithMessage("Node should be null")
92 .that(iterator.next())
93 .isNull();
94 }
95 }
96
97 @Test
98 public void testWithNull() {
99 final NodeInfo startNode = findNode("CLASS_DEF");
100
101 try (DescendantIterator iterator = new DescendantIterator(startNode, null)) {
102 assertWithMessage("Node should be null")
103 .that(iterator.next())
104 .isNull();
105 }
106 }
107 }