diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/test_clojure/main.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,50 @@
     1.4 +;   Copyright (c) Rich Hickey. All rights reserved.
     1.5 +;   The use and distribution terms for this software are covered by the
     1.6 +;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.7 +;   which can be found in the file epl-v10.html at the root of this distribution.
     1.8 +;   By using this software in any fashion, you are agreeing to be bound by
     1.9 +;   the terms of this license.
    1.10 +;   You must not remove this notice, or any other, from this software.
    1.11 +
    1.12 +; Author: Stuart Halloway
    1.13 +
    1.14 +
    1.15 +(ns clojure.test-clojure.main
    1.16 +  (:use clojure.test)
    1.17 +  (:require [clojure.main :as main]))
    1.18 +
    1.19 +(deftest eval-opt
    1.20 +  (testing "evals and prints forms"
    1.21 +    (is (= "2\n4\n" (with-out-str (#'clojure.main/eval-opt "(+ 1 1) (+ 2 2)")))))
    1.22 +
    1.23 +  (testing "skips printing nils"
    1.24 +    (is (= ":a\n:c\n" (with-out-str (#'clojure.main/eval-opt ":a nil :c")))))
    1.25 +
    1.26 +  (testing "does not block access to *in* (#299)"
    1.27 +    (with-in-str "(+ 1 1)"
    1.28 +      (is (= "(+ 1 1)\n" (with-out-str (#'clojure.main/eval-opt "(read)")))))))
    1.29 +
    1.30 +(defmacro with-err-str
    1.31 +  "Evaluates exprs in a context in which *err* is bound to a fresh
    1.32 +  StringWriter.  Returns the string created by any nested printing
    1.33 +  calls."
    1.34 +  [& body]
    1.35 +  `(let [s# (new java.io.StringWriter)
    1.36 +         p# (new java.io.PrintWriter s#)]
    1.37 +     (binding [*err* p#]
    1.38 +       ~@body
    1.39 +       (str s#))))
    1.40 +
    1.41 +(defn run-repl-and-return-err
    1.42 +  "Run repl, swallowing stdout and returing stderr."
    1.43 +  [in-str]
    1.44 +  (with-err-str
    1.45 +    (with-out-str
    1.46 +      (with-in-str in-str
    1.47 +        (main/repl)))))
    1.48 +
    1.49 +(deftest repl-exception-safety
    1.50 +  (testing "catches and prints exception on bad equals"
    1.51 +    (is (re-matches #"java\.lang\.NullPointerException\r?\n"
    1.52 +           (run-repl-and-return-err
    1.53 +            "(proxy [Object] [] (equals [o] (.toString nil)))")))))