Skip to content

Commit bcec687

Browse files
committed
Handle implicit pointer conversions
1 parent 8c19484 commit bcec687

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

Sources/Testing/Expectations/ExpectationContext.swift

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,92 @@ extension __ExpectationContext {
427427
}
428428
}
429429

430+
// MARK: - Implicit pointer conversion
431+
432+
extension __ExpectationContext {
433+
/// Convert a mutable pointer to an immutable one and capture information
434+
/// about it for use if the expectation currently being evaluated fails.
435+
///
436+
/// - Parameters:
437+
/// - value: The pointer to make immutable.
438+
/// - id: A value that uniquely identifies the represented expression in the
439+
/// context of the expectation currently being evaluated.
440+
///
441+
/// - Returns: `value`, cast to an immutable pointer.
442+
///
443+
/// This overload of `callAsFunction(_:_:)` handles the implicit conversion
444+
/// from a mutable to an immutable pointer that is normally provided by the
445+
/// compiler.
446+
///
447+
/// - Warning: This function is used to implement the `#expect()` and
448+
/// `#require()` macros. Do not call it directly.
449+
public mutating func callAsFunction<T>(_ value: UnsafeMutablePointer<T>, _ id: __ExpressionID) -> UnsafePointer<T> {
450+
UnsafePointer(self(value, id) as UnsafeMutablePointer<T>)
451+
}
452+
453+
/// Convert an optional mutable pointer to an immutable one and capture
454+
/// information about it for use if the expectation currently being evaluated
455+
/// fails.
456+
///
457+
/// - Parameters:
458+
/// - value: The pointer to make immutable, or `nil`.
459+
/// - id: A value that uniquely identifies the represented expression in the
460+
/// context of the expectation currently being evaluated.
461+
///
462+
/// - Returns: `value`, cast to an immutable pointer.
463+
///
464+
/// This overload of `callAsFunction(_:_:)` handles the implicit conversion
465+
/// from a mutable to an immutable pointer that is normally provided by the
466+
/// compiler.
467+
///
468+
/// - Warning: This function is used to implement the `#expect()` and
469+
/// `#require()` macros. Do not call it directly.
470+
public mutating func callAsFunction<T>(_ value: UnsafeMutablePointer<T>?, _ id: __ExpressionID) -> UnsafePointer<T>? {
471+
UnsafePointer(self(value, id) as UnsafeMutablePointer<T>?)
472+
}
473+
474+
/// Convert a mutable raw pointer to an immutable one and capture information
475+
/// about it for use if the expectation currently being evaluated fails.
476+
///
477+
/// - Parameters:
478+
/// - value: The pointer to make immutable.
479+
/// - id: A value that uniquely identifies the represented expression in the
480+
/// context of the expectation currently being evaluated.
481+
///
482+
/// - Returns: `value`, cast to an immutable raw pointer.
483+
///
484+
/// This overload of `callAsFunction(_:_:)` handles the implicit conversion
485+
/// from a mutable to an immutable pointer that is normally provided by the
486+
/// compiler.
487+
///
488+
/// - Warning: This function is used to implement the `#expect()` and
489+
/// `#require()` macros. Do not call it directly.
490+
public mutating func callAsFunction(_ value: UnsafeMutableRawPointer, _ id: __ExpressionID) -> UnsafeRawPointer {
491+
UnsafeRawPointer(self(value, id) as UnsafeMutableRawPointer)
492+
}
493+
494+
/// Convert an optional mutable raw pointer to an immutable one and capture
495+
/// information about it for use if the expectation currently being evaluated
496+
/// fails.
497+
///
498+
/// - Parameters:
499+
/// - value: The pointer to make immutable, or `nil`.
500+
/// - id: A value that uniquely identifies the represented expression in the
501+
/// context of the expectation currently being evaluated.
502+
///
503+
/// - Returns: `value`, cast to an immutable raw pointer.
504+
///
505+
/// This overload of `callAsFunction(_:_:)` handles the implicit conversion
506+
/// from a mutable to an immutable pointer that is normally provided by the
507+
/// compiler.
508+
///
509+
/// - Warning: This function is used to implement the `#expect()` and
510+
/// `#require()` macros. Do not call it directly.
511+
public mutating func callAsFunction(_ value: UnsafeMutableRawPointer?, _ id: __ExpressionID) -> UnsafeRawPointer? {
512+
UnsafeRawPointer(self(value, id) as UnsafeMutableRawPointer?)
513+
}
514+
}
515+
430516
#if !SWT_FIXED_122011759
431517
// MARK: - String-to-C-string handling
432518

Tests/TestingTests/Support/CartesianProductTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ struct CartesianProductTests {
3030
func count() {
3131
// Test the size of the product is correct.
3232
let (c1, c2, product) = computeCartesianProduct()
33-
#expect(product.underestimatedCount == c1.underestimatedCount * c2.underestimatedCount)
34-
#expect(Array(product).count == c1.count * c2.count)
35-
#expect(Array(product).count == 26 * 100)
33+
#expect(product.underestimatedCount == (c1.underestimatedCount * c2.underestimatedCount))
34+
#expect(Array(product).count == (c1.count * c2.count))
35+
#expect(Array(product).count == (26 * 100))
3636
}
3737

3838
@Test("First element is correct")

0 commit comments

Comments
 (0)