analysis_plugin_test_helper 1.0.5 copy "analysis_plugin_test_helper: ^1.0.5" to clipboard
analysis_plugin_test_helper: ^1.0.5 copied to clipboard

Utilities to help write tests for the analyzer plugins.

example/example.dart

// Example usage of the analysis_plugin_test_helper package.
// For a complete demonstration, see the test codes in the `test` folder.

import 'package:analysis_plugin_test_helper/analysis_plugin_test_helper.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:test/test.dart';

void main() {
  final resolver = DartUnitResolver();

  setUpAll(() async => resolver.setUp());
  tearDownAll(() async => resolver.tearDown());

  test('resolve Dart source string', () async {
    final result = await resolver.resolveSource('''
      class MyAnnotation {
        const MyAnnotation();
      }

      @MyAnnotation()
      class Foo {
        void myMethod() {}
      }
    ''');

    expect(result.diagnostics, isEmpty);
    // result.unit is a fully resolved CompilationUnit
  });

  test('find annotation on class', () async {
    final result = await resolver.resolveSource('''
      class MyAnnotation {
        const MyAnnotation();
      }

      @MyAnnotation()
      class Foo {}
    ''');

    final annotation = findAnnotation<ClassDeclaration>(
      result.unit,
      annotationName: 'MyAnnotation',
    );
    expect(annotation, isNotNull);
  });

  test('find method by name', () async {
    final result = await resolver.resolveSource('''
      class MyClass {
        void myMethod() {}
      }
    ''');

    final method = findMethodDeclaration(result.unit, 'myMethod');
    expect(method, isNotNull);
  });

  test('find constructor by name', () async {
    final result = await resolver.resolveSource('''
      class MyClass {
        MyClass();
        MyClass.named();
      }
    ''');

    final defaultCtor = findConstructorDeclaration(result.unit, null);
    final namedCtor = findConstructorDeclaration(result.unit, 'named');

    expect(defaultCtor, isNotNull);
    expect(namedCtor, isNotNull);
  });

  test('find import directive', () async {
    final result = await resolver.resolveSource('''
      import 'dart:core';

      class MyClass {}
    ''');

    final importDirective = findImportDirective(result.unit);
    expect(importDirective, isNotNull);
  });
}
1
likes
150
points
134
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Utilities to help write tests for the analyzer plugins.

Homepage
Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

Dependencies

analysis_server_plugin, analyzer, analyzer_plugin, analyzer_testing, path, test

More

Packages that depend on analysis_plugin_test_helper