| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'package:compiler/src/commandline_options.dart'; | 6 import 'package:compiler/src/commandline_options.dart'; |
| 7 import 'package:compiler/src/common.dart'; | 7 import 'package:compiler/src/common.dart'; |
| 8 import 'package:compiler/src/common_elements.dart'; | 8 import 'package:compiler/src/common_elements.dart'; |
| 9 import 'package:compiler/src/compiler.dart'; | 9 import 'package:compiler/src/compiler.dart'; |
| 10 import 'package:compiler/src/elements/elements.dart'; | 10 import 'package:compiler/src/elements/elements.dart'; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 /// Function that compiles [code] with [options] and returns the [Compiler] obje
ct. | 22 /// Function that compiles [code] with [options] and returns the [Compiler] obje
ct. |
| 23 typedef Future<Compiler> CompileFunction( | 23 typedef Future<Compiler> CompileFunction( |
| 24 AnnotatedCode code, Uri mainUri, List<String> options); | 24 AnnotatedCode code, Uri mainUri, List<String> options); |
| 25 | 25 |
| 26 /// Function that computes a data mapping for [member]. | 26 /// Function that computes a data mapping for [member]. |
| 27 /// | 27 /// |
| 28 /// Fills [actualMap] with the data and [sourceSpanMap] with the source spans | 28 /// Fills [actualMap] with the data and [sourceSpanMap] with the source spans |
| 29 /// for the data origin. | 29 /// for the data origin. |
| 30 typedef void ComputeMemberDataFunction(Compiler compiler, MemberEntity member, | 30 typedef void ComputeMemberDataFunction(Compiler compiler, MemberEntity member, |
| 31 Map<Id, String> actualMap, Map<Id, SourceSpan> sourceSpanMap); | 31 Map<Id, String> actualMap, Map<Id, SourceSpan> sourceSpanMap, |
| 32 {bool verbose}); |
| 32 | 33 |
| 33 /// Compile [code] from .dart sources. | 34 /// Compile [code] from .dart sources. |
| 34 Future<Compiler> compileFromSource( | 35 Future<Compiler> compileFromSource( |
| 35 AnnotatedCode code, Uri mainUri, List<String> options) async { | 36 AnnotatedCode code, Uri mainUri, List<String> options) async { |
| 36 Compiler compiler = compilerFor( | 37 Compiler compiler = compilerFor( |
| 37 memorySourceFiles: {'main.dart': code.sourceCode}, options: options); | 38 memorySourceFiles: {'main.dart': code.sourceCode}, options: options); |
| 38 compiler.stopAfterTypeInference = true; | 39 compiler.stopAfterTypeInference = true; |
| 39 await compiler.run(mainUri); | 40 await compiler.run(mainUri); |
| 40 return compiler; | 41 return compiler; |
| 41 } | 42 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 54 } | 55 } |
| 55 | 56 |
| 56 /// Compute expected and actual data for all members defined in [annotatedCode]. | 57 /// Compute expected and actual data for all members defined in [annotatedCode]. |
| 57 /// | 58 /// |
| 58 /// Actual data is computed using [computeMemberData] and [code] is compiled | 59 /// Actual data is computed using [computeMemberData] and [code] is compiled |
| 59 /// using [compileFunction]. | 60 /// using [compileFunction]. |
| 60 Future<IdData> computeData( | 61 Future<IdData> computeData( |
| 61 String annotatedCode, | 62 String annotatedCode, |
| 62 ComputeMemberDataFunction computeMemberData, | 63 ComputeMemberDataFunction computeMemberData, |
| 63 CompileFunction compileFunction, | 64 CompileFunction compileFunction, |
| 64 {List<String> options: const <String>[]}) async { | 65 {List<String> options: const <String>[], |
| 66 bool verbose: false}) async { |
| 65 AnnotatedCode code = | 67 AnnotatedCode code = |
| 66 new AnnotatedCode.fromText(annotatedCode, commentStart, commentEnd); | 68 new AnnotatedCode.fromText(annotatedCode, commentStart, commentEnd); |
| 67 Map<Id, String> expectedMap = computeExpectedMap(code); | 69 Map<Id, String> expectedMap = computeExpectedMap(code); |
| 68 Map<Id, String> actualMap = <Id, String>{}; | 70 Map<Id, String> actualMap = <Id, String>{}; |
| 69 Map<Id, SourceSpan> sourceSpanMap = <Id, SourceSpan>{}; | 71 Map<Id, SourceSpan> sourceSpanMap = <Id, SourceSpan>{}; |
| 70 Uri mainUri = Uri.parse('memory:main.dart'); | 72 Uri mainUri = Uri.parse('memory:main.dart'); |
| 71 Compiler compiler = await compileFunction(code, mainUri, options); | 73 Compiler compiler = await compileFunction(code, mainUri, options); |
| 72 ElementEnvironment elementEnvironment = | 74 ElementEnvironment elementEnvironment = |
| 73 compiler.backendClosedWorldForTesting.elementEnvironment; | 75 compiler.backendClosedWorldForTesting.elementEnvironment; |
| 74 LibraryEntity mainLibrary = elementEnvironment.mainLibrary; | 76 LibraryEntity mainLibrary = elementEnvironment.mainLibrary; |
| 75 elementEnvironment.forEachClass(mainLibrary, (ClassEntity cls) { | 77 elementEnvironment.forEachClass(mainLibrary, (ClassEntity cls) { |
| 76 elementEnvironment.forEachClassMember(cls, | 78 elementEnvironment.forEachClassMember(cls, |
| 77 (ClassEntity declarer, MemberEntity member) { | 79 (ClassEntity declarer, MemberEntity member) { |
| 78 if (cls == declarer) { | 80 if (cls == declarer) { |
| 79 computeMemberData(compiler, member, actualMap, sourceSpanMap); | 81 computeMemberData(compiler, member, actualMap, sourceSpanMap, |
| 82 verbose: verbose); |
| 80 } | 83 } |
| 81 }); | 84 }); |
| 82 }); | 85 }); |
| 83 elementEnvironment.forEachLibraryMember(mainLibrary, (MemberEntity member) { | 86 elementEnvironment.forEachLibraryMember(mainLibrary, (MemberEntity member) { |
| 84 computeMemberData(compiler, member, actualMap, sourceSpanMap); | 87 computeMemberData(compiler, member, actualMap, sourceSpanMap, |
| 88 verbose: verbose); |
| 85 }); | 89 }); |
| 86 return new IdData(compiler, elementEnvironment, mainUri, expectedMap, | 90 return new IdData(compiler, elementEnvironment, mainUri, expectedMap, |
| 87 actualMap, sourceSpanMap); | 91 actualMap, sourceSpanMap); |
| 88 } | 92 } |
| 89 | 93 |
| 90 /// Data collected by [computeData]. | 94 /// Data collected by [computeData]. |
| 91 class IdData { | 95 class IdData { |
| 92 final Compiler compiler; | 96 final Compiler compiler; |
| 93 final ElementEnvironment elementEnvironment; | 97 final ElementEnvironment elementEnvironment; |
| 94 final Uri mainUri; | 98 final Uri mainUri; |
| 95 final Map<Id, String> expectedMap; | 99 final Map<Id, String> expectedMap; |
| 96 final Map<Id, String> actualMap; | 100 final Map<Id, String> actualMap; |
| 97 final Map<Id, SourceSpan> sourceSpanMap; | 101 final Map<Id, SourceSpan> sourceSpanMap; |
| 98 | 102 |
| 99 IdData(this.compiler, this.elementEnvironment, this.mainUri, this.expectedMap, | 103 IdData(this.compiler, this.elementEnvironment, this.mainUri, this.expectedMap, |
| 100 this.actualMap, this.sourceSpanMap); | 104 this.actualMap, this.sourceSpanMap); |
| 101 } | 105 } |
| 102 | 106 |
| 103 /// Compiles the [annotatedCode] with the provided [options] and calls | 107 /// Compiles the [annotatedCode] with the provided [options] and calls |
| 104 /// [computeMemberData] for each member. The result is checked against the | 108 /// [computeMemberData] for each member. The result is checked against the |
| 105 /// expected data derived from [annotatedCode]. | 109 /// expected data derived from [annotatedCode]. |
| 106 Future checkCode( | 110 Future checkCode( |
| 107 String annotatedCode, | 111 String annotatedCode, |
| 108 ComputeMemberDataFunction computeMemberData, | 112 ComputeMemberDataFunction computeMemberData, |
| 109 CompileFunction compileFunction, | 113 CompileFunction compileFunction, |
| 110 {List<String> options: const <String>[]}) async { | 114 {List<String> options: const <String>[], |
| 115 bool verbose: false}) async { |
| 111 IdData data = await computeData( | 116 IdData data = await computeData( |
| 112 annotatedCode, computeMemberData, compileFunction, | 117 annotatedCode, computeMemberData, compileFunction, |
| 113 options: options); | 118 options: options, verbose: verbose); |
| 114 | 119 |
| 115 data.actualMap.forEach((Id id, String actual) { | 120 data.actualMap.forEach((Id id, String actual) { |
| 116 String expected = data.expectedMap.remove(id); | 121 if (!data.expectedMap.containsKey(id)) { |
| 117 if (actual != expected) { | 122 if (actual != '') { |
| 118 reportHere(data.compiler.reporter, data.sourceSpanMap[id], | 123 reportHere(data.compiler.reporter, data.sourceSpanMap[id], |
| 119 'expected:${expected},actual:${actual}'); | 124 'Id $id not expected in ${data.expectedMap.keys}'); |
| 125 } |
| 126 Expect.equals('', actual); |
| 127 } else { |
| 128 String expected = data.expectedMap.remove(id); |
| 129 if (actual != expected) { |
| 130 reportHere(data.compiler.reporter, data.sourceSpanMap[id], |
| 131 'expected:${expected},actual:${actual}'); |
| 132 } |
| 133 Expect.equals(expected, actual); |
| 120 } | 134 } |
| 121 Expect.equals(expected, actual); | |
| 122 }); | 135 }); |
| 123 | 136 |
| 124 data.expectedMap.forEach((Id id, String expected) { | 137 data.expectedMap.forEach((Id id, String expected) { |
| 125 reportHere( | 138 reportHere( |
| 126 data.compiler.reporter, | 139 data.compiler.reporter, |
| 127 computeSpannable(data.elementEnvironment, data.mainUri, id), | 140 computeSpannable(data.elementEnvironment, data.mainUri, id), |
| 128 'expected:${expected},actual:null'); | 141 'expected:${expected},actual:null'); |
| 129 }); | 142 }); |
| 130 Expect.isTrue( | 143 Expect.isTrue( |
| 131 data.expectedMap.isEmpty, "Ids not found: ${data.expectedMap}."); | 144 data.expectedMap.isEmpty, "Ids not found: ${data.expectedMap}."); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 | 327 |
| 315 visitVariableDeclaration(ir.VariableDeclaration node) { | 328 visitVariableDeclaration(ir.VariableDeclaration node) { |
| 316 computeForNode(node); | 329 computeForNode(node); |
| 317 super.visitVariableDeclaration(node); | 330 super.visitVariableDeclaration(node); |
| 318 } | 331 } |
| 319 | 332 |
| 320 visitFunctionDeclaration(ir.FunctionDeclaration node) { | 333 visitFunctionDeclaration(ir.FunctionDeclaration node) { |
| 321 computeForNode(node); | 334 computeForNode(node); |
| 322 super.visitFunctionDeclaration(node); | 335 super.visitFunctionDeclaration(node); |
| 323 } | 336 } |
| 337 |
| 338 visitFunctionExpression(ir.FunctionExpression node) { |
| 339 computeForNode(node); |
| 340 super.visitFunctionExpression(node); |
| 341 } |
| 324 } | 342 } |
| OLD | NEW |