diff --git a/Sources/Defaults/Observation.swift b/Sources/Defaults/Observation.swift index 9a5e470..ac9e1a2 100644 --- a/Sources/Defaults/Observation.swift +++ b/Sources/Defaults/Observation.swift @@ -446,3 +446,11 @@ extension Defaults.ObservationOptions { 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 { } diff --git a/Tests/DefaultsTests/DefaultsTests.swift b/Tests/DefaultsTests/DefaultsTests.swift index ca5009e..5a04656 100644 --- a/Tests/DefaultsTests/DefaultsTests.swift +++ b/Tests/DefaultsTests/DefaultsTests.swift @@ -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("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("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("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("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) + } }