Skip to content

Commit

Permalink
Unbox array with formatter tests now cover all code paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mikezs committed Jun 29, 2016
1 parent 46f5072 commit c0e5f9e
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Tests/UnboxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,25 @@ class UnboxTests: XCTestCase {
}
}

struct AllowInvalidElementsModel: Unboxable {
let date: NSDate
let dateArray: [NSDate]

init(unboxer: Unboxer) {
let formatter = NSDateFormatter()
formatter.dateFormat = "YYYY-MM-dd"
self.date = unboxer.unbox("date", formatter: formatter)
self.dateArray = unboxer.unbox("dateArray", formatter: formatter, allowInvalidElements: true)
}
}

let dictionary: UnboxableDictionary = [
"date" : "2015-12-15",
"dateArray" : ["2015-12-15"]
]

// Valid tests:

do {
let unboxed: Model = try Unbox(dictionary)

Expand All @@ -240,6 +254,28 @@ class UnboxTests: XCTestCase {
XCTFail("\(error)")
}

do {
let invalidValueDateArrayDictionary: UnboxableDictionary = [
"date" : "2015-12-15",
"dateArray" : ["2015-12-tuesday", "2015-12-15"]
]

let unboxed: AllowInvalidElementsModel = try Unbox(invalidValueDateArrayDictionary)

if let firstDate = unboxed.dateArray.first {
let calendar = NSCalendar.currentCalendar()
XCTAssertEqual(calendar.component(.Year, fromDate: firstDate), 2015)
XCTAssertEqual(calendar.component(.Month, fromDate: firstDate), 12)
XCTAssertEqual(calendar.component(.Day, fromDate: firstDate), 15)
} else {
XCTFail("Array empty")
}
} catch {
XCTFail("\(error)")
}

// Invalid tests:

do {
let invalidDateDictionary: UnboxableDictionary = [
"date" : "2015-12-tuesday",
Expand Down Expand Up @@ -278,6 +314,18 @@ class UnboxTests: XCTestCase {
}
}

struct AllowInvalidElementsModel: Unboxable {
let date: NSDate?
let dateArray: [NSDate]?

init(unboxer: Unboxer) {
let formatter = NSDateFormatter()
formatter.dateFormat = "YYYY-MM-dd"
self.date = unboxer.unbox("date", formatter: formatter)
self.dateArray = unboxer.unbox("dateArray", formatter: formatter, allowInvalidElements: true)
}
}

do {
let invalidDictionary: UnboxableDictionary = [
"date" : "2015-12-tuesday",
Expand All @@ -290,6 +338,28 @@ class UnboxTests: XCTestCase {
} catch {
XCTFail("\(error)")
}

do {
let invalidDictionary: UnboxableDictionary = [
"date" : "2015-12-tuesday",
"dateArray" : ["2015-12-15", "2015-12-tuesday"]
]

let unboxed: AllowInvalidElementsModel = try Unbox(invalidDictionary)
XCTAssertNil(unboxed.date)

let calendar = NSCalendar.currentCalendar()
if let firstDate = unboxed.dateArray?.first {
XCTAssertEqual(calendar.component(.Year, fromDate: firstDate), 2015)
XCTAssertEqual(calendar.component(.Month, fromDate: firstDate), 12)
XCTAssertEqual(calendar.component(.Day, fromDate: firstDate), 15)
} else {
XCTFail("Array empty")
}

} catch {
XCTFail("\(error)")
}
}

func testCustomDictionaryKeyType() {
Expand Down

0 comments on commit c0e5f9e

Please sign in to comment.