aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/styles/parsing/HtmlPathParsingTests.java
blob: 85591adef0c5aa05269c5ee7f24b4be15240c65b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package org.zwobble.mammoth.tests.styles.parsing;

import org.junit.jupiter.api.Test;
import org.zwobble.mammoth.internal.styles.HtmlPath;
import org.zwobble.mammoth.internal.styles.parsing.HtmlPathParser;
import org.zwobble.mammoth.internal.styles.parsing.StyleMappingTokeniser;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.zwobble.mammoth.internal.util.Lists.list;
import static org.zwobble.mammoth.internal.util.Maps.map;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;

public class HtmlPathParsingTests {
    @Test
    public void canParseEmptyPath() {
        assertThat(
            parseHtmlPath(""),
            deepEquals(HtmlPath.EMPTY));
    }

    @Test
    public void canReadSingleElement() {
        assertThat(
            parseHtmlPath("p"),
            deepEquals(HtmlPath.collapsibleElement("p")));
    }

    @Test
    public void canReadElementWithChoiceOfTagNames() {
        assertThat(
            parseHtmlPath("ul|ol"),
            deepEquals(HtmlPath.collapsibleElement(list("ul", "ol"))));

        assertThat(
            parseHtmlPath("ul|ol|p"),
            deepEquals(HtmlPath.collapsibleElement(list("ul", "ol", "p"))));
    }

    @Test
    public void canReadNestedElements() {
        assertThat(
            parseHtmlPath("ul > li"),
            deepEquals(HtmlPath.elements(
                HtmlElementBuilder.collapsible("ul").pathElement(),
                HtmlElementBuilder.collapsible("li").pathElement()
            )));
    }

    @Test
    public void canReadClassOnElement() {
        assertThat(
            parseHtmlPath("p.tip"),
            deepEquals(HtmlPath.collapsibleElement("p", map("class", "tip"))));
    }

    @Test
    public void canReadMultipleClassesOnElement() {
        assertThat(
            parseHtmlPath("p.tip.help"),
            deepEquals(HtmlPath.collapsibleElement("p", map("class", "tip help"))));
    }

    @Test
    public void canReadAttributeOnElement() {
        assertThat(
            parseHtmlPath("p[lang='fr']"),
            deepEquals(HtmlPath.collapsibleElement("p", map("lang", "fr"))));
    }

    @Test
    public void canReadMultipleAttributesOnElement() {
        assertThat(
            parseHtmlPath("p[lang='fr'][data-x='y']"),
            deepEquals(HtmlPath.collapsibleElement("p", map("lang", "fr", "data-x", "y"))));
    }

    @Test
    public void canReadWhenElementMustBeFresh() {
        assertThat(
            parseHtmlPath("p:fresh"),
            deepEquals(HtmlPath.element("p")));
    }

    @Test
    public void canReadSeparatorForElements() {
        assertThat(
            parseHtmlPath("p:separator('x')"),
            deepEquals(HtmlPath.elements(HtmlElementBuilder.collapsible("p").separator("x").pathElement()))
        );
    }

    @Test
    public void canReadIgnoreElement() {
        assertThat(
            parseHtmlPath("!"),
            deepEquals(HtmlPath.IGNORE)
        );
    }

    private HtmlPath parseHtmlPath(String input) {
        return HtmlPathParser.parse(StyleMappingTokeniser.tokenise(input));
    }
}