| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 // Test that the CPS IR code generator compiles programs and produces the | 5 // Test that the CPS IR code generator compiles programs and produces the |
| 6 // the expected output. | 6 // the expected output. |
| 7 | 7 |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:async_helper/async_helper.dart'; | 10 import 'package:async_helper/async_helper.dart'; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // test output. By default we match the output of main. | 21 // test output. By default we match the output of main. |
| 22 final RegExp elementNameRegExp = new RegExp(r'^// Method to test: (.*)$', | 22 final RegExp elementNameRegExp = new RegExp(r'^// Method to test: (.*)$', |
| 23 multiLine: true); | 23 multiLine: true); |
| 24 | 24 |
| 25 runTest(String filename, {bool update: false}) { | 25 runTest(String filename, {bool update: false}) { |
| 26 var outputname = filename.replaceFirst('.dart', '.js'); | 26 var outputname = filename.replaceFirst('.dart', '.js'); |
| 27 String source = new File.fromUri(Platform.script.resolve('input/$filename')) | 27 String source = new File.fromUri(Platform.script.resolve('input/$filename')) |
| 28 .readAsStringSync(); | 28 .readAsStringSync(); |
| 29 var expectedFile = | 29 var expectedFile = |
| 30 new File.fromUri(Platform.script.resolve('expected/$outputname')); | 30 new File.fromUri(Platform.script.resolve('expected/$outputname')); |
| 31 String expected = expectedFile.existsSync() | 31 String expected = ''; |
| 32 ? expectedFile.readAsStringSync() : ''; | 32 if (expectedFile.existsSync()) { |
| 33 expected = expectedFile.readAsStringSync() |
| 34 .replaceAll(new RegExp('^//.*\n', multiLine: true), '') |
| 35 .trim(); |
| 36 } |
| 37 |
| 33 var match = elementNameRegExp.firstMatch(source); | 38 var match = elementNameRegExp.firstMatch(source); |
| 34 var elementName = match?.group(1); | 39 var elementName = match?.group(1); |
| 35 | 40 |
| 36 Map files = { | 41 Map files = { |
| 37 TEST_MAIN_FILE: source, | 42 TEST_MAIN_FILE: source, |
| 38 'package:expect/expect.dart': ''' | 43 'package:expect/expect.dart': ''' |
| 39 class NoInline { | 44 class NoInline { |
| 40 const NoInline(); | 45 const NoInline(); |
| 41 } | 46 } |
| 42 class TrustTypeAnnotations { | 47 class TrustTypeAnnotations { |
| 43 const TrustTypeAnnotations(); | 48 const TrustTypeAnnotations(); |
| 44 } | 49 } |
| 45 class AssumeDynamic { | 50 class AssumeDynamic { |
| 46 const AssumeDynamic(); | 51 const AssumeDynamic(); |
| 47 } | 52 } |
| 48 ''', | 53 ''', |
| 49 }; | 54 }; |
| 50 asyncTest(() async { | 55 asyncTest(() async { |
| 51 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); | 56 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); |
| 52 String found = null; | 57 String found = null; |
| 53 try { | 58 try { |
| 54 CompilationResult result = await runCompiler( | 59 CompilationResult result = await runCompiler( |
| 55 entryPoint: uri, | 60 entryPoint: uri, |
| 56 memorySourceFiles: files, | 61 memorySourceFiles: files, |
| 57 options: <String>['--use-cps-ir']); | 62 options: <String>['--use-cps-ir']); |
| 58 Expect.isTrue(result.isSuccess); | 63 Expect.isTrue(result.isSuccess); |
| 59 CompilerImpl compiler = result.compiler; | 64 CompilerImpl compiler = result.compiler; |
| 60 if (expected != null) { | 65 if (expected != null) { |
| 61 String output = elementName == null | 66 found = elementName == null |
| 62 ? _getCodeForMain(compiler) | 67 ? _getCodeForMain(compiler) |
| 63 : _getCodeForMethod(compiler, elementName); | 68 : _getCodeForMethod(compiler, elementName); |
| 64 // Include the input in a comment of the expected file to make it easier | |
| 65 // to see the relation between input and output in code reviews. | |
| 66 found = '// Expectation for test: \n' | |
| 67 '// ${source.trim().replaceAll('\n', '\n// ')}\n\n' | |
| 68 '$output\n'; | |
| 69 } | 69 } |
| 70 } catch (e, st) { | 70 } catch (e, st) { |
| 71 print(e); | 71 print(e); |
| 72 print(st); | 72 print(st); |
| 73 var message = 'The following test failed to compile:\n' | 73 var message = 'The following test failed to compile:\n' |
| 74 '${_formatTest(files)}'; | 74 '${_formatTest(files)}'; |
| 75 if (update) { | 75 if (update) { |
| 76 print('\n\n$message\n'); | 76 print('\n\n$message\n'); |
| 77 return; | 77 return; |
| 78 } else { | 78 } else { |
| 79 Expect.fail(message); | 79 Expect.fail(message); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 if (expected != found) { | 82 if (expected != found) { |
| 83 if (update) { | 83 if (update) { |
| 84 expectedFile.writeAsStringSync(found); | 84 // Include the input in a comment of the expected file to make it easier |
| 85 // to see the relation between input and output in code reviews. |
| 86 String comment = source.trim().replaceAll('\n', '\n// '); |
| 87 expectedFile.writeAsStringSync('// Expectation for test: \n' |
| 88 '// ${comment}\n\n${found}\n'); |
| 85 print('INFO: $expectedFile was updated'); | 89 print('INFO: $expectedFile was updated'); |
| 86 } else { | 90 } else { |
| 87 Expect.fail('Unexpected output for test:\n ' | 91 Expect.fail('Unexpected output for test:\n ' |
| 88 '${_formatTest(files).replaceAll('\n', '\n ')}\n' | 92 '${_formatTest(files).replaceAll('\n', '\n ')}\n' |
| 89 'Expected:\n ${expected.replaceAll('\n', '\n ')}\n' | 93 'Expected:\n ${expected.replaceAll('\n', '\n ')}\n\n' |
| 90 'but found:\n ${found?.replaceAll('\n', '\n ')}\n' | 94 'but found:\n ${found?.replaceAll('\n', '\n ')}\n' |
| 91 '$regenerateCommand'); | 95 '$regenerateCommand'); |
| 92 } | 96 } |
| 93 } | 97 } |
| 94 }); | 98 }); |
| 95 } | 99 } |
| 96 | 100 |
| 97 String get regenerateCommand { | 101 String get regenerateCommand { |
| 98 var flags = Platform.packageRoot == null | 102 var flags = Platform.packageRoot == null |
| 99 ? '' : '--package-root=${Platform.packageRoot} '; | 103 ? '' : '--package-root=${Platform.packageRoot} '; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 } | 137 } |
| 134 } | 138 } |
| 135 | 139 |
| 136 if (foundElement == null) { | 140 if (foundElement == null) { |
| 137 Expect.fail('There is no compiled element called $name'); | 141 Expect.fail('There is no compiled element called $name'); |
| 138 } | 142 } |
| 139 | 143 |
| 140 js.Node ast = compiler.enqueuer.codegen.generatedCode[foundElement]; | 144 js.Node ast = compiler.enqueuer.codegen.generatedCode[foundElement]; |
| 141 return js.prettyPrint(ast, compiler); | 145 return js.prettyPrint(ast, compiler); |
| 142 } | 146 } |
| OLD | NEW |