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

Add support for providing generic Swift types in a ValueProvider #122

Merged

Conversation

andrewchang-bird
Copy link
Contributor

@andrewchang-bird andrewchang-bird commented May 9, 2020

(Stacked on #121)

Add support for providing generic Swift types in a ValueProvider

Due to limitations around Swift generics, this method relies on using module-scoped extensions on the generic type to provide values.

extension Array: Providable {
  public static func createInstance() -> Self? {
    return Array()
  }
}

// An empty array is registered for all specialized `Array` types
valueProvider.registerType(Array<Any>.self)

Also changes ValueProvider to use value semantics which improves safety around preset value providers.

This was causing all invocations to methods with generic return types to
be treated as returning the same type.

```swift
protocol MyProtocol {
  func genericMethod<T>() -> T
}

let myMock = mock(MyProtocol.self)
given(myMock.genericMethod()) ~> "Foo"
given(myMock.genericMethod()) ~> true

let foo: String = (myMock as MyProtocol).genericMethod()
let bar: Bool = (myMock as MyProtocol).genericMethod()

// Invocation counts for both were incorrectly 2 instead of 1
verify(myMock.genericMethod()).returning(String.self).wasCalled()
verify(myMock.genericMethod()).returning(Bool.self).wasCalled()
```

Fix is to also store the actual type returned at runtime in order to
differentiate invocations to the same generic method.
@andrewchang-bird andrewchang-bird added this to the Release 0.13 milestone May 9, 2020
@andrewchang-bird andrewchang-bird force-pushed the generic-value-provider branch 2 times, most recently from ae4eca4 to 0480ca3 Compare May 9, 2020 21:17
Due to limitations around Swift generics, this method relies on using
module-scoped extensions on the generic type to provide values.

```swift
extension Array: Providable {
  public static func createInstance() -> Self? {
    return Array()
  }
}

// An empty array is registered for all specialized `Array` types
valueProvider.registerType(Array<Any>.self)
```

Also changes `ValueProvider` to use value semantics which improves
safety around preset value providers.
@andrewchang-bird andrewchang-bird merged commit e131a0e into typealiased:master May 16, 2020
@andrewchang-bird andrewchang-bird deleted the generic-value-provider branch May 16, 2020 04:38
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.

2 participants