Skip to content

Commit b081d9d

Browse files
committed
Add some more documentaiton on assertParse
1 parent e8e50f8 commit b081d9d

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

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 fialure 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 fialure locally.
201205
""", file: file, line: line)
202206
_ = 1
203207
}

Tests/SwiftParserTest/Assertions.swift

Lines changed: 31 additions & 12 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 maker (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?
@@ -531,7 +531,7 @@ public struct AssertParseOptions: OptionSet {
531531
extension ParserTestCase {
532532
/// After a test case has been mutated, assert that the mutated source
533533
/// round-trips and doesn’t hit any assertion failures in the parser.
534-
fileprivate func assertRoundTrip<S: SyntaxProtocol>(
534+
fileprivate func assertMutatedSourceRoundTrips<S: SyntaxProtocol>(
535535
source: [UInt8],
536536
_ parse: (inout Parser) -> S,
537537
experimentalFeatures: Parser.ExperimentalFeatures,
@@ -564,23 +564,42 @@ extension ParserTestCase {
564564
}
565565
}
566566

567-
/// Removes any test markers from `markedSource` (1) and parses the result
568-
/// using `parse`. By default it only checks if the parsed syntax tree is
569-
/// printable back to the origin source, ie. it round trips.
567+
/// Verifies that parsing of `markedSource` produces expected results using a
568+
/// combination of various testing techniques:
569+
///
570+
/// 1. Asserts that parsing of `markedSource` round-trips, ie. that the printed
571+
/// parsed tree is the same as the input.
572+
/// 2. Checks that parsing produces the expected list of diagnostics. If no
573+
/// diagnostics are passed, asserts that the input parses without any errors.
574+
/// 3. Checks that applying all Fix-Its of the source code results in the
575+
/// expected fixed source, effectively testing the Fix-Its.
576+
/// 4. If a substructure is passed, asserts that the parsed tree contains a
577+
/// subtree of that structure.
578+
/// 5. Mutates the test input by flipping each token's presence (ie. for every
579+
/// token, remove it from the input if it is present in the parsed tree and
580+
/// synthesize a missing tokens into the input) and verifies that this
581+
/// mutated input round-trips. This test is disabled if the
582+
/// `SKIP_LONG_TESTS` environment variable is set.
583+
/// 6. If swift-syntax is compiled with the
584+
/// `SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION` environment variable
585+
/// set, mutates the input based on tokens the parse is trying to parse.
586+
/// See the *Test Case Mutation* section in CONTRIBUTING.md.
570587
///
571-
/// (1) `markedSource` is source that can include markers of the form `1️⃣`,
572-
/// to be used as locations in the following parameters when they exist.
573588
///
574589
/// - Parameters:
575-
/// - substructure: Asserts the parsed syntax tree contains this structure.
590+
/// - markedSource: source that can include markers of the form `1️⃣`,
591+
/// to be used as locations in the following parameters.
592+
/// - parse: The function with which the source code should be parsed.
593+
/// Defaults to parsing as a source file.
594+
/// - expectedSubstructure: Asserts the parsed syntax tree contains this structure.
576595
/// - substructureAfterMarker: Changes the position to start the structure
577596
/// assertion from, ie. allows matching a particular substructure rather
578597
/// than the whole source.
579-
/// - diagnostics: Asserts the given diagnostics were output, by default it
598+
/// - expectedDiagnostics: Asserts the given diagnostics were output, by default it
580599
/// asserts the parse was successful (ie. it has no diagnostics). Note
581600
/// that `DiagnosticsSpec` uses the location marked by `1️⃣` by default.
582601
/// - applyFixIts: Applies only the fix-its with these messages.
583-
/// - fixedSource: Asserts that the source after applying fix-its matches
602+
/// - expectedFixedSource: Asserts that the source after applying fix-its matches
584603
/// this string.
585604
/// - experimentalFeatures: A list of experimental features to enable, or
586605
/// `nil` to enable the default set of features provided by the test case.
@@ -693,7 +712,7 @@ extension ParserTestCase {
693712
DispatchQueue.concurrentPerform(iterations: Array(tree.tokens(viewMode: .all)).count) { tokenIndex in
694713
let flippedTokenTree = TokenPresenceFlipper(flipTokenAtIndex: tokenIndex).rewrite(Syntax(tree))
695714
_ = ParseDiagnosticsGenerator.diagnostics(for: flippedTokenTree)
696-
assertRoundTrip(source: flippedTokenTree.syntaxTextBytes, parse, experimentalFeatures: experimentalFeatures, file: file, line: line)
715+
assertMutatedSourceRoundTrips(source: flippedTokenTree.syntaxTextBytes, parse, experimentalFeatures: experimentalFeatures, file: file, line: line)
697716
}
698717

699718
#if SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION
@@ -703,7 +722,7 @@ extension ParserTestCase {
703722
DispatchQueue.concurrentPerform(iterations: mutations.count) { index in
704723
let mutation = mutations[index]
705724
let alternateSource = MutatedTreePrinter.print(tree: Syntax(tree), mutations: [mutation.offset: mutation.replacement])
706-
assertRoundTrip(source: alternateSource, parse, experimentalFeatures: experimentalFeatures, file: file, line: line)
725+
assertMutatedSourceRoundTrips(source: alternateSource, parse, experimentalFeatures: experimentalFeatures, file: file, line: line)
707726
}
708727
#endif
709728
}

0 commit comments

Comments
 (0)