Skip to content

Commit 08f54a8

Browse files
committed
more tests
1 parent cff9a5d commit 08f54a8

File tree

2 files changed

+246
-1
lines changed

2 files changed

+246
-1
lines changed

src/test/java/org/htmlunit/cssparser/parser/selector/DescendantSelectorImplTest.java

Lines changed: 170 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
package org.htmlunit.cssparser.parser.selector;
1616

1717
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
1820

1921
import org.htmlunit.cssparser.parser.selector.Selector.SelectorType;
2022
import org.junit.jupiter.api.Test;
2123

2224
/**
2325
* Testcases for {@link DescendantSelector}.
26+
*
2427
* @author Ronald Brill
2528
*/
2629
public class DescendantSelectorImplTest {
@@ -33,8 +36,17 @@ public void ancestorSelector() {
3336
final ElementSelector parent = new ElementSelector("p", null);
3437
final ElementSelector descendant = new ElementSelector("a", null);
3538
final DescendantSelector selector = new DescendantSelector(parent, descendant);
39+
40+
assertNotNull(selector.getAncestorSelector());
3641
assertEquals(parent, selector.getAncestorSelector());
42+
assertEquals(SelectorType.ELEMENT_NODE_SELECTOR, selector.getAncestorSelector().getSelectorType());
43+
assertEquals("p", ((ElementSelector) selector.getAncestorSelector()).getLocalName());
3744

45+
assertNotNull(selector.getSimpleSelector());
46+
assertEquals(descendant, selector.getSimpleSelector());
47+
assertEquals(SelectorType.ELEMENT_NODE_SELECTOR, selector.getSimpleSelector().getSelectorType());
48+
49+
assertEquals(SelectorType.DESCENDANT_SELECTOR, selector.getSelectorType());
3850
assertEquals("p a", selector.toString());
3951
}
4052

@@ -46,8 +58,13 @@ public void simpleSelector() {
4658
final ElementSelector parent = new ElementSelector("p", null);
4759
final ElementSelector descendant = new ElementSelector("a", null);
4860
final DescendantSelector selector = new DescendantSelector(parent, descendant);
61+
62+
assertNotNull(selector.getSimpleSelector());
4963
assertEquals(descendant, selector.getSimpleSelector());
64+
assertEquals(SelectorType.ELEMENT_NODE_SELECTOR, selector.getSimpleSelector().getSelectorType());
65+
assertEquals("a", ((ElementSelector) selector.getSimpleSelector()).getLocalName());
5066

67+
assertEquals(SelectorType.DESCENDANT_SELECTOR, selector.getSelectorType());
5168
assertEquals("p a", selector.toString());
5269
}
5370

@@ -59,8 +76,10 @@ public void selectorType() {
5976
final ElementSelector parent = new ElementSelector("p", null);
6077
final ElementSelector descendant = new ElementSelector("a", null);
6178
final DescendantSelector selector = new DescendantSelector(parent, descendant);
62-
assertEquals(SelectorType.DESCENDANT_SELECTOR, selector.getSelectorType());
6379

80+
assertEquals(SelectorType.DESCENDANT_SELECTOR, selector.getSelectorType());
81+
assertNotNull(selector.getAncestorSelector());
82+
assertNotNull(selector.getSimpleSelector());
6483
assertEquals("p a", selector.toString());
6584
}
6685

@@ -72,6 +91,16 @@ public void elementDescendant() {
7291
final ElementSelector parent = new ElementSelector("p", null);
7392
final ElementSelector descendant = new ElementSelector("a", null);
7493
final DescendantSelector selector = new DescendantSelector(parent, descendant);
94+
95+
assertNotNull(selector.getAncestorSelector());
96+
assertEquals("p", ((ElementSelector) selector.getAncestorSelector()).getLocalName());
97+
assertEquals("p", ((ElementSelector) selector.getAncestorSelector()).getLocalNameLowerCase());
98+
99+
assertNotNull(selector.getSimpleSelector());
100+
assertEquals("a", ((ElementSelector) selector.getSimpleSelector()).getLocalName());
101+
assertEquals("a", ((ElementSelector) selector.getSimpleSelector()).getLocalNameLowerCase());
102+
103+
assertEquals(SelectorType.DESCENDANT_SELECTOR, selector.getSelectorType());
75104
assertEquals("p a", selector.toString());
76105
}
77106

@@ -84,6 +113,146 @@ public void pseudoElementDescendant() {
84113
final PseudoElementSelector descendant = new PseudoElementSelector("after", null, false);
85114
final DescendantSelector selector = new DescendantSelector(parent, descendant);
86115

116+
assertNotNull(selector.getAncestorSelector());
117+
assertEquals(SelectorType.ELEMENT_NODE_SELECTOR, selector.getAncestorSelector().getSelectorType());
118+
assertEquals("a", ((ElementSelector) selector.getAncestorSelector()).getLocalName());
119+
120+
assertNotNull(selector.getSimpleSelector());
121+
assertEquals(SelectorType.PSEUDO_ELEMENT_SELECTOR, selector.getSimpleSelector().getSelectorType());
122+
assertEquals("after", ((PseudoElementSelector) selector.getSimpleSelector()).getLocalName());
123+
124+
assertEquals(SelectorType.DESCENDANT_SELECTOR, selector.getSelectorType());
87125
assertEquals("a:after", selector.toString());
88126
}
127+
128+
/**
129+
* @throws Exception on failure
130+
*/
131+
@Test
132+
public void nullAncestorSelector() {
133+
final ElementSelector descendant = new ElementSelector("a", null);
134+
final DescendantSelector selector = new DescendantSelector(null, descendant);
135+
136+
assertNull(selector.getAncestorSelector());
137+
138+
assertNotNull(selector.getSimpleSelector());
139+
assertEquals(descendant, selector.getSimpleSelector());
140+
assertEquals(SelectorType.ELEMENT_NODE_SELECTOR, selector.getSimpleSelector().getSelectorType());
141+
assertEquals("a", ((ElementSelector) selector.getSimpleSelector()).getLocalName());
142+
143+
assertEquals(SelectorType.DESCENDANT_SELECTOR, selector.getSelectorType());
144+
assertEquals(" a", selector.toString());
145+
}
146+
147+
/**
148+
* @throws Exception on failure
149+
*/
150+
@Test
151+
public void nullSimpleSelector() {
152+
final ElementSelector parent = new ElementSelector("p", null);
153+
final DescendantSelector selector = new DescendantSelector(parent, null);
154+
155+
assertNotNull(selector.getAncestorSelector());
156+
assertEquals(parent, selector.getAncestorSelector());
157+
assertEquals(SelectorType.ELEMENT_NODE_SELECTOR, selector.getAncestorSelector().getSelectorType());
158+
assertEquals("p", ((ElementSelector) selector.getAncestorSelector()).getLocalName());
159+
160+
assertNull(selector.getSimpleSelector());
161+
162+
assertEquals(SelectorType.DESCENDANT_SELECTOR, selector.getSelectorType());
163+
}
164+
165+
/**
166+
* @throws Exception on failure
167+
*/
168+
@Test
169+
public void complexDescendantChain() {
170+
final ElementSelector grandParent = new ElementSelector("div", null);
171+
final ElementSelector parent = new ElementSelector("p", null);
172+
final DescendantSelector level1 = new DescendantSelector(grandParent, parent);
173+
final ElementSelector child = new ElementSelector("a", null);
174+
final DescendantSelector level2 = new DescendantSelector(level1, child);
175+
176+
assertNotNull(level2.getAncestorSelector());
177+
assertEquals(level1, level2.getAncestorSelector());
178+
assertEquals(SelectorType.DESCENDANT_SELECTOR, level2.getAncestorSelector().getSelectorType());
179+
180+
assertNotNull(level2.getSimpleSelector());
181+
assertEquals(child, level2.getSimpleSelector());
182+
assertEquals(SelectorType.ELEMENT_NODE_SELECTOR, level2.getSimpleSelector().getSelectorType());
183+
assertEquals("a", ((ElementSelector) level2.getSimpleSelector()).getLocalName());
184+
185+
// Verify level1 structure
186+
assertNotNull(level1.getAncestorSelector());
187+
assertEquals(grandParent, level1.getAncestorSelector());
188+
assertEquals("div", ((ElementSelector) level1.getAncestorSelector()).getLocalName());
189+
190+
assertNotNull(level1.getSimpleSelector());
191+
assertEquals(parent, level1.getSimpleSelector());
192+
assertEquals("p", ((ElementSelector) level1.getSimpleSelector()).getLocalName());
193+
194+
assertEquals(SelectorType.DESCENDANT_SELECTOR, level2.getSelectorType());
195+
assertEquals("div p a", level2.toString());
196+
}
197+
198+
/**
199+
* @throws Exception on failure
200+
*/
201+
@Test
202+
public void complexDescendantWithPseudoElement() {
203+
final ElementSelector grandParent = new ElementSelector("div", null);
204+
final ElementSelector parent = new ElementSelector("p", null);
205+
final DescendantSelector level1 = new DescendantSelector(grandParent, parent);
206+
final PseudoElementSelector pseudo = new PseudoElementSelector("first-line", null, false);
207+
final DescendantSelector level2 = new DescendantSelector(level1, pseudo);
208+
209+
assertNotNull(level2.getAncestorSelector());
210+
assertEquals(level1, level2.getAncestorSelector());
211+
assertEquals(SelectorType.DESCENDANT_SELECTOR, level2.getAncestorSelector().getSelectorType());
212+
213+
assertNotNull(level2.getSimpleSelector());
214+
assertEquals(pseudo, level2.getSimpleSelector());
215+
assertEquals(SelectorType.PSEUDO_ELEMENT_SELECTOR, level2.getSimpleSelector().getSelectorType());
216+
assertEquals("first-line", ((PseudoElementSelector) level2.getSimpleSelector()).getLocalName());
217+
218+
assertEquals(SelectorType.DESCENDANT_SELECTOR, level2.getSelectorType());
219+
assertEquals("div p:first-line", level2.toString());
220+
}
221+
222+
/**
223+
* @throws Exception on failure
224+
*/
225+
@Test
226+
public void bothSelectorsNull() {
227+
final DescendantSelector selector = new DescendantSelector(null, null);
228+
229+
assertNull(selector.getAncestorSelector());
230+
assertNull(selector.getSimpleSelector());
231+
assertEquals(SelectorType.DESCENDANT_SELECTOR, selector.getSelectorType());
232+
}
233+
234+
/**
235+
* @throws Exception on failure
236+
*/
237+
@Test
238+
public void tripleNestedDescendant() {
239+
final ElementSelector level0 = new ElementSelector("html", null);
240+
final ElementSelector level1 = new ElementSelector("body", null);
241+
final ElementSelector level2 = new ElementSelector("div", null);
242+
final ElementSelector level3 = new ElementSelector("span", null);
243+
244+
final DescendantSelector desc1 = new DescendantSelector(level0, level1);
245+
final DescendantSelector desc2 = new DescendantSelector(desc1, level2);
246+
final DescendantSelector desc3 = new DescendantSelector(desc2, level3);
247+
248+
assertNotNull(desc3.getAncestorSelector());
249+
assertEquals(desc2, desc3.getAncestorSelector());
250+
assertEquals(SelectorType.DESCENDANT_SELECTOR, desc3.getAncestorSelector().getSelectorType());
251+
252+
assertNotNull(desc3.getSimpleSelector());
253+
assertEquals(level3, desc3.getSimpleSelector());
254+
assertEquals("span", ((ElementSelector) desc3.getSimpleSelector()).getLocalName());
255+
256+
assertEquals("html body div span", desc3.toString());
257+
}
89258
}

src/test/java/org/htmlunit/cssparser/parser/selector/SelectorSpecificityTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,80 @@ public void hasTest() throws Exception {
262262
selectorSpecifity(":has([data-theme='dark'], .night)", "0,0,1,0");
263263
selectorSpecifity(":has([data-theme='dark'], #night)", "0,1,0,0");
264264
}
265+
266+
/**
267+
* @throws Exception if the test fails
268+
*/
269+
@Test
270+
public void notPseudoClass() throws Exception {
271+
selectorSpecifity(":not(p)", "0,0,0,1");
272+
selectorSpecifity(":not(.class)", "0,0,1,0");
273+
selectorSpecifity(":not(#id)", "0,1,0,0");
274+
}
275+
276+
/**
277+
* @throws Exception if the test fails
278+
*/
279+
@Test
280+
public void isPseudoClass() throws Exception {
281+
selectorSpecifity(":is(p)", "0,0,0,1");
282+
selectorSpecifity(":is(.class)", "0,0,1,0");
283+
selectorSpecifity(":is(#id)", "0,1,0,0");
284+
}
285+
286+
/**
287+
* @throws Exception if the test fails
288+
*/
289+
@Test
290+
public void wherePseudoClass() throws Exception {
291+
selectorSpecifity(":where(p)", "0,0,0,0");
292+
selectorSpecifity(":where(.class)", "0,0,0,0");
293+
selectorSpecifity(":where(#id)", "0,0,0,0");
294+
}
295+
296+
/**
297+
* @throws Exception if the test fails
298+
*/
299+
@Test
300+
public void multipleIds() throws Exception {
301+
selectorSpecifity("#id1#id2", "0,2,0,0");
302+
selectorSpecifity("div#id1#id2", "0,2,0,1");
303+
}
304+
305+
/**
306+
* @throws Exception if the test fails
307+
*/
308+
@Test
309+
public void complexCombinations() throws Exception {
310+
selectorSpecifity("div.class1.class2#id", "0,1,2,1");
311+
selectorSpecifity("ul li:first-child a", "0,0,1,3");
312+
selectorSpecifity("#nav ul li a:hover", "0,1,1,3");
313+
}
314+
315+
/**
316+
* @throws Exception if the test fails
317+
*/
318+
@Test
319+
public void attributeSelectors() throws Exception {
320+
selectorSpecifity("[type]", "0,0,1,0");
321+
selectorSpecifity("input[type=\"text\"]", "0,0,1,1");
322+
selectorSpecifity("a[href^=\"https\"]", "0,0,1,1");
323+
}
324+
325+
/**
326+
* @throws Exception if the test fails
327+
*/
328+
@Test
329+
public void compareToVariousCombinations() throws Exception {
330+
final SelectorSpecificity s1 = selectorSpecifity("*", "0,0,0,0");
331+
final SelectorSpecificity s2 = selectorSpecifity("#id", "0,1,0,0");
332+
final SelectorSpecificity s3 = selectorSpecifity(".class", "0,0,1,0");
333+
final SelectorSpecificity s4 = selectorSpecifity("p", "0,0,0,1");
334+
335+
assertTrue(s1.compareTo(s2) < 0);
336+
assertTrue(s1.compareTo(s3) < 0);
337+
assertTrue(s1.compareTo(s4) < 0);
338+
assertTrue(s4.compareTo(s3) < 0);
339+
assertTrue(s3.compareTo(s2) < 0);
340+
}
265341
}

0 commit comments

Comments
 (0)