toolkit/devtools/pretty-fast/tests/unit/test.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2013 Mozilla Foundation and contributors
michael@0 3 * Licensed under the New BSD license. See LICENSE.md or:
michael@0 4 * http://opensource.org/licenses/BSD-2-Clause
michael@0 5 */
michael@0 6 var prettyFast = this.prettyFast || require("./pretty-fast");
michael@0 7
michael@0 8 var testCases = [
michael@0 9
michael@0 10 {
michael@0 11 name: "Simple function",
michael@0 12 input: "function foo() { bar(); }",
michael@0 13 output: "function foo() {\n" +
michael@0 14 " bar();\n" +
michael@0 15 "}\n",
michael@0 16 mappings: [
michael@0 17 // function foo() {
michael@0 18 {
michael@0 19 inputLine: 1,
michael@0 20 outputLine: 1
michael@0 21 },
michael@0 22 // bar();
michael@0 23 {
michael@0 24 inputLine: 1,
michael@0 25 outputLine: 2
michael@0 26 },
michael@0 27 // }
michael@0 28 {
michael@0 29 inputLine: 1,
michael@0 30 outputLine: 3
michael@0 31 },
michael@0 32 ]
michael@0 33 },
michael@0 34
michael@0 35 {
michael@0 36 name: "Nested function",
michael@0 37 input: "function foo() { function bar() { debugger; } bar(); }",
michael@0 38 output: "function foo() {\n" +
michael@0 39 " function bar() {\n" +
michael@0 40 " debugger;\n" +
michael@0 41 " }\n" +
michael@0 42 " bar();\n" +
michael@0 43 "}\n",
michael@0 44 mappings: [
michael@0 45 // function bar() {
michael@0 46 {
michael@0 47 inputLine: 1,
michael@0 48 outputLine: 2
michael@0 49 },
michael@0 50 // debugger;
michael@0 51 {
michael@0 52 inputLine: 1,
michael@0 53 outputLine: 3
michael@0 54 },
michael@0 55 // bar();
michael@0 56 {
michael@0 57 inputLine: 1,
michael@0 58 outputLine: 5
michael@0 59 },
michael@0 60 ]
michael@0 61 },
michael@0 62
michael@0 63 {
michael@0 64 name: "Immediately invoked function expression",
michael@0 65 input: "(function(){thingy()}())",
michael@0 66 output: "(function () {\n" +
michael@0 67 " thingy()\n" +
michael@0 68 "}())\n"
michael@0 69 },
michael@0 70
michael@0 71 {
michael@0 72 name: "Single line comment",
michael@0 73 input: "// Comment\n" +
michael@0 74 "function foo() { bar(); }\n",
michael@0 75 output: "// Comment\n" +
michael@0 76 "function foo() {\n" +
michael@0 77 " bar();\n" +
michael@0 78 "}\n",
michael@0 79 mappings: [
michael@0 80 // // Comment
michael@0 81 {
michael@0 82 inputLine: 1,
michael@0 83 outputLine: 1
michael@0 84 }
michael@0 85 ]
michael@0 86 },
michael@0 87
michael@0 88 {
michael@0 89 name: "Multi line comment",
michael@0 90 input: "/* Comment\n" +
michael@0 91 "more comment */\n" +
michael@0 92 "function foo() { bar(); }\n",
michael@0 93 output: "/* Comment\n" +
michael@0 94 "more comment */\n" +
michael@0 95 "function foo() {\n" +
michael@0 96 " bar();\n" +
michael@0 97 "}\n",
michael@0 98 mappings: [
michael@0 99 // /* Comment
michael@0 100 {
michael@0 101 inputLine: 1,
michael@0 102 outputLine: 1
michael@0 103 },
michael@0 104 // \nmore comment */
michael@0 105 {
michael@0 106 inputLine: 1,
michael@0 107 outputLine: 2
michael@0 108 }
michael@0 109 ]
michael@0 110 },
michael@0 111
michael@0 112 {
michael@0 113 name: "Null assignment",
michael@0 114 input: "var i=null;\n",
michael@0 115 output: "var i = null;\n",
michael@0 116 mappings: [
michael@0 117 {
michael@0 118 inputLine: 1,
michael@0 119 outputLine: 1
michael@0 120 }
michael@0 121 ]
michael@0 122 },
michael@0 123
michael@0 124 {
michael@0 125 name: "Undefined assignment",
michael@0 126 input: "var i=undefined;\n",
michael@0 127 output: "var i = undefined;\n"
michael@0 128 },
michael@0 129
michael@0 130 {
michael@0 131 name: "Void 0 assignment",
michael@0 132 input: "var i=void 0;\n",
michael@0 133 output: "var i = void 0;\n"
michael@0 134 },
michael@0 135
michael@0 136 {
michael@0 137 name: "This property access",
michael@0 138 input: "var foo=this.foo;\n",
michael@0 139 output: "var foo = this.foo;\n"
michael@0 140 },
michael@0 141
michael@0 142 {
michael@0 143 name: "True assignment",
michael@0 144 input: "var foo=true;\n",
michael@0 145 output: "var foo = true;\n"
michael@0 146 },
michael@0 147
michael@0 148 {
michael@0 149 name: "False assignment",
michael@0 150 input: "var foo=false;\n",
michael@0 151 output: "var foo = false;\n"
michael@0 152 },
michael@0 153
michael@0 154 {
michael@0 155 name: "For loop",
michael@0 156 input: "for (var i = 0; i < n; i++) { console.log(i); }",
michael@0 157 output: "for (var i = 0; i < n; i++) {\n" +
michael@0 158 " console.log(i);\n" +
michael@0 159 "}\n",
michael@0 160 mappings: [
michael@0 161 // for (var i = 0; i < n; i++) {
michael@0 162 {
michael@0 163 inputLine: 1,
michael@0 164 outputLine: 1
michael@0 165 },
michael@0 166 // console.log(i);
michael@0 167 {
michael@0 168 inputLine: 1,
michael@0 169 outputLine: 2
michael@0 170 },
michael@0 171 ]
michael@0 172 },
michael@0 173
michael@0 174 {
michael@0 175 name: "String with semicolon",
michael@0 176 input: "var foo = ';';\n",
michael@0 177 output: "var foo = ';';\n"
michael@0 178 },
michael@0 179
michael@0 180 {
michael@0 181 name: "String with quote",
michael@0 182 input: "var foo = \"'\";\n",
michael@0 183 output: "var foo = '\\'';\n"
michael@0 184 },
michael@0 185
michael@0 186 {
michael@0 187 name: "Function calls",
michael@0 188 input: "var result=func(a,b,c,d);",
michael@0 189 output: "var result = func(a, b, c, d);\n"
michael@0 190 },
michael@0 191
michael@0 192 {
michael@0 193 name: "Regexp",
michael@0 194 input: "var r=/foobar/g;",
michael@0 195 output: "var r = /foobar/g;\n"
michael@0 196 },
michael@0 197
michael@0 198 {
michael@0 199 name: "In operator",
michael@0 200 input: "if(foo in bar){doThing()}",
michael@0 201 output: "if (foo in bar) {\n" +
michael@0 202 " doThing()\n" +
michael@0 203 "}\n"
michael@0 204 },
michael@0 205
michael@0 206 {
michael@0 207 name: "With statement",
michael@0 208 input: "with(obj){crock()}",
michael@0 209 output: "with (obj) {\n" +
michael@0 210 " crock()\n" +
michael@0 211 "}\n"
michael@0 212 },
michael@0 213
michael@0 214 {
michael@0 215 name: "New expression",
michael@0 216 input: "var foo=new Foo();",
michael@0 217 output: "var foo = new Foo();\n"
michael@0 218 },
michael@0 219
michael@0 220 {
michael@0 221 name: "Continue/break statements",
michael@0 222 input: "while(1){if(x){continue}if(y){break}if(z){break foo}}",
michael@0 223 output: "while (1) {\n" +
michael@0 224 " if (x) {\n" +
michael@0 225 " continue\n" +
michael@0 226 " }\n" +
michael@0 227 " if (y) {\n" +
michael@0 228 " break\n" +
michael@0 229 " }\n" +
michael@0 230 " if (z) {\n" +
michael@0 231 " break foo\n" +
michael@0 232 " }\n" +
michael@0 233 "}\n"
michael@0 234 },
michael@0 235
michael@0 236 {
michael@0 237 name: "Instanceof",
michael@0 238 input: "var a=x instanceof y;",
michael@0 239 output: "var a = x instanceof y;\n"
michael@0 240 },
michael@0 241
michael@0 242 {
michael@0 243 name: "Binary operators",
michael@0 244 input: "var a=5*30;var b=5>>3;",
michael@0 245 output: "var a = 5 * 30;\n" +
michael@0 246 "var b = 5 >> 3;\n"
michael@0 247 },
michael@0 248
michael@0 249 {
michael@0 250 name: "Delete",
michael@0 251 input: "delete obj.prop;",
michael@0 252 output: "delete obj.prop;\n"
michael@0 253 },
michael@0 254
michael@0 255 {
michael@0 256 name: "Try/catch/finally statement",
michael@0 257 input: "try{dangerous()}catch(e){handle(e)}finally{cleanup()}",
michael@0 258 output: "try {\n" +
michael@0 259 " dangerous()\n" +
michael@0 260 "} catch (e) {\n" +
michael@0 261 " handle(e)\n" +
michael@0 262 "} finally {\n" +
michael@0 263 " cleanup()\n" +
michael@0 264 "}\n"
michael@0 265 },
michael@0 266
michael@0 267 {
michael@0 268 name: "If/else statement",
michael@0 269 input: "if(c){then()}else{other()}",
michael@0 270 output: "if (c) {\n" +
michael@0 271 " then()\n" +
michael@0 272 "} else {\n" +
michael@0 273 " other()\n" +
michael@0 274 "}\n"
michael@0 275 },
michael@0 276
michael@0 277 {
michael@0 278 name: "If/else without curlies",
michael@0 279 input: "if(c) a else b",
michael@0 280 output: "if (c) a else b\n"
michael@0 281 },
michael@0 282
michael@0 283 {
michael@0 284 name: "Objects",
michael@0 285 input: "var o={a:1,\n" +
michael@0 286 " b:2};",
michael@0 287 output: "var o = {\n" +
michael@0 288 " a: 1,\n" +
michael@0 289 " b: 2\n" +
michael@0 290 "};\n",
michael@0 291 mappings: [
michael@0 292 // a: 1,
michael@0 293 {
michael@0 294 inputLine: 1,
michael@0 295 outputLine: 2
michael@0 296 },
michael@0 297 // b: 2
michael@0 298 {
michael@0 299 inputLine: 2,
michael@0 300 outputLine: 3
michael@0 301 },
michael@0 302 ]
michael@0 303 },
michael@0 304
michael@0 305 {
michael@0 306 name: "Do/while loop",
michael@0 307 input: "do{x}while(y)",
michael@0 308 output: "do {\n" +
michael@0 309 " x\n" +
michael@0 310 "} while (y)\n"
michael@0 311 },
michael@0 312
michael@0 313 {
michael@0 314 name: "Arrays",
michael@0 315 input: "var a=[1,2,3];",
michael@0 316 output: "var a = [\n" +
michael@0 317 " 1,\n" +
michael@0 318 " 2,\n" +
michael@0 319 " 3\n" +
michael@0 320 "];\n"
michael@0 321 },
michael@0 322
michael@0 323 {
michael@0 324 name: "Code that relies on ASI",
michael@0 325 input: "var foo = 10\n" +
michael@0 326 "var bar = 20\n" +
michael@0 327 "function g() {\n" +
michael@0 328 " a()\n" +
michael@0 329 " b()\n" +
michael@0 330 "}",
michael@0 331 output: "var foo = 10\n" +
michael@0 332 "var bar = 20\n" +
michael@0 333 "function g() {\n" +
michael@0 334 " a()\n" +
michael@0 335 " b()\n" +
michael@0 336 "}\n"
michael@0 337 },
michael@0 338
michael@0 339 {
michael@0 340 name: "Ternary operator",
michael@0 341 input: "bar?baz:bang;",
michael@0 342 output: "bar ? baz : bang;\n"
michael@0 343 },
michael@0 344
michael@0 345 {
michael@0 346 name: "Switch statements",
michael@0 347 input: "switch(x){case a:foo();break;default:bar()}",
michael@0 348 output: "switch (x) {\n" +
michael@0 349 "case a:\n" +
michael@0 350 " foo();\n" +
michael@0 351 " break;\n" +
michael@0 352 "default:\n" +
michael@0 353 " bar()\n" +
michael@0 354 "}\n"
michael@0 355 },
michael@0 356
michael@0 357 {
michael@0 358 name: "Multiple single line comments",
michael@0 359 input: "function f() {\n" +
michael@0 360 " // a\n" +
michael@0 361 " // b\n" +
michael@0 362 " // c\n" +
michael@0 363 "}\n",
michael@0 364 output: "function f() {\n" +
michael@0 365 " // a\n" +
michael@0 366 " // b\n" +
michael@0 367 " // c\n" +
michael@0 368 "}\n",
michael@0 369 },
michael@0 370
michael@0 371 {
michael@0 372 name: "Indented multiline comment",
michael@0 373 input: "function foo() {\n" +
michael@0 374 " /**\n" +
michael@0 375 " * java doc style comment\n" +
michael@0 376 " * more comment\n" +
michael@0 377 " */\n" +
michael@0 378 " bar();\n" +
michael@0 379 "}\n",
michael@0 380 output: "function foo() {\n" +
michael@0 381 " /**\n" +
michael@0 382 " * java doc style comment\n" +
michael@0 383 " * more comment\n" +
michael@0 384 " */\n" +
michael@0 385 " bar();\n" +
michael@0 386 "}\n",
michael@0 387 },
michael@0 388
michael@0 389 {
michael@0 390 name: "ASI return",
michael@0 391 input: "function f() {\n" +
michael@0 392 " return\n" +
michael@0 393 " {}\n" +
michael@0 394 "}\n",
michael@0 395 output: "function f() {\n" +
michael@0 396 " return\n" +
michael@0 397 " {\n" +
michael@0 398 " }\n" +
michael@0 399 "}\n",
michael@0 400 },
michael@0 401
michael@0 402 {
michael@0 403 name: "Non-ASI property access",
michael@0 404 input: "[1,2,3]\n" +
michael@0 405 "[0]",
michael@0 406 output: "[\n" +
michael@0 407 " 1,\n" +
michael@0 408 " 2,\n" +
michael@0 409 " 3\n" +
michael@0 410 "]\n" +
michael@0 411 "[0]\n"
michael@0 412 },
michael@0 413
michael@0 414 {
michael@0 415 name: "Non-ASI in",
michael@0 416 input: "'x'\n" +
michael@0 417 "in foo",
michael@0 418 output: "'x' in foo\n"
michael@0 419 },
michael@0 420
michael@0 421 {
michael@0 422 name: "Non-ASI function call",
michael@0 423 input: "f\n" +
michael@0 424 "()",
michael@0 425 output: "f()\n"
michael@0 426 },
michael@0 427
michael@0 428 {
michael@0 429 name: "Non-ASI new",
michael@0 430 input: "new\n" +
michael@0 431 "F()",
michael@0 432 output: "new F()\n"
michael@0 433 },
michael@0 434
michael@0 435 {
michael@0 436 name: "Getter and setter literals",
michael@0 437 input: "var obj={get foo(){return this._foo},set foo(v){this._foo=v}}",
michael@0 438 output: "var obj = {\n" +
michael@0 439 " get foo() {\n" +
michael@0 440 " return this._foo\n" +
michael@0 441 " },\n" +
michael@0 442 " set foo(v) {\n" +
michael@0 443 " this._foo = v\n" +
michael@0 444 " }\n" +
michael@0 445 "}\n"
michael@0 446 },
michael@0 447
michael@0 448 {
michael@0 449 name: "Escaping backslashes in strings",
michael@0 450 input: "'\\\\'\n",
michael@0 451 output: "'\\\\'\n"
michael@0 452 },
michael@0 453
michael@0 454 {
michael@0 455 name: "Escaping carriage return in strings",
michael@0 456 input: "'\\r'\n",
michael@0 457 output: "'\\r'\n"
michael@0 458 },
michael@0 459
michael@0 460 {
michael@0 461 name: "Escaping tab in strings",
michael@0 462 input: "'\\t'\n",
michael@0 463 output: "'\\t'\n"
michael@0 464 },
michael@0 465
michael@0 466 {
michael@0 467 name: "Escaping vertical tab in strings",
michael@0 468 input: "'\\v'\n",
michael@0 469 output: "'\\v'\n"
michael@0 470 },
michael@0 471
michael@0 472 {
michael@0 473 name: "Escaping form feed in strings",
michael@0 474 input: "'\\f'\n",
michael@0 475 output: "'\\f'\n"
michael@0 476 },
michael@0 477
michael@0 478 {
michael@0 479 name: "Escaping null character in strings",
michael@0 480 input: "'\\0'\n",
michael@0 481 output: "'\\0'\n"
michael@0 482 },
michael@0 483
michael@0 484 ];
michael@0 485
michael@0 486 var sourceMap = this.sourceMap || require("source-map");
michael@0 487
michael@0 488 function run_test() {
michael@0 489 testCases.forEach(function (test) {
michael@0 490 console.log(test.name);
michael@0 491
michael@0 492 var actual = prettyFast(test.input, {
michael@0 493 indent: " ",
michael@0 494 url: "test.js"
michael@0 495 });
michael@0 496
michael@0 497 if (actual.code !== test.output) {
michael@0 498 throw new Error("Expected:\n" + test.output
michael@0 499 + "\nGot:\n" + actual.code);
michael@0 500 }
michael@0 501
michael@0 502 if (test.mappings) {
michael@0 503 var smc = new sourceMap.SourceMapConsumer(actual.map.toJSON());
michael@0 504 test.mappings.forEach(function (m) {
michael@0 505 var query = { line: m.outputLine, column: 0 };
michael@0 506 var original = smc.originalPositionFor(query);
michael@0 507 if (original.line != m.inputLine) {
michael@0 508 throw new Error("Querying:\n" + JSON.stringify(query, null, 2) + "\n"
michael@0 509 + "Expected line:\n" + m.inputLine + "\n"
michael@0 510 + "Got:\n" + JSON.stringify(original, null, 2));
michael@0 511 }
michael@0 512 });
michael@0 513 }
michael@0 514 });
michael@0 515 console.log("✓ All tests pass!");
michael@0 516 }
michael@0 517
michael@0 518 // Only run the tests if this is node and we are running this file
michael@0 519 // directly. (Firefox's test runner will import this test file, and then call
michael@0 520 // run_test itself.)
michael@0 521 if (typeof require == "function" && typeof module == "object"
michael@0 522 && require.main === module) {
michael@0 523 run_test();
michael@0 524 }

mercurial