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 |
| 11 main() { | 11 main() { |
| 12 | 12 |
| 13 /// Formatter tests | 13 /// Formatter tests |
| 14 group('formatter', () { | 14 group('formatter', () { |
| 15 | 15 |
| 16 test('failed parse', () { | 16 test('failed parse', () { |
| 17 var formatter = new CodeFormatter(); | 17 var formatter = new CodeFormatter(); |
| 18 expect(() => formatter.format(CodeKind.COMPILATION_UNIT, '~'), | 18 expect(() => formatter.format(CodeKind.COMPILATION_UNIT, '~'), |
| 19 throwsA(new isInstanceOf<FormatterException>())); | 19 throwsA(new isInstanceOf<FormatterException>())); |
| 20 }); | 20 }); |
| 21 | 21 |
| 22 test('CU (1)', () { | 22 test('CU (1)', () { |
| 23 expectCUFormatsTo( | 23 expectCUFormatsTo( |
| 24 'class A {\n' | 24 'class A {\n' |
| 25 '}', | 25 '}\n', |
| 26 'class A {\n' | 26 'class A {\n' |
| 27 '}\n' | 27 '}\n' |
| 28 ); | 28 ); |
| 29 }); | 29 }); |
| 30 | 30 |
| 31 test('CU (2)', () { | 31 test('CU (2)', () { |
| 32 expectCUFormatsTo( | 32 expectCUFormatsTo( |
| 33 'class A { \n' | 33 'class A { \n' |
| 34 '}', | 34 '}\n', |
| 35 'class A {\n' | 35 'class A {\n' |
| 36 '}\n' | 36 '}\n' |
| 37 ); | 37 ); |
| 38 }); | 38 }); |
| 39 | 39 |
| 40 test('CU (3)', () { | 40 test('CU (3)', () { |
| 41 expectCUFormatsTo( | 41 expectCUFormatsTo( |
| 42 'class A {\n' | 42 'class A {\n' |
| 43 ' }', | 43 ' }', |
| 44 'class A {\n' | 44 'class A {\n' |
| 45 '}\n' | 45 '}' |
| 46 ); | 46 ); |
| 47 }); | 47 }); |
| 48 | 48 |
| 49 test('CU (4)', () { | 49 test('CU (4)', () { |
| 50 expectCUFormatsTo( | 50 expectCUFormatsTo( |
| 51 ' class A {\n' | 51 ' class A {\n' |
| 52 '}', | 52 '}\n', |
| 53 'class A {\n' | 53 'class A {\n' |
| 54 '}\n' | 54 '}\n' |
| 55 ); | 55 ); |
| 56 }); | 56 }); |
| 57 | 57 |
| 58 test('CU (5)', () { | |
| 59 expectCUFormatsTo( | |
| 60 'class A { int meaningOfLife() => 42; }', | |
| 61 'class A {\n' | |
| 62 ' int meaningOfLife() => 42;\n' | |
| 63 '}' | |
| 64 ); | |
| 65 }); | |
| 66 | |
| 67 | |
| 68 // test('CU - comments', () { | |
| 69 // expectCUFormatsTo( | |
| 70 // 'library foo;\n' | |
| 71 // '\n' | |
| 72 // '//comment one\n\n' | |
| 73 // '//comment two\n\n' | |
| 74 // 'class C {\n}\n', | |
| 75 // 'library foo;\n' | |
| 76 // '\n' | |
| 77 // '//comment one\n\n' | |
| 78 // '//comment two\n\n' | |
| 79 // 'class C {\n}\n' | |
| 80 // ); | |
| 81 // }); | |
| 82 | |
| 83 test('CU - top level', () { | |
| 84 expectCUFormatsTo( | |
| 85 '\n\n' | |
| 86 'foo() {\n' | |
| 87 '}\n' | |
| 88 'bar() {\n' | |
| 89 '}\n', | |
| 90 '\n\n' | |
| 91 'foo() {\n' | |
| 92 '}\n' | |
| 93 'bar() {\n' | |
| 94 '}\n' | |
| 95 ); | |
| 96 }); | |
| 97 | |
| 98 test('CU - imports', () { | |
| 99 expectCUFormatsTo( | |
| 100 'import "dart:io";\n\n' | |
| 101 'import "package:unittest/unittest.dart";\n' | |
| 102 'foo() {\n' | |
| 103 '}\n', | |
| 104 'import "dart:io";\n\n' | |
| 105 'import "package:unittest/unittest.dart";\n' | |
| 106 'foo() {\n' | |
| 107 '}\n' | |
| 108 ); | |
| 109 }); | |
| 110 | |
| 58 test('CU w/class decl comment', () { | 111 test('CU w/class decl comment', () { |
| 59 expectCUFormatsTo( | 112 expectCUFormatsTo( |
| 60 'import "foo";\n\n' | 113 'import "foo";\n\n' |
| 61 '//Killer class\n' | 114 '//Killer class\n' |
| 62 'class A {\n' | 115 'class A {\n' |
| 63 '}', | 116 '}', |
| 64 'import "foo";\n\n' | 117 'import "foo";\n\n' |
| 65 '//Killer class\n' | 118 '//Killer class\n' |
| 66 'class A {\n' | 119 'class A {\n' |
| 67 '}\n' | 120 '}' |
| 68 ); | 121 ); |
| 69 }); | 122 }); |
| 70 | 123 |
| 71 | 124 |
| 72 test('CU (method indent)', () { | 125 test('CU (method indent)', () { |
| 73 expectCUFormatsTo( | 126 expectCUFormatsTo( |
| 74 'class A {\n' | 127 'class A {\n' |
| 75 'void x(){\n' | 128 'void x(){\n' |
| 76 '}\n' | 129 '}\n' |
| 77 '}', | 130 '}\n', |
| 78 'class A {\n' | 131 'class A {\n' |
| 79 ' void x() {\n' | 132 ' void x() {\n' |
| 80 ' }\n' | 133 ' }\n' |
| 81 '}\n' | 134 '}\n' |
| 82 ); | 135 ); |
| 83 }); | 136 }); |
| 84 | 137 |
| 85 test('CU (method indent - 2)', () { | 138 test('CU (method indent - 2)', () { |
| 86 expectCUFormatsTo( | 139 expectCUFormatsTo( |
| 87 'class A {\n' | 140 'class A {\n' |
| 88 ' static bool x(){ return true; }\n' | 141 ' static bool x(){\n' |
| 89 ' }', | 142 'return true; }\n' |
| 143 ' }\n', | |
| 90 'class A {\n' | 144 'class A {\n' |
| 91 ' static bool x() {\n' | 145 ' static bool x() {\n' |
| 92 ' return true;\n' | 146 ' return true;\n' |
| 93 ' }\n' | 147 ' }\n' |
| 94 '}\n' | 148 '}\n' |
| 95 ); | 149 ); |
| 96 }); | 150 }); |
| 97 | 151 |
| 98 test('CU (method indent - 3)', () { | 152 test('CU (method indent - 3)', () { |
| 99 expectCUFormatsTo( | 153 expectCUFormatsTo( |
| 100 'class A {\n' | 154 'class A {\n' |
| 101 ' int x() => 42 + 3 ; \n' | 155 ' int x() => 42 + 3 ; \n' |
| 102 ' }', | 156 ' }\n', |
| 103 'class A {\n' | 157 'class A {\n' |
| 104 ' int x() => 42 + 3;\n' | 158 ' int x() => 42 + 3;\n' |
| 105 '}\n' | 159 '}\n' |
| 106 ); | 160 ); |
| 107 }); | 161 }); |
| 108 | 162 |
| 109 test('CU (method indent - 4)', () { | 163 test('CU (method indent - 4)', () { |
| 110 expectCUFormatsTo( | 164 expectCUFormatsTo( |
| 111 'class A {\n' | 165 'class A {\n' |
| 112 ' int x() { \n' | 166 ' int x() { \n' |
| 113 'if (true) {return 42;\n' | 167 'if (true) {\nreturn 42;\n' |
|
Brian Wilkerson
2013/08/20 15:46:39
It seems like it would be easier to read the tests
pquitslund
2013/08/20 16:49:35
Agreed. I'll do a test cleanup pass soon.
| |
| 114 '} else { return 13; }\n' | 168 '} else {\n' |
| 169 'return 13;\n }\n' | |
| 115 ' }' | 170 ' }' |
| 116 '}', | 171 '}\n', |
| 117 'class A {\n' | 172 'class A {\n' |
| 118 ' int x() {\n' | 173 ' int x() {\n' |
| 119 ' if (true) {\n' | 174 ' if (true) {\n' |
| 120 ' return 42;\n' | 175 ' return 42;\n' |
| 121 ' } else {\n' | 176 ' } else {\n' |
| 122 ' return 13;\n' | 177 ' return 13;\n' |
| 123 ' }\n' | 178 ' }\n' |
| 124 ' }\n' | 179 ' }\n' |
| 125 '}\n' | 180 '}\n' |
| 126 ); | 181 ); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 | 240 |
| 186 test('stmt (switch)', () { | 241 test('stmt (switch)', () { |
| 187 expectStmtFormatsTo( | 242 expectStmtFormatsTo( |
| 188 'switch (fruit) {\n' | 243 'switch (fruit) {\n' |
| 189 'case "apple":\n' | 244 'case "apple":\n' |
| 190 'print("delish");\n' | 245 'print("delish");\n' |
| 191 'break;\n' | 246 'break;\n' |
| 192 'case "fig":\n' | 247 'case "fig":\n' |
| 193 'print("bleh");\n' | 248 'print("bleh");\n' |
| 194 'break;\n' | 249 'break;\n' |
| 195 '}\n', | 250 '}', |
| 196 'switch (fruit) {\n' | 251 'switch (fruit) {\n' |
| 197 ' case "apple":\n' | 252 ' case "apple":\n' |
| 198 ' print("delish");\n' | 253 ' print("delish");\n' |
| 199 ' break;\n' | 254 ' break;\n' |
| 200 ' case "fig":\n' | 255 ' case "fig":\n' |
| 201 ' print("bleh");\n' | 256 ' print("bleh");\n' |
| 202 ' break;\n' | 257 ' break;\n' |
| 203 '}\n' | 258 '}' |
| 204 ); | 259 ); |
| 205 }); | 260 }); |
| 206 | 261 |
| 207 test('stmt (generics)', () { | 262 test('stmt (generics)', () { |
| 208 expectStmtFormatsTo( | 263 expectStmtFormatsTo( |
| 209 'var numbers = <int>[1, 2, (3 + 4)];', | 264 'var numbers = <int>[1, 2, (3 + 4)];', |
| 210 'var numbers = <int>[1, 2, (3 + 4)];' | 265 'var numbers = <int>[1, 2, (3 + 4)];' |
| 211 ); | 266 ); |
| 212 }); | 267 }); |
| 213 | 268 |
| 214 test('stmt (try/catch)', () { | 269 test('stmt (try/catch)', () { |
| 215 expectStmtFormatsTo( | 270 expectStmtFormatsTo( |
| 216 'try {\n' | 271 'try {\n' |
| 217 'doSomething();\n' | 272 'doSomething();\n' |
| 218 '} catch (e) {\n' | 273 '} catch (e) {\n' |
| 219 'print(e);\n' | 274 'print(e);\n' |
| 220 '}\n', | 275 '}', |
| 221 'try {\n' | 276 'try {\n' |
| 222 ' doSomething();\n' | 277 ' doSomething();\n' |
| 223 '} catch (e) {\n' | 278 '} catch (e) {\n' |
| 224 ' print(e);\n' | 279 ' print(e);\n' |
| 225 '}\n' | 280 '}' |
| 226 ); | 281 ); |
| 227 }); | 282 }); |
| 228 | 283 |
| 229 test('stmt (binary/ternary ops)', () { | 284 test('stmt (binary/ternary ops)', () { |
| 230 expectStmtFormatsTo( | 285 expectStmtFormatsTo( |
| 231 'var a = 1 + 2 / (3 * -b);', | 286 'var a = 1 + 2 / (3 * -b);', |
| 232 'var a = 1 + 2 / (3 * -b);' | 287 'var a = 1 + 2 / (3 * -b);' |
| 233 ); | 288 ); |
| 234 expectStmtFormatsTo( | 289 expectStmtFormatsTo( |
| 235 'var c = !condition == a > b;', | 290 'var c = !condition == a > b;', |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 String formatCU(src, {options: const FormatterOptions()}) => | 435 String formatCU(src, {options: const FormatterOptions()}) => |
| 381 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); | 436 new CodeFormatter(options).format(CodeKind.COMPILATION_UNIT, src); |
| 382 | 437 |
| 383 String formatStatement(src, {options: const FormatterOptions()}) => | 438 String formatStatement(src, {options: const FormatterOptions()}) => |
| 384 new CodeFormatter(options).format(CodeKind.STATEMENT, src); | 439 new CodeFormatter(options).format(CodeKind.STATEMENT, src); |
| 385 | 440 |
| 386 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); | 441 expectCUFormatsTo(src, expected) => expect(formatCU(src), equals(expected)); |
| 387 | 442 |
| 388 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), | 443 expectStmtFormatsTo(src, expected) => expect(formatStatement(src), |
| 389 equals(expected)); | 444 equals(expected)); |
| OLD | NEW |