petergtz / pegomock

Pegomock is a powerful, yet simple mocking framework for the Go programming language
Apache License 2.0
253 stars 28 forks source link

Type check for return values #46

Open crisbodnar opened 6 years ago

crisbodnar commented 6 years ago

It would be useful to have a typecheck on the return value at compile time. Right now if I have func smth(a int) int { //some code here } I can safely write this without generating any compile time error: When(struct.smth(1)).ThenReturn("Not an int", "One more invalid return value")

petergtz commented 6 years ago

@crisbodnar Agree. Unfortunately, I haven't found a way to do this yet. Any ideas how to change the DSL to make this possible?

The problem is that When is not generated and hence cannot return specific return values.

In case we cannot make it a compile error, would you think that an improved runtime error message would help? What should it say?

crisbodnar commented 6 years ago

If you want to keep it in the exact style of Mockito I don't see any nice solution. The best thing I can think of right now is mock.When.smth(1).ThenReturn(1234) and in this way we avoid the use of global methods like When which complicate any way of doing type checks. It's going to be only generated code. I agree the When style is nice but we don't have generics :( and this is a significant improvement for the library. You can keep both versions in order not to break existent stuff.

What do you think @petergtz ?

petergtz commented 6 years ago

@crisbodnar Yes, why not. We can introduce this as an alternative syntax and see how it works out in practice and potentially deprecate one of the syntaxes once it turns out one is superior to the other.

I don't know when when I will have time to implement this. Are you interested in contributing this? I'd be more than happy to merge this as a pull request.

crisbodnar commented 6 years ago

Awesome. I wrote a design doc inside Improbable (https://improbable.io/) to switch to this mock library because it has type checks and it's much better than testify mocks in general (what we use now). Atm this is the only downside of this library. If we decide to do this switch (in the next few days) I am more than happy to implement this change.

petergtz commented 6 years ago

@crisbodnar Cool! Haven't heard of Improbable before, but will definitely have a closer look at it tonight. Would love to see guys adopting pegomock.

petergtz commented 6 years ago

@crisbodnar Any update on this? Just want to see if I can be of any help or so.

crisbodnar commented 6 years ago

Hi @petergtz. Sorry for the (very) late reply. Unfortunately, I am not working there anymore so not sure what the status is.

petergtz commented 6 years ago

@crisbodnar No worries. I will keep this issue open (in the Icebox), in case someone else would like to work on it.