Skip to content

Commit

Permalink
Better logging in random_test.dart, and less overall noise from warni…
Browse files Browse the repository at this point in the history
…ngs (#81)
  • Loading branch information
jonasfj authored May 30, 2024
1 parent 3191934 commit 8fdc96d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/src/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ class YamlEditor {
_yaml);
}

final actualTree = loadYamlNode(_yaml);
final actualTree = withYamlWarningCallback(() => loadYamlNode(_yaml));
if (!deepEquals(actualTree, expectedTree)) {
throw createAssertionError(
'Modification did not result in expected result.',
Expand Down
22 changes: 21 additions & 1 deletion lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,34 @@ import 'package:yaml/yaml.dart';
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].
///
/// This function is also capable of detecting if non-printable characters are
/// in [string].
bool isDangerousString(String string) {
try {
if (loadYamlNode(string).value != string) {
final node = withYamlWarningCallback(() => loadYamlNode(string));
if (node.value != string) {
return true;
}

Expand Down
16 changes: 5 additions & 11 deletions test/random_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -44,15 +43,10 @@ dev_dependencies:
''');

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,
);
}
});
}
Expand Down

0 comments on commit 8fdc96d

Please sign in to comment.