| 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 // 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 'package:async_helper/async_helper.dart'; | 8 import 'package:async_helper/async_helper.dart'; |
| 9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
| 10 import 'package:compiler/src/apiimpl.dart' | 10 import 'package:compiler/src/apiimpl.dart' |
| 11 show Compiler; | 11 show Compiler; |
| 12 import 'memory_compiler.dart'; | 12 import 'memory_compiler.dart'; |
| 13 import 'package:compiler/src/js/js.dart' as js; | 13 import 'package:compiler/src/js/js.dart' as js; |
| 14 import 'package:compiler/src/common.dart' show Element; | 14 import 'package:compiler/src/common.dart' show Element, ClassElement; |
| 15 | 15 |
| 16 const String TEST_MAIN_FILE = 'test.dart'; | 16 const String TEST_MAIN_FILE = 'test.dart'; |
| 17 | 17 |
| 18 class TestEntry { | 18 class TestEntry { |
| 19 final String source; | 19 final String source; |
| 20 final String expectation; | 20 final String expectation; |
| 21 const TestEntry(this.source, [this.expectation]); | 21 final String elementName; |
| 22 |
| 23 const TestEntry(this.source, [this.expectation]) |
| 24 : elementName = null; |
| 25 |
| 26 const TestEntry.forMethod(this.elementName, |
| 27 this.source, this.expectation); |
| 22 } | 28 } |
| 23 | 29 |
| 24 String formatTest(Map test) { | 30 String formatTest(Map test) { |
| 25 return test[TEST_MAIN_FILE]; | 31 return test[TEST_MAIN_FILE]; |
| 26 } | 32 } |
| 27 | 33 |
| 28 String getCodeForMain(Compiler compiler) { | 34 String getCodeForMain(Compiler compiler) { |
| 29 Element mainFunction = compiler.mainFunction; | 35 Element mainFunction = compiler.mainFunction; |
| 30 js.Node ast = compiler.enqueuer.codegen.generatedCode[mainFunction]; | 36 js.Node ast = compiler.enqueuer.codegen.generatedCode[mainFunction]; |
| 31 return js.prettyPrint(ast, compiler).getText(); | 37 return js.prettyPrint(ast, compiler).getText(); |
| 32 } | 38 } |
| 33 | 39 |
| 40 String getCodeForMethod(Compiler compiler, |
| 41 String name) { |
| 42 Element foundElement; |
| 43 for (Element element in compiler.enqueuer.codegen.generatedCode.keys) { |
| 44 if (element.toString() == name) { |
| 45 if (foundElement != null) { |
| 46 Expect.fail('Multiple compiled elements are called $name'); |
| 47 } |
| 48 foundElement = element; |
| 49 } |
| 50 } |
| 51 |
| 52 if (foundElement == null) { |
| 53 Expect.fail('There is no compiled element called $name'); |
| 54 } |
| 55 |
| 56 js.Node ast = compiler.enqueuer.codegen.generatedCode[foundElement]; |
| 57 return js.prettyPrint(ast, compiler).getText(); |
| 58 } |
| 34 | 59 |
| 35 runTests(List<TestEntry> tests) { | 60 runTests(List<TestEntry> tests) { |
| 36 Expect.isTrue(const bool.fromEnvironment("USE_CPS_IR")); | 61 Expect.isTrue(const bool.fromEnvironment("USE_CPS_IR"), |
| 62 'Run with USE_CPS_IR=true'); |
| 37 | 63 |
| 38 for (TestEntry test in tests) { | 64 for (TestEntry test in tests) { |
| 39 Map files = {TEST_MAIN_FILE: test.source}; | 65 Map files = {TEST_MAIN_FILE: test.source}; |
| 40 asyncTest(() { | 66 asyncTest(() { |
| 41 Compiler compiler = compilerFor(files); | 67 Compiler compiler = compilerFor(files); |
| 42 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); | 68 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); |
| 43 return compiler.run(uri).then((bool success) { | 69 return compiler.run(uri).then((bool success) { |
| 44 Expect.isTrue(success); | 70 Expect.isTrue(success); |
| 45 String expectation = test.expectation; | 71 String expectation = test.expectation; |
| 46 if (expectation != null) { | 72 if (expectation != null) { |
| 47 String expected = test.expectation; | 73 String expected = test.expectation; |
| 48 String found = getCodeForMain(compiler); | 74 String found = test.elementName == null |
| 75 ? getCodeForMain(compiler) |
| 76 : getCodeForMethod(compiler, test.elementName); |
| 49 if (expected != found) { | 77 if (expected != found) { |
| 50 Expect.fail('Expected:\n$expected\nbut found\n$found'); | 78 Expect.fail('Expected:\n$expected\nbut found\n$found'); |
| 51 } | 79 } |
| 52 } | 80 } |
| 53 }).catchError((e) { | 81 }).catchError((e) { |
| 54 print(e); | 82 print(e); |
| 55 Expect.fail('The following test failed to compile:\n' | 83 Expect.fail('The following test failed to compile:\n' |
| 56 '${formatTest(files)}'); | 84 '${formatTest(files)}'); |
| 57 }); | 85 }); |
| 58 }); | 86 }); |
| 59 } | 87 } |
| 60 } | 88 } |
| OLD | NEW |