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))
true (list 'quote val)))
(defn generate-assertion [fnname arg]
"Generate an appropiate assertion for this argument passed to this function"
(defn generate-assertion [fnname & args]
"Generate an appropiate assertion for these arguments passed to this function"
(try
(let [val (eval (list fnname arg))]
(list 'is (list '= (list fnname arg) (maybe-quote val))))
(catch Exception e (list 'is (list 'thrown? (.getClass e) (list fnname arg))))))
(let [val (eval (cons fnname args))]
(list 'is (list '= (cons fnname args) (maybe-quote val))))
(catch Exception e (list 'is (list 'thrown? (.getClass e) (cons fnname args))))))
(defn constant? [arg]
(not (or
@ -59,7 +59,7 @@
(cond (> prefix-position -1) (.substring without-suffix (+ prefix-position 4))
true without-suffix)))
(defn test-filename [filename]
(defn testname-from-filename [filename]
"return an approximately-correct filename in which to save tests"
(let [prefix-position (.indexOf filename "src/")
prefix (cond (> prefix-position -1) (.substring filename 0 prefix-position)
@ -68,7 +68,7 @@
(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."
(let [fn (clean-filename filename)]
(symbol (.replace fn "/" "."))))
@ -109,12 +109,12 @@
pn (packagename-from-filename filename)
extra-vars (find-vars-in-file filename)]
(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 fn)
(refer pn)
(with-open [eddi (java.io.PushbackReader. (reader filename))
dickens (writer (test-filename filename))]
dickens (writer (testname-from-filename filename))]
(write-header dickens pn)
(while (.ready eddi)
(println "reading...")

View file

@ -47,35 +47,76 @@
test-generate-assertion
(testing
"generate-assertion"
(is (thrown? clojure.lang.ArityException (generate-assertion nil)))
(is (thrown? clojure.lang.ArityException (generate-assertion ())))
(is
(thrown?
clojure.lang.ArityException
(generate-assertion '(a :b "c"))))
(is (thrown? clojure.lang.ArityException (generate-assertion true)))
(=
(generate-assertion nil)
'(is (thrown? clojure.lang.Compiler$CompilerException (nil)))))
(is
(thrown? clojure.lang.ArityException (generate-assertion "test")))
(is (thrown? clojure.lang.ArityException (generate-assertion :test)))
(is (thrown? clojure.lang.ArityException (generate-assertion 0)))
(=
(generate-assertion ())
'(is (thrown? java.lang.ClassCastException (())))))
(is
(thrown?
clojure.lang.ArityException
(generate-assertion Integer/MAX_VALUE)))
(is (thrown? clojure.lang.ArityException (generate-assertion 22/7)))
(=
(generate-assertion '(a :b "c"))
'(is
(thrown? clojure.lang.Compiler$CompilerException ((a :b "c"))))))
(is
(thrown? clojure.lang.ArityException (generate-assertion 1.0E-4)))
(=
(generate-assertion true)
'(is (thrown? java.lang.ClassCastException (true)))))
(is
(thrown? clojure.lang.ArityException (generate-assertion -1.0E-4)))
(=
(generate-assertion "test")
'(is (thrown? java.lang.ClassCastException ("test")))))
(is
(thrown?
clojure.lang.ArityException
(generate-assertion generic-args)))
(=
(generate-assertion :test)
'(is (thrown? java.lang.IllegalArgumentException (:test)))))
(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 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
test-test-filename
test-testname-from-filename
(testing
"test-filename"
(is (thrown? java.lang.NullPointerException (test-filename nil)))
(is (thrown? java.lang.IllegalArgumentException (test-filename ())))
"testname-from-filename"
(is
(thrown?
java.lang.NullPointerException
(testname-from-filename nil)))
(is
(thrown?
java.lang.IllegalArgumentException
(test-filename '(a :b "c"))))
(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)))
(testname-from-filename ())))
(is
(thrown?
java.lang.IllegalArgumentException
(test-filename Integer/MAX_VALUE)))
(is
(thrown? java.lang.IllegalArgumentException (test-filename 22/7)))
(is
(thrown? java.lang.IllegalArgumentException (test-filename 1.0E-4)))
(testname-from-filename '(a :b "c"))))
(is
(thrown?
java.lang.IllegalArgumentException
(test-filename -1.0E-4)))
(testname-from-filename true)))
(is (= (testname-from-filename "test") '"test/test_test.clj"))
(is
(thrown?
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
(=
(test-filename
(testname-from-filename
"return an approximately-correct filename in which to save tests")
'"test/return an approximately-correct filename in which to save tests_test.clj"))
(is (= (test-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 (= (testname-from-filename "src/") '"test/_test.clj"))
(is
(thrown? java.lang.IllegalArgumentException (test-filename true)))
(is (= (test-filename "") '"test/_test.clj"))
(is (= (test-filename "test/") '"test/test/_test.clj"))
(is (= (test-filename "_test.clj") '"test/_test_test.clj"))))
(thrown?
java.lang.IllegalArgumentException
(testname-from-filename -1)))
(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
(=
(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
"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 ".")))))