| 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 import 'dart:async'; | 4 import 'dart:async'; |
| 5 import 'dart:convert'; | 5 import 'dart:convert'; |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 | 7 |
| 8 import 'package:front_end/src/base/instrumentation.dart'; | 8 import 'package:front_end/src/base/instrumentation.dart'; |
| 9 import 'package:front_end/src/fasta/messages.dart'; | 9 import 'package:front_end/src/fasta/messages.dart'; |
| 10 import 'package:front_end/src/fasta/scanner.dart'; | 10 import 'package:front_end/src/fasta/scanner.dart'; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 new _Fix( | 71 new _Fix( |
| 72 expectation.commentOffset, expectation.commentLength, '')); | 72 expectation.commentOffset, expectation.commentLength, '')); |
| 73 } | 73 } |
| 74 }); | 74 }); |
| 75 }); | 75 }); |
| 76 } | 76 } |
| 77 | 77 |
| 78 /// Updates the source file at [uri] based on the actual property/value | 78 /// Updates the source file at [uri] based on the actual property/value |
| 79 /// pairs that were observed. | 79 /// pairs that were observed. |
| 80 Future<Null> fixSource(Uri uri, bool offsetsCountCharacters) async { | 80 Future<Null> fixSource(Uri uri, bool offsetsCountCharacters) async { |
| 81 uri = Uri.base.resolveUri(uri); |
| 81 var fixes = _fixes[uri]; | 82 var fixes = _fixes[uri]; |
| 82 if (fixes == null) return; | 83 if (fixes == null) return; |
| 83 File file = new File.fromUri(uri); | 84 File file = new File.fromUri(uri); |
| 84 var bytes = (await file.readAsBytes()).toList(); | 85 var bytes = (await file.readAsBytes()).toList(); |
| 85 int convertOffset(int offset) { | 86 int convertOffset(int offset) { |
| 86 if (offsetsCountCharacters) { | 87 if (offsetsCountCharacters) { |
| 87 return UTF8.encode(UTF8.decode(bytes).substring(0, offset)).length; | 88 return UTF8.encode(UTF8.decode(bytes).substring(0, offset)).length; |
| 88 } else { | 89 } else { |
| 89 return offset; | 90 return offset; |
| 90 } | 91 } |
| 91 } | 92 } |
| 92 | 93 |
| 93 // Apply the fixes in reverse order so that offsets don't need to be | 94 // Apply the fixes in reverse order so that offsets don't need to be |
| 94 // adjusted after each fix. | 95 // adjusted after each fix. |
| 95 fixes.sort((a, b) => b.offset.compareTo(a.offset)); | 96 fixes.sort((a, b) => b.offset.compareTo(a.offset)); |
| 96 for (var fix in fixes) { | 97 for (var fix in fixes) { |
| 97 bytes.replaceRange(convertOffset(fix.offset), | 98 bytes.replaceRange(convertOffset(fix.offset), |
| 98 convertOffset(fix.offset + fix.length), UTF8.encode(fix.replacement)); | 99 convertOffset(fix.offset + fix.length), UTF8.encode(fix.replacement)); |
| 99 } | 100 } |
| 100 await file.writeAsBytes(bytes); | 101 await file.writeAsBytes(bytes); |
| 101 } | 102 } |
| 102 | 103 |
| 103 /// Loads expectations from the source file located at [uri]. | 104 /// Loads expectations from the source file located at [uri]. |
| 104 /// | 105 /// |
| 105 /// Should be called before [finish]. | 106 /// Should be called before [finish]. |
| 106 Future<Null> loadExpectations(Uri uri) async { | 107 Future<Null> loadExpectations(Uri uri) async { |
| 108 uri = Uri.base.resolveUri(uri); |
| 107 var bytes = await readBytesFromFile(uri); | 109 var bytes = await readBytesFromFile(uri); |
| 108 var expectations = _unsatisfiedExpectations.putIfAbsent(uri, () => {}); | 110 var expectations = _unsatisfiedExpectations.putIfAbsent(uri, () => {}); |
| 109 var testedFeaturesState = _testedFeaturesState.putIfAbsent(uri, () => {}); | 111 var testedFeaturesState = _testedFeaturesState.putIfAbsent(uri, () => {}); |
| 110 ScannerResult result = scan(bytes, includeComments: true); | 112 ScannerResult result = scan(bytes, includeComments: true); |
| 111 for (Token token = result.tokens; !token.isEof; token = token.next) { | 113 for (Token token = result.tokens; !token.isEof; token = token.next) { |
| 112 for (analyzer.Token commentToken = token.precedingComments; | 114 for (analyzer.Token commentToken = token.precedingComments; |
| 113 commentToken != null; | 115 commentToken != null; |
| 114 commentToken = commentToken.next) { | 116 commentToken = commentToken.next) { |
| 115 String lexeme = commentToken.lexeme; | 117 String lexeme = commentToken.lexeme; |
| 116 if (lexeme.startsWith('/*@') && lexeme.endsWith('*/')) { | 118 if (lexeme.startsWith('/*@') && lexeme.endsWith('*/')) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 146 property, value, commentToken.offset, commentToken.length)); | 148 property, value, commentToken.offset, commentToken.length)); |
| 147 } | 149 } |
| 148 } | 150 } |
| 149 } | 151 } |
| 150 } | 152 } |
| 151 } | 153 } |
| 152 | 154 |
| 153 @override | 155 @override |
| 154 void record( | 156 void record( |
| 155 Uri uri, int offset, String property, InstrumentationValue value) { | 157 Uri uri, int offset, String property, InstrumentationValue value) { |
| 158 uri = Uri.base.resolveUri(uri); |
| 156 var expectationsForUri = _unsatisfiedExpectations[uri]; | 159 var expectationsForUri = _unsatisfiedExpectations[uri]; |
| 157 if (expectationsForUri == null) return; | 160 if (expectationsForUri == null) return; |
| 158 var expectationsAtOffset = expectationsForUri[offset]; | 161 var expectationsAtOffset = expectationsForUri[offset]; |
| 159 if (expectationsAtOffset != null) { | 162 if (expectationsAtOffset != null) { |
| 160 for (int i = 0; i < expectationsAtOffset.length; i++) { | 163 for (int i = 0; i < expectationsAtOffset.length; i++) { |
| 161 var expectation = expectationsAtOffset[i]; | 164 var expectation = expectationsAtOffset[i]; |
| 162 if (expectation.property == property) { | 165 if (expectation.property == property) { |
| 163 if (!value.matches(expectation.value)) { | 166 if (!value.matches(expectation.value)) { |
| 164 _problemWithStack( | 167 _problemWithStack( |
| 165 uri, | 168 uri, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 this.property, this.value, this.commentOffset, this.commentLength); | 238 this.property, this.value, this.commentOffset, this.commentLength); |
| 236 } | 239 } |
| 237 | 240 |
| 238 class _Fix { | 241 class _Fix { |
| 239 final int offset; | 242 final int offset; |
| 240 final int length; | 243 final int length; |
| 241 final String replacement; | 244 final String replacement; |
| 242 | 245 |
| 243 _Fix(this.offset, this.length, this.replacement); | 246 _Fix(this.offset, this.length, this.replacement); |
| 244 } | 247 } |
| OLD | NEW |