PackageContextForTest class
Creates a temporary directory with a pubspec using the test_descriptor
package, installs dependencies with dart pub get
, and sets up an analysis
context for the package.
Source files can then be added to the package with addFile, which will return a FileContext wrapper for use in tests.
Use this to setup tests for Suggestors that require the resolved AST, like
the AstVisitingSuggestor when shouldResolveAst()
returns true. Doing so
will enable the analyzer to resolve imports and symbols from other source
files and dependencies.
test('MySuggestor', () async {
var pkg = await PackageContextForTest.fromPubspec('''
name: pkg
version: 0.0.0
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
meta: ^1.0.0
''');
var context = await pkg.addFile('''
import 'package:meta/meta.dart';
@visibleForTesting var foo = true;
''');
var suggestor = MySuggestor();
var expectedOutput = '...';
expectSuggestorGeneratesPatches(suggestor, context, expectedOutput);
});
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
addFile(
String sourceText, [String? path]) → Future< FileContext> -
Creates a temporary file at the given
path
(relative to the root of this package) with the givensourceText
using thetest_descriptor
package and returns a FileContext wrapper around it. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
fromPubspec(
String pubspecContents, [String? dirName]) → Future< PackageContextForTest> -
Creates a temporary directory named
dirName
using thetest_descriptor
package, installs dependencies withdart pub get
, sets up an analysis context for the package, and returns a PackageContextForTest wrapper that allows you to add source files to the package and use them in tests.