Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
import XCTest
7
8
class RelativeDatesTests: XCTestCase {
9
func testRelativeDates() {
10
let dateOrig = Date()
11
var date = Date(timeInterval: 0, since: dateOrig)
12
13
XCTAssertEqual(date.toRelativeTimeString(), "just now")
14
15
date = Date(timeInterval: -10, since: dateOrig)
16
XCTAssertEqual(date.toRelativeTimeString(), "just now")
17
18
date = Date(timeInterval: -60, since: dateOrig)
19
XCTAssertEqual(date.toRelativeTimeString(), ("today at " + DateFormatter.localizedString(from: date, dateStyle: .none, timeStyle: .short)))
20
21
let calendar = Calendar.autoupdatingCurrent
22
date = calendar.date(byAdding: .day, value: -1, to: dateOrig)!
23
XCTAssertEqual(date.toRelativeTimeString(), "yesterday")
24
25
date = calendar.date(byAdding: .day, value: -2, to: dateOrig)!
26
XCTAssertEqual(date.toRelativeTimeString(), "this week")
27
28
date = calendar.date(byAdding: .day, value: -7, to: dateOrig)!
29
XCTAssertEqual(date.toRelativeTimeString(), "more than a week ago")
30
31
date = calendar.date(byAdding: .day, value: -7 * 5, to: dateOrig)!
32
XCTAssertEqual(date.toRelativeTimeString(), "more than a month ago")
33
34
date = Date(timeInterval: -60 * 60 * 24 * 7 * 5 * 2, since: dateOrig)
35
XCTAssertEqual(date.toRelativeTimeString(), DateFormatter.localizedString(from: date, dateStyle: .short, timeStyle: .short))
36
37
date = Date(timeInterval: -60 * 60 * 24 * 7 * 5 * 12 * 2, since: dateOrig)
38
XCTAssertEqual(date.toRelativeTimeString(), DateFormatter.localizedString(from: date, dateStyle: .short, timeStyle: .short))
39
}
40
}