From c0a2cc9a545d533c62b01ea58fb724c34129bfae Mon Sep 17 00:00:00 2001 From: Lennard Sprong Date: Tue, 28 Apr 2026 11:44:21 +0200 Subject: [PATCH] Add FoundationEssentials support --- Sources/PropertyBased/Date+String.swift | 24 +++++++++++++++++++ Sources/PropertyBased/FixedSeedTrait.swift | 2 +- Sources/PropertyBased/Gen+Date.swift | 6 ++++- Sources/PropertyBased/Gen+Float.swift | 6 ++++- Sources/PropertyBased/Shrink+Date.swift | 6 ++++- Sources/PropertyBased/Xoshiro.swift | 6 ++++- Tests/PropertyBasedTests/GenTests+Date.swift | 9 +++++-- .../PropertyBasedTests/ShrinkTests+Date.swift | 9 +++++-- 8 files changed, 59 insertions(+), 9 deletions(-) diff --git a/Sources/PropertyBased/Date+String.swift b/Sources/PropertyBased/Date+String.swift index c3d72e2..241ac4b 100644 --- a/Sources/PropertyBased/Date+String.swift +++ b/Sources/PropertyBased/Date+String.swift @@ -6,6 +6,7 @@ // #if canImport(Foundation) + import Foundation extension Calendar { @@ -50,4 +51,27 @@ extension Date: @retroactive ExpressibleByStringLiteral { } } +#elseif canImport(FoundationEssentials) + +import FoundationEssentials + +extension Date: @retroactive ExpressibleByStringLiteral { + /// Create a date from a ISO8601-formatted string. + /// + /// The date components are required. The time and offset components are optional. + /// + /// This initializer exists as a convenience for creating date ranges. It is not recommended for use in a production environment. + /// + /// - Parameter value: The string to parse. + public init(stringLiteral value: String) { + let formatter = ISO8601DateFormatter() + + guard let date = formatter.date(from: value) else { + preconditionFailure("\(value) is not a valid ISO 8601 date") + } + + self = date + } +} + #endif diff --git a/Sources/PropertyBased/FixedSeedTrait.swift b/Sources/PropertyBased/FixedSeedTrait.swift index 89cf0c5..80bd26c 100644 --- a/Sources/PropertyBased/FixedSeedTrait.swift +++ b/Sources/PropertyBased/FixedSeedTrait.swift @@ -50,7 +50,7 @@ public struct FixedSeedTrait: TestTrait, TestScoping { } extension Trait where Self == FixedSeedTrait { - #if canImport(Foundation) + #if canImport(FoundationEssentials) || canImport(Foundation) /// Override the seed used by all property checks within this Test. /// /// If one of your property checks fails intermittently, apply this trait to reliably reproduce the issue. diff --git a/Sources/PropertyBased/Gen+Date.swift b/Sources/PropertyBased/Gen+Date.swift index a3f22a5..c568127 100644 --- a/Sources/PropertyBased/Gen+Date.swift +++ b/Sources/PropertyBased/Gen+Date.swift @@ -5,8 +5,12 @@ // Created by Lennard Sprong on 30/05/2025. // -#if canImport(Foundation) +#if canImport(FoundationEssentials) +import FoundationEssentials +#elseif canImport(Foundation) import Foundation +#endif +#if canImport(FoundationEssentials) || canImport(Foundation) // This value is a constant, to keep test runs reproducible. // This value may only be changed between package releases. diff --git a/Sources/PropertyBased/Gen+Float.swift b/Sources/PropertyBased/Gen+Float.swift index 282bd42..1e25580 100644 --- a/Sources/PropertyBased/Gen+Float.swift +++ b/Sources/PropertyBased/Gen+Float.swift @@ -122,8 +122,12 @@ extension Gen where Value == Float80 { } #endif -#if canImport(Foundation) +#if canImport(FoundationEssentials) +import FoundationEssentials +#elseif canImport(Foundation) import Foundation +#endif +#if canImport(FoundationEssentials) || canImport(Foundation) extension Gen where Value == CGFloat { /// Produces a generator of random floats within the specified range. /// diff --git a/Sources/PropertyBased/Shrink+Date.swift b/Sources/PropertyBased/Shrink+Date.swift index faa8d56..dc75a84 100644 --- a/Sources/PropertyBased/Shrink+Date.swift +++ b/Sources/PropertyBased/Shrink+Date.swift @@ -5,8 +5,12 @@ // Created by Lennard Sprong on 30/05/2025. // -#if canImport(Foundation) +#if canImport(FoundationEssentials) +import FoundationEssentials +#elseif canImport(Foundation) import Foundation +#endif +#if canImport(FoundationEssentials) || canImport(Foundation) extension Shrink { /// Shrink a TimeInterval by rounding to seconds, then minutes, then 15 minute intervals. diff --git a/Sources/PropertyBased/Xoshiro.swift b/Sources/PropertyBased/Xoshiro.swift index df46c80..2683e19 100644 --- a/Sources/PropertyBased/Xoshiro.swift +++ b/Sources/PropertyBased/Xoshiro.swift @@ -49,8 +49,12 @@ extension Xoshiro: Hashable { } } -#if canImport(Foundation) +#if canImport(FoundationEssentials) +import FoundationEssentials +#elseif canImport(Foundation) import Foundation +#endif +#if canImport(FoundationEssentials) || canImport(Foundation) extension Xoshiro: SeededRandomNumberGenerator { public typealias Seed = String diff --git a/Tests/PropertyBasedTests/GenTests+Date.swift b/Tests/PropertyBasedTests/GenTests+Date.swift index ba0de33..6a861af 100644 --- a/Tests/PropertyBasedTests/GenTests+Date.swift +++ b/Tests/PropertyBasedTests/GenTests+Date.swift @@ -5,9 +5,14 @@ // Created by Lennard Sprong on 31/05/2025. // -#if canImport(Foundation) -import Testing +#if canImport(FoundationEssentials) +import FoundationEssentials +#elseif canImport(Foundation) import Foundation +#endif +#if canImport(FoundationEssentials) || canImport(Foundation) + +import Testing import PropertyBased extension Calendar { diff --git a/Tests/PropertyBasedTests/ShrinkTests+Date.swift b/Tests/PropertyBasedTests/ShrinkTests+Date.swift index 99188b7..034a616 100644 --- a/Tests/PropertyBasedTests/ShrinkTests+Date.swift +++ b/Tests/PropertyBasedTests/ShrinkTests+Date.swift @@ -5,9 +5,14 @@ // Created by Lennard Sprong on 31/05/2025. // -#if canImport(Foundation) -import Testing +#if canImport(FoundationEssentials) +import FoundationEssentials +#elseif canImport(Foundation) import Foundation +#endif +#if canImport(FoundationEssentials) || canImport(Foundation) + +import Testing @testable import PropertyBased @Suite struct ShrinkDateTests {