Hi! Wouldn't it be nice to have the Clock interfac...
# kotlinx-datetime
d
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
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
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
What do you usually pass as a Clock in tests?
d
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
We have a ManualClock for tests
It just exposes the instant in the constructor as a var and has functions like advanceBy(Duration)