Skip to content

Instantly share code, notes, and snippets.

@psyho
Last active October 15, 2015 07:46
Show Gist options
  • Save psyho/2a09dd7d2e8958fedd00 to your computer and use it in GitHub Desktop.
Save psyho/2a09dd7d2e8958fedd00 to your computer and use it in GitHub Desktop.
Property-based testing dojo
(ns dojo.core
(:require [clojure.test.check :as tc]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop]
[clojure.test :refer :all]))
(def balls identity)
(defn add-ball [rack ball] (conj rack ball))
(def empty-rack [])
(defmacro defprop [name bindings body]
`(tc/quick-check 1000 (prop/for-all ~bindings ~body)))
(def gen-ball (gen/choose 0 59))
(defprop "adding to empty rack increases size by one"
[ball gen-ball]
(= (count (balls (add-ball empty-rack ball))) 1))
(defprop "adding to empty rack contains the added element"
[ball gen-ball]
(= [ball] (balls (add-ball empty-rack ball))))
(def gen-rack (gen/fmap (fn [balls] (reduce add-ball empty-rack (set balls))) (gen/list gen-ball)))
(gen/sample gen-rack)
(defprop "adding-to-rack-contains-one-more-element"
[rack gen-rack
ball gen-ball]
(let [new-rack (add-ball rack ball)]
(= (count (balls new-rack))
(inc (count (balls rack))))))
; this is where we finished
; (defprop "adding element give orderd rack"
#_(gen/sample (gen/such-that #(and (= 2 (count %)) (apply not= %))
(gen/list (gen/elements [:adam :marcin :damian])) 100)
1)
(deftest adding-element-to-empty-rack
(is (= (balls (add-ball empty-rack 0)) [0])))
(defproject dojo "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/test.check "0.8.2"]]
:main ^:skip-aot dojo.core
:target-path "target/%s"
:plugins [[refactor-nrepl "1.1.0"]
[cider/cider-nrepl "0.9.1"]]
:profiles {:uberjar {:aot :all}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment