Fix for issue dart-lang/yaml_edit#23 (dart-lang/yaml_edit#34)
* Fix for selecting the correct index in insertion
* Add new test cases
* Add more tests and format corrections
diff --git a/pkgs/yaml_edit/lib/src/list_mutations.dart b/pkgs/yaml_edit/lib/src/list_mutations.dart
index e7a1c04..6e2368e 100644
--- a/pkgs/yaml_edit/lib/src/list_mutations.dart
+++ b/pkgs/yaml_edit/lib/src/list_mutations.dart
@@ -117,7 +117,7 @@
// Adjusts offset to after the trailing newline of the last entry, if it exists
if (list.isNotEmpty) {
final lastValueSpanEnd = list.nodes.last.span.end.offset;
- final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd);
+ final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd - 1);
if (nextNewLineIndex == -1) {
formattedValue = getLineEnding(yaml) + formattedValue;
} else {
diff --git a/pkgs/yaml_edit/test/append_test.dart b/pkgs/yaml_edit/test/append_test.dart
index a365b8c..cb705ed 100644
--- a/pkgs/yaml_edit/test/append_test.dart
+++ b/pkgs/yaml_edit/test/append_test.dart
@@ -149,6 +149,99 @@
2: null
'''));
});
+
+ test('block append (1)', () {
+ final yamlEditor = YamlEditor('''
+# comment
+- z:
+ x: 1
+ y: 2
+- z:
+ x: 3
+ y: 4
+''');
+ yamlEditor.appendToList([], {
+ 'z': {'x': 5, 'y': 6}
+ });
+
+ expect(yamlEditor.toString(), equals('''
+# comment
+- z:
+ x: 1
+ y: 2
+- z:
+ x: 3
+ y: 4
+- z:
+ x: 5
+ y: 6
+'''));
+ });
+
+ test('block append (2)', () {
+ final yamlEditor = YamlEditor('''
+# comment
+a:
+ - z:
+ x: 1
+ y: 2
+ - z:
+ x: 3
+ y: 4
+b:
+ - w:
+ m: 2
+ n: 4
+''');
+ yamlEditor.appendToList([
+ 'a'
+ ], {
+ 'z': {'x': 5, 'y': 6}
+ });
+
+ expect(yamlEditor.toString(), equals('''
+# comment
+a:
+ - z:
+ x: 1
+ y: 2
+ - z:
+ x: 3
+ y: 4
+ - z:
+ x: 5
+ y: 6
+b:
+ - w:
+ m: 2
+ n: 4
+'''));
+ });
+
+ test('block append nested and with comments', () {
+ final yamlEditor = YamlEditor('''
+a:
+ b:
+ - c:
+ d: 1
+ - c:
+ d: 2
+# comment
+ e:
+ - g:
+ e: 1
+ f: 2
+# comment
+''');
+ expect(
+ () => yamlEditor.appendToList([
+ 'a',
+ 'e'
+ ], {
+ 'g': {'e': 3, 'f': 4}
+ }),
+ returnsNormally);
+ });
});
group('flow list', () {
diff --git a/pkgs/yaml_edit/test/insert_test.dart b/pkgs/yaml_edit/test/insert_test.dart
index afb2735..8c0e3b2 100644
--- a/pkgs/yaml_edit/test/insert_test.dart
+++ b/pkgs/yaml_edit/test/insert_test.dart
@@ -98,6 +98,81 @@
'''));
expectYamlBuilderValue(doc, [0, 1, 2]);
});
+
+ for (var i = 0; i < 3; i++) {
+ test('block insert(1) at $i', () {
+ final yamlEditor = YamlEditor('''
+# comment
+- z:
+ x: 1
+ y: 2
+- z:
+ x: 3
+ y: 4
+''');
+ expect(
+ () => yamlEditor.insertIntoList(
+ [],
+ i,
+ {
+ 'z': {'x': 5, 'y': 6}
+ }),
+ returnsNormally);
+ });
+ }
+
+ for (var i = 0; i < 3; i++) {
+ test('block insert(2) at $i', () {
+ final yamlEditor = YamlEditor('''
+a:
+ - z:
+ x: 1
+ y: 2
+ - z:
+ x: 3
+ y: 4
+b:
+ - w:
+ m: 2
+ n: 4
+''');
+ expect(
+ () => yamlEditor.insertIntoList(
+ ['a'],
+ i,
+ {
+ 'z': {'x': 5, 'y': 6}
+ }),
+ returnsNormally);
+ });
+ }
+
+ for (var i = 0; i < 2; i++) {
+ test('block insert nested and with comments at $i', () {
+ final yamlEditor = YamlEditor('''
+a:
+ b:
+ - c:
+ d: 1
+ - c:
+ d: 2
+# comment
+ e:
+ - g:
+ e: 1
+ f: 2
+# comment
+''');
+ expect(
+ () => yamlEditor.insertIntoList(
+ ['a', 'b'],
+ i,
+ {
+ 'g': {'e': 3, 'f': 4}
+ }),
+ returnsNormally);
+ });
+ }
});
group('flow list', () {