-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
196 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// | ||
// ArrayTests.swift | ||
// Decodable | ||
// | ||
// Created by Johannes Lund on 2015-07-19. | ||
// Copyright © 2015 anviking. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import Decodable | ||
|
||
class DecodableArrayTests: XCTestCase { | ||
|
||
func testDecodeAnyDecodableArraySuccess() { | ||
// given | ||
let key = "key" | ||
let value: NSArray = ["value1", "value2", "value3"] | ||
let dictionary: NSDictionary = [key: value] | ||
// when | ||
let result = try! dictionary => key as Array<String> | ||
// then | ||
XCTAssertEqual(result, value) | ||
} | ||
|
||
func testDecodeNestedDecodableArraySuccess() { | ||
// given | ||
let key = "key" | ||
let value: NSArray = ["value1", "value2", "value3"] | ||
let dictionary: NSDictionary = [key: [key: value]] | ||
// when | ||
let result = try! dictionary => key => key as Array<String> | ||
// then | ||
XCTAssertEqual(result, value) | ||
} | ||
|
||
func testDecodeAnyDecodableOptionalArraySuccess() { | ||
// given | ||
let key = "key" | ||
let value = ["value"] | ||
let dictionary: NSDictionary = [key: value] | ||
// when | ||
let string = dictionary => key as [String]? | ||
// then | ||
XCTAssertEqual(string!, value) | ||
} | ||
|
||
func testDecodeAnyDecodableNestedOptionalArraySuccess() { | ||
// given | ||
let key = "key" | ||
let value = ["value"] | ||
let dictionary: NSDictionary = [key: [key: value]] | ||
// when | ||
let string = dictionary => key => key as [String]? | ||
// then | ||
XCTAssertEqual(string!, value) | ||
} | ||
|
||
func testDecodeAnyDecodableOptionalArrayNilSuccess() { | ||
// given | ||
let key = "key" | ||
let dictionary: NSDictionary = [key: NSNull()] | ||
// when | ||
let string = dictionary => key as [String]? | ||
// then | ||
XCTAssertNil(string) | ||
} | ||
|
||
func testDecodeAnyDecodableOptionalArrayMissingKeySuccess() { | ||
// given | ||
let key = "key" | ||
let dictionary = NSDictionary() | ||
// when | ||
let string = dictionary => key as [String]? | ||
// then | ||
XCTAssertNil(string) | ||
} | ||
|
||
|
||
// MARK: =>? | ||
|
||
func testDecodeSafeArraySuccess() { | ||
// given | ||
let key = "key" | ||
let value = ["A", "B", "C"] | ||
let dictionary: NSDictionary = [key: value] | ||
// when | ||
let string = try! dictionary =>? key as [String] | ||
// then | ||
XCTAssertEqual(string, value) | ||
} | ||
|
||
func testDecodeSafeArrayCatchTypeExceptionMismatch() { | ||
// given | ||
let key = "key" | ||
let value = ["A", 2, "B"] | ||
let dictionary: NSDictionary = [key: value] | ||
// when | ||
let string = try! dictionary =>? key as [String] | ||
// then | ||
XCTAssertEqual(string, ["A", "B"]) | ||
} | ||
|
||
func testDecodeSafeArrayCatchTypeMismatchExceptionInObjects() { | ||
// given | ||
let key = "key" | ||
let value = [["id": "007", "login": "mradams"], ["id": 1, "login": "jenglish"]] | ||
let dictionary: NSDictionary = [key: value] | ||
// when | ||
let array = try! dictionary =>? key as [Owner] | ||
// then | ||
XCTAssertEqual(array, [Owner(id: 1, login: "jenglish")]) | ||
} | ||
|
||
func testDecodeSafeArrayCatchJSONNotObjectException() { | ||
// given | ||
let key = "key" | ||
let value = [["id": 7, "login": "mradams"], 2] | ||
let dictionary: NSDictionary = [key: value] | ||
// when | ||
let array = try! dictionary =>? key as [Owner] | ||
// then | ||
XCTAssertEqual(array, [Owner(id: 7, login: "mradams")]) | ||
} | ||
|
||
func testDecodeSafeArrayCatchMissingKeyException() { | ||
// given | ||
let key = "key" | ||
let value = [["login": "mradams"], ["id": 1, "login": "jenglish"]] | ||
let dictionary: NSDictionary = [key: value] | ||
// when | ||
let array = try! dictionary =>? key as [Owner] | ||
// then | ||
XCTAssertEqual(array, [Owner(id: 1, login: "jenglish")]) | ||
} | ||
} |
Oops, something went wrong.