Skip to content

Commit

Permalink
add tests verifying entity printability and pr-str consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Scherer committed Oct 3, 2017
1 parent 032ab3b commit ea5981b
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/claro/data/error.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

(defmethod print-method ResolutionError
[^ResolutionError value ^java.io.Writer writer]
(.write writer "<error ")
(.write writer "#<claro/error ")
(print-method (.-message value) writer)
(when-let [data (.-data value)]
(.write writer ", ")
Expand Down
2 changes: 1 addition & 1 deletion src/claro/projection/bind.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
[template' (bind-fn partial-value)]
(pr/project template' value))))))

(defrecord BindProjection [template bind-fn]
(deftype BindProjection [template bind-fn]
pr/Projection
(project [_ value]
(then value #(bind-template % template bind-fn))))
Expand Down
4 changes: 2 additions & 2 deletions src/claro/projection/case.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

;; ## Records

(defrecord CaseResolvableProjection [class->template]
(deftype CaseResolvableProjection [class->template]
pr/Projection
(project [_ value]
(with-error? value
Expand All @@ -51,7 +51,7 @@
(print-method (.-class->template value) w)
(.write w ">"))

(defrecord CaseProjection [class->template]
(deftype CaseProjection [class->template]
pr/Projection
(project [_ value]
(with-error? value
Expand Down
12 changes: 9 additions & 3 deletions src/claro/projection/conditional.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@
(if else-template
[:done (pr/project else-template value)]))))

(defrecord ConditionalProjection [template
condition->template
else-template]
(deftype ConditionalProjection [template
condition->template
else-template]
pr/Projection
(project [_ value]
(with-error? value
(-> (pr/project template value)
(then!
#(project-match condition->template else-template value %))))))

(defmethod print-method ConditionalProjection
[^ConditionalProjection value ^java.io.Writer w]
(.write w "#<claro/conditional ")
(print-method (.-template value) w)
(.write w " => ...>"))

;; ## Constructors

(defn- make-conditional
Expand Down
2 changes: 1 addition & 1 deletion src/claro/projection/juxt.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

;; ## Record

(defrecord JuxtProjection [templates]
(deftype JuxtProjection [templates]
pr/Projection
(project [_ value]
(with-error? value
Expand Down
8 changes: 7 additions & 1 deletion src/claro/projection/level.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@
(map? value) (project-map value current-level)
:else (project-coll value current-level)))

(defrecord LevelProjection [n]
(deftype LevelProjection [n]
pr/Projection
(project [_ value]
(then value #(project-level % n))))

(defmethod print-method LevelProjection
[^LevelProjection value ^java.io.Writer w]
(.write w "#<claro/level")
(print-method (.-n value) w)
(.write w ">"))

;; ## Constructor

(defn levels
Expand Down
2 changes: 1 addition & 1 deletion src/claro/projection/maps.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
;;
;; This can be used directly in maps to indicate a renamed/copied key.

(defrecord AliasKey [alias-key key]
(deftype AliasKey [alias-key key]
MapKeyProjection
(target-key [this value]
(assert-no-override! this value alias-key)
Expand Down
4 changes: 2 additions & 2 deletions src/claro/projection/maybe.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

;; ## Maybe

(defrecord MaybeProjection [template]
(deftype MaybeProjection [template]
pr/Projection
(project [_ value]
(then value
Expand Down Expand Up @@ -38,7 +38,7 @@

;; ## Default

(defrecord DefaultProjection [template default-value]
(deftype DefaultProjection [template default-value]
pr/Projection
(project [_ value]
(then value
Expand Down
8 changes: 4 additions & 4 deletions src/claro/projection/objects.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

;; ## Templates

(defrecord LeafProjection []
(deftype LeafProjection []
Projection
(project [_ value]
(chain-eager value assert-leaf)))
Expand All @@ -35,7 +35,7 @@
(project [_ value]
(chain-eager value assert-leaf)))

(defrecord UnsafeProjection []
(deftype UnsafeProjection []
Projection
(project [_ value]
value))
Expand All @@ -50,8 +50,8 @@

(defmethod print-method LeafProjection
[value ^java.io.Writer writer]
(.write writer "<claro/leaf>"))
(.write writer "#<claro/leaf>"))

(defmethod print-method UnsafeProjection
[value ^java.io.Writer writer]
(.write writer "<claro/unsafe>"))
(.write writer "#<claro/unsafe>"))
4 changes: 2 additions & 2 deletions src/claro/projection/parameters.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
(assert-resolvable! params)
(p/set-parameters params)))

(defrecord ParametersProjection [params rest-template]
(deftype ParametersProjection [params rest-template]
pr/Projection
(project [_ value]
(with-error? value
Expand Down Expand Up @@ -91,7 +91,7 @@

;; ## Parameter Injection (w/ Null Tolerance)

(defrecord MaybeParametersProjection [params rest-template]
(deftype MaybeParametersProjection [params rest-template]
pr/Projection
(project [_ value]
(with-error? value
Expand Down
10 changes: 9 additions & 1 deletion src/claro/projection/remove_nil.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

;; ## Implementation

(defrecord RemoveNilProjection [template]
(deftype RemoveNilProjection [template]
p/Projection
(project [_ original]
(with-error? original
Expand All @@ -48,6 +48,14 @@
(p/project template result)
result)))))

(defmethod print-method RemoveNilProjection
[^RemoveNilProjection value ^java.io.Writer w]
(.write w "#<claro/remove-nil-elements")
(when-let [template (.-template value)]
(.write w " ")
(print-method template w))
(.write w ">"))

;; ## Wrappers

(defn remove-nil-elements
Expand Down
2 changes: 1 addition & 1 deletion src/claro/projection/sort.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[error :refer [with-error?]]
[ops :as ops]]))

(defrecord SortProjection [sort-template output-template]
(deftype SortProjection [sort-template output-template]
p/Projection
(project [_ original]
(with-error? original
Expand Down
6 changes: 3 additions & 3 deletions src/claro/projection/transform.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(->> (tree/transform-partial value f)
(pr/project rest-template))))

(defrecord Preparation [f rest-template]
(deftype Preparation [f rest-template]
pr/Projection
(project [_ value]
(apply-preparation value f rest-template)))
Expand All @@ -32,7 +32,7 @@

;; ## Transformation (after Resolution)

(defrecord Transformation [f input-template output-template]
(deftype Transformation [f input-template output-template]
pr/Projection
(project [_ value]
(with-error? value
Expand Down Expand Up @@ -76,7 +76,7 @@

;; ## Transformation to Finite Value

(defrecord FiniteTransformation [f input-template]
(deftype FiniteTransformation [f input-template]
pr/Projection
(project [_ value]
(with-error? value
Expand Down
4 changes: 2 additions & 2 deletions src/claro/projection/union.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
(into {} maps))
(first maps)))

(defrecord UnionProjection [templates]
(deftype UnionProjection [templates]
pr/Projection
(project [_ value]
(with-error? value
Expand All @@ -53,7 +53,7 @@
(into {} maps)
(first maps)))

(defrecord MergeProjection [templates]
(deftype MergeProjection [templates]
pr/Projection
(project [_ value]
(with-error? value
Expand Down
2 changes: 1 addition & 1 deletion src/claro/projection/value.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

;; ## Record

(defrecord ValueProjection [value template]
(deftype ValueProjection [value template]
pr/Projection
(project [_ value']
(with-error? value'
Expand Down
53 changes: 1 addition & 52 deletions test/claro/projection/error_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,9 @@
[claro.test :as test]
[claro.data :as data]
[claro.projection :as projection]
[claro.projection.generators :refer [gen-projection gen-error]]
[claro.engine.fixtures :refer [make-engine]]))

(defn key-set
[v]
(if (map? v)
(set (keys v))))

(defn gen-distinct
[g]
(gen/such-that
#(or (not-any? map? %)
(empty?
(clojure.set/intersection
(key-set (first %))
(key-set (second %)))))
(gen/vector g 2)))

(def gen-projection
(->> (gen/recursive-gen
(fn [g]
(gen/one-of
[(gen/fmap vector g)
(gen/fmap hash-set g)
(gen/let [v gen/string-alpha-numeric]
(gen/hash-map v g))

(gen/let [v gen/string-alpha-numeric]
(gen/hash-map (projection/alias v :nested) g))
(gen/fmap projection/maybe g)
(gen/fmap projection/extract gen/keyword)
(gen/fmap #(projection/juxt % %) g)
(gen/fmap #(projection/transform identity % %) g)
(gen/fmap #(projection/prepare identity %) g)
(gen/fmap #(projection/parameters {} %) g)
(gen/fmap #(projection/conditional % some? %) g)
(gen/fmap #(projection/case Object %) g)
(gen/fmap #(projection/case-resolvable Object %) g)
(gen/fmap projection/union* (gen-distinct g))]))
(gen/elements
[(projection/default projection/leaf ::default)
(projection/levels 1)
(projection/value ::value)
(projection/value {:a 1} {:a projection/leaf})
(projection/finite-value ::value)
projection/leaf]))))

(def gen-error
(->> (gen/tuple
gen/string-ascii
(gen/one-of
[(gen/return nil)
(gen/map gen/string-ascii gen/string-ascii)]))
(gen/fmap #(apply data/error %))))

(defspec t-projection-retains-error-values (test/times 250)
(let [run! (make-engine)]
(prop/for-all
Expand Down
64 changes: 64 additions & 0 deletions test/claro/projection/generators.clj
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,67 @@
(= (:value value) expected-value))
(or (not (contains? template :next))
(recur (:next value) (:next template) (inc expected-value)))))

(defn key-set
[v]
(if (map? v)
(set (keys v))))

;; ## Projection Generator

(defn gen-distinct
[g]
(gen/such-that
#(or (not-any? map? %)
(empty?
(clojure.set/intersection
(key-set (first %))
(key-set (second %)))))
(gen/vector g 2)))

(def gen-projection
(let [leaf-gen (gen/elements
[(projection/default projection/leaf ::default)
(projection/levels 1)
(projection/value ::value)
(projection/value {:a 1} {:a projection/leaf})
(projection/finite-value ::value)
projection/leaf
projection/unsafe])]
(gen/one-of
[(gen/recursive-gen
(fn [g]
(gen/one-of
[(gen/fmap vector g)
(gen/fmap hash-set g)
(gen/let [v gen/string-alpha-numeric]
(gen/hash-map v g))

(gen/let [v gen/string-alpha-numeric]
(gen/hash-map (projection/alias v :nested) g))
(gen/fmap projection/maybe g)
(gen/fmap projection/extract gen/keyword)
(gen/fmap #(projection/juxt % %) g)
(gen/fmap #(projection/transform identity % %) g)
(gen/fmap #(projection/transform-finite identity %) g)
(gen/fmap #(projection/prepare identity %) g)
(gen/fmap #(projection/parameters {} %) g)
(gen/fmap #(projection/maybe-parameters {} %) g)
(gen/fmap #(projection/conditional % some? %) g)
(gen/fmap #(projection/case Object %) g)
(gen/fmap #(projection/case-resolvable Object %) g)
(gen/fmap #(projection/sort-by % %) g)
(gen/fmap #(projection/bind (constantly %) %) g)
(gen/fmap projection/remove-nil-elements g)
(gen/fmap projection/union* (gen-distinct g))
(gen/fmap projection/merge* (gen-distinct g))]))
leaf-gen)
leaf-gen])))

(def gen-error
(->> (gen/tuple
gen/string-ascii
(gen/one-of
[(gen/return nil)
(gen/map gen/string-ascii gen/string-ascii)]))
(gen/fmap #(apply data/error %))))
Loading

0 comments on commit ea5981b

Please sign in to comment.