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( 35 llvm::StringRef Code, 36 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) { 37 return format(Code, 0, Code.size(), Style); 38 } 39 40 static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) { 41 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 42 Style.ColumnLimit = ColumnLimit; 43 return Style; 44 } 45 46 static void verifyFormat( 47 llvm::StringRef Code, 48 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) { 49 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 50 } 51 }; 52 53 TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) { 54 verifyFormat("a == = b;"); 55 verifyFormat("a != = b;"); 56 57 verifyFormat("a === b;"); 58 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10)); 59 verifyFormat("a !== b;"); 60 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10)); 61 verifyFormat("if (a + b + c +\n" 62 " d !==\n" 63 " e + f + g)\n" 64 " q();", 65 getGoogleJSStyleWithColumns(20)); 66 67 verifyFormat("a >> >= b;"); 68 69 verifyFormat("a >>> b;"); 70 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10)); 71 verifyFormat("a >>>= b;"); 72 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10)); 73 verifyFormat("if (a + b + c +\n" 74 " d >>>\n" 75 " e + f + g)\n" 76 " q();", 77 getGoogleJSStyleWithColumns(20)); 78 verifyFormat("var x = aaaaaaaaaa ?\n" 79 " bbbbbb :\n" 80 " ccc;", 81 getGoogleJSStyleWithColumns(20)); 82 83 verifyFormat("var b = a.map((x) => x + 1);"); 84 } 85 86 TEST_F(FormatTestJS, ES6DestructuringAssignment) { 87 verifyFormat("var [a, b, c] = [1, 2, 3];"); 88 verifyFormat("var {a, b} = {a: 1, b: 2};"); 89 } 90 91 TEST_F(FormatTestJS, SpacesInContainerLiterals) { 92 verifyFormat("var arr = [1, 2, 3];"); 93 verifyFormat("var obj = {a: 1, b: 2, c: 3};"); 94 95 verifyFormat("var obj = {a: 1, b: 2, c: 3};", 96 getChromiumStyle(FormatStyle::LK_JavaScript)); 97 verifyFormat("someVariable = {'a': [{}]};"); 98 } 99 100 TEST_F(FormatTestJS, SingleQuoteStrings) { 101 verifyFormat("this.function('', true);"); 102 } 103 104 TEST_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 111 TEST_F(FormatTestJS, Closures) { 112 verifyFormat("doFoo(function() { return 1; });"); 113 verifyFormat("var func = function() { return 1; };"); 114 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 "};"); 121 EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };", 122 format("abc=xyz?function(){return 1;}:function(){return -1;};")); 123 124 verifyFormat("var closure = goog.bind(\n" 125 " function() { // comment\n" 126 " foo();\n" 127 " bar();\n" 128 " },\n" 129 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" 130 " arg3IsReallyLongAndNeeedsLineBreaks);"); 131 verifyFormat("var closure = goog.bind(function() { // comment\n" 132 " foo();\n" 133 " bar();\n" 134 "}, this);"); 135 } 136 137 TEST_F(FormatTestJS, ReturnStatements) { 138 verifyFormat("function() { return [hello, world]; }"); 139 } 140 141 TEST_F(FormatTestJS, ClosureStyleComments) { 142 verifyFormat("var x = /** @type {foo} */ (bar);"); 143 } 144 145 TEST_F(FormatTestJS, TryCatch) { 146 verifyFormat("try {\n" 147 " f();\n" 148 "} catch (e) {\n" 149 " g();\n" 150 "} finally {\n" 151 " h();\n" 152 "}"); 153 } 154 155 TEST_F(FormatTestJS, RegexLiteralClassification) { 156 // Regex literals. 157 verifyFormat("var regex = /abc/;"); 158 verifyFormat("f(/abc/);"); 159 verifyFormat("f(abc, /abc/);"); 160 verifyFormat("some_map[/abc/];"); 161 verifyFormat("var x = a ? /abc/ : /abc/;"); 162 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 163 verifyFormat("var x = !/abc/.test(y);"); 164 verifyFormat("var x = a && /abc/.test(y);"); 165 verifyFormat("var x = a || /abc/.test(y);"); 166 verifyFormat("var x = a + /abc/.search(y);"); 167 verifyFormat("var regexs = {/abc/, /abc/};"); 168 verifyFormat("return /abc/;"); 169 170 // Not regex literals. 171 verifyFormat("var a = a / 2 + b / 3;"); 172 } 173 174 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 175 verifyFormat("var regex = /a*/;"); 176 verifyFormat("var regex = /a+/;"); 177 verifyFormat("var regex = /a?/;"); 178 verifyFormat("var regex = /.a./;"); 179 verifyFormat("var regex = /a\\*/;"); 180 verifyFormat("var regex = /^a$/;"); 181 verifyFormat("var regex = /\\/a/;"); 182 verifyFormat("var regex = /(?:x)/;"); 183 verifyFormat("var regex = /x(?=y)/;"); 184 verifyFormat("var regex = /x(?!y)/;"); 185 verifyFormat("var regex = /x|y/;"); 186 verifyFormat("var regex = /a{2}/;"); 187 verifyFormat("var regex = /a{1,3}/;"); 188 verifyFormat("var regex = /[abc]/;"); 189 verifyFormat("var regex = /[^abc]/;"); 190 verifyFormat("var regex = /[\\b]/;"); 191 verifyFormat("var regex = /\\b/;"); 192 verifyFormat("var regex = /\\B/;"); 193 verifyFormat("var regex = /\\d/;"); 194 verifyFormat("var regex = /\\D/;"); 195 verifyFormat("var regex = /\\f/;"); 196 verifyFormat("var regex = /\\n/;"); 197 verifyFormat("var regex = /\\r/;"); 198 verifyFormat("var regex = /\\s/;"); 199 verifyFormat("var regex = /\\S/;"); 200 verifyFormat("var regex = /\\t/;"); 201 verifyFormat("var regex = /\\v/;"); 202 verifyFormat("var regex = /\\w/;"); 203 verifyFormat("var regex = /\\W/;"); 204 verifyFormat("var regex = /a(a)\\1/;"); 205 verifyFormat("var regex = /\\0/;"); 206 verifyFormat("var regex = /\\\\/g;"); 207 verifyFormat("var regex = /\\a\\\\/g;"); 208 verifyFormat("var regex = /\a\\//g;"); 209 } 210 211 TEST_F(FormatTestJS, RegexLiteralModifiers) { 212 verifyFormat("var regex = /abc/g;"); 213 verifyFormat("var regex = /abc/i;"); 214 verifyFormat("var regex = /abc/m;"); 215 verifyFormat("var regex = /abc/y;"); 216 } 217 218 TEST_F(FormatTestJS, RegexLiteralLength) { 219 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 220 getGoogleJSStyleWithColumns(60)); 221 verifyFormat("var regex =\n" 222 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 223 getGoogleJSStyleWithColumns(60)); 224 } 225 226 TEST_F(FormatTestJS, RegexLiteralExamples) { 227 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 228 } 229 230 } // end namespace tooling 231 } // end namespace clang 232