Skip to content

aim2120/CuckooProtocolInheritanceExample

Repository files navigation

CuckooProtocolInheritanceExample

This repo demonstrates an issue when generating mocks with Cuckoo for protocols with conformance to other protocols. At time of writing, Cuckoo claims to support "inheritance (grandparent methods)", but it seems that this support currently does not extend to protocol conformance.

Environment

  • Cuckoo version: 2.0.18
  • Xcode version: 16.1
  • Swift version: 6.0.2
  • MacOS version: 15.5

Problem Summary

When building this project, you will encounter errors like:

Type 'MockChildProtocolWithNoArgs' does not conform to protocol 'ParentProtocolWithNoArgs'
Type 'ChildProtocolWithNoArgsStub' does not conform to protocol 'ParentProtocolWithNoArgs'
Type 'MockChildProtocolWithArg' does not conform to protocol 'ParentProtocolWithArg'
Type 'ChildProtocolWithArgStub' does not conform to protocol 'ParentProtocolWithArg'
Type 'MockChildProtocolWithAssociatedType' does not conform to protocol 'ParentProtocolWithAssociatedType'
Type 'ChildProtocolWithAssociatedTypeStub' does not conform to protocol 'ParentProtocolWithAssociatedType'

These errors occur in Tests/CuckooProtocolInheritanceExampleTests/GeneratedMocks.swift.

Cause

The mocks generated by Cuckoo for protocols that inherit from other protocols do not automatically implement the requirements of the parent protocols. For example, ChildProtocolWithNoArgs inherits from ParentProtocolWithNoArgs, but the generated mock only implements childFunction() and omits parentFunction(). This leads to a failure to conform to the parent protocol.

I've included a few types of parent function signature, just to be sure that it's not limited to an edge case with a particular parent protocol function signature type.

Repro Steps

  1. Clone this repository.
  2. Run swift test or test the project in Xcode.
  3. Observe the build errors in GeneratedMocks.swift as described above.

Project Structure

  • Protocols are defined in Sources/CuckooProtocolInheritanceExample/ParentProtocols.swift and ChildProtocols.swift.
  • Cuckoo is configured via Cuckoofile.toml.
  • The mocks generation script is a pre-action on the CuckooProtocolInheritanceExample target build action.

Other Notes

  • To work around this in my own code, I'm currently resorting to re-defining parent protocol conformances in child protocols, but this leads to undesirable code duplication and is not a pattern I would like to continue.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages