Solved the buffer-not-flushing bug. The reader was throwing an end-of-file

exception, which caused code to skip out of the try block. The writer
(dickens) wasn't in scope in the catch block, so could not have been
flushed there. Solution: move the try-catch into the loop.
This commit is contained in:
Simon Brooke 2014-04-07 12:50:56 +01:00
parent 7e173e99d0
commit 3361d55d81
2 changed files with 96 additions and 44 deletions

View file

@ -73,6 +73,7 @@
(symbol (.replace fn "/" "."))))
(defn find-vars-in-reader [eddie]
"Return a list of names of variables declared in the stream this reader reads"
(try
(let [sexpr (read eddie)]
(cond
@ -82,6 +83,7 @@
(catch RuntimeException eof)))
(defn find-vars-in-file [filename]
"Return a list of names of variables declared in the file at this path name"
(with-open [eddie (java.io.PushbackReader. (reader filename))]
(find-vars-in-reader eddie)))
@ -89,7 +91,6 @@
"Generate a suite of characterisation tests for the file indicated by this filename.
filename: the file path name of a file containing Clojure code to be tested."
(try
(let [fn (clean-filename filename)
pn (packagename-from-filename filename)
extra-vars (find-vars-in-file filename)]
@ -106,6 +107,7 @@
(.write dickens
";; auto-generated by testgen - see https://github.com/simon-brooke/testgen\n\n")
(while (.ready eddie)
(try
(println "reading...")
(let [form (read eddie)]
(cond (= (first form) 'defn)
@ -113,8 +115,10 @@
(println (first (rest form)) "...")
(pprint (testgen form extra-vars) dickens)
(.write dickens "\n\n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n")
))))
(.write dickens "\n\n;; end of file ;;\n\n")
(.flush dickens)))
)))
(catch Exception eof)))
(.write dickens "\n\n;; end of file ;;\n\n")
(.flush dickens))))

View file

@ -396,6 +396,11 @@
(is (= (find-vars-in-reader 1.0E-4) 'nil))
(is (= (find-vars-in-reader -1.0E-4) 'nil))
(is (= (find-vars-in-reader generic-args) 'nil))
(is
(=
(find-vars-in-reader
"Return a list of names of variables declared in the stream this reader reads")
'nil))
(is (= (find-vars-in-reader nil) 'nil))
(is (= (find-vars-in-reader true) 'nil))))
@ -447,7 +452,12 @@
(is
(thrown?
java.lang.IllegalArgumentException
(find-vars-in-file generic-args)))))
(find-vars-in-file generic-args)))
(is
(thrown?
java.io.FileNotFoundException
(find-vars-in-file
"Return a list of names of variables declared in the file at this path name")))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -456,45 +466,83 @@
test-generate-tests
(testing
"generate-tests"
(is (= (generate-tests nil) 'nil))
(is (= (generate-tests ()) 'nil))
(is (= (generate-tests '(a :b "c")) 'nil))
(is (= (generate-tests true) 'nil))
(is (= (generate-tests "test") 'nil))
(is (= (generate-tests :test) 'nil))
(is (= (generate-tests 0) 'nil))
(is (= (generate-tests Integer/MAX_VALUE) 'nil))
(is (= (generate-tests 22/7) 'nil))
(is (= (generate-tests 1.0E-4) 'nil))
(is (= (generate-tests -1.0E-4) 'nil))
(is (= (generate-tests generic-args) 'nil))
(is (thrown? java.lang.NullPointerException (generate-tests nil)))
(is (thrown? java.lang.IllegalArgumentException (generate-tests ())))
(is
(=
(thrown?
java.lang.IllegalArgumentException
(generate-tests '(a :b "c"))))
(is
(thrown? java.lang.IllegalArgumentException (generate-tests true)))
(is (thrown? java.io.FileNotFoundException (generate-tests "test")))
(is
(thrown? java.lang.IllegalArgumentException (generate-tests :test)))
(is (thrown? java.lang.IllegalArgumentException (generate-tests 0)))
(is
(thrown?
java.lang.IllegalArgumentException
(generate-tests Integer/MAX_VALUE)))
(is
(thrown? java.lang.IllegalArgumentException (generate-tests 22/7)))
(is
(thrown?
java.lang.IllegalArgumentException
(generate-tests 1.0E-4)))
(is
(thrown?
java.lang.IllegalArgumentException
(generate-tests -1.0E-4)))
(is
(thrown?
java.lang.IllegalArgumentException
(generate-tests generic-args)))
(is
(thrown?
java.io.FileNotFoundException
(generate-tests
"Generate a suite of characterisation tests for the file indicated by this filename.\n\n filename: the file path name of a file containing Clojure code to be tested.")
'nil))
(is (= (generate-tests "Read vars: ") 'nil))
(is (= (generate-tests "Writing to: ") 'nil))
(is (= (generate-tests "(ns ") 'nil))
(is (= (generate-tests "_test\n") 'nil))
"Generate a suite of characterisation tests for the file indicated by this filename.\n\n filename: the file path name of a file containing Clojure code to be tested.")))
(is
(=
(generate-tests "\t(:require [clojure.test :refer :all]\n\t[")
'nil))
(is (= (generate-tests " :refer :all]))\n\n") 'nil))
(thrown?
java.io.FileNotFoundException
(generate-tests "Read vars: ")))
(is
(=
(thrown?
java.io.FileNotFoundException
(generate-tests "Writing to: ")))
(is (thrown? java.io.FileNotFoundException (generate-tests "(ns ")))
(is
(thrown? java.io.FileNotFoundException (generate-tests "_test\n")))
(is
(thrown?
java.io.FileNotFoundException
(generate-tests "\t(:require [clojure.test :refer :all]\n\t[")))
(is
(thrown?
java.io.FileNotFoundException
(generate-tests " :refer :all]))\n\n")))
(is
(thrown?
java.io.FileNotFoundException
(generate-tests
";; auto-generated by testgen - see https://github.com/simon-brooke/testgen\n\n")
'nil))
(is (= (generate-tests "reading...") 'nil))
(is (= (generate-tests "...") 'nil))
";; auto-generated by testgen - see https://github.com/simon-brooke/testgen\n\n")))
(is
(=
(generate-tests "\n\n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n")
'nil))
(is (= (generate-tests "\n\n;; end of file ;;\n\n") 'nil))))
(thrown?
java.io.FileNotFoundException
(generate-tests "reading...")))
(is (thrown? java.io.FileNotFoundException (generate-tests "...")))
(is
(thrown?
java.io.FileNotFoundException
(generate-tests "\n\n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n")))
(is
(thrown?
java.io.FileNotFoundException
(generate-tests "\n\n;; end of file ;;\n\n")))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; end of file ;;