Skip to content

Commit

Permalink
experimenting with quickcheck style tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthalloway committed Feb 5, 2010
1 parent ab8b8d2 commit 869dde8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/circumspec/for_all.clj
@@ -0,0 +1,51 @@
(ns circumspec.for-all
(:use [clojure.contrib.seq-utils :only (rand-elt)]))

(def *size* 100)

(defmacro defgenerator [sym & forms]
`(def ~sym ~@forms))

(defn data-name-for-generator-coll
[generator-sym]
(symbol (str generator-sym "*")))

(defmacro generator-coll
[sym coll]
(let [data-name (data-name-for-generator-coll sym)]
`(do
(def ~data-name ~coll)
(defn ~sym [] (rand-elt ~data-name)))))

(defn choose-from
[& generators]
(fn [] ((rand-elt generators))))

(defn list-of
[& generators]
(let [gen (apply choose-from generators)]
(fn [] (take (rand-int *size*) (repeatedly gen)))))

(defn string-of
[& generators]
(let [gen (apply list-of generators)]
(fn [] (apply str (gen)))))

(defn symbol-of
[& generators]
(let [gen (apply string-of generators)]
(fn [] (symbol (gen)))))

(generator-coll famous-string [nil ""])
(generator-coll famous-whitespace " \t\n")
(generator-coll digits "1234567890")
(generator-coll ascii-upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
(generator-coll ascii-lower "abcdefghijklmnopqrstuvwxyz")
(defgenerator ascii-alpha (choose-from ascii-upper ascii-lower))

(defmacro for-all
[bindings & body]
`(dotimes [_# *size*]
(let ~bindings
~@body)))

13 changes: 12 additions & 1 deletion test/circumspec/utils_test.clj
@@ -1,7 +1,18 @@
(ns circumspec.utils-test
(:use circumspec circumspec.utils
(:use circumspec circumspec.utils circumspec.for-all
clojure.contrib.with-ns))

(testing dasherize
(let [whitespacey-string (string-of ascii-alpha famous-whitespace)]
(for-all [s (whitespacey-string)]
(should (not (re-find #" |\t|\n" (dasherize s)))))))

(testing "class-symbol always returns a boolean"
(let [ascii-symbol (symbol-of ascii-alpha)]
(for-all [s (ascii-symbol)]
(when-not (= s (symbol "")) ;; clojure bug? empty symbol barfs
(should (contains? #{true false} (class-symbol? s)))))))

(testing pop-optional-args
(for-these [output input] (should (= output (apply pop-optional-args input)))
[[1 2]] [[] [1 2]]
Expand Down

0 comments on commit 869dde8

Please sign in to comment.