Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 'package:unittest/unittest.dart'; | 5 import 'package:unittest/unittest.dart'; |
| 6 | 6 |
| 7 import 'package:analyzer_experimental/src/generated/scanner.dart'; | 7 import 'package:analyzer_experimental/src/generated/scanner.dart'; |
| 8 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; | 8 import 'package:analyzer_experimental/src/services/formatter_impl.dart'; |
| 9 import 'package:analyzer_experimental/src/services/writer.dart'; | 9 import 'package:analyzer_experimental/src/services/writer.dart'; |
| 10 | 10 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 | 61 |
| 62 test('CU (5)', () { | 62 test('CU (5)', () { |
| 63 expectCUFormatsTo( | 63 expectCUFormatsTo( |
| 64 'class A { int meaningOfLife() => 42; }', | 64 'class A { int meaningOfLife() => 42; }', |
| 65 'class A {\n' | 65 'class A {\n' |
| 66 ' int meaningOfLife() => 42;\n' | 66 ' int meaningOfLife() => 42;\n' |
| 67 '}' | 67 '}' |
| 68 ); | 68 ); |
| 69 }); | 69 }); |
| 70 | 70 |
| 71 test('CU - EOL comments', () { | |
| 72 expectCUFormatsTo( | |
| 73 '//comment one\n\n' | |
| 74 '//comment two\n\n', | |
| 75 '//comment one\n\n' | |
| 76 '//comment two\n\n' | |
| 77 ); | |
| 78 expectCUFormatsTo( | |
| 79 'library foo;\n' | |
| 80 '\n' | |
| 81 '//comment one\n' | |
| 82 '\n' | |
| 83 'class C {\n' | |
| 84 '}\n', | |
| 85 'library foo;\n' | |
| 86 '\n' | |
| 87 '//comment one\n' | |
| 88 '\n' | |
| 89 'class C {\n' | |
| 90 '}\n' | |
| 91 ); | |
| 92 expectCUFormatsTo( | |
| 93 'library foo;\n' | |
| 94 '\n' | |
| 95 '//comment one\n' | |
| 96 '\n' | |
| 97 '//comment two\n' | |
| 98 '\n' | |
| 99 'class C {\n' | |
| 100 '}\n', | |
| 101 'library foo;\n' | |
| 102 '\n' | |
| 103 '//comment one\n' | |
| 104 '\n' | |
| 105 '//comment two\n' | |
| 106 '\n' | |
| 107 'class C {\n' | |
| 108 '}\n' | |
| 109 ); | |
| 110 }); | |
| 71 | 111 |
| 72 // test('CU - comments', () { | 112 test('CU - nested functions', () { |
| 73 // expectCUFormatsTo( | 113 expectCUFormatsTo( |
| 74 // 'library foo;\n' | 114 'x() {\n' |
| 75 // '\n' | 115 ' y() {\n' |
| 76 // '//comment one\n\n' | 116 ' }\n' |
| 77 // '//comment two\n\n' | 117 '}\n', |
| 78 // 'class C {\n}\n', | 118 'x() {\n' |
| 79 // 'library foo;\n' | 119 ' y() {\n' |
| 80 // '\n' | 120 ' }\n' |
| 81 // '//comment one\n\n' | 121 '}\n' |
| 82 // '//comment two\n\n' | 122 ); |
| 83 // 'class C {\n}\n' | 123 }); |
| 84 // ); | |
| 85 // }); | |
| 86 | 124 |
| 87 test('CU - top level', () { | 125 test('CU - top level', () { |
| 88 expectCUFormatsTo( | 126 expectCUFormatsTo( |
| 89 '\n\n' | 127 '\n\n' |
| 90 'foo() {\n' | 128 'foo() {\n' |
| 91 '}\n' | 129 '}\n' |
| 92 'bar() {\n' | 130 'bar() {\n' |
| 93 '}\n', | 131 '}\n', |
| 94 '\n\n' | 132 '\n\n' |
| 95 'foo() {\n' | 133 'foo() {\n' |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 109 expectCUFormatsTo( | 147 expectCUFormatsTo( |
| 110 'import "dart:io";\n\n' | 148 'import "dart:io";\n\n' |
| 111 'import "package:unittest/unittest.dart";\n' | 149 'import "package:unittest/unittest.dart";\n' |
| 112 'foo() {\n' | 150 'foo() {\n' |
| 113 '}\n', | 151 '}\n', |
| 114 'import "dart:io";\n\n' | 152 'import "dart:io";\n\n' |
| 115 'import "package:unittest/unittest.dart";\n' | 153 'import "package:unittest/unittest.dart";\n' |
| 116 'foo() {\n' | 154 'foo() {\n' |
| 117 '}\n' | 155 '}\n' |
| 118 ); | 156 ); |
| 157 expectCUFormatsTo( | |
| 158 'library a; class B { }', | |
| 159 'library a;\n' | |
| 160 'class B {\n' | |
| 161 '}' | |
| 162 ); | |
| 119 }); | 163 }); |
| 120 | 164 |
| 121 test('CU - method invocations', () { | 165 test('CU - method invocations', () { |
| 122 expectCUFormatsTo( | 166 expectCUFormatsTo( |
| 123 'class A {\n' | 167 'class A {\n' |
| 124 ' foo() {\n' | 168 ' foo() {\n' |
| 125 ' bar();\n' | 169 ' bar();\n' |
| 126 ' for (int i = 0; i < 42; i++) {\n' | 170 ' for (int i = 0; i < 42; i++) {\n' |
| 127 ' baz();\n' | 171 ' baz();\n' |
| 128 ' }\n' | 172 ' }\n' |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 '}\n', | 314 '}\n', |
| 271 'class A {\n' | 315 'class A {\n' |
| 272 '}\n\n' | 316 '}\n\n' |
| 273 'class B {\n\n\n' | 317 'class B {\n\n\n' |
| 274 ' int b() => 42;\n\n' | 318 ' int b() => 42;\n\n' |
| 275 ' int c() => b();\n\n' | 319 ' int c() => b();\n\n' |
| 276 '}\n' | 320 '}\n' |
| 277 ); | 321 ); |
| 278 }); | 322 }); |
| 279 | 323 |
| 324 test('CU - Block comments', () { | |
| 325 expectCUFormatsTo( | |
| 326 '/** Old school class comment */\n' | |
| 327 'class C {\n' | |
| 328 ' /** Foo! */ int foo() => 42;\n' | |
| 329 '}\n', | |
| 330 '/** Old school class comment */\n' | |
| 331 'class C {\n' | |
| 332 ' /** Foo! */ int foo() => 42;\n' | |
| 333 '}\n' | |
| 334 ); | |
| 335 expectCUFormatsTo( | |
| 336 'library foo;\n' | |
| 337 'class C /* is cool */ {\n' | |
| 338 ' /* int */ foo() => 42;\n' | |
| 339 '}\n', | |
| 340 'library foo;\n' | |
| 341 'class C /* is cool */ {\n' | |
| 342 ' /* int */ foo() => 42;\n' | |
| 343 '}\n' | |
| 344 ); | |
| 345 expectCUFormatsTo( | |
| 346 'library foo;\n' | |
| 347 '/* A long\n' | |
| 348 ' * Comment\n' | |
| 349 '*/\n' | |
| 350 'class C /* is cool */ {\n' | |
| 351 ' /* int */ foo() => 42;\n' | |
| 352 '}\n', | |
| 353 'library foo;\n' | |
| 354 '/* A long\n' | |
| 355 ' * Comment\n' | |
| 356 '*/\n' | |
| 357 'class C /* is cool */ {\n' | |
| 358 ' /* int */ foo() => 42;\n' | |
| 359 '}\n' | |
| 360 ); | |
| 361 expectCUFormatsTo( | |
|
Brian Wilkerson
2013/08/27 21:12:45
Related to my earlier questions, what happens if t
pquitslund
2013/08/27 21:55:38
Predictably this breaks. Thanks for the catch. S
| |
| 362 'library foo;\n' | |
| 363 '/* A long\n' | |
| 364 ' * Comment\n' | |
| 365 '*/\n' | |
| 366 '\n' | |
| 367 '/* And\n' | |
| 368 ' * another...\n' | |
| 369 '*/\n' | |
| 370 '\n' | |
| 371 '// Mixing it up\n' | |
| 372 '\n' | |
| 373 'class C /* is cool */ {\n' | |
| 374 ' /* int */ foo() => 42;\n' | |
| 375 '}\n', | |
| 376 'library foo;\n' | |
| 377 '/* A long\n' | |
| 378 ' * Comment\n' | |
| 379 '*/\n' | |
| 380 '\n' | |
| 381 '/* And\n' | |
| 382 ' * another...\n' | |
| 383 '*/\n' | |
| 384 '\n' | |
| 385 '// Mixing it up\n' | |
| 386 '\n' | |
| 387 'class C /* is cool */ {\n' | |
| 388 ' /* int */ foo() => 42;\n' | |
| 389 '}\n' | |
| 390 ); | |
| 391 expectCUFormatsTo( | |
| 392 '/// Copyright info\n' | |
| 393 '\n' | |
| 394 'library foo;\n' | |
| 395 '/// Class comment\n' | |
| 396 '//TODO: implement\n' | |
| 397 'class C {\n' | |
| 398 '}\n', | |
| 399 '/// Copyright info\n' | |
| 400 '\n' | |
| 401 'library foo;\n' | |
| 402 '/// Class comment\n' | |
| 403 '//TODO: implement\n' | |
| 404 'class C {\n' | |
| 405 '}\n' | |
| 406 ); | |
| 407 }); | |
| 408 | |
| 409 | |
| 280 test('CU - constructor', () { | 410 test('CU - constructor', () { |
| 281 expectCUFormatsTo( | 411 expectCUFormatsTo( |
| 282 'class A {\n' | 412 'class A {\n' |
| 283 ' const _a;\n' | 413 ' const _a;\n' |
| 284 ' A();\n' | 414 ' A();\n' |
| 285 ' int a() => _a;\n' | 415 ' int a() => _a;\n' |
| 286 '}\n', | 416 '}\n', |
| 287 'class A {\n' | 417 'class A {\n' |
| 288 ' const _a;\n' | 418 ' const _a;\n' |
| 289 ' A();\n' | 419 ' A();\n' |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 'class A {\n' | 477 'class A {\n' |
| 348 ' int _a;\n' | 478 ' int _a;\n' |
| 349 ' A(this._a);\n' | 479 ' A(this._a);\n' |
| 350 '}\n' | 480 '}\n' |
| 351 ); | 481 ); |
| 352 }); | 482 }); |
| 353 | 483 |
| 354 test('CU - parts', () { | 484 test('CU - parts', () { |
| 355 expectCUFormatsTo( | 485 expectCUFormatsTo( |
| 356 'part of foo;', | 486 'part of foo;', |
| 357 'part of foo;' | 487 'part of foo;\n' |
| 358 ); | 488 ); |
| 359 }); | 489 }); |
| 360 | 490 |
| 361 test('stmt', () { | 491 test('stmt', () { |
| 362 expectStmtFormatsTo( | 492 expectStmtFormatsTo( |
| 363 'if (true){\n' | 493 'if (true){\n' |
| 364 'if (true){\n' | 494 'if (true){\n' |
| 365 'if (true){\n' | 495 'if (true){\n' |
| 366 'return true;\n' | 496 'return true;\n' |
| 367 '} else{\n' | 497 '} else{\n' |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 ); | 536 ); |
| 407 }); | 537 }); |
| 408 | 538 |
| 409 test('stmt (generics)', () { | 539 test('stmt (generics)', () { |
| 410 expectStmtFormatsTo( | 540 expectStmtFormatsTo( |
| 411 'var numbers = <int>[1, 2, (3 + 4)];', | 541 'var numbers = <int>[1, 2, (3 + 4)];', |
| 412 'var numbers = <int>[1, 2, (3 + 4)];' | 542 'var numbers = <int>[1, 2, (3 + 4)];' |
| 413 ); | 543 ); |
| 414 }); | 544 }); |
| 415 | 545 |
| 546 test('stmt (maps)', () { | |
| 547 expectStmtFormatsTo( | |
| 548 'var map = const {"foo": "bar", "fuz": null};', | |
| 549 'var map = const {"foo": "bar", "fuz": null};' | |
| 550 ); | |
| 551 }); | |
| 552 | |
| 416 test('stmt (try/catch)', () { | 553 test('stmt (try/catch)', () { |
| 417 expectStmtFormatsTo( | 554 expectStmtFormatsTo( |
| 418 'try {\n' | 555 'try {\n' |
| 419 'doSomething();\n' | 556 'doSomething();\n' |
| 420 '} catch (e) {\n' | 557 '} catch (e) {\n' |
| 421 'print(e);\n' | 558 'print(e);\n' |
| 422 '}', | 559 '}', |
| 423 'try {\n' | 560 'try {\n' |
| 424 ' doSomething();\n' | 561 ' doSomething();\n' |
| 425 '} catch (e) {\n' | 562 '} catch (e) {\n' |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 609 String formatCU(src, {options: const FormatterOptions()}) => | 746 String formatCU(src, {options: const FormatterOptions()}) => |
| 610 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); | 747 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); |
| 611 | 748 |
| 612 String formatStatement(src, {options: const FormatterOptions()}) => | 749 String formatStatement(src, {options: const FormatterOptions()}) => |
| 613 new CodeFormatter(options).format(CodeKind.STATEMENT, src); | 750 new CodeFormatter(options).format(CodeKind.STATEMENT, src); |
| 614 | 751 |
| 615 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); | 752 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); |
| 616 | 753 |
| 617 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), | 754 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), |
| 618 equals(expected)); | 755 equals(expected)); |
| OLD | NEW |