forked from jank-lang/clojure-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply.cljc
More file actions
46 lines (40 loc) · 2.14 KB
/
apply.cljc
File metadata and controls
46 lines (40 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(ns clojure.core-test.apply
(:require [clojure.test :as t :refer [are deftest is testing]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists] :as p]))
(when-var-exists apply
(deftest test-apply
(is (= 0 (apply + nil))) ; Apply + with no args
(is (= 0 (apply + '())))
(is (= 0 (apply + [])))
(is (= 0 (apply + {}))) ; map is a sequence of map entries
(is (= 0 (apply + #{})))
(is (= 0 (apply + ""))) ; string is a sequence of characters
(is (= 1 (apply + 1 nil))) ; Apply + with 1 arg and empty sequence
(is (= 1 (apply + 1 '())))
(is (= 1 (apply + 1 [])))
(is (= 1 (apply + 1 {})))
(is (= 1 (apply + 1 #{})))
(is (= 1 (apply + 1 "")))
(is (= 1 (apply + '(1)))) ; Zero non-sequence arg
(is (= 3 (apply + 1 '(2)))) ; One non-sequential arg
(is (= 6 (apply + 1 2 '(3)))) ; Two non-sequential args
(is (= 10 (apply + 1 2 3 '(4)))) ; Three non-sequential args
(is (= 15 (apply + 1 2 3 4 '(5)))) ; Four non-sequential args
(is (= 45 (apply + (range 10)))) ; All args as sequential
(is (= 10 (apply + 1 [2 3 4])))
(is (= 10 (apply + 1 #{2 3 4}))) ; A set works but order not guaranteed
(is (= #{[:a 1] [:b 2]} ; Can't guarantee order of map entries
(apply conj #{} {:a 1, :b 2})))
(is (= [\a \b \c] ; String is sequence of chars
(apply conj [] "abc")))
;; Try various IFn things
(is (= 1 (apply {:a 1} [:a]))) ; apply map to key
(is (= 1 (apply :a [{:a 1}]))) ; apply keyword to map
(is (= 2 (apply [0 1 2 3 4] [2]))) ; apply vector to index
(is (p/thrown? (apply 2 [[0 1 2 3 4]]))) ; but numbers don't implement IFn
(is (= :a (apply #{:a :b :c} [:a]))) ; apply set to element
(is (= :a (apply :a [#{:a :b :c}]))) ; apply keyword to set
;; apply recrusively
(is (= 10 (apply apply + [1 2 [3 4]])))
;; validate that `apply` doesn't try to further evaluate its arguments. If the range
(is (= 3 (count (apply conj [] [1 2 (range)]))))))