heimdall_test 0.1.0 copy "heimdall_test: ^0.1.0" to clipboard
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',
      ),
    ];
  });
}
0
likes
0
points
154
downloads

Publisher

unverified uploader

Weekly Downloads

Architecture rules for Dart source trees inspired by ArchUnit.

Repository (GitHub)
View/report issues

Topics

#architecture #testing #static-analysis #archunit

License

unknown (license)

Dependencies

analyzer, collection, meta, path, pub_semver, yaml

More

Packages that depend on heimdall_test