matchesGoldenKey function

Future<Matcher> matchesGoldenKey(
  1. {@required String key,
  2. @required String file}
)

Returns a future that completes with a Matcher checking for JSON contents.

If file is found, it is assumed to be a JSON-encoded object, with a key where its value is a UTF-8 encoded String. If file does not exist, or the contents do not match the actual String (or Object.toString value), then the matcher will fail. To ignore failures and automatically update the golden files, see updateGoldensOnFailure.

NOTE: This method returns a Future<Matcher> that must be awaited!

Implementation

Future<Matcher> matchesGoldenKey({
  @required String key,
  @required String file,
}) async {
  ArgumentError.checkNotNull(key, 'key');
  ArgumentError.checkNotNull(file, 'file');
  final handle = File(file);
  if (!await handle.exists() && !_updateGoldens) {
    return _FileNotFoundMatcher(file);
  } else {
    final tryJson = await handle.readAsString();
    try {
      final text = (jsonDecode(tryJson) as Map<String, Object>)[key] as String;
      if (_updateGoldens) {
        return _StringUpdateMatcher(text, file, key);
      } else {
        return _StringOutputMatcher(text, file, key);
      }
    } catch (e) {
      return _JsonNotFoundMatcher(e);
    }
  }
}