Skip to content

Commit 9fbef94

Browse files
authored
Fix for issue dart-lang/yaml_edit#23 (dart-archive/yaml_edit#34)
* Fix for selecting the correct index in insertion * Add new test cases * Add more tests and format corrections
1 parent c85d298 commit 9fbef94

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

pkgs/yaml_edit/lib/src/list_mutations.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ SourceEdit _appendToBlockList(
117117
// Adjusts offset to after the trailing newline of the last entry, if it exists
118118
if (list.isNotEmpty) {
119119
final lastValueSpanEnd = list.nodes.last.span.end.offset;
120-
final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd);
120+
final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd - 1);
121121
if (nextNewLineIndex == -1) {
122122
formattedValue = getLineEnding(yaml) + formattedValue;
123123
} else {

pkgs/yaml_edit/test/append_test.dart

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,99 @@ a:
149149
2: null
150150
'''));
151151
});
152+
153+
test('block append (1)', () {
154+
final yamlEditor = YamlEditor('''
155+
# comment
156+
- z:
157+
x: 1
158+
y: 2
159+
- z:
160+
x: 3
161+
y: 4
162+
''');
163+
yamlEditor.appendToList([], {
164+
'z': {'x': 5, 'y': 6}
165+
});
166+
167+
expect(yamlEditor.toString(), equals('''
168+
# comment
169+
- z:
170+
x: 1
171+
y: 2
172+
- z:
173+
x: 3
174+
y: 4
175+
- z:
176+
x: 5
177+
y: 6
178+
'''));
179+
});
180+
181+
test('block append (2)', () {
182+
final yamlEditor = YamlEditor('''
183+
# comment
184+
a:
185+
- z:
186+
x: 1
187+
y: 2
188+
- z:
189+
x: 3
190+
y: 4
191+
b:
192+
- w:
193+
m: 2
194+
n: 4
195+
''');
196+
yamlEditor.appendToList([
197+
'a'
198+
], {
199+
'z': {'x': 5, 'y': 6}
200+
});
201+
202+
expect(yamlEditor.toString(), equals('''
203+
# comment
204+
a:
205+
- z:
206+
x: 1
207+
y: 2
208+
- z:
209+
x: 3
210+
y: 4
211+
- z:
212+
x: 5
213+
y: 6
214+
b:
215+
- w:
216+
m: 2
217+
n: 4
218+
'''));
219+
});
220+
221+
test('block append nested and with comments', () {
222+
final yamlEditor = YamlEditor('''
223+
a:
224+
b:
225+
- c:
226+
d: 1
227+
- c:
228+
d: 2
229+
# comment
230+
e:
231+
- g:
232+
e: 1
233+
f: 2
234+
# comment
235+
''');
236+
expect(
237+
() => yamlEditor.appendToList([
238+
'a',
239+
'e'
240+
], {
241+
'g': {'e': 3, 'f': 4}
242+
}),
243+
returnsNormally);
244+
});
152245
});
153246

154247
group('flow list', () {

pkgs/yaml_edit/test/insert_test.dart

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,81 @@ void main() {
9898
'''));
9999
expectYamlBuilderValue(doc, [0, 1, 2]);
100100
});
101+
102+
for (var i = 0; i < 3; i++) {
103+
test('block insert(1) at $i', () {
104+
final yamlEditor = YamlEditor('''
105+
# comment
106+
- z:
107+
x: 1
108+
y: 2
109+
- z:
110+
x: 3
111+
y: 4
112+
''');
113+
expect(
114+
() => yamlEditor.insertIntoList(
115+
[],
116+
i,
117+
{
118+
'z': {'x': 5, 'y': 6}
119+
}),
120+
returnsNormally);
121+
});
122+
}
123+
124+
for (var i = 0; i < 3; i++) {
125+
test('block insert(2) at $i', () {
126+
final yamlEditor = YamlEditor('''
127+
a:
128+
- z:
129+
x: 1
130+
y: 2
131+
- z:
132+
x: 3
133+
y: 4
134+
b:
135+
- w:
136+
m: 2
137+
n: 4
138+
''');
139+
expect(
140+
() => yamlEditor.insertIntoList(
141+
['a'],
142+
i,
143+
{
144+
'z': {'x': 5, 'y': 6}
145+
}),
146+
returnsNormally);
147+
});
148+
}
149+
150+
for (var i = 0; i < 2; i++) {
151+
test('block insert nested and with comments at $i', () {
152+
final yamlEditor = YamlEditor('''
153+
a:
154+
b:
155+
- c:
156+
d: 1
157+
- c:
158+
d: 2
159+
# comment
160+
e:
161+
- g:
162+
e: 1
163+
f: 2
164+
# comment
165+
''');
166+
expect(
167+
() => yamlEditor.insertIntoList(
168+
['a', 'b'],
169+
i,
170+
{
171+
'g': {'e': 3, 'f': 4}
172+
}),
173+
returnsNormally);
174+
});
175+
}
101176
});
102177

103178
group('flow list', () {

0 commit comments

Comments
 (0)