Skip to content

Commit e149b01

Browse files
authored
Merge pull request #2236 from ahoppen/ahoppen/assertion-documentation
Add some more documentaiton on `assertParse`
2 parents 013a9fd + 1c852df commit e149b01

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxValidationFile.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead
162162
assertionFailure("""
163163
Error validating child at index \(index) of \(nodeKind):
164164
\(error.description)
165+
166+
See "RawSyntax Validation" in CONTRIBUTING.md to reproduce the failure locally.
165167
""", file: file, line: line)
166168
}
167169
}
@@ -179,6 +181,8 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead
179181
Node did not satisfy any node choice requirement.
180182
Validation failures:
181183
\(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))
184+
185+
See "RawSyntax Validation" in CONTRIBUTING.md to reproduce the failure locally.
182186
""", file: file, line: line)
183187
_ = 1
184188
}

Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
184184
assertionFailure("""
185185
Error validating child at index \(index) of \(nodeKind):
186186
\(error.description)
187+
188+
See "RawSyntax Validation" in CONTRIBUTING.md to reproduce the failure locally.
187189
""", file: file, line: line)
188190
}
189191
}
@@ -198,6 +200,8 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
198200
Node did not satisfy any node choice requirement.
199201
Validation failures:
200202
\(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n"))
203+
204+
See "RawSyntax Validation" in CONTRIBUTING.md to reproduce the failure locally.
201205
""", file: file, line: line)
202206
_ = 1
203207
}

Tests/SwiftParserTest/Assertions.swift

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ struct NoteSpec {
233233

234234
/// An abstract data structure to describe how a diagnostic produced by the parser should look like.
235235
struct DiagnosticSpec {
236-
/// The name of a maker (of the form `1️⃣`) in the source code that marks the location where the diagnostis should be produced.
236+
/// The name of a marker (of the form `1️⃣`) in the source code that marks the location where the diagnostic should be produced.
237237
let locationMarker: String
238238
/// If not `nil`, assert that the diagnostic has the given ID.
239239
let id: MessageID?
@@ -549,8 +549,11 @@ extension ParserTestCase {
549549
assertStringsEqualWithDiff(
550550
"\(mutatedTree)",
551551
mutatedSource,
552-
"A mutation of the original test case failed to round-trip",
552+
"A mutation of the original test case failed to round-trip.",
553553
additionalInfo: """
554+
To debug the failure, add a new `assertParse` test with the following source as input:
555+
\(mutatedSource)
556+
554557
Parsed syntax tree of mutation:
555558
\(mutatedTree.debugDescription)
556559
""",
@@ -560,23 +563,42 @@ extension ParserTestCase {
560563
}
561564
}
562565

563-
/// Removes any test markers from `markedSource` (1) and parses the result
564-
/// using `parse`. By default it only checks if the parsed syntax tree is
565-
/// printable back to the origin source, ie. it round trips.
566+
/// Verifies that parsing of `markedSource` produces expected results using a
567+
/// combination of various testing techniques:
568+
///
569+
/// 1. Asserts that parsing of `markedSource` round-trips, ie. that the printed
570+
/// parsed tree is the same as the input.
571+
/// 2. Checks that parsing produces the expected list of diagnostics. If no
572+
/// diagnostics are passed, asserts that the input parses without any errors.
573+
/// 3. Checks that applying all Fix-Its of the source code results in the
574+
/// expected fixed source, effectively testing the Fix-Its.
575+
/// 4. If a substructure is passed, asserts that the parsed tree contains a
576+
/// subtree of that structure.
577+
/// 5. Mutates the test input by flipping each token's presence (ie. for every
578+
/// token, either remove it from the input if it is present in the parsed
579+
/// tree or synthesize it if it was missing) and verifies that this
580+
/// mutated input round-trips. This test is disabled if the
581+
/// `SKIP_LONG_TESTS` environment variable is set.
582+
/// 6. If swift-syntax is compiled with the
583+
/// `SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION` environment variable
584+
/// set, mutates the input based on tokens the parse is trying to parse.
585+
/// See the *Test Case Mutation* section in CONTRIBUTING.md.
566586
///
567-
/// (1) `markedSource` is source that can include markers of the form `1️⃣`,
568-
/// to be used as locations in the following parameters when they exist.
569587
///
570588
/// - Parameters:
571-
/// - substructure: Asserts the parsed syntax tree contains this structure.
589+
/// - markedSource: source that can include markers of the form `1️⃣`,
590+
/// to be used as locations in the following parameters.
591+
/// - parse: The function with which the source code should be parsed.
592+
/// Defaults to parsing as a source file.
593+
/// - expectedSubstructure: Asserts the parsed syntax tree contains this structure.
572594
/// - substructureAfterMarker: Changes the position to start the structure
573595
/// assertion from, ie. allows matching a particular substructure rather
574596
/// than the whole source.
575-
/// - diagnostics: Asserts the given diagnostics were output, by default it
597+
/// - expectedDiagnostics: Asserts the given diagnostics were output, by default it
576598
/// asserts the parse was successful (ie. it has no diagnostics). Note
577599
/// that `DiagnosticsSpec` uses the location marked by `1️⃣` by default.
578600
/// - applyFixIts: Applies only the fix-its with these messages.
579-
/// - fixedSource: Asserts that the source after applying fix-its matches
601+
/// - expectedFixedSource: Asserts that the source after applying fix-its matches
580602
/// this string.
581603
/// - experimentalFeatures: A list of experimental features to enable, or
582604
/// `nil` to enable the default set of features provided by the test case.

0 commit comments

Comments
 (0)