midje

hrtmt brng 2025-03-20T19:40:51.885649Z

I am sometimes in the situation, that I want to check if a test result after applying a function equals something. For example like this.

(defn lower-case-is? [s]
  (fn [result]
    (= (clojure.string/lower-case result) s)))

(fact "ABC" => (lower-case-is? "abc")) 
It would be nice if this could be achieved without an extra function. Does anybody have an idea? I know, I can define an anonymous function in place. But that does not look very friendly. Something like this would be nice:
(fact "ABC" =clojure.string/lower-case=> "abc")

hrtmt brng 2025-03-23T06:33:13.854139Z

I implemented it myself (uses just with clojure.test).

(defn test-and-expect [[expr arrow & args]]
  (cond (= arrow '=>) [`(clojure.test/is (= ~expr ~(first args)))
                       (rest args)]
        (= arrow '=fn=>) [`(clojure.test/is (= (~(first args) ~expr) ~(second args)))
                          (nthrest args 2)]))
(defmacro protocol [& args]
  (loop [lines []
         more-args args]
    (if (empty? more-args) lines
        (let [[test still-more-args] (test-and-expect more-args)]
          (recur (conj lines test)
                 still-more-args)))))

(protocol (inc 4) => 5
          (dec 4) => 5
          (+ 2 1) =fn=> inc 4
          (+ 2 3) => 5)
; => [true false true true]