Use Case
I'm trying to create mocks for protocols that use subscripts for data access patterns (like caches, stores, dictionaries). Currently, @Mocked doesn't support protocols with subscript requirements.
Feature Proposal
I would like to see @Mocked support protocols with subscripts. For example:
@Mocked
protocol DataStore {
subscript(key: String) -> String? { get set }
}
Then the interface could look something like this
// In your test
let mock = DataStoreMock()
// Setting up the getter to return a value
mock._subscriptGet.implementation = .returns("cached value")
// Or with logic
mock._subscriptGet.implementation = .invokes { key in
if key == "user_id" {
return "12345"
}
return nil
}
// Setting up the setter
mock._subscriptSet.implementation = .invokes { key, value in
print("Set \(key) to \(value)")
}
// Using it
let value = mock["user_id"] // Returns "12345"
mock["name"] = "John" // Invokes the setter
Added complexity with multiple subscripts with different symbols.
Alternatives Considered
Use OSAllocatedUnfairLock inside Swift 6 packages.
Additional Context
No response
Code of Conduct
Use Case
I'm trying to create mocks for protocols that use subscripts for data access patterns (like caches, stores, dictionaries). Currently,
@Mockeddoesn't support protocols with subscript requirements.Feature Proposal
I would like to see
@Mockedsupport protocols with subscripts. For example:Then the interface could look something like this
Added complexity with multiple subscripts with different symbols.
Alternatives Considered
Use
OSAllocatedUnfairLockinside Swift 6 packages.Additional Context
No response
Code of Conduct