private static function parseFile()

in minitest/CodegenAssertUnchanged.hack [110:156]


  private static function parseFile(string $file_name): dict<string, string> {
    $map = dict[];

    if (!\file_exists($file_name)) {
      return $map;
    }

    $lines = \file($file_name);
    invariant(
      $lines !== false,
      'Fail to open the file %s for reading',
      $file_name,
    );

    $generated = \array_shift(inout $lines);
    invariant(
      Str\trim_right((string)$generated) === '@'.'generated',
      'Codegen test record file should start with a generated tag',
    );

    $token = null;
    $expected = '';
    foreach ($lines as $line) {
      if (Str\starts_with($line, self::SEPARATOR)) {
        if ($token !== null) {
          // We always add 1 newline at the end
          $expected = \substr($expected, 0, -1);
          $map[$token] = $expected;
        }
        // Format is separator:token\n
        $token = \substr(
          Str\trim_right((string)$line),
          Str\length(self::SEPARATOR),
        );
        $expected = '';
        continue;
      }

      $expected .= self::unescapeTokens($line);
    }
    if ($token !== null) {
      // We always add 1 newline at the end
      $expected = \substr($expected, 0, -1);
      $map[$token] = $expected;
    }
    return $map;
  }