verify static method

void verify(
  1. String response, {
  2. Options options = const Options(),
})

Implementation

static void verify(
  String response, {
  Options options = const Options(),
}) {
  // Get the file path without extension or use the provided file path
  final completedPath = options.namer?.filePath ??
      filePathExtractor.filePath.split('.dart').first;

  // Create namer object with given or computed file name
  final namer = makeNamer(
    completedPath,
    description: options.namer?.description,
    options: options.namer?.options,
    addTestName: options.namer?.addTestName,
  );

  try {
    // Create writer object with scrubbed response and file extension retrieved from options
    final writer = ApprovalTextWriter(
      options.scrubber.scrub(response),
    );

    // Write the content to a file whose path is specified in namer.received
    writer.writeToFile(namer.received);

    if (options.approveResult ||
        !ApprovalUtils.isFileExists(namer.approved)) {
      writer.writeToFile(namer.approved);
    }

    // Check if received file matches the approved file
    final bool isFilesMatch = options.comparator.compare(
      approvedPath: namer.approved,
      receivedPath: namer.received,
      isLogError: options.logErrors,
    );

    // Log results and throw exception if files do not match
    if (!isFilesMatch) {
      options.reporter.report(namer.approved, namer.received);
      throw DoesntMatchException(
        'Oops: [${namer.approvedFileName}] does not match [${namer.receivedFileName}].\n\n - Approved file path: ${namer.approved}\n\n - Received file path: ${namer.received}',
      );
    } else {
      if (options.logResults) {
        ApprovalLogger.success(
          'Test passed: [${namer.approvedFileName}] matches [${namer.receivedFileName}]\n\n- Approved file path: ${namer.approved}\n\n- Received file path: ${namer.received}',
        );
        if (options.deleteReceivedFile) {
          _deleteFileAfterTest(namer: namer, fileType: FileType.received);
        }
      }
    }
  } catch (e, st) {
    if (options.logErrors) {
      ApprovalLogger.exception(e, stackTrace: st);
    }
    rethrow;
  }
}