annotate src/clojure/test_clojure/main.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 ; Copyright (c) Rich Hickey. All rights reserved.
rlm@10 2 ; The use and distribution terms for this software are covered by the
rlm@10 3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 4 ; which can be found in the file epl-v10.html at the root of this distribution.
rlm@10 5 ; By using this software in any fashion, you are agreeing to be bound by
rlm@10 6 ; the terms of this license.
rlm@10 7 ; You must not remove this notice, or any other, from this software.
rlm@10 8
rlm@10 9 ; Author: Stuart Halloway
rlm@10 10
rlm@10 11
rlm@10 12 (ns clojure.test-clojure.main
rlm@10 13 (:use clojure.test)
rlm@10 14 (:require [clojure.main :as main]))
rlm@10 15
rlm@10 16 (deftest eval-opt
rlm@10 17 (testing "evals and prints forms"
rlm@10 18 (is (= "2\n4\n" (with-out-str (#'clojure.main/eval-opt "(+ 1 1) (+ 2 2)")))))
rlm@10 19
rlm@10 20 (testing "skips printing nils"
rlm@10 21 (is (= ":a\n:c\n" (with-out-str (#'clojure.main/eval-opt ":a nil :c")))))
rlm@10 22
rlm@10 23 (testing "does not block access to *in* (#299)"
rlm@10 24 (with-in-str "(+ 1 1)"
rlm@10 25 (is (= "(+ 1 1)\n" (with-out-str (#'clojure.main/eval-opt "(read)")))))))
rlm@10 26
rlm@10 27 (defmacro with-err-str
rlm@10 28 "Evaluates exprs in a context in which *err* is bound to a fresh
rlm@10 29 StringWriter. Returns the string created by any nested printing
rlm@10 30 calls."
rlm@10 31 [& body]
rlm@10 32 `(let [s# (new java.io.StringWriter)
rlm@10 33 p# (new java.io.PrintWriter s#)]
rlm@10 34 (binding [*err* p#]
rlm@10 35 ~@body
rlm@10 36 (str s#))))
rlm@10 37
rlm@10 38 (defn run-repl-and-return-err
rlm@10 39 "Run repl, swallowing stdout and returing stderr."
rlm@10 40 [in-str]
rlm@10 41 (with-err-str
rlm@10 42 (with-out-str
rlm@10 43 (with-in-str in-str
rlm@10 44 (main/repl)))))
rlm@10 45
rlm@10 46 (deftest repl-exception-safety
rlm@10 47 (testing "catches and prints exception on bad equals"
rlm@10 48 (is (re-matches #"java\.lang\.NullPointerException\r?\n"
rlm@10 49 (run-repl-and-return-err
rlm@10 50 "(proxy [Object] [] (equals [o] (.toString nil)))")))))