blob: cfb022a1563e987ccf00e0f8e045f7b64d1da7ce [file] [log] [blame]
// 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;
String testFile;
Uri testUri;
Source testSource;
ResolvedUnitResult testAnalysisResult;
CompilationUnit testUnit;
CompilationUnitElement testUnitElement;
LibraryElement testLibraryElement;
FindNode findNode;
FindElement findElement;
/// Whether the test should perform analysis with NNBD enabled.
///
/// `false` by default. May be overridden in derived test classes.
///
/// TODO(paulberry): once NNBD ships, this should no longer be needed.
bool get analyzeWithNnbd => false;
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);
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 = AbstractContextTest.testsPath;
if (analyzeWithNnbd) {
newFile(convertPath('$testRoot/analysis_options.yaml'), content: '''
analyzer:
enable-experiment:
- non-nullable
''');
}
super.setUp();
testFile = convertPath('$testRoot/lib/test.dart');
testUri = Uri.parse('package:tests/test.dart');
}
}