-
Notifications
You must be signed in to change notification settings - Fork 0
Migrate to swift6 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0b80e30
1f78955
e030f97
e8726b1
edb8dc6
dcaad6d
444948c
d4a0c33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,31 +85,28 @@ final class UserDefaultTests: XCTestCase { | |
| XCTAssertNil(userDefaults.object(forKey: "BoolKey")) | ||
| } | ||
|
|
||
| @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) | ||
| func testObserver() { | ||
| let wrapper = UserDefault<String>(.init("StringKey"), store: userDefaults, defaultValue: "") | ||
|
|
||
| var changes: [UserDefaults.Change<String>] = [] | ||
| let observer = wrapper.addObserver { changes.append($0) } | ||
| addTeardownBlock(observer.invalidate) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it seems unnecessary to use addTeardownBlock, I removed it. |
||
|
|
||
| wrapper.wrappedValue = "One" | ||
| wrapper.reset() | ||
| wrapper.wrappedValue = "Two" | ||
| userDefaults.x.set("Three", forKey: .init("StringKey")) | ||
|
|
||
| XCTAssertEqual(changes, [.initial(""), .update("One"), .update(""), .update("Two"), .update("Three")]) | ||
| observer.invalidate() | ||
| } | ||
|
|
||
| @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) | ||
| func testCodableWithDefault() { | ||
| let key = UserDefaults.Key("CodableKey") | ||
| let wrapper = UserDefault<Subject>(key, strategy: .json, store: userDefaults, defaultValue: Subject(value: "default")) | ||
|
|
||
| // Observe changes | ||
| var changes: [Subject] = [] | ||
| let token = wrapper.addObserver(handler: { changes.append($0.value) }) | ||
| addTeardownBlock(token.invalidate) | ||
|
|
||
| // Uses default | ||
| XCTAssertNil(userDefaults.object(forKey: key.rawValue)) | ||
|
|
@@ -135,17 +132,16 @@ final class UserDefaultTests: XCTestCase { | |
| "default", | ||
| "default" | ||
| ]) | ||
| token.invalidate() | ||
| } | ||
|
|
||
| @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) | ||
| func testCodable() { | ||
| let key = UserDefaults.Key("CodableKey") | ||
| let wrapper = UserDefault<Subject?>(key, strategy: .json, store: userDefaults) | ||
|
|
||
| // Observe changes | ||
| var changes: [Subject?] = [] | ||
| let token = wrapper.addObserver(handler: { changes.append($0.value) }) | ||
| addTeardownBlock(token.invalidate) | ||
|
|
||
| // nil when unset | ||
| XCTAssertNil(userDefaults.object(forKey: key.rawValue)) | ||
|
|
@@ -178,17 +174,16 @@ final class UserDefaultTests: XCTestCase { | |
| "value", | ||
| nil | ||
| ]) | ||
| token.invalidate() | ||
| } | ||
|
|
||
| @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) | ||
| func testRawRepresentableWithDefault() { | ||
| let key = UserDefaults.Key("RawRepresentableKey") | ||
| let wrapper = UserDefault<RawSubject>(key, store: userDefaults, defaultValue: .foo) | ||
|
|
||
| // Observe changes | ||
| var changes: [RawSubject] = [] | ||
| let token = wrapper.addObserver(handler: { changes.append($0.value) }) | ||
| addTeardownBlock(token.invalidate) | ||
|
|
||
| // Uses default | ||
| XCTAssertNil(userDefaults.object(forKey: key.rawValue)) | ||
|
|
@@ -214,17 +209,16 @@ final class UserDefaultTests: XCTestCase { | |
| .foo, | ||
| .foo | ||
| ]) | ||
| token.invalidate() | ||
| } | ||
|
|
||
| @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) | ||
| func testRawRepresentable() { | ||
| let key = UserDefaults.Key("RawRepresentableKey") | ||
| let wrapper = UserDefault<RawSubject?>(key, store: userDefaults) | ||
|
|
||
| // Observe changes | ||
| var changes: [RawSubject?] = [] | ||
| let token = wrapper.addObserver(handler: { changes.append($0.value) }) | ||
| addTeardownBlock(token.invalidate) | ||
|
|
||
| // Uses default | ||
| XCTAssertNil(userDefaults.object(forKey: key.rawValue)) | ||
|
|
@@ -256,5 +250,6 @@ final class UserDefaultTests: XCTestCase { | |
| nil, | ||
| .baz | ||
| ]) | ||
| token.invalidate() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,6 +54,27 @@ final class UserDefaultsObservationTests: XCTestCase { | |
| XCTAssertEqual(changes.map(\.value) as NSArray, [nil, "Test", nil, "Default", 1] as NSArray) | ||
| XCTAssertEqual(changes.map(\.label), [.initial, .update, .update, .update, .update]) | ||
| } | ||
|
|
||
| func testInvalidateOnDeinit() { | ||
| // Given an observer is registered | ||
| var changes: [UserDefaults.Change<Any?>] = [] | ||
|
|
||
| var observer: UserDefaults.Observation? = userDefaults.observeObject(forKey: "TestKey") { change in | ||
| changes.append(change) | ||
| } | ||
| _ = observer | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q. これは必要なコードですか
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Linterエラー対策で追記しました |
||
|
|
||
| userDefaults.set("Test", forKey: "TestKey") | ||
|
|
||
| // When the observer is deallocated | ||
| observer = nil | ||
|
|
||
| // Then no further changes should be recorded | ||
| userDefaults.set("NewTest", forKey: "TestKey") | ||
|
|
||
| XCTAssertEqual(changes.map(\.value) as NSArray, [nil, "Test"] as NSArray) | ||
| XCTAssertEqual(changes.map(\.label), [.initial, .update]) | ||
| } | ||
| } | ||
|
|
||
| private extension UserDefaults.Change { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swift-tools-version:6.0 is not supported in Xcode 15.4, so we are ending support for Xcode 15.4 in CI.