OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'package:expect/expect.dart'; | 6 import 'package:expect/expect.dart'; |
7 import 'package:async_helper/async_helper.dart'; | 7 import 'package:async_helper/async_helper.dart'; |
8 import 'mock_compiler.dart'; | 8 import 'mock_compiler.dart'; |
9 import 'package:compiler/src/js/js.dart' as jsAst; | 9 import 'package:compiler/src/js/js.dart' as jsAst; |
10 import 'package:compiler/src/js/js.dart' show js; | 10 import 'package:compiler/src/js/js.dart' show js; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 testStatement('if (#a) #b;', | 126 testStatement('if (#a) #b;', |
127 {'a': false, 'b': block12}, | 127 {'a': false, 'b': block12}, |
128 ';'), | 128 ';'), |
129 testStatement('if (#a) 3; else #b;', | 129 testStatement('if (#a) 3; else #b;', |
130 {'a': true, 'b': block12}, | 130 {'a': true, 'b': block12}, |
131 '3;'), | 131 '3;'), |
132 testStatement('if (#a) 3; else #b;', | 132 testStatement('if (#a) 3; else #b;', |
133 {'a': false, 'b': block12}, | 133 {'a': false, 'b': block12}, |
134 '{\n 1;\n 2;\n}'), | 134 '{\n 1;\n 2;\n}'), |
135 | 135 |
| 136 |
136 testStatement('while (#) #', [eOne, block12], 'while (1) {\n 1;\n 2;\n}'), | 137 testStatement('while (#) #', [eOne, block12], 'while (1) {\n 1;\n 2;\n}'), |
137 testStatement('while (#) #;', [eTrue, block12], | 138 testStatement('while (#) #;', [eTrue, block12], |
138 'while (true) {\n 1;\n 2;\n}'), | 139 'while (true) {\n 1;\n 2;\n}'), |
139 testStatement('while (#) #;', [eVar, block12], | 140 testStatement('while (#) #;', [eVar, block12], |
140 'while (x) {\n 1;\n 2;\n}'), | 141 'while (x) {\n 1;\n 2;\n}'), |
141 testStatement('while (#) #;', ['a', block12], | 142 testStatement('while (#) #;', ['a', block12], |
142 'while (a) {\n 1;\n 2;\n}'), | 143 'while (a) {\n 1;\n 2;\n}'), |
143 testStatement('while (#) #;', ['a', stm], | 144 testStatement('while (#) #;', ['a', stm], |
144 'while (a)\n foo();'), | 145 'while (a)\n foo();'), |
145 | 146 |
146 testStatement( | 147 testStatement( |
147 'do { {print(1);} do while(true); while (false) } while ( true )', [], | 148 'do { {print(1);} do while(true); while (false) } while ( true )', [], |
148 ''' | 149 ''' |
149 do { | 150 do { |
150 print(1); | 151 print(1); |
151 do | 152 do |
152 while (true) | 153 while (true) |
153 ; | 154 ; |
154 while (false); | 155 while (false); |
155 } while (true); | 156 } while (true); |
156 '''), | 157 '''), |
157 testStatement('do #; while ( # )', [block12, eOne], | 158 testStatement('do #; while ( # )', [block12, eOne], |
158 'do {\n 1;\n 2;\n} while (1); '), | 159 'do {\n 1;\n 2;\n} while (1); '), |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 // Use the same name several times. | 317 // Use the same name several times. |
317 testStatement('#a.prototype.#a = function(#b) { return #c.#c };', | 318 testStatement('#a.prototype.#a = function(#b) { return #c.#c };', |
318 {'a': 'name1_2', | 319 {'a': 'name1_2', |
319 'b': ['r', 'y'], | 320 'b': ['r', 'y'], |
320 'c': 'name4_5'}, | 321 'c': 'name4_5'}, |
321 'name1_2.prototype.name1_2 = function(r, y) {\n' | 322 'name1_2.prototype.name1_2 = function(r, y) {\n' |
322 ' return name4_5.name4_5;\n' | 323 ' return name4_5.name4_5;\n' |
323 '};'), | 324 '};'), |
324 | 325 |
325 testStatement('label: while (a) { label2: break label;}', [], | 326 testStatement('label: while (a) { label2: break label;}', [], |
326 'label:\n while (a) {\n label2:\n break label;\n }'), | 327 'label:\n while (a)\n label2:\n break label;\n '), |
327 | 328 |
328 | 329 |
329 testStatement('var # = 3', ['x'], 'var x = 3;'), | 330 testStatement('var # = 3', ['x'], 'var x = 3;'), |
330 testStatement('var # = 3', | 331 testStatement('var # = 3', |
331 [new jsAst.VariableDeclaration('x')], | 332 [new jsAst.VariableDeclaration('x')], |
332 'var x = 3;'), | 333 'var x = 3;'), |
333 testStatement('var # = 3, # = #', | 334 testStatement('var # = 3, # = #', |
334 ['x', 'y', js.number(2)], | 335 ['x', 'y', js.number(2)], |
335 'var x = 3, y = 2;'), | 336 'var x = 3, y = 2;'), |
336 testStatement('var #a = 3, #b = #c', | 337 testStatement('var #a = 3, #b = #c', |
337 {"a": 'x', "b": 'y', "c": js.number(2)}, | 338 {"a": 'x', "b": 'y', "c": js.number(2)}, |
338 'var x = 3, y = 2;'), | 339 'var x = 3, y = 2;'), |
339 testStatement('function #() {}', ['x'], 'function x() {\n}'), | 340 testStatement('function #() {}', ['x'], 'function x() {\n}'), |
340 testStatement('function #() {}', | 341 testStatement('function #() {}', |
341 [new jsAst.VariableDeclaration('x')], | 342 [new jsAst.VariableDeclaration('x')], |
342 'function x() {\n}'), | 343 'function x() {\n}'), |
343 testStatement('try {} catch (#) {}', ['x'], 'try {\n} catch (x) {\n}'), | 344 testStatement('try {} catch (#) {}', ['x'], 'try {\n} catch (x) {\n}'), |
344 testStatement('try {} catch (#a) {}', {"a": 'x'}, 'try {\n} catch (x) {\n}')
, | 345 testStatement('try {} catch (#a) {}', {"a": 'x'}, 'try {\n} catch (x) {\n}')
, |
345 testStatement('try {} catch (#a) {}', | 346 testStatement('try {} catch (#a) {}', |
346 {"a": new jsAst.VariableDeclaration('x')}, | 347 {"a": new jsAst.VariableDeclaration('x')}, |
347 'try {\n} catch (x) {\n}'), | 348 'try {\n} catch (x) {\n}'), |
| 349 |
| 350 // Test that braces around a single-statement block are removed by printer. |
| 351 testStatement('while (a) {foo()}', [], |
| 352 'while (a)\n foo();'), |
| 353 testStatement('if (a) {foo();}', [], |
| 354 'if (a)\n foo();'), |
| 355 testStatement('if (a) {foo();} else {foo2();}', [], |
| 356 'if (a)\n foo();\nelse\n foo2();'), |
| 357 testStatement('if (a) foo(); else {foo2();}', [], |
| 358 'if (a)\n foo();\nelse\n foo2();'), |
| 359 testStatement('do {foo();} while(a);', [], |
| 360 'do\n foo();\nwhile (a);'), |
| 361 testStatement('label: {foo();}', [], |
| 362 'label:\n foo();'), |
| 363 testStatement('for (var key in a) {foo();}', [], |
| 364 'for (var key in a)\n foo();'), |
| 365 // Test dangling else: |
| 366 testStatement('if (a) {if (b) {foo1();}} else {foo2();}', [], """ |
| 367 if (a) { |
| 368 if (b) |
| 369 foo1(); |
| 370 } else |
| 371 foo2();"""), |
| 372 testStatement('if (a) {if (b) {foo1();} else {foo2();}}', [], """ |
| 373 if (a) |
| 374 if (b) |
| 375 foo1(); |
| 376 else |
| 377 foo2(); |
| 378 """), |
| 379 testStatement('if (a) {if (b) {foo1();} else {foo2();}} else {foo3();}', |
| 380 [], """ |
| 381 if (a) |
| 382 if (b) |
| 383 foo1(); |
| 384 else |
| 385 foo2(); |
| 386 else |
| 387 foo3();"""), |
| 388 testStatement('if (a) {while (true) if (b) {foo1();}} else {foo2();}', |
| 389 [], """ |
| 390 if (a) { |
| 391 while (true) |
| 392 if (b) |
| 393 foo1(); |
| 394 } else |
| 395 foo2();"""), |
348 ])); | 396 ])); |
349 } | 397 } |
OLD | NEW |