Skip to content

Commit db86a0c

Browse files
committed
Add tests
1 parent db122d5 commit db86a0c

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

pkgs/yaml_edit/test/remove_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ key: >+
246246
247247
target-key: value # Comment
248248
# Comment
249+
# Indented, removed
250+
251+
# Not indented, kept
249252
next: value
250253
''');
251254

@@ -254,6 +257,7 @@ next: value
254257
key: >+
255258
folded with keep chomping
256259
260+
# Not indented, kept
257261
next: value
258262
''');
259263
});

pkgs/yaml_edit/test/utils_test.dart

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,183 @@ a:
445445
expect(() => assertValidScalar([1]), throwsArgumentError);
446446
});
447447
});
448+
449+
group('compact char test', () {
450+
test(
451+
'Returns a valid index of the character used to declare block node'
452+
' in compact-inline notation for an explicit key',
453+
() {
454+
const yaml = '''
455+
? - block
456+
- sequence
457+
458+
? key: value
459+
another: value
460+
''';
461+
462+
final mapKeys = (loadYamlNode(
463+
yaml,
464+
) as YamlMap)
465+
.nodes
466+
.keys
467+
.cast<YamlNode>()
468+
.toList();
469+
470+
// Compact block lists
471+
expect(
472+
indexOfCompactChar(
473+
yaml,
474+
mapKeys.first.span.start.offset,
475+
),
476+
equals((compactCharOffset: 0, lineEndingIndex: -1)),
477+
);
478+
479+
// Compact block maps
480+
expect(
481+
indexOfCompactChar(
482+
yaml,
483+
mapKeys[1].span.start.offset,
484+
),
485+
486+
// Skip first "?"
487+
equals(
488+
(compactCharOffset: yaml.indexOf('?', 1), lineEndingIndex: -1),
489+
),
490+
);
491+
},
492+
);
493+
494+
test(
495+
'Returns a valid index of the character used to declare block node'
496+
' in compact-inline notation for an explicit value',
497+
() {
498+
/// A key/value is always implicit unless an explicit key is seen.
499+
///
500+
/// See paragraph after example 8.17 in Block Mappings section.
501+
const yaml = '''
502+
? key
503+
: - block
504+
- sequence
505+
506+
? another
507+
: explict: block
508+
value: node
509+
''';
510+
511+
final values = (loadYamlNode(
512+
yaml,
513+
) as YamlMap)
514+
.nodes
515+
.values
516+
.toList();
517+
518+
final firstExplicitValueChar = yaml.indexOf(':');
519+
520+
// Compact block lists
521+
expect(
522+
indexOfCompactChar(yaml, values.first.span.start.offset),
523+
equals(
524+
(compactCharOffset: firstExplicitValueChar, lineEndingIndex: -1),
525+
),
526+
);
527+
528+
// Compact block maps
529+
expect(
530+
indexOfCompactChar(yaml, values[1].span.start.offset),
531+
532+
// Skip first ":"
533+
equals(
534+
(
535+
compactCharOffset: yaml.indexOf(':', firstExplicitValueChar + 1),
536+
lineEndingIndex: -1
537+
),
538+
),
539+
);
540+
},
541+
);
542+
543+
test(
544+
'Returns a valid index of the character used to declare block node'
545+
' in compact-inline notation for a block sequence',
546+
() {
547+
const yaml = '''
548+
- - block
549+
- sequence
550+
551+
- key: value
552+
another: value
553+
''';
554+
555+
final elements = (loadYamlNode(
556+
yaml,
557+
) as YamlList)
558+
.nodes;
559+
560+
// Compact block lists
561+
expect(
562+
indexOfCompactChar(yaml, elements.first.span.start.offset),
563+
equals((compactCharOffset: 0, lineEndingIndex: -1)),
564+
);
565+
566+
// Compact block maps
567+
expect(
568+
indexOfCompactChar(yaml, elements[1].span.start.offset),
569+
570+
// Skip first "?"
571+
equals(
572+
(compactCharOffset: yaml.lastIndexOf('-'), lineEndingIndex: -1),
573+
),
574+
);
575+
},
576+
);
577+
578+
test('Returns index of line break when not compact', () {
579+
const yaml = '''
580+
-
581+
- not compact
582+
-
583+
?
584+
- key not compact
585+
:
586+
- not compact
587+
''';
588+
589+
final list = (loadYamlNode(yaml) as YamlList).nodes;
590+
591+
// First nested block sequence is not compact
592+
expect(
593+
indexOfCompactChar(yaml, list[0].span.start.offset),
594+
equals((compactCharOffset: -1, lineEndingIndex: yaml.indexOf('\n'))),
595+
);
596+
597+
// Explicit key & value not compact.
598+
final map = list[1] as YamlMap;
599+
600+
final entries = map.nodes;
601+
602+
expect(
603+
indexOfCompactChar(
604+
yaml,
605+
entries.keys.cast<YamlNode>().first.span.start.offset,
606+
),
607+
equals((
608+
compactCharOffset: -1,
609+
lineEndingIndex: yaml.indexOf('\n', map.span.start.offset)
610+
)),
611+
);
612+
613+
expect(
614+
indexOfCompactChar(yaml, entries.values.first.span.start.offset),
615+
equals(
616+
(
617+
compactCharOffset: -1,
618+
lineEndingIndex: yaml.indexOf(
619+
'\n',
620+
yaml.indexOf(':', map.span.start.offset),
621+
)
622+
),
623+
),
624+
);
625+
});
626+
});
448627
}

0 commit comments

Comments
 (0)