Skip to content

Commit 4322f3b

Browse files
puredangerrichhickey
authored andcommitted
specs for defn, defn-, fn
Signed-off-by: Rich Hickey <richhickey@gmail.com>
1 parent dcc29d8 commit 4322f3b

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

src/clj/clojure/core/specs.clj

+35
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,38 @@
6161
(s/fdef clojure.core/when-let
6262
:args (s/cat :bindings (s/and vector? ::binding)
6363
:body (s/* any?)))
64+
65+
;; defn, defn-, fn
66+
67+
(s/def ::arg-list
68+
(s/and
69+
vector?
70+
(s/cat :args (s/* ::binding-form)
71+
:varargs (s/? (s/cat :amp #{'&} :form ::binding-form)))))
72+
73+
(s/def ::args+body
74+
(s/cat :args ::arg-list
75+
:prepost (s/? map?)
76+
:body (s/* any?)))
77+
78+
(def defn-args
79+
(s/cat :name simple-symbol?
80+
:docstring (s/? string?)
81+
:meta (s/? map?)
82+
:bs (s/alt :arity-1 ::args+body
83+
:arity-n (s/cat :bodies (s/+ (s/spec ::args+body))
84+
:attr (s/? map?)))))
85+
86+
(s/fdef clojure.core/defn
87+
:args defn-args
88+
:ret any?)
89+
90+
(s/fdef clojure.core/defn-
91+
:args defn-args
92+
:ret any?)
93+
94+
(s/fdef clojure.core/fn
95+
:args (s/cat :name (s/? simple-symbol?)
96+
:bs (s/alt :arity-1 ::args+body
97+
:arity-n (s/+ (s/spec ::args+body))))
98+
:ret any?)

test/clojure/test_clojure/def.clj

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@
1414
(testing "multiarity syntax invalid parameter declaration"
1515
(is (fails-with-cause?
1616
IllegalArgumentException
17-
#"Parameter declaration \"arg1\" should be a vector"
17+
#"Call to clojure.core/defn did not conform to spec"
1818
(eval-in-temp-ns (defn foo (arg1 arg2))))))
1919

2020
(testing "multiarity syntax invalid signature"
2121
(is (fails-with-cause?
2222
IllegalArgumentException
23-
#"Invalid signature \"\[a b\]\" should be a list"
23+
#"Call to clojure.core/defn did not conform to spec"
2424
(eval-in-temp-ns (defn foo
2525
([a] 1)
2626
[a b])))))
2727

2828
(testing "assume single arity syntax"
2929
(is (fails-with-cause?
3030
IllegalArgumentException
31-
#"Parameter declaration \"a\" should be a vector"
31+
#"Call to clojure.core/defn did not conform to spec"
3232
(eval-in-temp-ns (defn foo a)))))
3333

3434
(testing "bad name"
3535
(is (fails-with-cause?
3636
IllegalArgumentException
37-
#"First argument to defn must be a symbol"
37+
#"Call to clojure.core/defn did not conform to spec"
3838
(eval-in-temp-ns (defn "bad docstring" testname [arg1 arg2])))))
3939

4040
(testing "missing parameter/signature"
4141
(is (fails-with-cause?
4242
IllegalArgumentException
43-
#"Parameter declaration missing"
43+
#"Call to clojure.core/defn did not conform to spec"
4444
(eval-in-temp-ns (defn testname)))))
4545

4646
(testing "allow trailing map"
@@ -49,7 +49,7 @@
4949
(testing "don't allow interleaved map"
5050
(is (fails-with-cause?
5151
IllegalArgumentException
52-
#"Invalid signature \"\{:a :b\}\" should be a list"
52+
#"Call to clojure.core/defn did not conform to spec"
5353
(eval-in-temp-ns (defn a "asdf" ([a] 1) {:a :b} ([] 1)))))))
5454

5555
(deftest non-dynamic-warnings

test/clojure/test_clojure/fn.clj

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,42 @@
1414
(deftest fn-error-checking
1515
(testing "bad arglist"
1616
(is (fails-with-cause? java.lang.IllegalArgumentException
17-
#"Parameter declaration a should be a vector"
17+
#"Call to clojure.core/fn did not conform to spec"
1818
(eval '(fn "a" a)))))
1919

2020
(testing "treat first param as args"
2121
(is (fails-with-cause? java.lang.IllegalArgumentException
22-
#"Parameter declaration a should be a vector"
22+
#"Call to clojure.core/fn did not conform to spec"
2323
(eval '(fn "a" [])))))
2424

2525
(testing "looks like listy signature, but malformed declaration"
2626
(is (fails-with-cause? java.lang.IllegalArgumentException
27-
#"Parameter declaration 1 should be a vector"
27+
#"Call to clojure.core/fn did not conform to spec"
2828
(eval '(fn (1))))))
2929

3030
(testing "checks each signature"
3131
(is (fails-with-cause? java.lang.IllegalArgumentException
32-
#"Parameter declaration a should be a vector"
32+
#"Call to clojure.core/fn did not conform to spec"
3333
(eval '(fn
3434
([a] 1)
3535
("a" 2))))))
3636

3737
(testing "correct name but invalid args"
3838
(is (fails-with-cause? java.lang.IllegalArgumentException
39-
#"Parameter declaration a should be a vector"
39+
#"Call to clojure.core/fn did not conform to spec"
4040
(eval '(fn a "a")))))
4141

4242
(testing "first sig looks multiarity, rest of sigs should be lists"
4343
(is (fails-with-cause? java.lang.IllegalArgumentException
44-
#"Invalid signature \[a b\] should be a list"
44+
#"Call to clojure.core/fn did not conform to spec"
4545
(eval '(fn a
4646
([a] 1)
4747
[a b])))))
4848

4949
(testing "missing parameter declaration"
5050
(is (fails-with-cause? java.lang.IllegalArgumentException
51-
#"Parameter declaration missing"
51+
#"Call to clojure.core/fn did not conform to spec"
5252
(eval '(fn a))))
5353
(is (fails-with-cause? java.lang.IllegalArgumentException
54-
#"Parameter declaration missing"
54+
#"Call to clojure.core/fn did not conform to spec"
5555
(eval '(fn))))))

0 commit comments

Comments
 (0)