Skip to content

Commit eb8daa1

Browse files
authored
Add tests for apply (#870)
* Add tests for `apply` * Fix: don't depend on order of map entries * Fix comment and remove trailing white space * New test: apply `apply` recursively * Spell "sequence" correctly * Test whether an infinite sequence arg is realized * Finish comment
1 parent 485de6b commit eb8daa1

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

test/clojure/core_test/apply.cljc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
(ns clojure.core-test.apply
2+
(:require [clojure.test :as t :refer [are deftest is testing]]
3+
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists] :as p]))
4+
5+
(when-var-exists apply
6+
(deftest test-apply
7+
(is (= 0 (apply + nil))) ; Apply + with no args
8+
(is (= 0 (apply + '())))
9+
(is (= 0 (apply + [])))
10+
(is (= 0 (apply + {}))) ; map is a sequence of map entries
11+
(is (= 0 (apply + #{})))
12+
(is (= 0 (apply + ""))) ; string is a sequence of characters
13+
14+
(is (= 1 (apply + 1 nil))) ; Apply + with 1 arg and empty sequence
15+
(is (= 1 (apply + 1 '())))
16+
(is (= 1 (apply + 1 [])))
17+
(is (= 1 (apply + 1 {})))
18+
(is (= 1 (apply + 1 #{})))
19+
(is (= 1 (apply + 1 "")))
20+
21+
(is (= 1 (apply + '(1)))) ; Zero non-sequence arg
22+
(is (= 3 (apply + 1 '(2)))) ; One non-sequential arg
23+
(is (= 6 (apply + 1 2 '(3)))) ; Two non-sequential args
24+
(is (= 10 (apply + 1 2 3 '(4)))) ; Three non-sequential args
25+
(is (= 15 (apply + 1 2 3 4 '(5)))) ; Four non-sequential args
26+
(is (= 45 (apply + (range 10)))) ; All args as sequential
27+
(is (= 10 (apply + 1 [2 3 4])))
28+
(is (= 10 (apply + 1 #{2 3 4}))) ; A set works but order not guaranteed
29+
(is (= #{[:a 1] [:b 2]} ; Can't guarantee order of map entries
30+
(apply conj #{} {:a 1, :b 2})))
31+
(is (= [\a \b \c] ; String is sequence of chars
32+
(apply conj [] "abc")))
33+
34+
;; Try various IFn things
35+
(is (= 1 (apply {:a 1} [:a]))) ; apply map to key
36+
(is (= 1 (apply :a [{:a 1}]))) ; apply keyword to map
37+
(is (= 2 (apply [0 1 2 3 4] [2]))) ; apply vector to index
38+
(is (p/thrown? (apply 2 [[0 1 2 3 4]]))) ; but numbers don't implement IFn
39+
(is (= :a (apply #{:a :b :c} [:a]))) ; apply set to element
40+
(is (= :a (apply :a [#{:a :b :c}]))) ; apply keyword to set
41+
42+
;; apply recrusively
43+
(is (= 10 (apply apply + [1 2 [3 4]])))
44+
45+
;; validate that `apply` doesn't try to further evaluate its
46+
;; arguments. If the infinite range is realized, we would expect
47+
;; an OOM Exception at some point.
48+
(is (= 3 (count (apply conj [] [1 2 (range)]))))))

0 commit comments

Comments
 (0)