| 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 'dart:io'; | 6 import 'dart:io'; |
| 7 | 7 |
| 8 import 'package:compiler/src/common.dart'; | 8 import 'package:compiler/src/common.dart'; |
| 9 import 'package:compiler/src/common_elements.dart'; | 9 import 'package:compiler/src/common_elements.dart'; |
| 10 import 'package:compiler/src/compiler.dart'; | 10 import 'package:compiler/src/compiler.dart'; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 /// Actual data is computed using [computeMemberData] and [code] is compiled | 56 /// Actual data is computed using [computeMemberData] and [code] is compiled |
| 57 /// using [compileFunction]. | 57 /// using [compileFunction]. |
| 58 Future<IdData> computeData( | 58 Future<IdData> computeData( |
| 59 String annotatedCode, | 59 String annotatedCode, |
| 60 ComputeMemberDataFunction computeMemberData, | 60 ComputeMemberDataFunction computeMemberData, |
| 61 CompileFunction compileFunction, | 61 CompileFunction compileFunction, |
| 62 {List<String> options: const <String>[], | 62 {List<String> options: const <String>[], |
| 63 bool verbose: false}) async { | 63 bool verbose: false}) async { |
| 64 AnnotatedCode code = | 64 AnnotatedCode code = |
| 65 new AnnotatedCode.fromText(annotatedCode, commentStart, commentEnd); | 65 new AnnotatedCode.fromText(annotatedCode, commentStart, commentEnd); |
| 66 Map<Id, String> expectedMap = computeExpectedMap(code); | 66 Map<Id, IdValue> expectedMap = computeExpectedMap(code); |
| 67 Map<Id, ActualData> actualMap = <Id, ActualData>{}; | 67 Map<Id, ActualData> actualMap = <Id, ActualData>{}; |
| 68 Uri mainUri = Uri.parse('memory:main.dart'); | 68 Uri mainUri = Uri.parse('memory:main.dart'); |
| 69 Compiler compiler = await compileFunction(code, mainUri, options); | 69 Compiler compiler = await compileFunction(code, mainUri, options); |
| 70 ElementEnvironment elementEnvironment = | 70 ElementEnvironment elementEnvironment = |
| 71 compiler.backendClosedWorldForTesting.elementEnvironment; | 71 compiler.backendClosedWorldForTesting.elementEnvironment; |
| 72 LibraryEntity mainLibrary = elementEnvironment.mainLibrary; | 72 LibraryEntity mainLibrary = elementEnvironment.mainLibrary; |
| 73 elementEnvironment.forEachClass(mainLibrary, (ClassEntity cls) { | 73 elementEnvironment.forEachClass(mainLibrary, (ClassEntity cls) { |
| 74 elementEnvironment.forEachClassMember(cls, | 74 elementEnvironment.forEachClassMember(cls, |
| 75 (ClassEntity declarer, MemberEntity member) { | 75 (ClassEntity declarer, MemberEntity member) { |
| 76 if (cls == declarer) { | 76 if (cls == declarer) { |
| 77 computeMemberData(compiler, member, actualMap, verbose: verbose); | 77 computeMemberData(compiler, member, actualMap, verbose: verbose); |
| 78 } | 78 } |
| 79 }); | 79 }); |
| 80 }); | 80 }); |
| 81 elementEnvironment.forEachLibraryMember(mainLibrary, (MemberEntity member) { | 81 elementEnvironment.forEachLibraryMember(mainLibrary, (MemberEntity member) { |
| 82 computeMemberData(compiler, member, actualMap, verbose: verbose); | 82 computeMemberData(compiler, member, actualMap, verbose: verbose); |
| 83 }); | 83 }); |
| 84 return new IdData( | 84 return new IdData( |
| 85 code, compiler, elementEnvironment, mainUri, expectedMap, actualMap); | 85 code, compiler, elementEnvironment, mainUri, expectedMap, actualMap); |
| 86 } | 86 } |
| 87 | 87 |
| 88 /// Data collected by [computeData]. | 88 /// Data collected by [computeData]. |
| 89 class IdData { | 89 class IdData { |
| 90 final AnnotatedCode code; | 90 final AnnotatedCode code; |
| 91 final Compiler compiler; | 91 final Compiler compiler; |
| 92 final ElementEnvironment elementEnvironment; | 92 final ElementEnvironment elementEnvironment; |
| 93 final Uri mainUri; | 93 final Uri mainUri; |
| 94 final Map<Id, String> expectedMap; | 94 final Map<Id, IdValue> expectedMap; |
| 95 final Map<Id, ActualData> actualMap; | 95 final Map<Id, ActualData> actualMap; |
| 96 | 96 |
| 97 IdData(this.code, this.compiler, this.elementEnvironment, this.mainUri, | 97 IdData(this.code, this.compiler, this.elementEnvironment, this.mainUri, |
| 98 this.expectedMap, this.actualMap); | 98 this.expectedMap, this.actualMap); |
| 99 | 99 |
| 100 String withAnnotations(Map<int, String> annotations) { | 100 String withAnnotations(Map<int, List<String>> annotations) { |
| 101 StringBuffer sb = new StringBuffer(); | 101 StringBuffer sb = new StringBuffer(); |
| 102 int end = 0; | 102 int end = 0; |
| 103 for (int offset in annotations.keys.toList()..sort()) { | 103 for (int offset in annotations.keys.toList()..sort()) { |
| 104 if (offset > end) { | 104 if (offset > end) { |
| 105 sb.write(code.sourceCode.substring(end, offset)); | 105 sb.write(code.sourceCode.substring(end, offset)); |
| 106 } | 106 } |
| 107 sb.write('/* '); | 107 for (String annotation in annotations[offset]) { |
| 108 sb.write(annotations[offset]); | 108 sb.write('/* '); |
| 109 sb.write(' */'); | 109 sb.write(annotation); |
| 110 sb.write(' */'); |
| 111 } |
| 110 end = offset; | 112 end = offset; |
| 111 } | 113 } |
| 112 if (end < code.sourceCode.length) { | 114 if (end < code.sourceCode.length) { |
| 113 sb.write(code.sourceCode.substring(end)); | 115 sb.write(code.sourceCode.substring(end)); |
| 114 } | 116 } |
| 115 return sb.toString(); | 117 return sb.toString(); |
| 116 } | 118 } |
| 117 | 119 |
| 118 String get actualCode { | 120 String get actualCode { |
| 119 Map<int, String> annotations = <int, String>{}; | 121 Map<int, List<String>> annotations = <int, List<String>>{}; |
| 120 actualMap.forEach((Id id, ActualData data) { | 122 actualMap.forEach((Id id, ActualData data) { |
| 121 annotations[data.sourceSpan.begin] = data.value; | 123 annotations |
| 124 .putIfAbsent(data.sourceSpan.begin, () => []) |
| 125 .add('${data.value}'); |
| 122 }); | 126 }); |
| 123 return withAnnotations(annotations); | 127 return withAnnotations(annotations); |
| 124 } | 128 } |
| 125 | 129 |
| 126 String get diffCode { | 130 String get diffCode { |
| 127 Map<int, String> annotations = <int, String>{}; | 131 Map<int, List<String>> annotations = <int, List<String>>{}; |
| 128 actualMap.forEach((Id id, ActualData data) { | 132 actualMap.forEach((Id id, ActualData data) { |
| 129 String expected = expectedMap[id] ?? ''; | 133 String expected = expectedMap[id]?.value ?? ''; |
| 130 if (data.value != expected) { | 134 if (data.value != expected) { |
| 131 annotations[data.sourceSpan.begin] = '${expected} | ${data.value}'; | 135 annotations |
| 136 .putIfAbsent(data.sourceSpan.begin, () => []) |
| 137 .add('${expected} | ${data.value}'); |
| 132 } | 138 } |
| 133 }); | 139 }); |
| 134 expectedMap.forEach((Id id, String expected) { | 140 expectedMap.forEach((Id id, IdValue expected) { |
| 135 if (!actualMap.containsKey(id)) { | 141 if (!actualMap.containsKey(id)) { |
| 136 int offset = compiler.reporter | 142 int offset = compiler.reporter |
| 137 .spanFromSpannable( | 143 .spanFromSpannable( |
| 138 computeSpannable(elementEnvironment, mainUri, id)) | 144 computeSpannable(elementEnvironment, mainUri, id)) |
| 139 .begin; | 145 .begin; |
| 140 annotations[offset] = '${expected} | ---'; | 146 annotations.putIfAbsent(offset, () => []).add('${expected} | ---'); |
| 141 } | 147 } |
| 142 }); | 148 }); |
| 143 return withAnnotations(annotations); | 149 return withAnnotations(annotations); |
| 144 } | 150 } |
| 145 | 151 |
| 146 String computeDiffCodeFor(IdData other) { | 152 String computeDiffCodeFor(IdData other) { |
| 147 Map<int, String> annotations = <int, String>{}; | 153 Map<int, List<String>> annotations = <int, List<String>>{}; |
| 148 actualMap.forEach((Id id, ActualData data1) { | 154 actualMap.forEach((Id id, ActualData data1) { |
| 149 ActualData data2 = other.actualMap[id]; | 155 ActualData data2 = other.actualMap[id]; |
| 150 if (data1.value != data2?.value) { | 156 if (data1.value != data2?.value) { |
| 151 annotations[data1.sourceSpan.begin] = | 157 annotations |
| 152 '${data1.value} | ${data2?.value ?? '---'}'; | 158 .putIfAbsent(data1.sourceSpan.begin, () => []) |
| 159 .add('${data1.value} | ${data2?.value ?? '---'}'); |
| 153 } | 160 } |
| 154 }); | 161 }); |
| 155 other.actualMap.forEach((Id id, ActualData data2) { | 162 other.actualMap.forEach((Id id, ActualData data2) { |
| 156 if (!actualMap.containsKey(id)) { | 163 if (!actualMap.containsKey(id)) { |
| 157 int offset = compiler.reporter | 164 int offset = compiler.reporter |
| 158 .spanFromSpannable( | 165 .spanFromSpannable( |
| 159 computeSpannable(elementEnvironment, mainUri, id)) | 166 computeSpannable(elementEnvironment, mainUri, id)) |
| 160 .begin; | 167 .begin; |
| 161 annotations[offset] = '--- | ${data2.value}'; | 168 annotations.putIfAbsent(offset, () => []).add('--- | ${data2.value}'); |
| 162 } | 169 } |
| 163 }); | 170 }); |
| 164 return withAnnotations(annotations); | 171 return withAnnotations(annotations); |
| 165 } | 172 } |
| 166 } | 173 } |
| 167 | 174 |
| 168 /// Check code for all test files int [data] using [computeFromAst] and | 175 /// Check code for all test files int [data] using [computeFromAst] and |
| 169 /// [computeFromKernel] from the respective front ends. If [skipForKernel] | 176 /// [computeFromKernel] from the respective front ends. If [skipForKernel] |
| 170 /// contains the name of the test file it isn't tested for kernel. | 177 /// contains the name of the test file it isn't tested for kernel. |
| 171 Future checkTests(Directory dataDir, ComputeMemberDataFunction computeFromAst, | 178 Future checkTests(Directory dataDir, ComputeMemberDataFunction computeFromAst, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 202 String annotatedCode, | 209 String annotatedCode, |
| 203 ComputeMemberDataFunction computeMemberData, | 210 ComputeMemberDataFunction computeMemberData, |
| 204 CompileFunction compileFunction, | 211 CompileFunction compileFunction, |
| 205 {List<String> options: const <String>[], | 212 {List<String> options: const <String>[], |
| 206 bool verbose: false}) async { | 213 bool verbose: false}) async { |
| 207 IdData data = await computeData( | 214 IdData data = await computeData( |
| 208 annotatedCode, computeMemberData, compileFunction, | 215 annotatedCode, computeMemberData, compileFunction, |
| 209 options: options, verbose: verbose); | 216 options: options, verbose: verbose); |
| 210 | 217 |
| 211 data.actualMap.forEach((Id id, ActualData actualData) { | 218 data.actualMap.forEach((Id id, ActualData actualData) { |
| 212 String actual = actualData.value; | 219 IdValue actual = actualData.value; |
| 213 if (!data.expectedMap.containsKey(id)) { | 220 if (!data.expectedMap.containsKey(id)) { |
| 214 if (actual != '') { | 221 if (actual.value != '') { |
| 215 reportHere( | 222 reportHere( |
| 216 data.compiler.reporter, | 223 data.compiler.reporter, |
| 217 actualData.sourceSpan, | 224 actualData.sourceSpan, |
| 218 'Id $id = ${actual} for ${actualData.object} ' | 225 'Id $id = ${actual} for ${actualData.object} ' |
| 219 '(${actualData.object.runtimeType}) ' | 226 '(${actualData.object.runtimeType}) ' |
| 220 'not expected in ${data.expectedMap.keys}'); | 227 'not expected in ${data.expectedMap.keys}'); |
| 221 print('--annotations diff--------------------------------------------'); | 228 print('--annotations diff--------------------------------------------'); |
| 222 print(data.diffCode); | 229 print(data.diffCode); |
| 223 print('--------------------------------------------------------------'); | 230 print('--------------------------------------------------------------'); |
| 224 } | 231 } |
| 225 Expect.equals('', actual); | 232 Expect.equals('', actual.value); |
| 226 } else { | 233 } else { |
| 227 String expected = data.expectedMap[id]; | 234 IdValue expected = data.expectedMap[id]; |
| 228 if (actual != expected) { | 235 if (actual != expected) { |
| 229 reportHere( | 236 reportHere( |
| 230 data.compiler.reporter, | 237 data.compiler.reporter, |
| 231 actualData.sourceSpan, | 238 actualData.sourceSpan, |
| 232 'Object: ${actualData.object} (${actualData.object.runtimeType}), ' | 239 'Object: ${actualData.object} (${actualData.object.runtimeType}), ' |
| 233 'expected: ${expected}, actual: ${actual}'); | 240 'expected: ${expected}, actual: ${actual}'); |
| 234 print('--annotations diff--------------------------------------------'); | 241 print('--annotations diff--------------------------------------------'); |
| 235 print(data.diffCode); | 242 print(data.diffCode); |
| 236 print('--------------------------------------------------------------'); | 243 print('--------------------------------------------------------------'); |
| 237 } | 244 } |
| 238 Expect.equals(expected, actual); | 245 Expect.equals(expected, actual); |
| 239 } | 246 } |
| 240 }); | 247 }); |
| 241 | 248 |
| 242 Set<Id> missingIds = new Set<Id>(); | 249 Set<Id> missingIds = new Set<Id>(); |
| 243 data.expectedMap.forEach((Id id, String expected) { | 250 data.expectedMap.forEach((Id id, IdValue expected) { |
| 244 if (!data.actualMap.containsKey(id)) { | 251 if (!data.actualMap.containsKey(id)) { |
| 245 missingIds.add(id); | 252 missingIds.add(id); |
| 246 reportHere( | 253 reportHere( |
| 247 data.compiler.reporter, | 254 data.compiler.reporter, |
| 248 computeSpannable(data.elementEnvironment, data.mainUri, id), | 255 computeSpannable(data.elementEnvironment, data.mainUri, id), |
| 249 'Expected $expected for id $id missing in ${data.actualMap.keys}'); | 256 'Expected $expected for id $id missing in ${data.actualMap.keys}'); |
| 250 } | 257 } |
| 251 }); | 258 }); |
| 252 Expect.isTrue(missingIds.isEmpty, "Ids not found: ${missingIds}."); | 259 Expect.isTrue(missingIds.isEmpty, "Ids not found: ${missingIds}."); |
| 253 } | 260 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 264 elementEnvironment.lookupClass(library, id.className, required: true); | 271 elementEnvironment.lookupClass(library, id.className, required: true); |
| 265 return elementEnvironment.lookupClassMember(cls, id.memberName); | 272 return elementEnvironment.lookupClassMember(cls, id.memberName); |
| 266 } else { | 273 } else { |
| 267 return elementEnvironment.lookupLibraryMember(library, id.memberName); | 274 return elementEnvironment.lookupLibraryMember(library, id.memberName); |
| 268 } | 275 } |
| 269 } | 276 } |
| 270 throw new UnsupportedError('Unsupported id $id.'); | 277 throw new UnsupportedError('Unsupported id $id.'); |
| 271 } | 278 } |
| 272 | 279 |
| 273 /// Compute the expectancy map from [code]. | 280 /// Compute the expectancy map from [code]. |
| 274 Map<Id, String> computeExpectedMap(AnnotatedCode code) { | 281 Map<Id, IdValue> computeExpectedMap(AnnotatedCode code) { |
| 275 Map<Id, String> map = <Id, String>{}; | 282 Map<Id, IdValue> map = <Id, IdValue>{}; |
| 276 for (Annotation annotation in code.annotations) { | 283 for (Annotation annotation in code.annotations) { |
| 277 String text = annotation.text; | 284 IdValue idValue = IdValue.decode(annotation.offset, annotation.text); |
| 278 int colonPos = text.indexOf(':'); | 285 map[idValue.id] = idValue; |
| 279 Id id; | |
| 280 String expected; | |
| 281 if (colonPos == -1) { | |
| 282 id = new NodeId(annotation.offset); | |
| 283 expected = text; | |
| 284 } else { | |
| 285 id = new ElementId(text.substring(0, colonPos)); | |
| 286 expected = text.substring(colonPos + 1); | |
| 287 } | |
| 288 map[id] = expected; | |
| 289 } | 286 } |
| 290 return map; | 287 return map; |
| 291 } | 288 } |
| OLD | NEW |