diff --git a/README.md b/README.md index 4f06730..f0cde6c 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,40 @@ # testgen -A Clojure library designed to ... well, that part is up to you. +A Clojure library designed to generate charaterisation tests. ## Usage -FIXME +At this stage, try + + (testgen ) + +It will attempt to generate as Clojure source a set of clojure.test test definitions +for the code passed. For example: + + user=> (pprint (testgen '(defn testgen [fndef] + #_=> (cond (= (first fndef) 'defn) + #_=> (let [name (first (rest fndef))] + #_=> (list 'deftest (symbol (str "test-" name)) + #_=> (map #(write-test name %) (find-interesting-args fndef)))))) + #_=> ) + #_=> ) + (deftest + test-testgen + ((is (= (testgen nil) nil)) + (is (= (testgen ()) nil)) + (is (thrown? java.lang.IllegalArgumentException (testgen true))) + (is (= (testgen "test") nil)) + (is (thrown? java.lang.IllegalArgumentException (testgen :test))) + (is (thrown? java.lang.IllegalArgumentException (testgen 0))) + (is + (thrown? + java.lang.IllegalArgumentException + (testgen Integer/MAX_VALUE))) + (is (thrown? java.lang.IllegalArgumentException (testgen 1.0E-4))) + (is (thrown? java.lang.IllegalArgumentException (testgen -1.0E-4))) + (is (= (testgen "test-") nil)))) + +Note, however, that it only works if the function for which tests are being generated already exists in the environment. ## License diff --git a/src/testgen/core.clj b/src/testgen/core.clj index f7f8138..aec04b6 100644 --- a/src/testgen/core.clj +++ b/src/testgen/core.clj @@ -5,6 +5,7 @@ (list 'is (list '= (list fnname arg) (eval (list fnname arg)))) (catch Exception e (list 'is (list 'thrown? (.getClass e) (list fnname arg)))))) + (defn constant? [arg] (not (or (symbol? arg) @@ -14,11 +15,11 @@ (def generic-args '(nil () true "test" :test 0 Integer/MAX_VALUE 0.0001 -0.0001)) -(defn find-interesting-args [sexpr] - "Find things in sexpr which would be interesting if passed as arguments to it" - (filter constant? (flatten sexpr))) +(defn constants [form] + "return a list of all elements in this form which are constants" + (filter constant? (flatten form))) -(defn find-more-interesting-args [sexpr] +(defn find-interesting-args [sexpr] "Find things in sexpr which would be even more interesting if passed as arguments to it" (concat generic-args (flatten @@ -26,11 +27,15 @@ #(cond (number? %) (list % (inc %) (dec %)) true %) - (find-interesting-args sexpr))))) + (constants sexpr))))) (defn testgen [fndef] (cond (= (first fndef) 'defn) (let [name (first (rest fndef))] (list 'deftest (symbol (str "test-" name)) - (map #(write-test name %) (find-more-interesting-args fndef)))))) + (map #(write-test name %) (find-interesting-args fndef)))))) + +;; (defn gen-tests [fnname form] +;; (map (partial write-test fnname) (constants form))) +