Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "format-test" |
| 11 | |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 12 | #include "clang/Format/Format.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 13 | #include "../Tooling/RewriterTestContext.h" |
Chandler Carruth | b1ba0ef | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 14 | #include "clang/Lex/Lexer.h" |
| 15 | #include "llvm/Support/Debug.h" |
| 16 | #include "gtest/gtest.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 17 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 18 | namespace clang { |
| 19 | namespace format { |
| 20 | |
| 21 | class FormatTest : public ::testing::Test { |
| 22 | protected: |
| 23 | std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, |
| 24 | const FormatStyle &Style) { |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 25 | DEBUG(llvm::errs() << "---\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 26 | RewriterTestContext Context; |
| 27 | FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 28 | SourceLocation Start = |
| 29 | Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); |
| 30 | std::vector<CharSourceRange> Ranges( |
| 31 | 1, |
| 32 | CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 33 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, |
| 34 | getFormattingLangOpts()); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 35 | tooling::Replacements Replace = reformat( |
| 36 | Style, Lex, Context.Sources, Ranges, new IgnoringDiagConsumer()); |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 37 | ReplacementCount = Replace.size(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 38 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 39 | DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 40 | return Context.getRewrittenText(ID); |
| 41 | } |
| 42 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 43 | std::string |
| 44 | format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 45 | return format(Code, 0, Code.size(), Style); |
| 46 | } |
| 47 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 48 | std::string messUp(llvm::StringRef Code) { |
| 49 | std::string MessedUp(Code.str()); |
| 50 | bool InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 51 | bool InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 52 | bool JustReplacedNewline = false; |
| 53 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 54 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 55 | if (JustReplacedNewline) |
| 56 | MessedUp[i - 1] = '\n'; |
| 57 | InComment = true; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 58 | } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) { |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 59 | if (i != 0) |
| 60 | MessedUp[i - 1] = '\n'; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 61 | InPreprocessorDirective = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 62 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 63 | MessedUp[i] = ' '; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 64 | MessedUp[i + 1] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 65 | } else if (MessedUp[i] == '\n') { |
| 66 | if (InComment) { |
| 67 | InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 68 | } else if (InPreprocessorDirective) { |
| 69 | InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 70 | } else { |
| 71 | JustReplacedNewline = true; |
| 72 | MessedUp[i] = ' '; |
| 73 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 74 | } else if (MessedUp[i] != ' ') { |
| 75 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 76 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 77 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 78 | return MessedUp; |
| 79 | } |
| 80 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 81 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 82 | FormatStyle Style = getLLVMStyle(); |
| 83 | Style.ColumnLimit = ColumnLimit; |
| 84 | return Style; |
| 85 | } |
| 86 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 87 | FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { |
| 88 | FormatStyle Style = getGoogleStyle(); |
| 89 | Style.ColumnLimit = ColumnLimit; |
| 90 | return Style; |
| 91 | } |
| 92 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 93 | void verifyFormat(llvm::StringRef Code, |
| 94 | const FormatStyle &Style = getLLVMStyle()) { |
| 95 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 99 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 100 | } |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 101 | |
| 102 | void verifyIndependentOfContext(llvm::StringRef text) { |
| 103 | verifyFormat(text); |
| 104 | verifyFormat(llvm::Twine("void f() { " + text + " }").str()); |
| 105 | } |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 106 | |
| 107 | int ReplacementCount; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 110 | TEST_F(FormatTest, MessUp) { |
| 111 | EXPECT_EQ("1 2 3", messUp("1 2 3")); |
| 112 | EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); |
| 113 | EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); |
| 114 | EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); |
| 115 | EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); |
| 116 | } |
| 117 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 118 | //===----------------------------------------------------------------------===// |
| 119 | // Basic function tests. |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 122 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 123 | EXPECT_EQ(";", format(";")); |
| 124 | } |
| 125 | |
| 126 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 127 | EXPECT_EQ("int i;", format(" int i;")); |
| 128 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 129 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 130 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 131 | } |
| 132 | |
| 133 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 134 | EXPECT_EQ("int i;", format("int\ni;")); |
| 135 | } |
| 136 | |
| 137 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 138 | EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 141 | TEST_F(FormatTest, FormatsNestedCall) { |
| 142 | verifyFormat("Method(f1, f2(f3));"); |
| 143 | verifyFormat("Method(f1(f2, f3()));"); |
Daniel Jasper | 4e9008a | 2013-01-13 08:19:51 +0000 | [diff] [blame] | 144 | verifyFormat("Method(f1(f2, (f3())));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Daniel Jasper | daf1a15 | 2013-02-07 21:08:36 +0000 | [diff] [blame] | 147 | TEST_F(FormatTest, NestedNameSpecifiers) { |
Daniel Jasper | 6b825c2 | 2013-01-16 07:19:28 +0000 | [diff] [blame] | 148 | verifyFormat("vector< ::Type> v;"); |
Daniel Jasper | daf1a15 | 2013-02-07 21:08:36 +0000 | [diff] [blame] | 149 | verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); |
Daniel Jasper | 6b825c2 | 2013-01-16 07:19:28 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 152 | TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { |
| 153 | EXPECT_EQ("if (a) {\n" |
| 154 | " f();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 155 | "}", |
| 156 | format("if(a){f();}")); |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 157 | EXPECT_EQ(4, ReplacementCount); |
| 158 | EXPECT_EQ("if (a) {\n" |
| 159 | " f();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 160 | "}", |
| 161 | format("if (a) {\n" |
| 162 | " f();\n" |
| 163 | "}")); |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 164 | EXPECT_EQ(0, ReplacementCount); |
| 165 | } |
| 166 | |
Daniel Jasper | c78c6b3 | 2013-02-14 09:58:41 +0000 | [diff] [blame] | 167 | TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) { |
| 168 | EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle())); |
| 169 | } |
| 170 | |
Daniel Jasper | 2933316 | 2013-02-18 13:08:03 +0000 | [diff] [blame] | 171 | TEST_F(FormatTest, ReformatsMovedLines) { |
| 172 | EXPECT_EQ( |
| 173 | "template <typename T> T *getFETokenInfo() const {\n" |
| 174 | " return static_cast<T *>(FETokenInfo);\n" |
| 175 | "}\n" |
| 176 | " int a; // <- Should not be formatted", |
| 177 | format( |
| 178 | "template<typename T>\n" |
| 179 | "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" |
| 180 | " int a; // <- Should not be formatted", |
| 181 | 9, 5, getLLVMStyle())); |
| 182 | } |
| 183 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 184 | //===----------------------------------------------------------------------===// |
| 185 | // Tests for control statements. |
| 186 | //===----------------------------------------------------------------------===// |
| 187 | |
| 188 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 189 | verifyFormat("if (true)\n f();\ng();"); |
| 190 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 191 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 192 | |
| 193 | FormatStyle AllowsMergedIf = getGoogleStyle(); |
| 194 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; |
| 195 | verifyFormat("if (a)\n" |
| 196 | " // comment\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 197 | " f();", |
| 198 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 199 | |
| 200 | verifyFormat("if (a) // Can't merge this\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 201 | " f();\n", |
| 202 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 203 | verifyFormat("if (a) /* still don't merge */\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 204 | " f();", |
| 205 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 206 | verifyFormat("if (a) { // Never merge this\n" |
| 207 | " f();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 208 | "}", |
| 209 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 210 | verifyFormat("if (a) { /* Never merge this */\n" |
| 211 | " f();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 212 | "}", |
| 213 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 214 | |
| 215 | AllowsMergedIf.ColumnLimit = 14; |
| 216 | verifyFormat("if (a) return;", AllowsMergedIf); |
Daniel Jasper | fd0ca97 | 2013-01-14 16:02:06 +0000 | [diff] [blame] | 217 | verifyFormat("if (aaaaaaaaa)\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 218 | " return;", |
| 219 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 220 | |
| 221 | AllowsMergedIf.ColumnLimit = 13; |
| 222 | verifyFormat("if (a)\n return;", AllowsMergedIf); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | TEST_F(FormatTest, ParseIfElse) { |
| 226 | verifyFormat("if (true)\n" |
| 227 | " if (true)\n" |
| 228 | " if (true)\n" |
| 229 | " f();\n" |
| 230 | " else\n" |
| 231 | " g();\n" |
| 232 | " else\n" |
| 233 | " h();\n" |
| 234 | "else\n" |
| 235 | " i();"); |
| 236 | verifyFormat("if (true)\n" |
| 237 | " if (true)\n" |
| 238 | " if (true) {\n" |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 239 | " if (true)\n" |
| 240 | " f();\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 241 | " } else {\n" |
| 242 | " g();\n" |
| 243 | " }\n" |
| 244 | " else\n" |
| 245 | " h();\n" |
| 246 | "else {\n" |
| 247 | " i();\n" |
| 248 | "}"); |
| 249 | } |
| 250 | |
| 251 | TEST_F(FormatTest, ElseIf) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 252 | verifyFormat("if (a) {\n} else if (b) {\n}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 253 | verifyFormat("if (a)\n" |
| 254 | " f();\n" |
| 255 | "else if (b)\n" |
| 256 | " g();\n" |
| 257 | "else\n" |
| 258 | " h();"); |
| 259 | } |
| 260 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 261 | TEST_F(FormatTest, FormatsForLoop) { |
| 262 | verifyFormat( |
| 263 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 264 | " ++VeryVeryLongLoopVariable)\n" |
| 265 | " ;"); |
| 266 | verifyFormat("for (;;)\n" |
| 267 | " f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 268 | verifyFormat("for (;;) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 269 | verifyFormat("for (;;) {\n" |
| 270 | " f();\n" |
| 271 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 272 | |
| 273 | verifyFormat( |
| 274 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 275 | " E = UnwrappedLines.end();\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 276 | " I != E; ++I) {\n}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 277 | |
| 278 | verifyFormat( |
| 279 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 280 | " ++IIIII) {\n}"); |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 281 | verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 282 | " aaaaaaaaaaa = aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 283 | " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 284 | verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" |
| 285 | " I = FD->getDeclsInPrototypeScope().begin(),\n" |
| 286 | " E = FD->getDeclsInPrototypeScope().end();\n" |
| 287 | " I != E; ++I) {\n}"); |
Daniel Jasper | 7522318 | 2013-01-28 17:30:17 +0000 | [diff] [blame] | 288 | |
| 289 | // FIXME: Not sure whether we want extra identation in line 3 here: |
| 290 | verifyFormat( |
| 291 | "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" |
| 292 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" |
| 293 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 294 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" |
| 295 | " ++aaaaaaaaaaa) {\n}"); |
Daniel Jasper | 8159d2f | 2013-02-04 07:30:30 +0000 | [diff] [blame] | 296 | |
| 297 | verifyGoogleFormat( |
| 298 | "for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" |
| 299 | " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" |
| 300 | "}"); |
| 301 | verifyGoogleFormat( |
| 302 | "for (int aaaaaaaaaaa = 1;\n" |
| 303 | " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" |
| 304 | " aaaaaaaaaaaaaaaa,\n" |
| 305 | " aaaaaaaaaaaaaaaa,\n" |
| 306 | " aaaaaaaaaaaaaaaa);\n" |
| 307 | " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" |
| 308 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Daniel Jasper | 3b9a8fc | 2013-01-28 13:21:16 +0000 | [diff] [blame] | 311 | TEST_F(FormatTest, RangeBasedForLoops) { |
| 312 | verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
| 313 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 314 | verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" |
| 315 | " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); |
| 316 | } |
| 317 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 318 | TEST_F(FormatTest, FormatsWhileLoop) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 319 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 320 | verifyFormat("while (true)\n" |
| 321 | " f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 322 | verifyFormat("while () {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 323 | verifyFormat("while () {\n" |
| 324 | " f();\n" |
| 325 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 328 | TEST_F(FormatTest, FormatsDoWhile) { |
| 329 | verifyFormat("do {\n" |
| 330 | " do_something();\n" |
| 331 | "} while (something());"); |
| 332 | verifyFormat("do\n" |
| 333 | " do_something();\n" |
| 334 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 337 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 338 | verifyFormat("switch (x) {\n" |
| 339 | "case 1:\n" |
| 340 | " f();\n" |
| 341 | " break;\n" |
| 342 | "case kFoo:\n" |
| 343 | "case ns::kBar:\n" |
| 344 | "case kBaz:\n" |
| 345 | " break;\n" |
| 346 | "default:\n" |
| 347 | " g();\n" |
| 348 | " break;\n" |
| 349 | "}"); |
| 350 | verifyFormat("switch (x) {\n" |
| 351 | "case 1: {\n" |
| 352 | " f();\n" |
| 353 | " break;\n" |
| 354 | "}\n" |
| 355 | "}"); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 356 | verifyFormat("switch (x) {\n" |
| 357 | "case 1: {\n" |
| 358 | " f();\n" |
| 359 | " {\n" |
| 360 | " g();\n" |
| 361 | " h();\n" |
| 362 | " }\n" |
| 363 | " break;\n" |
| 364 | "}\n" |
| 365 | "}"); |
| 366 | verifyFormat("switch (x) {\n" |
| 367 | "case 1: {\n" |
| 368 | " f();\n" |
| 369 | " if (foo) {\n" |
| 370 | " g();\n" |
| 371 | " h();\n" |
| 372 | " }\n" |
| 373 | " break;\n" |
| 374 | "}\n" |
| 375 | "}"); |
| 376 | verifyFormat("switch (x) {\n" |
| 377 | "case 1: {\n" |
| 378 | " f();\n" |
| 379 | " g();\n" |
| 380 | "} break;\n" |
| 381 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 382 | verifyFormat("switch (test)\n" |
| 383 | " ;"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 384 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 385 | verifyGoogleFormat("switch (x) {\n" |
| 386 | " case 1:\n" |
| 387 | " f();\n" |
| 388 | " break;\n" |
| 389 | " case kFoo:\n" |
| 390 | " case ns::kBar:\n" |
| 391 | " case kBaz:\n" |
| 392 | " break;\n" |
| 393 | " default:\n" |
| 394 | " g();\n" |
| 395 | " break;\n" |
| 396 | "}"); |
| 397 | verifyGoogleFormat("switch (x) {\n" |
| 398 | " case 1: {\n" |
| 399 | " f();\n" |
| 400 | " break;\n" |
| 401 | " }\n" |
| 402 | "}"); |
| 403 | verifyGoogleFormat("switch (test)\n" |
| 404 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 407 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 408 | verifyFormat("void f() {\n" |
| 409 | " some_code();\n" |
| 410 | "test_label:\n" |
| 411 | " some_other_code();\n" |
| 412 | " {\n" |
| 413 | " some_more_code();\n" |
| 414 | " another_label:\n" |
| 415 | " some_more_code();\n" |
| 416 | " }\n" |
| 417 | "}"); |
| 418 | verifyFormat("some_code();\n" |
| 419 | "test_label:\n" |
| 420 | "some_other_code();"); |
| 421 | } |
| 422 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 423 | //===----------------------------------------------------------------------===// |
| 424 | // Tests for comments. |
| 425 | //===----------------------------------------------------------------------===// |
| 426 | |
| 427 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 428 | verifyFormat("// line 1\n" |
| 429 | "// line 2\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 430 | "void f() {}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 431 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 432 | verifyFormat("void f() {\n" |
| 433 | " // Doesn't do anything\n" |
| 434 | "}"); |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 435 | verifyFormat("void f(int i, // some comment (probably for i)\n" |
| 436 | " int j, // some comment (probably for j)\n" |
Daniel Jasper | 487f64b | 2013-01-13 16:10:20 +0000 | [diff] [blame] | 437 | " int k); // some comment (probably for k)"); |
| 438 | verifyFormat("void f(int i,\n" |
| 439 | " // some comment (probably for j)\n" |
| 440 | " int j,\n" |
| 441 | " // some comment (probably for k)\n" |
| 442 | " int k);"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 443 | |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 444 | verifyFormat("int i // This is a fancy variable\n" |
| 445 | " = 5; // with nicely aligned comment."); |
| 446 | |
| 447 | verifyFormat("// Leading comment.\n" |
| 448 | "int a; // Trailing comment."); |
| 449 | verifyFormat("int a; // Trailing comment\n" |
| 450 | " // on 2\n" |
| 451 | " // or 3 lines.\n" |
| 452 | "int b;"); |
| 453 | verifyFormat("int a; // Trailing comment\n" |
| 454 | "\n" |
| 455 | "// Leading comment.\n" |
| 456 | "int b;"); |
| 457 | verifyFormat("int a; // Comment.\n" |
| 458 | " // More details.\n" |
| 459 | "int bbbb; // Another comment."); |
| 460 | verifyFormat( |
| 461 | "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" |
| 462 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" |
| 463 | "int cccccccccccccccccccccccccccccc; // comment\n" |
| 464 | "int ddd; // looooooooooooooooooooooooong comment\n" |
| 465 | "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" |
| 466 | "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" |
| 467 | "int ccccccccccccccccccc; // comment"); |
| 468 | |
Daniel Jasper | 7d1185d | 2013-01-18 09:19:33 +0000 | [diff] [blame] | 469 | verifyFormat("#include \"a\" // comment\n" |
| 470 | "#include \"a/b/c\" // comment"); |
| 471 | verifyFormat("#include <a> // comment\n" |
| 472 | "#include <a/b/c> // comment"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 473 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 474 | verifyFormat("enum E {\n" |
| 475 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 476 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 477 | " VAL_B\n" |
| 478 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 479 | |
| 480 | verifyFormat( |
| 481 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 482 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Daniel Jasper | 043835a | 2013-01-09 09:33:39 +0000 | [diff] [blame] | 483 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 484 | " // Comment inside a statement.\n" |
| 485 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
Daniel Jasper | 36ba0ff | 2013-02-06 15:23:09 +0000 | [diff] [blame] | 486 | verifyFormat( |
| 487 | "bool aaaaaaaaaaaaa = // comment\n" |
| 488 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 489 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 490 | |
Daniel Jasper | 42f458d | 2013-02-13 19:25:54 +0000 | [diff] [blame] | 491 | verifyFormat("int aaaa; // aaaaa\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 492 | "int aa; // aaaaaaa", |
| 493 | getLLVMStyleWithColumns(20)); |
Daniel Jasper | 42f458d | 2013-02-13 19:25:54 +0000 | [diff] [blame] | 494 | |
Daniel Jasper | 821627e | 2013-01-21 22:49:20 +0000 | [diff] [blame] | 495 | EXPECT_EQ("void f() { // This does something ..\n" |
| 496 | "}\n" |
| 497 | "int a; // This is unrelated", |
| 498 | format("void f() { // This does something ..\n" |
| 499 | " }\n" |
| 500 | "int a; // This is unrelated")); |
| 501 | EXPECT_EQ("void f() { // This does something ..\n" |
| 502 | "} // awesome..\n" |
| 503 | "\n" |
| 504 | "int a; // This is unrelated", |
| 505 | format("void f() { // This does something ..\n" |
| 506 | " } // awesome..\n" |
| 507 | " \n" |
| 508 | "int a; // This is unrelated")); |
| 509 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 510 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 511 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 512 | |
| 513 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | d4f2c2e | 2013-01-29 19:41:55 +0000 | [diff] [blame] | 514 | |
| 515 | verifyFormat("someFunction(anotherFunction( // Force break.\n" |
| 516 | " parameter));"); |
Daniel Jasper | 5b7e7b0 | 2013-02-05 09:34:14 +0000 | [diff] [blame] | 517 | |
| 518 | verifyGoogleFormat("#endif // HEADER_GUARD"); |
Daniel Jasper | 474e462 | 2013-02-06 22:04:05 +0000 | [diff] [blame] | 519 | |
| 520 | verifyFormat("const char *test[] = {\n" |
| 521 | " // A\n" |
| 522 | " \"aaaa\",\n" |
| 523 | " // B\n" |
| 524 | " \"aaaaa\",\n" |
| 525 | "};"); |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 526 | verifyGoogleFormat( |
| 527 | "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 528 | " aaaaaaaaaaaaaaaaaaaaaa); // 81 cols with this comment"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 532 | verifyFormat("f(/*test=*/ true);"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 533 | EXPECT_EQ( |
| 534 | "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" |
| 535 | " bbbbbbbbbbbbbbbbbbbbbbbbb);", |
| 536 | format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n" |
| 537 | " bbbbbbbbbbbbbbbbbbbbbbbbb);")); |
| 538 | EXPECT_EQ( |
| 539 | "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 540 | " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", |
| 541 | format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" |
| 542 | "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); |
Daniel Jasper | d4f2c2e | 2013-01-29 19:41:55 +0000 | [diff] [blame] | 543 | |
| 544 | verifyGoogleFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" |
| 545 | " /* parameter 2 */ aaaaaa,\n" |
| 546 | " /* parameter 3 */ aaaaaa,\n" |
| 547 | " /* parameter 4 */ aaaaaa);"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | TEST_F(FormatTest, CommentsInStaticInitializers) { |
| 551 | EXPECT_EQ( |
| 552 | "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n" |
| 553 | " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" |
| 554 | " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" |
| 555 | " aaaaaaaaaaaaaaaaaaaa, // comment\n" |
| 556 | " aaaaaaaaaaaaaaaaaaaa };", |
| 557 | format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" |
| 558 | " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" |
| 559 | " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" |
| 560 | " aaaaaaaaaaaaaaaaaaaa , // comment\n" |
| 561 | " aaaaaaaaaaaaaaaaaaaa };")); |
| 562 | verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" |
| 563 | " bbbbbbbbbbb, ccccccccccc };"); |
| 564 | verifyFormat("static SomeType type = { aaaaaaaaaaa,\n" |
| 565 | " // comment for bb....\n" |
| 566 | " bbbbbbbbbbb, ccccccccccc };"); |
| 567 | verifyGoogleFormat( |
| 568 | "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 569 | " bbbbbbbbbbb, ccccccccccc };"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 570 | verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n" |
| 571 | " // comment for bb....\n" |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 572 | " bbbbbbbbbbb, ccccccccccc };"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 573 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 574 | verifyFormat("S s = { { a, b, c }, // Group #1\n" |
| 575 | " { d, e, f }, // Group #2\n" |
| 576 | " { g, h, i } }; // Group #3"); |
| 577 | verifyFormat("S s = { { // Group #1\n" |
| 578 | " a, b, c },\n" |
| 579 | " { // Group #2\n" |
| 580 | " d, e, f },\n" |
| 581 | " { // Group #3\n" |
| 582 | " g, h, i } };"); |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 583 | |
| 584 | EXPECT_EQ("S s = {\n" |
| 585 | " // Some comment\n" |
| 586 | " a\n" |
| 587 | "\n" |
| 588 | " // Comment after empty line\n" |
| 589 | " b\n" |
| 590 | "}", format("S s = {\n" |
| 591 | " // Some comment\n" |
| 592 | " a\n" |
| 593 | " \n" |
| 594 | " // Comment after empty line\n" |
| 595 | " b\n" |
| 596 | "}")); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 599 | //===----------------------------------------------------------------------===// |
| 600 | // Tests for classes, namespaces, etc. |
| 601 | //===----------------------------------------------------------------------===// |
| 602 | |
| 603 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 604 | verifyFormat("class A {\n};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 608 | verifyFormat("class A {\n" |
| 609 | "public:\n" |
| 610 | "protected:\n" |
| 611 | "private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 612 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 613 | "};"); |
| 614 | verifyGoogleFormat("class A {\n" |
| 615 | " public:\n" |
| 616 | " protected:\n" |
| 617 | " private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 618 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 619 | "};"); |
| 620 | } |
| 621 | |
| 622 | TEST_F(FormatTest, FormatsDerivedClass) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 623 | verifyFormat("class A : public B {\n};"); |
| 624 | verifyFormat("class A : public ::B {\n};"); |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 625 | |
| 626 | verifyFormat( |
| 627 | "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" |
| 628 | " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" |
| 629 | "};\n"); |
| 630 | verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA :\n" |
| 631 | " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" |
| 632 | " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" |
| 633 | "};\n"); |
| 634 | verifyFormat( |
| 635 | "class A : public B, public C, public D, public E, public F, public G {\n" |
| 636 | "};"); |
| 637 | verifyFormat("class AAAAAAAAAAAA : public B,\n" |
| 638 | " public C,\n" |
| 639 | " public D,\n" |
| 640 | " public E,\n" |
| 641 | " public F,\n" |
| 642 | " public G {\n" |
| 643 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 646 | TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 647 | verifyFormat("class A {\n} a, b;"); |
| 648 | verifyFormat("struct A {\n} a, b;"); |
| 649 | verifyFormat("union A {\n} a;"); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 652 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 653 | verifyFormat("enum {\n" |
| 654 | " Zero,\n" |
| 655 | " One = 1,\n" |
| 656 | " Two = One + 1,\n" |
| 657 | " Three = (One + Two),\n" |
| 658 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 659 | " Five = (One, Two, Three, Four, 5)\n" |
| 660 | "};"); |
| 661 | verifyFormat("enum Enum {\n" |
| 662 | "};"); |
| 663 | verifyFormat("enum {\n" |
| 664 | "};"); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 665 | verifyFormat("enum X E {\n} d;"); |
| 666 | verifyFormat("enum __attribute__((...)) E {\n} d;"); |
| 667 | verifyFormat("enum __declspec__((...)) E {\n} d;"); |
| 668 | verifyFormat("enum X f() {\n a();\n return 42;\n}"); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Nico Weber | efaddc0 | 2013-01-14 05:49:49 +0000 | [diff] [blame] | 671 | TEST_F(FormatTest, FormatsBitfields) { |
| 672 | verifyFormat("struct Bitfields {\n" |
| 673 | " unsigned sClass : 8;\n" |
| 674 | " unsigned ValueKind : 2;\n" |
| 675 | "};"); |
| 676 | } |
| 677 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 678 | TEST_F(FormatTest, FormatsNamespaces) { |
| 679 | verifyFormat("namespace some_namespace {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 680 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 681 | "void f() { f(); }\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 682 | "}"); |
| 683 | verifyFormat("namespace {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 684 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 685 | "void f() { f(); }\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 686 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 687 | verifyFormat("inline namespace X {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 688 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 689 | "void f() { f(); }\n" |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 690 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 691 | verifyFormat("using namespace some_namespace;\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 692 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 693 | "void f() { f(); }"); |
Manuel Klimek | 7fc2db0 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 694 | |
| 695 | // This code is more common than we thought; if we |
| 696 | // layout this correctly the semicolon will go into |
| 697 | // its own line, which is undesireable. |
| 698 | verifyFormat("namespace {\n};"); |
| 699 | verifyFormat("namespace {\n" |
| 700 | "class A {\n" |
| 701 | "};\n" |
| 702 | "};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 705 | TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 706 | |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 707 | TEST_F(FormatTest, FormatTryCatch) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 708 | // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll |
| 709 | // also not create single-line-blocks. |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 710 | verifyFormat("try {\n" |
| 711 | " throw a * b;\n" |
| 712 | "}\n" |
| 713 | "catch (int a) {\n" |
| 714 | " // Do nothing.\n" |
| 715 | "}\n" |
| 716 | "catch (...) {\n" |
| 717 | " exit(42);\n" |
| 718 | "}"); |
| 719 | |
| 720 | // Function-level try statements. |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 721 | verifyFormat("int f() try { return 4; }\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 722 | "catch (...) {\n" |
| 723 | " return 5;\n" |
| 724 | "}"); |
| 725 | verifyFormat("class A {\n" |
| 726 | " int a;\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 727 | " A() try : a(0) {}\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 728 | " catch (...) {\n" |
| 729 | " throw;\n" |
| 730 | " }\n" |
| 731 | "};\n"); |
| 732 | } |
| 733 | |
| 734 | TEST_F(FormatTest, FormatObjCTryCatch) { |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 735 | verifyFormat("@try {\n" |
| 736 | " f();\n" |
| 737 | "}\n" |
| 738 | "@catch (NSException e) {\n" |
| 739 | " @throw;\n" |
| 740 | "}\n" |
| 741 | "@finally {\n" |
| 742 | " exit(42);\n" |
| 743 | "}"); |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 746 | TEST_F(FormatTest, StaticInitializers) { |
| 747 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 748 | |
| 749 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 750 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 751 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 752 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 753 | "};"); |
| 754 | |
| 755 | verifyFormat( |
| 756 | "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" |
| 757 | " looooooooooooooooooooooooooooooooooongname,\n" |
| 758 | " looooooooooooooooooooooooooooooong };"); |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 759 | // Allow bin-packing in static initializers as this would often lead to |
| 760 | // terrible results, e.g.: |
| 761 | verifyGoogleFormat( |
| 762 | "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" |
| 763 | " looooooooooooooooooooooooooooooooooongname,\n" |
| 764 | " looooooooooooooooooooooooooooooong };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 767 | TEST_F(FormatTest, NestedStaticInitializers) { |
| 768 | verifyFormat("static A x = { { {} } };\n"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 769 | verifyFormat("static A x = { { { init1, init2, init3, init4 },\n" |
| 770 | " { init1, init2, init3, init4 } } };"); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 771 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 772 | verifyFormat("somes Status::global_reps[3] = {\n" |
| 773 | " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" |
| 774 | " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" |
| 775 | " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" |
| 776 | "};"); |
| 777 | verifyGoogleFormat("somes Status::global_reps[3] = {\n" |
| 778 | " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" |
| 779 | " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" |
| 780 | " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" |
| 781 | "};"); |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 782 | verifyFormat( |
| 783 | "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n" |
| 784 | " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop" |
| 785 | " } };"); |
| 786 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 787 | verifyFormat( |
| 788 | "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n" |
| 789 | " { 111111111111111111111111111111,\n" |
| 790 | " 222222222222222222222222222222,\n" |
| 791 | " 333333333333333333333333333333 },\n" |
| 792 | " { 1, 2, 3 }, { 1, 2, 3 } } };"); |
Daniel Jasper | 6bd0232 | 2013-02-03 18:07:15 +0000 | [diff] [blame] | 793 | verifyFormat( |
| 794 | "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n" |
| 795 | " { { 111111111111111111111111111111,\n" |
| 796 | " 222222222222222222222222222222,\n" |
| 797 | " 333333333333333333333333333333 } },\n" |
| 798 | " { { 1, 2, 3 } }, { { 1, 2, 3 } } };"); |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 799 | |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 800 | // FIXME: We might at some point want to handle this similar to parameter |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 801 | // lists, where we have an option to put each on a single line. |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 802 | verifyFormat( |
| 803 | "struct {\n" |
| 804 | " unsigned bit;\n" |
| 805 | " const char *const name;\n" |
| 806 | "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n" |
| 807 | " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };"); |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 810 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 811 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 812 | " \\\n" |
| 813 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 814 | } |
| 815 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 816 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 817 | verifyFormat("virtual void write(ELFWriter *writerrr,\n" |
| 818 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Manuel Klimek | bd04f2a | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 821 | TEST_F(FormatTest, LayoutUnknownPPDirective) { |
| 822 | EXPECT_EQ("#123 \"A string literal\"", |
| 823 | format(" # 123 \"A string literal\"")); |
| 824 | EXPECT_EQ("#;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 825 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 829 | EXPECT_EQ("#line 42 \"test\"\n", |
| 830 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 831 | EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", |
| 832 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 836 | EXPECT_EQ("#line 42 \"test\"", |
| 837 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 838 | EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 841 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 842 | // If the macro fits in one line, we still do not get the full |
| 843 | // line, as only the next line decides whether we need an escaped newline and |
| 844 | // thus use the last column. |
| 845 | verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 846 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 847 | verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); |
| 848 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 849 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 850 | |
| 851 | verifyFormat("#define A A\n#define A A"); |
| 852 | verifyFormat("#define A(X) A\n#define A A"); |
| 853 | |
| 854 | verifyFormat("#define Something Other", getLLVMStyleWithColumns(24)); |
| 855 | verifyFormat("#define Something \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 856 | " Other", |
| 857 | getLLVMStyleWithColumns(23)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 861 | EXPECT_EQ("// some comment\n" |
| 862 | "#include \"a.h\"\n" |
| 863 | "#define A(A,\\\n" |
| 864 | " B)\n" |
| 865 | "#include \"b.h\"\n" |
| 866 | "// some comment\n", |
| 867 | format(" // some comment\n" |
| 868 | " #include \"a.h\"\n" |
| 869 | "#define A(A,\\\n" |
| 870 | " B)\n" |
| 871 | " #include \"b.h\"\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 872 | " // some comment\n", |
| 873 | getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 876 | TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 877 | |
| 878 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 879 | EXPECT_EQ("#define A \\\n" |
| 880 | " c; \\\n" |
| 881 | " e;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 882 | "f;", |
| 883 | format("#define A c; e;\n" |
| 884 | "f;", |
| 885 | getLLVMStyleWithColumns(14))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 888 | TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 889 | |
| 890 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 891 | EXPECT_EQ("# define A\\\n b;", |
| 892 | format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 896 | EXPECT_EQ("int x,\n" |
| 897 | "#define A\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 898 | " y;", |
| 899 | format("int x,\n#define A\ny;")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 902 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 903 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 904 | verifyFormat("#define A \\\n" |
| 905 | " { \\\n" |
| 906 | " f(#c);\\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 907 | " }", |
| 908 | getLLVMStyleWithColumns(11)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 909 | |
| 910 | verifyFormat("#define A(X) \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 911 | " void function##X()", |
| 912 | getLLVMStyleWithColumns(22)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 913 | |
| 914 | verifyFormat("#define A(a, b, c) \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 915 | " void a##b##c()", |
| 916 | getLLVMStyleWithColumns(22)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 917 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 918 | verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Manuel Klimek | 7ccbc21 | 2013-01-23 14:37:36 +0000 | [diff] [blame] | 921 | TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { |
| 922 | verifyFormat("#define A (1)"); |
| 923 | } |
| 924 | |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 925 | TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { |
| 926 | EXPECT_EQ("#define A b;", format("#define A \\\n" |
| 927 | " \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 928 | " b;", |
| 929 | getLLVMStyleWithColumns(25))); |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 930 | EXPECT_EQ("#define A \\\n" |
| 931 | " \\\n" |
| 932 | " a; \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 933 | " b;", |
| 934 | format("#define A \\\n" |
| 935 | " \\\n" |
| 936 | " a; \\\n" |
| 937 | " b;", |
| 938 | getLLVMStyleWithColumns(11))); |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 939 | EXPECT_EQ("#define A \\\n" |
| 940 | " a; \\\n" |
| 941 | " \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 942 | " b;", |
| 943 | format("#define A \\\n" |
| 944 | " a; \\\n" |
| 945 | " \\\n" |
| 946 | " b;", |
| 947 | getLLVMStyleWithColumns(11))); |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Daniel Jasper | 34511fb | 2013-02-19 17:14:38 +0000 | [diff] [blame] | 950 | TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { |
| 951 | // FIXME: Improve formatting of case labels in macros. |
| 952 | verifyFormat("#define SOMECASES \\\n" |
| 953 | "case 1: \\\n" |
| 954 | " case 2\n", |
| 955 | getLLVMStyleWithColumns(20)); |
| 956 | |
| 957 | verifyFormat("#define A template <typename T>"); |
Daniel Jasper | 03628b8 | 2013-02-19 20:05:41 +0000 | [diff] [blame] | 958 | verifyFormat("#define STR(x) #x\n" |
| 959 | "f(STR(this_is_a_string_literal{));"); |
Daniel Jasper | 34511fb | 2013-02-19 17:14:38 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 962 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 963 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 964 | } |
| 965 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 966 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 967 | verifyFormat("{\n { a #c; }\n}"); |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 970 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 971 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 972 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 973 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 974 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 975 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 976 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 977 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 978 | EXPECT_EQ( |
| 979 | "#define A \\\n int i; \\\n int j;", |
| 980 | format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 983 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 984 | verifyFormat("#define A \\\n" |
| 985 | " int v( \\\n" |
| 986 | " a); \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 987 | " int i;", |
| 988 | getLLVMStyleWithColumns(11)); |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 991 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 992 | EXPECT_EQ( |
| 993 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 994 | " \\\n" |
| 995 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 996 | "\n" |
| 997 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 998 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 999 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 1000 | "\\\n" |
| 1001 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 1002 | " \n" |
| 1003 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 1004 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1007 | TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { |
| 1008 | EXPECT_EQ("int\n" |
| 1009 | "#define A\n" |
| 1010 | " a;", |
| 1011 | format("int\n#define A\na;")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1012 | verifyFormat("functionCallTo(\n" |
| 1013 | " someOtherFunction(\n" |
| 1014 | " withSomeParameters, whichInSequence,\n" |
| 1015 | " areLongerThanALine(andAnotherCall,\n" |
| 1016 | "#define A B\n" |
| 1017 | " withMoreParamters,\n" |
| 1018 | " whichStronglyInfluenceTheLayout),\n" |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 1019 | " andMoreParameters),\n" |
| 1020 | " trailing);", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1021 | getLLVMStyleWithColumns(69)); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1024 | TEST_F(FormatTest, LayoutBlockInsideParens) { |
| 1025 | EXPECT_EQ("functionCall({\n" |
| 1026 | " int i;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1027 | "});", |
| 1028 | format(" functionCall ( {int i;} );")); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | TEST_F(FormatTest, LayoutBlockInsideStatement) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1032 | EXPECT_EQ("SOME_MACRO { int i; }\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1033 | "int i;", |
| 1034 | format(" SOME_MACRO {int i;} int i;")); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | TEST_F(FormatTest, LayoutNestedBlocks) { |
| 1038 | verifyFormat("void AddOsStrings(unsigned bitmask) {\n" |
| 1039 | " struct s {\n" |
| 1040 | " int i;\n" |
| 1041 | " };\n" |
| 1042 | " s kBitsToOs[] = { { 10 } };\n" |
| 1043 | " for (int i = 0; i < 10; ++i)\n" |
| 1044 | " return;\n" |
| 1045 | "}"); |
| 1046 | } |
| 1047 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1048 | TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { |
| 1049 | EXPECT_EQ("{}", format("{}")); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1050 | |
| 1051 | // Negative test for enum. |
| 1052 | verifyFormat("enum E {\n};"); |
| 1053 | |
| 1054 | // Note that when there's a missing ';', we still join... |
| 1055 | verifyFormat("enum E {}"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1058 | //===----------------------------------------------------------------------===// |
| 1059 | // Line break tests. |
| 1060 | //===----------------------------------------------------------------------===// |
| 1061 | |
| 1062 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 1063 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 1064 | " int h, int j, int f,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1065 | " int c, int ddddddddddddd) {}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 1069 | verifyFormat( |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 1070 | "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" |
| 1071 | " parameter, parameter, parameter)),\n" |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 1072 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 1075 | TEST_F(FormatTest, PreventConfusingIndents) { |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 1076 | verifyFormat( |
| 1077 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1078 | " aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1079 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1080 | " aaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1081 | verifyFormat( |
| 1082 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n" |
| 1083 | " aaaaaaaaaaaaaaaaaaaaaaaa[\n" |
| 1084 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n" |
| 1085 | " aaaaaaaaaaaaaaaaaaaaaaaa];"); |
| 1086 | verifyFormat( |
| 1087 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" |
| 1088 | " aaaaaaaaaaaaaaaaaaaaaaaa<\n" |
| 1089 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" |
| 1090 | " aaaaaaaaaaaaaaaaaaaaaaaa>;"); |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 1091 | verifyFormat("int a = bbbb && ccc && fffff(\n" |
| 1092 | "#define A Just forcing a new line\n" |
| 1093 | " ddd);"); |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1096 | TEST_F(FormatTest, ConstructorInitializers) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1097 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1098 | verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", |
| 1099 | getLLVMStyleWithColumns(45)); |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1100 | verifyFormat("Constructor()\n" |
| 1101 | " : Inttializer(FitsOnTheLine) {}", |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1102 | getLLVMStyleWithColumns(44)); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1103 | verifyFormat("Constructor()\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1104 | " : Inttializer(FitsOnTheLine) {}", |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1105 | getLLVMStyleWithColumns(43)); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1106 | |
| 1107 | verifyFormat( |
| 1108 | "SomeClass::Constructor()\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1109 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1110 | |
| 1111 | verifyFormat( |
| 1112 | "SomeClass::Constructor()\n" |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1113 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1114 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1115 | verifyFormat( |
| 1116 | "SomeClass::Constructor()\n" |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1117 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1118 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1119 | |
| 1120 | verifyFormat("Constructor()\n" |
| 1121 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1122 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1123 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1124 | " aaaaaaaaaaaaaaaaaaaaaaa() {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1125 | |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 1126 | verifyFormat("Constructor()\n" |
| 1127 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1128 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 1129 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1130 | // Here a line could be saved by splitting the second initializer onto two |
| 1131 | // lines, but that is not desireable. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1132 | verifyFormat( |
| 1133 | "Constructor()\n" |
| 1134 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1135 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1136 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1137 | |
Daniel Jasper | b130a54 | 2013-02-15 16:49:44 +0000 | [diff] [blame] | 1138 | FormatStyle OnePerLine = getLLVMStyle(); |
| 1139 | OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
| 1140 | verifyFormat("SomeClass::Constructor()\n" |
| 1141 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
| 1142 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1143 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1144 | OnePerLine); |
Daniel Jasper | b130a54 | 2013-02-15 16:49:44 +0000 | [diff] [blame] | 1145 | verifyFormat("SomeClass::Constructor()\n" |
| 1146 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" |
| 1147 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1148 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1149 | OnePerLine); |
Daniel Jasper | b130a54 | 2013-02-15 16:49:44 +0000 | [diff] [blame] | 1150 | verifyFormat("MyClass::MyClass(int var)\n" |
| 1151 | " : some_var_(var), // 4 space indent\n" |
| 1152 | " some_other_var_(var + 1) { // lined up\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1153 | "}", |
| 1154 | OnePerLine); |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 1155 | |
| 1156 | // This test takes VERY long when memoization is broken. |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1157 | std::string input = "Constructor()\n" |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 1158 | " : aaaa(a,\n"; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1159 | for (unsigned i = 0, e = 80; i != e; ++i) { |
| 1160 | input += " a,\n"; |
| 1161 | } |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1162 | input += " a) {}"; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1163 | verifyGoogleFormat(input); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1166 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 1167 | verifyFormat( |
| 1168 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 1169 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 1170 | " f();"); |
| 1171 | } |
| 1172 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1173 | TEST_F(FormatTest, BreaksDesireably) { |
| 1174 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 1175 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1176 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 1177 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1178 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" |
| 1179 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1180 | |
| 1181 | verifyFormat( |
| 1182 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1183 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1184 | |
| 1185 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1186 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1187 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 1188 | |
| 1189 | verifyFormat( |
| 1190 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1191 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 1192 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1193 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 1194 | |
| 1195 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 1196 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1197 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 1198 | verifyFormat( |
| 1199 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 1200 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 1201 | verifyFormat( |
| 1202 | "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1203 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
| 1204 | verifyFormat( |
| 1205 | "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1206 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 1207 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 1208 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 1209 | // taking into account the StopAt value. |
| 1210 | verifyFormat( |
| 1211 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1212 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 1213 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 1214 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 1215 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1216 | verifyFormat("{\n {\n {\n" |
| 1217 | " Annotation.SpaceRequiredBefore =\n" |
| 1218 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 1219 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 1220 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1223 | TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { |
Daniel Jasper | d399bff | 2013-02-05 09:41:21 +0000 | [diff] [blame] | 1224 | verifyGoogleFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" |
| 1225 | " aaaaaaaaaaaaaaaaaaaa,\n" |
| 1226 | " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);"); |
| 1227 | verifyGoogleFormat( |
| 1228 | "aaaaaaa(aaaaaaaaaaaaa,\n" |
| 1229 | " aaaaaaaaaaaaa,\n" |
| 1230 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1231 | verifyGoogleFormat( |
| 1232 | "aaaaaaaa(aaaaaaaaaaaaa,\n" |
| 1233 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1234 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 1235 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1236 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
| 1237 | verifyGoogleFormat( |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1238 | "aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" |
| 1239 | " .aaaaaaaaaaaaaaaaaa();"); |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1240 | verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1241 | " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1242 | |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1243 | verifyGoogleFormat( |
| 1244 | "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1245 | " aaaaaaaaaaaa,\n" |
| 1246 | " aaaaaaaaaaaa);"); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1247 | verifyGoogleFormat( |
| 1248 | "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" |
| 1249 | " ddddddddddddddddddddddddddddd),\n" |
| 1250 | " test);"); |
| 1251 | |
| 1252 | verifyGoogleFormat( |
| 1253 | "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1254 | " aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1255 | " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;"); |
| 1256 | verifyGoogleFormat("a(\"a\"\n" |
| 1257 | " \"a\",\n" |
| 1258 | " a);"); |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1259 | |
| 1260 | FormatStyle Style = getGoogleStyle(); |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 1261 | Style.AllowAllParametersOfDeclarationOnNextLine = false; |
| 1262 | verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1263 | " aaaaaaaaa,\n" |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 1264 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
| 1265 | Style); |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1266 | verifyFormat( |
| 1267 | "void f() {\n" |
| 1268 | " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" |
| 1269 | " .aaaaaaa();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1270 | "}", |
| 1271 | Style); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1274 | TEST_F(FormatTest, FormatsBuilderPattern) { |
| 1275 | verifyFormat( |
| 1276 | "return llvm::StringSwitch<Reference::Kind>(name)\n" |
Daniel Jasper | f39c885 | 2013-01-23 16:58:21 +0000 | [diff] [blame] | 1277 | " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" |
| 1278 | " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" |
| 1279 | " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" |
| 1280 | " .Default(ORDER_TEXT);\n"); |
Daniel Jasper | 4a544e5 | 2013-02-15 20:33:06 +0000 | [diff] [blame] | 1281 | |
| 1282 | verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" |
| 1283 | " aaaaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); |
Daniel Jasper | 515f65d | 2013-02-18 13:24:21 +0000 | [diff] [blame] | 1284 | verifyFormat( |
| 1285 | "aaaaaaa->aaaaaaa\n" |
| 1286 | " ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1287 | " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); |
Daniel Jasper | 82282dc | 2013-02-18 13:52:06 +0000 | [diff] [blame] | 1288 | verifyFormat( |
| 1289 | "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" |
| 1290 | " aaaaaaaaaaaaaa);"); |
| 1291 | verifyFormat( |
| 1292 | "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n" |
| 1293 | " ->aaaaaaaaaaaaaaaa(\n" |
| 1294 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1295 | " ->aaaaaaaaaaaaaaaaa();"); |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1298 | TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { |
| 1299 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1300 | " GUARDED_BY(aaaaaaaaaaaaa);"); |
Daniel Jasper | 60ca75d | 2013-01-17 13:31:52 +0000 | [diff] [blame] | 1301 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
| 1302 | " GUARDED_BY(aaaaaaaaaaaaa);"); |
| 1303 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1304 | " GUARDED_BY(aaaaaaaaaaaaa) {}"); |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1307 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 1308 | verifyFormat( |
| 1309 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1310 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1311 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1312 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1313 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1314 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1315 | verifyFormat( |
| 1316 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1317 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1318 | verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" |
| 1319 | " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" |
| 1320 | " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" |
| 1321 | " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); |
| 1322 | verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" |
| 1323 | " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" |
| 1324 | " aaaaaaaaaaaaaaa != aa) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Daniel Jasper | 7006e7e | 2013-01-23 12:27:43 +0000 | [diff] [blame] | 1327 | TEST_F(FormatTest, BreaksAfterAssignments) { |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1328 | verifyFormat( |
Daniel Jasper | 7006e7e | 2013-01-23 12:27:43 +0000 | [diff] [blame] | 1329 | "unsigned Cost =\n" |
| 1330 | " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" |
| 1331 | " SI->getPointerAddressSpaceee());\n"); |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1332 | verifyFormat( |
Daniel Jasper | 13cb7c2 | 2013-01-23 15:55:19 +0000 | [diff] [blame] | 1333 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 1334 | " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); |
Daniel Jasper | f39c885 | 2013-01-23 16:58:21 +0000 | [diff] [blame] | 1335 | |
| 1336 | verifyFormat( |
| 1337 | "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" |
| 1338 | " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1341 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 1342 | verifyFormat( |
| 1343 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1344 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1345 | verifyFormat( |
| 1346 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1347 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1348 | verifyFormat( |
| 1349 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1350 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1351 | verifyFormat( |
| 1352 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1353 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1354 | verifyFormat("double LooooooooooooooooooooooooongResult =\n" |
| 1355 | " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1356 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 1360 | verifyFormat( |
| 1361 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1362 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1363 | verifyFormat( |
| 1364 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1365 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1366 | } |
| 1367 | |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 1368 | TEST_F(FormatTest, BreaksConditionalExpressions) { |
| 1369 | verifyFormat( |
| 1370 | "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 1371 | " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1372 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 1f2b078 | 2013-01-16 16:23:19 +0000 | [diff] [blame] | 1373 | verifyFormat( |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 1374 | "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1375 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1376 | verifyFormat( |
| 1377 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" |
| 1378 | " : aaaaaaaaaaaaa);"); |
| 1379 | verifyFormat( |
| 1380 | "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1381 | " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1382 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1383 | " aaaaaaaaaaaaa);"); |
| 1384 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1385 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1386 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1387 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1388 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1389 | verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1390 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1391 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1392 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1393 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1394 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1395 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 1396 | |
| 1397 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1398 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1399 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1400 | |
| 1401 | // FIXME: The trailing third parameter here is kind of hidden. Prefer putting |
| 1402 | // it on the next line. |
Daniel Jasper | 087387a | 2013-02-08 16:49:27 +0000 | [diff] [blame] | 1403 | verifyFormat( |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 1404 | "unsigned Indent =\n" |
| 1405 | " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" |
| 1406 | " ? IndentForLevel[TheLine.Level]\n" |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 1407 | " : TheLine * 2,\n" |
| 1408 | " TheLine.InPPDirective, PreviousEndOfLineColumn);", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1409 | getLLVMStyleWithColumns(70)); |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 1410 | |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 1413 | TEST_F(FormatTest, DeclarationsOfMultipleVariables) { |
| 1414 | verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" |
| 1415 | " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); |
| 1416 | verifyFormat("bool a = true, b = false;"); |
| 1417 | |
| 1418 | // FIXME: Indentation looks weird. |
| 1419 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 1420 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" |
| 1421 | " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1422 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 1423 | verifyFormat( |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 1424 | "bool aaaaaaaaaaaaaaaaaaaaa =\n" |
| 1425 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" |
| 1426 | " d = e && f;"); |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 1427 | |
| 1428 | } |
| 1429 | |
Nico Weber | 7d37b8b | 2013-01-12 01:28:06 +0000 | [diff] [blame] | 1430 | TEST_F(FormatTest, ConditionalExpressionsInBrackets) { |
| 1431 | verifyFormat("arr[foo ? bar : baz];"); |
| 1432 | verifyFormat("f()[foo ? bar : baz];"); |
| 1433 | verifyFormat("(a + b)[foo ? bar : baz];"); |
| 1434 | verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); |
| 1435 | } |
| 1436 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1437 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 1438 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 1439 | " \"short literal\");"); |
| 1440 | verifyFormat( |
| 1441 | "looooooooooooooooooooooooongFunction(\n" |
| 1442 | " \"short literal\"\n" |
| 1443 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1444 | verifyFormat("someFunction(\"Always break between multi-line\"\n" |
| 1445 | " \" string literals\",\n" |
| 1446 | " and, other, parameters);"); |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 1447 | EXPECT_EQ("fun + \"1243\" /* comment */\n" |
| 1448 | " \"5678\";", |
| 1449 | format("fun + \"1243\" /* comment */\n" |
| 1450 | " \"5678\";", |
| 1451 | getLLVMStyleWithColumns(28))); |
| 1452 | EXPECT_EQ( |
| 1453 | "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" |
| 1454 | " \"aaaaaaaaaaaaaaaaaaaaa\"\n" |
| 1455 | " \"aaaaaaaaaaaaaaaa\";", |
| 1456 | format("aaaaaa =" |
| 1457 | "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " |
| 1458 | "aaaaaaaaaaaaaaaaaaaaa\" " |
| 1459 | "\"aaaaaaaaaaaaaaaa\";")); |
Manuel Klimek | e9a6226 | 2013-02-20 15:32:58 +0000 | [diff] [blame] | 1460 | verifyFormat("a = a + \"a\"\n" |
| 1461 | " \"a\"\n" |
| 1462 | " \"a\";"); |
Daniel Jasper | 3a20441 | 2013-02-23 07:46:38 +0000 | [diff] [blame] | 1463 | |
| 1464 | verifyFormat( |
| 1465 | "#define LL_FORMAT \"ll\"\n" |
| 1466 | "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" |
| 1467 | " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1470 | TEST_F(FormatTest, AlignsPipes) { |
| 1471 | verifyFormat( |
| 1472 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1473 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1474 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1475 | verifyFormat( |
| 1476 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 1477 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 1478 | verifyFormat( |
| 1479 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1480 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1481 | verifyFormat( |
| 1482 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 1483 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 1484 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 1485 | verifyFormat( |
| 1486 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1487 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1488 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | fa543ac | 2013-02-04 07:34:48 +0000 | [diff] [blame] | 1489 | |
| 1490 | verifyFormat("return out << \"somepacket = {\\n\"\n" |
| 1491 | " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" |
| 1492 | " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" |
| 1493 | " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" |
| 1494 | " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" |
| 1495 | " << \"}\";"); |
Daniel Jasper | 4e8a7b4 | 2013-02-06 21:04:05 +0000 | [diff] [blame] | 1496 | |
| 1497 | verifyFormat( |
| 1498 | "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" |
| 1499 | " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" |
| 1500 | " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" |
| 1501 | " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" |
| 1502 | " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1505 | TEST_F(FormatTest, UnderstandsEquals) { |
| 1506 | verifyFormat( |
| 1507 | "aaaaaaaaaaaaaaaaa =\n" |
| 1508 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1509 | verifyFormat( |
| 1510 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1511 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1512 | verifyFormat( |
| 1513 | "if (a) {\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1514 | " f();\n" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1515 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1516 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 1517 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1518 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1519 | verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 1520 | " 100000000 + 10000000) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1523 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1524 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 1525 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1526 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1527 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 1528 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1529 | |
| 1530 | verifyFormat( |
| 1531 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 1532 | " Parameter2);"); |
| 1533 | |
| 1534 | verifyFormat( |
| 1535 | "ShortObject->shortFunction(\n" |
| 1536 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 1537 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 1538 | |
| 1539 | verifyFormat("loooooooooooooongFunction(\n" |
| 1540 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 1541 | |
| 1542 | verifyFormat( |
| 1543 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 1544 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 1545 | |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1546 | verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" |
| 1547 | " .WillRepeatedly(Return(SomeValue));"); |
| 1548 | verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" |
| 1549 | " .insert(ccccccccccccccccccccccc);"); |
| 1550 | |
Daniel Jasper | cf5767d | 2013-02-18 11:05:07 +0000 | [diff] [blame] | 1551 | verifyGoogleFormat( |
| 1552 | "aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" |
| 1553 | " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" |
| 1554 | " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" |
| 1555 | " aaaaaaaaaaaaaaaaaaa,\n" |
| 1556 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1557 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 1558 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1559 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1560 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 1561 | verifyFormat( |
| 1562 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 1563 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1564 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9fc56f2 | 2013-02-14 15:01:34 +0000 | [diff] [blame] | 1565 | |
| 1566 | verifyFormat( |
| 1567 | "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1568 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1571 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 1572 | verifyFormat("template <typename T>\n" |
| 1573 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 1574 | verifyFormat( |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1575 | "template <typename T>\n" |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1576 | "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1577 | verifyFormat("template <typename T>\n" |
| 1578 | "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 1579 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1580 | verifyFormat( |
| 1581 | "template <typename T>\n" |
| 1582 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 1583 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 1584 | verifyFormat( |
| 1585 | "template <typename T>\n" |
| 1586 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 1587 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 1588 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | d64f738 | 2013-01-09 09:50:48 +0000 | [diff] [blame] | 1589 | verifyFormat("template <typename T>\n" |
| 1590 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1591 | " int aaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 1592 | verifyFormat( |
| 1593 | "template <typename T1, typename T2 = char, typename T3 = char,\n" |
| 1594 | " typename T4 = char>\n" |
| 1595 | "void f();"); |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 1596 | verifyFormat( |
| 1597 | "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" |
| 1598 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 1599 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1600 | verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" |
| 1601 | " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Daniel Jasper | 63f0036 | 2013-01-25 10:57:27 +0000 | [diff] [blame] | 1604 | TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { |
| 1605 | verifyFormat( |
| 1606 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1607 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
| 1608 | verifyFormat( |
| 1609 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1610 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1611 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); |
| 1612 | |
| 1613 | // FIXME: Should we have an extra indent after the second break? |
| 1614 | verifyFormat( |
| 1615 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1616 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1617 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
| 1618 | |
| 1619 | // FIXME: Look into whether we should indent 4 from the start or 4 from |
| 1620 | // "bbbbb..." here instead of what we are doing now. |
| 1621 | verifyFormat( |
| 1622 | "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" |
Daniel Jasper | 5f2173e | 2013-01-28 07:43:15 +0000 | [diff] [blame] | 1623 | " cccccccccccccccccccccccccccccccccccccccccccccc());"); |
Daniel Jasper | 63f0036 | 2013-01-25 10:57:27 +0000 | [diff] [blame] | 1624 | |
| 1625 | // Breaking at nested name specifiers is generally not desirable. |
| 1626 | verifyFormat( |
| 1627 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1628 | " aaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 1629 | |
| 1630 | verifyFormat( |
| 1631 | "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1632 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1633 | " aaaaaaaaaaaaaaaaaaaaa);", |
| 1634 | getLLVMStyleWithColumns(74)); |
Daniel Jasper | 63f0036 | 2013-01-25 10:57:27 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1637 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 1638 | verifyFormat("A<int> a;"); |
| 1639 | verifyFormat("A<A<A<int> > > a;"); |
| 1640 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 1641 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 1642 | verifyFormat("bool x = 5 < f<int>();"); |
| 1643 | verifyFormat("bool x = f<int>() > 5;"); |
| 1644 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 1645 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 1646 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 1647 | |
| 1648 | verifyGoogleFormat("A<A<int>> a;"); |
| 1649 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 1650 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1651 | verifyGoogleFormat("A<A<int> > a;"); |
| 1652 | verifyGoogleFormat("A<A<A<int> > > a;"); |
| 1653 | verifyGoogleFormat("A<A<A<A<int> > > > a;"); |
| 1654 | EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); |
| 1655 | EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1656 | |
| 1657 | verifyFormat("test >> a >> b;"); |
| 1658 | verifyFormat("test << a >> b;"); |
| 1659 | |
| 1660 | verifyFormat("f<int>();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1661 | verifyFormat("template <typename T> void f() {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1664 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1665 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1666 | verifyFormat("f(-1, -2, -3);"); |
| 1667 | verifyFormat("a[-1] = 5;"); |
| 1668 | verifyFormat("int a = 5 + -2;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1669 | verifyFormat("if (i == -1) {\n}"); |
| 1670 | verifyFormat("if (i != -1) {\n}"); |
| 1671 | verifyFormat("if (i > -1) {\n}"); |
| 1672 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 1673 | verifyFormat("++(a->f());"); |
| 1674 | verifyFormat("--(a->f());"); |
Daniel Jasper | 4abbb53 | 2013-01-14 12:18:19 +0000 | [diff] [blame] | 1675 | verifyFormat("(a->f())++;"); |
| 1676 | verifyFormat("a[42]++;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1677 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1678 | |
| 1679 | verifyFormat("a-- > b;"); |
| 1680 | verifyFormat("b ? -a : c;"); |
| 1681 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 1682 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1683 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 1684 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 1685 | |
| 1686 | verifyFormat("return -1;"); |
| 1687 | verifyFormat("switch (a) {\n" |
| 1688 | "case -1:\n" |
| 1689 | " break;\n" |
| 1690 | "}"); |
Nico Weber | cc191d1 | 2013-01-12 05:41:23 +0000 | [diff] [blame] | 1691 | |
| 1692 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); |
| 1693 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1694 | |
| 1695 | verifyFormat("int a = /* confusing comment */ -1;"); |
| 1696 | // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. |
| 1697 | verifyFormat("int a = i /* confusing comment */++;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1701 | verifyFormat("bool operator<();"); |
| 1702 | verifyFormat("bool operator>();"); |
| 1703 | verifyFormat("bool operator=();"); |
| 1704 | verifyFormat("bool operator==();"); |
| 1705 | verifyFormat("bool operator!=();"); |
| 1706 | verifyFormat("int operator+();"); |
| 1707 | verifyFormat("int operator++();"); |
| 1708 | verifyFormat("bool operator();"); |
| 1709 | verifyFormat("bool operator()();"); |
| 1710 | verifyFormat("bool operator[]();"); |
| 1711 | verifyFormat("operator bool();"); |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1712 | verifyFormat("operator int();"); |
| 1713 | verifyFormat("operator void *();"); |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1714 | verifyFormat("operator SomeType<int>();"); |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1715 | verifyFormat("operator SomeType<int, int>();"); |
| 1716 | verifyFormat("operator SomeType<SomeType<int> >();"); |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1717 | verifyFormat("void *operator new(std::size_t size);"); |
| 1718 | verifyFormat("void *operator new[](std::size_t size);"); |
| 1719 | verifyFormat("void operator delete(void *ptr);"); |
| 1720 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1721 | |
Daniel Jasper | dc2efa1 | 2013-02-15 19:24:08 +0000 | [diff] [blame] | 1722 | verifyFormat( |
| 1723 | "ostream &operator<<(ostream &OutputStream,\n" |
| 1724 | " SomeReallyLongType WithSomeReallyLongValue);"); |
| 1725 | |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1726 | verifyGoogleFormat("operator void*();"); |
| 1727 | verifyGoogleFormat("operator SomeType<SomeType<int>>();"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Daniel Jasper | 088dab5 | 2013-01-11 16:09:04 +0000 | [diff] [blame] | 1730 | TEST_F(FormatTest, UnderstandsNewAndDelete) { |
Daniel Jasper | 37eff83 | 2013-02-23 08:07:18 +0000 | [diff] [blame] | 1731 | verifyFormat("void f() {\n" |
| 1732 | " A *a = new A;\n" |
| 1733 | " A *a = new (placement) A;\n" |
| 1734 | " delete a;\n" |
| 1735 | " delete (A *)a;\n" |
| 1736 | "}"); |
Daniel Jasper | 088dab5 | 2013-01-11 16:09:04 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1739 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1740 | verifyFormat("int *f(int *a) {}"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1741 | verifyFormat("int main(int argc, char **argv) {}"); |
Daniel Jasper | d0f349b | 2013-02-18 12:44:35 +0000 | [diff] [blame] | 1742 | verifyFormat("Test::Test(int b) : a(b * b) {}"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1743 | verifyIndependentOfContext("f(a, *a);"); |
| 1744 | verifyIndependentOfContext("f(*a);"); |
| 1745 | verifyIndependentOfContext("int a = b * 10;"); |
| 1746 | verifyIndependentOfContext("int a = 10 * b;"); |
| 1747 | verifyIndependentOfContext("int a = b * c;"); |
| 1748 | verifyIndependentOfContext("int a += b * c;"); |
| 1749 | verifyIndependentOfContext("int a -= b * c;"); |
| 1750 | verifyIndependentOfContext("int a *= b * c;"); |
| 1751 | verifyIndependentOfContext("int a /= b * c;"); |
| 1752 | verifyIndependentOfContext("int a = *b;"); |
| 1753 | verifyIndependentOfContext("int a = *b * c;"); |
| 1754 | verifyIndependentOfContext("int a = b * *c;"); |
| 1755 | verifyIndependentOfContext("return 10 * b;"); |
| 1756 | verifyIndependentOfContext("return *b * *c;"); |
| 1757 | verifyIndependentOfContext("return a & ~b;"); |
| 1758 | verifyIndependentOfContext("f(b ? *c : *d);"); |
| 1759 | verifyIndependentOfContext("int a = b ? *c : *d;"); |
| 1760 | verifyIndependentOfContext("*b = a;"); |
| 1761 | verifyIndependentOfContext("a * ~b;"); |
| 1762 | verifyIndependentOfContext("a * !b;"); |
| 1763 | verifyIndependentOfContext("a * +b;"); |
| 1764 | verifyIndependentOfContext("a * -b;"); |
| 1765 | verifyIndependentOfContext("a * ++b;"); |
| 1766 | verifyIndependentOfContext("a * --b;"); |
| 1767 | verifyIndependentOfContext("a[4] * b;"); |
| 1768 | verifyIndependentOfContext("f() * b;"); |
| 1769 | verifyIndependentOfContext("a * [self dostuff];"); |
| 1770 | verifyIndependentOfContext("a * (a + b);"); |
| 1771 | verifyIndependentOfContext("(a *)(a + b);"); |
| 1772 | verifyIndependentOfContext("int *pa = (int *)&a;"); |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 1773 | verifyIndependentOfContext("return sizeof(int **);"); |
| 1774 | verifyIndependentOfContext("return sizeof(int ******);"); |
| 1775 | verifyIndependentOfContext("return (int **&)a;"); |
| 1776 | verifyGoogleFormat("return sizeof(int**);"); |
| 1777 | verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); |
| 1778 | verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); |
| 1779 | // FIXME: The newline is wrong. |
| 1780 | verifyFormat("auto a = [](int **&, int ***) {}\n;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 1781 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1782 | verifyIndependentOfContext("InvalidRegions[*R] = 0;"); |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 1783 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1784 | verifyIndependentOfContext("A<int *> a;"); |
| 1785 | verifyIndependentOfContext("A<int **> a;"); |
| 1786 | verifyIndependentOfContext("A<int *, int *> a;"); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 1787 | verifyIndependentOfContext( |
| 1788 | "const char *const p = reinterpret_cast<const char *const>(q);"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1789 | verifyIndependentOfContext("A<int **, int **> a;"); |
Daniel Jasper | 6b5ba8b | 2013-02-06 10:57:42 +0000 | [diff] [blame] | 1790 | verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1791 | |
Daniel Jasper | 2db356d | 2013-01-08 20:03:18 +0000 | [diff] [blame] | 1792 | verifyFormat( |
| 1793 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1794 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1795 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1796 | verifyGoogleFormat("int main(int argc, char** argv) {}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1797 | verifyGoogleFormat("A<int*> a;"); |
| 1798 | verifyGoogleFormat("A<int**> a;"); |
| 1799 | verifyGoogleFormat("A<int*, int*> a;"); |
| 1800 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1801 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 1802 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | 48bd7b7 | 2013-01-16 16:04:06 +0000 | [diff] [blame] | 1803 | verifyGoogleFormat("Type* t = **x;"); |
| 1804 | verifyGoogleFormat("Type* t = *++*x;"); |
| 1805 | verifyGoogleFormat("*++*x;"); |
| 1806 | verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); |
| 1807 | verifyGoogleFormat("Type* t = x++ * y;"); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 1808 | verifyGoogleFormat( |
| 1809 | "const char* const p = reinterpret_cast<const char* const>(q);"); |
Manuel Klimek | 1b6f4bd | 2013-01-14 10:58:01 +0000 | [diff] [blame] | 1810 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1811 | verifyIndependentOfContext("a = *(x + y);"); |
| 1812 | verifyIndependentOfContext("a = &(x + y);"); |
| 1813 | verifyIndependentOfContext("*(x + y).call();"); |
| 1814 | verifyIndependentOfContext("&(x + y)->call();"); |
| 1815 | verifyIndependentOfContext("&(*I).first"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1816 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1817 | verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1818 | verifyFormat( |
| 1819 | "int *MyValues = {\n" |
| 1820 | " *A, // Operator detection might be confused by the '{'\n" |
| 1821 | " *BB // Operator detection might be confused by previous comment\n" |
| 1822 | "};"); |
Nico Weber | 5096a44 | 2013-01-17 17:17:19 +0000 | [diff] [blame] | 1823 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1824 | verifyIndependentOfContext("if (int *a = &b)"); |
| 1825 | verifyIndependentOfContext("if (int &a = *b)"); |
| 1826 | verifyIndependentOfContext("if (a & b[i])"); |
| 1827 | verifyIndependentOfContext("if (a::b::c::d & b[i])"); |
| 1828 | verifyIndependentOfContext("if (*b[i])"); |
| 1829 | verifyIndependentOfContext("if (int *a = (&b))"); |
| 1830 | verifyIndependentOfContext("while (int *a = &b)"); |
Daniel Jasper | 20d3583 | 2013-01-23 12:58:14 +0000 | [diff] [blame] | 1831 | verifyFormat("void f() {\n" |
| 1832 | " for (const int &v : Values) {\n" |
| 1833 | " }\n" |
| 1834 | "}"); |
Daniel Jasper | d0f349b | 2013-02-18 12:44:35 +0000 | [diff] [blame] | 1835 | verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); |
| 1836 | verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); |
Daniel Jasper | ffee171 | 2013-01-22 11:46:26 +0000 | [diff] [blame] | 1837 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1838 | verifyIndependentOfContext("A = new SomeType *[Length]();"); |
Daniel Jasper | ffee171 | 2013-01-22 11:46:26 +0000 | [diff] [blame] | 1839 | verifyGoogleFormat("A = new SomeType* [Length]();"); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1840 | |
| 1841 | EXPECT_EQ("int *a;\n" |
| 1842 | "int *a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1843 | "int *a;", |
| 1844 | format("int *a;\n" |
| 1845 | "int* a;\n" |
| 1846 | "int *a;", |
| 1847 | getGoogleStyle())); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1848 | EXPECT_EQ("int* a;\n" |
| 1849 | "int* a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1850 | "int* a;", |
| 1851 | format("int* a;\n" |
| 1852 | "int* a;\n" |
| 1853 | "int *a;", |
| 1854 | getGoogleStyle())); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1855 | EXPECT_EQ("int *a;\n" |
| 1856 | "int *a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1857 | "int *a;", |
| 1858 | format("int *a;\n" |
| 1859 | "int * a;\n" |
| 1860 | "int * a;", |
| 1861 | getGoogleStyle())); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Manuel Klimek | a32a7fd | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 1864 | TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { |
| 1865 | verifyFormat("void f() {\n" |
| 1866 | " x[aaaaaaaaa -\n" |
| 1867 | " b] = 23;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1868 | "}", |
| 1869 | getLLVMStyleWithColumns(15)); |
Manuel Klimek | a32a7fd | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 1872 | TEST_F(FormatTest, FormatsCasts) { |
| 1873 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 1874 | verifyFormat("Type *A = (Type *)P;"); |
| 1875 | verifyFormat("Type *A = (vector<Type *, int *>)P;"); |
| 1876 | verifyFormat("int a = (int)(2.0f);"); |
| 1877 | |
| 1878 | // FIXME: These also need to be identified. |
| 1879 | verifyFormat("int a = (int) 2.0f;"); |
| 1880 | verifyFormat("int a = (int) * b;"); |
| 1881 | |
| 1882 | // These are not casts. |
| 1883 | verifyFormat("void f(int *) {}"); |
Nico Weber | 6fa7e74 | 2013-02-13 04:32:57 +0000 | [diff] [blame] | 1884 | verifyFormat("f(foo)->b;"); |
| 1885 | verifyFormat("f(foo).b;"); |
| 1886 | verifyFormat("f(foo)(b);"); |
| 1887 | verifyFormat("f(foo)[b];"); |
| 1888 | verifyFormat("[](foo) { return 4; }(bar)];"); |
| 1889 | verifyFormat("(*funptr)(foo)[4];"); |
| 1890 | verifyFormat("funptrs[4](foo)[4];"); |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 1891 | verifyFormat("void f(int *);"); |
| 1892 | verifyFormat("void f(int *) = 0;"); |
| 1893 | verifyFormat("void f(SmallVector<int>) {}"); |
| 1894 | verifyFormat("void f(SmallVector<int>);"); |
| 1895 | verifyFormat("void f(SmallVector<int>) = 0;"); |
Nico Weber | 7728057 | 2013-02-09 18:02:07 +0000 | [diff] [blame] | 1896 | verifyFormat("void f(int i = (kValue) * kMask) {}"); |
| 1897 | verifyFormat("void f(int i = (kA * kB) & kMask) {}"); |
Nico Weber | 6fa7e74 | 2013-02-13 04:32:57 +0000 | [diff] [blame] | 1898 | verifyFormat("int a = sizeof(int) * b;"); |
| 1899 | verifyFormat("int a = alignof(int) * b;"); |
Daniel Jasper | 37eff83 | 2013-02-23 08:07:18 +0000 | [diff] [blame] | 1900 | |
| 1901 | // These are not casts, but at some point were confused with casts. |
| 1902 | verifyFormat("virtual void foo(int *) override;"); |
| 1903 | verifyFormat("virtual void foo(char &) const;"); |
| 1904 | verifyFormat("virtual void foo(int *a, char *) const;"); |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1907 | TEST_F(FormatTest, FormatsFunctionTypes) { |
| 1908 | // FIXME: Determine the cases that need a space after the return type and fix. |
| 1909 | verifyFormat("A<bool()> a;"); |
| 1910 | verifyFormat("A<SomeType()> a;"); |
| 1911 | verifyFormat("A<void(*)(int, std::string)> a;"); |
| 1912 | |
| 1913 | verifyFormat("int(*func)(void *);"); |
| 1914 | } |
| 1915 | |
Daniel Jasper | 83f25ba | 2013-01-28 15:16:31 +0000 | [diff] [blame] | 1916 | TEST_F(FormatTest, BreaksFunctionDeclarations) { |
| 1917 | verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1918 | " int LoooooooooooooooooooongParam2) {}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1919 | verifyFormat( |
Daniel Jasper | 83f25ba | 2013-01-28 15:16:31 +0000 | [diff] [blame] | 1920 | "TypeSpecDecl *\n" |
| 1921 | "TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1922 | " IdentifierIn *II, Type *T) {}"); |
Daniel Jasper | d75ff64 | 2013-01-28 15:40:20 +0000 | [diff] [blame] | 1923 | verifyGoogleFormat( |
| 1924 | "TypeSpecDecl* TypeSpecDecl::Create(\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1925 | " ASTContext& C, DeclContext* DC, SourceLocation L) {}"); |
Daniel Jasper | a03ab10 | 2013-02-13 20:33:44 +0000 | [diff] [blame] | 1926 | verifyGoogleFormat( |
| 1927 | "some_namespace::LongReturnType\n" |
| 1928 | "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1929 | " int first_long_parameter, int second_parameter) {}"); |
Daniel Jasper | a03ab10 | 2013-02-13 20:33:44 +0000 | [diff] [blame] | 1930 | |
| 1931 | verifyGoogleFormat("template <typename T>\n" |
| 1932 | "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1933 | "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 1936 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 1937 | verifyFormat("(a)->b();"); |
| 1938 | verifyFormat("--a;"); |
| 1939 | } |
| 1940 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1941 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 1942 | verifyFormat("#include <string>\n" |
| 1943 | "#include <a/b/c.h>\n" |
| 1944 | "#include \"a/b/string\"\n" |
| 1945 | "#include \"string.h\"\n" |
| 1946 | "#include \"string.h\"\n" |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 1947 | "#include <a-a>\n" |
Daniel Jasper | 3a20441 | 2013-02-23 07:46:38 +0000 | [diff] [blame] | 1948 | "#include < path with space >\n" |
| 1949 | "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", |
| 1950 | getLLVMStyleWithColumns(35)); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 1951 | |
Daniel Jasper | 8134e1e | 2013-01-13 08:12:18 +0000 | [diff] [blame] | 1952 | verifyFormat("#import <string>"); |
| 1953 | verifyFormat("#import <a/b/c.h>"); |
| 1954 | verifyFormat("#import \"a/b/string\""); |
| 1955 | verifyFormat("#import \"string.h\""); |
| 1956 | verifyFormat("#import \"string.h\""); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1959 | //===----------------------------------------------------------------------===// |
| 1960 | // Error recovery tests. |
| 1961 | //===----------------------------------------------------------------------===// |
| 1962 | |
Daniel Jasper | c5cfa49 | 2013-02-14 09:19:04 +0000 | [diff] [blame] | 1963 | TEST_F(FormatTest, IncompleteParameterLists) { |
| 1964 | verifyGoogleFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" |
| 1965 | " double *min_x,\n" |
| 1966 | " double *max_x,\n" |
| 1967 | " double *min_y,\n" |
| 1968 | " double *max_y,\n" |
| 1969 | " double *min_z,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1970 | " double *max_z, ) {}"); |
Daniel Jasper | c5cfa49 | 2013-02-14 09:19:04 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 1973 | TEST_F(FormatTest, IncorrectCodeTrailingStuff) { |
Alexander Kornienko | 51c23fa | 2013-01-16 11:45:16 +0000 | [diff] [blame] | 1974 | verifyFormat("void f() { return; }\n42"); |
| 1975 | verifyFormat("void f() {\n" |
| 1976 | " if (0)\n" |
| 1977 | " return;\n" |
| 1978 | "}\n" |
| 1979 | "42"); |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 1980 | verifyFormat("void f() { return }\n42"); |
| 1981 | verifyFormat("void f() {\n" |
| 1982 | " if (0)\n" |
| 1983 | " return\n" |
| 1984 | "}\n" |
| 1985 | "42"); |
| 1986 | } |
| 1987 | |
| 1988 | TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { |
| 1989 | EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); |
| 1990 | EXPECT_EQ("void f() {\n" |
| 1991 | " if (a)\n" |
| 1992 | " return\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1993 | "}", |
| 1994 | format("void f ( ) { if ( a ) return }")); |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 1995 | EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); |
| 1996 | EXPECT_EQ("namespace N {\n" |
| 1997 | "void f() {}\n" |
| 1998 | "void g()\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1999 | "}", |
| 2000 | format("namespace N { void f( ) { } void g( ) }")); |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 2003 | TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { |
| 2004 | verifyFormat("int aaaaaaaa =\n" |
| 2005 | " // Overly long comment\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2006 | " b;", |
| 2007 | getLLVMStyleWithColumns(20)); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 2008 | verifyFormat("function(\n" |
| 2009 | " ShortArgument,\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2010 | " LoooooooooooongArgument);\n", |
| 2011 | getLLVMStyleWithColumns(20)); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2014 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 2015 | verifyFormat("public:"); |
| 2016 | verifyFormat("class A {\n" |
| 2017 | "public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2018 | " void f() {}\n" |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2019 | "};"); |
| 2020 | verifyFormat("public\n" |
| 2021 | "int qwerty;"); |
| 2022 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2023 | "B {}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2024 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2025 | "{}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2026 | verifyFormat("public\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 2027 | "B { int x; }"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2028 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2029 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2030 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { verifyFormat("{"); } |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2031 | |
| 2032 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2033 | verifyFormat("do {\n}"); |
| 2034 | verifyFormat("do {\n}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2035 | "f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2036 | verifyFormat("do {\n}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2037 | "wheeee(fun);"); |
| 2038 | verifyFormat("do {\n" |
| 2039 | " f();\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 2040 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 2043 | TEST_F(FormatTest, IncorrectCodeMissingParens) { |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 2044 | verifyFormat("if {\n foo;\n foo();\n}"); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 2045 | verifyFormat("switch {\n foo;\n foo();\n}"); |
| 2046 | verifyFormat("for {\n foo;\n foo();\n}"); |
| 2047 | verifyFormat("while {\n foo;\n foo();\n}"); |
| 2048 | verifyFormat("do {\n foo;\n foo();\n} while;"); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 2051 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 2052 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 2053 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2056 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2057 | EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); |
| 2058 | EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); |
| 2059 | EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); |
| 2060 | EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2061 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2062 | EXPECT_EQ("{\n" |
| 2063 | " {\n" |
| 2064 | " breakme(\n" |
| 2065 | " qwe);\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2066 | "}\n", |
| 2067 | format("{\n" |
| 2068 | " {\n" |
| 2069 | " breakme(qwe);\n" |
| 2070 | "}\n", |
| 2071 | getLLVMStyleWithColumns(10))); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2074 | TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2075 | verifyFormat("int x = {\n" |
| 2076 | " avariable,\n" |
| 2077 | " b(alongervariable)\n" |
| 2078 | "};", |
| 2079 | getLLVMStyleWithColumns(25)); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 2082 | TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { |
| 2083 | verifyFormat("return (a)(b) { 1, 2, 3 };"); |
| 2084 | } |
| 2085 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2086 | TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 2087 | // FIXME: This is bad, find a better and more generic solution. |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2088 | verifyFormat( |
| 2089 | "Aaa({\n" |
| 2090 | " int i;\n" |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 2091 | "},\n" |
| 2092 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
| 2093 | " ccccccccccccccccc));"); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 2096 | TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { |
| 2097 | verifyFormat("void f() { return 42; }"); |
| 2098 | verifyFormat("void f() {\n" |
| 2099 | " // Comment\n" |
| 2100 | "}"); |
| 2101 | verifyFormat("{\n" |
| 2102 | "#error {\n" |
| 2103 | " int a;\n" |
| 2104 | "}"); |
| 2105 | verifyFormat("{\n" |
| 2106 | " int a;\n" |
| 2107 | "#error {\n" |
| 2108 | "}"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2109 | |
| 2110 | verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); |
| 2111 | verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); |
| 2112 | |
| 2113 | verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); |
| 2114 | verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2117 | TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { |
| 2118 | // Elaborate type variable declarations. |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 2119 | verifyFormat("struct foo a = { bar };\nint n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2120 | verifyFormat("class foo a = { bar };\nint n;"); |
| 2121 | verifyFormat("union foo a = { bar };\nint n;"); |
| 2122 | |
| 2123 | // Elaborate types inside function definitions. |
| 2124 | verifyFormat("struct foo f() {}\nint n;"); |
| 2125 | verifyFormat("class foo f() {}\nint n;"); |
| 2126 | verifyFormat("union foo f() {}\nint n;"); |
| 2127 | |
| 2128 | // Templates. |
| 2129 | verifyFormat("template <class X> void f() {}\nint n;"); |
| 2130 | verifyFormat("template <struct X> void f() {}\nint n;"); |
| 2131 | verifyFormat("template <union X> void f() {}\nint n;"); |
| 2132 | |
| 2133 | // Actual definitions... |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2134 | verifyFormat("struct {\n} n;"); |
| 2135 | verifyFormat( |
| 2136 | "template <template <class T, class Y>, class Z> class X {\n} n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2137 | verifyFormat("union Z {\n int n;\n} x;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2138 | verifyFormat("class MACRO Z {\n} n;"); |
| 2139 | verifyFormat("class MACRO(X) Z {\n} n;"); |
| 2140 | verifyFormat("class __attribute__(X) Z {\n} n;"); |
| 2141 | verifyFormat("class __declspec(X) Z {\n} n;"); |
Manuel Klimek | b8b1ce1 | 2013-02-06 15:57:54 +0000 | [diff] [blame] | 2142 | verifyFormat("class A##B##C {\n} n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2143 | |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 2144 | // Redefinition from nested context: |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2145 | verifyFormat("class A::B::C {\n} n;"); |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 2146 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 2147 | // Template definitions. |
| 2148 | // FIXME: This is still incorrectly handled at the formatter side. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2149 | verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 2150 | |
| 2151 | // FIXME: |
| 2152 | // This now gets parsed incorrectly as class definition. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2153 | // verifyFormat("class A<int> f() {\n}\nint n;"); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 2154 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2155 | // Elaborate types where incorrectly parsing the structural element would |
| 2156 | // break the indent. |
| 2157 | verifyFormat("if (true)\n" |
| 2158 | " class X x;\n" |
| 2159 | "else\n" |
| 2160 | " f();\n"); |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 2163 | TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { |
| 2164 | verifyFormat("#error Leave all white!!!!! space* alone!\n"); |
| 2165 | verifyFormat("#warning Leave all white!!!!! space* alone!\n"); |
| 2166 | EXPECT_EQ("#error 1", format(" # error 1")); |
| 2167 | EXPECT_EQ("#warning 1", format(" # warning 1")); |
| 2168 | } |
| 2169 | |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 2170 | TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { |
| 2171 | FormatStyle AllowsMergedIf = getGoogleStyle(); |
| 2172 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; |
| 2173 | verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); |
| 2174 | verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 2175 | verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); |
| 2176 | EXPECT_EQ("if (true) return 42;", |
| 2177 | format("if (true)\nreturn 42;", AllowsMergedIf)); |
| 2178 | FormatStyle ShortMergedIf = AllowsMergedIf; |
| 2179 | ShortMergedIf.ColumnLimit = 25; |
| 2180 | verifyFormat("#define A \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2181 | " if (true) return 42;", |
| 2182 | ShortMergedIf); |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 2183 | verifyFormat("#define A \\\n" |
| 2184 | " f(); \\\n" |
| 2185 | " if (true)\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2186 | "#define B", |
| 2187 | ShortMergedIf); |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 2188 | verifyFormat("#define A \\\n" |
| 2189 | " f(); \\\n" |
| 2190 | " if (true)\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2191 | "g();", |
| 2192 | ShortMergedIf); |
Manuel Klimek | 0fbe008 | 2013-01-21 14:16:56 +0000 | [diff] [blame] | 2193 | verifyFormat("{\n" |
| 2194 | "#ifdef A\n" |
| 2195 | " // Comment\n" |
| 2196 | " if (true) continue;\n" |
| 2197 | "#endif\n" |
| 2198 | " // Comment\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2199 | " if (true) continue;", |
| 2200 | ShortMergedIf); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2203 | TEST_F(FormatTest, BlockCommentsInControlLoops) { |
| 2204 | verifyFormat("if (0) /* a comment in a strange place */ {\n" |
| 2205 | " f();\n" |
| 2206 | "}"); |
| 2207 | verifyFormat("if (0) /* a comment in a strange place */ {\n" |
| 2208 | " f();\n" |
| 2209 | "} /* another comment */ else /* comment #3 */ {\n" |
| 2210 | " g();\n" |
| 2211 | "}"); |
| 2212 | verifyFormat("while (0) /* a comment in a strange place */ {\n" |
| 2213 | " f();\n" |
| 2214 | "}"); |
| 2215 | verifyFormat("for (;;) /* a comment in a strange place */ {\n" |
| 2216 | " f();\n" |
| 2217 | "}"); |
| 2218 | verifyFormat("do /* a comment in a strange place */ {\n" |
| 2219 | " f();\n" |
| 2220 | "} /* another comment */ while (0);"); |
| 2221 | } |
| 2222 | |
| 2223 | TEST_F(FormatTest, BlockComments) { |
| 2224 | EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", |
| 2225 | format("/* *//* */ /* */\n/* *//* */ /* */")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2226 | EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2227 | EXPECT_EQ("#define A /* */\\\n" |
| 2228 | " b\n" |
| 2229 | "/* */\n" |
| 2230 | "someCall(\n" |
| 2231 | " parameter);", |
| 2232 | format("#define A /* */ b\n" |
| 2233 | "/* */\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2234 | "someCall(parameter);", |
| 2235 | getLLVMStyleWithColumns(15))); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2236 | |
| 2237 | EXPECT_EQ("#define A\n" |
| 2238 | "/* */ someCall(\n" |
| 2239 | " parameter);", |
| 2240 | format("#define A\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2241 | "/* */someCall(parameter);", |
| 2242 | getLLVMStyleWithColumns(15))); |
Daniel Jasper | 2752ff3 | 2013-02-04 07:32:14 +0000 | [diff] [blame] | 2243 | |
| 2244 | EXPECT_EQ("someFunction(1, /* comment 1 */\n" |
| 2245 | " 2, /* comment 2 */\n" |
| 2246 | " 3, /* comment 3 */\n" |
Daniel Jasper | cda1650 | 2013-02-04 08:34:57 +0000 | [diff] [blame] | 2247 | " aaaa,\n" |
| 2248 | " bbbb);", |
Daniel Jasper | 2752ff3 | 2013-02-04 07:32:14 +0000 | [diff] [blame] | 2249 | format("someFunction (1, /* comment 1 */\n" |
| 2250 | " 2, /* comment 2 */ \n" |
| 2251 | " 3, /* comment 3 */\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2252 | "aaaa, bbbb );", |
| 2253 | getGoogleStyle())); |
Daniel Jasper | 36ba0ff | 2013-02-06 15:23:09 +0000 | [diff] [blame] | 2254 | verifyFormat( |
| 2255 | "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 2256 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 2257 | EXPECT_EQ( |
| 2258 | "bool aaaaaaaaaaaaa = /* trailing comment */\n" |
| 2259 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 2260 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", |
| 2261 | format( |
| 2262 | "bool aaaaaaaaaaaaa = /* trailing comment */\n" |
| 2263 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 2264 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); |
Daniel Jasper | 15417ef | 2013-02-06 20:07:35 +0000 | [diff] [blame] | 2265 | EXPECT_EQ( |
| 2266 | "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" |
| 2267 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" |
| 2268 | "int cccccccccccccccccccccccccccccc; /* comment */\n", |
| 2269 | format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" |
| 2270 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" |
| 2271 | "int cccccccccccccccccccccccccccccc; /* comment */\n")); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 2274 | TEST_F(FormatTest, BlockCommentsInMacros) { |
| 2275 | EXPECT_EQ("#define A \\\n" |
| 2276 | " { \\\n" |
| 2277 | " /* one line */ \\\n" |
| 2278 | " someCall();", |
| 2279 | format("#define A { \\\n" |
| 2280 | " /* one line */ \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2281 | " someCall();", |
| 2282 | getLLVMStyleWithColumns(20))); |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 2283 | EXPECT_EQ("#define A \\\n" |
| 2284 | " { \\\n" |
| 2285 | " /* previous */ \\\n" |
| 2286 | " /* one line */ \\\n" |
| 2287 | " someCall();", |
| 2288 | format("#define A { \\\n" |
| 2289 | " /* previous */ \\\n" |
| 2290 | " /* one line */ \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2291 | " someCall();", |
| 2292 | getLLVMStyleWithColumns(20))); |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { |
| 2296 | // FIXME: This is not what we want... |
| 2297 | verifyFormat("{\n" |
| 2298 | "// a" |
| 2299 | "// b"); |
| 2300 | } |
| 2301 | |
Manuel Klimek | 092a2c7 | 2013-01-23 10:09:28 +0000 | [diff] [blame] | 2302 | TEST_F(FormatTest, FormatStarDependingOnContext) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 2303 | verifyFormat("void f(int *a);"); |
| 2304 | verifyFormat("void f() { f(fint * b); }"); |
Manuel Klimek | 836b58f | 2013-01-23 11:03:04 +0000 | [diff] [blame] | 2305 | verifyFormat("class A {\n void f(int *a);\n};"); |
| 2306 | verifyFormat("class A {\n int *a;\n};"); |
| 2307 | verifyFormat("namespace a {\n" |
| 2308 | "namespace b {\n" |
| 2309 | "class A {\n" |
| 2310 | " void f() {}\n" |
| 2311 | " int *a;\n" |
| 2312 | "};\n" |
| 2313 | "}\n" |
| 2314 | "}"); |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Manuel Klimek | 092a2c7 | 2013-01-23 10:09:28 +0000 | [diff] [blame] | 2317 | TEST_F(FormatTest, SpecialTokensAtEndOfLine) { |
| 2318 | verifyFormat("while"); |
| 2319 | verifyFormat("operator"); |
| 2320 | } |
| 2321 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2322 | //===----------------------------------------------------------------------===// |
| 2323 | // Objective-C tests. |
| 2324 | //===----------------------------------------------------------------------===// |
| 2325 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 2326 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 2327 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 2328 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 2329 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 2330 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 2331 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 2332 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 2333 | format("-(NSInteger)Method3:(id)anObject;")); |
| 2334 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 2335 | format("-(NSInteger)Method4:(id)anObject;")); |
| 2336 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 2337 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 2338 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 2339 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 2340 | EXPECT_EQ( |
| 2341 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2342 | format( |
| 2343 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 2344 | |
| 2345 | // Very long objectiveC method declaration. |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 2346 | verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" |
| 2347 | " inRange:(NSRange)range\n" |
| 2348 | " outRange:(NSRange)out_range\n" |
| 2349 | " outRange1:(NSRange)out_range1\n" |
| 2350 | " outRange2:(NSRange)out_range2\n" |
| 2351 | " outRange3:(NSRange)out_range3\n" |
| 2352 | " outRange4:(NSRange)out_range4\n" |
| 2353 | " outRange5:(NSRange)out_range5\n" |
| 2354 | " outRange6:(NSRange)out_range6\n" |
| 2355 | " outRange7:(NSRange)out_range7\n" |
| 2356 | " outRange8:(NSRange)out_range8\n" |
| 2357 | " outRange9:(NSRange)out_range9;"); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 2358 | |
| 2359 | verifyFormat("- (int)sum:(vector<int>)numbers;"); |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2360 | verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 2361 | // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC |
| 2362 | // protocol lists (but not for template classes): |
| 2363 | //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 2364 | |
| 2365 | verifyFormat("- (int(*)())foo:(int(*)())f;"); |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2366 | verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 2367 | |
| 2368 | // If there's no return type (very rare in practice!), LLVM and Google style |
| 2369 | // agree. |
| 2370 | verifyFormat("- foo:(int)f;"); |
| 2371 | verifyGoogleFormat("- foo:(int)foo;"); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 2374 | TEST_F(FormatTest, FormatObjCBlocks) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 2375 | verifyFormat("int (^Block)(int, int);"); |
| 2376 | verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2379 | TEST_F(FormatTest, FormatObjCInterface) { |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2380 | verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2381 | "@public\n" |
| 2382 | " int field1;\n" |
| 2383 | "@protected\n" |
| 2384 | " int field2;\n" |
| 2385 | "@private\n" |
| 2386 | " int field3;\n" |
| 2387 | "@package\n" |
| 2388 | " int field4;\n" |
| 2389 | "}\n" |
| 2390 | "+ (id)init;\n" |
| 2391 | "@end"); |
| 2392 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2393 | verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 2394 | " @public\n" |
| 2395 | " int field1;\n" |
| 2396 | " @protected\n" |
| 2397 | " int field2;\n" |
| 2398 | " @private\n" |
| 2399 | " int field3;\n" |
| 2400 | " @package\n" |
| 2401 | " int field4;\n" |
| 2402 | "}\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2403 | "+ (id)init;\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2404 | "@end"); |
| 2405 | |
Nico Weber | f5ecfa5 | 2013-01-22 16:53:59 +0000 | [diff] [blame] | 2406 | verifyFormat("@interface /* wait for it */ Foo\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2407 | "+ (id)init;\n" |
| 2408 | "// Look, a comment!\n" |
| 2409 | "- (int)answerWith:(int)i;\n" |
| 2410 | "@end"); |
| 2411 | |
| 2412 | verifyFormat("@interface Foo\n" |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 2413 | "@end\n" |
| 2414 | "@interface Bar\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2415 | "@end"); |
| 2416 | |
| 2417 | verifyFormat("@interface Foo : Bar\n" |
| 2418 | "+ (id)init;\n" |
| 2419 | "@end"); |
| 2420 | |
Nico Weber | f5ecfa5 | 2013-01-22 16:53:59 +0000 | [diff] [blame] | 2421 | verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2422 | "+ (id)init;\n" |
| 2423 | "@end"); |
| 2424 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2425 | verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2426 | "+ (id)init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2427 | "@end"); |
| 2428 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 2429 | verifyFormat("@interface Foo (HackStuff)\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2430 | "+ (id)init;\n" |
| 2431 | "@end"); |
| 2432 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 2433 | verifyFormat("@interface Foo ()\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2434 | "+ (id)init;\n" |
| 2435 | "@end"); |
| 2436 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2437 | verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2438 | "+ (id)init;\n" |
| 2439 | "@end"); |
| 2440 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2441 | verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2442 | "+ (id)init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2443 | "@end"); |
| 2444 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2445 | verifyFormat("@interface Foo {\n" |
| 2446 | " int _i;\n" |
| 2447 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2448 | "+ (id)init;\n" |
| 2449 | "@end"); |
| 2450 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2451 | verifyFormat("@interface Foo : Bar {\n" |
| 2452 | " int _i;\n" |
| 2453 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2454 | "+ (id)init;\n" |
| 2455 | "@end"); |
| 2456 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2457 | verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" |
| 2458 | " int _i;\n" |
| 2459 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2460 | "+ (id)init;\n" |
| 2461 | "@end"); |
| 2462 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2463 | verifyFormat("@interface Foo (HackStuff) {\n" |
| 2464 | " int _i;\n" |
| 2465 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2466 | "+ (id)init;\n" |
| 2467 | "@end"); |
| 2468 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2469 | verifyFormat("@interface Foo () {\n" |
| 2470 | " int _i;\n" |
| 2471 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2472 | "+ (id)init;\n" |
| 2473 | "@end"); |
| 2474 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2475 | verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" |
| 2476 | " int _i;\n" |
| 2477 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2478 | "+ (id)init;\n" |
| 2479 | "@end"); |
| 2480 | } |
| 2481 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2482 | TEST_F(FormatTest, FormatObjCImplementation) { |
| 2483 | verifyFormat("@implementation Foo : NSObject {\n" |
| 2484 | "@public\n" |
| 2485 | " int field1;\n" |
| 2486 | "@protected\n" |
| 2487 | " int field2;\n" |
| 2488 | "@private\n" |
| 2489 | " int field3;\n" |
| 2490 | "@package\n" |
| 2491 | " int field4;\n" |
| 2492 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2493 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2494 | "@end"); |
| 2495 | |
| 2496 | verifyGoogleFormat("@implementation Foo : NSObject {\n" |
| 2497 | " @public\n" |
| 2498 | " int field1;\n" |
| 2499 | " @protected\n" |
| 2500 | " int field2;\n" |
| 2501 | " @private\n" |
| 2502 | " int field3;\n" |
| 2503 | " @package\n" |
| 2504 | " int field4;\n" |
| 2505 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2506 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2507 | "@end"); |
| 2508 | |
| 2509 | verifyFormat("@implementation Foo\n" |
| 2510 | "+ (id)init {\n" |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 2511 | " if (true)\n" |
| 2512 | " return nil;\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2513 | "}\n" |
| 2514 | "// Look, a comment!\n" |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2515 | "- (int)answerWith:(int)i {\n" |
| 2516 | " return i;\n" |
| 2517 | "}\n" |
Nico Weber | 67015ed | 2013-01-11 21:14:08 +0000 | [diff] [blame] | 2518 | "+ (int)answerWith:(int)i {\n" |
| 2519 | " return i;\n" |
| 2520 | "}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2521 | "@end"); |
| 2522 | |
| 2523 | verifyFormat("@implementation Foo\n" |
| 2524 | "@end\n" |
| 2525 | "@implementation Bar\n" |
| 2526 | "@end"); |
| 2527 | |
| 2528 | verifyFormat("@implementation Foo : Bar\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2529 | "+ (id)init {\n}\n" |
| 2530 | "- (void)foo {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2531 | "@end"); |
| 2532 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2533 | verifyFormat("@implementation Foo {\n" |
| 2534 | " int _i;\n" |
| 2535 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2536 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2537 | "@end"); |
| 2538 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2539 | verifyFormat("@implementation Foo : Bar {\n" |
| 2540 | " int _i;\n" |
| 2541 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2542 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2543 | "@end"); |
| 2544 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 2545 | verifyFormat("@implementation Foo (HackStuff)\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2546 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2547 | "@end"); |
| 2548 | } |
| 2549 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2550 | TEST_F(FormatTest, FormatObjCProtocol) { |
| 2551 | verifyFormat("@protocol Foo\n" |
| 2552 | "@property(weak) id delegate;\n" |
| 2553 | "- (NSUInteger)numberOfThings;\n" |
| 2554 | "@end"); |
| 2555 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2556 | verifyFormat("@protocol MyProtocol <NSObject>\n" |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2557 | "- (NSUInteger)numberOfThings;\n" |
| 2558 | "@end"); |
| 2559 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2560 | verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2561 | "- (NSUInteger)numberOfThings;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2562 | "@end"); |
| 2563 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2564 | verifyFormat("@protocol Foo;\n" |
| 2565 | "@protocol Bar;\n"); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 2566 | |
| 2567 | verifyFormat("@protocol Foo\n" |
| 2568 | "@end\n" |
| 2569 | "@protocol Bar\n" |
| 2570 | "@end"); |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 2571 | |
| 2572 | verifyFormat("@protocol myProtocol\n" |
| 2573 | "- (void)mandatoryWithInt:(int)i;\n" |
| 2574 | "@optional\n" |
| 2575 | "- (void)optional;\n" |
| 2576 | "@required\n" |
| 2577 | "- (void)required;\n" |
Nico Weber | 880e538 | 2013-01-10 00:42:07 +0000 | [diff] [blame] | 2578 | "@optional\n" |
| 2579 | "@property(assign) int madProp;\n" |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 2580 | "@end\n"); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 2583 | TEST_F(FormatTest, FormatObjCMethodDeclarations) { |
| 2584 | verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" |
| 2585 | " rect:(NSRect)theRect\n" |
| 2586 | " interval:(float)theInterval {\n" |
| 2587 | "}"); |
| 2588 | verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" |
| 2589 | " longKeyword:(NSRect)theRect\n" |
| 2590 | " evenLongerKeyword:(float)theInterval\n" |
| 2591 | " error:(NSError **)theError {\n" |
| 2592 | "}"); |
| 2593 | } |
| 2594 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2595 | TEST_F(FormatTest, FormatObjCMethodExpr) { |
| 2596 | verifyFormat("[foo bar:baz];"); |
| 2597 | verifyFormat("return [foo bar:baz];"); |
| 2598 | verifyFormat("f([foo bar:baz]);"); |
| 2599 | verifyFormat("f(2, [foo bar:baz]);"); |
| 2600 | verifyFormat("f(2, a ? b : c);"); |
| 2601 | verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); |
| 2602 | |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 2603 | // Unary operators. |
| 2604 | verifyFormat("int a = +[foo bar:baz];"); |
| 2605 | verifyFormat("int a = -[foo bar:baz];"); |
| 2606 | verifyFormat("int a = ![foo bar:baz];"); |
| 2607 | verifyFormat("int a = ~[foo bar:baz];"); |
| 2608 | verifyFormat("int a = ++[foo bar:baz];"); |
| 2609 | verifyFormat("int a = --[foo bar:baz];"); |
| 2610 | verifyFormat("int a = sizeof [foo bar:baz];"); |
| 2611 | verifyFormat("int a = alignof [foo bar:baz];"); |
Nico Weber | e8a9798 | 2013-02-06 06:20:11 +0000 | [diff] [blame] | 2612 | verifyFormat("int a = &[foo bar:baz];"); |
| 2613 | verifyFormat("int a = *[foo bar:baz];"); |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 2614 | // FIXME: Make casts work, without breaking f()[4]. |
Nico Weber | 4c2cc60 | 2013-02-13 03:48:27 +0000 | [diff] [blame] | 2615 | //verifyFormat("int a = (int)[foo bar:baz];"); |
Nico Weber | e0dc7c7 | 2013-02-13 04:40:03 +0000 | [diff] [blame] | 2616 | //verifyFormat("return (int)[foo bar:baz];"); |
| 2617 | //verifyFormat("(void)[foo bar:baz];"); |
Nico Weber | 4c2cc60 | 2013-02-13 03:48:27 +0000 | [diff] [blame] | 2618 | verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 2619 | |
| 2620 | // Binary operators. |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2621 | verifyFormat("[foo bar:baz], [foo bar:baz];"); |
| 2622 | verifyFormat("[foo bar:baz] = [foo bar:baz];"); |
| 2623 | verifyFormat("[foo bar:baz] *= [foo bar:baz];"); |
| 2624 | verifyFormat("[foo bar:baz] /= [foo bar:baz];"); |
| 2625 | verifyFormat("[foo bar:baz] %= [foo bar:baz];"); |
| 2626 | verifyFormat("[foo bar:baz] += [foo bar:baz];"); |
| 2627 | verifyFormat("[foo bar:baz] -= [foo bar:baz];"); |
| 2628 | verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); |
| 2629 | verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); |
| 2630 | verifyFormat("[foo bar:baz] &= [foo bar:baz];"); |
| 2631 | verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); |
| 2632 | verifyFormat("[foo bar:baz] |= [foo bar:baz];"); |
| 2633 | verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); |
| 2634 | verifyFormat("[foo bar:baz] || [foo bar:baz];"); |
| 2635 | verifyFormat("[foo bar:baz] && [foo bar:baz];"); |
| 2636 | verifyFormat("[foo bar:baz] | [foo bar:baz];"); |
| 2637 | verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); |
| 2638 | verifyFormat("[foo bar:baz] & [foo bar:baz];"); |
| 2639 | verifyFormat("[foo bar:baz] == [foo bar:baz];"); |
| 2640 | verifyFormat("[foo bar:baz] != [foo bar:baz];"); |
| 2641 | verifyFormat("[foo bar:baz] >= [foo bar:baz];"); |
| 2642 | verifyFormat("[foo bar:baz] <= [foo bar:baz];"); |
| 2643 | verifyFormat("[foo bar:baz] > [foo bar:baz];"); |
| 2644 | verifyFormat("[foo bar:baz] < [foo bar:baz];"); |
| 2645 | verifyFormat("[foo bar:baz] >> [foo bar:baz];"); |
| 2646 | verifyFormat("[foo bar:baz] << [foo bar:baz];"); |
| 2647 | verifyFormat("[foo bar:baz] - [foo bar:baz];"); |
| 2648 | verifyFormat("[foo bar:baz] + [foo bar:baz];"); |
| 2649 | verifyFormat("[foo bar:baz] * [foo bar:baz];"); |
| 2650 | verifyFormat("[foo bar:baz] / [foo bar:baz];"); |
| 2651 | verifyFormat("[foo bar:baz] % [foo bar:baz];"); |
| 2652 | // Whew! |
| 2653 | |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 2654 | verifyFormat("return in[42];"); |
| 2655 | verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" |
| 2656 | "}"); |
| 2657 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2658 | verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); |
| 2659 | verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); |
| 2660 | verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); |
| 2661 | verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); |
| 2662 | verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); |
Nico Weber | 6292dd4 | 2013-01-12 23:41:33 +0000 | [diff] [blame] | 2663 | verifyFormat("[button setAction:@selector(zoomOut:)];"); |
Nico Weber | cd45833 | 2013-01-12 23:48:49 +0000 | [diff] [blame] | 2664 | verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 2665 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2666 | verifyFormat("arr[[self indexForFoo:a]];"); |
| 2667 | verifyFormat("throw [self errorFor:a];"); |
| 2668 | verifyFormat("@throw [self errorFor:a];"); |
| 2669 | |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 2670 | // This tests that the formatter doesn't break after "backing" but before ":", |
| 2671 | // which would be at 80 columns. |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2672 | verifyFormat( |
| 2673 | "void f() {\n" |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 2674 | " if ((self = [super initWithContentRect:contentRect\n" |
| 2675 | " styleMask:styleMask\n" |
| 2676 | " backing:NSBackingStoreBuffered\n" |
| 2677 | " defer:YES]))"); |
| 2678 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2679 | verifyFormat( |
| 2680 | "[foo checkThatBreakingAfterColonWorksOk:\n" |
| 2681 | " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 2682 | |
| 2683 | verifyFormat("[myObj short:arg1 // Force line break\n" |
| 2684 | " longKeyword:arg2\n" |
| 2685 | " evenLongerKeyword:arg3\n" |
| 2686 | " error:arg4];"); |
| 2687 | verifyFormat( |
| 2688 | "void f() {\n" |
| 2689 | " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" |
| 2690 | " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" |
| 2691 | " pos.width(), pos.height())\n" |
| 2692 | " styleMask:NSBorderlessWindowMask\n" |
| 2693 | " backing:NSBackingStoreBuffered\n" |
| 2694 | " defer:NO]);\n" |
| 2695 | "}"); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2696 | verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" |
| 2697 | " with:contentsNativeView];"); |
| 2698 | |
| 2699 | verifyFormat( |
| 2700 | "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" |
| 2701 | " owner:nillllll];"); |
| 2702 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2703 | verifyFormat( |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 2704 | "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2705 | " forType:kBookmarkButtonDragType];"); |
| 2706 | |
| 2707 | verifyFormat("[defaultCenter addObserver:self\n" |
| 2708 | " selector:@selector(willEnterFullscreen)\n" |
| 2709 | " name:kWillEnterFullscreenNotification\n" |
| 2710 | " object:nil];"); |
Daniel Jasper | 9e9e6e0 | 2013-02-06 16:00:26 +0000 | [diff] [blame] | 2711 | verifyFormat("[image_rep drawInRect:drawRect\n" |
| 2712 | " fromRect:NSZeroRect\n" |
| 2713 | " operation:NSCompositeCopy\n" |
| 2714 | " fraction:1.0\n" |
| 2715 | " respectFlipped:NO\n" |
| 2716 | " hints:nil];"); |
Nico Weber | 4ed7f3e | 2013-02-06 16:54:35 +0000 | [diff] [blame] | 2717 | |
| 2718 | verifyFormat( |
| 2719 | "scoped_nsobject<NSTextField> message(\n" |
| 2720 | " // The frame will be fixed up when |-setMessageText:| is called.\n" |
| 2721 | " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 2724 | TEST_F(FormatTest, ObjCAt) { |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2725 | verifyFormat("@autoreleasepool"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2726 | verifyFormat("@catch"); |
| 2727 | verifyFormat("@class"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2728 | verifyFormat("@compatibility_alias"); |
| 2729 | verifyFormat("@defs"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 2730 | verifyFormat("@dynamic"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2731 | verifyFormat("@encode"); |
| 2732 | verifyFormat("@end"); |
| 2733 | verifyFormat("@finally"); |
| 2734 | verifyFormat("@implementation"); |
| 2735 | verifyFormat("@import"); |
| 2736 | verifyFormat("@interface"); |
| 2737 | verifyFormat("@optional"); |
| 2738 | verifyFormat("@package"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2739 | verifyFormat("@private"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2740 | verifyFormat("@property"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2741 | verifyFormat("@protected"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2742 | verifyFormat("@protocol"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2743 | verifyFormat("@public"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2744 | verifyFormat("@required"); |
| 2745 | verifyFormat("@selector"); |
| 2746 | verifyFormat("@synchronized"); |
| 2747 | verifyFormat("@synthesize"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2748 | verifyFormat("@throw"); |
| 2749 | verifyFormat("@try"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2750 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 2751 | EXPECT_EQ("@interface", format("@ interface")); |
| 2752 | |
| 2753 | // The precise formatting of this doesn't matter, nobody writes code like |
| 2754 | // this. |
| 2755 | verifyFormat("@ /*foo*/ interface"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 2758 | TEST_F(FormatTest, ObjCSnippets) { |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2759 | verifyFormat("@autoreleasepool {\n" |
| 2760 | " foo();\n" |
| 2761 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2762 | verifyFormat("@class Foo, Bar;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 2763 | verifyFormat("@compatibility_alias AliasName ExistingClass;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2764 | verifyFormat("@dynamic textColor;"); |
Nico Weber | 5dfe9b4 | 2013-01-18 05:11:47 +0000 | [diff] [blame] | 2765 | verifyFormat("char *buf1 = @encode(int *);"); |
| 2766 | verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 2767 | verifyFormat("char *buf1 = @encode(int **);"); |
Nico Weber | 3a2673e | 2013-01-08 20:16:23 +0000 | [diff] [blame] | 2768 | verifyFormat("Protocol *proto = @protocol(p1);"); |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 2769 | verifyFormat("SEL s = @selector(foo:);"); |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2770 | verifyFormat("@synchronized(self) {\n" |
| 2771 | " f();\n" |
| 2772 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2773 | |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 2774 | verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 2775 | verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 2776 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2777 | verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 2778 | verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); |
| 2779 | verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 2782 | TEST_F(FormatTest, ObjCLiterals) { |
| 2783 | verifyFormat("@\"String\""); |
| 2784 | verifyFormat("@1"); |
| 2785 | verifyFormat("@+4.8"); |
| 2786 | verifyFormat("@-4"); |
| 2787 | verifyFormat("@1LL"); |
| 2788 | verifyFormat("@.5"); |
| 2789 | verifyFormat("@'c'"); |
| 2790 | verifyFormat("@true"); |
| 2791 | |
| 2792 | verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); |
| 2793 | verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); |
| 2794 | verifyFormat("NSNumber *favoriteColor = @(Green);"); |
| 2795 | verifyFormat("NSString *path = @(getenv(\"PATH\"));"); |
| 2796 | |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 2797 | verifyFormat("@["); |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 2798 | verifyFormat("@[]"); |
| 2799 | verifyFormat( |
| 2800 | "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); |
| 2801 | verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); |
| 2802 | |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 2803 | verifyFormat("@{"); |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 2804 | verifyFormat("@{}"); |
| 2805 | verifyFormat("@{ @\"one\" : @1 }"); |
| 2806 | verifyFormat("return @{ @\"one\" : @1 };"); |
| 2807 | verifyFormat("@{ @\"one\" : @1, }"); |
| 2808 | verifyFormat("@{ @\"one\" : @{ @2 : @1 } }"); |
| 2809 | verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }"); |
| 2810 | verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); |
| 2811 | verifyFormat("[self setDict:@{}"); |
| 2812 | verifyFormat("[self setDict:@{ @1 : @2 }"); |
| 2813 | verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);"); |
| 2814 | verifyFormat( |
| 2815 | "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };"); |
Nico Weber | e626200 | 2013-02-10 20:39:05 +0000 | [diff] [blame] | 2816 | verifyFormat( |
| 2817 | "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2818 | |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 2819 | // FIXME: Nested and multi-line array and dictionary literals need more work. |
Nico Weber | 0ac717b | 2013-02-10 20:48:24 +0000 | [diff] [blame] | 2820 | verifyFormat( |
| 2821 | "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n" |
| 2822 | " @\"processInfo\" : [NSProcessInfo processInfo] };"); |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 2825 | TEST_F(FormatTest, ReformatRegionAdjustsIndent) { |
| 2826 | EXPECT_EQ("{\n" |
| 2827 | "{\n" |
| 2828 | "a;\n" |
| 2829 | "b;\n" |
| 2830 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2831 | "}", |
| 2832 | format("{\n" |
| 2833 | "{\n" |
| 2834 | "a;\n" |
| 2835 | " b;\n" |
| 2836 | "}\n" |
| 2837 | "}", |
| 2838 | 13, 2, getLLVMStyle())); |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 2839 | EXPECT_EQ("{\n" |
| 2840 | "{\n" |
| 2841 | " a;\n" |
| 2842 | "b;\n" |
| 2843 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2844 | "}", |
| 2845 | format("{\n" |
| 2846 | "{\n" |
| 2847 | " a;\n" |
| 2848 | "b;\n" |
| 2849 | "}\n" |
| 2850 | "}", |
| 2851 | 9, 2, getLLVMStyle())); |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 2852 | EXPECT_EQ("{\n" |
| 2853 | "{\n" |
| 2854 | "public:\n" |
| 2855 | " b;\n" |
| 2856 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2857 | "}", |
| 2858 | format("{\n" |
| 2859 | "{\n" |
| 2860 | "public:\n" |
| 2861 | " b;\n" |
| 2862 | "}\n" |
| 2863 | "}", |
| 2864 | 17, 2, getLLVMStyle())); |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 2865 | EXPECT_EQ("{\n" |
| 2866 | "{\n" |
| 2867 | "a;\n" |
| 2868 | "}\n" |
| 2869 | "{\n" |
| 2870 | " b;\n" |
| 2871 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2872 | "}", |
| 2873 | format("{\n" |
| 2874 | "{\n" |
| 2875 | "a;\n" |
| 2876 | "}\n" |
| 2877 | "{\n" |
| 2878 | " b;\n" |
| 2879 | "}\n" |
| 2880 | "}", |
| 2881 | 22, 2, getLLVMStyle())); |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 2882 | EXPECT_EQ(" {\n" |
| 2883 | " a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2884 | " }", |
| 2885 | format(" {\n" |
| 2886 | "a;\n" |
| 2887 | " }", |
| 2888 | 4, 2, getLLVMStyle())); |
Daniel Jasper | 9ece2bb | 2013-02-12 16:51:23 +0000 | [diff] [blame] | 2889 | EXPECT_EQ("void f() {}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2890 | "void g() {}", |
| 2891 | format("void f() {}\n" |
| 2892 | "void g() {}", |
| 2893 | 13, 0, getLLVMStyle())); |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 2894 | } |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 2895 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 2896 | TEST_F(FormatTest, BreakStringLiterals) { |
| 2897 | EXPECT_EQ("\"some text \"\n" |
| 2898 | "\"other\";", |
| 2899 | format("\"some text other\";", getLLVMStyleWithColumns(12))); |
| 2900 | EXPECT_EQ( |
| 2901 | "#define A \\\n" |
| 2902 | " \"some \" \\\n" |
| 2903 | " \"text \" \\\n" |
| 2904 | " \"other\";", |
| 2905 | format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); |
| 2906 | EXPECT_EQ( |
| 2907 | "#define A \\\n" |
| 2908 | " \"so \" \\\n" |
| 2909 | " \"text \" \\\n" |
| 2910 | " \"other\";", |
| 2911 | format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); |
| 2912 | |
| 2913 | EXPECT_EQ("\"some text\"", |
| 2914 | format("\"some text\"", getLLVMStyleWithColumns(1))); |
| 2915 | EXPECT_EQ("\"some text\"", |
| 2916 | format("\"some text\"", getLLVMStyleWithColumns(11))); |
| 2917 | EXPECT_EQ("\"some \"\n" |
| 2918 | "\"text\"", |
| 2919 | format("\"some text\"", getLLVMStyleWithColumns(10))); |
| 2920 | EXPECT_EQ("\"some \"\n" |
| 2921 | "\"text\"", |
| 2922 | format("\"some text\"", getLLVMStyleWithColumns(7))); |
| 2923 | EXPECT_EQ("\"some text\"", |
| 2924 | format("\"some text\"", getLLVMStyleWithColumns(6))); |
| 2925 | |
| 2926 | EXPECT_EQ("variable =\n" |
| 2927 | " \"long string \"\n" |
| 2928 | " \"literal\";", |
| 2929 | format("variable = \"long string literal\";", |
| 2930 | getLLVMStyleWithColumns(20))); |
| 2931 | |
| 2932 | EXPECT_EQ("variable = f(\n" |
| 2933 | " \"long string \"\n" |
| 2934 | " \"literal\", short,\n" |
| 2935 | " loooooooooooooooooooong);", |
| 2936 | format("variable = f(\"long string literal\", short, " |
| 2937 | "loooooooooooooooooooong);", |
| 2938 | getLLVMStyleWithColumns(20))); |
| 2939 | EXPECT_EQ( |
| 2940 | "f(\"one two\".split(\n" |
| 2941 | " variable));", |
| 2942 | format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); |
| 2943 | EXPECT_EQ("f(\"one two three four five six \"\n" |
| 2944 | " \"seven\".split(\n" |
| 2945 | " really_looooong_variable));", |
| 2946 | format("f(\"one two three four five six seven\"." |
| 2947 | "split(really_looooong_variable));", |
| 2948 | getLLVMStyleWithColumns(33))); |
| 2949 | |
| 2950 | EXPECT_EQ("f(\"some \"\n" |
| 2951 | " \"text\",\n" |
| 2952 | " other);", |
| 2953 | format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); |
| 2954 | } |
| 2955 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 2956 | } // end namespace tooling |
| 2957 | } // end namespace clang |