Skip to content
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

[4/x] Fix nested optional property and Obj-C types #274

Merged

Conversation

andrewchang-bird
Copy link
Contributor

@andrewchang-bird andrewchang-bird commented Jan 19, 2022

Stack

πŸ“š #275 [5/x] Clean up Synchronized wrapper
πŸ“š #274 ← [4/x] Fix nested optional property and Obj-C types
πŸ“š #273 [3/x] Improve compile-time checks for Swift mocks
πŸ“š #272 [2/x] Remove Obj-C based nil check
πŸ“š #271 [1/x] Add Algolia DocSearch to the documentation

Overview

Returning nil from a nested optional property or Objective-C member incorrectly applies additional optional wrapping due to coercion to Any?.

protocol Foo {
  var nested: Int?? { get }
}
given(myMock.nested).willReturn(nil)
print(myMock.nested)  // Prints "Optional(nil)" instead of "nil"
XCTAssertNil(myMock.nested)  // Fails

This PR replaces the Any? coercion with Any when creating a new dynamic stub.

Test Plan

  • Added additional optionals tests for both the trivial nil case and returning a wrapped nil
  • CI tests pass

@andrewchang-bird andrewchang-bird force-pushed the dev/andrewchang-bird/add-static-typechecking branch from 57eeb30 to f308e36 Compare January 19, 2022 19:44
Base automatically changed from dev/andrewchang-bird/add-static-typechecking to master January 19, 2022 20:39
@andrewchang-bird andrewchang-bird force-pushed the dev/andrewchang-bird/fix-nullability-handling branch from 6ac5ed9 to 6f51908 Compare January 19, 2022 20:42
@andrewchang-bird andrewchang-bird merged commit f3aab70 into master Jan 19, 2022
@andrewchang-bird andrewchang-bird deleted the dev/andrewchang-bird/fix-nullability-handling branch January 19, 2022 21:27
andrewchang-bird added a commit that referenced this pull request Jan 19, 2022
Returning `nil` from a nested optional property or Objective-C member
incorrectly applies additional optional wrapping due to coercion to
`Any?`.

```swift
protocol Foo {
  var nested: Int?? { get }
}
given(myMock.nested).willReturn(nil)
print(myMock.nested)  // Prints "Optional(nil)" instead of "nil"
XCTAssertNil(myMock.nested)  // Fails
```

This PR replaces the `Any?` coercion with `Any` when creating a new
dynamic stub.

- Added additional optionals tests for both the trivial `nil` case and
returning a wrapped `nil`
- CI tests pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants