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

@ClassName()
class Example {}

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

// **************************************************************************
// 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.dart'],
      ['example.g.dart'],
      ClassNameGenerator(),
      compareWithFixture: true, // use `false` to validate input only only
    );

// Test that the generator runs successfully
generator.test();

Builder

final generator = SuccessGenerator.fromBuilder(
      ['example.dart'],
      ['example.g.dart'],
      classNameBuilder, // entry point for the build_runner
      compareWithFixture: true, // use `false` to validate input only only
      options: {}, // config for your builder ie: {'add_comment': true}
    );

// test the output
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 input only
        );

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

Find the values you're looking for!

analyzer_api_debug

Libraries

generator_test