diff --git a/src/milkwood_clj/core.clj b/src/milkwood_clj/core.clj index 0138fbd..9b538f6 100644 --- a/src/milkwood_clj/core.clj +++ b/src/milkwood_clj/core.clj @@ -24,14 +24,15 @@ See http://codekata.pragprog.com/2007/01/kata_fourteen_t.html" "The length of the sequences to analyse it into (integer)" :parse-fn #(Integer. %) :default 2]) - file (arguments :file)] + file (arguments :file) + output (arguments :output)] (cond (= file nil) (print banner) (arguments :help) (print banner) true (synthesise/write-output (synthesise/compose-nonsense (analyse/analyse-file file (arguments :tuple-length)) - (arguments :output-length)))) + (arguments :output-length)) output)) (flush))) diff --git a/src/milkwood_clj/synthesise.clj b/src/milkwood_clj/synthesise.clj index 9aa8ea8..68886db 100644 --- a/src/milkwood_clj/synthesise.clj +++ b/src/milkwood_clj/synthesise.clj @@ -159,7 +159,14 @@ TODO: does not yet work. Should take an optional second argument, the file to write to if any (default to standard out). - output: a sequence of tokens to write." - [output] - (dorun (map write-token (top-and-tail output))) - (print "\n\n")) + output: a sequence of tokens to write; + destination: if not null, the name of the file to which to write it." + ([output destination] + (cond + destination + (let [text (with-out-str (write-output output))] + (spit destination text)) + true (write-output output))) + ([output] + (dorun (map write-token (top-and-tail output))) + (print "\n\n"))) diff --git a/test/milkwood_clj/synthesise_test.clj b/test/milkwood_clj/synthesise_test.clj index 71b161a..4d0891e 100644 --- a/test/milkwood_clj/synthesise_test.clj +++ b/test/milkwood_clj/synthesise_test.clj @@ -7,3 +7,9 @@ (is (= (top-and-tail '("a" "b" "c" "?" "d" "e" "f" "." "g" "h" "i" "!")) '("d" "e" "f" "." "g" "h" "i" "!"))) (is (= (top-and-tail '("a" "b" "c" "?" "d" "e" "f" "." "g" "h" "i")) '("d" "e" "f" "."))) )) + +(deftest write-output-test + (testing "Test output to file" + (is (= (do ;; (spit "test.out" "") + (write-output '("Test" "output" ".") "test.out") + (slurp "test.out"))) "Test output.")))