source_gen_test 1.4.1 copy "source_gen_test: ^1.4.1" to clipboard
source_gen_test: ^1.4.1 copied to clipboard

Test support for the source_gen package. Includes helpers to make it easy to validate both success and failure cases.

Pub package CI package publisher

Make it easy to test Generators derived from package:source_gen by annotating test files.

@ShouldGenerate(
  r'''
const TestClass1NameLength = 10;

const TestClass1NameLowerCase = 'testclass1';
''',
  configurations: ['default', 'no-prefix-required'],
)
@ShouldThrow(
  'Uh...',
  configurations: ['vague'],
  element: false,
)
@TestAnnotation()
class TestClass1 {}

Test against a golden output file if you also want to write tests on the output itself.

part 'goldens/testclass2.dart';

@ShouldGenerateFile(
  'goldens/testclass2.dart',
  partOfCurrent: true,
  configurations: ['default', 'no-prefix-required'],
)
@ShouldThrow(
  'Uh...',
  configurations: ['vague'],
  element: false,
)
@TestAnnotation()
class TestClass2 {}

Other helpers are also provided.

Combinatorial Batch Verification (verifyGeneratorBatch) #

Use cartesianProduct and verifyGeneratorBatch to validate large matrices of AST/declaration permutations against a Generator or GeneratorForAnnotation in a single shared package:analyzer context and batched Isolate.spawnUri runtime pass:

final axes = cartesianProduct({
  'fieldType': ['int', 'int?', 'List<int?>'],
  'includeIfNull': ['true', 'false'],
});

final report = await verifyGeneratorBatch(
  generator: MyGenerator(),
  imports: const ["import 'package:my_pkg/annotations.dart';"],
  cases: [
    for (var i = 0; i < axes.length; i++)
      CodegenCase(
        id: 'case_$i',
        declarations: '''
@MyAnnotation(includeIfNull: ${axes[i]['includeIfNull']})
class Case$i {
  final ${axes[i]['fieldType']} value;
  Case$i(this.value);
}
''',
        runtimeCheck: 'final c = Case$i(null);',
      ),
  ],
);
13
likes
160
points
53k
downloads

Documentation

API reference

Publisher

verified publisherkevmoo.com

Weekly Downloads

Test support for the source_gen package. Includes helpers to make it easy to validate both success and failure cases.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

analyzer, build, build_test, cli_util, dart_style, meta, path, source_gen, test

More

Packages that depend on source_gen_test