I have an array of dates that I would like to get an NSDate object from, using an NSDateFormatter.
let dates = [
"Tue, 04 Feb 2014 22:03:45 Z",
"Sun, 05 Jun 2016 08:35:14 Z",
"Sun, 05 Jun 2016 08:54 +0000",
"Mon, 21 Mar 2016 13:31:23 GMT",
"Sat, 04 Jun 2016 16:26:37 EDT",
"Sat, 04 Jun 2016 11:55:28 PDT",
"Sun, 5 Jun 2016 01:51:07 -0700",
"Sun, 5 Jun 2016 01:30:30 -0700",
"Thu, 02 June 2016 14:43:37 GMT",
"Sun, 5 Jun 2016 01:49:56 -0700",
"Fri, 27 May 2016 14:32:19 -0400",
"Sun, 05 Jun 2016 01:45:00 -0700",
"Sun, 05 Jun 2016 08:32:03 +0000",
"Sat, 04 Jun 2016 22:33:02 +0000",
"Sun, 05 Jun 2016 01:52:30 -0700",
"Thu, 02 Jun 2016 15:24:37 +0000"
]
I was going with the GWT pattern, but felt strange structuring my unit test like that, because I need to write a dozen or more distinct test funtions for each case.
Can anyone suggest a better approach to this? Or is this an acceptable usage of the "Given, When, Then" pattern?
..I really don't want to have a testRFC822DateFormatter1(), testRFC822DateFormatter2(), testRFC822DateFormatter3(),...
func testRFC822DateFormatter() {
// Given
let rfc822DateFormatter = RFC822DateFormatter()
let dateString = "Tue, 04 Feb 2014 22:03:45 Z"
// When
let date = rfc822DateFormatter.dateFromString(dateString)
// Then
XCTAssertNotNil(date)
let components = NSCalendar.currentCalendar().components([.Year, .Month, .Day, .Hour, .Minute, .Second, .TimeZone, .Calendar], fromDate: date!)
XCTAssertEqual(components.day, 4)
XCTAssertEqual(components.month, 2)
XCTAssertEqual(components.year, 2014)
XCTAssertEqual(components.hour, 22)
XCTAssertEqual(components.minute, 3)
XCTAssertEqual(components.second, 45)
XCTAssertEqual(components.timeZone?.daylightSavingTimeOffset, 3600)
XCTAssertEqual(components.timeZone?.secondsFromGMT, 3600)
XCTAssertEqual(components.calendar?.calendarIdentifier, NSCalendarIdentifierGregorian)
}
Thank you!