damus

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

TimeAgoTests.swift (2221B)


      1 //
      2 //  TimeAgoTests.swift
      3 //  damusTests
      4 //
      5 //  Created by Terry Yiu on 12/30/22.
      6 //
      7 
      8 import XCTest
      9 @testable import damus
     10 
     11 final class TimeAgoTests: XCTestCase {
     12 
     13     func testTimeAgoSince() {
     14         let locale = Locale(identifier: "en-US")
     15         let calendar = locale.calendar
     16 
     17         XCTAssertEqual(time_ago_since(Date.now, calendar), "now")
     18         XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-2), calendar), "now")
     19         XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-3), calendar), "3s")
     20         XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-59), calendar), "59s")
     21         XCTAssertEqual(time_ago_since(Date.now.addingTimeInterval(-60), calendar), "1m")
     22         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .hour, value: -1, to: Date.now)!.addingTimeInterval(1), calendar), "59m")
     23         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .hour, value: -1, to: Date.now)!, calendar), "1h")
     24         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .day, value: -1, to: Date.now)!.addingTimeInterval(1), calendar), "23h")
     25         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .day, value: -1, to: Date.now)!, calendar), "1d")
     26         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .weekOfMonth, value: -1, to: Date.now)!.addingTimeInterval(1), calendar), "6d")
     27         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .weekOfMonth, value: -1, to: Date.now)!, calendar), "1w")
     28         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .weekOfMonth, value: -2, to: Date.now)!, calendar), "2w")
     29         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .weekOfMonth, value: -3, to: Date.now)!, calendar), "3w")
     30         // 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.
     31         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .month, value: -1, to: Date.now)!, calendar), "1mo")
     32         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .year, value: -1, to: Date.now)!.addingTimeInterval(1), calendar), "11mo")
     33         XCTAssertEqual(time_ago_since(calendar.date(byAdding: .year, value: -1, to: Date.now)!, calendar), "1y")
     34     }
     35 
     36 }