-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestDateFactory.swift
189 lines (174 loc) · 6.44 KB
/
TestDateFactory.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// TestUtil.swift
// PDTest
//
// Created by Juliya Smith on 8/2/21.
// Copyright © 2021 Juliya Smith. All rights reserved.
//
import Foundation
import XCTest
import PDKit
public class TestDateFactory {
public static var defaultDate: Date {
DateFactory.createDefaultDate()
}
/// Attempts to create midnight.
/// If it fails, calls `XCTFail()` and returns the default date.
public static func createTestMidnight(
date: Date?=nil, now: NowProtocol?=nil, file: StaticString=#filePath, line: UInt=#line
) -> Date {
let date = date ?? now?.now ?? Date()
guard let midnight = DateFactory.createMidnight(of: date, now: now) else {
return failed(toCreate: "midnight", file: file, line: line)
}
return midnight
}
/// Attempts to create the date with the specified parameters.
/// If it fails, calls `XCTFail()` and returns the default date.
public static func createTestDate(
hour: Int=0,
minute: Int=0,
second: Int=0,
date: Date?=nil,
now: NowProtocol?=nil,
file: StaticString=#filePath,
line: UInt=#line
) -> Date {
let date = consolidate(date, now)
guard let testDate = DateFactory.createDate(
date, hour: hour, minute: minute, second: second
) else {
return failed(file: file, line: line)
}
return testDate
}
/// Attempts to create a date exactly yesterday.
/// If it fails, calls `XCTFail()` and returns the default date.
public static func createYesterday(
file: StaticString=#filePath, line: UInt=#line
) -> Date {
guard let yesterday = DateFactory.createDate(daysFrom: -1) else {
return failed(toCreate: "yesterday", file: file, line: line)
}
return yesterday
}
/// Attempts to create a date using the specified parameters.
/// If it fails, calls `XCTFail()` and returns the default date.
public static func createTestDate(
daysFrom days: Int=0,
hoursFrom hours: Int=0,
minutesFrom minutes: Int=0,
date: Date?=nil,
now: NowProtocol?=nil,
file: StaticString=#filePath,
line: UInt=#line
) -> Date {
let hoursInDays = 24 * days
let totalHours = hoursInDays + hours
let minutesInHours = 60 * totalHours
let totaMinutes = minutesInHours + minutes
guard let date = createDate(minutes: totaMinutes, fromDate: date, now: now) else {
return failed(when: "\(totaMinutes) minutes ago.", file: file, line: line)
}
return date
}
/// Attempts to create a date using the specified parameters.
/// If it fails, calls `XCTFail()` and returns the default date.
public static func createTestDate(
on onDate: Date?=nil,
at atDate: Time?=nil,
now: NowProtocol?=nil,
file: StaticString=#filePath,
line: UInt=#line
) -> Date {
let onDate = consolidate(onDate, now)
let atDate = consolidate(atDate, now)
guard let testDate = DateFactory.createDate(on: onDate, at: atDate) else {
return failed(when: "on \(onDate) at \(atDate)", file: file, line: line)
}
return testDate
}
/// Attempts to create a date using the specified parameters.
/// If it fails, calls `XCTFail()` and returns the default date.
public static func createTestDate(
byAddingMonths months: Int,
to date: Date?=nil,
now: NowProtocol?=nil,
file: StaticString=#filePath,
line: UInt=#line
) -> Date {
let date = consolidate(date, now)
guard let testDate = DateFactory.createDate(byAddingMonths: months, to: date) else {
return failed(when: "when adding \(months) to \(date).", file: file, line: line)
}
return testDate
}
/// Attempts to create a date using the specified parameters.
/// If it fails, calls `XCTFail()` and returns the default date.
public static func createTestDate(
byAddingHours hours: Int,
to date: Date?=nil,
now: NowProtocol?=nil,
file: StaticString=#filePath,
line: UInt=#line
) -> Date {
let date = consolidate(date, now)
guard let testDate = DateFactory.createDate(byAddingHours: hours, to: date) else {
return failed(when: "when adding \(hours) to \(date).", file: file, line: line)
}
return testDate
}
/// Attempts to create a date using the specified parameters.
/// If it fails, calls `XCTFail()` and returns the default date.
public static func createTestDate(
byAddingMinutes minutes: Int,
to date: Date?=nil,
now: NowProtocol?=nil,
file: StaticString=#filePath,
line: UInt=#line
) -> Date {
let date = consolidate(date, now)
guard let testDate = DateFactory.createDate(byAddingMinutes: minutes, to: date) else {
return failed(when: "when adding \(minutes) to \(date).", file: file, line: line)
}
return testDate
}
/// Attempts to create a date using the specified parameters.
/// If it fails, calls `XCTFail()` and returns the default date.
public static func createTestDate(
byAddingSeconds seconds: Int,
to date: Date?=nil,
now: NowProtocol?=nil,
file: StaticString=#filePath,
line: UInt=#line
) -> Date {
let date = consolidate(date, now)
guard let testDate = DateFactory.createDate(byAddingSeconds: seconds, to: date) else {
return failed(when: "when adding \(seconds) to \(date).", file: file, line: line)
}
return testDate
}
private static func consolidate(_ date: Date?, _ now: NowProtocol?) -> Date {
date ?? now?.now ?? Date()
}
private static func createDate(
minutes: Int=0,
fromDate: Date?=nil,
now: NowProtocol?=nil
) -> Date? {
DateFactory.createDate(minutesFrom: minutes, fromDate: fromDate, now: now)
}
private static func failed(
toCreate dateVariable: String="testDate",
when situation: String?=nil,
file: StaticString=#filePath,
line: UInt=#line
) -> Date {
var duringSituation = ""
if let situation = situation {
duringSituation = " \(situation)"
}
XCTFail("Failed to create \(dateVariable)\(duringSituation)", file: file, line: line)
return defaultDate
}
}