FormattedStringTests.cpp
7.34 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//===-- FormattedStringTests.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "FormattedString.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
namespace markup {
namespace {
TEST(Render, Escaping) {
// Check some ASCII punctuation
Paragraph P;
P.appendText("*!`");
EXPECT_EQ(P.asMarkdown(), "\\*\\!\\`");
// Check all ASCII punctuation.
P = Paragraph();
std::string Punctuation = R"txt(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)txt";
// Same text, with each character escaped.
std::string EscapedPunctuation;
EscapedPunctuation.reserve(2 * Punctuation.size());
for (char C : Punctuation)
EscapedPunctuation += std::string("\\") + C;
P.appendText(Punctuation);
EXPECT_EQ(P.asMarkdown(), EscapedPunctuation);
// In code blocks we don't need to escape ASCII punctuation.
P = Paragraph();
P.appendCode("* foo !+ bar * baz");
EXPECT_EQ(P.asMarkdown(), "`* foo !+ bar * baz`");
// But we have to escape the backticks.
P = Paragraph();
P.appendCode("foo`bar`baz");
EXPECT_EQ(P.asMarkdown(), "`foo``bar``baz`");
// Inline code blocks starting or ending with backticks should add spaces.
P = Paragraph();
P.appendCode("`foo");
EXPECT_EQ(P.asMarkdown(), "` ``foo `");
P = Paragraph();
P.appendCode("foo`");
EXPECT_EQ(P.asMarkdown(), "` foo`` `");
P = Paragraph();
P.appendCode("`foo`");
EXPECT_EQ(P.asMarkdown(), "` ``foo`` `");
// Code blocks might need more than 3 backticks.
Document D;
D.addCodeBlock("foobarbaz `\nqux");
EXPECT_EQ(D.asMarkdown(), "```cpp\n"
"foobarbaz `\nqux\n"
"```");
D = Document();
D.addCodeBlock("foobarbaz ``\nqux");
EXPECT_THAT(D.asMarkdown(), "```cpp\n"
"foobarbaz ``\nqux\n"
"```");
D = Document();
D.addCodeBlock("foobarbaz ```\nqux");
EXPECT_EQ(D.asMarkdown(), "````cpp\n"
"foobarbaz ```\nqux\n"
"````");
D = Document();
D.addCodeBlock("foobarbaz ` `` ``` ```` `\nqux");
EXPECT_EQ(D.asMarkdown(), "`````cpp\n"
"foobarbaz ` `` ``` ```` `\nqux\n"
"`````");
}
TEST(Paragraph, SeparationOfChunks) {
// This test keeps appending contents to a single Paragraph and checks
// expected accumulated contents after each one.
// Purpose is to check for separation between different chunks.
Paragraph P;
P.appendText("after");
EXPECT_EQ(P.asMarkdown(), "after");
EXPECT_EQ(P.asPlainText(), "after");
P.appendCode("foobar");
EXPECT_EQ(P.asMarkdown(), "after `foobar`");
EXPECT_EQ(P.asPlainText(), "after foobar");
P.appendText("bat");
EXPECT_EQ(P.asMarkdown(), "after `foobar` bat");
EXPECT_EQ(P.asPlainText(), "after foobar bat");
}
TEST(Paragraph, ExtraSpaces) {
// Make sure spaces inside chunks are dropped.
Paragraph P;
P.appendText("foo\n \t baz");
P.appendCode(" bar\n");
EXPECT_EQ(P.asMarkdown(), "foo baz `bar`");
EXPECT_EQ(P.asPlainText(), "foo baz bar");
}
TEST(Paragraph, NewLines) {
// New lines before and after chunks are dropped.
Paragraph P;
P.appendText(" \n foo\nbar\n ");
P.appendCode(" \n foo\nbar \n ");
EXPECT_EQ(P.asMarkdown(), "foo bar `foo bar`");
EXPECT_EQ(P.asPlainText(), "foo bar foo bar");
}
TEST(Document, Separators) {
Document D;
D.addParagraph().appendText("foo");
D.addCodeBlock("test");
D.addParagraph().appendText("bar");
const char ExpectedMarkdown[] = R"md(foo
```cpp
test
```
bar)md";
EXPECT_EQ(D.asMarkdown(), ExpectedMarkdown);
const char ExpectedText[] = R"pt(foo
test
bar)pt";
EXPECT_EQ(D.asPlainText(), ExpectedText);
}
TEST(Document, Ruler) {
Document D;
D.addParagraph().appendText("foo");
D.addRuler();
// Ruler followed by paragraph.
D.addParagraph().appendText("bar");
EXPECT_EQ(D.asMarkdown(), "foo \n\n---\nbar");
EXPECT_EQ(D.asPlainText(), "foo\n\nbar");
D = Document();
D.addParagraph().appendText("foo");
D.addRuler();
D.addCodeBlock("bar");
// Ruler followed by a codeblock.
EXPECT_EQ(D.asMarkdown(), "foo \n\n---\n```cpp\nbar\n```");
EXPECT_EQ(D.asPlainText(), "foo\n\nbar");
// Ruler followed by another ruler
D = Document();
D.addParagraph().appendText("foo");
D.addRuler();
D.addRuler();
EXPECT_EQ(D.asMarkdown(), "foo");
EXPECT_EQ(D.asPlainText(), "foo");
// Multiple rulers between blocks
D.addRuler();
D.addParagraph().appendText("foo");
EXPECT_EQ(D.asMarkdown(), "foo \n\n---\nfoo");
EXPECT_EQ(D.asPlainText(), "foo\n\nfoo");
}
TEST(Document, Heading) {
Document D;
D.addHeading(1).appendText("foo");
D.addHeading(2).appendText("bar");
D.addParagraph().appendText("baz");
EXPECT_EQ(D.asMarkdown(), "# foo \n## bar \nbaz");
EXPECT_EQ(D.asPlainText(), "foo\nbar\nbaz");
}
TEST(CodeBlock, Render) {
Document D;
// Code blocks preserves any extra spaces.
D.addCodeBlock("foo\n bar\n baz");
llvm::StringRef ExpectedMarkdown =
R"md(```cpp
foo
bar
baz
```)md";
llvm::StringRef ExpectedPlainText =
R"pt(foo
bar
baz)pt";
EXPECT_EQ(D.asMarkdown(), ExpectedMarkdown);
EXPECT_EQ(D.asPlainText(), ExpectedPlainText);
D.addCodeBlock("foo");
ExpectedMarkdown =
R"md(```cpp
foo
bar
baz
```
```cpp
foo
```)md";
EXPECT_EQ(D.asMarkdown(), ExpectedMarkdown);
ExpectedPlainText =
R"pt(foo
bar
baz
foo)pt";
EXPECT_EQ(D.asPlainText(), ExpectedPlainText);
}
TEST(BulletList, Render) {
BulletList L;
// Flat list
L.addItem().addParagraph().appendText("foo");
EXPECT_EQ(L.asMarkdown(), "- foo");
EXPECT_EQ(L.asPlainText(), "- foo");
L.addItem().addParagraph().appendText("bar");
llvm::StringRef Expected = R"md(- foo
- bar)md";
EXPECT_EQ(L.asMarkdown(), Expected);
EXPECT_EQ(L.asPlainText(), Expected);
// Nested list, with a single item.
Document &D = L.addItem();
// First item with foo\nbaz
D.addParagraph().appendText("foo");
D.addParagraph().appendText("baz");
// Nest one level.
Document &Inner = D.addBulletList().addItem();
Inner.addParagraph().appendText("foo");
// Nest one more level.
BulletList &InnerList = Inner.addBulletList();
// Single item, baz\nbaz
Document &DeepDoc = InnerList.addItem();
DeepDoc.addParagraph().appendText("baz");
DeepDoc.addParagraph().appendText("baz");
StringRef ExpectedMarkdown = R"md(- foo
- bar
- foo
baz
- foo
- baz
baz)md";
EXPECT_EQ(L.asMarkdown(), ExpectedMarkdown);
StringRef ExpectedPlainText = R"pt(- foo
- bar
- foo
baz
- foo
- baz
baz)pt";
EXPECT_EQ(L.asPlainText(), ExpectedPlainText);
// Termination
Inner.addParagraph().appendText("after");
ExpectedMarkdown = R"md(- foo
- bar
- foo
baz
- foo
- baz
baz
after)md";
EXPECT_EQ(L.asMarkdown(), ExpectedMarkdown);
ExpectedPlainText = R"pt(- foo
- bar
- foo
baz
- foo
- baz
baz
after)pt";
EXPECT_EQ(L.asPlainText(), ExpectedPlainText);
}
} // namespace
} // namespace markup
} // namespace clangd
} // namespace clang