blob: ddd594bb6e702bd86fb556b17fafb6db8c073154 [file] [log] [blame]
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +00001//===- unittest/Format/FormatTestJS.cpp - Formatting unit tests for JS ----===//
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
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000010#include "FormatTestUtils.h"
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000011#include "clang/Format/Format.h"
12#include "llvm/Support/Debug.h"
13#include "gtest/gtest.h"
14
Chandler Carruth10346662014-04-22 03:17:02 +000015#define DEBUG_TYPE "format-test"
16
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000017namespace clang {
18namespace format {
19
20class FormatTestJS : public ::testing::Test {
21protected:
22 static std::string format(llvm::StringRef Code, unsigned Offset,
23 unsigned Length, const FormatStyle &Style) {
24 DEBUG(llvm::errs() << "---\n");
25 DEBUG(llvm::errs() << Code << "\n\n");
26 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27 tooling::Replacements Replaces = reformat(Style, Code, Ranges);
28 std::string Result = applyAllReplacements(Code, Replaces);
29 EXPECT_NE("", Result);
30 DEBUG(llvm::errs() << "\n" << Result << "\n\n");
31 return Result;
32 }
33
Daniel Jasper069e5f42014-05-20 11:14:57 +000034 static std::string format(
35 llvm::StringRef Code,
36 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000037 return format(Code, 0, Code.size(), Style);
38 }
39
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000040 static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
Nico Weber514ecc82014-02-02 20:50:45 +000041 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000042 Style.ColumnLimit = ColumnLimit;
43 return Style;
44 }
45
Nico Weber514ecc82014-02-02 20:50:45 +000046 static void verifyFormat(
47 llvm::StringRef Code,
48 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000049 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
50 }
51};
52
53TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
54 verifyFormat("a == = b;");
55 verifyFormat("a != = b;");
56
57 verifyFormat("a === b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000058 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000059 verifyFormat("a !== b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000060 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000061 verifyFormat("if (a + b + c +\n"
62 " d !==\n"
63 " e + f + g)\n"
64 " q();",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000065 getGoogleJSStyleWithColumns(20));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000066
67 verifyFormat("a >> >= b;");
68
69 verifyFormat("a >>> b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000070 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000071 verifyFormat("a >>>= b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000072 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000073 verifyFormat("if (a + b + c +\n"
74 " d >>>\n"
75 " e + f + g)\n"
76 " q();",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000077 getGoogleJSStyleWithColumns(20));
78 verifyFormat("var x = aaaaaaaaaa ?\n"
79 " bbbbbb :\n"
80 " ccc;",
81 getGoogleJSStyleWithColumns(20));
Daniel Jasper78214392014-05-19 07:27:02 +000082
83 verifyFormat("var b = a.map((x) => x + 1);");
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000084}
85
Daniel Jasper0dd52912014-05-19 07:37:07 +000086TEST_F(FormatTestJS, ES6DestructuringAssignment) {
87 verifyFormat("var [a, b, c] = [1, 2, 3];");
88 verifyFormat("var {a, b} = {a: 1, b: 2};");
89}
90
Daniel Jasperb2e10a52014-01-15 15:09:08 +000091TEST_F(FormatTestJS, SpacesInContainerLiterals) {
92 verifyFormat("var arr = [1, 2, 3];");
93 verifyFormat("var obj = {a: 1, b: 2, c: 3};");
Nico Weber514ecc82014-02-02 20:50:45 +000094
Daniel Jasper2a958322014-05-21 13:26:58 +000095 verifyFormat("var object_literal_with_long_name = {\n"
96 " a: 'aaaaaaaaaaaaaaaaaa',\n"
97 " b: 'bbbbbbbbbbbbbbbbbb'\n"
98 "};");
99
Nico Weber514ecc82014-02-02 20:50:45 +0000100 verifyFormat("var obj = {a: 1, b: 2, c: 3};",
101 getChromiumStyle(FormatStyle::LK_JavaScript));
Daniel Jasper89519082014-05-09 10:26:08 +0000102 verifyFormat("someVariable = {'a': [{}]};");
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000103}
104
Daniel Jasper86fee2f2014-01-31 12:49:42 +0000105TEST_F(FormatTestJS, SingleQuoteStrings) {
106 verifyFormat("this.function('', true);");
107}
108
Daniel Jasper4a39c842014-05-06 13:54:10 +0000109TEST_F(FormatTestJS, GoogScopes) {
110 verifyFormat("goog.scope(function() {\n"
111 "var x = a.b;\n"
112 "var y = c.d;\n"
113 "}); // goog.scope");
114}
115
Daniel Jasper79dffb42014-05-07 09:48:30 +0000116TEST_F(FormatTestJS, Closures) {
117 verifyFormat("doFoo(function() { return 1; });");
118 verifyFormat("var func = function() { return 1; };");
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000119 verifyFormat("return {\n"
120 " body: {\n"
121 " setAttribute: function(key, val) { this[key] = val; },\n"
122 " getAttribute: function(key) { return this[key]; },\n"
123 " style: {direction: ''}\n"
124 " }\n"
125 "};");
Daniel Jasper069e5f42014-05-20 11:14:57 +0000126 EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };",
127 format("abc=xyz?function(){return 1;}:function(){return -1;};"));
Daniel Jasperb16b9692014-05-21 12:51:23 +0000128
129 verifyFormat("var closure = goog.bind(\n"
130 " function() { // comment\n"
131 " foo();\n"
132 " bar();\n"
133 " },\n"
134 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
135 " arg3IsReallyLongAndNeeedsLineBreaks);");
136 verifyFormat("var closure = goog.bind(function() { // comment\n"
137 " foo();\n"
138 " bar();\n"
139 "}, this);");
Daniel Jasper5f3ea472014-05-22 08:36:53 +0000140
141 verifyFormat("var x = {a: function() { return 1; }};",
142 getGoogleJSStyleWithColumns(38));
143 verifyFormat("var x = {\n"
144 " a: function() { return 1; }\n"
145 "};",
146 getGoogleJSStyleWithColumns(37));
Daniel Jasper79dffb42014-05-07 09:48:30 +0000147}
148
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000149TEST_F(FormatTestJS, MultipleFunctionLiterals) {
150 verifyFormat("promise.then(\n"
151 " function success() {\n"
152 " doFoo();\n"
153 " doBar();\n"
154 " },\n"
155 " function error() {\n"
156 " doFoo();\n"
157 " doBaz();\n"
158 " },\n"
159 " []);\n");
160 verifyFormat("promise.then(\n"
161 " function success() {\n"
162 " doFoo();\n"
163 " doBar();\n"
164 " },\n"
165 " [],\n"
166 " function error() {\n"
167 " doFoo();\n"
168 " doBaz();\n"
169 " });\n");
170 // FIXME: Here, we should probably break right after the "(" for consistency.
171 verifyFormat("promise.then([],\n"
172 " function success() {\n"
173 " doFoo();\n"
174 " doBar();\n"
175 " },\n"
176 " function error() {\n"
177 " doFoo();\n"
178 " doBaz();\n"
179 " });\n");
180}
181
Daniel Jasper166c19b2014-05-06 14:12:21 +0000182TEST_F(FormatTestJS, ReturnStatements) {
183 verifyFormat("function() { return [hello, world]; }");
184}
185
Daniel Jasper484033b2014-05-06 14:41:29 +0000186TEST_F(FormatTestJS, ClosureStyleComments) {
187 verifyFormat("var x = /** @type {foo} */ (bar);");
188}
189
Daniel Jasper04a71a42014-05-08 11:58:24 +0000190TEST_F(FormatTestJS, TryCatch) {
191 verifyFormat("try {\n"
192 " f();\n"
193 "} catch (e) {\n"
194 " g();\n"
195 "} finally {\n"
196 " h();\n"
197 "}");
198}
199
Daniel Jasper49802ef2014-05-22 09:10:04 +0000200TEST_F(FormatTestJS, StringLiteralConcatenation) {
201 verifyFormat("var literal = 'hello ' +\n"
202 " 'world';");
203}
204
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000205TEST_F(FormatTestJS, RegexLiteralClassification) {
206 // Regex literals.
207 verifyFormat("var regex = /abc/;");
208 verifyFormat("f(/abc/);");
209 verifyFormat("f(abc, /abc/);");
210 verifyFormat("some_map[/abc/];");
211 verifyFormat("var x = a ? /abc/ : /abc/;");
212 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
213 verifyFormat("var x = !/abc/.test(y);");
214 verifyFormat("var x = a && /abc/.test(y);");
215 verifyFormat("var x = a || /abc/.test(y);");
216 verifyFormat("var x = a + /abc/.search(y);");
Daniel Jasperf7405c12014-05-08 07:45:18 +0000217 verifyFormat("var regexs = {/abc/, /abc/};");
218 verifyFormat("return /abc/;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000219
220 // Not regex literals.
221 verifyFormat("var a = a / 2 + b / 3;");
222}
223
224TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
225 verifyFormat("var regex = /a*/;");
226 verifyFormat("var regex = /a+/;");
227 verifyFormat("var regex = /a?/;");
228 verifyFormat("var regex = /.a./;");
229 verifyFormat("var regex = /a\\*/;");
230 verifyFormat("var regex = /^a$/;");
231 verifyFormat("var regex = /\\/a/;");
232 verifyFormat("var regex = /(?:x)/;");
233 verifyFormat("var regex = /x(?=y)/;");
234 verifyFormat("var regex = /x(?!y)/;");
235 verifyFormat("var regex = /x|y/;");
236 verifyFormat("var regex = /a{2}/;");
237 verifyFormat("var regex = /a{1,3}/;");
238 verifyFormat("var regex = /[abc]/;");
239 verifyFormat("var regex = /[^abc]/;");
240 verifyFormat("var regex = /[\\b]/;");
241 verifyFormat("var regex = /\\b/;");
242 verifyFormat("var regex = /\\B/;");
243 verifyFormat("var regex = /\\d/;");
244 verifyFormat("var regex = /\\D/;");
245 verifyFormat("var regex = /\\f/;");
246 verifyFormat("var regex = /\\n/;");
247 verifyFormat("var regex = /\\r/;");
248 verifyFormat("var regex = /\\s/;");
249 verifyFormat("var regex = /\\S/;");
250 verifyFormat("var regex = /\\t/;");
251 verifyFormat("var regex = /\\v/;");
252 verifyFormat("var regex = /\\w/;");
253 verifyFormat("var regex = /\\W/;");
254 verifyFormat("var regex = /a(a)\\1/;");
255 verifyFormat("var regex = /\\0/;");
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000256 verifyFormat("var regex = /\\\\/g;");
257 verifyFormat("var regex = /\\a\\\\/g;");
258 verifyFormat("var regex = /\a\\//g;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000259}
260
261TEST_F(FormatTestJS, RegexLiteralModifiers) {
262 verifyFormat("var regex = /abc/g;");
263 verifyFormat("var regex = /abc/i;");
264 verifyFormat("var regex = /abc/m;");
265 verifyFormat("var regex = /abc/y;");
266}
267
268TEST_F(FormatTestJS, RegexLiteralLength) {
269 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
270 getGoogleJSStyleWithColumns(60));
271 verifyFormat("var regex =\n"
272 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
273 getGoogleJSStyleWithColumns(60));
274}
275
276TEST_F(FormatTestJS, RegexLiteralExamples) {
277 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
278}
279
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000280} // end namespace tooling
281} // end namespace clang