Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: tests/compiler/dart2js/js_parser_statements_test.dart

Issue 949753004: Avoid printing braces for one-statement blocks in js-printer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix whitespace Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 // `label: break label;` gives problems on IE. Test that it is avoided.
366 testStatement('label: {break label;}', [],
367 'label: {\n break label;\n}'),
368 // This works on IE:
369 testStatement('label: {label2: {break label;}}', [],
370 'label:\n label2:\n break label;\n'),
371 // Test dangling else:
372 testStatement('if (a) {if (b) {foo1();}} else {foo2();}', [], """
373 if (a) {
374 if (b)
375 foo1();
376 } else
377 foo2();"""),
378 testStatement('if (a) {if (b) {foo1();} else {foo2();}}', [], """
379 if (a)
380 if (b)
381 foo1();
382 else
383 foo2();
384 """),
385 testStatement('if (a) {if (b) {foo1();} else {foo2();}} else {foo3();}',
386 [], """
387 if (a)
388 if (b)
389 foo1();
390 else
391 foo2();
392 else
393 foo3();"""),
394 testStatement('if (a) {while (true) if (b) {foo1();}} else {foo2();}',
395 [], """
396 if (a) {
397 while (true)
398 if (b)
399 foo1();
400 } else
401 foo2();"""),
348 ])); 402 ]));
349 } 403 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698