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 PrecedingIteratorTest {
30
31 @Test
32 public void testPrecedingNodes() {
33 final NodeInfo startNode = findNode("SLIST");
34
35 try (PrecedingIterator iterator = new PrecedingIterator(startNode)) {
36 assertWithMessage("Invalid node")
37 .that(iterator.next())
38 .isEqualTo(findNode("PARAMETERS"));
39 assertWithMessage("Invalid node")
40 .that(iterator.next())
41 .isEqualTo(findNode("METHOD_DEF"));
42 assertWithMessage("Invalid node")
43 .that(iterator.next())
44 .isEqualTo(findNode("LCURLY"));
45 assertWithMessage("Invalid node")
46 .that(iterator.next())
47 .isEqualTo(findNode("OBJBLOCK"));
48 assertWithMessage("Invalid node")
49 .that(iterator.next())
50 .isEqualTo(findNode("CLASS_DEF"));
51 assertWithMessage("Invalid node")
52 .that(iterator.next())
53 .isEqualTo(findNode("ANNOTATIONS"));
54 assertWithMessage("Invalid node")
55 .that(iterator.next())
56 .isEqualTo(findNode("ANNOTATION_DEF"));
57 assertWithMessage("Invalid node")
58 .that(iterator.next())
59 .isEqualTo(findNode("PACKAGE_DEF"));
60 assertWithMessage("Invalid node")
61 .that(iterator.next())
62 .isEqualTo(findNode("ROOT"));
63 assertWithMessage("Node should be null")
64 .that(iterator.next())
65 .isNull();
66 assertWithMessage("Node should be null")
67 .that(iterator.next())
68 .isNull();
69 }
70 }
71
72 @Test
73 public void testNoParent() {
74 final NodeInfo startNode = findNode("ROOT");
75
76 try (PrecedingIterator iterator = new PrecedingIterator(startNode)) {
77 assertWithMessage("Node should be null")
78 .that(iterator.next())
79 .isNull();
80 }
81 }
82
83 @Test
84 public void testReverseOrderOfDescendants() {
85 final NodeInfo startNode = findNode("RCURLY");
86
87 try (PrecedingIterator iterator = new PrecedingIterator(startNode)) {
88 assertWithMessage("Invalid node")
89 .that(iterator.next())
90 .isEqualTo(findNode("METHOD_DEF"));
91 assertWithMessage("Invalid node")
92 .that(iterator.next())
93 .isEqualTo(findNode("SLIST"));
94 assertWithMessage("Invalid node")
95 .that(iterator.next())
96 .isEqualTo(findNode("PARAMETERS"));
97 assertWithMessage("Invalid node")
98 .that(iterator.next())
99 .isEqualTo(findNode("LCURLY"));
100 assertWithMessage("Invalid node")
101 .that(iterator.next())
102 .isEqualTo(findNode("OBJBLOCK"));
103 assertWithMessage("Invalid node")
104 .that(iterator.next())
105 .isEqualTo(findNode("CLASS_DEF"));
106 assertWithMessage("Invalid node")
107 .that(iterator.next())
108 .isEqualTo(findNode("ANNOTATIONS"));
109 assertWithMessage("Invalid node")
110 .that(iterator.next())
111 .isEqualTo(findNode("ANNOTATION_DEF"));
112 assertWithMessage("Invalid node")
113 .that(iterator.next())
114 .isEqualTo(findNode("PACKAGE_DEF"));
115 assertWithMessage("Invalid node")
116 .that(iterator.next())
117 .isEqualTo(findNode("ROOT"));
118 assertWithMessage("Node should be null")
119 .that(iterator.next())
120 .isNull();
121 }
122 }
123
124 @Test
125 public void testNoSibling() {
126 final NodeInfo startNode = findNode("ANNOTATIONS");
127
128 try (PrecedingIterator iterator = new PrecedingIterator(startNode)) {
129 assertWithMessage("Invalid node")
130 .that(iterator.next())
131 .isEqualTo(findNode("PACKAGE_DEF"));
132 assertWithMessage("Invalid node")
133 .that(iterator.next())
134 .isEqualTo(findNode("ROOT"));
135 assertWithMessage("Node should be null")
136 .that(iterator.next())
137 .isNull();
138 }
139 }
140 }