Merge branch 'master' of github.com:simon-brooke/testgen

Conflicts:
	src/testgen/core.clj
This commit is contained in:
Simon Brooke 2014-04-02 22:42:29 +01:00
commit f80627763c
2 changed files with 43 additions and 8 deletions

View file

@ -1,10 +1,40 @@
# testgen # testgen
A Clojure library designed to ... well, that part is up to you. A Clojure library designed to generate charaterisation tests.
## Usage ## Usage
FIXME At this stage, try
(testgen <function-source>)
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 ## License

View file

@ -5,6 +5,7 @@
(list 'is (list '= (list fnname arg) (eval (list fnname arg)))) (list 'is (list '= (list fnname arg) (eval (list fnname arg))))
(catch Exception e (list 'is (list 'thrown? (.getClass e) (list fnname arg)))))) (catch Exception e (list 'is (list 'thrown? (.getClass e) (list fnname arg))))))
(defn constant? [arg] (defn constant? [arg]
(not (or (not (or
(symbol? arg) (symbol? arg)
@ -14,11 +15,11 @@
(def generic-args '(nil () true "test" :test 0 Integer/MAX_VALUE 0.0001 -0.0001)) (def generic-args '(nil () true "test" :test 0 Integer/MAX_VALUE 0.0001 -0.0001))
(defn find-interesting-args [sexpr] (defn constants [form]
"Find things in sexpr which would be interesting if passed as arguments to it" "return a list of all elements in this form which are constants"
(filter constant? (flatten sexpr))) (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" "Find things in sexpr which would be even more interesting if passed as arguments to it"
(concat generic-args (concat generic-args
(flatten (flatten
@ -26,11 +27,15 @@
#(cond #(cond
(number? %) (list % (inc %) (dec %)) (number? %) (list % (inc %) (dec %))
true %) true %)
(find-interesting-args sexpr))))) (constants sexpr)))))
(defn testgen [fndef] (defn testgen [fndef]
(cond (= (first fndef) 'defn) (cond (= (first fndef) 'defn)
(let [name (first (rest fndef))] (let [name (first (rest fndef))]
(list 'deftest (symbol (str "test-" name)) (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)))