File tree Expand file tree Collapse file tree 5 files changed +46
-10
lines changed Expand file tree Collapse file tree 5 files changed +46
-10
lines changed Original file line number Diff line number Diff line change 44
55### New Features
66
7+ * ` zone-id? ` predicate
8+ * ` set-clock! ` - sets the mocked clock value to the supplied instant
9+
10+ ## 0.3.1
11+
12+ ### New Features
13+
714* ` clock? ` predicate
815* ` mock-clock ` - returns a mocked instance of ` java.time.Clock ` .
916
Original file line number Diff line number Diff line change @@ -443,6 +443,13 @@ can be very handy in testing:
443443(with-clock clock
444444 (j/instant ))
445445=> #object[java.time.Instant " 1970-01-01T05:20:00Z" ]
446+
447+ (set-clock! clock 0 )
448+ => nil
449+
450+ (with-clock clock
451+ (j/instant ))
452+ => #object[java.time.Instant " 1970-01-01T00:00:00Z" ]
446453```
447454
448455Clock overrides works for all of the date-time types.
Original file line number Diff line number Diff line change 4444 with-zone-same-instant with-offset with-offset-same-instant]
4545
4646 [java-time.mock
47- mock-clock advance-clock!]
47+ mock-clock advance-clock! set-clock! ]
4848
4949 [java-time.convert
5050 as-map convert-amount to-java-date to-sql-date to-sql-timestamp
Original file line number Diff line number Diff line change 11(ns java-time.mock
22 (:require [java-time
3+ [amount :as amount]
34 [core :as core]
45 [temporal :as temporal]
56 [zone :as zone]])
5556 This mutates the mock clock."
5657 [^IMockClock clock amount]
5758 (.advanceClock clock amount))
59+
60+ (defn set-clock!
61+ " Sets the `clock` to the given `time`.
62+
63+ This mutates the mock clock."
64+ [^Clock clock time]
65+ (let [current (.instant clock)]
66+ (->> (core/time-between :millis current (temporal/instant time))
67+ (amount/millis )
68+ (advance-clock! clock))))
Original file line number Diff line number Diff line change 467467
468468 (testing " by negative amount"
469469 (j/advance-clock! clock (j/millis -1 ))
470- (is (= 0 (j/value clock))))
470+ (is (= 0 (j/value clock))))))
471471
472- (testing " clone with a different zone"
473- (let [cloned-clock (j/with-zone clock " GMT" )]
474- (is (not (identical? clock cloned-clock)))
475- (is (= (j/zone-id " GMT" ) (j/zone-id cloned-clock)))
476- (is (= (j/value cloned-clock) (j/value clock)))
472+ (testing " clone with a different zone"
473+ (let [clock (utc-clock 0 )
474+ cloned-clock (j/with-zone clock " GMT" )]
475+ (is (not (identical? clock cloned-clock)))
476+ (is (= (j/zone-id " GMT" ) (j/zone-id cloned-clock)))
477+ (is (= (j/value cloned-clock) (j/value clock)))
477478
478- (j/advance-clock! cloned-clock (j/seconds 1 ))
479- (is (= 1000 (j/value cloned-clock)))
480- (is (not= (j/value cloned-clock) (j/value clock)))))))))
479+ (j/advance-clock! cloned-clock (j/seconds 1 ))
480+ (is (= 1000 (j/value cloned-clock)))
481+ (is (not= (j/value cloned-clock) (j/value clock)))))
482+
483+ (testing " set"
484+ (let [clock (utc-clock 0 )]
485+ (testing " into future"
486+ (j/set-clock! clock 100 )
487+ (is (= 100 (j/value clock))))
488+
489+ (testing " into past"
490+ (j/set-clock! clock 0 )
491+ (is (= 0 (j/value clock))))))))
481492
482493(deftest properties
483494 (testing " units"
You can’t perform that action at this time.
0 commit comments