Fixed bug with adding empty map (#78)
* Fixed bug with adding empty map
* Added comments to clarify change
diff --git a/lib/src/strings.dart b/lib/src/strings.dart
index 6f7a155..515b814 100644
--- a/lib/src/strings.dart
+++ b/lib/src/strings.dart
@@ -279,7 +279,9 @@
final formattedValue =
yamlEncodeBlockString(entry.value, newIndentation, lineEnding);
- if (isCollection(entry.value)) {
+ /// Empty collections are always encoded in flow-style, so new-line must
+ /// be avoided
+ if (isCollection(entry.value) && !isEmpty(entry.value)) {
return formattedKey + ':\n' + formattedValue;
}
diff --git a/test/update_test.dart b/test/update_test.dart
index 23b0023..24f9530 100644
--- a/test/update_test.dart
+++ b/test/update_test.dart
@@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-import 'package:yaml_edit/yaml_edit.dart';
import 'package:test/test.dart';
+import 'package:yaml/yaml.dart';
+import 'package:yaml_edit/yaml_edit.dart';
import 'test_utils.dart';
@@ -864,6 +865,94 @@
'''));
expectYamlBuilderValue(doc, {'a': 1, 'b': 2, 'c': 3, 'd': 4});
});
+
+ test('adds an empty map properly', () {
+ final doc = YamlEditor('a: b');
+ doc.update(['key'], {});
+ expectYamlBuilderValue(doc, {'a': 'b', 'key': {}});
+ });
+
+ test('adds an empty map properly (2)', () {
+ final doc = YamlEditor('a: b');
+ doc.update(['a'], {'key': {}});
+ expectYamlBuilderValue(doc, {
+ 'a': {'key': {}}
+ });
+ });
+ });
+
+ group('empty starting document', () {
+ test('empty map', () {
+ final doc = YamlEditor('');
+ doc.update([], {'key': {}});
+ expectYamlBuilderValue(doc, {'key': {}});
+ });
+
+ test('empty map (2)', () {
+ final doc = YamlEditor('');
+ doc.update([], {});
+ expectYamlBuilderValue(doc, {});
+ });
+
+ test('empty map (3)', () {
+ final doc = YamlEditor('');
+ doc.update(
+ [],
+ wrapAsYamlNode(
+ {'key': {}},
+ collectionStyle: CollectionStyle.BLOCK,
+ ),
+ );
+ expectYamlBuilderValue(doc, {'key': {}});
+ });
+
+ test('empty map (4)', () {
+ final doc = YamlEditor('');
+ doc.update(
+ [],
+ wrapAsYamlNode(
+ {},
+ collectionStyle: CollectionStyle.BLOCK,
+ ),
+ );
+ expectYamlBuilderValue(doc, {});
+ });
+
+ test('empty list', () {
+ final doc = YamlEditor('');
+ doc.update([], {'key': []});
+ expectYamlBuilderValue(doc, {'key': []});
+ });
+
+ test('empty list (2)', () {
+ final doc = YamlEditor('');
+ doc.update([], []);
+ expectYamlBuilderValue(doc, []);
+ });
+
+ test('empty list (3)', () {
+ final doc = YamlEditor('');
+ doc.update(
+ [],
+ wrapAsYamlNode(
+ {'key': []},
+ collectionStyle: CollectionStyle.BLOCK,
+ ),
+ );
+ expectYamlBuilderValue(doc, {'key': []});
+ });
+
+ test('empty map (4)', () {
+ final doc = YamlEditor('');
+ doc.update(
+ [],
+ wrapAsYamlNode(
+ [],
+ collectionStyle: CollectionStyle.BLOCK,
+ ),
+ );
+ expectYamlBuilderValue(doc, []);
+ });
});
});
}
diff --git a/test/wrap_test.dart b/test/wrap_test.dart
index 3aed89c..50dc2da 100644
--- a/test/wrap_test.dart
+++ b/test/wrap_test.dart
@@ -329,8 +329,8 @@
});
test(
- 'returns the same result for two YamlMaps with same value but different styles',
- () {
+ 'returns the same result for two YamlMaps with same value but '
+ 'different styles', () {
final hashCode1 = deepHashCode(wrapAsYamlNode({'a': 1, 'b': 2},
collectionStyle: CollectionStyle.BLOCK));
final hashCode2 = deepHashCode(wrapAsYamlNode({'a': 1, 'b': 2},