-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathTestCalendar.swift
220 lines (186 loc) · 8.7 KB
/
TestCalendar.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif
class TestCalendar: XCTestCase {
static var allTests: [(String, (TestCalendar) -> () throws -> Void)] {
return [
("test_allCalendars", test_allCalendars),
("test_gettingDatesOnGregorianCalendar", test_gettingDatesOnGregorianCalendar ),
("test_gettingDatesOnHebrewCalendar", test_gettingDatesOnHebrewCalendar ),
("test_gettingDatesOnChineseCalendar", test_gettingDatesOnChineseCalendar),
("test_gettingDatesOnISO8601Calendar", test_gettingDatesOnISO8601Calendar),
("test_copy",test_copy),
("test_addingDates", test_addingDates),
("test_datesNotOnWeekend", test_datesNotOnWeekend),
("test_datesOnWeekend", test_datesOnWeekend),
("test_customMirror", test_customMirror)
// Disabled because this fails on linux https://bugs.swift.org/browse/SR-320
// ("test_currentCalendarRRstability", test_currentCalendarRRstability),
]
}
func test_allCalendars() {
for identifier in [
Calendar.Identifier.buddhist,
Calendar.Identifier.chinese,
Calendar.Identifier.coptic,
Calendar.Identifier.ethiopicAmeteAlem,
Calendar.Identifier.ethiopicAmeteMihret,
Calendar.Identifier.gregorian,
Calendar.Identifier.hebrew,
Calendar.Identifier.indian,
Calendar.Identifier.islamic,
Calendar.Identifier.islamicCivil,
Calendar.Identifier.islamicTabular,
Calendar.Identifier.islamicUmmAlQura,
Calendar.Identifier.iso8601,
Calendar.Identifier.japanese,
Calendar.Identifier.persian,
Calendar.Identifier.republicOfChina
] {
let calendar = Calendar(identifier: identifier)
XCTAssertEqual(identifier,calendar.identifier)
}
}
func test_gettingDatesOnGregorianCalendar() {
let date = Date(timeIntervalSince1970: 1449332351)
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(identifier: "UTC")!
let components = calendar.dateComponents([.year, .month, .day], from: date)
XCTAssertEqual(components.year, 2015)
XCTAssertEqual(components.month, 12)
XCTAssertEqual(components.day, 5)
// Test for problem reported by Malcolm Barclay via swift-corelibs-dev
// https://lists.swift.org/pipermail/swift-corelibs-dev/Week-of-Mon-20161128/001031.html
let fromDate = Date()
let interval = 200
let toDate = Date(timeInterval: TimeInterval(interval), since: fromDate)
let fromToComponents = calendar.dateComponents([.second], from: fromDate, to: toDate)
XCTAssertEqual(fromToComponents.second, interval);
// Issue with 32-bit CF calendar vector on Linux
// Crashes on macOS 10.12.2/Foundation 1349.25
// (Possibly related) rdar://24384757
/*
let interval2 = Int(INT32_MAX) + 1
let toDate2 = Date(timeInterval: TimeInterval(interval2), since: fromDate)
let fromToComponents2 = calendar.dateComponents([.second], from: fromDate, to: toDate2)
XCTAssertEqual(fromToComponents2.second, interval2);
*/
}
func test_gettingDatesOnISO8601Calendar() {
let date = Date(timeIntervalSince1970: 1449332351)
var calendar = Calendar(identifier: .iso8601)
calendar.timeZone = TimeZone(identifier: "UTC")!
let components = calendar.dateComponents([.year, .month, .day], from: date)
XCTAssertEqual(components.year, 2015)
XCTAssertEqual(components.month, 12)
XCTAssertEqual(components.day, 5)
}
func test_gettingDatesOnHebrewCalendar() {
let date = Date(timeIntervalSince1970: 1552580351)
var calendar = Calendar(identifier: .hebrew)
calendar.timeZone = TimeZone(identifier: "UTC")!
let components = calendar.dateComponents([.year, .month, .day], from: date)
XCTAssertEqual(components.year, 5779)
XCTAssertEqual(components.month, 7)
XCTAssertEqual(components.day, 7)
XCTAssertEqual(components.isLeapMonth, false)
}
func test_gettingDatesOnChineseCalendar() {
let date = Date(timeIntervalSince1970: 1591460351.0)
var calendar = Calendar(identifier: .chinese)
calendar.timeZone = TimeZone(identifier: "UTC")!
let components = calendar.dateComponents([.year, .month, .day], from: date)
XCTAssertEqual(components.year, 37)
XCTAssertEqual(components.month, 4)
XCTAssertEqual(components.day, 15)
XCTAssertEqual(components.isLeapMonth, true)
}
func test_currentRRstability() {
var AMSymbols = [String]()
for _ in 1...10 {
let cal = Calendar.current
AMSymbols.append(cal.amSymbol)
}
XCTAssertEqual(AMSymbols.count, 10, "Accessing current calendar should work over multiple callouts")
}
func test_copy() {
var calendar = Calendar.current
//Mutate below fields and check if change is being reflected in copy.
calendar.firstWeekday = 2
calendar.minimumDaysInFirstWeek = 2
let copy = calendar
XCTAssertTrue(copy == calendar)
//verify firstWeekday and minimumDaysInFirstWeek of 'copy'.
XCTAssertEqual(copy.firstWeekday, 2)
XCTAssertEqual(copy.minimumDaysInFirstWeek, 2)
}
func test_addingDates() {
let calendar = Calendar(identifier: .gregorian)
let thisDay = calendar.date(from: DateComponents(year: 2016, month: 10, day: 4))!
let diffComponents = DateComponents(day: 1)
let dayAfter = calendar.date(byAdding: diffComponents, to: thisDay)
let dayAfterComponents = calendar.dateComponents([.year, .month, .day], from: dayAfter!)
XCTAssertEqual(dayAfterComponents.year, 2016)
XCTAssertEqual(dayAfterComponents.month, 10)
XCTAssertEqual(dayAfterComponents.day, 5)
}
func test_datesNotOnWeekend() {
let calendar = Calendar(identifier: .gregorian)
let wednesdayInFebruary = calendar.date(from: DateComponents(year: 2016, month: 2, day: 17))!
XCTAssertFalse(calendar.isDateInWeekend(wednesdayInFebruary))
}
func test_datesOnWeekend() {
let calendar = Calendar(identifier: .gregorian)
let sundayInFebruary = calendar.date(from: DateComponents(year: 2016, month: 2, day: 14))!
XCTAssertTrue(calendar.isDateInWeekend(sundayInFebruary))
}
func test_customMirror() {
let calendar = Calendar(identifier: .gregorian)
let calendarMirror = calendar.customMirror
XCTAssertEqual(calendar.identifier, calendarMirror.descendant("identifier") as? Calendar.Identifier)
XCTAssertEqual(calendar.locale, calendarMirror.descendant("locale") as? Locale)
XCTAssertEqual(calendar.timeZone, calendarMirror.descendant("timeZone") as? TimeZone)
XCTAssertEqual(calendar.firstWeekday, calendarMirror.descendant("firstWeekDay") as? Int)
XCTAssertEqual(calendar.minimumDaysInFirstWeek, calendarMirror.descendant("minimumDaysInFirstWeek") as? Int)
}
}
class TestNSDateComponents: XCTestCase {
static var allTests: [(String, (TestNSDateComponents) -> () throws -> Void)] {
return [
("test_copyNSDateComponents", test_copyNSDateComponents),
]
}
func test_copyNSDateComponents() {
let components = NSDateComponents()
components.year = 1987
components.month = 3
components.day = 17
components.hour = 14
components.minute = 20
components.second = 0
let copy = components.copy(with: nil) as! NSDateComponents
XCTAssertTrue(components.isEqual(copy))
XCTAssertTrue(components == copy)
XCTAssertFalse(components === copy)
XCTAssertEqual(copy.year, 1987)
XCTAssertEqual(copy.month, 3)
XCTAssertEqual(copy.day, 17)
XCTAssertEqual(copy.isLeapMonth, false)
//Mutate NSDateComponents and verify that it does not reflect in the copy
components.hour = 12
XCTAssertEqual(components.hour, 12)
XCTAssertEqual(copy.hour, 14)
}
}