Files
sdk/pkg/analysis_server/test/abstract_single_unit.dart
T
Konstantin Shcheglov e97f1bdbf0 Switch analysis_server to language 2.12, so null safety, but opt-out files.
This should allow doing partial migration, specifically protocol files,
which are imported by other libraries, but are a small library cycle
that does not import much outside of it.

Change-Id: I904c05d6d5b444ee9a9dbd1f7ada12aabdcc5165
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193583
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2021-03-30 23:39:37 +00:00

101 lines
3.2 KiB
Dart

// Copyright (c) 2014, 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.
// @dart = 2.9
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/file_system/file_system.dart';
import 'package:analyzer/src/dart/error/hint_codes.dart';
import 'package:analyzer/src/test_utilities/find_element.dart';
import 'package:analyzer/src/test_utilities/find_node.dart';
import 'package:analyzer/src/test_utilities/platform.dart';
import 'package:test/test.dart';
import 'abstract_context.dart';
class AbstractSingleUnitTest extends AbstractContextTest {
bool verifyNoTestUnitErrors = true;
/// Whether to rewrite line endings in test code based on platform.
bool useLineEndingsForPlatform = false;
String testCode;
String testFile;
ResolvedUnitResult testAnalysisResult;
CompilationUnit testUnit;
CompilationUnitElement testUnitElement;
LibraryElement testLibraryElement;
FindNode findNode;
FindElement findElement;
@override
void addSource(String path, String content) {
if (useLineEndingsForPlatform) {
content = normalizeNewlinesForPlatform(content);
}
super.addSource(path, content);
}
void addTestSource(String code) {
if (useLineEndingsForPlatform) {
code = normalizeNewlinesForPlatform(code);
}
testCode = code;
addSource(testFile, code);
}
int findEnd(String search) {
return findOffset(search) + search.length;
}
int findOffset(String search) {
var offset = testCode.indexOf(search);
expect(offset, isNonNegative, reason: "Not found '$search' in\n$testCode");
return offset;
}
@override
File newFile(String path, {String content = ''}) {
if (useLineEndingsForPlatform) {
content = normalizeNewlinesForPlatform(content);
}
return super.newFile(path, content: content);
}
Future<void> resolveTestCode(String code) async {
addTestSource(code);
await resolveTestFile();
}
Future<void> resolveTestFile() async {
testAnalysisResult = await session.getResolvedUnit(testFile);
testCode = testAnalysisResult.content;
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_FIELD &&
error.errorCode != HintCode.UNUSED_IMPORT &&
error.errorCode != HintCode.UNUSED_LOCAL_VARIABLE;
}), isEmpty);
}
testUnitElement = testUnit.declaredElement;
testLibraryElement = testUnitElement.library;
findNode = FindNode(testCode, testUnit);
findElement = FindElement(testUnit);
}
@override
void setUp() {
super.setUp();
testFile = convertPath('/home/test/lib/test.dart');
}
}