-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refined Dictionary get/set extensions
- Loading branch information
Showing
2 changed files
with
80 additions
and
35 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
56 changes: 56 additions & 0 deletions
56
Tests/SwiftConvenienceTests/Extensions Tests/DictionaryTests.swift
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,56 @@ | ||
import SwiftConvenience | ||
|
||
import XCTest | ||
|
||
class DictionaryTests: XCTestCase { | ||
func test_keyPath() { | ||
let dict: [String : Any] = [ | ||
"lv1_key1": "lv1_val1", | ||
"lv1_key2": [ | ||
2: "lv2_val2", | ||
"lv2_key1": 21, | ||
"lv2_key2": [ | ||
"lv3_key1": "lv3_val1" | ||
] | ||
] | ||
] | ||
|
||
XCTAssertEqual(dict[keyPath: ["lv1_key1"]] as? String, "lv1_val1") | ||
XCTAssertEqual(dict[keyPath: ["lv1_key2", 2]] as? String, "lv2_val2") | ||
XCTAssertEqual(dict[keyPath: ["lv1_key2", "lv2_key1"]] as? Int, 21) | ||
XCTAssertEqual(dict[keyPath: ["lv1_key2", "lv2_key2", "lv3_key1"]] as? String, "lv3_val1") | ||
|
||
XCTAssertEqual(dict[dotPath: "lv1_key1"] as? String, "lv1_val1") | ||
XCTAssertNil(dict[dotPath: "lv1_key2.2"]) | ||
XCTAssertEqual(dict[dotPath: "lv1_key2.lv2_key1"] as? Int, 21) | ||
XCTAssertEqual(dict[dotPath: "lv1_key2.lv2_key2.lv3_key1"] as? String, "lv3_val1") | ||
} | ||
|
||
func test_insertKeyPath() throws { | ||
var dict: [String : Any] = [:] | ||
|
||
try dict.insert(value: "lv1_val1", at: ["lv1_key1"]) | ||
try dict.insert(value: "lv2_val2", at: ["lv1_key2", 2]) | ||
try dict.insert(value: 21, at: ["lv1_key2", "lv2_key1"]) | ||
try dict.insert(value: "lv3_val1", at: ["lv1_key2", "lv2_key2", "lv3_key1"]) | ||
|
||
XCTAssertEqual(dict[keyPath: ["lv1_key1"]] as? String, "lv1_val1") | ||
XCTAssertEqual(dict[keyPath: ["lv1_key2", 2]] as? String, "lv2_val2") | ||
XCTAssertEqual(dict[keyPath: ["lv1_key2", "lv2_key1"]] as? Int, 21) | ||
XCTAssertEqual(dict[keyPath: ["lv1_key2", "lv2_key2", "lv3_key1"]] as? String, "lv3_val1") | ||
} | ||
|
||
func test_insertDotPath() throws { | ||
var dict: [String : Any] = [:] | ||
|
||
try dict.insert(value: "lv1_val1", at: ["lv1_key1"]) | ||
try dict.insert(value: "lv2_val2", at: "lv1_key2.2") | ||
try dict.insert(value: 21, at: "lv1_key2.lv2_key1") | ||
try dict.insert(value: "lv3_val1", at: "lv1_key2.lv2_key2.lv3_key1") | ||
|
||
XCTAssertEqual(dict[keyPath: ["lv1_key1"]] as? String, "lv1_val1") | ||
XCTAssertEqual(dict[keyPath: ["lv1_key2", "2"]] as? String, "lv2_val2") | ||
XCTAssertEqual(dict[keyPath: ["lv1_key2", "lv2_key1"]] as? Int, 21) | ||
XCTAssertEqual(dict[keyPath: ["lv1_key2", "lv2_key2", "lv3_key1"]] as? String, "lv3_val1") | ||
} | ||
} |