How can I get Clojure :pre & :post to report their failing value?

后端 未结 4 1613
粉色の甜心
粉色の甜心 2020-12-05 06:42
(defn string-to-string [s1] 
  {:pre  [(string? s1)]
   :post [(string? %)]}
  s1)

I like :pre and :post conditions, they allow me to figure out wh

4条回答
  •  旧时难觅i
    2020-12-05 07:27

    You could wrap your predicate with the is macro from clojure.test

    (defn string-to-string [s1] 
      {:pre  [(is (string? s1))]
       :post [(is (string? %))]}
     s1)
    

    Then you get:

    (string-to-string 10)
    ;FAIL in clojure.lang.PersistentList$EmptyList@1 (scratch.clj:5)
    ;expected: (string? s1)
    ;actual: (not (string? 10))
    

提交回复
热议问题