| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Test that the CPS IR code generator compiles programs and produces the | |
| 6 // the expected output. | |
| 7 | |
| 8 import 'package:async_helper/async_helper.dart'; | |
| 9 import 'package:expect/expect.dart'; | |
| 10 import 'package:compiler/src/apiimpl.dart' show | |
| 11 CompilerImpl; | |
| 12 import 'memory_compiler.dart'; | |
| 13 import 'package:compiler/src/js/js.dart' as js; | |
| 14 import 'package:compiler/src/elements/elements.dart' show | |
| 15 ClassElement, | |
| 16 Element; | |
| 17 | |
| 18 const String TEST_MAIN_FILE = 'test.dart'; | |
| 19 | |
| 20 class TestEntry { | |
| 21 final String source; | |
| 22 final String expectation; | |
| 23 final String elementName; | |
| 24 | |
| 25 const TestEntry(this.source, [this.expectation]) | |
| 26 : elementName = null; | |
| 27 | |
| 28 const TestEntry.forMethod(this.elementName, | |
| 29 this.source, this.expectation); | |
| 30 } | |
| 31 | |
| 32 String formatTest(Map test) { | |
| 33 return test[TEST_MAIN_FILE]; | |
| 34 } | |
| 35 | |
| 36 String getCodeForMain(CompilerImpl compiler) { | |
| 37 Element mainFunction = compiler.mainFunction; | |
| 38 js.Node ast = compiler.enqueuer.codegen.generatedCode[mainFunction]; | |
| 39 return js.prettyPrint(ast, compiler).getText(); | |
| 40 } | |
| 41 | |
| 42 String getCodeForMethod(CompilerImpl compiler, | |
| 43 String name) { | |
| 44 Element foundElement; | |
| 45 for (Element element in compiler.enqueuer.codegen.generatedCode.keys) { | |
| 46 if (element.toString() == name) { | |
| 47 if (foundElement != null) { | |
| 48 Expect.fail('Multiple compiled elements are called $name'); | |
| 49 } | |
| 50 foundElement = element; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 if (foundElement == null) { | |
| 55 Expect.fail('There is no compiled element called $name'); | |
| 56 } | |
| 57 | |
| 58 js.Node ast = compiler.enqueuer.codegen.generatedCode[foundElement]; | |
| 59 return js.prettyPrint(ast, compiler).getText(); | |
| 60 } | |
| 61 | |
| 62 runTests(List<TestEntry> tests) { | |
| 63 for (TestEntry test in tests) { | |
| 64 Map files = {TEST_MAIN_FILE: test.source}; | |
| 65 asyncTest(() async { | |
| 66 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); | |
| 67 String expected = test.expectation; | |
| 68 String found = null; | |
| 69 try { | |
| 70 CompilationResult result = await runCompiler( | |
| 71 entryPoint: uri, | |
| 72 memorySourceFiles: files, | |
| 73 options: <String>['--use-cps-ir']); | |
| 74 Expect.isTrue(result.isSuccess); | |
| 75 CompilerImpl compiler = result.compiler; | |
| 76 if (expected != null) { | |
| 77 found = test.elementName == null | |
| 78 ? getCodeForMain(compiler) | |
| 79 : getCodeForMethod(compiler, test.elementName); | |
| 80 } | |
| 81 } catch (e, st) { | |
| 82 print(e); | |
| 83 print(st); | |
| 84 Expect.fail('The following test failed to compile:\n' | |
| 85 '${formatTest(files)}'); | |
| 86 } | |
| 87 if (expected != found) { | |
| 88 Expect.fail('Unexpected output for test:\n ' | |
| 89 '${formatTest(files).replaceAll('\n', '\n ')}\n' | |
| 90 'Expected:\n ${expected.replaceAll('\n', '\n ')}\n' | |
| 91 'but found:\n ${found?.replaceAll('\n', '\n ')}'); | |
| 92 } | |
| 93 }); | |
| 94 } | |
| 95 } | |
| OLD | NEW |