Skip to content

Commit

Permalink
add tests for collection operations, improve error message for 'nth'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Scherer committed Oct 3, 2017
1 parent f653eb6 commit e6d5c55
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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
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))))))

0 comments on commit e6d5c55

Please sign in to comment.