mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 01:45:06 +00:00
20b560226e
Change-Id: I1c2a4f15dee607a0a6241dfaf2d764a7d284389b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/241510 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Samuel Rawlins <srawlins@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
84 lines
2.8 KiB
Dart
84 lines
2.8 KiB
Dart
// Copyright (c) 2019, 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/analysis/results.dart';
|
|
import 'package:analyzer/dart/ast/ast.dart';
|
|
import 'package:analyzer/dart/element/element.dart';
|
|
import 'package:analyzer/error/error.dart';
|
|
import 'package:analyzer/src/error/codes.dart';
|
|
import 'package:analyzer/src/generated/source.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';
|
|
|
|
/// TODO(paulberry): this logic is duplicated from other packages. Find a way
|
|
/// share it, or avoid relying on it.
|
|
class AbstractSingleUnitTest extends AbstractContextTest {
|
|
bool verifyNoTestUnitErrors = true;
|
|
|
|
String? testCode;
|
|
late String testFile;
|
|
Uri? testUri;
|
|
Source? testSource;
|
|
late ResolvedUnitResult testAnalysisResult;
|
|
CompilationUnit? testUnit;
|
|
CompilationUnitElement? testUnitElement;
|
|
LibraryElement? testLibraryElement;
|
|
late FindNode findNode;
|
|
late FindElement findElement;
|
|
|
|
void addTestSource(String code, [Uri? uri]) {
|
|
testCode = code;
|
|
testSource = addSource(testFile, code, uri);
|
|
}
|
|
|
|
Future<void> resolveTestUnit(String code) async {
|
|
addTestSource(code, testUri);
|
|
testAnalysisResult =
|
|
await session.getResolvedUnit(testFile) as ResolvedUnitResult;
|
|
testUnit = testAnalysisResult.unit;
|
|
if (verifyNoTestUnitErrors) {
|
|
expect(testAnalysisResult.errors.where((AnalysisError error) {
|
|
return error.errorCode != HintCode.DEAD_CODE &&
|
|
error.errorCode != HintCode.UNUSED_CATCH_CLAUSE &&
|
|
error.errorCode != HintCode.UNUSED_CATCH_STACK &&
|
|
error.errorCode != HintCode.UNUSED_ELEMENT &&
|
|
error.errorCode != HintCode.UNUSED_ELEMENT_PARAMETER &&
|
|
error.errorCode != HintCode.UNUSED_FIELD &&
|
|
error.errorCode != HintCode.UNUSED_IMPORT &&
|
|
error.errorCode != HintCode.UNUSED_LOCAL_VARIABLE;
|
|
}), isEmpty);
|
|
}
|
|
testUnitElement = testUnit!.declaredElement;
|
|
testLibraryElement = testUnitElement!.library;
|
|
findNode = FindNode(code, testUnit!);
|
|
findElement = FindElement(testUnit!);
|
|
}
|
|
|
|
@override
|
|
void setUp() {
|
|
var testRoot = testsPath;
|
|
if (analyzeWithNnbd) {
|
|
newFile('$testRoot/analysis_options.yaml', '''
|
|
analyzer:
|
|
enable-experiment:
|
|
- non-nullable
|
|
''');
|
|
}
|
|
if (analyzeWithNnbd) {
|
|
newPubspecYamlFile(testRoot, '''
|
|
name: tests
|
|
version: 1.0.0
|
|
environment:
|
|
sdk: '>=2.12.0 <3.0.0'
|
|
''');
|
|
}
|
|
super.setUp();
|
|
testFile = convertPath('$testRoot/lib/test.dart');
|
|
testUri = Uri.parse('package:tests/test.dart');
|
|
}
|
|
}
|