generator_test 0.1.0 generator_test: ^0.1.0 copied to clipboard
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 part
s for you, whether it should be included or not, and the extension to be used based on the builder being tested.
Take Note:
You must include the generator's (class) name, enclosed in ***
, within the fixture file
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!