implement tests for protocol paths, make clear that it only works in clj

This commit is contained in:
Nathan Marz 2015-12-12 12:37:21 -05:00
parent fc51d70b0f
commit 827726cf54
2 changed files with 52 additions and 4 deletions

View file

@ -566,12 +566,12 @@
(defn params-needed-transformer [^com.rpl.specter.impl.ParamsNeededPath path] (defn params-needed-transformer [^com.rpl.specter.impl.ParamsNeededPath path]
(-> path .-transform-fns .-transformer)) (-> path .-transform-fns .-transformer))
#+clj
(defn extend-protocolpath* [protpath-prot extensions] (defn extend-protocolpath* [protpath-prot extensions]
(let [extensions (partition 2 extensions) (let [extensions (partition 2 extensions)
m (-> protpath-prot :sigs keys first)] m (-> protpath-prot :sigs keys first)]
(doseq [[atype apath] extensions] (doseq [[atype apath] extensions]
;;TODO: validate that the path has the correct number of args ;;TODO: validate that the path has the correct number of args (or none at all)
(let [p (comp-paths* apath)] (let [p (comp-paths* apath)]
(extend atype protpath-prot {m (fn [_] p)}) (extend atype protpath-prot {m (fn [_] p)})
)))) ))))

View file

@ -3,12 +3,12 @@
[cljs.test :refer [is deftest]] [cljs.test :refer [is deftest]]
[cljs.test.check.cljs-test :refer [defspec]] [cljs.test.check.cljs-test :refer [defspec]]
[com.rpl.specter.cljs-test-helpers :refer [for-all+]] [com.rpl.specter.cljs-test-helpers :refer [for-all+]]
[com.rpl.specter.macros :refer [paramsfn]]) [com.rpl.specter.macros :refer [paramsfn defprotocolpath defpath extend-protocolpath]])
(:use (:use
#+clj [clojure.test :only [deftest is]] #+clj [clojure.test :only [deftest is]]
#+clj [clojure.test.check.clojure-test :only [defspec]] #+clj [clojure.test.check.clojure-test :only [defspec]]
#+clj [com.rpl.specter.test-helpers :only [for-all+]] #+clj [com.rpl.specter.test-helpers :only [for-all+]]
#+clj [com.rpl.specter.macros :only [paramsfn]] #+clj [com.rpl.specter.macros :only [paramsfn defprotocolpath defpath extend-protocolpath]]
) )
@ -579,3 +579,51 @@
;;TODO: there's a bug in clojurescript that won't allow ;;TODO: there's a bug in clojurescript that won't allow
;; non function implementations of IFn to have more than 20 arguments ;; non function implementations of IFn to have more than 20 arguments
#+clj
(do
(defprotocolpath AccountPath [])
(defrecord Account [funds])
(defrecord User [account])
(defrecord Family [accounts])
(extend-protocolpath AccountPath User :account Family [:accounts s/ALL])
)
#+clj
(deftest protocolpath-basic-test
(let [data [(->User (->Account 30))
(->User (->Account 50))
(->Family [(->Account 51) (->Account 52)])]]
(is (= [30 50 51 52]
(s/select [s/ALL AccountPath :funds] data)))
(is (= [(->User (->Account 31))
(->User (->Account 51))
(->Family [(->Account 52) (->Account 53)])]
(s/transform [s/ALL AccountPath :funds]
inc
data)))
))
#+clj
(do
(defprotocolpath LabeledAccountPath [label])
(defrecord LabeledUser [account])
(defrecord LabeledFamily [accounts])
(extend-protocolpath LabeledAccountPath
LabeledUser [:account s/keypath]
LabeledFamily [:accounts s/keypath s/ALL])
)
#+clj
(deftest protocolpath-params-test
(let [data [(->LabeledUser {:a (->Account 30)})
(->LabeledUser {:a (->Account 50)})
(->LabeledFamily {:a [(->Account 51) (->Account 52)]})]]
(is (= [30 50 51 52]
(s/select [s/ALL (LabeledAccountPath :a) :funds] data)))
(is (= [(->LabeledUser {:a (->Account 31)})
(->LabeledUser {:a (->Account 51)})
(->LabeledFamily {:a [(->Account 52) (->Account 53)]})]
(s/transform [s/ALL (LabeledAccountPath :a) :funds]
inc
data)))
))