| 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 is able to compile the provided | 6 // Test that the CPS IR code generator is able to compile the provided |
| 7 // example programs. | 7 // example programs. |
| 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'; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 """, | 61 """, |
| 62 """function() { | 62 """function() { |
| 63 return V.foo(); | 63 return V.foo(); |
| 64 }"""), | 64 }"""), |
| 65 const TestEntry("main() {}"), | 65 const TestEntry("main() {}"), |
| 66 const TestEntry("main() { return 42; }"), | 66 const TestEntry("main() { return 42; }"), |
| 67 const TestEntry("main() { return; }", """ | 67 const TestEntry("main() { return; }", """ |
| 68 function() { | 68 function() { |
| 69 return null; | 69 return null; |
| 70 }"""), | 70 }"""), |
| 71 const TestEntry("main() { return true ? 42 : 'foo'; }"), |
| 72 const TestEntry(""" |
| 73 foo() => foo; |
| 74 main() { |
| 75 print(foo() ? "hello world" : "bad bad"); |
| 76 }""", |
| 77 """function() { |
| 78 P.print(P.identical(V.foo(), true) ? "hello world" : "bad bad"); |
| 79 return null; |
| 80 }"""), |
| 81 const TestEntry(""" |
| 82 get foo => foo; |
| 83 main() { |
| 84 print(foo ? "hello world" : "bad bad"); |
| 85 }""", |
| 86 """function() { |
| 87 P.print(P.identical(V.foo(), true) ? "hello world" : "bad bad"); |
| 88 return null; |
| 89 }"""), |
| 90 const TestEntry(""" |
| 91 get foo => foo; |
| 92 main() { print(foo && foo); }""", |
| 93 """function() { |
| 94 P.print(P.identical(V.foo(), true) && P.identical(V.foo(), true)); |
| 95 return null; |
| 96 }"""), |
| 97 const TestEntry(""" |
| 98 get foo => foo; |
| 99 main() { print(foo || foo); }""", |
| 100 """function() { |
| 101 P.print(P.identical(V.foo(), true) || P.identical(V.foo(), true)); |
| 102 return null; |
| 103 }"""), |
| 71 ]; | 104 ]; |
| 72 | 105 |
| 73 String formatTest(Map test) { | 106 String formatTest(Map test) { |
| 74 return test[TEST_MAIN_FILE]; | 107 return test[TEST_MAIN_FILE]; |
| 75 } | 108 } |
| 76 | 109 |
| 77 String getCodeForMain(Compiler compiler) { | 110 String getCodeForMain(Compiler compiler) { |
| 78 Element mainFunction = compiler.mainFunction; | 111 Element mainFunction = compiler.mainFunction; |
| 79 js.Node ast = compiler.enqueuer.codegen.generatedCode[mainFunction]; | 112 js.Node ast = compiler.enqueuer.codegen.generatedCode[mainFunction]; |
| 80 return js.prettyPrint(ast, compiler).getText(); | 113 return js.prettyPrint(ast, compiler).getText(); |
| 81 } | 114 } |
| 82 | 115 |
| 83 main() { | 116 main() { |
| 84 Expect.isTrue(const bool.fromEnvironment("USE_CPS_IR")); | 117 Expect.isTrue(const bool.fromEnvironment("USE_CPS_IR")); |
| 85 | 118 |
| 86 for (TestEntry test in tests) { | 119 for (TestEntry test in tests) { |
| 87 Map files = {TEST_MAIN_FILE: test.source}; | 120 Map files = {TEST_MAIN_FILE: test.source}; |
| 88 asyncTest(() { | 121 asyncTest(() { |
| 89 Compiler compiler = compilerFor(files); | 122 Compiler compiler = compilerFor(files); |
| 90 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); | 123 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); |
| 91 return compiler.run(uri).then((_) { | 124 return compiler.run(uri).then((_) { |
| 92 Expect.isNotNull(compiler.assembledCode); | 125 Expect.isNotNull(compiler.assembledCode); |
| 93 String expectation = test.expectation; | 126 String expectation = test.expectation; |
| 94 if (expectation != null) { | 127 if (expectation != null) { |
| 95 Expect.equals(test.expectation, getCodeForMain(compiler)); | 128 String expected = test.expectation; |
| 129 String found = getCodeForMain(compiler); |
| 130 if (expected != found) { |
| 131 Expect.fail('Expected:\n$expected\nbut found\n$found'); |
| 132 } |
| 96 } | 133 } |
| 97 }).catchError((e) { | 134 }).catchError((e) { |
| 98 print(e); | 135 print(e); |
| 99 Expect.fail('The following test failed to compile:\n' | 136 Expect.fail('The following test failed to compile:\n' |
| 100 '${formatTest(files)}'); | 137 '${formatTest(files)}'); |
| 101 }); | 138 }); |
| 102 }); | 139 }); |
| 103 } | 140 } |
| 104 } | 141 } |
| OLD | NEW |