Skip to content

Commit

Permalink
Included catch for the case where an array of strings expected to tra…
Browse files Browse the repository at this point in the history
…nsform to an array of ints contains elements that cannot be transformed.

Included tests
  • Loading branch information
clayellis committed Jul 26, 2016
1 parent 35e0b25 commit 5a4f0b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
9 changes: 7 additions & 2 deletions Sources/Unbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,13 @@ public class Unboxer {
}

let transformedStringArray = UnboxValueResolver<[String]>(self).resolveOptionalValueForKey(key, isKeyPath: isKeyPath, transform: { stringArray -> [T]? in
return stringArray.flatMap {
return T.transformUnboxedString($0)
do {
return try stringArray.flatMap {
guard let transformedUnboxedString = T.transformUnboxedString($0) else { throw UnboxValueError.InvalidValue($0, key) }
return transformedUnboxedString
}
} catch {
return nil
}
})

Expand Down
23 changes: 20 additions & 3 deletions Tests/UnboxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1358,13 +1358,13 @@ class UnboxTests: XCTestCase {
}
}

func testUnboxingArrayOfStringTransformedToInt() {
func testUnboxingArrayOfStringsTransformedToInt() {
let dictionary: UnboxableDictionary = ["intArray": ["123", "456", "789"]]

struct ModelA: Unboxable {
let intArray: [Int]
init(unboxer: Unboxer) {
intArray = unboxer.unbox("intArray")
self.intArray = unboxer.unbox("intArray")
}
}

Expand All @@ -1374,11 +1374,28 @@ class UnboxTests: XCTestCase {
XCTAssertEqual(modelA.intArray[1], 456)
XCTAssertEqual(modelA.intArray[2], 789)
} catch {
print(error)
XCTFail()
}
}

func testUnboxingArrayOfBadStringsTransformedToInt() {
let dictionary: UnboxableDictionary = ["intArray": ["123", "abc", "789"]]

struct ModelA: Unboxable {
let intArray: [Int]
init(unboxer: Unboxer) {
self.intArray = unboxer.unbox("intArray")
}
}

do {
try Unbox(dictionary) as ModelA
XCTFail()
} catch {
// Test Passed
}
}

}

private func UnboxTestDictionaryWithAllRequiredKeysWithValidValues(nested: Bool) -> UnboxableDictionary {
Expand Down

0 comments on commit 5a4f0b9

Please sign in to comment.