Skip to content

Commit

Permalink
[#16] improve test coverage & fix discovered bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
xsc committed Oct 3, 2017
2 parents aa13cc4 + d56f45c commit 1f501f8
Show file tree
Hide file tree
Showing 29 changed files with 590 additions and 88 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
14 changes: 13 additions & 1 deletion src/claro/data/ops/collections.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,19 @@
p/every-processable?
(wrap-assert-coll
(fn [[v n]]
(core/nth v n))
(try
(core/nth v n)
(catch java.lang.IndexOutOfBoundsException e
(throw
(IllegalArgumentException.
(format
(str "index %d out of bounds when calling 'nth'.%n"
"resolvable: %s%n"
"collection: %s")
n
(pr-str value)
(pr-str v))
e)))))
sequential?
"can only apply 'nth' to sequentials, given:")))

Expand Down
2 changes: 1 addition & 1 deletion src/claro/middleware/observe.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
(engine/impl engine)
(resolver env batch)
(fn [result]
(when (predicate result)
(when (predicate batch)
(observer-fn result))
result))))
(engine/wrap-transform engine))))
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>"))
11 changes: 6 additions & 5 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,13 +91,14 @@

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

(defrecord MaybeParametersProjection [params rest-template]
(deftype MaybeParametersProjection [params rest-template]
pr/Projection
(project [_ value]
(with-error? value
(->> #(some-> % (inject-params params))
(tree/transform-partial value)
(pr/project rest-template)))))
(->> #(some-> %
(inject-params params)
(->> (pr/project rest-template)))
(tree/transform-partial value)))))

(defmethod print-method MaybeParametersProjection
[^MaybeParametersProjection value ^java.io.Writer w]
Expand Down
12 changes: 10 additions & 2 deletions src/claro/projection/remove_nil.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@

;; ## Implementation

(defrecord RemoveNilProjection [template]
(deftype RemoveNilProjection [template]
p/Projection
(project [_ original]
(with-error? original
(let [result (then original remove-nil-elements*)]
(if template
(p/project template result)
(then result #(p/project template %))
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
36 changes: 36 additions & 0 deletions test/claro/data/ops/collections_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,39 @@
[{nums1 :nums, r1 :resolvable} gen-collection
{nums2 :nums, r2 :resolvable} gen-collection]
(= (map + nums1 nums2) (run!! (ops/map + r1 r2))))))

(defspec t-first (test/times 50)
(let [run!! (comp deref (make-engine))]
(prop/for-all
[{:keys [nums resolvable]} gen-collection]
(= (first nums) (run!! (ops/first resolvable))))))

(defspec t-nth (test/times 50)
(let [run!! (comp deref (make-engine))]
(prop/for-all
[{:keys [nums resolvable]} gen-collection
n gen/pos-int
wrapper (gen/elements [identity ->Identity])]
(let [value (ops/nth resolvable (wrapper n))]
(if (< n (count nums))
(= (nth nums n) (run!! value))
(boolean
(is
(thrown-with-msg?
java.lang.IllegalArgumentException
#"index \d+ out of bounds when calling 'nth'"
(run!! value)))))))))

(defspec t-take (test/times 50)
(let [run!! (comp deref (make-engine))]
(prop/for-all
[{:keys [nums resolvable]} gen-collection
n gen/pos-int]
(= (take n nums) (run!! (ops/take n resolvable))))))

(defspec t-drop (test/times 50)
(let [run!! (comp deref (make-engine))]
(prop/for-all
[{:keys [nums resolvable]} gen-collection
n gen/pos-int]
(= (drop n nums) (run!! (ops/drop n resolvable))))))
51 changes: 51 additions & 0 deletions test/claro/data/ops/fmap_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(ns claro.data.ops.fmap-test
(:require [clojure.test.check :as tc]
[clojure.test.check
[clojure-test :refer [defspec]]
[generators :as gen]
[properties :as prop]]
[clojure.test :refer :all]
[claro.test :as test]
[claro.data :as data]
[claro.data.ops.fmap :as ops]
[claro.engine.fixtures :refer [make-engine]]))

;; ## Resolvable

(defrecord Identity [v]
data/Resolvable
(resolve! [_ _]
v))

(def gen-nums
(gen/not-empty
(gen/vector
(gen/one-of
[gen/int
(gen/fmap ->Identity gen/int)]))))

(def gen-op
(gen/elements [vector + hash-set]))

;; ## Tests

(defspec t-fmap (test/times 100)
(let [run!! (comp deref (make-engine))]
(prop/for-all
[nums gen-nums, op gen-op]
(let [value (apply ops/fmap op nums)]
(= (apply op (run!! nums)) (run!! value))))))

(defspec t-fmap-on (test/times 100)
(let [run!! (comp deref (make-engine))]
(prop/for-all
[nums gen-nums, op gen-op]
(let [value (apply ops/fmap-on #(every? number? %&) op nums)]
(= (apply op (run!! nums)) (run!! value))))))

(defspec t-fmap! (test/times 100)
(let [run!! (comp deref (make-engine))]
(prop/for-all
[nums gen-nums, op gen-op]
(let [value (apply ops/fmap! op nums)]
(= (apply op (run!! nums)) (run!! value))))))
Loading

0 comments on commit 1f501f8

Please sign in to comment.