From 1399ff52560383fd76c64cd8eafa2d0960bd3e64 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Tue, 18 Jun 2024 08:09:04 -0700 Subject: [PATCH] Swift Language Support: Drop <5.9, Add 6.0 (#73) * Swift Language Support: Drop <5.9, Add 6.0 * wip * wip --- .github/workflows/ci.yml | 27 ++- Makefile | 38 --- Package.swift | 10 +- Package@swift-6.0.swift | 24 ++ Sources/Tagged/Tagged.swift | 248 +++++++------------- Sources/TaggedMoney/TaggedMoney.swift | 6 +- Sources/TaggedTime/TaggedTime.swift | 72 +++--- Tests/TaggedTests/TaggedTests.swift | 52 ++-- Tests/TaggedTimeTests/TaggedTimeTests.swift | 24 +- 9 files changed, 214 insertions(+), 287 deletions(-) delete mode 100644 Makefile create mode 100644 Package@swift-6.0.swift diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d94d6ba..238e003 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,17 +10,26 @@ on: jobs: build: - name: MacOS - runs-on: macOS-latest + name: macOS + runs-on: macos-14 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + - name: Select Xcode + run: sudo xcode-select -s /Applications/Xcode_15.4.app - name: Run tests - run: make test-swift + run: swift test - ubuntu: - name: Ubuntu + linux: + strategy: + matrix: + swift: + - '5.10' + name: Linux (Swift ${{ matrix.swift }}) runs-on: ubuntu-latest + container: swift:${{ matrix.swift }} steps: - - uses: actions/checkout@v2 - - name: Run tests - run: make test-linux + - uses: actions/checkout@v4 + - name: Run tests + run: swift test --parallel + - name: Run tests (release) + run: swift test -c release --parallel diff --git a/Makefile b/Makefile deleted file mode 100644 index baef401..0000000 --- a/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -xcodeproj: - PF_DEVELOP=1 swift run xcodegen - -test-linux: - docker run \ - --rm \ - -v "$(PWD):$(PWD)" \ - -w "$(PWD)" \ - swift:5.3 \ - bash -c 'make test-swift' - -test-macos: - set -o pipefail && \ - xcodebuild test \ - -scheme Tagged_macOS \ - -destination platform="macOS" \ - -derivedDataPath ./.derivedData \ - | xcpretty - -test-ios: - set -o pipefail && \ - xcodebuild test \ - -scheme Tagged_iOS \ - -destination platform="iOS Simulator,name=iPhone 11 Pro Max,OS=13.2.2" \ - | xcpretty - -test-swift: - swift test \ - --enable-test-discovery \ - --parallel - -test-playgrounds: test-macos - find . \ - -path '*.playground/*' \ - -name '*.swift' \ - -exec swift -F .derivedData/Build/Products/Debug/ -suppress-warnings {} + - -test-all: test-linux test-macos test-ios test-playgrounds test-swift diff --git a/Package.swift b/Package.swift index c76463e..614c536 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,5 @@ -// swift-tools-version:5.1 +// swift-tools-version: 5.9 + import Foundation import PackageDescription @@ -20,3 +21,10 @@ var package = Package( .testTarget(name: "TaggedTimeTests", dependencies: ["TaggedTime"]), ] ) + +for target in package.targets { + target.swiftSettings = target.swiftSettings ?? [] + target.swiftSettings!.append(contentsOf: [ + .enableExperimentalFeature("StrictConcurrency") + ]) +} diff --git a/Package@swift-6.0.swift b/Package@swift-6.0.swift new file mode 100644 index 0000000..099ec0a --- /dev/null +++ b/Package@swift-6.0.swift @@ -0,0 +1,24 @@ +// swift-tools-version: 6.0 + +import Foundation +import PackageDescription + +var package = Package( + name: "swift-tagged", + products: [ + .library(name: "Tagged", targets: ["Tagged"]), + .library(name: "TaggedMoney", targets: ["TaggedMoney"]), + .library(name: "TaggedTime", targets: ["TaggedTime"]), + ], + targets: [ + .target(name: "Tagged", dependencies: []), + .testTarget(name: "TaggedTests", dependencies: ["Tagged"]), + + .target(name: "TaggedMoney", dependencies: ["Tagged"]), + .testTarget(name: "TaggedMoneyTests", dependencies: ["TaggedMoney"]), + + .target(name: "TaggedTime", dependencies: ["Tagged"]), + .testTarget(name: "TaggedTimeTests", dependencies: ["TaggedTime"]), + ], + swiftLanguageVersions: [.v6] +) diff --git a/Sources/Tagged/Tagged.swift b/Sources/Tagged/Tagged.swift index cfcfb50..1c96826 100644 --- a/Sources/Tagged/Tagged.swift +++ b/Sources/Tagged/Tagged.swift @@ -10,20 +10,24 @@ public struct Tagged { self.rawValue = rawValue } - public func map(_ f: (RawValue) -> B) -> Tagged { - return .init(rawValue: f(self.rawValue)) + public subscript(dynamicMember keyPath: KeyPath) -> Subject { + rawValue[keyPath: keyPath] } -} -extension Tagged { - public subscript(dynamicMember keyPath: KeyPath) -> T { - return self.rawValue[keyPath: keyPath] - } + public func map( + _ transform: (RawValue) throws -> NewValue + ) rethrows -> Tagged { + Tagged(rawValue: try transform(rawValue)) + } + + public func coerced(to type: NewTag.Type) -> Tagged { + unsafeBitCast(self, to: Tagged.self) + } } extension Tagged: CustomStringConvertible { public var description: String { - return String(describing: self.rawValue) + String(describing: rawValue) } } @@ -31,40 +35,37 @@ extension Tagged: RawRepresentable {} extension Tagged: CustomPlaygroundDisplayConvertible { public var playgroundDescription: Any { - return self.rawValue + rawValue } } // MARK: - Conditional Conformances extension Tagged: Collection where RawValue: Collection { - public typealias Element = RawValue.Element - public typealias Index = RawValue.Index - public func index(after i: RawValue.Index) -> RawValue.Index { - return rawValue.index(after: i) + rawValue.index(after: i) } public subscript(position: RawValue.Index) -> RawValue.Element { - return rawValue[position] + rawValue[position] } public var startIndex: RawValue.Index { - return rawValue.startIndex + rawValue.startIndex } public var endIndex: RawValue.Index { - return rawValue.endIndex + rawValue.endIndex } - public __consuming func makeIterator() -> RawValue.Iterator { - return rawValue.makeIterator() + public consuming func makeIterator() -> RawValue.Iterator { + rawValue.makeIterator() } } extension Tagged: Comparable where RawValue: Comparable { - public static func < (lhs: Tagged, rhs: Tagged) -> Bool { - return lhs.rawValue < rhs.rawValue + public static func < (lhs: Self, rhs: Self) -> Bool { + lhs.rawValue < rhs.rawValue } } @@ -73,7 +74,7 @@ extension Tagged: Decodable where RawValue: Decodable { do { self.init(rawValue: try decoder.singleValueContainer().decode(RawValue.self)) } catch { - self.init(rawValue: try .init(from: decoder)) + self.init(rawValue: try RawValue(from: decoder)) } } } @@ -89,109 +90,74 @@ extension Tagged: Encodable where RawValue: Encodable { } } -#if swift(>=5.6) - @available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *) - extension Tagged: CodingKeyRepresentable where RawValue: CodingKeyRepresentable { - public init?(codingKey: T) { - guard let rawValue = RawValue(codingKey: codingKey) - else { return nil } - self.init(rawValue: rawValue) - } +@available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *) +extension Tagged: CodingKeyRepresentable where RawValue: CodingKeyRepresentable { + public init?(codingKey: some CodingKey) { + guard let rawValue = RawValue(codingKey: codingKey) + else { return nil } + self.init(rawValue: rawValue) + } - public var codingKey: CodingKey { - self.rawValue.codingKey - } + public var codingKey: CodingKey { + self.rawValue.codingKey } -#endif +} extension Tagged: Equatable where RawValue: Equatable {} extension Tagged: Error where RawValue: Error {} -#if canImport(_Concurrency) && compiler(>=5.5.2) extension Tagged: Sendable where RawValue: Sendable {} -#endif - -#if canImport(Foundation) -import Foundation -extension Tagged: LocalizedError where RawValue: Error { - public var errorDescription: String? { - return rawValue.localizedDescription - } - public var failureReason: String? { - return (rawValue as? LocalizedError)?.failureReason - } - public var helpAnchor: String? { - return (rawValue as? LocalizedError)?.helpAnchor - } - public var recoverySuggestion: String? { - return (rawValue as? LocalizedError)?.recoverySuggestion - } -} -#endif extension Tagged: ExpressibleByBooleanLiteral where RawValue: ExpressibleByBooleanLiteral { - public typealias BooleanLiteralType = RawValue.BooleanLiteralType - public init(booleanLiteral value: RawValue.BooleanLiteralType) { self.init(rawValue: RawValue(booleanLiteral: value)) } } -extension Tagged: ExpressibleByExtendedGraphemeClusterLiteral where RawValue: ExpressibleByExtendedGraphemeClusterLiteral { - public typealias ExtendedGraphemeClusterLiteralType = RawValue.ExtendedGraphemeClusterLiteralType - - public init(extendedGraphemeClusterLiteral: ExtendedGraphemeClusterLiteralType) { +extension Tagged: ExpressibleByExtendedGraphemeClusterLiteral +where RawValue: ExpressibleByExtendedGraphemeClusterLiteral { + public init(extendedGraphemeClusterLiteral: RawValue.ExtendedGraphemeClusterLiteralType) { self.init(rawValue: RawValue(extendedGraphemeClusterLiteral: extendedGraphemeClusterLiteral)) } } extension Tagged: ExpressibleByFloatLiteral where RawValue: ExpressibleByFloatLiteral { - public typealias FloatLiteralType = RawValue.FloatLiteralType - - public init(floatLiteral: FloatLiteralType) { + public init(floatLiteral: RawValue.FloatLiteralType) { self.init(rawValue: RawValue(floatLiteral: floatLiteral)) } } extension Tagged: ExpressibleByIntegerLiteral where RawValue: ExpressibleByIntegerLiteral { - public typealias IntegerLiteralType = RawValue.IntegerLiteralType - - public init(integerLiteral: IntegerLiteralType) { + public init(integerLiteral: RawValue.IntegerLiteralType) { self.init(rawValue: RawValue(integerLiteral: integerLiteral)) } } extension Tagged: ExpressibleByStringLiteral where RawValue: ExpressibleByStringLiteral { - public typealias StringLiteralType = RawValue.StringLiteralType - - public init(stringLiteral: StringLiteralType) { + public init(stringLiteral: RawValue.StringLiteralType) { self.init(rawValue: RawValue(stringLiteral: stringLiteral)) } } -extension Tagged: ExpressibleByStringInterpolation where RawValue: ExpressibleByStringInterpolation { - public typealias StringInterpolation = RawValue.StringInterpolation - - public init(stringInterpolation: Self.StringInterpolation) { +extension Tagged: ExpressibleByStringInterpolation +where RawValue: ExpressibleByStringInterpolation { + public init(stringInterpolation: RawValue.StringInterpolation) { self.init(rawValue: RawValue(stringInterpolation: stringInterpolation)) } } -extension Tagged: ExpressibleByUnicodeScalarLiteral where RawValue: ExpressibleByUnicodeScalarLiteral { - public typealias UnicodeScalarLiteralType = RawValue.UnicodeScalarLiteralType - - public init(unicodeScalarLiteral: UnicodeScalarLiteralType) { +extension Tagged: ExpressibleByUnicodeScalarLiteral +where RawValue: ExpressibleByUnicodeScalarLiteral { + public init(unicodeScalarLiteral: RawValue.UnicodeScalarLiteralType) { self.init(rawValue: RawValue(unicodeScalarLiteral: unicodeScalarLiteral)) } } -@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) +@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) extension Tagged: Identifiable where RawValue: Identifiable { - public typealias ID = RawValue.ID - - public var id: ID { - return rawValue.id + public var id: RawValue.ID { + rawValue.id } } @@ -202,116 +168,72 @@ extension Tagged: LosslessStringConvertible where RawValue: LosslessStringConver } } -#if compiler(>=5) extension Tagged: AdditiveArithmetic where RawValue: AdditiveArithmetic { - public static var zero: Tagged { - return self.init(rawValue: .zero) + public static var zero: Self { + Self(rawValue: .zero) } - public static func + (lhs: Tagged, rhs: Tagged) -> Tagged { - return self.init(rawValue: lhs.rawValue + rhs.rawValue) + public static func + (lhs: Self, rhs: Self) -> Self { + Self(rawValue: lhs.rawValue + rhs.rawValue) } - public static func += (lhs: inout Tagged, rhs: Tagged) { + public static func += (lhs: inout Self, rhs: Self) { lhs.rawValue += rhs.rawValue } - public static func - (lhs: Tagged, rhs: Tagged) -> Tagged { - return self.init(rawValue: lhs.rawValue - rhs.rawValue) + public static func - (lhs: Self, rhs: Self) -> Self { + Self(rawValue: lhs.rawValue - rhs.rawValue) } - public static func -= (lhs: inout Tagged, rhs: Tagged) { + public static func -= (lhs: inout Self, rhs: Self) { lhs.rawValue -= rhs.rawValue } } extension Tagged: Numeric where RawValue: Numeric { - public init?(exactly source: T) where T: BinaryInteger { + public init?(exactly source: some BinaryInteger) { guard let rawValue = RawValue(exactly: source) else { return nil } self.init(rawValue: rawValue) } public var magnitude: RawValue.Magnitude { - return self.rawValue.magnitude - } - - public static func * (lhs: Tagged, rhs: Tagged) -> Tagged { - return self.init(rawValue: lhs.rawValue * rhs.rawValue) - } - - public static func *= (lhs: inout Tagged, rhs: Tagged) { - lhs.rawValue *= rhs.rawValue - } -} -#else -extension Tagged: Numeric where RawValue: Numeric { - public typealias Magnitude = RawValue.Magnitude - - public init?(exactly source: T) where T: BinaryInteger { - guard let rawValue = RawValue(exactly: source) else { return nil } - self.init(rawValue: rawValue) - } - public var magnitude: RawValue.Magnitude { - return self.rawValue.magnitude + rawValue.magnitude } - public static func + (lhs: Tagged, rhs: Tagged) -> Tagged { - return self.init(rawValue: lhs.rawValue + rhs.rawValue) + public static func * (lhs: Self, rhs: Self) -> Self { + Self(rawValue: lhs.rawValue * rhs.rawValue) } - public static func += (lhs: inout Tagged, rhs: Tagged) { - lhs.rawValue += rhs.rawValue - } - - public static func * (lhs: Tagged, rhs: Tagged) -> Tagged { - return self.init(rawValue: lhs.rawValue * rhs.rawValue) - } - - public static func *= (lhs: inout Tagged, rhs: Tagged) { + public static func *= (lhs: inout Self, rhs: Self) { lhs.rawValue *= rhs.rawValue } - - public static func - (lhs: Tagged, rhs: Tagged) -> Tagged { - return self.init(rawValue: lhs.rawValue - rhs.rawValue) - } - - public static func -= (lhs: inout Tagged, rhs: Tagged) { - lhs.rawValue -= rhs.rawValue - } } -#endif extension Tagged: Hashable where RawValue: Hashable {} extension Tagged: SignedNumeric where RawValue: SignedNumeric {} extension Tagged: Sequence where RawValue: Sequence { - public typealias Iterator = RawValue.Iterator - - public __consuming func makeIterator() -> RawValue.Iterator { - return rawValue.makeIterator() + public consuming func makeIterator() -> RawValue.Iterator { + rawValue.makeIterator() } } extension Tagged: Strideable where RawValue: Strideable { - public typealias Stride = RawValue.Stride - - public func distance(to other: Tagged) -> RawValue.Stride { - self.rawValue.distance(to: other.rawValue) + public func distance(to other: Self) -> RawValue.Stride { + rawValue.distance(to: other.rawValue) } - public func advanced(by n: RawValue.Stride) -> Tagged { - Tagged(rawValue: self.rawValue.advanced(by: n)) + public func advanced(by n: RawValue.Stride) -> Self { + Tagged(rawValue: rawValue.advanced(by: n)) } } extension Tagged: ExpressibleByArrayLiteral where RawValue: ExpressibleByArrayLiteral { - public typealias ArrayLiteralElement = RawValue.ArrayLiteralElement - - public init(arrayLiteral elements: ArrayLiteralElement...) { + public init(arrayLiteral elements: RawValue.ArrayLiteralElement...) { let f = unsafeBitCast( - RawValue.init(arrayLiteral:) as (ArrayLiteralElement...) -> RawValue, - to: (([ArrayLiteralElement]) -> RawValue).self + RawValue.init(arrayLiteral:) as (RawValue.ArrayLiteralElement...) -> RawValue, + to: (([RawValue.ArrayLiteralElement]) -> RawValue).self ) self.init(rawValue: f(elements)) @@ -319,12 +241,9 @@ extension Tagged: ExpressibleByArrayLiteral where RawValue: ExpressibleByArrayLi } extension Tagged: ExpressibleByDictionaryLiteral where RawValue: ExpressibleByDictionaryLiteral { - public typealias Key = RawValue.Key - public typealias Value = RawValue.Value - - public init(dictionaryLiteral elements: (Key, Value)...) { + public init(dictionaryLiteral elements: (RawValue.Key, RawValue.Value)...) { let f = unsafeBitCast( - RawValue.init(dictionaryLiteral:) as ((Key, Value)...) -> RawValue, + RawValue.init(dictionaryLiteral:) as ((RawValue.Key, RawValue.Value)...) -> RawValue, to: (([(Key, Value)]) -> RawValue).self ) @@ -332,9 +251,24 @@ extension Tagged: ExpressibleByDictionaryLiteral where RawValue: ExpressibleByDi } } -// MARK: - Coerce -extension Tagged { - public func coerced(to type: Tag2.Type) -> Tagged { - return unsafeBitCast(self, to: Tagged.self) +#if canImport(Foundation) + import Foundation + + extension Tagged: LocalizedError where RawValue: Error { + public var errorDescription: String? { + rawValue.localizedDescription + } + + public var failureReason: String? { + (rawValue as? LocalizedError)?.failureReason + } + + public var helpAnchor: String? { + (rawValue as? LocalizedError)?.helpAnchor + } + + public var recoverySuggestion: String? { + (rawValue as? LocalizedError)?.recoverySuggestion + } } -} +#endif diff --git a/Sources/TaggedMoney/TaggedMoney.swift b/Sources/TaggedMoney/TaggedMoney.swift index 50b6701..56ab177 100644 --- a/Sources/TaggedMoney/TaggedMoney.swift +++ b/Sources/TaggedMoney/TaggedMoney.swift @@ -1,23 +1,25 @@ import Tagged public enum CentsTag {} + /// A type that represents cents, i.e. one hundredth of a dollar. public typealias Cents = Tagged public enum DollarsTag {} + /// A type that represents a dollar, i.e. a base unit of any currency. public typealias Dollars = Tagged extension Tagged where Tag == CentsTag, RawValue: BinaryFloatingPoint { /// Converts cents into dollars by dividing by 100. public var dollars: Dollars { - return .init(rawValue: self.rawValue / 100) + return Dollars(rawValue: rawValue / 100) } } extension Tagged where Tag == DollarsTag, RawValue: Numeric { /// Converts dollars into cents by multiplying by 100. public var cents: Cents { - return .init(rawValue: self.rawValue * 100) + return Cents(rawValue: rawValue * 100) } } diff --git a/Sources/TaggedTime/TaggedTime.swift b/Sources/TaggedTime/TaggedTime.swift index 8e5e85d..214880f 100644 --- a/Sources/TaggedTime/TaggedTime.swift +++ b/Sources/TaggedTime/TaggedTime.swift @@ -15,12 +15,12 @@ public typealias Seconds = Tagged extension Tagged where Tag == MillisecondsTag, RawValue: BinaryFloatingPoint { /// Converts milliseconds to seconds. public var seconds: Seconds { - Seconds(rawValue: self.rawValue / 1000) + Seconds(rawValue: rawValue / 1000) } /// Converts milliseconds into `TimeInterval`, which is measured in seconds. public var timeInterval: TimeInterval { - let seconds = self.seconds.rawValue + let seconds = seconds.rawValue return TimeInterval( sign: seconds.sign, exponentBitPattern: UInt(seconds.exponentBitPattern), @@ -30,83 +30,75 @@ extension Tagged where Tag == MillisecondsTag, RawValue: BinaryFloatingPoint { /// Converts milliseconds into `Date`, which is measured in seconds. public var date: Date { - Date(timeIntervalSince1970: self.timeInterval) + Date(timeIntervalSince1970: timeInterval) } - #if swift(>=5.7) - /// Converts milliseconds into `Duration`. - @available(macOS 13, iOS 16, watchOS 9, tvOS 16, *) - public var duration: Duration { - .milliseconds(Double(self.rawValue)) - } - #endif + /// Converts milliseconds into `Duration`. + @available(macOS 13, iOS 16, watchOS 9, tvOS 16, *) + public var duration: Duration { + .milliseconds(Double(rawValue)) + } } extension Tagged where Tag == MillisecondsTag, RawValue: BinaryInteger { /// Converts milliseconds into `TimeInterval`, which is measured in seconds. public var timeInterval: TimeInterval { - self.map(TimeInterval.init).timeInterval + map(TimeInterval.init).timeInterval } /// Converts milliseconds into `DispatchTimeInterval`. public var dispatchTimeInterval: DispatchTimeInterval { - .milliseconds(Int(self.rawValue)) + .milliseconds(Int(rawValue)) } /// Converts milliseconds into `Date`, which is measured in seconds. public var date: Date { - Date(timeIntervalSince1970: self.timeInterval) + Date(timeIntervalSince1970: timeInterval) } - #if swift(>=5.7) - /// Converts milliseconds into `Duration`. - @available(macOS 13, iOS 16, watchOS 9, tvOS 16, *) - public var duration: Duration { - .milliseconds(self.rawValue) - } - #endif + /// Converts milliseconds into `Duration`. + @available(macOS 13, iOS 16, watchOS 9, tvOS 16, *) + public var duration: Duration { + .milliseconds(rawValue) + } } extension Tagged where Tag == SecondsTag, RawValue: Numeric { /// Converts seconds in milliseconds. public var milliseconds: Milliseconds { - return Milliseconds(rawValue: self.rawValue * 1000) + return Milliseconds(rawValue: rawValue * 1000) } } extension Tagged where Tag == SecondsTag, RawValue: BinaryInteger { /// Converts seconds into `TimeInterval`. public var timeInterval: TimeInterval { - TimeInterval(Int64(self.rawValue)) + TimeInterval(Int64(rawValue)) } /// Converts seconds into `DispatchTimeInterval`. public var dispatchTimeInterval: DispatchTimeInterval { - .seconds(Int(self.rawValue)) + .seconds(Int(rawValue)) } /// Converts seconds into `Date`. public var date: Date { - Date(timeIntervalSince1970: self.timeInterval) + Date(timeIntervalSince1970: timeInterval) } - #if swift(>=5.7) - /// Converts seconds into `Duration`. - @available(macOS 13, iOS 16, watchOS 9, tvOS 16, *) - public var duration: Duration { - .seconds(self.rawValue) - } - #endif + /// Converts seconds into `Duration`. + @available(macOS 13, iOS 16, watchOS 9, tvOS 16, *) + public var duration: Duration { + .seconds(rawValue) + } } extension Tagged where Tag == SecondsTag, RawValue: BinaryFloatingPoint { - #if swift(>=5.7) - /// Converts milliseconds into `Duration`. - @available(macOS 13, iOS 16, watchOS 9, tvOS 16, *) - public var duration: Duration { - .seconds(Double(self.rawValue)) - } - #endif + /// Converts milliseconds into `Duration`. + @available(macOS 13, iOS 16, watchOS 9, tvOS 16, *) + public var duration: Duration { + .seconds(Double(rawValue)) + } } extension Date { @@ -115,7 +107,7 @@ extension Date { /// - Parameter date: The date with which to compare the receiver. /// - Returns: The number of seconds between the receiver and the other date. public func secondsSince(_ date: Date) -> Seconds { - Seconds(rawValue: self.timeIntervalSince(date)) + Seconds(rawValue: timeIntervalSince(date)) } /// Computes the number of milliseconds between the receiver and another given date. @@ -123,6 +115,6 @@ extension Date { /// - Parameter date: The date with which to compare the receiver. /// - Returns: The number of milliseconds between the receiver and the other date. public func millisecondsSince(_ date: Date) -> Milliseconds { - self.secondsSince(date).milliseconds + secondsSince(date).milliseconds } } diff --git a/Tests/TaggedTests/TaggedTests.swift b/Tests/TaggedTests/TaggedTests.swift index a36fa94..1475ced 100644 --- a/Tests/TaggedTests/TaggedTests.swift +++ b/Tests/TaggedTests/TaggedTests.swift @@ -70,18 +70,16 @@ final class TaggedTests: XCTestCase { ) } - #if swift(>=5.6) - func testCodingKeyRepresentable() { - if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *) { - enum Key {} - let xs: [Tagged: String] = [Tagged("Hello"): "World"] - XCTAssertEqual( - String(decoding: try JSONEncoder().encode(xs), as: UTF8.self), - #"{"Hello":"World"}"# - ) - } + func testCodingKeyRepresentable() { + if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *) { + enum Key {} + let xs: [Tagged: String] = [Tagged("Hello"): "World"] + XCTAssertEqual( + String(decoding: try JSONEncoder().encode(xs), as: UTF8.self), + #"{"Hello":"World"}"# + ) } - #endif + } func testEquatable() { XCTAssertEqual(Tagged(rawValue: 1), Tagged(rawValue: 1)) @@ -92,23 +90,23 @@ final class TaggedTests: XCTestCase { } #if canImport(Foundation) - func testLocalizedError() { - let taggedError: Error = Tagged(rawValue: Unit()) - XCTAssertEqual(taggedError.localizedDescription, Unit().localizedDescription) - - struct DummyLocalizedError: LocalizedError { - var errorDescription: String? { return "errorDescription" } - var failureReason: String? { return "failureReason" } - var helpAnchor: String? { return "helpAnchor" } - var recoverySuggestion: String? { return "recoverySuggestion" } + func testLocalizedError() { + let taggedError: Error = Tagged(rawValue: Unit()) + XCTAssertEqual(taggedError.localizedDescription, Unit().localizedDescription) + + struct DummyLocalizedError: LocalizedError { + var errorDescription: String? { return "errorDescription" } + var failureReason: String? { return "failureReason" } + var helpAnchor: String? { return "helpAnchor" } + var recoverySuggestion: String? { return "recoverySuggestion" } + } + let taggedLocalizedError: LocalizedError = Tagged(rawValue: DummyLocalizedError()) + XCTAssertEqual(taggedLocalizedError.localizedDescription, DummyLocalizedError().localizedDescription) + XCTAssertEqual(taggedLocalizedError.errorDescription, DummyLocalizedError().errorDescription) + XCTAssertEqual(taggedLocalizedError.failureReason, DummyLocalizedError().failureReason) + XCTAssertEqual(taggedLocalizedError.helpAnchor, DummyLocalizedError().helpAnchor) + XCTAssertEqual(taggedLocalizedError.recoverySuggestion, DummyLocalizedError().recoverySuggestion) } - let taggedLocalizedError: LocalizedError = Tagged(rawValue: DummyLocalizedError()) - XCTAssertEqual(taggedLocalizedError.localizedDescription, DummyLocalizedError().localizedDescription) - XCTAssertEqual(taggedLocalizedError.errorDescription, DummyLocalizedError().errorDescription) - XCTAssertEqual(taggedLocalizedError.failureReason, DummyLocalizedError().failureReason) - XCTAssertEqual(taggedLocalizedError.helpAnchor, DummyLocalizedError().helpAnchor) - XCTAssertEqual(taggedLocalizedError.recoverySuggestion, DummyLocalizedError().recoverySuggestion) - } #endif func testExpressibleByBooleanLiteral() { diff --git a/Tests/TaggedTimeTests/TaggedTimeTests.swift b/Tests/TaggedTimeTests/TaggedTimeTests.swift index 07ba19d..c2c7fdd 100644 --- a/Tests/TaggedTimeTests/TaggedTimeTests.swift +++ b/Tests/TaggedTimeTests/TaggedTimeTests.swift @@ -48,21 +48,19 @@ final class TaggedTimeTests: XCTestCase { XCTAssertEqual(12, seconds) } - #if swift(>=5.7) - func testDuration() { - if #available(macOS 13, iOS 16, watchOS 9, tvOS 16, *) { - let intSeconds: Seconds = 12 - XCTAssertEqual(.seconds(12), intSeconds.duration) + func testDuration() { + if #available(macOS 13, iOS 16, watchOS 9, tvOS 16, *) { + let intSeconds: Seconds = 12 + XCTAssertEqual(.seconds(12), intSeconds.duration) - let doubleSeconds: Seconds = 1.2 - XCTAssertEqual(.seconds(1.2), doubleSeconds.duration) + let doubleSeconds: Seconds = 1.2 + XCTAssertEqual(.seconds(1.2), doubleSeconds.duration) - let intMilliseconds: Milliseconds = 12000 - XCTAssertEqual(.milliseconds(12000), intMilliseconds.duration) + let intMilliseconds: Milliseconds = 12000 + XCTAssertEqual(.milliseconds(12000), intMilliseconds.duration) - let doubleMilliseconds: Milliseconds = 1.2 - XCTAssertEqual(.milliseconds(1.2), doubleMilliseconds.duration) - } + let doubleMilliseconds: Milliseconds = 1.2 + XCTAssertEqual(.milliseconds(1.2), doubleMilliseconds.duration) } - #endif + } }