From 3361d55d81713be423a9f2deafb52c0dcd1d26ef Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Mon, 7 Apr 2014 12:50:56 +0100 Subject: [PATCH] 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. --- src/testgen/core.clj | 26 +++++---- test/testgen/core_test.clj | 114 ++++++++++++++++++++++++++----------- 2 files changed, 96 insertions(+), 44 deletions(-) diff --git a/src/testgen/core.clj b/src/testgen/core.clj index d83a492..b6a094b 100644 --- a/src/testgen/core.clj +++ b/src/testgen/core.clj @@ -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,15 +107,18 @@ (.write dickens ";; auto-generated by testgen - see https://github.com/simon-brooke/testgen\n\n") (while (.ready eddie) - (println "reading...") - (let [form (read eddie)] - (cond (= (first form) 'defn) - (do - (println (first (rest form)) "...") - (pprint (testgen form extra-vars) dickens) - (.write dickens "\n\n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n") - )))) + (try + (println "reading...") + (let [form (read eddie)] + (cond (= (first form) 'defn) + (do + (println (first (rest form)) "...") + (pprint (testgen form extra-vars) dickens) + (.write dickens "\n\n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n") + ))) + (catch Exception eof))) (.write dickens "\n\n;; end of file ;;\n\n") - (.flush dickens))) - (catch Exception eof))) + (.flush dickens)))) + + diff --git a/test/testgen/core_test.clj b/test/testgen/core_test.clj index 4ae3879..9c2a3fc 100644 --- a/test/testgen/core_test.clj +++ b/test/testgen/core_test.clj @@ -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 ;; +