Can swift macro create separate implementation of a declaration?

For my case at least (and perhaps OP's as well from the examples presented?), I was looking to create something like a Mocking library, that you can use for testing instead of having to create mocks manually and/or rely on a code generation tool for this.
Something you can use like:

func testThisWorksFine() {
   @Mocked let repository: MyRepositoryProtocol
   let sut = MyClass(repository: repository)
}

Either like that, or creating a

@Mocked(MyRepositoryProtocol)
class MockedRepository {}

or something that would simplify code around mocking. I realise that going through the effort of supporting every edge case is the path to insanity and to flakiness for future swift releases, so I see that as a no-go for now haha.

Maybe something simpler but still handy could be the way to go, like creating your class (maybe with a conformance macro still for protocol conformance) and a simpler macro to keep track of method call count and things like that, like:

@Mock
class MyMock: CoolProtocol {
   // macro could generate this prop
   var callStack: MyMockCallStack

   @Mock
   func coolAction() {
      // and the macro would generate:
      callStack.coolAction.record()
   }
}

so in a test you could:

func test() {
   let myMock = MyMock()
   // ....
   XCTAssertEqual(myMock.coolAction.callCount, 1)
}
1 Like