Skip to content

Commit

Permalink
now and parse tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
henryw374 committed Jan 15, 2024
1 parent ee407fd commit e9eb155
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 218 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,27 @@ api todo - see todos in tempo.cljc

### tests

## todo

accessors
- generic construct
- assert isa?

e.g. create zdt. access day from zdt. confirm it is the right day

comparators
- generic construct.
- >> by suitable duration
- compare + not=, hash not=


## Done

for each entity:

construct

- from: parse, now (+clock), parts
- assert isa?

accessors
- generic construct
- assert isa?

comparators
- generic construct.
- >> by suitable duration
- compare + not=, hash not=

= , hash, compare
36 changes: 23 additions & 13 deletions dev/com/widdindustries/gen/cljs_protocols.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,22 @@
js/Temporal.TimeZone
(-hash [o] (hash (.-id ^js o))))))))

(defn temporal-fn-gen [temporal-type]
(backtick/template
(defn ~(symbol temporal-type) []
(extend-protocol IEquiv
~(symbol "entities" temporal-type)
(-equiv [o other] (.equals ^js o other)))
(defn temporal-fn-gen [temporal-type overrides]
(let [compar (or (get overrides :compare)
(backtick/template (-compare [x y] (.compare ~(symbol "entities" temporal-type) ^js x y))))]
(backtick/template
(defn ~(symbol temporal-type) []
(extend-protocol IEquiv
~(symbol "entities" temporal-type)
(-equiv [o other] (.equals ^js o other)))

(extend-protocol IHash
~(symbol "entities" temporal-type)
(-hash [o] (hash (str o))))
(extend-protocol IHash
~(symbol "entities" temporal-type)
(-hash [o] (hash (str o))))

(extend-protocol IComparable
~(symbol "entities" temporal-type)
(-compare [x y] (.compare ~(symbol "entities" temporal-type) ^js x y))))))
(extend-protocol IComparable
~(symbol "entities" temporal-type)
~compar)))))

(defn extend-all [entities]
(backtick/template
Expand All @@ -60,6 +62,14 @@
entities))
))

(def overrides
{'monthday {:compare (backtick/template
(-compare [^js x ^js y]
(let [m (compare (.-monthCode x) (.-monthCode y))]
(if (zero? m)
(compare (.-day x) (.-day y))
m))))}})

(defn gen-protocols []
(gen/gen (str "src/" (->
(name 'com.widdindustries.tempo.cljs-protocols)
Expand All @@ -70,5 +80,5 @@
(concat [(ns-decl)]
(non-temporals)
(for [tt graph/temporal-types]
(temporal-fn-gen (str tt)))
(temporal-fn-gen (str tt) (get overrides tt)))
[(extend-all (concat graph/temporal-types ['timezone 'duration]))])))
54 changes: 28 additions & 26 deletions dev/com/widdindustries/gen/gen/accessors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
(conj sub-paths (vec (take x path)))))))


(def ^{:doc "each path is a list from root to leaf"}
(def ^{:doc "each path is a list from root to leaf"}
full-paths
(->>
(loop [[next-path & remaining] all-paths
Expand All @@ -39,12 +39,12 @@
access-paths
(recur remaining
(set/union access-paths (->sub-paths next-path)))))

(remove #(= 1 (count %)))
(m/distinct-by (fn [path] [(-> path first :tempo) (-> path last :tempo)]))
(sort-by (juxt (comp first :tempo) (comp second :tempo)))))

(def kw->class
(def kw->class
{
'date 'LocalDate
'timezone 'ZoneId
Expand All @@ -68,7 +68,7 @@
'yearmonth 'cljc.java-time.year-month
})

(def kw->temporal-class
(def kw->temporal-class
{
'date 'PlainDate
'timezone 'TimeZone
Expand All @@ -85,12 +85,14 @@
(subs s 0 1)) (subs s 1 (count s))))

(defn special-accessor [target]
(cond (:accessor target) (str "." (:accessor target))
(cond
(:xform-fn target) (:xform-fn target)
(:accessor target) (str "." (:accessor target))
:else nil))

(defn cljc-accessor [feature path]
(let [subject (:tempo (first path))
target (last path)
target (last path)
target-name (:tempo target)
fn-name (symbol (str (name subject) "->" (name target-name)))]
(when-not (:ignore-accessor target)
Expand All @@ -109,6 +111,8 @@
target (last path)
target-name (:tempo target)
fn-name (symbol (str (name subject) "->" (name target-name)))]
#_(when (and (= 'zdt subject) (= 'timezone target-name))
(sc.api/spy) (comment (eval `(sc.api/defsc ~(sc.api/last-ep-id)))))
(when-not (:ignore-accessor target)
(backtick/template
(defn ~fn-name [~(with-meta 'foo {:tag (symbol (str "js/Temporal." (str (get kw->temporal-class subject))))})]
Expand All @@ -117,8 +121,8 @@
x
(if-let [target-class (get kw->temporal-class target-name)]
(str ".to" (str target-class))
(str ".-" (csk/->camelCaseString (name target-name)))))) foo)
)))))
(str ".-" (csk/->camelCaseString (name target-name))))))
foo))))))

(comment
(comment
Expand Down Expand Up @@ -147,7 +151,7 @@
;_ (comment (eval `(sc.api/defsc ~(sc.api/last-ep-id))))
fn-name (symbol (str (name subject) "->" (name target-name)))
]
(when-not (:ignore-accessor target)
(when-not (:ignore-accessor target)
(backtick/template
(defn ~fn-name [~(with-meta 'foo {:tag (get kw->class subject)})]
(~(symbol
Expand All @@ -160,15 +164,15 @@
(defn accessor-forms [feature]
; problems
;ignore-accessor

(->> full-paths
(keep (fn [path]
(case feature
:cljay (java-accessor feature path)
:cljc (cljc-accessor feature path)
;todo
:cljs (temporal-accessor feature path))
))))
(case feature
:cljay (java-accessor feature path)
:cljc (cljc-accessor feature path)
;todo
:cljs (temporal-accessor feature path))
))))

(defn parse-fn [feature subject]
(when (get kw->class (:tempo subject))
Expand All @@ -178,7 +182,7 @@
:cljc (str (get kw->cljc-ns (:tempo subject)) "/" (or (-> subject feature :parse) "parse"))
:cljs (str "js/Temporal." (get kw->temporal-class (:tempo subject)) ".from")
)]

(backtick/template
(defn ~(symbol fn-name) [~(with-meta 'foo {:tag String})]
(~(symbol parser) foo)))
Expand All @@ -198,9 +202,9 @@
(when (and (not (:no-now subject)) (get kw->class (:tempo subject)))
(let [fn-name (str (:tempo subject) "-now")
nower (case feature
:cljay (str (get kw->class (:tempo subject)) "/" (or (-> subject feature :parse) "now"))
:cljs (str "clock/"(:tempo subject) )
:cljc (str (get kw->cljc-ns (:tempo subject)) "/" (or (-> subject feature :parse) "now")))]
:cljay (str (get kw->class (:tempo subject)) "/" (or (-> subject feature :parse) "now"))
:cljs (str "clock/" (:tempo subject))
:cljc (str (get kw->cljc-ns (:tempo subject)) "/" (or (-> subject feature :parse) "now")))]
(backtick/template
(defn ~(symbol fn-name)
([] (~(symbol nower)))
Expand All @@ -214,7 +218,7 @@
(keep (fn [thing]
(now-fn feature thing)))))

(defn now-test [ subject]
(defn now-test [subject]
(when (and (not (:no-now subject)) (get kw->class (:tempo subject)))
(let [fn-name (str (:tempo subject) "-now-test")]
(backtick/template
Expand All @@ -227,9 +231,7 @@
now-clock-2 (~(symbol (str "t/" (:tempo subject) "-now")) clock-2)
]
(is (~(symbol (str "t/" (:tempo subject) "?")) now-clock-1))
(is (t/> now-clock-2 now-clock-1))
)
)))))
(is (t/> now-clock-2 now-clock-1))))))))

(defn now-tests []
(->> (apply concat full-paths)
Expand All @@ -239,13 +241,13 @@


(comment

(binding [*print-meta* true]
(pr (java-accessor [:zdt :date])))
(defn zdt->date [^java.time.ZonedDateTime foo] (.toLocalDate foo))
(zdt->date (ZonedDateTime/now))
(->> (apply concat full-paths) set)

)

(defn parse-test [subject]
Expand Down
6 changes: 5 additions & 1 deletion dev/com/widdindustries/gen/gen/tempo.clj
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@

)))

(defn generate-all [_]
(defn gen-after []
(gen-tempo "no-deps" #{:cljay} :cljay)
(gen-tempo "no-deps" #{:cljs} :cljs)
(cljs-protocols/gen-protocols)
(generate-test))

(defn generate-all [_]
(clojure.tools.namespace.repl/refresh :after `gen-after)
)

(comment
(generate-all nil)
;(accessors/accessor-forms :cljs)
Expand Down
23 changes: 13 additions & 10 deletions dev/com/widdindustries/gen/graph.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
'date
'datetime
'time
'month-day
'year-month
'monthday
'yearmonth
])

(def non-temporal-types
Expand All @@ -58,12 +58,13 @@

(def timezone
{:no-now true
:cljay {:parse 'of
:accessor 'getZone}
:cljc {:parse 'of
:accessor 'getZone}
:cljs {:accessor 'timeZoneId}
:tempo 'timezone})
:cljay {:parse 'of
:accessor 'getZone}
:cljc {:parse 'of
:accessor 'getZone}
:cljs {:xform-fn 'methods/has-timezone->timezone
}
:tempo 'timezone})

(def yearmonth
{:needed-to-go-up {'day-of-month {}}
Expand Down Expand Up @@ -120,11 +121,13 @@
month {}}}}}
{:parts
{monthday {:parts {month {}
{:tempo 'day-of-month} {}}}}}
{:tempo 'day-of-month
:cljs {:accessor '-day}} {}}}}}
{:parts
{{:tempo 'year} {}
month {}
{:tempo 'day-of-month} {}}}
{:tempo 'day-of-month
:cljs {:accessor '-day}} {}}}
{:parts
{day-of-week {}}}]
}
Expand Down
4 changes: 2 additions & 2 deletions dev/dev.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@

; start up live-compilation of tests
(test-watch)
; run cljs tests, having opened browser at test page (see print output of above "for tests, open...")
(util/run-tests)

(refresh/clear)
(gen/gen-after)
(gen/generate-all nil)
(run-clj-tests nil)
; start a cljs repl session in the test build. :cljs/quit to exit
Expand Down
6 changes: 3 additions & 3 deletions gen_in/tempo.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
(defn time? [v] #?(:cljay(instance? LocalTime v)
:cljs (instance? entities/time v)))
(defn monthday? [v] #?(:cljay(instance? MonthDay v)
:cljs (instance? entities/month-day v)))
:cljs (instance? entities/monthday v)))
(defn yearmonth? [v] #?(:cljay(instance? YearMonth v)
:cljs (instance? entities/year-month v)))
:cljs (instance? entities/yearmonth v)))
(defn timezone? [v] #?(:cljay(instance? ZoneId v)
:cljs (instance? entities/time-zone v)))
:cljs (instance? entities/timezone v)))
(defn zdt? [v] #?(:cljay(instance? ZonedDateTime v)
:cljs (instance? entities/zdt v)))

Expand Down
Loading

0 comments on commit e9eb155

Please sign in to comment.