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