Better logging in random_test.dart, and less overall noise from warnings (dart-lang/yaml_edit#81)
diff --git a/pkgs/yaml_edit/lib/src/editor.dart b/pkgs/yaml_edit/lib/src/editor.dart
index be09f98..2079a3f 100644
--- a/pkgs/yaml_edit/lib/src/editor.dart
+++ b/pkgs/yaml_edit/lib/src/editor.dart
@@ -574,7 +574,7 @@
_yaml);
}
- final actualTree = loadYamlNode(_yaml);
+ final actualTree = withYamlWarningCallback(() => loadYamlNode(_yaml));
if (!deepEquals(actualTree, expectedTree)) {
throw createAssertionError(
'Modification did not result in expected result.',
diff --git a/pkgs/yaml_edit/lib/src/utils.dart b/pkgs/yaml_edit/lib/src/utils.dart
index a85a155..e5eb697 100644
--- a/pkgs/yaml_edit/lib/src/utils.dart
+++ b/pkgs/yaml_edit/lib/src/utils.dart
@@ -8,6 +8,25 @@
import 'editor.dart';
import 'wrap.dart';
+/// Invoke [fn] while setting [yamlWarningCallback] to [warn], and restore
+/// [YamlWarningCallback] after [fn] returns.
+///
+/// Defaults to a [warn] function that ignores all warnings.
+T withYamlWarningCallback<T>(
+ T Function() fn, {
+ YamlWarningCallback warn = _ignoreWarning,
+}) {
+ final original = yamlWarningCallback;
+ try {
+ yamlWarningCallback = warn;
+ return fn();
+ } finally {
+ yamlWarningCallback = original;
+ }
+}
+
+void _ignoreWarning(String warning, [SourceSpan? span]) {/* ignore warning */}
+
/// Determines if [string] is dangerous by checking if parsing the plain string
/// can return a result different from [string].
///
@@ -15,7 +34,8 @@
/// in [string].
bool isDangerousString(String string) {
try {
- if (loadYamlNode(string).value != string) {
+ final node = withYamlWarningCallback(() => loadYamlNode(string));
+ if (node.value != string) {
return true;
}
diff --git a/pkgs/yaml_edit/test/random_test.dart b/pkgs/yaml_edit/test/random_test.dart
index 0502df4..079fb1c 100644
--- a/pkgs/yaml_edit/test/random_test.dart
+++ b/pkgs/yaml_edit/test/random_test.dart
@@ -2,8 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-import 'dart:async';
-import 'dart:math';
+import 'dart:math' show Random;
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';
@@ -44,15 +43,10 @@
''');
for (var j = 0; j < modificationsPerRound; j++) {
- /// Using [runZoned] to hide `package:yaml`'s warnings.
- /// Test failures and errors will still be shown.
- runZoned(() {
- expect(
- () => generator.performNextModification(editor), returnsNormally);
- },
- zoneSpecification: ZoneSpecification(
- print: (Zone self, ZoneDelegate parent, Zone zone,
- String message) {}));
+ expect(
+ () => generator.performNextModification(editor),
+ returnsNormally,
+ );
}
});
}