Permalink
Browse files

experimenting with quickcheck style tests

  • Loading branch information...
1 parent ab8b8d2 commit 869dde8912bce55e1d5f67ed1507ef87375084cc @stuarthalloway committed Feb 5, 2010
Showing with 63 additions and 1 deletion.
  1. +51 −0 src/circumspec/for_all.clj
  2. +12 −1 test/circumspec/utils_test.clj
View
@@ -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)))
+
@@ -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]]

0 comments on commit 869dde8

Please sign in to comment.