blob: 657f10874e022b0007aed5b4a89344c81fe0e7c8 [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
95 verifyFormat("var obj = {a: 1, b: 2, c: 3};",
96 getChromiumStyle(FormatStyle::LK_JavaScript));
Daniel Jasper89519082014-05-09 10:26:08 +000097 verifyFormat("someVariable = {'a': [{}]};");
Daniel Jasperb2e10a52014-01-15 15:09:08 +000098}
99
Daniel Jasper86fee2f2014-01-31 12:49:42 +0000100TEST_F(FormatTestJS, SingleQuoteStrings) {
101 verifyFormat("this.function('', true);");
102}
103
Daniel Jasper4a39c842014-05-06 13:54:10 +0000104TEST_F(FormatTestJS, GoogScopes) {
105 verifyFormat("goog.scope(function() {\n"
106 "var x = a.b;\n"
107 "var y = c.d;\n"
108 "}); // goog.scope");
109}
110
Daniel Jasper79dffb42014-05-07 09:48:30 +0000111TEST_F(FormatTestJS, Closures) {
112 verifyFormat("doFoo(function() { return 1; });");
113 verifyFormat("var func = function() { return 1; };");
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000114 verifyFormat("return {\n"
115 " body: {\n"
116 " setAttribute: function(key, val) { this[key] = val; },\n"
117 " getAttribute: function(key) { return this[key]; },\n"
118 " style: {direction: ''}\n"
119 " }\n"
120 "};");
Daniel Jasper069e5f42014-05-20 11:14:57 +0000121 EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };",
122 format("abc=xyz?function(){return 1;}:function(){return -1;};"));
Daniel Jasper79dffb42014-05-07 09:48:30 +0000123}
124
Daniel Jasper166c19b2014-05-06 14:12:21 +0000125TEST_F(FormatTestJS, ReturnStatements) {
126 verifyFormat("function() { return [hello, world]; }");
127}
128
Daniel Jasper484033b2014-05-06 14:41:29 +0000129TEST_F(FormatTestJS, ClosureStyleComments) {
130 verifyFormat("var x = /** @type {foo} */ (bar);");
131}
132
Daniel Jasper04a71a42014-05-08 11:58:24 +0000133TEST_F(FormatTestJS, TryCatch) {
134 verifyFormat("try {\n"
135 " f();\n"
136 "} catch (e) {\n"
137 " g();\n"
138 "} finally {\n"
139 " h();\n"
140 "}");
141}
142
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000143TEST_F(FormatTestJS, RegexLiteralClassification) {
144 // Regex literals.
145 verifyFormat("var regex = /abc/;");
146 verifyFormat("f(/abc/);");
147 verifyFormat("f(abc, /abc/);");
148 verifyFormat("some_map[/abc/];");
149 verifyFormat("var x = a ? /abc/ : /abc/;");
150 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
151 verifyFormat("var x = !/abc/.test(y);");
152 verifyFormat("var x = a && /abc/.test(y);");
153 verifyFormat("var x = a || /abc/.test(y);");
154 verifyFormat("var x = a + /abc/.search(y);");
Daniel Jasperf7405c12014-05-08 07:45:18 +0000155 verifyFormat("var regexs = {/abc/, /abc/};");
156 verifyFormat("return /abc/;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000157
158 // Not regex literals.
159 verifyFormat("var a = a / 2 + b / 3;");
160}
161
162TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
163 verifyFormat("var regex = /a*/;");
164 verifyFormat("var regex = /a+/;");
165 verifyFormat("var regex = /a?/;");
166 verifyFormat("var regex = /.a./;");
167 verifyFormat("var regex = /a\\*/;");
168 verifyFormat("var regex = /^a$/;");
169 verifyFormat("var regex = /\\/a/;");
170 verifyFormat("var regex = /(?:x)/;");
171 verifyFormat("var regex = /x(?=y)/;");
172 verifyFormat("var regex = /x(?!y)/;");
173 verifyFormat("var regex = /x|y/;");
174 verifyFormat("var regex = /a{2}/;");
175 verifyFormat("var regex = /a{1,3}/;");
176 verifyFormat("var regex = /[abc]/;");
177 verifyFormat("var regex = /[^abc]/;");
178 verifyFormat("var regex = /[\\b]/;");
179 verifyFormat("var regex = /\\b/;");
180 verifyFormat("var regex = /\\B/;");
181 verifyFormat("var regex = /\\d/;");
182 verifyFormat("var regex = /\\D/;");
183 verifyFormat("var regex = /\\f/;");
184 verifyFormat("var regex = /\\n/;");
185 verifyFormat("var regex = /\\r/;");
186 verifyFormat("var regex = /\\s/;");
187 verifyFormat("var regex = /\\S/;");
188 verifyFormat("var regex = /\\t/;");
189 verifyFormat("var regex = /\\v/;");
190 verifyFormat("var regex = /\\w/;");
191 verifyFormat("var regex = /\\W/;");
192 verifyFormat("var regex = /a(a)\\1/;");
193 verifyFormat("var regex = /\\0/;");
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000194 verifyFormat("var regex = /\\\\/g;");
195 verifyFormat("var regex = /\\a\\\\/g;");
196 verifyFormat("var regex = /\a\\//g;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000197}
198
199TEST_F(FormatTestJS, RegexLiteralModifiers) {
200 verifyFormat("var regex = /abc/g;");
201 verifyFormat("var regex = /abc/i;");
202 verifyFormat("var regex = /abc/m;");
203 verifyFormat("var regex = /abc/y;");
204}
205
206TEST_F(FormatTestJS, RegexLiteralLength) {
207 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
208 getGoogleJSStyleWithColumns(60));
209 verifyFormat("var regex =\n"
210 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
211 getGoogleJSStyleWithColumns(60));
212}
213
214TEST_F(FormatTestJS, RegexLiteralExamples) {
215 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
216}
217
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000218} // end namespace tooling
219} // end namespace clang