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 125 TEST_F(FormatTestJS, ReturnStatements) { 126 verifyFormat("function() { return [hello, world]; }"); 127 } 128 129 TEST_F(FormatTestJS, ClosureStyleComments) { 130 verifyFormat("var x = /** @type {foo} */ (bar);"); 131 } 132 133 TEST_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 143 TEST_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);"); 155 verifyFormat("var regexs = {/abc/, /abc/};"); 156 verifyFormat("return /abc/;"); 157 158 // Not regex literals. 159 verifyFormat("var a = a / 2 + b / 3;"); 160 } 161 162 TEST_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/;"); 194 verifyFormat("var regex = /\\\\/g;"); 195 verifyFormat("var regex = /\\a\\\\/g;"); 196 verifyFormat("var regex = /\a\\//g;"); 197 } 198 199 TEST_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 206 TEST_F(FormatTestJS, RegexLiteralLength) { 207 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 208 getGoogleJSStyleWithColumns(60)); 209 verifyFormat("var regex =\n" 210 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 211 getGoogleJSStyleWithColumns(60)); 212 } 213 214 TEST_F(FormatTestJS, RegexLiteralExamples) { 215 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 216 } 217 218 } // end namespace tooling 219 } // end namespace clang 220