xref: /llvm-project/clang/unittests/Format/FormatTestJS.cpp (revision 04a71a45ff8e8b8a8493d394dfbc4205724b9353)
1 //===- 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 
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 
20 class FormatTestJS : public ::testing::Test {
21 protected:
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 
34   static std::string format(llvm::StringRef Code, const FormatStyle &Style) {
35     return format(Code, 0, Code.size(), Style);
36   }
37 
38   static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
39     FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
40     Style.ColumnLimit = ColumnLimit;
41     return Style;
42   }
43 
44   static void verifyFormat(
45       llvm::StringRef Code,
46       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
47     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
48   }
49 };
50 
51 TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
52   verifyFormat("a == = b;");
53   verifyFormat("a != = b;");
54 
55   verifyFormat("a === b;");
56   verifyFormat("aaaaaaa ===\n    b;", getGoogleJSStyleWithColumns(10));
57   verifyFormat("a !== b;");
58   verifyFormat("aaaaaaa !==\n    b;", getGoogleJSStyleWithColumns(10));
59   verifyFormat("if (a + b + c +\n"
60                "        d !==\n"
61                "    e + f + g)\n"
62                "  q();",
63                getGoogleJSStyleWithColumns(20));
64 
65   verifyFormat("a >> >= b;");
66 
67   verifyFormat("a >>> b;");
68   verifyFormat("aaaaaaa >>>\n    b;", getGoogleJSStyleWithColumns(10));
69   verifyFormat("a >>>= b;");
70   verifyFormat("aaaaaaa >>>=\n    b;", getGoogleJSStyleWithColumns(10));
71   verifyFormat("if (a + b + c +\n"
72                "        d >>>\n"
73                "    e + f + g)\n"
74                "  q();",
75                getGoogleJSStyleWithColumns(20));
76   verifyFormat("var x = aaaaaaaaaa ?\n"
77                "            bbbbbb :\n"
78                "            ccc;",
79                getGoogleJSStyleWithColumns(20));
80 }
81 
82 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
83   verifyFormat("var arr = [1, 2, 3];");
84   verifyFormat("var obj = {a: 1, b: 2, c: 3};");
85 
86   verifyFormat("var obj = {a: 1, b: 2, c: 3};",
87                getChromiumStyle(FormatStyle::LK_JavaScript));
88 }
89 
90 TEST_F(FormatTestJS, SingleQuoteStrings) {
91   verifyFormat("this.function('', true);");
92 }
93 
94 TEST_F(FormatTestJS, GoogScopes) {
95   verifyFormat("goog.scope(function() {\n"
96                "var x = a.b;\n"
97                "var y = c.d;\n"
98                "});  // goog.scope");
99 }
100 
101 TEST_F(FormatTestJS, Closures) {
102   verifyFormat("doFoo(function() { return 1; });");
103   verifyFormat("var func = function() { return 1; };");
104   verifyFormat("return {\n"
105                "  body: {\n"
106                "    setAttribute: function(key, val) { this[key] = val; },\n"
107                "    getAttribute: function(key) { return this[key]; },\n"
108                "    style: {direction: ''}\n"
109                "  }\n"
110                "};");
111 }
112 
113 TEST_F(FormatTestJS, ReturnStatements) {
114   verifyFormat("function() { return [hello, world]; }");
115 }
116 
117 TEST_F(FormatTestJS, ClosureStyleComments) {
118   verifyFormat("var x = /** @type {foo} */ (bar);");
119 }
120 
121 TEST_F(FormatTestJS, TryCatch) {
122   verifyFormat("try {\n"
123                "  f();\n"
124                "} catch (e) {\n"
125                "  g();\n"
126                "} finally {\n"
127                "  h();\n"
128                "}");
129 }
130 
131 TEST_F(FormatTestJS, RegexLiteralClassification) {
132   // Regex literals.
133   verifyFormat("var regex = /abc/;");
134   verifyFormat("f(/abc/);");
135   verifyFormat("f(abc, /abc/);");
136   verifyFormat("some_map[/abc/];");
137   verifyFormat("var x = a ? /abc/ : /abc/;");
138   verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
139   verifyFormat("var x = !/abc/.test(y);");
140   verifyFormat("var x = a && /abc/.test(y);");
141   verifyFormat("var x = a || /abc/.test(y);");
142   verifyFormat("var x = a + /abc/.search(y);");
143   verifyFormat("var regexs = {/abc/, /abc/};");
144   verifyFormat("return /abc/;");
145 
146   // Not regex literals.
147   verifyFormat("var a = a / 2 + b / 3;");
148 }
149 
150 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
151   verifyFormat("var regex = /a*/;");
152   verifyFormat("var regex = /a+/;");
153   verifyFormat("var regex = /a?/;");
154   verifyFormat("var regex = /.a./;");
155   verifyFormat("var regex = /a\\*/;");
156   verifyFormat("var regex = /^a$/;");
157   verifyFormat("var regex = /\\/a/;");
158   verifyFormat("var regex = /(?:x)/;");
159   verifyFormat("var regex = /x(?=y)/;");
160   verifyFormat("var regex = /x(?!y)/;");
161   verifyFormat("var regex = /x|y/;");
162   verifyFormat("var regex = /a{2}/;");
163   verifyFormat("var regex = /a{1,3}/;");
164   verifyFormat("var regex = /[abc]/;");
165   verifyFormat("var regex = /[^abc]/;");
166   verifyFormat("var regex = /[\\b]/;");
167   verifyFormat("var regex = /\\b/;");
168   verifyFormat("var regex = /\\B/;");
169   verifyFormat("var regex = /\\d/;");
170   verifyFormat("var regex = /\\D/;");
171   verifyFormat("var regex = /\\f/;");
172   verifyFormat("var regex = /\\n/;");
173   verifyFormat("var regex = /\\r/;");
174   verifyFormat("var regex = /\\s/;");
175   verifyFormat("var regex = /\\S/;");
176   verifyFormat("var regex = /\\t/;");
177   verifyFormat("var regex = /\\v/;");
178   verifyFormat("var regex = /\\w/;");
179   verifyFormat("var regex = /\\W/;");
180   verifyFormat("var regex = /a(a)\\1/;");
181   verifyFormat("var regex = /\\0/;");
182 }
183 
184 TEST_F(FormatTestJS, RegexLiteralModifiers) {
185   verifyFormat("var regex = /abc/g;");
186   verifyFormat("var regex = /abc/i;");
187   verifyFormat("var regex = /abc/m;");
188   verifyFormat("var regex = /abc/y;");
189 }
190 
191 TEST_F(FormatTestJS, RegexLiteralLength) {
192   verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
193                getGoogleJSStyleWithColumns(60));
194   verifyFormat("var regex =\n"
195                "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
196                getGoogleJSStyleWithColumns(60));
197 }
198 
199 TEST_F(FormatTestJS, RegexLiteralExamples) {
200   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
201 }
202 
203 } // end namespace tooling
204 } // end namespace clang
205