Skip to content

Commit 3a96615

Browse files
authored
Add expectations for emitted warnings (#2366)
Pulled from #2362 and refactored to test for exactly 1 warning. Add a test utility to check for warnings emitted while parsing and use it in two test cases that emit warnings.
1 parent 100f16a commit 3a96615

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

pkgs/yaml/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## 3.1.4-wip
2+
13
## 3.1.3
24

35
* Require Dart 3.4

pkgs/yaml/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: yaml
2-
version: 3.1.3
2+
version: 3.1.4-wip
33
description: A parser for YAML, a human-friendly data serialization standard
44
repository: https://github.com/dart-lang/tools/tree/main/pkgs/yaml
55
issue_tracker: https://github.com/dart-lang/tools/labels/package%3Ayaml

pkgs/yaml/test/utils.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ void expectYamlFails(String source) {
5353
expect(() => loadYaml(cleanUpLiteral(source)), throwsYamlException);
5454
}
5555

56+
/// Asserts that a string containing a single YAML document produces a given
57+
/// value and emits warnings when loaded.
58+
void expectYamlLoadsWithWarning(
59+
Object? expected, String source, String expectedWarning) {
60+
final warnings = <String>[];
61+
final oldCallback = yamlWarningCallback;
62+
yamlWarningCallback = (message, [_]) {
63+
warnings.add(message);
64+
};
65+
try {
66+
expectYamlLoads(expected, source);
67+
expect(warnings, ['Warning: $expectedWarning']);
68+
} finally {
69+
yamlWarningCallback = oldCallback;
70+
}
71+
}
72+
5673
/// Removes eight spaces of leading indentation from a multiline string.
5774
///
5875
/// Note that this is very sensitive to how the literals are styled. They should

pkgs/yaml/test/yaml_test.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -992,21 +992,25 @@ void main() {
992992
});
993993

994994
group('6.8: Directives', () {
995-
// TODO(nweiz): assert that this produces a warning
996995
test('[Example 6.13]', () {
997-
expectYamlLoads('foo', '''
996+
expectYamlLoadsWithWarning(
997+
'foo',
998+
'''
998999
%FOO bar baz # Should be ignored
9991000
# with a warning.
1000-
--- "foo"''');
1001+
--- "foo"''',
1002+
'unknown directive.');
10011003
});
10021004

1003-
// TODO(nweiz): assert that this produces a warning.
10041005
test('[Example 6.14]', () {
1005-
expectYamlLoads('foo', '''
1006+
expectYamlLoadsWithWarning(
1007+
'foo',
1008+
'''
10061009
%YAML 1.3 # Attempt parsing
10071010
# with a warning
10081011
---
1009-
"foo"''');
1012+
"foo"''',
1013+
'this parser only supports YAML 1.1 and 1.2.');
10101014
});
10111015

10121016
test('[Example 6.15]', () {

0 commit comments

Comments
 (0)