-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rethink how we capture expectation conditions and their subexpressions.
This PR completely rewrites how we capture expectation conditions. For example, given the following expectation: ```swift ``` We currently detect that there is a binary operation and emit code that calls the binary operator as a closure and passes the left-hand value and right-hand value, then checks that the result of the operation is `true`. This is sufficient for simpler expressions like that one, but more complex ones (including any that involve `try` or `await` keywords) cannot be expanded correctly. With this PR, such expressions _can_ generally be expanded correctly. The change involves rewriting the macro condition as a closure to which is passed a local, mutable "context" value. Subexpressions of the condition expression are then rewritten by walking the syntax tree of the expression (using typical swift-syntax API) and replacing them with calls into the context value that pass in the value and related state. If the expectation ultimately fails, the collected data is transformed into an instance of the SPI type `Expression` that contains the source code of the expression and interesting subexpressions as well as the runtime values of those subexpressions. Nodes in the syntax tree are identified by a unique ID which is composed of the swift-syntax ID for that node as well as all its parent nodes in a compact bitmask format. These IDs can be transformed into graph/trie key paths when expression/subexpression relationships need to be reconstructed on failure, meaning that a single rewritten node doesn't otherwise need to know its "place" in the overall expression. There remain a few caveats (that also generally affect the current implementation): - Mutating member functions are syntactically indistinguishable from non-mutating ones and miscompile when rewritten; - Expressions involving move-only types are also indistinguishable, but need lifetime management to be rewritten correctly; and - Expressions where the `try` or `await` keyword is _outside_ the `#expect` macro cannot be expanded correctly because the macro cannot see those keywords during expansion. The first issue might be resolvable in the future using pointer tricks, although I don't hold a lot of hope for it. The second issue is probably resolved by non-escaping types. The third issue is an area of active exploration for us and the macros/swift-syntax team.
- Loading branch information
Showing
42 changed files
with
2,674 additions
and
1,862 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
Sources/Testing/ABI/v0/Encoded/ABIv0.EncodedExpectation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
// | ||
|
||
extension ABIv0 { | ||
/// A type implementing the JSON encoding of ``Expectation`` for the ABI entry | ||
/// point and event stream output. | ||
/// | ||
/// This type is not part of the public interface of the testing library. It | ||
/// assists in converting values to JSON; clients that consume this JSON are | ||
/// expected to write their own decoders. | ||
/// | ||
/// - Warning: Expectations are not yet part of the JSON schema. | ||
struct EncodedExpectation: Sendable { | ||
/// The expression evaluated by this expectation. | ||
/// | ||
/// - Warning: Expressions are not yet part of the JSON schema. | ||
var _expression: EncodedExpression | ||
|
||
init(encoding expectation: borrowing Expectation, in eventContext: borrowing Event.Context) { | ||
_expression = EncodedExpression(encoding: expectation.evaluatedExpression, in: eventContext) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Codable | ||
|
||
extension ABIv0.EncodedExpectation: Codable {} |
52 changes: 52 additions & 0 deletions
52
Sources/Testing/ABI/v0/Encoded/ABIv0.EncodedExpression.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
// | ||
|
||
extension ABIv0 { | ||
/// A type implementing the JSON encoding of ``Expression`` for the ABI entry | ||
/// point and event stream output. | ||
/// | ||
/// This type is not part of the public interface of the testing library. It | ||
/// assists in converting values to JSON; clients that consume this JSON are | ||
/// expected to write their own decoders. | ||
/// | ||
/// - Warning: Expressions are not yet part of the JSON schema. | ||
struct EncodedExpression: Sendable { | ||
/// The source code of the original captured expression. | ||
var sourceCode: String | ||
|
||
/// A string representation of the runtime value of this expression. | ||
/// | ||
/// If the runtime value of this expression has not been evaluated, the | ||
/// value of this property is `nil`. | ||
var runtimeValue: String? | ||
|
||
/// The fully-qualified name of the type of value represented by | ||
/// `runtimeValue`, or `nil` if that value has not been captured. | ||
var runtimeTypeName: String? | ||
|
||
/// Any child expressions within this expression. | ||
var children: [EncodedExpression]? | ||
|
||
init(encoding expression: borrowing __Expression, in eventContext: borrowing Event.Context) { | ||
sourceCode = expression.sourceCode | ||
runtimeValue = expression.runtimeValue.map(String.init(describingForTest:)) | ||
runtimeTypeName = expression.runtimeValue.map(\.typeInfo.fullyQualifiedName) | ||
if !expression.subexpressions.isEmpty { | ||
children = expression.subexpressions.map { [eventContext = copy eventContext] subexpression in | ||
Self(encoding: subexpression, in: eventContext) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Codable | ||
|
||
extension ABIv0.EncodedExpression: Codable {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.