Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better logging in random_test.dart, and less overall noise from warnings #81

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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