clang-tools  9.0.0
FormattedStringTests.cpp
Go to the documentation of this file.
1 //===-- FormattedStringTests.cpp ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "FormattedString.h"
9 #include "clang/Basic/LLVM.h"
10 #include "llvm/ADT/StringRef.h"
11 
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
14 
15 namespace clang {
16 namespace clangd {
17 namespace {
18 
19 TEST(FormattedString, Basic) {
20  FormattedString S;
21  EXPECT_EQ(S.renderAsPlainText(), "");
22  EXPECT_EQ(S.renderAsMarkdown(), "");
23 
24  S.appendText("foobar ");
25  S.appendText("baz");
26  EXPECT_EQ(S.renderAsPlainText(), "foobar baz");
27  EXPECT_EQ(S.renderAsMarkdown(), "foobar baz");
28 
29  S = FormattedString();
30  S.appendInlineCode("foobar");
31  EXPECT_EQ(S.renderAsPlainText(), "foobar");
32  EXPECT_EQ(S.renderAsMarkdown(), "`foobar`");
33 
34  S = FormattedString();
35  S.appendCodeBlock("foobar");
36  EXPECT_EQ(S.renderAsPlainText(), "foobar");
37  EXPECT_EQ(S.renderAsMarkdown(), "```cpp\n"
38  "foobar\n"
39  "```\n");
40 }
41 
42 TEST(FormattedString, CodeBlocks) {
43  FormattedString S;
44  S.appendCodeBlock("foobar");
45  S.appendCodeBlock("bazqux", "javascript");
46  S.appendText("after");
47 
48  std::string ExpectedText = R"(foobar
49 
50 bazqux
51 
52 after)";
53  EXPECT_EQ(S.renderAsPlainText(), ExpectedText);
54  std::string ExpectedMarkdown = R"md(```cpp
55 foobar
56 ```
57 ```javascript
58 bazqux
59 ```
60 after)md";
61  EXPECT_EQ(S.renderAsMarkdown(), ExpectedMarkdown);
62 
63  S = FormattedString();
64  S.appendInlineCode("foobar");
65  S.appendInlineCode("bazqux");
66  EXPECT_EQ(S.renderAsPlainText(), "foobar bazqux");
67  EXPECT_EQ(S.renderAsMarkdown(), "`foobar` `bazqux`");
68 
69  S = FormattedString();
70  S.appendText("foo");
71  S.appendInlineCode("bar");
72  S.appendText("baz");
73 
74  EXPECT_EQ(S.renderAsPlainText(), "foo bar baz");
75  EXPECT_EQ(S.renderAsMarkdown(), "foo`bar`baz");
76 }
77 
78 TEST(FormattedString, Escaping) {
79  // Check some ASCII punctuation
80  FormattedString S;
81  S.appendText("*!`");
82  EXPECT_EQ(S.renderAsMarkdown(), "\\*\\!\\`");
83 
84  // Check all ASCII punctuation.
85  S = FormattedString();
86  std::string Punctuation = R"txt(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)txt";
87  // Same text, with each character escaped.
88  std::string EscapedPunctuation;
89  EscapedPunctuation.reserve(2 * Punctuation.size());
90  for (char C : Punctuation)
91  EscapedPunctuation += std::string("\\") + C;
92  S.appendText(Punctuation);
93  EXPECT_EQ(S.renderAsMarkdown(), EscapedPunctuation);
94 
95  // In code blocks we don't need to escape ASCII punctuation.
96  S = FormattedString();
97  S.appendInlineCode("* foo !+ bar * baz");
98  EXPECT_EQ(S.renderAsMarkdown(), "`* foo !+ bar * baz`");
99  S = FormattedString();
100  S.appendCodeBlock("#define FOO\n* foo !+ bar * baz");
101  EXPECT_EQ(S.renderAsMarkdown(), "```cpp\n"
102  "#define FOO\n* foo !+ bar * baz\n"
103  "```\n");
104 
105  // But we have to escape the backticks.
106  S = FormattedString();
107  S.appendInlineCode("foo`bar`baz");
108  EXPECT_EQ(S.renderAsMarkdown(), "`foo``bar``baz`");
109 
110  S = FormattedString();
111  S.appendCodeBlock("foo`bar`baz");
112  EXPECT_EQ(S.renderAsMarkdown(), "```cpp\n"
113  "foo`bar`baz\n"
114  "```\n");
115 
116  // Inline code blocks starting or ending with backticks should add spaces.
117  S = FormattedString();
118  S.appendInlineCode("`foo");
119  EXPECT_EQ(S.renderAsMarkdown(), "` ``foo `");
120  S = FormattedString();
121  S.appendInlineCode("foo`");
122  EXPECT_EQ(S.renderAsMarkdown(), "` foo`` `");
123  S = FormattedString();
124  S.appendInlineCode("`foo`");
125  EXPECT_EQ(S.renderAsMarkdown(), "` ``foo`` `");
126 
127  // Should also add extra spaces if the block stars and ends with spaces.
128  S = FormattedString();
129  S.appendInlineCode(" foo ");
130  EXPECT_EQ(S.renderAsMarkdown(), "` foo `");
131  S = FormattedString();
132  S.appendInlineCode("foo ");
133  EXPECT_EQ(S.renderAsMarkdown(), "`foo `");
134  S = FormattedString();
135  S.appendInlineCode(" foo");
136  EXPECT_EQ(S.renderAsMarkdown(), "` foo`");
137 
138  // Code blocks might need more than 3 backticks.
139  S = FormattedString();
140  S.appendCodeBlock("foobarbaz `\nqux");
141  EXPECT_EQ(S.renderAsMarkdown(), "```cpp\n"
142  "foobarbaz `\nqux\n"
143  "```\n");
144  S = FormattedString();
145  S.appendCodeBlock("foobarbaz ``\nqux");
146  EXPECT_EQ(S.renderAsMarkdown(), "```cpp\n"
147  "foobarbaz ``\nqux\n"
148  "```\n");
149  S = FormattedString();
150  S.appendCodeBlock("foobarbaz ```\nqux");
151  EXPECT_EQ(S.renderAsMarkdown(), "````cpp\n"
152  "foobarbaz ```\nqux\n"
153  "````\n");
154  S = FormattedString();
155  S.appendCodeBlock("foobarbaz ` `` ``` ```` `\nqux");
156  EXPECT_EQ(S.renderAsMarkdown(), "`````cpp\n"
157  "foobarbaz ` `` ``` ```` `\nqux\n"
158  "`````\n");
159 }
160 
161 } // namespace
162 } // namespace clangd
163 } // namespace clang
TEST(BackgroundQueueTest, Priority)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//