Skip to content

Commit 124669e

Browse files
authored
Expose ABI.EncodedInstant as tools SPI. (#1588)
Follow-up to #1587. Exposes `ABI.EncodedInstant` as SPI along with some conversion helpers to get a Foundation `Date` or suspending clock instant from an instance thereof. A value of this type is associated with each event in the event stream. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 70db9ce commit 124669e

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// This source file is part of the Swift.org open source project
33
//
4-
// Copyright (c) 2024 Apple Inc. and the Swift project authors
4+
// Copyright (c) 2024–2026 Apple Inc. and the Swift project authors
55
// Licensed under Apache License v2.0 with Runtime Library Exception
66
//
77
// See https://swift.org/LICENSE.txt for license information
@@ -12,19 +12,32 @@
1212
@_spi(Experimental) @_spi(ForToolsIntegrationOnly) public import Testing
1313
public import Foundation
1414

15+
@_spi(ForToolsIntegrationOnly)
1516
extension Date {
1617
/// Initialize this date to the equivalent of the same date on the testing
1718
/// library's clock.
1819
///
1920
/// - Parameters:
20-
/// - testClockInstant: The equivalent instant on ``Test/Clock``.
21+
/// - instant: The equivalent instant on ``Test/Clock``.
2122
///
2223
/// The resulting instance is equivalent to the wall-clock time represented by
23-
/// `testClockInstant`. For precise date/time calculations, convert instances
24-
/// of ``Test/Clock/Instant`` to `SuspendingClock.Instant` instead of `Date`.
25-
@_spi(Experimental) @_spi(ForToolsIntegrationOnly)
26-
public init(_ testClockInstant: Test.Clock.Instant) {
27-
self.init(timeIntervalSince1970: testClockInstant.durationSince1970 / .seconds(1))
24+
/// `instant`. For precise date/time calculations, convert instances of
25+
/// ``Test/Clock/Instant`` to `SuspendingClock.Instant` instead of `Date`.
26+
public init(_ instant: Test.Clock.Instant) {
27+
self.init(timeIntervalSince1970: instant.durationSince1970 / .seconds(1))
28+
}
29+
30+
/// Initialize this date to equal an instant from the testing library's event
31+
/// stream.
32+
///
33+
/// - Parameters:
34+
/// - instant: The encoded instant to initialize this instance from.
35+
///
36+
/// The resulting instance is equivalent to the wall-clock time represented by
37+
/// `instant`. For precise date/time calculations, convert instances of
38+
/// ``ABI/EncodedInstant`` to `SuspendingClock.Instant` instead of `Date`.
39+
public init?<V>(decoding instant: ABI.EncodedInstant<V>) {
40+
self.init(timeIntervalSince1970: instant.since1970)
2841
}
2942
}
3043
#endif

Sources/Testing/ABI/Encoded/ABI.EncodedEvent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension ABI {
4040
var kind: Kind
4141

4242
/// The instant at which the event occurred.
43-
var instant: EncodedInstant<V>
43+
public var instant: EncodedInstant<V>
4444

4545
/// The issue that occurred, if any.
4646
///
Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// This source file is part of the Swift.org open source project
33
//
4-
// Copyright (c) 2024 Apple Inc. and the Swift project authors
4+
// Copyright (c) 2024–2026 Apple Inc. and the Swift project authors
55
// Licensed under Apache License v2.0 with Runtime Library Exception
66
//
77
// See https://swift.org/LICENSE.txt for license information
@@ -12,29 +12,54 @@ extension ABI {
1212
/// A type implementing the JSON encoding of ``Test/Clock/Instant`` for the
1313
/// ABI entry point and event stream output.
1414
///
15-
/// This type is not part of the public interface of the testing library. It
16-
/// assists in converting values to JSON; clients that consume this JSON are
17-
/// expected to write their own decoders.
18-
struct EncodedInstant<V>: Sendable where V: ABI.Version {
15+
/// You can use this type and its conformance to [`Codable`](https://developer.apple.com/documentation/swift/codable),
16+
/// when integrating the testing library with development tools. It is not
17+
/// part of the testing library's public interface.
18+
public struct EncodedInstant<V>: Sendable where V: ABI.Version {
1919
/// The number of seconds since the system-defined suspending epoch.
2020
///
2121
/// For more information, see [`SuspendingClock`](https://developer.apple.com/documentation/swift/suspendingclock).
2222
var absolute: Double
2323

2424
/// The number of seconds since the UNIX epoch (1970-01-01 00:00:00 UT).
25-
var since1970: Double
25+
package var since1970: Double
26+
}
27+
}
28+
29+
// MARK: - Conversion to/from library types
2630

27-
init(encoding instant: borrowing Test.Clock.Instant) {
28-
absolute = instant.suspending.rawValue / .seconds(1)
31+
extension ABI.EncodedInstant {
32+
/// Initialize an instance of this type from the given value.
33+
///
34+
/// - Parameters:
35+
/// - instant: The instant to initialize this instance from.
36+
public init(encoding instant: borrowing Test.Clock.Instant) {
37+
absolute = instant.suspending.rawValue / .seconds(1)
2938
#if !SWT_NO_UTC_CLOCK
30-
since1970 = instant.wall.rawValue / .seconds(1)
39+
since1970 = instant.wall.rawValue / .seconds(1)
3140
#else
32-
since1970 = 0
41+
since1970 = 0
3342
#endif
34-
}
3543
}
3644
}
3745

46+
@_spi(ForToolsIntegrationOnly)
47+
extension SuspendingClock.Instant {
48+
/// Initialize this instant to equal an instant from the testing library's
49+
/// event stream.
50+
///
51+
/// - Parameters:
52+
/// - instant: The encoded instant to initialize this instance from.
53+
///
54+
/// The resulting instance is equivalent to the suspending-clock time
55+
/// represented by `instant`.
56+
public init?<V>(decoding instant: ABI.EncodedInstant<V>) {
57+
self = SuspendingClock().systemEpoch + .seconds(instant.absolute)
58+
}
59+
}
60+
61+
// Date.init(decoding:) is in the Foundation overlay.
62+
3863
// MARK: - Codable
3964

4065
extension ABI.EncodedInstant: Codable {}

0 commit comments

Comments
 (0)