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);',
      ),
  ],
);

AI Agent Skill (source-gen-test-fuzz)

package:source_gen_test ships a bundled source-gen-test-fuzz agent skill for stress-testing Generator and GeneratorForAnnotation implementations with cartesianProduct and verifyGeneratorBatch.

If your package depends on source_gen_test in dev_dependencies, install all bundled skills from your dependencies with package:skills:

dart run skills@ get

Or install the skill directly from GitHub:

dart run skills@ add kevmoo/source_gen_test --skill source-gen-test-fuzz
# or
npx skills add kevmoo/source_gen_test --skill source-gen-test-fuzz

Libraries

annotations
Use these types to annotate members you'd like to test.
source_gen_test