saveCases static method

void saveCases(
  1. dynamic receivedItems,
  2. dynamic expectedItems, {
  3. String? filePath,
  4. String? fileName,
})

Saves approved test cases to a Dart file.

Implementation

static void saveCases(dynamic receivedItems, dynamic expectedItems, {String? filePath, String? fileName}) {
  if (receivedItems is List && expectedItems is List) {
    List<Map<String, dynamic>> approvedCases = [];
    for (int i = 0; i < receivedItems.length; i++) {
      approvedCases.add({
        "received": receivedItems[i].toJson(),
        "expected": expectedItems[i].toJson(),
      });
    }
    final String name = fileName != null ? _toCamelCaseFromSnakeCase(fileName.replaceAll(".g.dart", "")) : "approvedCases";
    File file = File(filePath ?? (fileName != null ? directoryPath() + fileName : _defaultPath));

    var buffer = StringBuffer();
    buffer.writeln("const $name = [");
    for (var testCase in approvedCases) {
      buffer.writeln("  ${jsonEncode(testCase)}${testCase == approvedCases.last ? "" : ","}");
    }
    buffer.writeln("];");
    file.writeAsStringSync(buffer.toString());
  } else if (expectedItems is! List && receivedItems == null) {
    var approvedBuffer = StringBuffer();

    final String approvedName = "approved_cases.txt";

    approvedBuffer.writeln(expectedItems);

    File approvedFile = File(filePath ?? directoryPath() + approvedName);

    approvedFile.writeAsStringSync(approvedBuffer.toString());

    print("Approved cases have been saved.");
  }
}