saveTests method

Future<List<Future<Map<String, dynamic>>>> saveTests(
  1. ClassElement element,
  2. AssetId assetId,
  3. ConstantReader annotation
)

Implementation

Future<List<Future<Map<String, dynamic>>>> saveTests(
    ClassElement element, AssetId assetId, ConstantReader annotation) async {
  final List<String> excludedMethods = annotation.excludeMethods;
  final classAstNode = await getResolvedAstNodeForElement(element, _buildStep)
      as ClassDeclaration; //Orignally:  (await _buildStep.resolver.astNodeFor(element)) as ClassDeclaration;
  final fileAstNode = classAstNode.parent;
  final fileImport =
      'package:${assetId.package}/${assetId.path.replaceAll('lib/', '')}';

  final classVisitor = ClassVisitor();

  element.visitChildren(classVisitor);

  final Map<String, MethodDeclaration> methodAstNodes =
      await _astElementExecutor.methodNodesFromClassVisitor(
          _buildStep, classVisitor);
  final List<Future<Map<String, dynamic>>> saveFutures = [];
  for (int i = 0; i < methodAstNodes.entries.length; i++) {
    final method = methodAstNodes.entries.elementAt(i);
    final testRequestData = await _unitTestExecutor.prepareRequest(
        method: method,
        fileAstNode: fileAstNode,
        classAstNode: classAstNode,
        fileImport: fileImport,
        className: classVisitor.className,
        methodAstNodes: methodAstNodes,
        buildStep: _buildStep,
        excludedMethods: excludedMethods);
    if (testRequestData == null) continue;

    final file = await _pathRepository.correspondingPermanentTestFile(
        method.key, assetId);

    if (file == null) continue;

    var hasErrors =
        await _fixerExecutor.hasCompilationErrors(file.absolute.path);

    if (hasErrors) {
      log.info(
          "⚠️ File ${file.path.split(Platform.pathSeparator).last} has errors and will not be updated");
      continue;
    }
    saveFutures.add(_unitTestExecutor.updateTest(
        testRequestData, await file.readAsString()));
  }
  return saveFutures;
}