generator_test 0.2.0 copy "generator_test: ^0.2.0" to clipboard
generator_test: ^0.2.0 copied to clipboard

outdated

A library to help test generators and build runner packages using dart files instead of Strings

Generator Test

A library to help test generators and build runner packages using dart files instead of Strings

Purpose #

Testing generators and build_runner packages can be difficult. generator_test is a library aimed at making it easier to accomplish this!

Usage #

Depend on it #

Add generator_test to your pubspec.yaml

dev_dependencies:
  generator_test: # RECENT_VERSION

Use it #

Create a fixture file #

A fixture is

A known state which a test is running against

Meaning, you can create an "output" to compare against the generated code based on a specific input. You can read more about it here

To use this package, you will create files, an input & a fixture file (aka the expected generated output).

Here is an example of what your input file could look like

part 'fixtures/example.dart';

@ClassName()
class Example {}

And here is an example of what your fixture file could look like

part of '../example.dart';

// **************************************************************************
// ClassNameGenerator
// **************************************************************************

// The class name is `Example`

The generator_test package will handle the files parts for you, whether it should be included or not, and the extension to be used based on the builder being tested.

You can provide the generator name wrapped in * (77 total) or you can let the package handle it for you. Like so,

// @generator=ClassNameGenerator

generator_test will use the provided name of the generator and wrap it with *

Create your test #

Generator

// create the test generator

final generator = SuccessGenerator(
      'example',
      ClassNameGenerator(),
      inputDir: 'test/fixture', // default
      fixtureDir: 'test/fixture/fixtures', // default
      compareWithFixture: true, // use `false` to validate dart code only
      fixtureFileName: ..., // used when fixture file name does not match input file name
    );
);

// run the test
generator.test();

Builder

final generator = SuccessGenerator.fromBuilder(
      'example',
      classNameBuilder, // entry point for the build_runner
      inputDir: 'test/fixture', // default
      fixtureDir: 'test/fixture/fixtures', // default
      compareWithFixture: true, // use `false` to validate dart code only
      fixtureFileName: ..., // used when fixture file name does not match input file name
      extension: ..., // retrieves from the builder ie: .g.dart
      options: ..., // config for your builder ie: {'add_comment': true}
    );

// run the test
generator.test();

Debug Bonus #

Debugging build_runners is not s simple task, and on top of that, the analyzer's api is HUGE. It can be daunting to write build_runner packages with the amount of data you need to filter through to get the values you want.

generator_test makes it possible to add break points during your tests, which makes developing build_runner packages much easier!

Simply add async/await to your test and run in debug mode!

void main(){
  test('generates successfully', () async {
    final generator = SuccessGenerator.fromBuilder(
          'example',
          classNameBuilder,
          compareWithFixture: false, // use `false` to validate dart code
        );

    // dubug your package
    await generator.test();
  });
}

Find the values you're looking for!

analyzer_api_debug

4
likes
0
pub points
7%
popularity

Publisher

verified publishermrgnhnt.com

A library to help test generators and build runner packages using dart files instead of Strings

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

build, build_test, dart_style, logging, meta, source_gen

More

Packages that depend on generator_test