https://kotlinlang.org logo
Title
d

dave08

05/10/2021, 3:10 PM
Hi! Wouldn't it be nice to have the Clock interface declared as a
fun interface
? That would allow very simple overriding of
now()
in unit tests... unless there are plans to add other functions to it?
i

ilya.gorbunov

05/11/2021, 3:41 PM
Being a functional interface,
Clock
would allow passing lambdas where
Clock
is expected. This is certainly not how we would like the API with clocks to look like. A helper constructor-like method could be introduced, though, e.g.
Clock(() -> Instant)
d

dave08

05/11/2021, 3:43 PM
Yeah, I guess that's a good point! It's a bit easy to be trigger-happy with `fun interface`s 😊! But the helper method would certainly be a nice addition... I find myself making it on every project's test utils...
i

ilya.gorbunov

05/11/2021, 4:12 PM
What do you usually pass as a Clock in tests?
d

dave08

05/12/2021, 9:09 AM
Usually an
Instant
var
... since I need to modify it for certain tests in the same class. I use a base
Instant
and either subtract from it or add to it to modify the
now
var
I passed in to clock.
p

Paul Woitaschek

05/15/2021, 6:21 AM
We have a ManualClock for tests
It just exposes the instant in the constructor as a var and has functions like advanceBy(Duration)