9#include "clang/Basic/LLVM.h"
10#include "llvm/ADT/StringRef.h"
11#include "gmock/gmock.h"
12#include "gtest/gtest.h"
19std::string escape(llvm::StringRef
Text) {
20 return Paragraph().appendText(
Text.str()).asMarkdown();
24 return testing::ExplainMatchResult(::testing::HasSubstr(std::string{
'\\',
C}),
25 arg, result_listener);
29 return testing::ExplainMatchResult(::testing::Not(::testing::HasSubstr(
"\\")),
30 arg, result_listener);
33TEST(Render, Escaping) {
35 std::string
Punctuation = R
"txt(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)txt";
36 std::string EscapedPunc = R"txt(!"#$%&'()\*+,-./:;<=>?@[\\]^\_\`{|}~)txt";
40 EXPECT_EQ(escape(
"`foo`"), R
"(\`foo\`)");
41 EXPECT_EQ(escape("`foo"), R
"(\`foo)");
42 EXPECT_EQ(escape("foo`"), R
"(foo\`)");
43 EXPECT_EQ(escape("``foo``"), R
"(\`\`foo\`\`)");
45 EXPECT_EQ(escape(
"```"), R
"(\`\`\`)");
46 EXPECT_EQ(escape(
"~~~"), R
"(\~~~)");
49 EXPECT_THAT(escape(
"## Heading"), escaped(
'#'));
50 EXPECT_THAT(escape(
"Foo # bar"), escapedNone());
51 EXPECT_EQ(escape(
"---"), R
"(\---)");
52 EXPECT_EQ(escape("-"), R
"(\-)");
53 EXPECT_EQ(escape("==="), R
"(\===)");
54 EXPECT_EQ(escape("="), R
"(\=)");
55 EXPECT_EQ(escape("***"), R
"(\*\*\*)");
58 EXPECT_THAT(escape(
"<pre"), escaped(
'<'));
59 EXPECT_THAT(escape(
"< pre"), escapedNone());
60 EXPECT_THAT(escape(
"if a<b then"), escaped(
'<'));
61 EXPECT_THAT(escape(
"if a<b then c."), escapedNone());
62 EXPECT_THAT(escape(
"if a<b then c='foo'."), escaped(
'<'));
63 EXPECT_THAT(escape(
"std::vector<T>"), escaped(
'<'));
64 EXPECT_THAT(escape(
"std::vector<std::string>"), escaped(
'<'));
65 EXPECT_THAT(escape(
"std::map<int, int>"), escapedNone());
67 EXPECT_THAT(escape(
"Email <foo@bar.com>"), escapedNone());
68 EXPECT_THAT(escape(
"Website <http://foo.bar>"), escapedNone());
71 EXPECT_THAT(escape(
"- foo"), escaped(
'-'));
72 EXPECT_THAT(escape(
"* foo"), escaped(
'*'));
73 EXPECT_THAT(escape(
"+ foo"), escaped(
'+'));
74 EXPECT_THAT(escape(
"+"), escaped(
'+'));
75 EXPECT_THAT(escape(
"a + foo"), escapedNone());
76 EXPECT_THAT(escape(
"a+ foo"), escapedNone());
77 EXPECT_THAT(escape(
"1. foo"), escaped(
'.'));
78 EXPECT_THAT(escape(
"a. foo"), escapedNone());
81 EXPECT_EQ(escape(
"*foo*"), R
"(\*foo\*)");
82 EXPECT_EQ(escape("**foo**"), R
"(\*\*foo\*\*)");
83 EXPECT_THAT(escape("*foo"), escaped(
'*'));
84 EXPECT_THAT(escape(
"foo *"), escapedNone());
85 EXPECT_THAT(escape(
"foo * bar"), escapedNone());
86 EXPECT_THAT(escape(
"foo_bar"), escapedNone());
87 EXPECT_THAT(escape(
"foo _bar"), escaped(
'_'));
88 EXPECT_THAT(escape(
"foo_ bar"), escaped(
'_'));
89 EXPECT_THAT(escape(
"foo _ bar"), escapedNone());
92 EXPECT_THAT(escape(
"fish &chips;"), escaped(
'&'));
93 EXPECT_THAT(escape(
"fish & chips;"), escapedNone());
94 EXPECT_THAT(escape(
"fish &chips"), escapedNone());
95 EXPECT_THAT(escape(
"foo * bar"), escaped(
'&'));
96 EXPECT_THAT(escape(
"foo ¯ bar"), escaped(
'&'));
97 EXPECT_THAT(escape(
"foo &?; bar"), escapedNone());
100 EXPECT_THAT(escape(
"[foo](bar)"), escaped(
']'));
101 EXPECT_THAT(escape(
"[foo]: bar"), escaped(
']'));
103 EXPECT_THAT(escape(
"[foo][]"), escapedNone());
104 EXPECT_THAT(escape(
"[foo][bar]"), escapedNone());
105 EXPECT_THAT(escape(
"[foo]"), escapedNone());
108 Paragraph P = Paragraph();
109 P.appendCode(
"* foo !+ bar * baz");
110 EXPECT_EQ(P.asMarkdown(),
"`* foo !+ bar * baz`");
114 P.appendCode(
"foo`bar`baz",
true);
115 EXPECT_EQ(P.asMarkdown(),
"`foo``bar``baz`");
117 EXPECT_EQ(P.asPlainText(),
"'foo`bar`baz'");
121 P.appendCode(
"`foo");
122 EXPECT_EQ(P.asMarkdown(),
"` ``foo `");
124 P.appendCode(
"foo`");
125 EXPECT_EQ(P.asMarkdown(),
"` foo`` `");
127 P.appendCode(
"`foo`");
128 EXPECT_EQ(P.asMarkdown(),
"` ``foo`` `");
132 D.addCodeBlock(
"foobarbaz `\nqux");
133 EXPECT_EQ(D.asMarkdown(),
"```cpp\n"
137 D.addCodeBlock(
"foobarbaz ``\nqux");
138 EXPECT_THAT(D.asMarkdown(),
"```cpp\n"
139 "foobarbaz ``\nqux\n"
142 D.addCodeBlock(
"foobarbaz ```\nqux");
143 EXPECT_EQ(D.asMarkdown(),
"````cpp\n"
144 "foobarbaz ```\nqux\n"
147 D.addCodeBlock(
"foobarbaz ` `` ``` ```` `\nqux");
148 EXPECT_EQ(D.asMarkdown(),
"`````cpp\n"
149 "foobarbaz ` `` ``` ```` `\nqux\n"
153TEST(Paragraph, Chunks) {
154 Paragraph P = Paragraph();
155 P.appendText(
"One ");
156 P.appendCode(
"fish");
157 P.appendText(
", two ");
158 P.appendCode(
"fish",
true);
160 EXPECT_EQ(P.asMarkdown(),
"One `fish`, two `fish`");
161 EXPECT_EQ(P.asPlainText(),
"One fish, two `fish`");
164TEST(Paragraph, SeparationOfChunks) {
170 P.appendText(
"after ");
171 EXPECT_EQ(P.asMarkdown(),
"after");
172 EXPECT_EQ(P.asPlainText(),
"after");
174 P.appendCode(
"foobar").appendSpace();
175 EXPECT_EQ(P.asMarkdown(),
"after `foobar`");
176 EXPECT_EQ(P.asPlainText(),
"after foobar");
179 EXPECT_EQ(P.asMarkdown(),
"after `foobar` bat");
180 EXPECT_EQ(P.asPlainText(),
"after foobar bat");
182 P.appendCode(
"no").appendCode(
"space");
183 EXPECT_EQ(P.asMarkdown(),
"after `foobar` bat`no` `space`");
184 EXPECT_EQ(P.asPlainText(),
"after foobar batno space");
187TEST(Paragraph, ExtraSpaces) {
190 P.appendText(
"foo\n \t baz");
191 P.appendCode(
" bar\n");
192 EXPECT_EQ(P.asMarkdown(),
"foo baz`bar`");
193 EXPECT_EQ(P.asPlainText(),
"foo bazbar");
196TEST(Paragraph, SpacesCollapsed) {
198 P.appendText(
" foo bar ");
199 P.appendText(
" baz ");
200 EXPECT_EQ(P.asMarkdown(),
"foo bar baz");
201 EXPECT_EQ(P.asPlainText(),
"foo bar baz");
204TEST(Paragraph, NewLines) {
207 P.appendText(
" \n foo\nbar\n ");
208 P.appendCode(
" \n foo\nbar \n ");
209 EXPECT_EQ(P.asMarkdown(),
"foo bar `foo bar`");
210 EXPECT_EQ(P.asPlainText(),
"foo bar foo bar");
213TEST(Document, Separators) {
215 D.addParagraph().appendText(
"foo");
216 D.addCodeBlock(
"test");
217 D.addParagraph().appendText(
"bar");
219 const char ExpectedMarkdown[] = R
"md(foo
224 EXPECT_EQ(D.asMarkdown(), ExpectedMarkdown);
226 const char ExpectedText[] = R
"pt(foo
231 EXPECT_EQ(D.asPlainText(), ExpectedText);
234TEST(Document, Ruler) {
236 D.addParagraph().appendText("foo");
240 D.addParagraph().appendText(
"bar");
241 EXPECT_EQ(D.asMarkdown(),
"foo \n\n---\nbar");
242 EXPECT_EQ(D.asPlainText(),
"foo\n\nbar");
245 D.addParagraph().appendText(
"foo");
247 D.addCodeBlock(
"bar");
249 EXPECT_EQ(D.asMarkdown(),
"foo \n\n---\n```cpp\nbar\n```");
250 EXPECT_EQ(D.asPlainText(),
"foo\n\nbar");
254 D.addParagraph().appendText(
"foo");
257 EXPECT_EQ(D.asMarkdown(),
"foo");
258 EXPECT_EQ(D.asPlainText(),
"foo");
262 D.addParagraph().appendText(
"foo");
263 EXPECT_EQ(D.asMarkdown(),
"foo \n\n---\nfoo");
264 EXPECT_EQ(D.asPlainText(),
"foo\n\nfoo");
267TEST(Document, Append) {
269 D.addParagraph().appendText(
"foo");
273 E.addParagraph().appendText(
"bar");
274 D.append(std::move(
E));
275 EXPECT_EQ(D.asMarkdown(),
"foo \n\n---\nbar");
278TEST(Document, Heading) {
280 D.addHeading(1).appendText(
"foo");
281 D.addHeading(2).appendText(
"bar");
282 D.addParagraph().appendText(
"baz");
283 EXPECT_EQ(D.asMarkdown(),
"# foo \n## bar \nbaz");
284 EXPECT_EQ(D.asPlainText(),
"foo\nbar\nbaz");
287TEST(CodeBlock, Render) {
290 D.addCodeBlock(
"foo\n bar\n baz");
292 llvm::StringRef ExpectedMarkdown =
298 llvm::StringRef ExpectedPlainText =
302 EXPECT_EQ(D.asMarkdown(), ExpectedMarkdown);
303 EXPECT_EQ(D.asPlainText(), ExpectedPlainText);
304 D.addCodeBlock("foo");
314 EXPECT_EQ(D.asMarkdown(), ExpectedMarkdown);
321 EXPECT_EQ(D.asPlainText(), ExpectedPlainText);
324TEST(BulletList, Render) {
327 L.addItem().addParagraph().appendText(
"foo");
328 EXPECT_EQ(L.asMarkdown(),
"- foo");
329 EXPECT_EQ(L.asPlainText(),
"- foo");
331 L.addItem().addParagraph().appendText(
"bar");
332 llvm::StringRef
Expected = R
"md(- foo
334 EXPECT_EQ(L.asMarkdown(), Expected);
335 EXPECT_EQ(L.asPlainText(), Expected);
338 Document &D = L.addItem();
340 D.addParagraph().appendText(
"foo");
341 D.addParagraph().appendText(
"baz");
344 Document &
Inner = D.addBulletList().addItem();
345 Inner.addParagraph().appendText(
"foo");
348 BulletList &InnerList =
Inner.addBulletList();
350 Document &DeepDoc = InnerList.addItem();
351 DeepDoc.addParagraph().appendText(
"baz");
352 DeepDoc.addParagraph().appendText(
"baz");
353 StringRef ExpectedMarkdown = R
"md(- foo
360 EXPECT_EQ(L.asMarkdown(), ExpectedMarkdown);
361 StringRef ExpectedPlainText = R"pt(- foo
368 EXPECT_EQ(L.asPlainText(), ExpectedPlainText);
371 Inner.addParagraph().appendText(
"after");
372 ExpectedMarkdown = R
"md(- foo
381 EXPECT_EQ(L.asMarkdown(), ExpectedMarkdown);
382 ExpectedPlainText = R"pt(- foo
390 EXPECT_EQ(L.asPlainText(), ExpectedPlainText);
std::pair< Context, Canceler > Inner
std::vector< const char * > Expected
TEST(BackgroundQueueTest, Priority)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//