damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit badbd8a92ed8bc8a6928f19b9a2c9d4b8cf1bffb
parent aec61906ae44f4367fd410f438181fc171dfb0d3
Author: Terry Yiu <963907+tyiu@users.noreply.github.com>
Date:   Sun,  1 Jan 2023 22:38:25 -0500

Fix flakey TimeAgo test by anchoring locale to en_US

Closes: #210

Diffstat:
Mdamus/Util/TimeAgo.swift | 3+--
MdamusTests/TimeAgoTests.swift | 37++++++++++++++++++++-----------------
2 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/damus/Util/TimeAgo.swift b/damus/Util/TimeAgo.swift @@ -7,9 +7,8 @@ import Foundation -public func time_ago_since(_ date: Date) -> String { +public func time_ago_since(_ date: Date, _ calendar: Calendar = Calendar.current) -> String { - let calendar = Calendar.current let now = Date() let unitFlags: NSCalendar.Unit = [.second, .minute, .hour, .day, .weekOfMonth, .month, .year] diff --git a/damusTests/TimeAgoTests.swift b/damusTests/TimeAgoTests.swift @@ -11,24 +11,27 @@ import XCTest final class TimeAgoTests: XCTestCase { func testTimeAgoSince() { - XCTAssertEqual(time_ago_since(Date.now), "now") - XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-2)), "now") - XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-3)), "3s") - XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-59)), "59s") - XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-60)), "1min") - XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-3599)), "59min") - XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-3600)), "1h") - XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-86399)), "23h") - XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-86400)), "1d") - XCTAssertEqual(time_ago_since(Calendar.current.date(byAdding: .weekOfMonth, value: -1, to: Date.now)!.addingTimeInterval(1)), "6d") - XCTAssertEqual(time_ago_since(Calendar.current.date(byAdding: .weekOfMonth, value: -1, to: Date.now)!), "1w") - XCTAssertEqual(time_ago_since(Calendar.current.date(byAdding: .weekOfMonth, value: -2, to: Date.now)!), "2w") - XCTAssertEqual(time_ago_since(Calendar.current.date(byAdding: .weekOfMonth, value: -3, to: Date.now)!), "3w") + let locale = Locale(identifier: "en_US") + let calendar = locale.calendar + + XCTAssertEqual(time_ago_since(Date.now, calendar), "now") + XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-2), calendar), "now") + XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-3), calendar), "3s") + XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-59), calendar), "59s") + XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-60), calendar), "1min") + XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-3599), calendar), "59min") + XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-3600), calendar), "1h") + XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-86399), calendar), "23h") + XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-86400), calendar), "1d") + XCTAssertEqual(time_ago_since(calendar.date(byAdding: .weekOfMonth, value: -1, to: Date.now)!.addingTimeInterval(1), calendar), "6d") + XCTAssertEqual(time_ago_since(calendar.date(byAdding: .weekOfMonth, value: -1, to: Date.now)!, calendar), "1w") + XCTAssertEqual(time_ago_since(calendar.date(byAdding: .weekOfMonth, value: -2, to: Date.now)!, calendar), "2w") + XCTAssertEqual(time_ago_since(calendar.date(byAdding: .weekOfMonth, value: -3, to: Date.now)!, calendar), "3w") // Not testing the 4-5 week boundary since how it is formatted depends on which month and year it is currently when this test executes. - XCTAssertEqual(time_ago_since(Calendar.current.date(byAdding: .month, value: -1, to: Date.now)!), "1mo") - XCTAssertEqual(time_ago_since(Calendar.current.date(byAdding: .year, value: -1, to: Date.now)!.addingTimeInterval(1)), "11mo") - XCTAssertEqual(time_ago_since(Calendar.current.date(byAdding: .year, value: -1, to: Date.now)!), "1y") - XCTAssertEqual(time_ago_since(Calendar.current.date(byAdding: .year, value: -1000, to: Date.now)!), "1,000y") + XCTAssertEqual(time_ago_since(calendar.date(byAdding: .month, value: -1, to: Date.now)!, calendar), "1mo") + XCTAssertEqual(time_ago_since(calendar.date(byAdding: .year, value: -1, to: Date.now)!.addingTimeInterval(1), calendar), "11mo") + XCTAssertEqual(time_ago_since(calendar.date(byAdding: .year, value: -1, to: Date.now)!, calendar), "1y") + XCTAssertEqual(time_ago_since(calendar.date(byAdding: .year, value: -1000, to: Date.now)!, calendar), "1,000y") } }