struct Person and tests for new mapping functions

This commit is contained in:
Roman Podymov 2020-01-08 21:12:41 +01:00 committed by GitHub
parent 5fb0714f47
commit 35379bb8a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}