Skip to content

Instantly share code, notes, and snippets.

@quii
Last active April 14, 2023 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quii/82c5aa1472d9e7c730aa1b6353d47c2e to your computer and use it in GitHub Desktop.
Save quii/82c5aa1472d9e7c730aa1b6353d47c2e to your computer and use it in GitHub Desktop.
Clojure contracts
(ns tdd-clojure.contract-test
(:require [clojure.string :as str]
[clojure.test :refer :all]))
(defn test-greet-contract [greet-fn]
(testing "greet function should return a greeting with the given name"
(let [name "Chris"]
(is (= (greet-fn name) (str "Hello, " name))))))
;; Example implementation of the greet function
(defn greet1 [name]
(str "Hello, " name))
;; Another way to do a greet function
(defn greet2 [name]
(str/join " " ["Hello," name]))
;; Greet using fmt
(defn greet3 [name]
(format "Hello, %s" name))
;; Greet using replace (dumb but fun)
(defn greet4 [name]
(str/replace "Hello, WAT" #"WAT" name))
;; Test the example implementation using the contract test
(test-greet-contract greet1)
(test-greet-contract greet2)
(test-greet-contract greet3)
(test-greet-contract greet4)
@quii
Copy link
Author

quii commented Apr 10, 2023

(ns tdd-clojure.contract-test
  (:require [clojure.string :as str]
            [clojure.test :refer :all]))

;; Example implementation of the greet function
(defn greet1 [name]
  (str "Hello, " name))

;; Another way to do a greet function
(defn greet2 [name]
  (str/join " " ["Hello," name]))

;; Greet using fmt
(defn greet3 [name]
  (format "Hello, %s" name))

;; Greet using replace (dumb but fun)
(defn greet4 [name]
  (str/replace "Hello, WAT" #"WAT" name))

(defn greet-contract [greet-fn]
  (let [name "Chris"]
    (is (= (greet-fn name) (str "Hello, " name)))))

(deftest greet1-test "Test greet1" (greet-contract greet1))
(deftest greet2-test "Test greet2" (greet-contract greet2))
(deftest greet3-test "Test greet3" (greet-contract greet3))
(deftest greet4-test "Test greet3" (greet-contract greet4))

This one seems more IDE-friendly, Intellij now understands them to be tests (presumably it just looks for deftest), not sure if it's "better" though

@quii
Copy link
Author

quii commented Apr 10, 2023

Plot thickens

(ns tdd-clojure.contract-test
  (:require [clojure.string :as str]
            [clojure.test :refer :all]))

;; Example implementation of the greet function
(defn greet1
  ([] "Hello, World")
  ([name] (str "Hello, " name)))

;; Another way to do a greet function
(defn greet2 [name]
  (str/join " " ["Hello," name]))

;; Greet using fmt
(defn greet3 [name]
  (format "Hello, %s" name))

;; Greet using replace (dumb but fun)
(defn greet4 [name]
  (str/replace "Hello, WAT" #"WAT" name))

(defn greet-contract [greet-fn]
  (let [name "Chris"]
    (is (= (greet-fn name) (str "Hello, " name)))))

(defn greet-contract-multi-arity [greet-fn]
  (let [name "Chris"]
    (is (= (greet-fn name) (str "Hello, " name)))
    (is (= (greet-fn) (str "Hello, World")))))

(deftest greet1-test "Test greet1"
                     (greet-contract greet1) (greet-contract-multi-arity greet1))
(deftest greet2-test "Test greet2" (greet-contract greet2))
(deftest greet3-test "Test greet3" (greet-contract greet3))
(deftest greet4-test "Test greet3" (greet-contract greet4))

@theronic
Copy link

theronic commented Apr 11, 2023

Hi @quii :) Showing use of docstrings (the first arg after function name can be multiline docstring), testing context helper and clojure.test/are which is like a templated version of is:

(ns tdd-clojure.contract-test
  (:require [clojure.test :refer :all]
            [clojure.spec.alpha :as s]
            [clojure.string :as string]))

(defn greet1
  "Example implementation of the greet function"
  [name]
  (str "Hello, " name))

(defn greet2
  "Another way to do a greet function"
  [name]
  (string/join " " ["Hello," name]))

(defn greet3
  "Greet using fmt"
  [name]
  (format "Hello, %s" name))

(defn greet4
  "Greet using replace (dumb but fun)"
  [name]
  (string/replace "Hello, WAT" #"WAT" name))

(deftest greeting-tests
  (testing "greeting functions"
    (are [greet-fn] (= "Hello, Chris" (greet-fn "Chris"))
      greet1
      greet2
      greet3
      greet4)))

(comment
  (run-tests *ns*))

are is rarely used and there are few enough cases that I would probably just do this because it's explicit:

(deftest greeting-tests
  (let [in       "Chris"
        expected "Hello, Chris"]
    (is (= expected (greet1 in)))
    (is (= expected (greet2 in)))
    (is (= expected (greet3 in)))
    (is (= expected (greet4 in)))))

Also note existence of :pre and :post conditions in a map right after function arguments: https://clojure.org/reference/special_forms#_fn_name_param_condition_map_expr_2

(I typically require clojure.string :as string instead of :as str to distinguish from built-in str function, even though str/join works fine.)

@quii
Copy link
Author

quii commented Apr 11, 2023

@theronic Thanks so much! Much needed experience here

@quii
Copy link
Author

quii commented Apr 11, 2023

@theronic I guess the point of the "contract" being a separate thing that lives outside of deftest is that other implementations of it could import the contract to verify themselves. A common use-case for contracts is you may want to have an in-memory store say, and a Postgres one, and you want to verify they both behave how you want.

@theronic
Copy link

Use RCF: https://github.com/hyperfiddle/rcf

RCF is async-friendly hotness with := syntax, e.g. (greet-fn "Chris") := "Hello, Chris". Smarter IDE will hopefully also show test result next to the expression.

Re: contracts, so...interfaces? :) Hmm, I would rather test against a Clojure protocol then (interfaces). I guess tests are usually not easy to generalize once written? But then might as well wrap impl. in interface and update tests?

Also look at Clojure Spec, esp. for generative testing.

@quii
Copy link
Author

quii commented Apr 14, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment