From 03e5f6f6a83efb91d81e9058c51f14dcc6c31e94 Mon Sep 17 00:00:00 2001 From: Kelvin Kavisi <68240897+kekavc24@users.noreply.github.com> Date: Fri, 31 May 2024 07:36:42 +0100 Subject: [PATCH 1/5] Check for key order only when one key is present --- lib/src/utils.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/src/utils.dart b/lib/src/utils.dart index e5eb697..de45333 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -144,6 +144,11 @@ bool isFlowYamlCollectionNode(Object value) => int getMapInsertionIndex(YamlMap map, Object newKey) { final keys = map.nodes.keys.map((k) => k.toString()).toList(); + // We can't deduce ordering if list is empty, so then we just we just append + if (keys.length <= 1) { + return map.length; + } + for (var i = 1; i < keys.length; i++) { if (keys[i].compareTo(keys[i - 1]) < 0) { return map.length; From 3e565602dcf51915b72df44d3b8c245c9f5987b6 Mon Sep 17 00:00:00 2001 From: Kelvin Kavisi <68240897+kekavc24@users.noreply.github.com> Date: Fri, 31 May 2024 07:37:38 +0100 Subject: [PATCH 2/5] Update tests to match latest fix > Add tests for key order --- test/editor_test.dart | 8 ++++---- test/update_test.dart | 45 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/test/editor_test.dart b/test/editor_test.dart index 880b443..b0a0081 100644 --- a/test/editor_test.dart +++ b/test/editor_test.dart @@ -29,8 +29,8 @@ void main() { expect(yamlEditor.edits, [ SourceEdit(5, 5, " YAML Ain't Markup Language"), - SourceEdit(0, 0, 'XML: Extensible Markup Language\n'), - SourceEdit(32, 32, '') + SourceEdit(32, 0, '\nXML: Extensible Markup Language\n'), + SourceEdit(0, 33, '') ]); }); @@ -48,8 +48,8 @@ void main() { expect(firstEdits, [SourceEdit(5, 5, " YAML Ain't Markup Language")]); expect(yamlEditor.edits, [ SourceEdit(5, 5, " YAML Ain't Markup Language"), - SourceEdit(0, 0, 'XML: Extensible Markup Language\n'), - SourceEdit(32, 32, '') + SourceEdit(32, 0, '\nXML: Extensible Markup Language\n'), + SourceEdit(0, 33, '') ]); }); }); diff --git a/test/update_test.dart b/test/update_test.dart index 8b9b953..01dfde0 100644 --- a/test/update_test.dart +++ b/test/update_test.dart @@ -715,9 +715,12 @@ c: 3 doc.update(['XML'], 'Extensible Markup Language'); expect( - doc.toString(), - equals('{XML: Extensible Markup Language, ' - "YAML: YAML Ain't Markup Language}")); + doc.toString(), + equals( + "{YAML: YAML Ain't Markup Language, " + 'XML: Extensible Markup Language}', + ), + ); expectYamlBuilderValue(doc, { 'XML': 'Extensible Markup Language', 'YAML': "YAML Ain't Markup Language", @@ -876,6 +879,42 @@ d: 4 'a': {'key': {}} }); }); + + test('adds and preserves key order (ascending)', () { + final doc = YamlEditor(''' +a: 1 +b: 2 +c: 3 + + +'''); + + doc.update(['d'], 4); + expect(doc.toString(), equals(''' +a: 1 +b: 2 +c: 3 +d: 4 + + +''')); + }); + + test('adds at the end when no key order is present', () { + final doc = YamlEditor(''' +a: 1 +c: 2 +b: 3 +'''); + + doc.update(['d'], 4); + expect(doc.toString(), equals(''' +a: 1 +c: 2 +b: 3 +d: 4 +''')); + }); }); group('empty starting document', () { From c2d5847a7afc0ecccaf24f95cedfb653185c7119 Mon Sep 17 00:00:00 2001 From: Kelvin Kavisi <68240897+kekavc24@users.noreply.github.com> Date: Fri, 31 May 2024 07:38:43 +0100 Subject: [PATCH 3/5] Add golden tests for fix --- test/testdata/input/ignore_key_order.test | 7 +++++ test/testdata/input/respect_key_order.test | 6 ++++ .../output/allowed_characters_in_keys.golden | 28 +++++++++---------- test/testdata/output/ignore_key_order.golden | 8 ++++++ test/testdata/output/respect_key_order.golden | 6 ++++ 5 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 test/testdata/input/ignore_key_order.test create mode 100644 test/testdata/input/respect_key_order.test create mode 100644 test/testdata/output/ignore_key_order.golden create mode 100644 test/testdata/output/respect_key_order.golden diff --git a/test/testdata/input/ignore_key_order.test b/test/testdata/input/ignore_key_order.test new file mode 100644 index 0000000..e9d6201 --- /dev/null +++ b/test/testdata/input/ignore_key_order.test @@ -0,0 +1,7 @@ +IGNORES KEY ORDER +--- +Z: 1 +D: 2 +F: 3 +--- +- [update, ['A'], 4] \ No newline at end of file diff --git a/test/testdata/input/respect_key_order.test b/test/testdata/input/respect_key_order.test new file mode 100644 index 0000000..dee95f4 --- /dev/null +++ b/test/testdata/input/respect_key_order.test @@ -0,0 +1,6 @@ +RESPECTS THE KEY ORDER +--- +A: first +C: third +--- +- [update, [B], second] \ No newline at end of file diff --git a/test/testdata/output/allowed_characters_in_keys.golden b/test/testdata/output/allowed_characters_in_keys.golden index 4cfa06f..3fd7885 100644 --- a/test/testdata/output/allowed_characters_in_keys.golden +++ b/test/testdata/output/allowed_characters_in_keys.golden @@ -1,26 +1,26 @@ test: test --- -"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe test: test ---- -?foo: safe question mark "a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe -test: test --- -:foo: safe colon -?foo: safe question mark -"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe test: test ---- --foo: safe dash -:foo: safe colon -?foo: safe question mark "a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe -test: test +?foo: safe question mark --- --foo: safe dash -:foo: safe colon +test: test +"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe ?foo: safe question mark +:foo: safe colon +--- +test: test "a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe +?foo: safe question mark +:foo: safe colon +-foo: safe dash +--- test: test +"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe +?foo: safe question mark +:foo: safe colon +-foo: safe dash this is#not: a comment diff --git a/test/testdata/output/ignore_key_order.golden b/test/testdata/output/ignore_key_order.golden new file mode 100644 index 0000000..93cc9df --- /dev/null +++ b/test/testdata/output/ignore_key_order.golden @@ -0,0 +1,8 @@ +Z: 1 +D: 2 +F: 3 +--- +Z: 1 +D: 2 +F: 3 +A: 4 diff --git a/test/testdata/output/respect_key_order.golden b/test/testdata/output/respect_key_order.golden new file mode 100644 index 0000000..a20ecde --- /dev/null +++ b/test/testdata/output/respect_key_order.golden @@ -0,0 +1,6 @@ +A: first +C: third +--- +A: first +B: second +C: third From 8cc8580616c083ca9db04ea3a05fc8a39594b3f0 Mon Sep 17 00:00:00 2001 From: Kelvin Kavisi <68240897+kekavc24@users.noreply.github.com> Date: Fri, 31 May 2024 07:39:25 +0100 Subject: [PATCH 4/5] Skip random test until issue #85 is fixed --- test/random_test.dart | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/test/random_test.dart b/test/random_test.dart index 079fb1c..98e3f8b 100644 --- a/test/random_test.dart +++ b/test/random_test.dart @@ -24,8 +24,10 @@ void main() { const modificationsPerRound = 1000; for (var i = 0; i < roundsOfTesting; i++) { - test('testing with randomly generated modifications: test $i', () { - final editor = YamlEditor(''' + test( + 'testing with randomly generated modifications: test $i', + () { + final editor = YamlEditor(''' name: yaml_edit description: A library for YAML manipulation with comment and whitespace preservation. version: 0.0.1-dev @@ -42,13 +44,15 @@ dev_dependencies: test: ^1.14.4 '''); - for (var j = 0; j < modificationsPerRound; j++) { - expect( - () => generator.performNextModification(editor), - returnsNormally, - ); - } - }); + for (var j = 0; j < modificationsPerRound; j++) { + expect( + () => generator.performNextModification(editor), + returnsNormally, + ); + } + }, + skip: 'Remove once issue #85 is fixed', + ); } } From 1326fa3ff0436c75c27f805f1a178e8b5bd99c3a Mon Sep 17 00:00:00 2001 From: Kelvin Kavisi <68240897+kekavc24@users.noreply.github.com> Date: Fri, 31 May 2024 07:39:56 +0100 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ae3c66..d4b5280 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## 2.2.2-wip + - Suppress warnings previously printed to `stdout` when parsing YAML internally. +- Fix error thrown when inserting duplicate keys to different maps in the same + list. + ([#69](https://github.com/dart-lang/yaml_edit/issues/69)) ## 2.2.1