aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/styles/ParagraphMatcherTests.java
blob: 9ec58fc9da5080c23768fe614dc0a46f31d597cc (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
104
package org.zwobble.mammoth.tests.styles;

import org.junit.jupiter.api.Test;
import org.zwobble.mammoth.internal.documents.NumberingLevel;
import org.zwobble.mammoth.internal.documents.Paragraph;
import org.zwobble.mammoth.internal.documents.Style;
import org.zwobble.mammoth.internal.styles.ParagraphMatcher;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*;

public class ParagraphMatcherTests {
    @Test
    public void matcherWithoutConstraintsMatchesEverything() {
        assertTrue(ParagraphMatcher.ANY.matches(paragraph()));

        Paragraph paragraphWithStyleId = paragraph(withStyle(new Style("TipsParagraph", Optional.empty())));
        assertTrue(ParagraphMatcher.ANY.matches(paragraphWithStyleId));

        Paragraph paragraphWithStyleName = paragraph(withStyle(new Style("TipsParagraph", Optional.of("Tips Paragraph"))));
        assertTrue(ParagraphMatcher.ANY.matches(paragraphWithStyleName));
    }

    @Test
    public void matcherWithStyleIdOnlyMatchesParagraphsWithThatStyleId() {
        ParagraphMatcher matcher = ParagraphMatcher.styleId("TipsParagraph");
        assertFalse(matcher.matches(paragraph()));

        Paragraph paragraphWithCorrectStyleId = paragraph(
            withStyle(new Style("TipsParagraph", Optional.empty())));
        assertTrue(matcher.matches(paragraphWithCorrectStyleId));

        Paragraph paragraphWithIncorrectStyleId = paragraph(
            withStyle(new Style("Heading 1", Optional.empty())));
        assertFalse(matcher.matches(paragraphWithIncorrectStyleId));
    }

    @Test
    public void matcherWithStyleNameOnlyMatchesParagraphsWithThatStyleName() {
        ParagraphMatcher matcher = ParagraphMatcher.styleName("Tips Paragraph");
        assertFalse(matcher.matches(paragraph()));

        Paragraph paragraphWithNamelessStyle = paragraph(
            withStyle(new Style("TipsParagraph", Optional.empty())));
        assertFalse(matcher.matches(paragraphWithNamelessStyle));

        Paragraph paragraphWithCorrectStyleName = paragraph(
            withStyle(new Style("TipsParagraph", Optional.of("Tips Paragraph"))));
        assertTrue(matcher.matches(paragraphWithCorrectStyleName));

        Paragraph paragraphWithIncorrectStyleName = paragraph(
            withStyle(new Style("Heading 1", Optional.of("Heading 1"))));
        assertFalse(matcher.matches(paragraphWithIncorrectStyleName));
    }

    @Test
    public void styleNamesAreCaseInsensitive() {
        ParagraphMatcher matcher = ParagraphMatcher.styleName("tips paragraph");
        assertFalse(matcher.matches(paragraph()));

        Paragraph paragraphWithCorrectStyleName = paragraph(
            withStyle(new Style("TipsParagraph", Optional.of("Tips Paragraph"))));
        assertTrue(matcher.matches(paragraphWithCorrectStyleName));
    }

    @Test
    public void matcherWithOrderedListOnlyMatchesParagraphsWithOrderedListAtThatLeve() {
        ParagraphMatcher matcher = ParagraphMatcher.orderedList("4");
        assertFalse(matcher.matches(paragraph()));

        Paragraph paragraphWithCorrectNumbering = paragraph(
            withNumbering(NumberingLevel.ordered("4")));
        assertTrue(matcher.matches(paragraphWithCorrectNumbering));

        Paragraph paragraphWithIncorrectLevel = paragraph(
            withNumbering(NumberingLevel.ordered("3")));
        assertFalse(matcher.matches(paragraphWithIncorrectLevel));

        Paragraph paragraphWithIncorrectOrdering = paragraph(
            withNumbering(NumberingLevel.unordered("4")));
        assertFalse(matcher.matches(paragraphWithIncorrectOrdering));
    }

    @Test
    public void matcherWithUnorderedListOnlyMatchesParagraphsWithUnrderedListAtThatLeve() {
        ParagraphMatcher matcher = ParagraphMatcher.unorderedList("4");
        assertFalse(matcher.matches(paragraph()));

        Paragraph paragraphWithCorrectNumbering = paragraph(
            withNumbering(NumberingLevel.unordered("4")));
        assertTrue(matcher.matches(paragraphWithCorrectNumbering));

        Paragraph paragraphWithIncorrectLevel = paragraph(
            withNumbering(NumberingLevel.unordered("3")));
        assertFalse(matcher.matches(paragraphWithIncorrectLevel));

        Paragraph paragraphWithIncorrectOrdering = paragraph(
            withNumbering(NumberingLevel.ordered("4")));
        assertFalse(matcher.matches(paragraphWithIncorrectOrdering));
    }
}