| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, 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 generates source information. | |
| 6 | |
| 7 library source_information_tests; | |
| 8 | |
| 9 import 'package:async_helper/async_helper.dart'; | |
| 10 import 'package:expect/expect.dart'; | |
| 11 import 'package:compiler/src/apiimpl.dart' | |
| 12 show CompilerImpl; | |
| 13 import 'memory_compiler.dart'; | |
| 14 import 'package:compiler/src/cps_ir/cps_ir_nodes.dart' as ir; | |
| 15 import 'package:compiler/src/cps_ir/cps_ir_nodes_sexpr.dart' as ir; | |
| 16 import 'package:compiler/src/js/js.dart' as js; | |
| 17 import 'package:compiler/src/js_backend/js_backend.dart'; | |
| 18 import 'package:compiler/src/elements/elements.dart'; | |
| 19 | |
| 20 const String TEST_MAIN_FILE = 'test.dart'; | |
| 21 | |
| 22 class TestEntry { | |
| 23 final String source; | |
| 24 final List<String> expectation; | |
| 25 final String elementName; | |
| 26 | |
| 27 const TestEntry(this.source, this.expectation) | |
| 28 : elementName = null; | |
| 29 | |
| 30 const TestEntry.forMethod(this.elementName, | |
| 31 this.source, this.expectation); | |
| 32 } | |
| 33 | |
| 34 String formatTest(Map test) { | |
| 35 return test[TEST_MAIN_FILE]; | |
| 36 } | |
| 37 | |
| 38 js.Node getCodeForMain(CompilerImpl compiler) { | |
| 39 Element mainFunction = compiler.mainFunction; | |
| 40 return compiler.enqueuer.codegen.generatedCode[mainFunction]; | |
| 41 } | |
| 42 | |
| 43 js.Node getJsNodeForElement(CompilerImpl compiler, Element element) { | |
| 44 return compiler.enqueuer.codegen.generatedCode[element]; | |
| 45 } | |
| 46 | |
| 47 String getCodeForMethod(CompilerImpl compiler, String name) { | |
| 48 Element foundElement; | |
| 49 for (Element element in compiler.enqueuer.codegen.generatedCode.keys) { | |
| 50 if (element.toString() == name) { | |
| 51 if (foundElement != null) { | |
| 52 Expect.fail('Multiple compiled elements are called $name'); | |
| 53 } | |
| 54 foundElement = element; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 if (foundElement == null) { | |
| 59 Expect.fail('There is no compiled element called $name'); | |
| 60 } | |
| 61 | |
| 62 js.Node ast = compiler.enqueuer.codegen.generatedCode[foundElement]; | |
| 63 return js.prettyPrint(ast, compiler); | |
| 64 } | |
| 65 | |
| 66 runTests(List<TestEntry> tests) { | |
| 67 for (TestEntry test in tests) { | |
| 68 Map files = {TEST_MAIN_FILE: test.source}; | |
| 69 asyncTest(() { | |
| 70 CompilerImpl compiler = compilerFor( | |
| 71 memorySourceFiles: files, options: <String>['--use-cps-ir']); | |
| 72 ir.FunctionDefinition irNodeForMain; | |
| 73 | |
| 74 void cacheIrNodeForMain(Element function, ir.FunctionDefinition irNode) { | |
| 75 if (function == compiler.mainFunction) { | |
| 76 assert(irNodeForMain == null); | |
| 77 irNodeForMain = irNode; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 Uri uri = Uri.parse('memory:$TEST_MAIN_FILE'); | |
| 82 JavaScriptBackend backend = compiler.backend; | |
| 83 var functionCompiler = backend.functionCompiler; | |
| 84 functionCompiler.cpsBuilderTask.builderCallback = cacheIrNodeForMain; | |
| 85 | |
| 86 return compiler.run(uri).then((bool success) { | |
| 87 Expect.isTrue(success); | |
| 88 | |
| 89 IrSourceInformationVisitor irVisitor = new IrSourceInformationVisitor(); | |
| 90 irNodeForMain.accept(irVisitor); | |
| 91 | |
| 92 js.Node jsNode = getJsNodeForElement(compiler, compiler.mainFunction); | |
| 93 JsSourceInformationVisitor jsVisitor = new JsSourceInformationVisitor(); | |
| 94 jsNode.accept(jsVisitor); | |
| 95 | |
| 96 List<String> expectation = test.expectation; | |
| 97 // Visiting of CPS is in structural order so we check for set equality. | |
| 98 Expect.setEquals(expectation, irVisitor.sourceInformation, | |
| 99 'Unexpected IR source information. ' | |
| 100 'Expected:\n$expectation\n' | |
| 101 'but found\n${irVisitor.sourceInformation}\n' | |
| 102 'in\n${test.source}' | |
| 103 'CPS:\n${irNodeForMain.accept(new ir.SExpressionStringifier())}'); | |
| 104 Expect.listEquals(expectation, jsVisitor.sourceInformation, | |
| 105 'Unexpected JS source information. ' | |
| 106 'Expected:\n$expectation\n' | |
| 107 'but found\n${jsVisitor.sourceInformation}\n' | |
| 108 'in\n${test.source}'); | |
| 109 }).catchError((e) { | |
| 110 print(e); | |
| 111 Expect.fail('The following test failed to compile:\n' | |
| 112 '${formatTest(files)}'); | |
| 113 }); | |
| 114 }); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 class JsSourceInformationVisitor extends js.BaseVisitor { | |
| 119 List<String> sourceInformation = <String>[]; | |
| 120 | |
| 121 @override | |
| 122 visitCall(js.Call node) { | |
| 123 sourceInformation.add('${node.sourceInformation}'); | |
| 124 super.visitCall(node); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 class IrSourceInformationVisitor extends ir.TrampolineRecursiveVisitor { | |
| 129 List<String> sourceInformation = <String>[]; | |
| 130 | |
| 131 @override | |
| 132 processInvokeStatic(ir.InvokeStatic node) { | |
| 133 sourceInformation.add('${node.sourceInformation}'); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 const List<TestEntry> tests = const [ | |
| 138 const TestEntry(""" | |
| 139 main() { print('Hello World'); } | |
| 140 """, const ['memory:test.dart:[1,10]']), | |
| 141 const TestEntry(""" | |
| 142 main() { | |
| 143 print('Hello'); | |
| 144 print('World'); | |
| 145 } | |
| 146 """, const ['memory:test.dart:[2,3]', | |
| 147 'memory:test.dart:[3,3]']), | |
| 148 ]; | |
| 149 | |
| 150 void main() { | |
| 151 runTests(tests); | |
| 152 } | |
| OLD | NEW |