Right! Generate-assertion now takes varargs and would generate assertions

for varargs, I'm just not yet passing the right number or arguments to it.
That's the next step, and shouldn't be hard.
This commit is contained in:
simon 2014-04-07 21:15:42 +01:00
parent 6d3b5d76a3
commit 1e2c8852d4
2 changed files with 140 additions and 62 deletions

View file

@ -9,12 +9,12 @@
(symbol? val) (list 'symbol (str val)) (symbol? val) (list 'symbol (str val))
true (list 'quote val))) true (list 'quote val)))
(defn generate-assertion [fnname arg] (defn generate-assertion [fnname & args]
"Generate an appropiate assertion for this argument passed to this function" "Generate an appropiate assertion for these arguments passed to this function"
(try (try
(let [val (eval (list fnname arg))] (let [val (eval (cons fnname args))]
(list 'is (list '= (list fnname arg) (maybe-quote val)))) (list 'is (list '= (cons fnname args) (maybe-quote val))))
(catch Exception e (list 'is (list 'thrown? (.getClass e) (list fnname arg)))))) (catch Exception e (list 'is (list 'thrown? (.getClass e) (cons fnname args))))))
(defn constant? [arg] (defn constant? [arg]
(not (or (not (or
@ -59,7 +59,7 @@
(cond (> prefix-position -1) (.substring without-suffix (+ prefix-position 4)) (cond (> prefix-position -1) (.substring without-suffix (+ prefix-position 4))
true without-suffix))) true without-suffix)))
(defn test-filename [filename] (defn testname-from-filename [filename]
"return an approximately-correct filename in which to save tests" "return an approximately-correct filename in which to save tests"
(let [prefix-position (.indexOf filename "src/") (let [prefix-position (.indexOf filename "src/")
prefix (cond (> prefix-position -1) (.substring filename 0 prefix-position) prefix (cond (> prefix-position -1) (.substring filename 0 prefix-position)
@ -68,7 +68,7 @@
(defn packagename-from-filename [filename] (defn packagename-from-filename [filename]
"Return, as a symbol, the package name associated with this filename. There's "Return, as a symbol, an appropiate name for a test file associated with this filename. There's
probably a better way of doing this." probably a better way of doing this."
(let [fn (clean-filename filename)] (let [fn (clean-filename filename)]
(symbol (.replace fn "/" ".")))) (symbol (.replace fn "/" "."))))
@ -109,12 +109,12 @@
pn (packagename-from-filename filename) pn (packagename-from-filename filename)
extra-vars (find-vars-in-file filename)] extra-vars (find-vars-in-file filename)]
(println "Read vars: " extra-vars) (println "Read vars: " extra-vars)
(println "Writing to: " (test-filename filename)) (println "Writing to: " (testname-from-filename filename))
;; load the file so that any functions in it are usable ;; load the file so that any functions in it are usable
(load fn) (load fn)
(refer pn) (refer pn)
(with-open [eddi (java.io.PushbackReader. (reader filename)) (with-open [eddi (java.io.PushbackReader. (reader filename))
dickens (writer (test-filename filename))] dickens (writer (testname-from-filename filename))]
(write-header dickens pn) (write-header dickens pn)
(while (.ready eddi) (while (.ready eddi)
(println "reading...") (println "reading...")

View file

@ -47,35 +47,76 @@
test-generate-assertion test-generate-assertion
(testing (testing
"generate-assertion" "generate-assertion"
(is (thrown? clojure.lang.ArityException (generate-assertion nil)))
(is (thrown? clojure.lang.ArityException (generate-assertion ())))
(is (is
(thrown? (=
clojure.lang.ArityException (generate-assertion nil)
(generate-assertion '(a :b "c")))) '(is (thrown? clojure.lang.Compiler$CompilerException (nil)))))
(is (thrown? clojure.lang.ArityException (generate-assertion true)))
(is (is
(thrown? clojure.lang.ArityException (generate-assertion "test"))) (=
(is (thrown? clojure.lang.ArityException (generate-assertion :test))) (generate-assertion ())
(is (thrown? clojure.lang.ArityException (generate-assertion 0))) '(is (thrown? java.lang.ClassCastException (())))))
(is (is
(thrown? (=
clojure.lang.ArityException (generate-assertion '(a :b "c"))
(generate-assertion Integer/MAX_VALUE))) '(is
(is (thrown? clojure.lang.ArityException (generate-assertion 22/7))) (thrown? clojure.lang.Compiler$CompilerException ((a :b "c"))))))
(is (is
(thrown? clojure.lang.ArityException (generate-assertion 1.0E-4))) (=
(generate-assertion true)
'(is (thrown? java.lang.ClassCastException (true)))))
(is (is
(thrown? clojure.lang.ArityException (generate-assertion -1.0E-4))) (=
(generate-assertion "test")
'(is (thrown? java.lang.ClassCastException ("test")))))
(is (is
(thrown? (=
clojure.lang.ArityException (generate-assertion :test)
(generate-assertion generic-args))) '(is (thrown? java.lang.IllegalArgumentException (:test)))))
(is (is
(thrown? (=
clojure.lang.ArityException (generate-assertion 0)
'(is (thrown? java.lang.ClassCastException (0)))))
(is
(=
(generate-assertion Integer/MAX_VALUE)
'(is (thrown? java.lang.ClassCastException (2147483647)))))
(is
(=
(generate-assertion 22/7)
'(is (thrown? java.lang.ClassCastException (22/7)))))
(is
(=
(generate-assertion 1.0E-4)
'(is (thrown? java.lang.ClassCastException (1.0E-4)))))
(is
(=
(generate-assertion -1.0E-4)
'(is (thrown? java.lang.ClassCastException (-1.0E-4)))))
(is
(=
(generate-assertion generic-args)
'(is
(thrown?
clojure.lang.Compiler$CompilerException
((nil
()
'(a :b "c")
true
"test"
:test
0
Integer/MAX_VALUE
22/7
1.0E-4
-1.0E-4))))))
(is
(=
(generate-assertion (generate-assertion
"Generate an appropiate assertion for this argument passed to this function"))))) "Generate an appropiate assertion for these arguments passed to this function")
'(is
(thrown?
java.lang.ClassCastException
("Generate an appropiate assertion for these arguments passed to this function")))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -285,54 +326,91 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftest (deftest
test-test-filename test-testname-from-filename
(testing (testing
"test-filename" "testname-from-filename"
(is (thrown? java.lang.NullPointerException (test-filename nil))) (is
(is (thrown? java.lang.IllegalArgumentException (test-filename ()))) (thrown?
java.lang.NullPointerException
(testname-from-filename nil)))
(is (is
(thrown? (thrown?
java.lang.IllegalArgumentException java.lang.IllegalArgumentException
(test-filename '(a :b "c")))) (testname-from-filename ())))
(is
(thrown? java.lang.IllegalArgumentException (test-filename true)))
(is (= (test-filename "test") '"test/test_test.clj"))
(is
(thrown? java.lang.IllegalArgumentException (test-filename :test)))
(is (thrown? java.lang.IllegalArgumentException (test-filename 0)))
(is (is
(thrown? (thrown?
java.lang.IllegalArgumentException java.lang.IllegalArgumentException
(test-filename Integer/MAX_VALUE))) (testname-from-filename '(a :b "c"))))
(is
(thrown? java.lang.IllegalArgumentException (test-filename 22/7)))
(is
(thrown? java.lang.IllegalArgumentException (test-filename 1.0E-4)))
(is (is
(thrown? (thrown?
java.lang.IllegalArgumentException java.lang.IllegalArgumentException
(test-filename -1.0E-4))) (testname-from-filename true)))
(is (= (testname-from-filename "test") '"test/test_test.clj"))
(is (is
(thrown? (thrown?
java.lang.IllegalArgumentException java.lang.IllegalArgumentException
(test-filename generic-args))) (testname-from-filename :test)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename 0)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename Integer/MAX_VALUE)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename 22/7)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename 1.0E-4)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename -1.0E-4)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename generic-args)))
(is (is
(= (=
(test-filename (testname-from-filename
"return an approximately-correct filename in which to save tests") "return an approximately-correct filename in which to save tests")
'"test/return an approximately-correct filename in which to save tests_test.clj")) '"test/return an approximately-correct filename in which to save tests_test.clj"))
(is (= (test-filename "src/") '"test/_test.clj")) (is (= (testname-from-filename "src/") '"test/_test.clj"))
(is (thrown? java.lang.IllegalArgumentException (test-filename -1)))
(is (thrown? java.lang.IllegalArgumentException (test-filename 0)))
(is (thrown? java.lang.IllegalArgumentException (test-filename -2)))
(is (thrown? java.lang.IllegalArgumentException (test-filename 0)))
(is (thrown? java.lang.IllegalArgumentException (test-filename 1)))
(is (thrown? java.lang.IllegalArgumentException (test-filename -1)))
(is (is
(thrown? java.lang.IllegalArgumentException (test-filename true))) (thrown?
(is (= (test-filename "") '"test/_test.clj")) java.lang.IllegalArgumentException
(is (= (test-filename "test/") '"test/test/_test.clj")) (testname-from-filename -1)))
(is (= (test-filename "_test.clj") '"test/_test_test.clj")))) (is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename 0)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename -2)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename 0)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename 1)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename -1)))
(is
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename true)))
(is (= (testname-from-filename "") '"test/_test.clj"))
(is (= (testname-from-filename "test/") '"test/test/_test.clj"))
(is (= (testname-from-filename "_test.clj") '"test/_test_test.clj"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -389,9 +467,9 @@
(is (is
(= (=
(packagename-from-filename (packagename-from-filename
"Return, as a symbol, the package name associated with this filename. There's\n probably a better way of doing this.") "Return, as a symbol, an appropiate name for a test file associated with this filename. There's\n probably a better way of doing this.")
(symbol (symbol
"Return, as a symbol, the package name associated with this filename. There's\n probably a better way of doing this."))) "Return, as a symbol, an appropiate name for a test file associated with this filename. There's\n probably a better way of doing this.")))
(is (= (packagename-from-filename "/") (symbol "."))) (is (= (packagename-from-filename "/") (symbol ".")))
(is (= (packagename-from-filename ".") (symbol "."))))) (is (= (packagename-from-filename ".") (symbol ".")))))