Skip to content

[Feature] Support @Mocked on protocols with subscripts #130

@gage-halverson-fetch

Description

@gage-halverson-fetch

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

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions