| 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 // VMOptions=-DUSE_CPS_IR=true | 4 // VMOptions=-DUSE_CPS_IR=true |
| 5 | 5 |
| 6 // Test that the CPS IR code generator compiles programs and produces the | 6 // Test that the CPS IR code generator compiles programs and produces the |
| 7 // the expected output. | 7 // the expected output. |
| 8 | 8 |
| 9 import 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
| 10 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
| 11 import 'package:compiler/src/apiimpl.dart' | 11 import 'package:compiler/src/apiimpl.dart' |
| 12 show Compiler; | 12 show Compiler; |
| 13 import 'memory_compiler.dart'; | 13 import 'memory_compiler.dart'; |
| 14 import 'package:compiler/src/js/js.dart' as js; | 14 import 'package:compiler/src/js/js.dart' as js; |
| 15 import 'package:compiler/src/common.dart' show Element; | 15 import 'package:compiler/src/common.dart' show Element; |
| 16 | 16 |
| 17 import 'js_backend_cps_ir_basic.dart' as basic; | 17 import 'js_backend_cps_ir_basic.dart' as basic; |
| 18 import 'js_backend_cps_ir_literals.dart' as literals; | 18 import 'js_backend_cps_ir_literals.dart' as literals; |
| 19 import 'js_backend_cps_ir_operators.dart' as operators; |
| 19 | 20 |
| 20 const String TEST_MAIN_FILE = 'test.dart'; | 21 const String TEST_MAIN_FILE = 'test.dart'; |
| 21 | 22 |
| 22 List<TestEntry> tests = <TestEntry>[] | 23 List<TestEntry> tests = <TestEntry>[] |
| 23 ..addAll(basic.tests) | 24 ..addAll(basic.tests) |
| 24 ..addAll(literals.tests); | 25 ..addAll(literals.tests) |
| 26 ..addAll(operators.tests); |
| 25 | 27 |
| 26 class TestEntry { | 28 class TestEntry { |
| 27 final String source; | 29 final String source; |
| 28 final String expectation; | 30 final String expectation; |
| 29 const TestEntry(this.source, [this.expectation]); | 31 const TestEntry(this.source, [this.expectation]); |
| 30 } | 32 } |
| 31 | 33 |
| 32 String formatTest(Map test) { | 34 String formatTest(Map test) { |
| 33 return test[TEST_MAIN_FILE]; | 35 return test[TEST_MAIN_FILE]; |
| 34 } | 36 } |
| 35 | 37 |
| 36 String getCodeForMain(Compiler compiler) { | 38 String getCodeForMain(Compiler compiler) { |
| 37 Element mainFunction = compiler.mainFunction; | 39 Element mainFunction = compiler.mainFunction; |
| 38 js.Node ast = compiler.enqueuer.codegen.generatedCode[mainFunction]; | 40 js.Node ast = compiler.enqueuer.codegen.generatedCode[mainFunction]; |
| 39 return js.prettyPrint(ast, compiler).getText(); | 41 return js.prettyPrint(ast, compiler).getText(); |
| 40 } | 42 } |
| 41 | 43 |
| 42 main() { | 44 main() { |
| 43 Expect.isTrue(const bool.fromEnvironment("USE_CPS_IR")); | 45 Expect.isTrue(const bool.fromEnvironment("USE_CPS_IR")); |
| 44 | 46 |
| 45 for (TestEntry test in tests) { | 47 for (TestEntry test in tests) { |
| 46 Map files = {TEST_MAIN_FILE: test.source}; | 48 Map files = {TEST_MAIN_FILE: test.source}; |
| 47 asyncTest(() { | 49 asyncTest(() { |
| 48 Compiler compiler = compilerFor(files); | 50 Compiler compiler = compilerFor(files); |
| 49 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); | 51 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); |
| 50 return compiler.run(uri).then((_) { | 52 return compiler.run(uri).then((_) { |
| 51 Expect.isNotNull(compiler.assembledCode); | 53 Expect.isNotNull(compiler.assembledCode); |
| 52 String expectation = test.expectation; | 54 String expectation = test.expectation; |
| 53 if (expectation != null) { | 55 if (expectation != null) { |
| 54 Expect.equals(test.expectation, getCodeForMain(compiler)); | 56 String expected = test.expectation; |
| 57 String found = getCodeForMain(compiler); |
| 58 if (expected != found) { |
| 59 Expect.fail('Expected:\n$expected\nbut found\n$found'); |
| 60 } |
| 55 } | 61 } |
| 56 }).catchError((e) { | 62 }).catchError((e) { |
| 57 print(e); | 63 print(e); |
| 58 Expect.fail('The following test failed to compile:\n' | 64 Expect.fail('The following test failed to compile:\n' |
| 59 '${formatTest(files)}'); | 65 '${formatTest(files)}'); |
| 60 }); | 66 }); |
| 61 }); | 67 }); |
| 62 } | 68 } |
| 63 } | 69 } |
| OLD | NEW |