Skip to content

Commit 2f8b271

Browse files
committed
test juxt
1 parent fc93f9a commit 2f8b271

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test/clojure/core_test/juxt.cljc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
(ns clojure.core-test.juxt
2+
(:require [clojure.test :as t :refer [deftest testing is are]]
3+
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
4+
5+
(when-var-exists juxt
6+
(deftest test-juxt
7+
(testing "'returns a function'"
8+
(is (ifn? (juxt juxt partial comp :foo)))
9+
(is (ifn? (juxt nil)))
10+
(is (ifn? (juxt (range)))))
11+
12+
(testing "'the returned fn...returns a vector'"
13+
(is (vector? ((juxt inc) 5))))
14+
15+
(testing "'The returned fn takes a variable number of args'"
16+
(is (= ["12345678910" 1 10]
17+
((juxt str min max) 1 2 3 4 5 6 7 8 9 10))))
18+
19+
(testing "'returns a fn that is the juxtaposition of those fns'"
20+
(is (let [arg 10]
21+
(= (vector (inc arg) (dec arg) (identity arg))
22+
((juxt inc dec identity) arg))))
23+
(is (= ["foo" nil "bar"]
24+
((juxt :foo :missing :bar) {:foo "foo", :bar "bar"})))
25+
(is (= [:namespace/keyword "keyword" "namespace"]
26+
((juxt identity name namespace) :namespace/keyword)))
27+
(is (= [:v] ((juxt {:k :v}) :k)))
28+
(is (= [:v] ((juxt :k) {:k :v})))
29+
30+
(is (= [:v :value "value"]
31+
((juxt {:k :v}
32+
#(get {:k :value} %)
33+
(fn [x] (get {:k "value"} x)))
34+
:k))))
35+
36+
(testing "wrong-shape input is mostly accepted (and throws when invoked)"
37+
(is (thrown? #?(:cljs :default, :clj java.lang.NullPointerException)
38+
((juxt nil))))
39+
(is (thrown? #?(:cljs :default, :clj Exception)
40+
((juxt (range)))))
41+
(is (thrown? #?(:cljs :default, :clj Exception)
42+
((juxt 1)))))))

0 commit comments

Comments
 (0)