diff --git a/Tests/CorePromise/ThenableTests.swift b/Tests/CorePromise/ThenableTests.swift index 485833c..4063a87 100644 --- a/Tests/CorePromise/ThenableTests.swift +++ b/Tests/CorePromise/ThenableTests.swift @@ -2,6 +2,22 @@ import PromiseKit import Dispatch import XCTest +struct Person: Equatable { + let name: String + let age: Int? + let isStudent: Bool + + init( + name: String = "", + age: Int? = nil, + isStudent: Bool = false + ) { + self.name = name + self.age = age + self.isStudent = isStudent + } +} + class ThenableTests: XCTestCase { func testGet() { let ex1 = expectation(description: "") @@ -30,8 +46,8 @@ class ThenableTests: XCTestCase { #if swift(>=4) func testMapByKeyPath() { let ex = expectation(description: "") - Promise.value("Hello world").map(\.count).done { - XCTAssertEqual($0, 11) + Promise.value(Person(name: "Max")).map(\.name).done { + XCTAssertEqual($0, "Max") ex.fulfill() }.silenceWarning() wait(for: [ex], timeout: 10) @@ -94,6 +110,39 @@ class ThenableTests: XCTestCase { wait(for: [ex], timeout: 10) } + #if swift(>=4) + func testCompactMapByKeyPath() { + let ex = expectation(description: "") + Promise.value(Person(name: "Roman", age: 26)).compactMap(\.age).done { + XCTAssertEqual($0, 26) + ex.fulfill() + }.silenceWarning() + wait(for: [ex], timeout: 10) + } + #endif + + func testMapValues() { + let ex = expectation(description: "") + Promise.value([14, 20, 45]).mapValues { + $0 * 2 + }.done { + XCTAssertEqual([28, 40, 90], $0) + ex.fulfill() + }.silenceWarning() + wait(for: [ex], timeout: 10) + } + + #if swift(>=4) + func testMapValuesByKeyPath() { + let ex = expectation(description: "") + Promise.value([Person(name: "Max"), Person(name: "Roman"), Person(name: "John")]).mapValues(\.name).done { + XCTAssertEqual(["Max", "Roman", "John"], $0) + ex.fulfill() + }.silenceWarning() + wait(for: [ex], timeout: 10) + } + #endif + func testCompactMapValues() { let ex = expectation(description: "") Promise.value(["1","2","a","4"]).compactMapValues { @@ -105,6 +154,17 @@ class ThenableTests: XCTestCase { wait(for: [ex], timeout: 10) } + #if swift(>=4) + func testCompactMapValuesByKeyPath() { + let ex = expectation(description: "") + Promise.value([Person(name: "Max"), Person(name: "Roman", age: 26), Person(name: "John", age: 23)]).compactMapValues(\.age).done { + XCTAssertEqual([26, 23], $0) + ex.fulfill() + }.silenceWarning() + wait(for: [ex], timeout: 10) + } + #endif + func testThenMap() { let ex = expectation(description: "") Promise.value([1,2,3,4]).thenMap { @@ -127,6 +187,28 @@ class ThenableTests: XCTestCase { wait(for: [ex], timeout: 10) } + func testFilterValues() { + let ex = expectation(description: "") + Promise.value([Person(name: "Max"), Person(name: "Roman", age: 26, isStudent: false), Person(name: "John", age: 23, isStudent: true)]).filterValues { + $0.isStudent + }.done { + XCTAssertEqual([Person(name: "John", age: 23, isStudent: true)], $0) + ex.fulfill() + }.silenceWarning() + wait(for: [ex], timeout: 10) + } + + #if swift(>=4) + func testFilterValuesByKeyPath() { + let ex = expectation(description: "") + Promise.value([Person(name: "Max"), Person(name: "Roman", age: 26, isStudent: false), Person(name: "John", age: 23, isStudent: true)]).filterValues(\.isStudent).done { + XCTAssertEqual([Person(name: "John", age: 23, isStudent: true)], $0) + ex.fulfill() + }.silenceWarning() + wait(for: [ex], timeout: 10) + } + #endif + func testLastValueForEmpty() { XCTAssertTrue(Promise.value([]).lastValue.isRejected) }