Skip to content

Commit 1e0a6f5

Browse files
authored
test fn? (#104)
* test `fn?` * test fn? for protocols and multimethods This is arguably an implementation detail and not worth testing. Included for completeness nonetheless. * don't test fn? for multimethods or protocols * `testing` groups > comments
1 parent d863fd7 commit 1e0a6f5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(ns clojure.core-test.fn-qmark
2+
(:require clojure.core
3+
[clojure.test :as t :refer [deftest testing is are]]
4+
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
5+
6+
(defn foo [x] (str "hello " x))
7+
8+
(when-var-exists clojure.core/fn?
9+
(deftest test-fn?
10+
(testing "`fn?`"
11+
(testing "functions, functions from HOFs, transducers, #() reader macro, `fn`, `defn`"
12+
(is (fn? juxt))
13+
(is (fn? (juxt inc dec)))
14+
(is (fn? (map inc)))
15+
(is (fn? #(str "hello " %)))
16+
(is (fn? (fn [x] (str "hello " x))))
17+
(is (fn? foo)))
18+
19+
;; Note: we intentionally do not test multimethods or protocols, because
20+
;; behavior differs across dialects due to implementation decisions that
21+
;; don't matter for our purposes.
22+
23+
(testing "atomic values are not functions"
24+
(is (not (fn? nil)))
25+
(is (not (fn? 12345678)))
26+
(is (not (fn? "string")))
27+
(is (not (fn? :keyword))) ; note: also IFn
28+
(is (not (fn? 'symbol)))
29+
(is (not (fn? \space))))
30+
31+
(testing "implementing IFn is not the same as implementing Fn"
32+
(is (not (fn? {:a :b})))
33+
(is (not (fn? #{:a :b :c})))
34+
(is (not (fn? [:a :b])))))))

0 commit comments

Comments
 (0)