| 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 /// End-to-end test of the analyzer2dart compiler. | 5 /// End-to-end test of the analyzer2dart compiler. |
| 6 | 6 library test.end2end; |
| 7 import 'dart:async'; | |
| 8 | 7 |
| 9 import 'mock_sdk.dart'; | 8 import 'mock_sdk.dart'; |
| 10 import 'package:analyzer/file_system/memory_file_system.dart'; | 9 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 11 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
| 12 import 'package:analyzer/src/generated/sdk.dart'; | 11 import 'package:analyzer/src/generated/sdk.dart'; |
| 13 import 'package:analyzer/src/generated/source.dart'; | 12 import 'package:analyzer/src/generated/source.dart'; |
| 14 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 15 | 14 |
| 16 import '../lib/src/closed_world.dart'; | 15 import '../lib/src/closed_world.dart'; |
| 17 import '../lib/src/driver.dart'; | 16 import '../lib/src/driver.dart'; |
| 18 import '../lib/src/converted_world.dart'; | 17 import '../lib/src/converted_world.dart'; |
| 19 import '../lib/src/dart_backend.dart'; | 18 import '../lib/src/dart_backend.dart'; |
| 20 | 19 |
| 20 import 'test_helper.dart' hide TestSpec; |
| 21 import 'output_helper.dart'; |
| 22 import 'end2end_data.dart'; |
| 23 |
| 21 main() { | 24 main() { |
| 22 test('Empty main', () { | 25 performTests(TEST_DATA, unittester, checkResult); |
| 23 String expectedResult = ''' | |
| 24 main() {} | |
| 25 '''; | |
| 26 checkResult(''' | |
| 27 main() {}''', | |
| 28 expectedResult); | |
| 29 | |
| 30 checkResult(''' | |
| 31 main() {} | |
| 32 foo() {}''', | |
| 33 expectedResult); | |
| 34 }); | |
| 35 | |
| 36 test('Simple call-chains', () { | |
| 37 checkResult(''' | |
| 38 foo() {} | |
| 39 main() { | |
| 40 foo(); | |
| 41 } | |
| 42 '''); | |
| 43 | |
| 44 checkResult(''' | |
| 45 bar() {} | |
| 46 foo() { | |
| 47 bar(); | |
| 48 } | |
| 49 main() { | |
| 50 foo(); | |
| 51 } | |
| 52 '''); | |
| 53 | |
| 54 checkResult(''' | |
| 55 bar() { | |
| 56 main(); | |
| 57 } | |
| 58 foo() { | |
| 59 bar(); | |
| 60 } | |
| 61 main() { | |
| 62 foo(); | |
| 63 } | |
| 64 '''); | |
| 65 }); | |
| 66 | |
| 67 test('Literals', () { | |
| 68 checkResult(''' | |
| 69 main() { | |
| 70 return 0; | |
| 71 } | |
| 72 '''); | |
| 73 | |
| 74 checkResult(''' | |
| 75 main() { | |
| 76 return 1.5; | |
| 77 } | |
| 78 '''); | |
| 79 | |
| 80 checkResult(''' | |
| 81 main() { | |
| 82 return true; | |
| 83 } | |
| 84 '''); | |
| 85 | |
| 86 checkResult(''' | |
| 87 main() { | |
| 88 return false; | |
| 89 } | |
| 90 '''); | |
| 91 | |
| 92 checkResult(''' | |
| 93 main() { | |
| 94 return "a"; | |
| 95 } | |
| 96 '''); | |
| 97 | |
| 98 checkResult(''' | |
| 99 main() { | |
| 100 return "a" "b"; | |
| 101 } | |
| 102 ''', ''' | |
| 103 main() { | |
| 104 return "ab"; | |
| 105 } | |
| 106 '''); | |
| 107 }); | |
| 108 | |
| 109 test('Parameters', () { | |
| 110 checkResult(''' | |
| 111 main(args) {} | |
| 112 '''); | |
| 113 | |
| 114 checkResult(''' | |
| 115 main(a, b) {} | |
| 116 '''); | |
| 117 }); | |
| 118 | |
| 119 test('Typed parameters', () { | |
| 120 checkResult(''' | |
| 121 void main(args) {} | |
| 122 '''); | |
| 123 | |
| 124 checkResult(''' | |
| 125 main(int a, String b) {} | |
| 126 '''); | |
| 127 | |
| 128 checkResult(''' | |
| 129 main(Comparator a, List b) {} | |
| 130 '''); | |
| 131 | |
| 132 checkResult(''' | |
| 133 main(Comparator<dynamic> a, List<dynamic> b) {} | |
| 134 ''', ''' | |
| 135 main(Comparator a, List b) {} | |
| 136 '''); | |
| 137 | |
| 138 checkResult(''' | |
| 139 main(Map a, Map<dynamic, List<int>> b) {} | |
| 140 '''); | |
| 141 }); | |
| 142 | |
| 143 test('Pass arguments', () { | |
| 144 checkResult(''' | |
| 145 foo(a) {} | |
| 146 main() { | |
| 147 foo(null); | |
| 148 } | |
| 149 '''); | |
| 150 | |
| 151 checkResult(''' | |
| 152 bar(b, c) {} | |
| 153 foo(a) {} | |
| 154 main() { | |
| 155 foo(null); | |
| 156 bar(0, ""); | |
| 157 } | |
| 158 '''); | |
| 159 | |
| 160 checkResult(''' | |
| 161 bar(b) {} | |
| 162 foo(a) { | |
| 163 bar(a); | |
| 164 } | |
| 165 main() { | |
| 166 foo(null); | |
| 167 } | |
| 168 '''); | |
| 169 }); | |
| 170 | |
| 171 test('Top level field access', () { | |
| 172 checkResult(''' | |
| 173 main(args) { | |
| 174 return deprecated; | |
| 175 } | |
| 176 '''); | |
| 177 }); | |
| 178 | |
| 179 test('Local variables', () { | |
| 180 checkResult(''' | |
| 181 main() { | |
| 182 var a; | |
| 183 return a; | |
| 184 } | |
| 185 ''', ''' | |
| 186 main() {} | |
| 187 '''); | |
| 188 | |
| 189 checkResult(''' | |
| 190 main() { | |
| 191 var a = 0; | |
| 192 return a; | |
| 193 } | |
| 194 ''', ''' | |
| 195 main() { | |
| 196 return 0; | |
| 197 } | |
| 198 '''); | |
| 199 }); | |
| 200 | |
| 201 test('Dynamic access', () { | |
| 202 checkResult(''' | |
| 203 main(a) { | |
| 204 return a.foo; | |
| 205 } | |
| 206 ''', ''' | |
| 207 main(a) { | |
| 208 return a.foo; | |
| 209 } | |
| 210 '''); | |
| 211 | |
| 212 checkResult(''' | |
| 213 main() { | |
| 214 var a = ""; | |
| 215 return a.foo; | |
| 216 } | |
| 217 ''', ''' | |
| 218 main() { | |
| 219 return "".foo; | |
| 220 } | |
| 221 '''); | |
| 222 }); | |
| 223 | |
| 224 test('Dynamic invocation', () { | |
| 225 checkResult(''' | |
| 226 main(a) { | |
| 227 return a.foo(0); | |
| 228 } | |
| 229 ''', ''' | |
| 230 main(a) { | |
| 231 return a.foo(0); | |
| 232 } | |
| 233 '''); | |
| 234 | |
| 235 checkResult(''' | |
| 236 main() { | |
| 237 var a = ""; | |
| 238 return a.foo(0, 1); | |
| 239 } | |
| 240 ''', ''' | |
| 241 main() { | |
| 242 return "".foo(0, 1); | |
| 243 } | |
| 244 '''); | |
| 245 }); | |
| 246 | |
| 247 test('Binary expressions', () { | |
| 248 checkResult(''' | |
| 249 main(a) { | |
| 250 return a + deprecated; | |
| 251 } | |
| 252 ''', ''' | |
| 253 main(a) { | |
| 254 return a + deprecated; | |
| 255 } | |
| 256 '''); | |
| 257 | |
| 258 checkResult(''' | |
| 259 main(a) { | |
| 260 return a - deprecated; | |
| 261 } | |
| 262 ''', ''' | |
| 263 main(a) { | |
| 264 return a - deprecated; | |
| 265 } | |
| 266 '''); | |
| 267 | |
| 268 checkResult(''' | |
| 269 main(a) { | |
| 270 return a * deprecated; | |
| 271 } | |
| 272 ''', ''' | |
| 273 main(a) { | |
| 274 return a * deprecated; | |
| 275 } | |
| 276 '''); | |
| 277 | |
| 278 checkResult(''' | |
| 279 main(a) { | |
| 280 return a / deprecated; | |
| 281 } | |
| 282 ''', ''' | |
| 283 main(a) { | |
| 284 return a / deprecated; | |
| 285 } | |
| 286 '''); | |
| 287 | |
| 288 checkResult(''' | |
| 289 main(a) { | |
| 290 return a ~/ deprecated; | |
| 291 } | |
| 292 ''', ''' | |
| 293 main(a) { | |
| 294 return a ~/ deprecated; | |
| 295 } | |
| 296 '''); | |
| 297 | |
| 298 checkResult(''' | |
| 299 main(a) { | |
| 300 return a % deprecated; | |
| 301 } | |
| 302 ''', ''' | |
| 303 main(a) { | |
| 304 return a % deprecated; | |
| 305 } | |
| 306 '''); | |
| 307 | |
| 308 checkResult(''' | |
| 309 main(a) { | |
| 310 return a < deprecated; | |
| 311 } | |
| 312 ''', ''' | |
| 313 main(a) { | |
| 314 return a < deprecated; | |
| 315 } | |
| 316 '''); | |
| 317 | |
| 318 checkResult(''' | |
| 319 main(a) { | |
| 320 return a <= deprecated; | |
| 321 } | |
| 322 ''', ''' | |
| 323 main(a) { | |
| 324 return a <= deprecated; | |
| 325 } | |
| 326 '''); | |
| 327 | |
| 328 checkResult(''' | |
| 329 main(a) { | |
| 330 return a > deprecated; | |
| 331 } | |
| 332 ''', ''' | |
| 333 main(a) { | |
| 334 return a > deprecated; | |
| 335 } | |
| 336 '''); | |
| 337 | |
| 338 checkResult(''' | |
| 339 main(a) { | |
| 340 return a >= deprecated; | |
| 341 } | |
| 342 ''', ''' | |
| 343 main(a) { | |
| 344 return a >= deprecated; | |
| 345 } | |
| 346 '''); | |
| 347 | |
| 348 checkResult(''' | |
| 349 main(a) { | |
| 350 return a << deprecated; | |
| 351 } | |
| 352 ''', ''' | |
| 353 main(a) { | |
| 354 return a << deprecated; | |
| 355 } | |
| 356 '''); | |
| 357 | |
| 358 checkResult(''' | |
| 359 main(a) { | |
| 360 return a >> deprecated; | |
| 361 } | |
| 362 ''', ''' | |
| 363 main(a) { | |
| 364 return a >> deprecated; | |
| 365 } | |
| 366 '''); | |
| 367 | |
| 368 checkResult(''' | |
| 369 main(a) { | |
| 370 return a & deprecated; | |
| 371 } | |
| 372 ''', ''' | |
| 373 main(a) { | |
| 374 return a & deprecated; | |
| 375 } | |
| 376 '''); | |
| 377 | |
| 378 checkResult(''' | |
| 379 main(a) { | |
| 380 return a | deprecated; | |
| 381 } | |
| 382 ''', ''' | |
| 383 main(a) { | |
| 384 return a | deprecated; | |
| 385 } | |
| 386 '''); | |
| 387 | |
| 388 checkResult(''' | |
| 389 main(a) { | |
| 390 return a ^ deprecated; | |
| 391 } | |
| 392 ''', ''' | |
| 393 main(a) { | |
| 394 return a ^ deprecated; | |
| 395 } | |
| 396 '''); | |
| 397 | |
| 398 checkResult(''' | |
| 399 main(a) { | |
| 400 return a == deprecated; | |
| 401 } | |
| 402 ''', ''' | |
| 403 main(a) { | |
| 404 return a == deprecated; | |
| 405 } | |
| 406 '''); | |
| 407 | |
| 408 checkResult(''' | |
| 409 main(a) { | |
| 410 return a != deprecated; | |
| 411 } | |
| 412 ''', ''' | |
| 413 main(a) { | |
| 414 return !(a == deprecated); | |
| 415 } | |
| 416 '''); | |
| 417 | |
| 418 checkResult(''' | |
| 419 main(a) { | |
| 420 return a || deprecated; | |
| 421 } | |
| 422 ''', ''' | |
| 423 main(a) { | |
| 424 return a || deprecated; | |
| 425 } | |
| 426 '''); | |
| 427 | |
| 428 checkResult(''' | |
| 429 main(a) { | |
| 430 return a && deprecated; | |
| 431 } | |
| 432 ''', ''' | |
| 433 main(a) { | |
| 434 return a && deprecated; | |
| 435 } | |
| 436 '''); | |
| 437 }); | |
| 438 | |
| 439 test('If statement', () { | |
| 440 checkResult(''' | |
| 441 main(a) { | |
| 442 if (a) { | |
| 443 print(0); | |
| 444 } | |
| 445 } | |
| 446 ''', ''' | |
| 447 main(a) { | |
| 448 if (a) { | |
| 449 print(0); | |
| 450 } | |
| 451 } | |
| 452 '''); | |
| 453 | |
| 454 checkResult(''' | |
| 455 main(a) { | |
| 456 if (a) { | |
| 457 print(0); | |
| 458 } else { | |
| 459 print(1); | |
| 460 } | |
| 461 } | |
| 462 ''', ''' | |
| 463 main(a) { | |
| 464 a ? print(0) : print(1); | |
| 465 } | |
| 466 '''); | |
| 467 | |
| 468 checkResult(''' | |
| 469 main(a) { | |
| 470 if (a) { | |
| 471 print(0); | |
| 472 } else { | |
| 473 print(1); | |
| 474 print(2); | |
| 475 } | |
| 476 } | |
| 477 ''', ''' | |
| 478 main(a) { | |
| 479 if (a) { | |
| 480 print(0); | |
| 481 } else { | |
| 482 print(1); | |
| 483 print(2); | |
| 484 } | |
| 485 } | |
| 486 '''); | |
| 487 }); | |
| 488 | |
| 489 test('If statement', () { | |
| 490 checkResult(''' | |
| 491 main(a) { | |
| 492 return a ? print(0) : print(1); | |
| 493 } | |
| 494 ''', ''' | |
| 495 main(a) { | |
| 496 return a ? print(0) : print(1); | |
| 497 } | |
| 498 '''); | |
| 499 }); | |
| 500 } | 26 } |
| 501 | 27 |
| 502 checkResult(String input, [String expectedOutput]) { | 28 checkResult(TestSpec result) { |
| 503 if (expectedOutput == null) { | 29 String input = result.input; |
| 504 expectedOutput = input; | 30 String expectedOutput = result.output; |
| 505 } | |
| 506 | 31 |
| 507 CollectingOutputProvider outputProvider = new CollectingOutputProvider(); | 32 CollectingOutputProvider outputProvider = new CollectingOutputProvider(); |
| 508 MemoryResourceProvider provider = new MemoryResourceProvider(); | 33 MemoryResourceProvider provider = new MemoryResourceProvider(); |
| 509 DartSdk sdk = new MockSdk(); | 34 DartSdk sdk = new MockSdk(); |
| 510 Driver driver = new Driver(provider, sdk, outputProvider); | 35 Driver driver = new Driver(provider, sdk, outputProvider); |
| 511 String rootFile = '/root.dart'; | 36 String rootFile = '/root.dart'; |
| 512 provider.newFile(rootFile, input); | 37 provider.newFile(rootFile, input); |
| 513 Source rootSource = driver.setRoot(rootFile); | 38 Source rootSource = driver.setRoot(rootFile); |
| 514 FunctionElement entryPoint = driver.resolveEntryPoint(rootSource); | 39 FunctionElement entryPoint = driver.resolveEntryPoint(rootSource); |
| 515 ClosedWorld world = driver.computeWorld(entryPoint); | 40 ClosedWorld world = driver.computeWorld(entryPoint); |
| 516 ConvertedWorld convertedWorld = convertWorld(world); | 41 ConvertedWorld convertedWorld = convertWorld(world); |
| 517 compileToDart(driver, convertedWorld); | 42 compileToDart(driver, convertedWorld); |
| 518 String output = outputProvider.output.text; | 43 String output = outputProvider.output.text; |
| 519 expect(output, equals(expectedOutput)); | 44 expect(output, equals(expectedOutput)); |
| 520 } | 45 } |
| 521 | |
| 522 class CollectingOutputProvider { | |
| 523 StringBufferSink output; | |
| 524 | |
| 525 EventSink<String> call(String name, String extension) { | |
| 526 return output = new StringBufferSink(); | |
| 527 } | |
| 528 } | |
| 529 | |
| 530 class StringBufferSink implements EventSink<String> { | |
| 531 StringBuffer sb = new StringBuffer(); | |
| 532 | |
| 533 void add(String text) { | |
| 534 sb.write(text); | |
| 535 } | |
| 536 | |
| 537 void addError(errorEvent, [StackTrace stackTrace]) {} | |
| 538 | |
| 539 void close() {} | |
| 540 | |
| 541 String get text => sb.toString(); | |
| 542 } | |
| 543 | |
| OLD | NEW |