diff --git a/src/claro/data/ops/collections.cljc b/src/claro/data/ops/collections.cljc index 2044abf..8cbc675 100644 --- a/src/claro/data/ops/collections.cljc +++ b/src/claro/data/ops/collections.cljc @@ -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:"))) diff --git a/test/claro/data/ops/collections_test.clj b/test/claro/data/ops/collections_test.clj index bf513b4..f642b22 100644 --- a/test/claro/data/ops/collections_test.clj +++ b/test/claro/data/ops/collections_test.clj @@ -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))))))