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 subscripts #89

Merged

Conversation

andrewchang-bird
Copy link
Contributor

@andrewchang-bird andrewchang-bird commented Apr 21, 2020

Closes #31

Similar to properties, subscripts require synthetic construction of getters and setters. Although subscripts have get and set handlers, they can define (multiple) parameters and are parsed as methods, making it a slight variant on the typical method handling.

protocol SubscriptedProtocol {
  subscript(row: Int, col: Int) -> String { get set }
}

The above roughly produces the following mock code:

class SubscriptedProtocolMock {
  // Mocked implementation
  override subscript(row: Int, col: Int) -> String {
    get {
      // ...
    }
    set {
      // ...
    }
  }

  // Matchable implementation
  func getSubscript(row: Int, col: Int) -> String {
    // ...
  }

  func setSubscript(row: Int, col: Int, newValue: String) {
    // ...
  }
}

Which can then be tested with similar syntax to properties. The parameter newValue is named to differentiate between indices and the value being set.

// Stubbing
given(myMock.getSubscript(1, 2)) ~> "foobar"

// Verification
verify(myMock.setSubscript(1, 2, newValue: any())).wasCalled()

Copy link
Contributor

@ryanmeisters ryanmeisters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super cool

Similar to properties, subscripts require synthetic construction of
getters and setters. Although subscripts have `get` and `set` handlers,
they can define (multiple) parameters and are parsed as methods, making
it a slight variant on the typical method handling.

```swift
protocol SubscriptedProtocol {
  subscript(row: Int, col: Int) -> String
}
```

The above roughly produces the following mock code:

```swift
class SubscriptedProtocolMock {
  // Mocked implementation
  override subscript(row: Int, col: Int) -> String {
    get {
      // ...
    }
    set {
      // ...
    }
  }

  // Matchable implementation
  func getSubscript(row: Int, col: Int) -> String {
    // ...
  }

  func setSubscript(row: Int, col: Int, newValue: String) {
    // ...
  }
}
```

Which can then be tested with similar syntax to properties. The
parameter `newValue` is named to differentiate between indices and the
value being set.

```swift
// Stubbing
given(myMock.getSubscript(1, 2)) ~> "foobar"

// Verification
verify(myMock.setSubscript(1, 2, newValue: any())).wasCalled()
```
@andrewchang-bird andrewchang-bird merged commit 0cb39a0 into typealiased:master Apr 25, 2020
@andrewchang-bird andrewchang-bird deleted the add-subscript-support branch April 25, 2020 22:40
@andrewchang-bird andrewchang-bird added this to the Release 0.12 milestone Apr 28, 2020
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.

Missing subscript support
2 participants