Kolos65 / Mockable

A Swift macro driven auto-mocking library.
MIT License
198 stars 14 forks source link

Opt out for MOCKING compile flag #45

Open mihaicris-adoreme opened 2 months ago

mihaicris-adoreme commented 2 months ago

Just a thought..

I had a situation when I needed to have a null pattern implementation for production environment of a mockable protocol which was easily created with Mock..(policy: .relaxed). However by default all mocks are under #if MOCKING compiler flag. Would that be possible to opt out for the MOCKING flag when generating mocks? Such that the mock can be used in release configuration. Something like this:

@Mockable(underFlag: false) // or better naming
protocol ... { }

Currently I configure MOCKING also for release config, but this makes all protocols available in release. I only needed for some protocols. Alternative is to create the implementation for release config manually without using the Mock generated.

Kolos65 commented 1 month ago

Hey @mihaicris-adoreme, sorry for the late response!

Extending the library with more control over the compile flag is a great idea, it already crossed my mind!

That being said, I would not suggest using the generated mocks in production as a default implementation or similar. They are designed for testing and will keep track of every function call, store return values, etc..., that can result in poor performance and unexpected results when used as a "real" implementation. The main use case for the generated mocks was meant to be SwiftUI previews and unit testing. If I needed relaxed mock implementations in prod, I would create them manually.

Regardless, I will happily consider this feature request as it could help in different architectures and make the library usage more seamless.

Im thinking of letting users provide the flag as an optional string literal with a default "MOCKING" value like:

@Mockable(condition: "DEBUG")
protocol Service {}

or if no flag is desired:

@Mockable(condition: .none)
protocol Service {}
mihaicris-adoreme commented 1 month ago

Hi @Kolos65 , thanks for the comments, and considering the flag options. You are right, I'll define custom implementations manually for production.!