-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathCustomDateComponentsFormattingTests.swift
90 lines (70 loc) · 2.53 KB
/
CustomDateComponentsFormattingTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// CustomDateComponentsFormattingTests.swift
// MullvadVPNTests
//
// Created by pronebird on 14/05/2020.
// Copyright © 2020 Mullvad VPN AB. All rights reserved.
//
@testable import MullvadVPN
import XCTest
class CustomDateComponentsFormattingTests: XCTestCase {
func testEqualToTwoYearsFormatting() throws {
var dateComponents = DateComponents()
dateComponents.year = 2
let (startDate, endDate) = makeDateRange(addingComponents: dateComponents)
let result = CustomDateComponentsFormatting.localizedString(
from: startDate,
to: endDate,
calendar: calendar,
unitsStyle: .full
)
XCTAssertEqual(result, "2 years")
}
func testTimeIsPassedFormatting() throws {
var dateComponents = DateComponents()
dateComponents.day = -1
let (startDate, endDate) = makeDateRange(addingComponents: dateComponents)
let result = CustomDateComponentsFormatting.localizedString(
from: startDate,
to: endDate,
calendar: calendar,
unitsStyle: .full
)
XCTAssertEqual(result, "Less than a day")
}
func testLessThanTwoYearsFormatting() throws {
var dateComponents = DateComponents()
dateComponents.day = 365
let (startDate, endDate) = makeDateRange(addingComponents: dateComponents)
let result = CustomDateComponentsFormatting.localizedString(
from: startDate,
to: endDate,
calendar: calendar,
unitsStyle: .full
)
XCTAssertEqual(result, "365 days")
}
func testCloseToOneDayFormatting() throws {
var dateComponents = DateComponents()
dateComponents.hour = 23
dateComponents.minute = 30
let (startDate, endDate) = makeDateRange(addingComponents: dateComponents)
let result = CustomDateComponentsFormatting.localizedString(
from: startDate,
to: endDate,
calendar: calendar,
unitsStyle: .full
)
XCTAssertEqual(result, "Less than a day")
}
private func makeDateRange(addingComponents dateComponents: DateComponents) -> (Date, Date) {
let startDate = Date()
let endDate = calendar.date(byAdding: dateComponents, to: startDate)!
return (startDate, endDate)
}
private var calendar: Calendar {
var calendar = Calendar(identifier: .gregorian)
calendar.locale = Locale(identifier: "en_US_POSIX")
return calendar
}
}