• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- HighlighterTest.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 
9 #include "gtest/gtest.h"
10 
11 #include "lldb/Core/Highlighter.h"
12 #include "lldb/Host/FileSystem.h"
13 
14 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
15 #include "Plugins/Language/ObjC/ObjCLanguage.h"
16 #include "Plugins/Language/ObjCPlusPlus/ObjCPlusPlusLanguage.h"
17 #include "TestingSupport/SubsystemRAII.h"
18 
19 using namespace lldb_private;
20 
21 namespace {
22 class HighlighterTest : public testing::Test {
23   SubsystemRAII<FileSystem, CPlusPlusLanguage, ObjCLanguage,
24                 ObjCPlusPlusLanguage>
25       subsystems;
26 };
27 } // namespace
28 
getName(lldb::LanguageType type)29 static std::string getName(lldb::LanguageType type) {
30   HighlighterManager m;
31   return m.getHighlighterFor(type, "").GetName().str();
32 }
33 
getName(llvm::StringRef path)34 static std::string getName(llvm::StringRef path) {
35   HighlighterManager m;
36   return m.getHighlighterFor(lldb::eLanguageTypeUnknown, path).GetName().str();
37 }
38 
TEST_F(HighlighterTest,HighlighterSelectionType)39 TEST_F(HighlighterTest, HighlighterSelectionType) {
40   EXPECT_EQ(getName(lldb::eLanguageTypeC_plus_plus), "clang");
41   EXPECT_EQ(getName(lldb::eLanguageTypeC_plus_plus_03), "clang");
42   EXPECT_EQ(getName(lldb::eLanguageTypeC_plus_plus_11), "clang");
43   EXPECT_EQ(getName(lldb::eLanguageTypeC_plus_plus_14), "clang");
44   EXPECT_EQ(getName(lldb::eLanguageTypeObjC), "clang");
45   EXPECT_EQ(getName(lldb::eLanguageTypeObjC_plus_plus), "clang");
46 
47   EXPECT_EQ(getName(lldb::eLanguageTypeUnknown), "none");
48   EXPECT_EQ(getName(lldb::eLanguageTypeJulia), "none");
49   EXPECT_EQ(getName(lldb::eLanguageTypeHaskell), "none");
50 }
51 
TEST_F(HighlighterTest,HighlighterSelectionPath)52 TEST_F(HighlighterTest, HighlighterSelectionPath) {
53   EXPECT_EQ(getName("myfile.cc"), "clang");
54   EXPECT_EQ(getName("moo.cpp"), "clang");
55   EXPECT_EQ(getName("mar.cxx"), "clang");
56   EXPECT_EQ(getName("foo.C"), "clang");
57   EXPECT_EQ(getName("bar.CC"), "clang");
58   EXPECT_EQ(getName("a/dir.CC"), "clang");
59   EXPECT_EQ(getName("/a/dir.hpp"), "clang");
60   EXPECT_EQ(getName("header.h"), "clang");
61   EXPECT_EQ(getName("foo.m"), "clang");
62   EXPECT_EQ(getName("foo.mm"), "clang");
63 
64   EXPECT_EQ(getName(""), "none");
65   EXPECT_EQ(getName("/dev/null"), "none");
66   EXPECT_EQ(getName("Factory.java"), "none");
67   EXPECT_EQ(getName("poll.py"), "none");
68   EXPECT_EQ(getName("reducer.hs"), "none");
69 }
70 
TEST_F(HighlighterTest,FallbackHighlighter)71 TEST_F(HighlighterTest, FallbackHighlighter) {
72   HighlighterManager mgr;
73   const Highlighter &h =
74       mgr.getHighlighterFor(lldb::eLanguageTypePascal83, "foo.pas");
75 
76   HighlightStyle style;
77   style.identifier.Set("[", "]");
78   style.semicolons.Set("<", ">");
79 
80   const char *code = "program Hello;";
81   std::string output = h.Highlight(style, code, llvm::Optional<size_t>());
82 
83   EXPECT_STREQ(output.c_str(), code);
84 }
85 
86 static std::string
highlightDefault(llvm::StringRef code,HighlightStyle style,llvm::Optional<size_t> cursor=llvm::Optional<size_t> ())87 highlightDefault(llvm::StringRef code, HighlightStyle style,
88                  llvm::Optional<size_t> cursor = llvm::Optional<size_t>()) {
89   HighlighterManager mgr;
90   return mgr.getDefaultHighlighter().Highlight(style, code, cursor);
91 }
92 
TEST_F(HighlighterTest,DefaultHighlighter)93 TEST_F(HighlighterTest, DefaultHighlighter) {
94   const char *code = "int my_main() { return 22; } \n";
95 
96   HighlightStyle style;
97   EXPECT_EQ(code, highlightDefault(code, style));
98 }
99 
TEST_F(HighlighterTest,DefaultHighlighterWithCursor)100 TEST_F(HighlighterTest, DefaultHighlighterWithCursor) {
101   HighlightStyle style;
102   style.selected.Set("<c>", "</c>");
103   EXPECT_EQ("<c>a</c> bc", highlightDefault("a bc", style, 0));
104   EXPECT_EQ("a<c> </c>bc", highlightDefault("a bc", style, 1));
105   EXPECT_EQ("a <c>b</c>c", highlightDefault("a bc", style, 2));
106   EXPECT_EQ("a b<c>c</c>", highlightDefault("a bc", style, 3));
107 }
108 
TEST_F(HighlighterTest,DefaultHighlighterWithCursorOutOfBounds)109 TEST_F(HighlighterTest, DefaultHighlighterWithCursorOutOfBounds) {
110   HighlightStyle style;
111   style.selected.Set("<c>", "</c>");
112   EXPECT_EQ("a bc", highlightDefault("a bc", style, 4));
113 }
114 // Tests highlighting with the Clang highlighter.
115 
116 static std::string
highlightC(llvm::StringRef code,HighlightStyle style,llvm::Optional<size_t> cursor=llvm::Optional<size_t> ())117 highlightC(llvm::StringRef code, HighlightStyle style,
118            llvm::Optional<size_t> cursor = llvm::Optional<size_t>()) {
119   HighlighterManager mgr;
120   const Highlighter &h = mgr.getHighlighterFor(lldb::eLanguageTypeC, "main.c");
121   return h.Highlight(style, code, cursor);
122 }
123 
TEST_F(HighlighterTest,ClangEmptyInput)124 TEST_F(HighlighterTest, ClangEmptyInput) {
125   HighlightStyle s;
126   EXPECT_EQ("", highlightC("", s));
127 }
128 
TEST_F(HighlighterTest,ClangScalarLiterals)129 TEST_F(HighlighterTest, ClangScalarLiterals) {
130   HighlightStyle s;
131   s.scalar_literal.Set("<scalar>", "</scalar>");
132 
133   EXPECT_EQ(" int i = <scalar>22</scalar>;", highlightC(" int i = 22;", s));
134 }
135 
TEST_F(HighlighterTest,ClangStringLiterals)136 TEST_F(HighlighterTest, ClangStringLiterals) {
137   HighlightStyle s;
138   s.string_literal.Set("<str>", "</str>");
139 
140   EXPECT_EQ("const char *f = 22 + <str>\"foo\"</str>;",
141             highlightC("const char *f = 22 + \"foo\";", s));
142 }
143 
TEST_F(HighlighterTest,ClangUnterminatedString)144 TEST_F(HighlighterTest, ClangUnterminatedString) {
145   HighlightStyle s;
146   s.string_literal.Set("<str>", "</str>");
147 
148   EXPECT_EQ(" f = \"", highlightC(" f = \"", s));
149 }
150 
TEST_F(HighlighterTest,Keywords)151 TEST_F(HighlighterTest, Keywords) {
152   HighlightStyle s;
153   s.keyword.Set("<k>", "</k>");
154 
155   EXPECT_EQ(" <k>return</k> 1; ", highlightC(" return 1; ", s));
156 }
157 
TEST_F(HighlighterTest,Colons)158 TEST_F(HighlighterTest, Colons) {
159   HighlightStyle s;
160   s.colon.Set("<c>", "</c>");
161 
162   EXPECT_EQ("foo<c>:</c><c>:</c>bar<c>:</c>", highlightC("foo::bar:", s));
163 }
164 
TEST_F(HighlighterTest,ClangBraces)165 TEST_F(HighlighterTest, ClangBraces) {
166   HighlightStyle s;
167   s.braces.Set("<b>", "</b>");
168 
169   EXPECT_EQ("a<b>{</b><b>}</b>", highlightC("a{}", s));
170 }
171 
TEST_F(HighlighterTest,ClangSquareBrackets)172 TEST_F(HighlighterTest, ClangSquareBrackets) {
173   HighlightStyle s;
174   s.square_brackets.Set("<sb>", "</sb>");
175 
176   EXPECT_EQ("a<sb>[</sb><sb>]</sb>", highlightC("a[]", s));
177 }
178 
TEST_F(HighlighterTest,ClangCommas)179 TEST_F(HighlighterTest, ClangCommas) {
180   HighlightStyle s;
181   s.comma.Set("<comma>", "</comma>");
182 
183   EXPECT_EQ(" bool f = foo()<comma>,</comma> 1;",
184             highlightC(" bool f = foo(), 1;", s));
185 }
186 
TEST_F(HighlighterTest,ClangPPDirectives)187 TEST_F(HighlighterTest, ClangPPDirectives) {
188   HighlightStyle s;
189   s.pp_directive.Set("<pp>", "</pp>");
190 
191   EXPECT_EQ("<pp>#</pp><pp>include</pp><pp> </pp><pp>\"foo\"</pp><pp> </pp>//c",
192             highlightC("#include \"foo\" //c", s));
193 }
194 
TEST_F(HighlighterTest,ClangPreserveNewLine)195 TEST_F(HighlighterTest, ClangPreserveNewLine) {
196   HighlightStyle s;
197   s.comment.Set("<cc>", "</cc>");
198 
199   EXPECT_EQ("<cc>//</cc>\n", highlightC("//\n", s));
200 }
201 
TEST_F(HighlighterTest,ClangTrailingBackslashBeforeNewline)202 TEST_F(HighlighterTest, ClangTrailingBackslashBeforeNewline) {
203   HighlightStyle s;
204 
205   EXPECT_EQ("\\\n", highlightC("\\\n", s));
206   EXPECT_EQ("\\\r\n", highlightC("\\\r\n", s));
207 
208   EXPECT_EQ("#define a \\\n", highlightC("#define a \\\n", s));
209   EXPECT_EQ("#define a \\\r\n", highlightC("#define a \\\r\n", s));
210   EXPECT_EQ("#define a \\\r", highlightC("#define a \\\r", s));
211 }
212 
TEST_F(HighlighterTest,ClangTrailingBackslashWithWhitespace)213 TEST_F(HighlighterTest, ClangTrailingBackslashWithWhitespace) {
214   HighlightStyle s;
215 
216   EXPECT_EQ("\\  \n", highlightC("\\  \n", s));
217   EXPECT_EQ("\\ \t\n", highlightC("\\ \t\n", s));
218   EXPECT_EQ("\\ \n", highlightC("\\ \n", s));
219   EXPECT_EQ("\\\t\n", highlightC("\\\t\n", s));
220 
221   EXPECT_EQ("#define a \\  \n", highlightC("#define a \\  \n", s));
222   EXPECT_EQ("#define a \\ \t\n", highlightC("#define a \\ \t\n", s));
223   EXPECT_EQ("#define a \\ \n", highlightC("#define a \\ \n", s));
224   EXPECT_EQ("#define a \\\t\n", highlightC("#define a \\\t\n", s));
225 }
226 
TEST_F(HighlighterTest,ClangTrailingBackslashMissingNewLine)227 TEST_F(HighlighterTest, ClangTrailingBackslashMissingNewLine) {
228   HighlightStyle s;
229   EXPECT_EQ("\\", highlightC("\\", s));
230   EXPECT_EQ("#define a\\", highlightC("#define a\\", s));
231 }
232 
TEST_F(HighlighterTest,ClangComments)233 TEST_F(HighlighterTest, ClangComments) {
234   HighlightStyle s;
235   s.comment.Set("<cc>", "</cc>");
236 
237   EXPECT_EQ(" <cc>/*com */</cc> <cc>// com /*n*/</cc>",
238             highlightC(" /*com */ // com /*n*/", s));
239 }
240 
TEST_F(HighlighterTest,ClangOperators)241 TEST_F(HighlighterTest, ClangOperators) {
242   HighlightStyle s;
243   s.operators.Set("[", "]");
244 
245   EXPECT_EQ(" 1[+]2[/]a[*]f[&]x[|][~]l", highlightC(" 1+2/a*f&x|~l", s));
246 }
247 
TEST_F(HighlighterTest,ClangIdentifiers)248 TEST_F(HighlighterTest, ClangIdentifiers) {
249   HighlightStyle s;
250   s.identifier.Set("<id>", "</id>");
251 
252   EXPECT_EQ(" <id>foo</id> <id>c</id> = <id>bar</id>(); return 1;",
253             highlightC(" foo c = bar(); return 1;", s));
254 }
255 
TEST_F(HighlighterTest,ClangCursorPos)256 TEST_F(HighlighterTest, ClangCursorPos) {
257   HighlightStyle s;
258   s.selected.Set("<c>", "</c>");
259 
260   EXPECT_EQ("<c> </c>foo c = bar(); return 1;",
261             highlightC(" foo c = bar(); return 1;", s, 0));
262   EXPECT_EQ(" <c>foo</c> c = bar(); return 1;",
263             highlightC(" foo c = bar(); return 1;", s, 1));
264   EXPECT_EQ(" <c>foo</c> c = bar(); return 1;",
265             highlightC(" foo c = bar(); return 1;", s, 2));
266   EXPECT_EQ(" <c>foo</c> c = bar(); return 1;",
267             highlightC(" foo c = bar(); return 1;", s, 3));
268   EXPECT_EQ(" foo<c> </c>c = bar(); return 1;",
269             highlightC(" foo c = bar(); return 1;", s, 4));
270   EXPECT_EQ(" foo <c>c</c> = bar(); return 1;",
271             highlightC(" foo c = bar(); return 1;", s, 5));
272 }
273 
TEST_F(HighlighterTest,ClangCursorPosEndOfLine)274 TEST_F(HighlighterTest, ClangCursorPosEndOfLine) {
275   HighlightStyle s;
276   s.selected.Set("<c>", "</c>");
277 
278   EXPECT_EQ("f", highlightC("f", s, 1));
279 }
280 
TEST_F(HighlighterTest,ClangCursorOutOfBounds)281 TEST_F(HighlighterTest, ClangCursorOutOfBounds) {
282   HighlightStyle s;
283   s.selected.Set("<c>", "</c>");
284   EXPECT_EQ("f", highlightC("f", s, 2));
285   EXPECT_EQ("f", highlightC("f", s, 3));
286   EXPECT_EQ("f", highlightC("f", s, 4));
287 }
288 
TEST_F(HighlighterTest,ClangCursorPosBeforeOtherToken)289 TEST_F(HighlighterTest, ClangCursorPosBeforeOtherToken) {
290   HighlightStyle s;
291   s.selected.Set("<c>", "</c>");
292   s.identifier.Set("<id>", "</id>");
293 
294   EXPECT_EQ("<c> </c><id>foo</id> <id>c</id> = <id>bar</id>(); return 1;",
295             highlightC(" foo c = bar(); return 1;", s, 0));
296 }
297 
TEST_F(HighlighterTest,ClangCursorPosAfterOtherToken)298 TEST_F(HighlighterTest, ClangCursorPosAfterOtherToken) {
299   HighlightStyle s;
300   s.selected.Set("<c>", "</c>");
301   s.identifier.Set("<id>", "</id>");
302 
303   EXPECT_EQ(" <id>foo</id><c> </c><id>c</id> = <id>bar</id>(); return 1;",
304             highlightC(" foo c = bar(); return 1;", s, 4));
305 }
306 
TEST_F(HighlighterTest,ClangCursorPosInOtherToken)307 TEST_F(HighlighterTest, ClangCursorPosInOtherToken) {
308   HighlightStyle s;
309   s.selected.Set("<c>", "</c>");
310   s.identifier.Set("<id>", "</id>");
311 
312   EXPECT_EQ(" <id><c>foo</c></id> <id>c</id> = <id>bar</id>(); return 1;",
313             highlightC(" foo c = bar(); return 1;", s, 1));
314   EXPECT_EQ(" <id><c>foo</c></id> <id>c</id> = <id>bar</id>(); return 1;",
315             highlightC(" foo c = bar(); return 1;", s, 2));
316   EXPECT_EQ(" <id><c>foo</c></id> <id>c</id> = <id>bar</id>(); return 1;",
317             highlightC(" foo c = bar(); return 1;", s, 3));
318 }
319