Browse Source

Fix flakey TimeAgo test by anchoring locale to en_US

Closes: #210
post-button-style
Terry Yiu 2 years ago
committed by William Casarin
parent
commit
badbd8a92e
  1. 3
      damus/Util/TimeAgo.swift
  2. 37
      damusTests/TimeAgoTests.swift

3
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]

37
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")
}
}

Loading…
Cancel
Save