Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 000982c

Browse files
committed
Fixes for tests
1 parent de8d19a commit 000982c

File tree

9 files changed

+30
-54
lines changed

9 files changed

+30
-54
lines changed

JSONCodable/JSONDecodable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public protocol JSONDecodable {
5252
}
5353

5454
public extension JSONDecodable {
55-
init?(JSONDictionary: JSONObject) {
55+
public init?(optional: JSONObject) {
5656
do {
57-
try self.init(object: JSONDictionary)
57+
try self.init(object: optional)
5858
} catch {
5959
return nil
6060
}

JSONCodableTests/Company.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,9 @@ func ==(lhs: Company, rhs: Company) -> Bool {
2121
extension Company: JSONEncodable {}
2222

2323
extension Company: JSONDecodable {
24-
init?(JSONDictionary: JSONObject) {
25-
let decoder = JSONDecoder(object: JSONDictionary)
26-
do {
27-
name = try decoder.decode("name")
28-
address = try decoder.decode("address")
29-
}
30-
catch {
31-
return nil
32-
}
24+
init(object: JSONObject) throws {
25+
let decoder = JSONDecoder(object: object)
26+
name = try decoder.decode("name")
27+
address = try decoder.decode("address")
3328
}
3429
}

JSONCodableTests/EnumTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ class EnumTests: XCTestCase {
1717
let decodedValue2 = Food(name: "Seaweed Pasta", cuisines: [.Italian, .Japanese])
1818

1919
func testDecodingEnum() {
20-
guard let fruit = Fruit(JSONDictionary: encodedValue) else {
20+
let a = Fruit(optional: encodedValue)
21+
guard let fruit = Fruit(optional: encodedValue) else {
2122
XCTFail()
2223
return
2324
}
2425

2526
XCTAssertEqual(fruit, decodedValue)
2627

27-
guard let food = Food(JSONDictionary: encodedValue2) else {
28+
guard let food = Food(optional: encodedValue2) else {
2829
XCTFail()
2930
return
3031
}

JSONCodableTests/Food.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,10 @@ func ==(lhs: Food, rhs: Food) -> Bool {
3232
}
3333

3434
extension Food: JSONCodable {
35-
init?(JSONDictionary: JSONObject) {
36-
let decoder = JSONDecoder(object: JSONDictionary)
37-
do {
38-
name = try decoder.decode("name")
39-
cuisines = try decoder.decode("cuisines")
40-
}
41-
catch {
42-
return nil
43-
}
35+
init(object: JSONObject) throws {
36+
let decoder = JSONDecoder(object: object)
37+
name = try decoder.decode("name")
38+
cuisines = try decoder.decode("cuisines")
4439
}
4540

4641
func toJSON() throws -> AnyObject {

JSONCodableTests/Fruit.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@ func ==(lhs: Fruit, rhs: Fruit) -> Bool {
2424
}
2525

2626
extension Fruit: JSONCodable {
27-
init?(JSONDictionary: JSONObject) {
28-
let decoder = JSONDecoder(object: JSONDictionary)
29-
do {
30-
name = try decoder.decode("name")
31-
color = try decoder.decode("color")
32-
}
33-
catch {
34-
return nil
35-
}
27+
init(object: JSONObject) throws {
28+
let decoder = JSONDecoder(object: object)
29+
name = try decoder.decode("name")
30+
color = try decoder.decode("color")
3631
}
3732

3833
func toJSON() throws -> AnyObject {

JSONCodableTests/ImageAsset.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,9 @@ extension ImageAsset: JSONEncodable {
2828
}
2929

3030
extension ImageAsset: JSONDecodable {
31-
init?(JSONDictionary: JSONObject) {
32-
let decoder = JSONDecoder(object: JSONDictionary)
33-
do {
34-
name = try decoder.decode("name")
35-
uri = try decoder.decode("uri", transformer: JSONTransformers.StringToNSURL)
36-
}
37-
catch {
38-
return nil
39-
}
31+
init(object: JSONObject) throws {
32+
let decoder = JSONDecoder(object: object)
33+
name = try decoder.decode("name")
34+
uri = try decoder.decode("uri", transformer: JSONTransformers.StringToNSURL)
4035
}
4136
}

JSONCodableTests/RegularTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class RegularTests: XCTestCase {
3434
])
3535

3636
func testDecodingRegular() {
37-
guard let user = User(JSONDictionary: encodedValue) else {
37+
guard let user = User(optional: encodedValue) else {
3838
XCTFail()
3939
return
4040
}

JSONCodableTests/TransformerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class TransformerTests: XCTestCase {
2020
)
2121

2222
func testDecodingTransformer() {
23-
guard let asset = ImageAsset(JSONDictionary: encodedValue) else {
23+
guard let asset = ImageAsset(optional: encodedValue) else {
2424
XCTFail()
2525
return
2626
}

JSONCodableTests/User.swift

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,12 @@ extension User: JSONEncodable {
3838
}
3939

4040
extension User: JSONDecodable {
41-
init?(JSONDictionary: JSONObject) {
42-
let decoder = JSONDecoder(object: JSONDictionary)
43-
do {
44-
id = try decoder.decode("id")
45-
name = try decoder.decode("full_name")
46-
email = try decoder.decode("email")
47-
company = try decoder.decode("company")
48-
friends = try decoder.decode("friends")
49-
}
50-
catch {
51-
return nil
52-
}
41+
init(object: JSONObject) throws {
42+
let decoder = JSONDecoder(object: object)
43+
id = try decoder.decode("id")
44+
name = try decoder.decode("full_name")
45+
email = try decoder.decode("email")
46+
company = try decoder.decode("company")
47+
friends = try decoder.decode("friends")
5348
}
5449
}

0 commit comments

Comments
 (0)