Skip to content

Commit 2127595

Browse files
committed
Add tests for the indexOfCompact utility function
1 parent a10c2b9 commit 2127595

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

pkgs/yaml_edit/test/utils_test.dart

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

0 commit comments

Comments
 (0)