77446204da
This reverts commite48f35b43d. Reason for revert: linter is fixed. Original change's description: > Revert "[analyzer] Move 4 more HintCodes to be WarningCodes, UNUSED_*" > > This reverts commit0ef7ed2ebd. > > Reason for revert: Tests are failing in google3 - linter needs to be bumped > > Original change's description: > > [analyzer] Move 4 more HintCodes to be WarningCodes, UNUSED_* > > > > Bug: https://github.com/dart-lang/sdk/issues/50796 > > Change-Id: Ib5b153bc6e64bc433df1f05c53d82f71b470bbec > > TEST=presubmit bots > > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290703 > > Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> > > Reviewed-by: Alexander Aprelev <aam@google.com> > > Commit-Queue: Samuel Rawlins <srawlins@google.com> > > Reviewed-by: Nate Bosch <nbosch@google.com> > > Bug: https://github.com/dart-lang/sdk/issues/50796 > Change-Id: If1c460bdcf422033648417da5ba2f5fbc1b459c2 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/291460 > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> > Auto-Submit: Oleh Prypin <oprypin@google.com> > Reviewed-by: Alexander Thomas <athom@google.com> > Commit-Queue: Alexander Thomas <athom@google.com> > Commit-Queue: Oleh Prypin <oprypin@google.com> Bug: https://github.com/dart-lang/sdk/issues/50796 Change-Id: Ie503b57a19df1a50cb3dfe50c840d20779310e87 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/291560 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Oleh Prypin <oprypin@google.com> Reviewed-by: Nate Bosch <nbosch@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com> Reviewed-by: Alexander Aprelev <aam@google.com> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
128 lines
3.7 KiB
Dart
128 lines
3.7 KiB
Dart
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'package:analyzer/dart/ast/ast.dart';
|
|
import 'package:analyzer/dart/element/element.dart';
|
|
import 'package:analyzer/error/error.dart';
|
|
import 'package:analyzer/src/dart/ast/element_locator.dart';
|
|
import 'package:analyzer/src/dart/ast/utilities.dart';
|
|
import 'package:analyzer/src/error/codes.dart';
|
|
import 'package:analyzer/src/test_utilities/find_element.dart';
|
|
import 'package:analyzer/src/test_utilities/find_node.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
import 'abstract_context.dart';
|
|
|
|
class AbstractSingleUnitTest extends AbstractContextTest {
|
|
bool verifyNoTestUnitErrors = true;
|
|
|
|
late String testCode;
|
|
late String testFile;
|
|
late CompilationUnit testUnit;
|
|
late FindNode findNode;
|
|
late FindElement findElement;
|
|
|
|
void addTestSource(String code) {
|
|
testCode = code;
|
|
addSource(testFile, code);
|
|
}
|
|
|
|
int findEnd(String search) {
|
|
return findOffset(search) + search.length;
|
|
}
|
|
|
|
/// Returns the [SimpleIdentifier] at the given search pattern.
|
|
SimpleIdentifier findIdentifier(String search) {
|
|
return findNodeAtString(search, (node) => node is SimpleIdentifier)
|
|
as SimpleIdentifier;
|
|
}
|
|
|
|
AstNode? findNodeAtOffset(
|
|
int offset, [
|
|
bool Function(AstNode)? predicate,
|
|
]) {
|
|
var result = NodeLocator(offset).searchWithin(testUnit);
|
|
if (result != null && predicate != null) {
|
|
result = result.thisOrAncestorMatching(predicate);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
AstNode? findNodeAtString(
|
|
String search, [
|
|
bool Function(AstNode)? predicate,
|
|
]) {
|
|
var offset = findOffset(search);
|
|
return findNodeAtOffset(offset, predicate);
|
|
}
|
|
|
|
Element? findNodeElementAtString(
|
|
String search, [
|
|
bool Function(AstNode)? predicate,
|
|
]) {
|
|
var node = findNodeAtString(search, predicate);
|
|
if (node == null) {
|
|
return null;
|
|
}
|
|
return ElementLocator.locate(node);
|
|
}
|
|
|
|
int findOffset(String search) {
|
|
var offset = testCode.indexOf(search);
|
|
expect(offset, isNonNegative, reason: "Not found '$search' in\n$testCode");
|
|
return offset;
|
|
}
|
|
|
|
int getLeadingIdentifierLength(String search) {
|
|
var length = 0;
|
|
while (length < search.length) {
|
|
var c = search.codeUnitAt(length);
|
|
if (c >= 'a'.codeUnitAt(0) && c <= 'z'.codeUnitAt(0)) {
|
|
length++;
|
|
continue;
|
|
}
|
|
if (c >= 'A'.codeUnitAt(0) && c <= 'Z'.codeUnitAt(0)) {
|
|
length++;
|
|
continue;
|
|
}
|
|
if (c >= '0'.codeUnitAt(0) && c <= '9'.codeUnitAt(0)) {
|
|
length++;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
return length;
|
|
}
|
|
|
|
Future<void> resolveTestCode(String code) async {
|
|
addTestSource(code);
|
|
await resolveTestFile();
|
|
}
|
|
|
|
Future<void> resolveTestFile() async {
|
|
var result = await resolveFile(testFile);
|
|
testCode = result.content;
|
|
testUnit = result.unit;
|
|
if (verifyNoTestUnitErrors) {
|
|
expect(result.errors.where((AnalysisError error) {
|
|
return error.errorCode != WarningCode.DEAD_CODE &&
|
|
error.errorCode != WarningCode.UNUSED_CATCH_CLAUSE &&
|
|
error.errorCode != WarningCode.UNUSED_CATCH_STACK &&
|
|
error.errorCode != WarningCode.UNUSED_ELEMENT &&
|
|
error.errorCode != WarningCode.UNUSED_FIELD &&
|
|
error.errorCode != WarningCode.UNUSED_IMPORT &&
|
|
error.errorCode != WarningCode.UNUSED_LOCAL_VARIABLE;
|
|
}), isEmpty);
|
|
}
|
|
findNode = FindNode(testCode, testUnit);
|
|
findElement = FindElement(testUnit);
|
|
}
|
|
|
|
@override
|
|
void setUp() {
|
|
super.setUp();
|
|
testFile = convertPath('$testPackageRootPath/test.dart');
|
|
}
|
|
}
|