diff --git a/checked_yaml/CHANGELOG.md b/checked_yaml/CHANGELOG.md index a3b7dac7..567d1479 100644 --- a/checked_yaml/CHANGELOG.md +++ b/checked_yaml/CHANGELOG.md @@ -1,4 +1,4 @@ -## 2.0.4-wip +## 2.0.4 - Require Dart 3.8 diff --git a/checked_yaml/lib/checked_yaml.dart b/checked_yaml/lib/checked_yaml.dart index 17c5ce0c..1b6ad05d 100644 --- a/checked_yaml/lib/checked_yaml.dart +++ b/checked_yaml/lib/checked_yaml.dart @@ -36,7 +36,6 @@ T checkedYamlDecode( if (yaml is YamlMap) { map = yaml; } else if (allowNull && yaml is YamlScalar && yaml.value == null) { - // TODO: test this case! map = null; } else { throw ParsedYamlException('Not a map', yaml); @@ -73,34 +72,29 @@ ParsedYamlException toParsedYamlException( ) as YamlNode; return ParsedYamlException(exception.message!, node, innerError: exception); - } else { - if (exception.key == null) { - return ParsedYamlException( - exception.message ?? 'There was an error parsing the map.', - yamlMap, - innerError: exception, - ); - } else if (!yamlMap.containsKey(exception.key)) { - return ParsedYamlException( - [ - 'Missing key "${exception.key}".', - if (exception.message != null) exception.message!, - ].join(' '), - yamlMap, - innerError: exception, - ); - } else { - var message = 'Unsupported value for "${exception.key}".'; - if (exception.message != null) { - message = '$message ${exception.message}'; - } - return ParsedYamlException( - message, - yamlMap.nodes[exception.key] ?? yamlMap, - innerError: exception, - ); - } } + + if (exception.key == null) { + return ParsedYamlException( + exception.message ?? 'There was an error parsing the map.', + yamlMap, + innerError: exception, + ); + } + + if (!yamlMap.containsKey(exception.key)) { + return ParsedYamlException( + ['Missing key "${exception.key}".', ?exception.message].join(' '), + yamlMap, + innerError: exception, + ); + } + + return ParsedYamlException( + ['Unsupported value for "${exception.key}".', ?exception.message].join(' '), + yamlMap.nodes[exception.key] ?? yamlMap, + innerError: exception, + ); } /// An exception thrown when parsing YAML that contains information about the @@ -118,12 +112,10 @@ class ParsedYamlException implements Exception { /// contains the source error object. final Object? innerError; - ParsedYamlException(String message, YamlNode this.yamlNode, {this.innerError}) - : // TODO(kevmoo) remove when dart-lang/sdk#50756 is fixed! - message = message.replaceAll(" of ' in type cast'", ' in type cast'); + ParsedYamlException(this.message, YamlNode this.yamlNode, {this.innerError}); - factory ParsedYamlException.fromYamlException(YamlException exception) => - _WrappedYamlException(exception); + factory ParsedYamlException.fromYamlException(YamlException exception) = + _WrappedYamlException; /// Returns [message] formatted with source information provided by /// [yamlNode]. @@ -147,7 +139,4 @@ class _WrappedYamlException implements ParsedYamlException { @override YamlNode? get yamlNode => null; - - @override - String toString() => 'ParsedYamlException: $formattedMessage'; } diff --git a/checked_yaml/pubspec.yaml b/checked_yaml/pubspec.yaml index ada78745..6195805d 100644 --- a/checked_yaml/pubspec.yaml +++ b/checked_yaml/pubspec.yaml @@ -1,5 +1,5 @@ name: checked_yaml -version: 2.0.4-wip +version: 2.0.4 description: >- Generate more helpful exceptions when decoding YAML documents using diff --git a/checked_yaml/test/custom_error_test.dart b/checked_yaml/test/custom_error_test.dart index 6c17bda9..b4d8826c 100644 --- a/checked_yaml/test/custom_error_test.dart +++ b/checked_yaml/test/custom_error_test.dart @@ -4,7 +4,7 @@ import 'package:test/test.dart'; import 'package:yaml/yaml.dart'; void main() { - test('bob', () { + test('simple test', () { expect( () => checkedYamlDecode('{"innerMap": {}}', (m) { throw CheckedFromJsonException( @@ -36,4 +36,35 @@ line 1, column 14: There was an error parsing the map. ), ); }); + + test('null map - allowed', () { + expect( + () => checkedYamlDecode(allowNull: true, 'null', (m) => Object()), + isA(), + ); + }); + + test('null map - not allowed', () { + expect( + () => checkedYamlDecode('null', (m) { + throw TestFailure('should never get here!'); + }), + throwsA( + isA() + .having((e) => e.message, 'message', 'Not a map') + .having( + (e) => e.yamlNode, + 'yamlNode', + isA().having((s) => s.value, 'value', isNull), + ) + .having((e) => e.innerError, 'innerError', isNull) + .having((e) => e.formattedMessage, 'formattedMessage', ''' +line 1, column 1: Not a map + ╷ +1 │ null + │ ^^^^ + ╵'''), + ), + ); + }); }