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