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' show | 10 import 'package:compiler/src/apiimpl.dart' show |
11 Compiler; | 11 CompilerImpl; |
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/elements/elements.dart' show | 14 import 'package:compiler/src/elements/elements.dart' show |
15 ClassElement, | 15 ClassElement, |
16 Element; | 16 Element; |
17 | 17 |
18 const String TEST_MAIN_FILE = 'test.dart'; | 18 const String TEST_MAIN_FILE = 'test.dart'; |
19 | 19 |
20 class TestEntry { | 20 class TestEntry { |
21 final String source; | 21 final String source; |
22 final String expectation; | 22 final String expectation; |
23 final String elementName; | 23 final String elementName; |
24 | 24 |
25 const TestEntry(this.source, [this.expectation]) | 25 const TestEntry(this.source, [this.expectation]) |
26 : elementName = null; | 26 : elementName = null; |
27 | 27 |
28 const TestEntry.forMethod(this.elementName, | 28 const TestEntry.forMethod(this.elementName, |
29 this.source, this.expectation); | 29 this.source, this.expectation); |
30 } | 30 } |
31 | 31 |
32 String formatTest(Map test) { | 32 String formatTest(Map test) { |
33 return test[TEST_MAIN_FILE]; | 33 return test[TEST_MAIN_FILE]; |
34 } | 34 } |
35 | 35 |
36 String getCodeForMain(Compiler compiler) { | 36 String getCodeForMain(CompilerImpl compiler) { |
37 Element mainFunction = compiler.mainFunction; | 37 Element mainFunction = compiler.mainFunction; |
38 js.Node ast = compiler.enqueuer.codegen.generatedCode[mainFunction]; | 38 js.Node ast = compiler.enqueuer.codegen.generatedCode[mainFunction]; |
39 return js.prettyPrint(ast, compiler).getText(); | 39 return js.prettyPrint(ast, compiler).getText(); |
40 } | 40 } |
41 | 41 |
42 String getCodeForMethod(Compiler compiler, | 42 String getCodeForMethod(CompilerImpl compiler, |
43 String name) { | 43 String name) { |
44 Element foundElement; | 44 Element foundElement; |
45 for (Element element in compiler.enqueuer.codegen.generatedCode.keys) { | 45 for (Element element in compiler.enqueuer.codegen.generatedCode.keys) { |
46 if (element.toString() == name) { | 46 if (element.toString() == name) { |
47 if (foundElement != null) { | 47 if (foundElement != null) { |
48 Expect.fail('Multiple compiled elements are called $name'); | 48 Expect.fail('Multiple compiled elements are called $name'); |
49 } | 49 } |
50 foundElement = element; | 50 foundElement = element; |
51 } | 51 } |
52 } | 52 } |
(...skipping 10 matching lines...) Expand all Loading... |
63 for (TestEntry test in tests) { | 63 for (TestEntry test in tests) { |
64 Map files = {TEST_MAIN_FILE: test.source}; | 64 Map files = {TEST_MAIN_FILE: test.source}; |
65 asyncTest(() async { | 65 asyncTest(() async { |
66 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); | 66 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); |
67 try { | 67 try { |
68 CompilationResult result = await runCompiler( | 68 CompilationResult result = await runCompiler( |
69 entryPoint: uri, | 69 entryPoint: uri, |
70 memorySourceFiles: files, | 70 memorySourceFiles: files, |
71 options: <String>['--use-cps-ir']); | 71 options: <String>['--use-cps-ir']); |
72 Expect.isTrue(result.isSuccess); | 72 Expect.isTrue(result.isSuccess); |
73 Compiler compiler = result.compiler; | 73 CompilerImpl compiler = result.compiler; |
74 String expectation = test.expectation; | 74 String expectation = test.expectation; |
75 if (expectation != null) { | 75 if (expectation != null) { |
76 String expected = test.expectation; | 76 String expected = test.expectation; |
77 String found = test.elementName == null | 77 String found = test.elementName == null |
78 ? getCodeForMain(compiler) | 78 ? getCodeForMain(compiler) |
79 : getCodeForMethod(compiler, test.elementName); | 79 : getCodeForMethod(compiler, test.elementName); |
80 if (expected != found) { | 80 if (expected != found) { |
81 Expect.fail('Expected:\n$expected\nbut found\n$found'); | 81 Expect.fail('Expected:\n$expected\nbut found\n$found'); |
82 } | 82 } |
83 } | 83 } |
84 } catch (e, st) { | 84 } catch (e, st) { |
85 print(e); | 85 print(e); |
86 print(st); | 86 print(st); |
87 Expect.fail('The following test failed to compile:\n' | 87 Expect.fail('The following test failed to compile:\n' |
88 '${formatTest(files)}'); | 88 '${formatTest(files)}'); |
89 } | 89 } |
90 }); | 90 }); |
91 } | 91 } |
92 } | 92 } |
OLD | NEW |