Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10 | #include "FormatTestUtils.h" |
| 11 | #include "clang/Format/Format.h" |
| 12 | #include "llvm/Support/Debug.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 15 | #define DEBUG_TYPE "format-test" |
| 16 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 34 | static std::string format( |
| 35 | llvm::StringRef Code, |
| 36 | const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 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)); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 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};"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 95 | verifyFormat("var object_literal_with_long_name = {\n" |
| 96 | " a: 'aaaaaaaaaaaaaaaaaa',\n" |
| 97 | " b: 'bbbbbbbbbbbbbbbbbb'\n" |
| 98 | "};"); |
| 99 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 100 | verifyFormat("var obj = {a: 1, b: 2, c: 3};", |
| 101 | getChromiumStyle(FormatStyle::LK_JavaScript)); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 102 | verifyFormat("someVariable = {'a': [{}]};"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TEST_F(FormatTestJS, SingleQuoteStrings) { |
| 106 | verifyFormat("this.function('', true);"); |
| 107 | } |
| 108 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 109 | TEST_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 | |
| 116 | TEST_F(FormatTestJS, Closures) { |
| 117 | verifyFormat("doFoo(function() { return 1; });"); |
| 118 | verifyFormat("var func = function() { return 1; };"); |
| 119 | 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 | "};"); |
| 126 | EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };", |
| 127 | format("abc=xyz?function(){return 1;}:function(){return -1;};")); |
| 128 | |
| 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);"); |
| 140 | |
| 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)); |
| 147 | } |
| 148 | |
| 149 | TEST_F(FormatTestJS, ReturnStatements) { |
| 150 | verifyFormat("function() { return [hello, world]; }"); |
| 151 | } |
| 152 | |
| 153 | TEST_F(FormatTestJS, ClosureStyleComments) { |
| 154 | verifyFormat("var x = /** @type {foo} */ (bar);"); |
| 155 | } |
| 156 | |
| 157 | TEST_F(FormatTestJS, TryCatch) { |
| 158 | verifyFormat("try {\n" |
| 159 | " f();\n" |
| 160 | "} catch (e) {\n" |
| 161 | " g();\n" |
| 162 | "} finally {\n" |
| 163 | " h();\n" |
| 164 | "}"); |
| 165 | } |
| 166 | |
| 167 | TEST_F(FormatTestJS, StringLiteralConcatenation) { |
| 168 | verifyFormat("var literal = 'hello ' +\n" |
| 169 | " 'world';"); |
| 170 | } |
| 171 | |
| 172 | TEST_F(FormatTestJS, RegexLiteralClassification) { |
| 173 | // Regex literals. |
| 174 | verifyFormat("var regex = /abc/;"); |
| 175 | verifyFormat("f(/abc/);"); |
| 176 | verifyFormat("f(abc, /abc/);"); |
| 177 | verifyFormat("some_map[/abc/];"); |
| 178 | verifyFormat("var x = a ? /abc/ : /abc/;"); |
| 179 | verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); |
| 180 | verifyFormat("var x = !/abc/.test(y);"); |
| 181 | verifyFormat("var x = a && /abc/.test(y);"); |
| 182 | verifyFormat("var x = a || /abc/.test(y);"); |
| 183 | verifyFormat("var x = a + /abc/.search(y);"); |
| 184 | verifyFormat("var regexs = {/abc/, /abc/};"); |
| 185 | verifyFormat("return /abc/;"); |
| 186 | |
| 187 | // Not regex literals. |
| 188 | verifyFormat("var a = a / 2 + b / 3;"); |
| 189 | } |
| 190 | |
| 191 | TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { |
| 192 | verifyFormat("var regex = /a*/;"); |
| 193 | verifyFormat("var regex = /a+/;"); |
| 194 | verifyFormat("var regex = /a?/;"); |
| 195 | verifyFormat("var regex = /.a./;"); |
| 196 | verifyFormat("var regex = /a\\*/;"); |
| 197 | verifyFormat("var regex = /^a$/;"); |
| 198 | verifyFormat("var regex = /\\/a/;"); |
| 199 | verifyFormat("var regex = /(?:x)/;"); |
| 200 | verifyFormat("var regex = /x(?=y)/;"); |
| 201 | verifyFormat("var regex = /x(?!y)/;"); |
| 202 | verifyFormat("var regex = /x|y/;"); |
| 203 | verifyFormat("var regex = /a{2}/;"); |
| 204 | verifyFormat("var regex = /a{1,3}/;"); |
| 205 | verifyFormat("var regex = /[abc]/;"); |
| 206 | verifyFormat("var regex = /[^abc]/;"); |
| 207 | verifyFormat("var regex = /[\\b]/;"); |
| 208 | verifyFormat("var regex = /\\b/;"); |
| 209 | verifyFormat("var regex = /\\B/;"); |
| 210 | verifyFormat("var regex = /\\d/;"); |
| 211 | verifyFormat("var regex = /\\D/;"); |
| 212 | verifyFormat("var regex = /\\f/;"); |
| 213 | verifyFormat("var regex = /\\n/;"); |
| 214 | verifyFormat("var regex = /\\r/;"); |
| 215 | verifyFormat("var regex = /\\s/;"); |
| 216 | verifyFormat("var regex = /\\S/;"); |
| 217 | verifyFormat("var regex = /\\t/;"); |
| 218 | verifyFormat("var regex = /\\v/;"); |
| 219 | verifyFormat("var regex = /\\w/;"); |
| 220 | verifyFormat("var regex = /\\W/;"); |
| 221 | verifyFormat("var regex = /a(a)\\1/;"); |
| 222 | verifyFormat("var regex = /\\0/;"); |
| 223 | verifyFormat("var regex = /\\\\/g;"); |
| 224 | verifyFormat("var regex = /\\a\\\\/g;"); |
| 225 | verifyFormat("var regex = /\a\\//g;"); |
| 226 | } |
| 227 | |
| 228 | TEST_F(FormatTestJS, RegexLiteralModifiers) { |
| 229 | verifyFormat("var regex = /abc/g;"); |
| 230 | verifyFormat("var regex = /abc/i;"); |
| 231 | verifyFormat("var regex = /abc/m;"); |
| 232 | verifyFormat("var regex = /abc/y;"); |
| 233 | } |
| 234 | |
| 235 | TEST_F(FormatTestJS, RegexLiteralLength) { |
| 236 | verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", |
| 237 | getGoogleJSStyleWithColumns(60)); |
| 238 | verifyFormat("var regex =\n" |
| 239 | " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", |
| 240 | getGoogleJSStyleWithColumns(60)); |
| 241 | } |
| 242 | |
| 243 | TEST_F(FormatTestJS, RegexLiteralExamples) { |
| 244 | verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); |
| 245 | } |
| 246 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 247 | } // end namespace tooling |
| 248 | } // end namespace clang |