Last active
February 16, 2022 11:59
-
-
Save Rovanion/0a609839a1944a201fbd4e4bbc330992 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns tove.specs.generators | |
"Based on https://github.com/dm3/clojure.joda-time/blob/master/test/joda_time/generators.clj" | |
(:require [clojure.test.check.generators :as gen] | |
[clojure.string :as string] | |
#?(:clj [java-time :as jt] | |
:cljs [cljs-time.core :as jst])) | |
#?(:clj (:import [java.time LocalDate LocalDateTime ZonedDateTime] | |
[java.time.chrono IsoChronology]))) | |
;;; Strings | |
(defn -string-gen [{:keys [min max]}] | |
(cond | |
(and min (= min max)) (gen/fmap string/join (gen/vector gen/char min)) | |
(and min max) (gen/fmap string/join (gen/vector gen/char min max)) | |
min (gen/fmap string/join (gen/vector gen/char min (* 2 min))) | |
max (gen/fmap string/join (gen/vector gen/char 0 max)) | |
:else gen/string)) | |
;;; Time | |
(def default-chronology #?(:clj IsoChronology/INSTANCE | |
:cljs nil)) | |
(def year-of-century | |
(gen/choose 1 100)) | |
(def year-of-era | |
(gen/choose 1 10000)) | |
(def month-of-year | |
(gen/choose 1 12)) | |
(def day-of-week | |
(gen/choose 1 7)) | |
(def day-of-month | |
(gen/choose 1 28)) | |
(def hour-of-day | |
(gen/choose 0 23)) | |
(def minute-of-hour | |
(gen/choose 0 59)) | |
(def second-of-minute | |
(gen/choose 0 59)) | |
(def millis-of-second | |
(gen/choose 0 999)) | |
;;; Date-Times | |
;; (defn- zoned-date-time-tuple [& {:keys [chrono] :or {chrono default-chronology}}] | |
;; (gen/tuple year-of-era month-of-year day-of-month | |
;; hour-of-day minute-of-hour second-of-minute | |
;; millis-of-second (gen/return chrono))) | |
;; (defn zoned-date-time [& {:keys [chrono] :or {chrono default-chronology}}] | |
;; (gen/fmap (partial apply #(ZonedDateTime/of %1 %2 %3 %4 %5 %6 %7 %8)) | |
;; (zoned-date-time-tuple :chrono chrono))) | |
(defn- date-time-tuple [] | |
(gen/tuple year-of-era month-of-year day-of-month | |
hour-of-day minute-of-hour second-of-minute | |
millis-of-second)) | |
(def local-date-time | |
(gen/fmap (partial apply #?(:clj #(LocalDateTime/of %1 %2 %3 %4 %5 %6 %7) | |
:cljs #(jst/local-date-time %1 %2 %3 %4 %5 %6 %7))) | |
(date-time-tuple))) | |
(defn- date-tuple [& _] | |
(gen/tuple year-of-era month-of-year day-of-month)) | |
(def local-date | |
(gen/fmap (partial apply #?(:clj #(LocalDate/of %1 %2 %3) | |
:cljs #(jst/local-date %1 %2 %3))) | |
(date-tuple))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment