heimdall_test 0.1.0
heimdall_test: ^0.1.0 copied to clipboard
Architecture rules for Dart source trees inspired by ArchUnit.
example/heimdall_test_example.dart
// This command-line example writes rule reports to stdout.
// ignore_for_file: avoid_print, cascade_invocations
import 'package:heimdall_test/heimdall_test.dart';
void main() {
final project = const HeimdallFileImporter(
importOptions: [
ExcludeGeneratedDartImportOption(),
],
).importPath('lib');
final reports = [
Heimdall.code().shouldParse().check(project),
Heimdall.code().shouldNotImportDartMirrors().check(project),
Heimdall.classes()
.that()
.resideInPath('src')
.and()
.arePublic()
.should()
.satisfy(_havePascalCaseName())
.as('public types should use PascalCase')
.check(project),
Heimdall.layers()
.layer('Public')
.definedBy(['heimdall_test.dart'])
.layer('Core')
.definedBy(['src/core.dart', 'src/core/(**)', 'src/mapper/(**)'])
.layer('Library')
.definedBy(['src/library_rules.dart', 'src/library/(**)'])
.layer('Plugins')
.definedBy(['src/plugins.dart'])
.whereLayer('Public')
.mayOnlyAccessLayers(['Core', 'Library', 'Plugins'])
.whereLayer('Core')
.mayOnlyAccessLayers(['Core'])
.whereLayer('Library')
.mayOnlyAccessLayers(['Core', 'Library'])
.whereLayer('Plugins')
.mayOnlyAccessLayers(['Core'])
.consideringOnlyDependenciesInLayers()
.asRule(description: 'package source layers')
.check(project),
];
reports.forEach(_printReport);
}
void _printReport(HeimdallReport report) {
if (!report.hasFindings) {
print('[OK] ${report.description} (${report.checkedCount} checked)');
return;
}
print('[FAIL] ${report.description} (${report.checkedCount} checked)');
for (final finding in report.findings) {
print(' - $finding');
}
}
HeimdallCondition<CompilationUnitMember> _havePascalCaseName() {
final pascalCase = RegExp(r'^[A-Z][A-Za-z0-9]*$');
return HeimdallCondition('have PascalCase names', (item, _) {
if (pascalCase.hasMatch(item.name)) return const [];
return [
HeimdallFinding(
filePath: item.sourcePath,
line: item.line,
message: '${item.name} should use PascalCase',
),
];
});
}