|
| 1 | +// |
| 2 | +// DateExtensionsTest.swift |
| 3 | +// NativeAppTemplate |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +@testable import NativeAppTemplate |
| 8 | +import Testing |
| 9 | + |
| 10 | +struct DateExtensionsTest { |
| 11 | + private func date(_ iso: String) throws -> Date { |
| 12 | + try #require(iso.iso8601) |
| 13 | + } |
| 14 | + |
| 15 | + @Test |
| 16 | + func dateByAddingNumberOfSecondsAddsPositiveSeconds() throws { |
| 17 | + let date = try date("2026-01-01T00:00:00.000Z") |
| 18 | + let later = date.dateByAddingNumberOfSeconds(3600) |
| 19 | + #expect(later.timeIntervalSince(date) == 3600) |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + func dateByAddingNumberOfSecondsAcceptsNegative() throws { |
| 24 | + let date = try date("2026-01-01T00:00:00.000Z") |
| 25 | + let earlier = date.dateByAddingNumberOfSeconds(-60) |
| 26 | + #expect(earlier.timeIntervalSince(date) == -60) |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + func cardDateStringFormatsAsYearMonthDay() throws { |
| 31 | + let date = try date("2026-04-29T12:34:56.000Z") |
| 32 | + let formatted = date.cardDateString |
| 33 | + // Output is locale-fixed (en_US_POSIX) but uses the device's current |
| 34 | + // time zone, so we just assert the format shape. |
| 35 | + #expect(formatted.count == 10) |
| 36 | + #expect(formatted.contains("/")) |
| 37 | + let parts = formatted.split(separator: "/") |
| 38 | + #expect(parts.count == 3) |
| 39 | + #expect(parts[0].count == 4) // year |
| 40 | + #expect(parts[1].count == 2) // month |
| 41 | + #expect(parts[2].count == 2) // day |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + func cardTimeStringFormatsAsHourMinute() throws { |
| 46 | + let date = try date("2026-04-29T12:34:56.000Z") |
| 47 | + let formatted = date.cardTimeString |
| 48 | + #expect(formatted.count == 5) |
| 49 | + #expect(formatted[formatted.index(formatted.startIndex, offsetBy: 2)] == ":") |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + func cardDateTimeStringConcatenatesDateAndTime() throws { |
| 54 | + let date = try date("2026-04-29T12:34:56.000Z") |
| 55 | + let combined = date.cardDateTimeString |
| 56 | + #expect(combined == "\(date.cardDateString) \(date.cardTimeString)") |
| 57 | + } |
| 58 | +} |
0 commit comments