-
Notifications
You must be signed in to change notification settings - Fork 4
[Feature] Support @Mocked on protocols with subscripts #130
Copy link
Copy link
Open
Labels
enhancementNew feature or requestNew feature or request
Description
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 setterAdded complexity with multiple subscripts with different symbols.
Alternatives Considered
Use OSAllocatedUnfairLock inside Swift 6 packages.
Additional Context
No response
Code of Conduct
- I agree to follow this project's Code of Conduct
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request