Support `.removeDuplicates()` for `Defaults.publisher` (#60)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
This commit is contained in:
parent
030c70a55e
commit
760dbfb077
|
@ -446,3 +446,11 @@ extension Defaults.ObservationOptions {
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(macOS 10.13, macOSApplicationExtension 10.13, watchOSApplicationExtension 4.0, *)
|
||||||
|
extension Defaults.NSSecureCodingKeyChange: Equatable where Value: Equatable { }
|
||||||
|
|
||||||
|
@available(macOS 10.13, macOSApplicationExtension 10.13, watchOSApplicationExtension 4.0, *)
|
||||||
|
extension Defaults.NSSecureCodingOptionalKeyChange: Equatable where Value: Equatable { }
|
||||||
|
|
||||||
|
extension Defaults.KeyChange: Equatable where Value: Equatable { }
|
||||||
|
|
|
@ -812,4 +812,117 @@ final class DefaultsTests: XCTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
||||||
|
func testRemoveDuplicatesObserveKeyCombine() {
|
||||||
|
let key = Defaults.Key<Bool>("observeKey", default: false)
|
||||||
|
let expect = expectation(description: "Observation closure being called")
|
||||||
|
|
||||||
|
let inputArray = [true, false, false, false, false, false, false, true]
|
||||||
|
let expectedArray = [true, false, true]
|
||||||
|
|
||||||
|
let cancellable = Defaults
|
||||||
|
.publisher(key, options: [])
|
||||||
|
.removeDuplicates()
|
||||||
|
.map(\.newValue)
|
||||||
|
.collect(expectedArray.count)
|
||||||
|
.sink { result in
|
||||||
|
print("Result array: \(result)")
|
||||||
|
result == expectedArray ? expect.fulfill() : XCTFail("Expected Array is not matched")
|
||||||
|
}
|
||||||
|
|
||||||
|
inputArray.forEach {
|
||||||
|
Defaults[key] = $0
|
||||||
|
}
|
||||||
|
|
||||||
|
Defaults.reset(key)
|
||||||
|
cancellable.cancel()
|
||||||
|
|
||||||
|
waitForExpectations(timeout: 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
||||||
|
func testRemoveDuplicatesOptionalObserveKeyCombine() {
|
||||||
|
let key = Defaults.Key<Bool?>("observeOptionalKey", default: nil)
|
||||||
|
let expect = expectation(description: "Observation closure being called")
|
||||||
|
|
||||||
|
let inputArray = [true, nil, nil, nil, false, false, false, nil]
|
||||||
|
let expectedArray = [true, nil, false, nil]
|
||||||
|
|
||||||
|
let cancellable = Defaults
|
||||||
|
.publisher(key, options: [])
|
||||||
|
.removeDuplicates()
|
||||||
|
.map(\.newValue)
|
||||||
|
.collect(expectedArray.count)
|
||||||
|
.sink { result in
|
||||||
|
print("Result array: \(result)")
|
||||||
|
result == expectedArray ? expect.fulfill() : XCTFail("Expected Array is not matched")
|
||||||
|
}
|
||||||
|
|
||||||
|
inputArray.forEach {
|
||||||
|
Defaults[key] = $0
|
||||||
|
}
|
||||||
|
|
||||||
|
Defaults.reset(key)
|
||||||
|
cancellable.cancel()
|
||||||
|
|
||||||
|
waitForExpectations(timeout: 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
||||||
|
func testRemoveDuplicatesObserveNSSecureCodingKeyCombine() {
|
||||||
|
let key = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey", default: ExamplePersistentHistory(value: "TestValue"))
|
||||||
|
let expect = expectation(description: "Observation closure being called")
|
||||||
|
|
||||||
|
let inputArray = ["NewTestValue", "NewTestValue", "NewTestValue", "NewTestValue2", "NewTestValue2", "NewTestValue2", "NewTestValue3", "NewTestValue3"]
|
||||||
|
let expectedArray = ["NewTestValue", "NewTestValue2", "NewTestValue3"]
|
||||||
|
|
||||||
|
let cancellable = Defaults
|
||||||
|
.publisher(key, options: [])
|
||||||
|
.removeDuplicates()
|
||||||
|
.map(\.newValue.value)
|
||||||
|
.collect(expectedArray.count)
|
||||||
|
.sink { result in
|
||||||
|
print("Result array: \(result)")
|
||||||
|
result == expectedArray ? expect.fulfill() : XCTFail("Expected Array is not matched")
|
||||||
|
}
|
||||||
|
|
||||||
|
inputArray.forEach {
|
||||||
|
Defaults[key] = ExamplePersistentHistory(value: $0)
|
||||||
|
}
|
||||||
|
|
||||||
|
Defaults.reset(key)
|
||||||
|
cancellable.cancel()
|
||||||
|
|
||||||
|
waitForExpectations(timeout: 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
|
||||||
|
func testRemoveDuplicatesObserveNSSecureCodingOptionalKeyCombine() {
|
||||||
|
let key = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("observeNSSecureCodingOptionalKey")
|
||||||
|
let expect = expectation(description: "Observation closure being called")
|
||||||
|
|
||||||
|
let inputArray = ["NewTestValue", "NewTestValue", "NewTestValue", "NewTestValue2", "NewTestValue2", "NewTestValue2", "NewTestValue3", "NewTestValue3"]
|
||||||
|
let expectedArray = ["NewTestValue", "NewTestValue2", "NewTestValue3", nil]
|
||||||
|
|
||||||
|
let cancellable = Defaults
|
||||||
|
.publisher(key, options: [])
|
||||||
|
.removeDuplicates()
|
||||||
|
.map(\.newValue)
|
||||||
|
.map { $0?.value }
|
||||||
|
.collect(expectedArray.count)
|
||||||
|
.sink { result in
|
||||||
|
print("Result array: \(result)")
|
||||||
|
result == expectedArray ? expect.fulfill() : XCTFail("Expected Array is not matched")
|
||||||
|
}
|
||||||
|
|
||||||
|
inputArray.forEach {
|
||||||
|
Defaults[key] = ExamplePersistentHistory(value: $0)
|
||||||
|
}
|
||||||
|
|
||||||
|
Defaults.reset(key)
|
||||||
|
cancellable.cancel()
|
||||||
|
|
||||||
|
waitForExpectations(timeout: 10)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue