1 #include "ClangTidyTest.h"
2 #include "readability/BracesAroundStatementsCheck.h"
3 #include "readability/NamespaceCommentCheck.h"
4 #include "readability/SimplifyBooleanExprCheck.h"
5 #include "gtest/gtest.h"
11 using readability::BracesAroundStatementsCheck
;
12 using readability::NamespaceCommentCheck
;
13 using readability::SimplifyBooleanExprCheck
;
15 TEST(NamespaceCommentCheckTest
, Basic
) {
16 EXPECT_EQ("namespace i {\n} // namespace i",
17 runCheckOnCode
<NamespaceCommentCheck
>("namespace i {\n}"));
18 EXPECT_EQ("namespace {\n} // namespace",
19 runCheckOnCode
<NamespaceCommentCheck
>("namespace {\n}"));
20 EXPECT_EQ("namespace i { namespace j {\n} // namespace j\n } // namespace i",
21 runCheckOnCode
<NamespaceCommentCheck
>(
22 "namespace i { namespace j {\n} }"));
25 TEST(NamespaceCommentCheckTest
, SingleLineNamespaces
) {
27 "namespace i { namespace j { } }",
28 runCheckOnCode
<NamespaceCommentCheck
>("namespace i { namespace j { } }"));
31 TEST(NamespaceCommentCheckTest
, CheckExistingComments
) {
32 EXPECT_EQ("namespace i { namespace j {\n"
33 "} /* namespace j */ } // namespace i\n"
34 " /* random comment */",
35 runCheckOnCode
<NamespaceCommentCheck
>(
36 "namespace i { namespace j {\n"
37 "} /* namespace j */ } /* random comment */"));
38 EXPECT_EQ("namespace {\n"
40 runCheckOnCode
<NamespaceCommentCheck
>("namespace {\n"
42 EXPECT_EQ("namespace {\n"
44 runCheckOnCode
<NamespaceCommentCheck
>("namespace {\n"
46 EXPECT_EQ("namespace {\n"
47 "} // anonymous namespace",
48 runCheckOnCode
<NamespaceCommentCheck
>("namespace {\n"
49 "} // anonymous namespace"));
50 EXPECT_EQ("namespace {\n"
51 "} // Anonymous namespace.",
52 runCheckOnCode
<NamespaceCommentCheck
>("namespace {\n"
53 "} // Anonymous namespace."));
57 runCheckOnCode
<NamespaceCommentCheck
>("namespace q {\n"
58 "} // anonymous namespace q"));
60 "namespace My_NameSpace123 {\n"
61 "} // namespace My_NameSpace123",
62 runCheckOnCode
<NamespaceCommentCheck
>("namespace My_NameSpace123 {\n"
63 "} // namespace My_NameSpace123"));
65 "namespace My_NameSpace123 {\n"
66 "} //namespace My_NameSpace123",
67 runCheckOnCode
<NamespaceCommentCheck
>("namespace My_NameSpace123 {\n"
68 "} //namespace My_NameSpace123"));
69 EXPECT_EQ("namespace My_NameSpace123 {\n"
70 "} // end namespace My_NameSpace123",
71 runCheckOnCode
<NamespaceCommentCheck
>(
72 "namespace My_NameSpace123 {\n"
73 "} // end namespace My_NameSpace123"));
74 // Understand comments only on the same line.
75 EXPECT_EQ("namespace {\n"
78 runCheckOnCode
<NamespaceCommentCheck
>("namespace {\n"
83 TEST(NamespaceCommentCheckTest
, FixWrongComments
) {
84 EXPECT_EQ("namespace i { namespace jJ0_ {\n"
85 "} // namespace jJ0_\n"
87 " /* random comment */",
88 runCheckOnCode
<NamespaceCommentCheck
>(
89 "namespace i { namespace jJ0_ {\n"
90 "} /* namespace qqq */ } /* random comment */"));
91 EXPECT_EQ("namespace {\n"
93 runCheckOnCode
<NamespaceCommentCheck
>("namespace {\n"
94 "} // namespace asdf"));
95 // Remove unknown line comments. These are likely to be an unrecognized form
96 // of a namespace ending comment.
97 EXPECT_EQ("namespace {\n"
99 runCheckOnCode
<NamespaceCommentCheck
>("namespace {\n"
100 "} // random text"));
103 TEST(BracesAroundStatementsCheckTest
, IfWithComments
) {
104 EXPECT_EQ("int main() {\n"
105 " if (false /*dummy token*/) {\n"
110 " return -1; // comment\n"
114 "}/* multi-line \n comment */\n"
116 runCheckOnCode
<BracesAroundStatementsCheck
>(
118 " if (false /*dummy token*/)\n"
122 " return -1; // comment\n"
124 " return -1; /* multi-line \n comment */\n"
126 EXPECT_EQ("int main() {\n"
127 " if (false /*dummy token*/) {\n"
129 " return -1 /**/ ;\n"
132 runCheckOnCode
<BracesAroundStatementsCheck
>(
134 " if (false /*dummy token*/)\n"
136 " return -1 /**/ ;\n"
140 TEST(BracesAroundStatementsCheckTest
, If
) {
141 EXPECT_NO_CHANGES(BracesAroundStatementsCheck
, "int main() {\n"
146 EXPECT_NO_CHANGES(BracesAroundStatementsCheck
, "int main() {\n"
147 " if (auto Cond = false) {\n"
151 EXPECT_NO_CHANGES(BracesAroundStatementsCheck
, "int main() {\n"
158 EXPECT_EQ("int main() {\n"
163 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
167 EXPECT_EQ("int main() {\n"
168 " if (auto Cond = false /**/ ) {\n"
172 runCheckOnCode
<BracesAroundStatementsCheck
>(
174 " if (auto Cond = false /**/ )\n"
177 // FIXME: Consider adding braces before EMPTY_MACRO and after the statement.
178 EXPECT_NO_CHANGES(BracesAroundStatementsCheck
,
179 "#define EMPTY_MACRO\n"
181 " if (auto Cond = false EMPTY_MACRO /**/ ) EMPTY_MACRO\n"
184 EXPECT_EQ("int main() {\n"
185 " if (true) { return -1/**/ ;\n"
188 runCheckOnCode
<BracesAroundStatementsCheck
>(
190 " if (true) return -1/**/ ;\n"
192 EXPECT_EQ("int main() {\n"
199 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
205 EXPECT_EQ("int main() {\n"
208 " } else if (1 == 2) {\n"
214 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
217 " else if (1 == 2)\n"
222 EXPECT_EQ("int main() {\n"
225 " } else if (1 == 2) {\n"
231 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
234 " else if (1 == 2) {\n"
241 TEST(BracesAroundStatementsCheckTest
, IfElseWithShortStatements
) {
242 ClangTidyOptions Options
;
243 Options
.CheckOptions
["test-check-0.ShortStatementLines"] = "1";
245 EXPECT_EQ("int main() {\n"
246 " if (true) return 1;\n"
247 " if (false) { return -1;\n"
248 " } else if (1 == 2) { return -2;\n"
249 " } else { return -3;\n"
252 runCheckOnCode
<BracesAroundStatementsCheck
>(
254 " if (true) return 1;\n"
255 " if (false) return -1;\n"
256 " else if (1 == 2) return -2;\n"
259 nullptr, "input.cc", std::nullopt
, Options
));
261 // If the last else is an else-if, we also force it.
262 EXPECT_EQ("int main() {\n"
263 " if (false) { return -1;\n"
264 " } else if (1 == 2) { return -2;\n"
267 runCheckOnCode
<BracesAroundStatementsCheck
>(
269 " if (false) return -1;\n"
270 " else if (1 == 2) return -2;\n"
272 nullptr, "input.cc", std::nullopt
, Options
));
275 TEST(BracesAroundStatementsCheckTest
, For
) {
276 EXPECT_NO_CHANGES(BracesAroundStatementsCheck
, "int main() {\n"
281 EXPECT_EQ("int main() {\n"
286 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
290 EXPECT_EQ("int main() {\n"
295 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
299 EXPECT_EQ("int main() {\n"
301 " return -1 /**/ ;\n"
304 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
306 " return -1 /**/ ;\n"
310 TEST(BracesAroundStatementsCheckTest
, ForRange
) {
311 EXPECT_NO_CHANGES(BracesAroundStatementsCheck
, "int main() {\n"
313 " for (int i : arr) {\n"
317 EXPECT_EQ("int main() {\n"
319 " for (int i : arr) {\n"
322 " for (int i : arr) {\n"
326 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
328 " for (int i : arr)\n"
330 " for (int i : arr)\n"
335 TEST(BracesAroundStatementsCheckTest
, DoWhile
) {
336 EXPECT_NO_CHANGES(BracesAroundStatementsCheck
, "int main() {\n"
339 " } while (false);\n"
341 EXPECT_EQ("int main() {\n"
344 " } while (false);\n"
346 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
353 TEST(BracesAroundStatementsCheckTest
, While
) {
354 EXPECT_NO_CHANGES(BracesAroundStatementsCheck
, "int main() {\n"
359 EXPECT_EQ("int main() {\n"
364 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
368 EXPECT_EQ("int main() {\n"
369 " while (auto Cond = false) {\n"
373 runCheckOnCode
<BracesAroundStatementsCheck
>(
375 " while (auto Cond = false)\n"
378 EXPECT_EQ("int main() {\n"
379 " while (false /*dummy token*/) {\n"
383 runCheckOnCode
<BracesAroundStatementsCheck
>(
385 " while (false /*dummy token*/)\n"
388 EXPECT_EQ("int main() {\n"
393 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
397 EXPECT_EQ("int main() {\n"
402 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
406 EXPECT_EQ("int main() {\n"
411 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
417 TEST(BracesAroundStatementsCheckTest
, Nested
) {
418 EXPECT_EQ("int main() {\n"
419 " do { if (true) {}} while (false);\n"
421 runCheckOnCode
<BracesAroundStatementsCheck
>(
423 " do if (true) {}while (false);\n"
425 EXPECT_EQ("int main() {\n"
426 " do { if (true) {}} while (false);\n"
428 runCheckOnCode
<BracesAroundStatementsCheck
>(
430 " do if (true) {}while (false);\n"
438 " /**/ ; // comment\n"
442 runCheckOnCode
<BracesAroundStatementsCheck
>("int main() {\n"
447 " /**/ ; // comment\n"
452 TEST(BracesAroundStatementsCheckTest
, Macros
) {
453 EXPECT_NO_CHANGES(BracesAroundStatementsCheck
,
454 "#define IF(COND) if (COND) return -1;\n"
458 EXPECT_NO_CHANGES(BracesAroundStatementsCheck
,
459 "#define FOR(COND) for (COND) return -1;\n"
463 EXPECT_EQ("#define DO_IT ++i\n"
468 " } else if (1 == 2) {\n"
474 runCheckOnCode
<BracesAroundStatementsCheck
>("#define DO_IT ++i\n"
479 " else if (1 == 2)\n"
486 #define EXPECT_NO_CHANGES_WITH_OPTS(Check, Opts, Code) \
487 EXPECT_EQ(Code, runCheckOnCode<Check>(Code, nullptr, "input.cc", \
489 TEST(BracesAroundStatementsCheckTest
, ImplicitCastInReturn
) {
490 ClangTidyOptions Opts
;
491 Opts
.CheckOptions
["test-check-0.ShortStatementLines"] = "1";
493 StringRef Input
= "const char *f() {\n"
494 " if (true) return \"\";\n"
497 EXPECT_NO_CHANGES_WITH_OPTS(BracesAroundStatementsCheck
, Opts
, Input
);
498 EXPECT_EQ("const char *f() {\n"
499 " if (true) { return \"\";\n"
503 runCheckOnCode
<BracesAroundStatementsCheck
>(Input
));
506 TEST(SimplifyBooleanExprCheckTest
, CodeWithError
) {
508 // Need to downgrade Wreturn-type from error as runCheckOnCode will fatal_exit
509 // if any errors occur.
510 EXPECT_EQ("void foo(bool b){ return b; }",
511 runCheckOnCode
<SimplifyBooleanExprCheck
>(
512 "void foo(bool b){ if (b) return true; return false; }",
513 nullptr, "input.cc", {"-Wno-error=return-type"}));