Skip to content

Commit

Permalink
use me.raystubbs/check and happy-dom for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
raystubbs committed Nov 3, 2024
1 parent 5b8fc59 commit 81631b0
Show file tree
Hide file tree
Showing 18 changed files with 566 additions and 2,797 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ jobs:
- name: Run browser tests
run: yarn test
- name: Run Clojure tests
run: clojure -X:test-clj
run: clojure -A:dev -X test/run
2 changes: 1 addition & 1 deletion bench/bench.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
(prof/serve-ui 8888)))

(defn bench-all!
[]
[& _]
(c/quick-bench (zh/html !db normal-page))
(c/quick-bench (str (h/html normal-page)))
(c/quick-bench (ch/html normal-page)))
Expand Down
24 changes: 8 additions & 16 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
{:paths ["src"]
:deps {}
:deps {me.raystubbs/check {:local/root "../check"}}

:aliases
{:dev
{:extra-deps {org.clojure/clojure {:mvn/version "1.11.3"}
org.clojure/clojurescript {:mvn/version "1.11.60"}}
:extra-paths ["test/cljs" "test/clj" "test/cljc"]}
{:extra-deps {org.clojure/clojure {:mvn/version "1.12.0"}
org.clojure/clojurescript {:mvn/version "1.11.60"}
thheller/shadow-cljs {:mvn/version "2.21.0"}}
:me.raystubbs.check/enabled true
:extra-paths ["dev"]}

:build
{:deps {io.github.clojure/tools.build {:git/tag "v0.9.6" :git/sha "8e78bcc"}
slipset/deps-deploy {:mvn/version "0.2.2"}}
:ns-default build}

:test-cljs
{:extra-deps {thheller/shadow-cljs {:mvn/version "2.21.0"}}
:extra-paths ["test/cljs" "test/cljc" "test/browser"]}

:test-clj
{:extra-paths ["test/cljc" "test/clj"]
:extra-deps {io.github.cognitect-labs/test-runner {:git/tag "v0.5.1" :git/sha "dfb30dd"}}
:main-opts ["-m" "cognitect.test-runner"]
:exec-fn cognitect.test-runner.api/test}

:bench
{:extra-deps {criterium/criterium {:mvn/version "0.4.6"}
dev.onionpancakes/chassis {:mvn/version "1.0.365"}
Expand All @@ -30,5 +22,5 @@
selmer/selmer {:mvn/version "1.12.59"}
enlive/enlive {:mvn/version "1.1.6"}}
:jvm-opts ["-Djdk.attach.allowAttachSelf"]
:extra-paths ["dev" "test" "resources" "bench"]
:exec-fn bench/bench-all}}}
:extra-paths ["dev" "resources" "bench"]
:exec-fn bench/bench-all!}}}
13 changes: 13 additions & 0 deletions dev/dev.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns dev
(:require
["happy-dom" :as happy-dom]))

(defn create-window ^js [& {:as opts}]
(happy-dom/Window. (clj->js opts)))

(defn dispose-window [^js/Window window]
(-> window .-happyDOM .waitUntilComplete
(.then
(fn []
(-> window .-happyDOM .close)))))

17 changes: 17 additions & 0 deletions dev/test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns test
(:require
[check.core :as check]
[subzero.core]
[subzero.rstore]
[subzero.plugins.html]))

(defn handle-status
[status]
(when-not (some #(= ::check/pending %) (vals status))
(when (some #(= ::check/failure %) (vals status))
(System/exit 1))))

(defn- run
[& _args]
(add-watch check/!status ::status (fn [_ _ _ status] (handle-status status)))
(handle-status @check/!status))
16 changes: 16 additions & 0 deletions dev/test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns test
(:require
[check.core :as check]
[subzero.core]
[subzero.rstore]
[subzero.plugins.html]
[subzero.plugins.web-components]))

(defn handle-status
[status]
(when-not (some #(= ::check/pending %) (vals status))
(when (some #(= ::check/failure %) (vals status))
(js/process.exit 1))))

(add-watch check/!status ::status (fn [_ _ _ status] (handle-status status)))
(handle-status @check/!status)
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"scripts": {
"test": "shadow-cljs compile browser-tests node-tests && web-test-runner \"target/browser-tests/all.js\" --node-resolve && node target/node-tests.js"
"test": "shadow-cljs compile test && node --enable-source-maps target/test/all.js"
},
"devDependencies": {
"@web/test-runner": "^0.18.1",
"happy-dom": "^15.8.0",
"shadow-cljs": "^2.28.4"
}
}
},
"type": "module"
}
16 changes: 6 additions & 10 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{:deps {:aliases [:test-cljs]}
{:deps {:aliases [:dev]}

:builds
{:browser-tests
{:test
{:target :esm
:output-dir "target/browser-tests"
:ns-regexp ".+[-]btest$"
:modules {:all {:entries [subzero.component-btest]}}}

:node-tests
{:target :node-test
:ns-regexp ".+[-]test$"
:output-to "target/node-tests.js"}}}
:runtime :node
:output-dir "target/test"
:js-options {:js-provider :import}
:modules {:all {:entries [dev test]}}}}}
40 changes: 31 additions & 9 deletions src/subzero/core.cljc
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
(ns subzero.core
(:require
[subzero.rstore :as rstore]
[subzero.impl.markup :as markup]))
[subzero.impl.markup :as markup]
[check.core :refer [check]]))

(defn create-db
[]
(rstore/rstore {}))

(defn dispose-db
(defn dispose-db!
[!db]
(doseq [plugin-state (vals @!db)
:let [finl-fn (::finl plugin-state)]
:when (ifn? finl-fn)]
(finl-fn))
(finl-fn plugin-state))
(rstore/patch! !db {:path [] :change [:value {}]}))

(defn install-plugin!
Expand All @@ -36,7 +37,7 @@
(let [[old-db _] (rstore/patch! !db {:path [] :change [:clear k]})]
(when-let [plugin-state (get old-db k)]
(when (fn? (::finl plugin-state))
((::finl plugin-state))))))
((::finl plugin-state) plugin-state)))))

(defn element-name
[kw]
Expand All @@ -46,10 +47,10 @@
(do
(defn js-proxy [x & {:keys [get]}]
(js* "new Proxy(~{}, ~{})" x #js{:get get}))

(def ^:private iequiv-prop "cljs$core$IEquiv$_equiv$arity$2")
(defonce ^:private key-eq-sym (js/Symbol "equivKey"))

(defn with-ident-eq "
Returns a version of `x` with IEquiv overridden with
an identity check. Useful to avoid comparison overhead
Expand All @@ -62,7 +63,7 @@
(if (= prop iequiv-prop)
identical?
(js/Reflect.get target prop receiver)))))

(defn with-const-eq "
Returns a version of `x` with IEquiv overridden to compare
against a given constant. Meant to help optimize prop
Expand All @@ -79,11 +80,32 @@
(cond
(= key-eq-sym prop)
k

(= iequiv-prop prop)
equiv-fn

:else
(js/Reflect.get target prop receiver))))))
nil))

#?(:cljs
(do
(check ::with-ident-eq
(let [m1 (with-ident-eq (hash-map :foo "BAR"))
m2 (with-ident-eq (hash-map :foo "BAR"))]
(assert (not= m1 m2))
(assert (= m1 m1))
(assert (= m2 m2))
(assert (map? m1))
(assert (map? m2))))
(check ::with-const-eq
(let [m1 (with-const-eq :foo {:foo "BAR"})
m2 (with-const-eq :foo {:foo "BAR"})
m3 (with-const-eq :bar {:foo "BAR"})
m4 {:foo "BAR"}]
(assert (= m1 m2))
(assert (not= m1 m3))
(assert (not= m1 m4))
(assert (map? m1))
(assert (map? m2))
(assert (map? m3))))))
8 changes: 6 additions & 2 deletions src/subzero/logger.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#?(:clj [clojure.stacktrace :as stacktrace])))

(defmulti log (fn log-dispatch [level _msg & _opts] level))
(def ^:private devtools? (boolean (find-ns 'devtools.formatters.core)))

(defmethod log :default
[level msg {:keys [data ex file line]}]
Expand All @@ -18,7 +19,10 @@
(conj (cond-> data (and file line) (conj [:loc (str file ":" line)])))

(some? ex)
(into ["\n" ex]))))
(into ["\n" ex])

(not devtools?)
(->> (map pr-str)))))
:clj (binding [*out* *err*]
(printf "%s %s%n"
(case level
Expand Down Expand Up @@ -69,4 +73,4 @@
{:data {:spy spy-name}}
(-> &form meta (select-keys [:file :line]))
{:ns `'~(ns-name *ns*)}))
v#)))
v#)))
Loading

0 comments on commit 81631b0

Please sign in to comment.