keptNames method

Set<String> keptNames(
  1. Map<String, BeanDefinition<Object>> definitions
)

The names, among definitions (the application's, by bean name), of the beans the slice keeps.

Throws a StateError when a type of include is the type of none of them.

Implementation

Set<String> keptNames(Map<String, BeanDefinition<Object>> definitions) {
  final kept = <String>{};
  final included = <String>{};
  for (final type in include) {
    final names = [
      for (final MapEntry(:key, :value) in definitions.entries)
        if (value.types.contains(type)) key,
    ];
    if (names.isEmpty) {
      throw StateError(
        '@DataTest(include: [$type]): no bean of the application has this '
        'type (a class is a bean when it is annotated @Component, @Service, '
        '@Configuration...)',
      );
    }
    included.addAll(names);
  }
  kept.addAll(included);
  for (final MapEntry(:key, :value) in definitions.entries) {
    if (isDataBean(value) || included.contains(value.factoryBeanName)) {
      kept.add(key);
    }
  }
  // A @Bean method needs the object of its configuration class.
  for (final name in kept.toList()) {
    final factory = definitions[name]!.factoryBeanName;
    if (factory != null && definitions.containsKey(factory)) {
      kept.add(factory);
    }
  }
  return kept;
}