Skip to content

Commit 2279864

Browse files
author
Vadim Platonov
committed
add set-clock!
1 parent 5433356 commit 2279864

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
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

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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

448455
Clock overrides works for all of the date-time types.

src/java_time.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
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

src/java_time/mock.clj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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]])
@@ -55,3 +56,13 @@
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))))

test/java_time_test.clj

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,17 +467,28 @@
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"

0 commit comments

Comments
 (0)