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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
/*
* SonarQube
* Copyright (C) 2009-2022 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.test.html;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import static java.util.Collections.emptyList;
public class HtmlParagraphAssert extends HtmlBlockAssert<HtmlParagraphAssert> {
private final Iterator<Element> nextBlocks;
public HtmlParagraphAssert(Element paragraph, Iterator<Element> nextBlocks) {
super(paragraph, HtmlParagraphAssert.class);
this.nextBlocks = nextBlocks;
}
static void verifyIsParagraph(Element element) {
Assertions.assertThat(element.tagName())
.describedAs(
"next block is not a <%s> (got <%s>):" + PRINT_FRAGMENT_TEMPLATE,
"p", element.tagName(), element.toString())
.isEqualTo("p");
}
/**
* Verify the next block exists, is a paragraph and returns an Assert on this block.
*/
public HtmlParagraphAssert hasParagraph() {
return new HtmlParagraphAssert(hasParagraphImpl(), nextBlocks);
}
private Element hasParagraphImpl() {
isNotNull();
Assertions.assertThat(nextBlocks.hasNext())
.describedAs("no more bloc")
.isTrue();
Element element = nextBlocks.next();
verifyIsParagraph(element);
return element;
}
/**
* Convenience method.
* Sames as {@code hasParagraph().withText(text)}.
*/
public HtmlParagraphAssert hasParagraph(String text) {
return hasParagraph()
.withText(text);
}
/**
* Verifies next paragraph is empty or contains only " "
*/
public HtmlParagraphAssert hasEmptyParagraph() {
Element paragraph = hasParagraphImpl();
Assertions.assertThat(paragraph.text())
.describedAs(PRINT_FRAGMENT_TEMPLATE, paragraph)
.isIn("", "\u00A0");
return new HtmlParagraphAssert(paragraph, nextBlocks);
}
/**
* Verifies there is no more block.
*/
public void noMoreBlock() {
isNotNull();
Assertions.assertThat(nextBlocks.hasNext())
.describedAs("there are still some paragraph. Next one:" + PRINT_FRAGMENT_TEMPLATE,
new Object() {
@Override
public String toString() {
return nextBlocks.next().toString();
}
})
.isFalse();
}
/**
* Verifies the current block as the specified text, ignoring lines.
*/
public HtmlParagraphAssert withText(String text) {
isNotNull();
Assertions.assertThat(actual.text())
.describedAs(PRINT_FRAGMENT_TEMPLATE, actual)
.isEqualTo(text);
return this;
}
/**
* Verifies the current block has all and only the specified lines, in order.
*/
public HtmlParagraphAssert withLines(String firstLine, String... otherLines) {
isNotNull();
List<String> actualLines = toLines(actual);
String[] expectedLines = Stream.concat(
Stream.of(firstLine),
Arrays.stream(otherLines))
.toArray(String[]::new);
Assertions.assertThat(actualLines)
.describedAs(PRINT_FRAGMENT_TEMPLATE, actual)
.containsExactly(expectedLines);
return this;
}
private static List<String> toLines(Element parent) {
Iterator<Node> iterator = parent.childNodes().iterator();
if (!iterator.hasNext()) {
return emptyList();
}
List<String> actualLines = new ArrayList<>(parent.childNodeSize());
StringBuilder currentLine = null;
while (iterator.hasNext()) {
Node node = iterator.next();
if (node instanceof TextNode) {
if (currentLine == null) {
currentLine = new StringBuilder(node.toString());
} else {
currentLine.append(node.toString());
}
} else if (node instanceof Element) {
Element element = (Element) node;
if (element.tagName().equals("br")) {
actualLines.add(currentLine == null ? "" : currentLine.toString());
currentLine = null;
} else {
if (currentLine == null) {
currentLine = new StringBuilder(element.text());
} else {
currentLine.append(element.text());
}
}
} else {
throw new IllegalStateException("unsupported node " + node.getClass());
}
if (!iterator.hasNext()) {
actualLines.add(currentLine == null ? "" : currentLine.toString());
currentLine = null;
}
}
return actualLines;
}
/**
* Convenience method.
* Same as {@code hasList().withItemTexts("foo", "bar")}.
*/
public HtmlListAssert hasList(String firstItemText, String... otherItemsText) {
return hasList()
.withItemTexts(firstItemText, otherItemsText);
}
public HtmlListAssert hasList() {
isNotNull();
Assertions.assertThat(nextBlocks.hasNext())
.describedAs("no more block")
.isTrue();
Element element = nextBlocks.next();
HtmlListAssert.verifyIsList(element);
return new HtmlListAssert(element, nextBlocks);
}
}
|