Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@senia-psm
Copy link
Member

Varargs support in mockable annotation macro together with tests for mockable annotation macro.

Unfortunately due to bugs in Context.typecheck it's impossible to use typeCheck tests for mockable annotation. It might be possible to implement such tests with calls to scalac, though it looks quite difficult.

There is also a fix for impure methods support in mockable macro (it was broken).

@adamgfraser adamgfraser requested a review from ioleo April 5, 2020 15:04
@senia-psm senia-psm force-pushed the feature/Varargs_support_in_mockable branch 5 times, most recently from d28f267 to 3ea7a48 Compare April 5, 2020 17:18
Copy link
Member

@ioleo ioleo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! 🙏 If you could just remove the impure val static example and elaborate on why you introduced the class (just curious) and we're good to go!

type SimpleImpureDefsModule = Has[SimpleImpureDefsModule.Service]
object SimpleImpureDefsModule {
trait Service {
val static: String
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support impure vals in ZIO Mock. In a user finds himself needing an impure val in his Service, he should instead define it as def static: String and then in the actual live implementation can implement that with a val.

The reason is that the ZIO Mocks compose layer must satisfy the contract of the service (returning an impure value), but if we do that, then the static proxy will always be called (when the layer is created). One could overcome that by overriding it with lazy val in the Mock, but that will only work in 2.x (dotty will raise a compile time error) and its not considered a good practice.

On the other hand, implementing a def in terms of lazy val (in Mock) and val (in Live) is perfectly fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add another issue to document this and also throw a compile time error when @mockable is used in such scenario.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done #3298

_root_.zio.ZLayer.fromServiceM { proxy =>
withRuntime.map { rts =>
new $service {
class $serviceClassName extends $service {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Member Author

@senia-psm senia-psm Apr 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.replaceAll("\\{[\\n\\s]*\\};?", "") below breaks compilation for empty service: new Service()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see 👍

@senia-psm senia-psm force-pushed the feature/Varargs_support_in_mockable branch from 3ea7a48 to 2b2b388 Compare April 5, 2020 20:28
@senia-psm
Copy link
Member Author

@ioleo done

@adamgfraser adamgfraser merged commit 165df59 into zio:master Apr 5, 2020
@senia-psm senia-psm deleted the feature/Varargs_support_in_mockable branch April 5, 2020 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants