Add protocol-level @available annotation inheritance to generated mocks#315
Conversation
|
Any updates on this? When this draft PR becomes ready? |
|
Sorry for the long silence. I got sidetracked and lost track of this. I'll add tests and mark this PR as ready soon. Thanks for your patience. |
sidepelican
left a comment
There was a problem hiding this comment.
Thanks for the PR. Overall it looks good, but I noticed a few minor details that I'd like you to address before we merge.
| /// @mockable | ||
| @available(iOS 18.0, *) | ||
| public protocol Foo: Sendable { | ||
| func bar() -> String | ||
| } |
There was a problem hiding this comment.
I want to enforce a compilation error when @available isn't applied to a mock.
| /// @mockable | |
| @available(iOS 18.0, *) | |
| public protocol Foo: Sendable { | |
| func bar() -> String | |
| } | |
| @available(macOS 99.0, *) | |
| struct Bar {} | |
| /// @mockable | |
| @available(macOS 99.0, *) | |
| protocol Foo: Sendable { | |
| func bar() -> Bar | |
| } |
There was a problem hiding this comment.
Fixed in 2fa6c3d.
I updated the fixture to use an availability-gated return type (Bar) so the generated mock must inherit @available to compile.
|
|
||
| func model() -> Model { | ||
| let metadata = entity.metadata | ||
| // Combine protocol-level attributes with member-level attributes |
There was a problem hiding this comment.
| // Combine protocol-level attributes with member-level attributes |
The meaning feels a bit off and it’s redundant, so let's remove it.
There was a problem hiding this comment.
Fixed in 3e51751.
Removed the redundant comment.
| } | ||
|
|
||
| @MainActor | ||
| /// @mockable |
There was a problem hiding this comment.
It's strange for @mockable to come along too.
How about just inheriting available?
You can change EntityNode.attributesDescription requirement to var attributes: [String] { get }.
There was a problem hiding this comment.
Fixed in 7f3a38b.
I changed declaration attribute handling to keep syntax attributes as separate entries, so @mockable trivia no longer gets carried along with @available.
| return "" | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
I need a test case for protocol inheritance scenarios:
@available(macOS 100.0, *)
struct Bar {}
@available(macOS 90.0, *)
protocol Foo {
}
/// @mockable
@available(macOS 100.0, *)
protocol Foo2: Foo {
var bar: Bar { get set }
}|
Thank you for the review. |
sidepelican
left a comment
There was a problem hiding this comment.
Sorry for the late review.
It looks good. Thank you for your contribution!
Overview
Fixes #314
This draft PR addresses an issue where
@availableannotations on protocols are not inherited by the generated mock classes. This can cause compilation errors when the protocol uses types or members that are only available in specific OS versions.Problem
Currently, when generating mocks for protocols with
@availableannotations, Mockolo does not apply these availability constraints to the generated mock. As a result, using the mock can trigger compile-time errors when availability-constrained types or members are involved.Example
Incorrect mock (missing
@available):Correct mock:
Solution
@availableattributes and apply them to generated mocks.Benefits
Open to feedback!