Skip to content

Add subscript setter support with handler and call count#337

Merged
sidepelican merged 2 commits intouber:masterfrom
farkasseb:subscript-setter-support
Mar 3, 2026
Merged

Add subscript setter support with handler and call count#337
sidepelican merged 2 commits intouber:masterfrom
farkasseb:subscript-setter-support

Conversation

@farkasseb
Copy link
Copy Markdown
Contributor

Summary

  • Generate functional subscript setters with SetCallCount and SetHandler instead of the previous empty set { } stub
  • Distinguish { get } and { get set } subscripts correctly; get-only subscripts now omit the setter entirely
  • Support both regular and concurrency-safe mocks (Sendable and actor mocks) using the same MockoloMutex / MockoloHandlerState pattern as getter handling

Design decisions

No built-in value store for setters

The generated setter handler receives all subscript parameters plus newValue, but it does not persist values automatically.

A generic built-in store is hard to make sound:

  • not all subscript key or value types are Hashable
  • generic subscript signatures can be complex
  • Any-erased storage would lose type safety

Instead, storage can be added explicitly in tests when needed:

var storage: [Int: String] = [:]
mock.subscriptSetHandler = { key, newValue in storage[key] = newValue }
mock.subscriptHandler = { key in storage[key] ?? "" }

This keeps the generated code minimal and leaves storage semantics under the test author's control.

Current limitation

Protocol refinement is not reconciled across inheritance. If a parent protocol declares a { get set } subscript and a child narrows it to { get }, the generated mock follows the accessor block on the protocol being mocked rather than merging inherited accessor requirements.

Behavioral changes

Previously, all subscripts, including { get }-only subscripts, were generated with an empty set { } block. That made get-only mocks writable and diverged from the protocol contract.

Now:

  • { get } subscripts generate only a getter
  • { get set } subscripts generate a functional setter with SetCallCount and SetHandler

The main compatibility impact is that code which wrote to a get-only mock subscript will now fail to compile, which is the correct behavior.

…cking

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@sidepelican sidepelican left a comment

Choose a reason for hiding this comment

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

Nice catch. Thank you!

Comment thread Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift Outdated
@sidepelican sidepelican merged commit 31c307c into uber:master Mar 3, 2026
8 checks passed
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