PureYAML/Tests/ParsingMultilineScalarTests.swift
Mihaela Mihaljevic 1875d66d46 fix(parsing): parse multi-line and block scalars at the document root
Model libyaml's parser->indent (the enclosing block-collection column, -1 at the bare root) separately from per-line indentation, via a blockIndent stack that rolls on block-sequence entries and mapping keys, unrolls at each block-line start, and resets at document boundaries. Plain-scalar continuation folding now compares against blockIndent, so a root scalar folds its column-0 continuations (single break to space, blank line to newline, per-line whitespace stripped) while a scalar inside a collection still ends at a sibling line. The ':' value indicator deliberately does not roll the block indent.

A '---' document-start marker may now be followed by the document's root node on the same line ('--- foo', '--- [1, 2]', '--- |', '--- >'), and a block scalar whose body sits at the document root's column 0 is captured and ends at the next '---'/'...' marker.

Flips 33 official YAML test suite cases (338/402): 18 false-rejects and 12 value mismatches now match, and 3 invalid inputs (9KBC, ZVH3, 9MQT/01) are now correctly rejected. M7A3 and NB6Z now parse but with mismatched values (separate multi-doc and tab-folding gaps) and stay tracked. Adds root multi-line plain scalar regression tests; updates manifest, fixtures, counts, and roadmap.
2026-06-23 22:56:33 +02:00

282 lines
12 KiB
Swift

@testable import PureYAML
import Testing
@Suite("Parsing Multiline Scalars")
struct ParsingMultilineScalarTests {
@Test("Parses multiline plain scalars in mappings")
func test_multilinePlainScalarsInMappings() throws {
let root = try requireMapping(PureYAML.parse(
"""
columns:
- description: CreationTimestamp is a timestamp representing the server time when
this object was created. It is not guaranteed to be set in happens-before
order across separate operations.
jsonPath: .metadata.creationTimestamp
""",
))
let expectedDescription = [
"CreationTimestamp is a timestamp representing the server time when this object was created.",
"It is not guaranteed to be set in happens-before order across separate operations.",
].joined(separator: " ")
let column = root?.sequence("columns")?.first?.mapping
#expect(column?["description"] == .string(expectedDescription))
#expect(column?["jsonPath"] == .string(".metadata.creationTimestamp"))
#expect(column?["missing"] == nil)
}
@Test("Parses multiline plain scalars before the next sequence entry")
func test_multilinePlainScalarsBeforeNextSequenceEntry() throws {
let value = try PureYAML.parse(
"""
- name: code-quality
description: Insights into reliability, maintainability, and efficiency of your
codebase.
- name: codespaces
description: Endpoints to manage Codespaces using the REST API.
""",
)
let tags = requireSequence(value)
let first = tags?.first?.mapping
let second = tags?.dropFirst().first?.mapping
#expect(first?["name"] == .string("code-quality"))
#expect(first?["description"] == .string("Insights into reliability, maintainability, and efficiency of your codebase."))
#expect(second?["name"] == .string("codespaces"))
#expect(second?["description"] == .string("Endpoints to manage Codespaces using the REST API."))
#expect(first?["missing"] == nil)
}
@Test("Parses multiline plain scalar sequence items")
func test_multilinePlainScalarSequenceItems() throws {
let root = try requireMapping(PureYAML.parse(
"""
examples:
- Updated instructions to help the team with planning and coordination
tasks
next: done
""",
))
#expect(root?.sequence("examples") == [
.string("Updated instructions to help the team with planning and coordination tasks"),
])
#expect(root?["next"] == .string("done"))
#expect(root?["missing"] == nil)
}
@Test("Parses multiline plain scalar continuations that start with quotes")
func test_multilinePlainScalarContinuationsThatStartWithQuotes() throws {
let root = try requireMapping(PureYAML.parse(
"""
get:
description: Gets a team using the team's slug. To create the slug, GitHub replaces
special characters in the name string, changes all words to lowercase, and
replaces spaces with a `-` separator and adds the "ent:" prefix. For example,
"My TEam Näme" would become `ent:my-team-name`.
tags:
- enterprise-teams
""",
))
let expectedDescription = [
"Gets a team using the team's slug.",
"To create the slug, GitHub replaces special characters in the name string,",
"changes all words to lowercase, and replaces spaces with a `-` separator",
"and adds the \"ent:\" prefix. For example,",
"\"My TEam Näme\" would become `ent:my-team-name`.",
].joined(separator: " ")
let get = root?.mapping("get")
#expect(get?["description"] == .string(expectedDescription))
#expect(get?.sequence("tags") == [.string("enterprise-teams")])
#expect(get?["missing"] == nil)
}
@Test("Parses multiline plain scalar continuations that start with flow markers")
func test_multilinePlainScalarContinuationsThatStartWithFlowMarkers() throws {
let root = try requireMapping(PureYAML.parse(
"""
selected_repository_ids:
description: An array of repository ids that can access the organization
secret. You can manage the list of
selected repositories using the [List selected repositories for
an organization secret](https://docs.example.com/list),
[Set selected repositories for an organization secret](https://docs.example.com/set),
and [Remove selected repository from an organization secret](https://docs.example.com/remove)
endpoints.
items:
type: integer
""",
))
let expectedDescription = [
"An array of repository ids that can access the organization secret.",
"You can manage the list of selected repositories using the",
"[List selected repositories for an organization secret](https://docs.example.com/list),",
"[Set selected repositories for an organization secret](https://docs.example.com/set),",
"and [Remove selected repository from an organization secret](https://docs.example.com/remove) endpoints.",
].joined(separator: " ")
let selected = root?.mapping("selected_repository_ids")
#expect(selected?["description"] == .string(expectedDescription))
#expect(selected?.mapping("items")?["type"] == .string("integer"))
#expect(selected?["missing"] == nil)
}
@Test("Parses flow-marker continuations after previous sequence indentation")
func test_flowMarkerContinuationsAfterPreviousSequenceIndentation() throws {
let root = try requireMapping(PureYAML.parse(
"""
schema:
enum:
- all
- private
selected_repository_ids:
description: An array of repository ids that can access the organization
[Set selected repositories](https://docs.example.com/set),
and more.
items:
type: integer
""",
))
let selected = root?.mapping("schema")?.mapping("selected_repository_ids")
#expect(
selected?["description"] ==
.string("An array of repository ids that can access the organization [Set selected repositories](https://docs.example.com/set), and more."),
)
#expect(selected?.mapping("items")?["type"] == .string("integer"))
#expect(selected?["missing"] == nil)
#expect(root?.mapping("schema")?.sequence("enum") == [.string("all"), .string("private")])
}
@Test("Parses colon-style markup inside multiline plain scalars")
func test_colonStyleMarkupInsideMultilinePlainScalars() throws {
let root = try requireMapping(PureYAML.parse(
"""
label:
description: The name of the label. Emoji can be added to label
names, using either native emoji or colon-style markup. For example,
typing `:strawberry:` will render the emoji ![:strawberry:](https://example.com/1f353.png
":strawberry:"). For a full list of available emoji and codes.
color: f29513
""",
))
let expectedDescription = [
"The name of the label. Emoji can be added to label names,",
"using either native emoji or colon-style markup. For example,",
"typing `:strawberry:` will render the emoji",
"![:strawberry:](https://example.com/1f353.png \":strawberry:\").",
"For a full list of available emoji and codes.",
].joined(separator: " ")
#expect(root?.mapping("label")?["description"] == .string(expectedDescription))
#expect(root?.mapping("label")?["color"] == .string("f29513"))
#expect(root?.mapping("label")?["missing"] == nil)
}
@Test("Parses block scalar text that starts with YAML indicators")
func test_blockScalarTextThatStartsWithYAMLIndicators() throws {
let root = try requireMapping(PureYAML.parse(
"""
description: |
**Fine-grained access tokens**
* [GitHub App user access tokens](https://docs.github.com)
- dash list item stays text
""",
))
#expect(root?["description"] == .string(
"""
**Fine-grained access tokens**
* [GitHub App user access tokens](https://docs.github.com)
- dash list item stays text
""",
))
}
@Test("Block scalars keep a leading '#' as literal content, not a comment")
func test_blockScalarLeadingHashIsLiteralContent() throws {
let root = try requireMapping(PureYAML.parse(
"""
folded:
$ref: >-
#/components/schemas/foo
literal:
$ref: |-
#/components/schemas/bar
""",
))
#expect(root?.mapping("folded")?["$ref"] == .string("#/components/schemas/foo"))
#expect(root?.mapping("literal")?["$ref"] == .string("#/components/schemas/bar"))
}
@Test("A leading '#' is preserved across every line of a block scalar")
func test_blockScalarLeadingHashAcrossMultipleLines() throws {
let root = try requireMapping(PureYAML.parse(
"""
doc: |-
#/components/schemas/foo
plain line
#/components/schemas/bar
after: tail
""",
))
#expect(root?["doc"] == .string(
"""
#/components/schemas/foo
plain line
#/components/schemas/bar
""",
))
// The block scalar ends at the dedent; the following key is unaffected.
#expect(root?["after"] == .string("tail"))
}
@Test("A bare multi-line plain scalar at the document root folds")
func test_rootMultilinePlainScalarFolds() throws {
// YAML test suite 9YRD: a plain scalar is the document's root node and spans
// several lines. A single line break folds to a space, a blank line folds to
// a newline, and per-line leading/trailing whitespace is stripped.
#expect(try PureYAML.parse("a\nb \n c\nd\n\ne\n") == .string("a b c d\ne"))
// EX5H: the same content after an explicit `---` document-start marker.
#expect(try PureYAML.parse("---\na\nb \n c\nd\n\ne\n") == .string("a b c d\ne"))
// 82AN: `---word1` is plain content (the `---` is not a marker because it is
// not followed by a space), and the scalar continues onto the next line.
#expect(try PureYAML.parse("---word1\nword2\n") == .string("---word1 word2"))
}
@Test("A multi-line plain scalar as a mapping value at root folds")
func test_rootMappingValueMultilinePlainScalarFolds() throws {
// YAML test suite 4CQQ: the value indented on its own lines folds even though
// each continuation line sits at the same column as the first content line.
let root = try requireMapping(PureYAML.parse(
"""
plain:
This unquoted scalar
spans many lines.
""",
))
#expect(root?["plain"] == .string("This unquoted scalar spans many lines."))
}
@Test("A genuine trailing comment after a plain value is still ignored")
func test_plainValueTrailingCommentStillIgnored() throws {
let root = try requireMapping(PureYAML.parse(
"""
kind: widget # this is a comment
# whole-line comment
count: 3
""",
))
#expect(root?["kind"] == .string("widget"))
#expect(root?["count"] == .int(3))
}
}