PureYAML/Tests/ParsingMultilineScalarTests.swift
Mihaela M d90a091ab0 fix(parsing): fold a plain scalar value that begins on its own line
A multi-line plain scalar whose value sits one line below its mapping key
stopped after the first line: the continuation check compared the next
line's indent against the value's own indent, which had been pushed onto
the indentation stack, so a continuation at the same indent failed the
strict '>'. It now compares against the parent block's indent (the level
below the value's, when the scalar begins exactly at the indentation top),
matching the spec: a continuation must exceed the mapping's indentation,
not the value's own. Inline values are unaffected, since their top-of-
stack already is the parent block's indent.

Measured against the official YAML test-suite, parse-level conformance
rises from 211/333 (63.4%) to 221/333 (66.4%) with no regression across
the existing 306 tests. The +10 covers the multi-line plain scalar family
the harness flagged, not just the case investigated.
2026-06-26 18:52:09 +02:00

288 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
""",
))
// A literal block scalar preserves the blank line between the heading and
// the list (YAML 1.2 line 8.1.1.1); clip keeps a single trailing newline.
#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 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))
}
@Test("Folds a plain scalar value that begins on its own line under a key")
func test_plainScalarValueOnOwnLineFolds() throws {
// The value sits one line below the key and spans two lines at the same indent. Its
// continuation lines are indented more than the mapping (not more than the value's own
// first line), so both must fold into one scalar.
let root = try requireMapping(PureYAML.parse(
"""
plain:
This unquoted scalar
spans many lines.
""",
))
#expect(root?["plain"] == .string("This unquoted scalar spans many lines."))
}
@Test("Folds a multi-line plain scalar nested two levels deep")
func test_plainScalarValueOnOwnLineFoldsWhenNested() throws {
let root = try requireMapping(PureYAML.parse(
"""
outer:
inner:
first part
second part
""",
))
let inner = root?.mapping("outer")
#expect(inner?["inner"] == .string("first part second part"))
}
}