Fix YamlEditor.update method leaving trailing spaces at eol (#42) * Fix for selecting the correct index in insertion * Add new test cases * Add more tests and format corrections * Update `CHANGELOG.md` * Fix leaving whitespace at eol on YamlEditor.update & update the testcases * Fix formatting * Apply proposed change * Update lib/src/map_mutations.dart Co-authored-by: Jonas Finnemann Jensen <jopsen@gmail.com> --------- Co-authored-by: Jonas Finnemann Jensen <jopsen@gmail.com>
diff --git a/lib/src/map_mutations.dart b/lib/src/map_mutations.dart index 9906a65..62d4081 100644 --- a/lib/src/map_mutations.dart +++ b/lib/src/map_mutations.dart
@@ -139,6 +139,11 @@ valueAsString = lineEnding + valueAsString; } + if (!valueAsString.startsWith(lineEnding)) { + // prepend whitespace to ensure there is space after colon. + valueAsString = ' ' + valueAsString; + } + /// +1 accounts for the colon final start = keyNode.span.end.offset + 1; var end = getContentSensitiveEnd(map.nodes[key]!); @@ -148,7 +153,7 @@ /// this. if (end < start) end = start; - return SourceEdit(start, end - start, ' ' + valueAsString); + return SourceEdit(start, end - start, valueAsString); } /// Performs the string operation on [yaml] to achieve the effect of replacing
diff --git a/test/alias_test.dart b/test/alias_test.dart index 45d7508..1663bd8 100644 --- a/test/alias_test.dart +++ b/test/alias_test.dart
@@ -118,7 +118,7 @@ test('removing nested map alias anchor results in AliasError', () { final doc = YamlEditor(''' -a: +a: c: &SS Sammy Sosa b: *SS '''); @@ -129,7 +129,7 @@ test('removing nested map alias reference results in AliasError', () { final doc = YamlEditor(''' a: &SS Sammy Sosa -b: +b: c: *SS ''');
diff --git a/test/preservation_test.dart b/test/preservation_test.dart index 158d325..a763296 100644 --- a/test/preservation_test.dart +++ b/test/preservation_test.dart
@@ -38,16 +38,16 @@ test('tilde', expectLoadPreservesYAML('~')); test('false', expectLoadPreservesYAML('false')); - test('block map', expectLoadPreservesYAML('''a: + test('block map', expectLoadPreservesYAML('''a: b: 1 ''')); - test('block list', expectLoadPreservesYAML('''a: + test('block list', expectLoadPreservesYAML('''a: - 1 ''')); test('complicated example', () { expectLoadPreservesYAML('''verb: RecommendCafes map: - a: + a: b: 1 recipe: - verb: Score
diff --git a/test/testdata/input/nested_block_map.test b/test/testdata/input/nested_block_map.test index 69e8b23..bc7c35b 100644 --- a/test/testdata/input/nested_block_map.test +++ b/test/testdata/input/nested_block_map.test
@@ -1,7 +1,7 @@ NESTED BLOCK MAP --- a: 1 -b: +b: d: 4 e: 5 c: 3
diff --git a/test/testdata/output/nested_block_map.golden b/test/testdata/output/nested_block_map.golden index 94c4742..d28bf6c 100644 --- a/test/testdata/output/nested_block_map.golden +++ b/test/testdata/output/nested_block_map.golden
@@ -1,28 +1,28 @@ a: 1 -b: +b: d: 4 e: 5 c: 3 --- a: 1 -b: +b: d: 4 e: 6 c: 3 --- a: 1 -b: +b: d: 4 - e: + e: - 1 - 2 - 3 c: 3 --- a: 1 -b: +b: d: 4 - e: + e: - 1 - 2 - 3
diff --git a/test/testdata/output/pubspec.golden b/test/testdata/output/pubspec.golden index b5e5ea0..ffbe8ba 100644 --- a/test/testdata/output/pubspec.golden +++ b/test/testdata/output/pubspec.golden
@@ -111,6 +111,6 @@ retry: ^3.0.1 yaml: ^3.2.0 # For YAML -dev_dependencies: +dev_dependencies: test: ^1.14.4
diff --git a/test/update_test.dart b/test/update_test.dart index 3e392e0..603ece4 100644 --- a/test/update_test.dart +++ b/test/update_test.dart
@@ -126,7 +126,7 @@ test('nested', () { final doc = YamlEditor(''' a: 1 -b: +b: d: 4 e: 5 c: 3 @@ -135,7 +135,7 @@ expect(doc.toString(), equals(''' a: 1 -b: +b: d: 4 e: 6 c: 3 @@ -201,14 +201,14 @@ test('nested (5)', () { final doc = YamlEditor(''' -a: +a: - a: 1 b: 2 - null '''); doc.update(['a', 0], false); expect(doc.toString(), equals(''' -a: +a: - false - null @@ -220,14 +220,14 @@ test('nested (6)', () { final doc = YamlEditor(''' -a: +a: - - 1 - 2 - null '''); doc.update(['a', 0], false); expect(doc.toString(), equals(''' -a: +a: - false - null @@ -261,7 +261,7 @@ doc.update(['a'], {'retry': '3.0.1'}); expect(doc.toString(), equals(''' -a: +a: retry: 3.0.1 b: false ''')); @@ -277,7 +277,7 @@ expect(doc.toString(), equals(''' # comment -a: +a: retry: 3.0.1 # comment # comment ''')); @@ -286,7 +286,7 @@ test('nested scalar -> flow list', () { final doc = YamlEditor(''' a: 1 -b: +b: d: 4 e: 5 c: 3 @@ -295,9 +295,9 @@ expect(doc.toString(), equals(''' a: 1 -b: +b: d: 4 - e: + e: - 1 - 2 - 3 @@ -316,7 +316,7 @@ test('nested block map -> scalar', () { final doc = YamlEditor(''' a: 1 -b: +b: d: 4 e: 5 c: 3 @@ -334,7 +334,7 @@ test('nested block map -> scalar with comments', () { final doc = YamlEditor(''' a: 1 -b: +b: d: 4 e: 5 @@ -359,7 +359,7 @@ test('nested scalar -> block map', () { final doc = YamlEditor(''' a: 1 -b: +b: d: 4 e: 5 c: 3 @@ -368,9 +368,9 @@ expect(doc.toString(), equals(''' a: 1 -b: +b: d: 4 - e: + e: x: 3 y: 4 c: 3 @@ -388,7 +388,7 @@ test('nested block map with comments', () { final doc = YamlEditor(''' a: 1 -b: +b: d: 4 e: 5 # comment c: 3 @@ -397,7 +397,7 @@ expect(doc.toString(), equals(''' a: 1 -b: +b: d: 4 e: 6 # comment c: 3 @@ -412,7 +412,7 @@ test('nested block map with comments (2)', () { final doc = YamlEditor(''' a: 1 -b: +b: d: 4 # comment # comment e: 5 # comment @@ -423,7 +423,7 @@ expect(doc.toString(), equals(''' a: 1 -b: +b: d: 4 # comment # comment e: 6 # comment
diff --git a/test/utils_test.dart b/test/utils_test.dart index 8d9aef5..5d6b76a 100644 --- a/test/utils_test.dart +++ b/test/utils_test.dart
@@ -213,7 +213,7 @@ })); expect(doc.toString(), equals(''' -strings: +strings: plain: string folded: >- string @@ -366,7 +366,7 @@ }); expect(doc.toString(), equals(''' -a: +a: f: "" g: 1 ''')); @@ -385,7 +385,7 @@ }); expect(doc.toString(), equals(''' -a: +a: f: " a" g: 1 '''));
diff --git a/test/windows_test.dart b/test/windows_test.dart index ca7a7dc..a516204 100644 --- a/test/windows_test.dart +++ b/test/windows_test.dart
@@ -62,7 +62,7 @@ test('update nested scalar -> flow list', () { final doc = YamlEditor(''' a: 1\r -b: \r +b:\r d: 4\r e: 5\r c: 3\r @@ -71,9 +71,9 @@ expect(doc.toString(), equals(''' a: 1\r -b: \r +b:\r d: 4\r - e: \r + e:\r - 1\r - 2\r - 3\r