You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.5 KiB
37 lines
1.5 KiB
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'models.g.dart';
|
|
|
|
void main() {
|
|
final fixtures = jsonDecode(File.fromUri(Platform.script.resolve('../fixtures.json')).readAsStringSync()) as Map<String, dynamic>;
|
|
final cases = fixtures['valid'] as List;
|
|
for (final fixture in cases) {
|
|
final value = fixture['value'];
|
|
final decoded = switch (fixture['model']) {
|
|
'UUID' || 'Instant' || 'Locale' => value as String,
|
|
'Revision' => value as int,
|
|
'LocalizedMessage' => LocalizedMessage.fromJson(value).toJson(),
|
|
'ErrorData' => ErrorData.fromJson(value).toJson(),
|
|
'HttpProblem' => HttpProblem.fromJson(value).toJson(),
|
|
'ErrorMessagePayload' => ErrorMessagePayload.fromJson(value).toJson(),
|
|
'CommandRejection' => CommandRejection.fromJson(value).toJson(),
|
|
'CatalogPage' => CatalogPage.fromJson(value).toJson(),
|
|
'CatalogResponse' => CatalogResponse.fromJson(value).toJson(),
|
|
_ => throw StateError('Missing fixture decoder: ${fixture['model']}'),
|
|
};
|
|
if (jsonEncode(_canonical(value)) != jsonEncode(_canonical(decoded))) {
|
|
throw StateError('JSON changed: ${fixture['name']}');
|
|
}
|
|
}
|
|
print('${cases.length} shared Dart JSON round trips verified.');
|
|
}
|
|
|
|
dynamic _canonical(dynamic value) {
|
|
if (value is List) return value.map(_canonical).toList();
|
|
if (value is Map) {
|
|
final keys = value.keys.cast<String>().toList()..sort();
|
|
return {for (final key in keys) key: _canonical(value[key])};
|
|
}
|
|
return value;
|
|
}
|