diff --git a/Sources/MarvelService/Internal/Extensions/TimeInterval+Computed.swift b/Sources/MarvelService/Internal/Extensions/TimeInterval+Computed.swift new file mode 100644 index 00000000..467ff0c2 --- /dev/null +++ b/Sources/MarvelService/Internal/Extensions/TimeInterval+Computed.swift @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------=== +// +// This source file is part of the MarvelService open source project +// +// Copyright (c) -2025 Röck+Cöde VoF. and the MarvelService project authors +// Licensed under the EUPL 1.2 or later. +// +// See LICENSE for license information +// See CONTRIBUTORS for the list of MarvelService project authors +// +//===----------------------------------------------------------------------=== + +import struct Foundation.TimeInterval + +extension TimeInterval { + + // MARK: Functions + + /// Converts a time interval to a string value. + /// - Returns: A time interval as a string. + var asString: String { + .init(format: "%f", self) + } + +} diff --git a/Tests/MarvelService/Cases/Internal/Extensions/TimeInterval+ComputedTests.swift b/Tests/MarvelService/Cases/Internal/Extensions/TimeInterval+ComputedTests.swift new file mode 100644 index 00000000..3ccdb0fc --- /dev/null +++ b/Tests/MarvelService/Cases/Internal/Extensions/TimeInterval+ComputedTests.swift @@ -0,0 +1,90 @@ +//===----------------------------------------------------------------------=== +// +// This source file is part of the MarvelService open source project +// +// Copyright (c) 2025 Röck+Cöde VoF. and the MarvelService project authors +// Licensed under the EUPL 1.2 or later. +// +// See LICENSE for license information +// See CONTRIBUTORS for the list of MarvelService project authors +// +//===----------------------------------------------------------------------=== + +import Testing + +import struct Foundation.TimeInterval + +@testable import MarvelService + +@Suite("Time Interval computed properties", .tags(.extension)) +struct TimeIntervalComputedTests { + + // MARK: Properties tests + +#if swift(>=6.2) + @Test(arguments: zip( + Input.timestamps, + Output.timestampAsString + )) + func `asString`( + timestamp: TimeInterval, + expects string: String + ) { + assertAsString( + timestamp: timestamp, + expects: string + ) + } +#else + @Test("asString", arguments: zip( + Input.timestamps, + Output.timestampAsString + )) + func asString( + timestamp: TimeInterval, + expects string: String + ) { + assertAsString( + timestamp: timestamp, + expects: string + ) + } +#endif + +} + +// MARK: - Assertions + +private extension TimeIntervalComputedTests { + + // MARK: Functions + + /// Asserts the timestamp to string conversion. + /// - Parameters: + /// - timestamp: A timestamp to convert. + /// - string: An expected timestamp converted to string. + func assertAsString( + timestamp: TimeInterval, + expects string: String + ) { + // GIVEN + // WHEN + let result = timestamp.asString + + // THEN + #expect(result == string) + } + +} + +// MARK: - Constants + +private extension Output { + /// A list of outcomes that are expected from converting timestamps to string. + static let timestampAsString: [String] = [ + "0.000000", + "1000.000000", + "1000000.000000", + "1000000000.000000" + ] +} diff --git a/Tests/MarvelService/Types/Extensions/Tag+Customs.swift b/Tests/MarvelService/Types/Extensions/Tag+Customs.swift new file mode 100644 index 00000000..1cbacb4f --- /dev/null +++ b/Tests/MarvelService/Types/Extensions/Tag+Customs.swift @@ -0,0 +1,21 @@ +//===----------------------------------------------------------------------=== +// +// This source file is part of the MarvelService open source project +// +// Copyright (c) 2025 Röck+Cöde VoF. and the MarvelService project authors +// Licensed under the EUPL 1.2 or later. +// +// See LICENSE for license information +// See CONTRIBUTORS for the list of MarvelService project authors +// +//===----------------------------------------------------------------------=== + +import Testing + +extension Tag { + + // MARK: Constants + + /// A flag that indicates tests for a type extension. + @Tag static var `extension`: Self +} diff --git a/Tests/MarvelService/Types/Samples/Input.swift b/Tests/MarvelService/Types/Samples/Input.swift new file mode 100644 index 00000000..42ed60c9 --- /dev/null +++ b/Tests/MarvelService/Types/Samples/Input.swift @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------=== +// +// This source file is part of the MarvelService open source project +// +// Copyright (c) 2025 Röck+Cöde VoF. and the MarvelService project authors +// Licensed under the EUPL 1.2 or later. +// +// See LICENSE for license information +// See CONTRIBUTORS for the list of MarvelService project authors +// +//===----------------------------------------------------------------------=== + +import struct Foundation.Date +import struct Foundation.TimeInterval + +/// A namespace assigned for input arguments on test cases. +enum Input { + + // MARK: Constants + + /// A list of timestamps samples. + static let timestamps: [TimeInterval] = [ + Date(timeIntervalSince1970: 0).timeIntervalSince1970, + Date(timeIntervalSince1970: 1_000).timeIntervalSince1970, + Date(timeIntervalSince1970: 1_000_000).timeIntervalSince1970, + Date(timeIntervalSince1970: 1_000_000_000).timeIntervalSince1970, + ] +} diff --git a/Tests/MarvelService/Types/Samples/Output.swift b/Tests/MarvelService/Types/Samples/Output.swift new file mode 100644 index 00000000..0bc713eb --- /dev/null +++ b/Tests/MarvelService/Types/Samples/Output.swift @@ -0,0 +1,14 @@ +//===----------------------------------------------------------------------=== +// +// This source file is part of the MarvelService open source project +// +// Copyright (c) 2025 Röck+Cöde VoF. and the MarvelService project authors +// Licensed under the EUPL 1.2 or later. +// +// See LICENSE for license information +// See CONTRIBUTORS for the list of MarvelService project authors +// +//===----------------------------------------------------------------------=== + +/// A namespace assigned for output arguments on test cases, that are expected results. +enum Output {}