Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Supplementary test helper code for https://jakubturek.com/bottom-up-unit-testing/
extension String {
var date: Date {
let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
guard let date = formatter.date(from: self) else {
fatalError("Cannot build a date from \(self) string")
}
return date
}
}
class DateFromStringSpec: QuickSpec {
override func spec() {
describe("String+Date") {
it("should return correct date for 1990-01-09 12:00:00") {
expect("1990-01-09 12:00:00".date)
== Date(timeIntervalSince1970: 631886400)
}
it("should return correct date for 2020-11-11 11:11:11") {
expect("2020-11-11 11:11:11".date)
== Date(timeIntervalSince1970: 1605093071)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment