diff src/clojure/test_clojure/ns_libs.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/ns_libs.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,85 @@
     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 +; Authors: Frantisek Sodomka, Stuart Halloway
    1.13 +
    1.14 +(ns clojure.test-clojure.ns-libs
    1.15 +  (:use clojure.test))
    1.16 +
    1.17 +; http://clojure.org/namespaces
    1.18 +
    1.19 +; in-ns ns create-ns
    1.20 +; alias import intern refer
    1.21 +; all-ns find-ns
    1.22 +; ns-name ns-aliases ns-imports ns-interns ns-map ns-publics ns-refers
    1.23 +; resolve ns-resolve namespace
    1.24 +; ns-unalias ns-unmap remove-ns
    1.25 +
    1.26 +
    1.27 +; http://clojure.org/libs
    1.28 +
    1.29 +; require use
    1.30 +; loaded-libs
    1.31 +
    1.32 +(deftest test-require
    1.33 +         (is (thrown? Exception (require :foo)))
    1.34 +         (is (thrown? Exception (require))))
    1.35 +
    1.36 +(deftest test-use
    1.37 +         (is (thrown? Exception (use :foo)))
    1.38 +         (is (thrown? Exception (use))))
    1.39 +
    1.40 +(deftest reimporting-deftypes
    1.41 +  (let [inst1 (binding [*ns* *ns*]
    1.42 +                (eval '(do (ns exporter)
    1.43 +                           (defrecord ReimportMe [a])
    1.44 +                           (ns importer)
    1.45 +                           (import exporter.ReimportMe)
    1.46 +                           (ReimportMe. 1))))
    1.47 +        inst2 (binding [*ns* *ns*]
    1.48 +                (eval '(do (ns exporter)
    1.49 +                           (defrecord ReimportMe [a b])
    1.50 +                           (ns importer)
    1.51 +                           (import exporter.ReimportMe)
    1.52 +                           (ReimportMe. 1 2))))]
    1.53 +    (testing "you can reimport a changed class and see the changes"
    1.54 +      (is (= [:a] (keys inst1)))
    1.55 +      (is (= [:a :b] (keys inst2))))
    1.56 +    (testing "you cannot import same local name from a different namespace"
    1.57 +      (is (thrown? clojure.lang.Compiler$CompilerException
    1.58 +                  #"ReimportMe already refers to: class exporter.ReimportMe in namespace: importer"
    1.59 +                  (binding [*ns* *ns*]
    1.60 +                    (eval '(do (ns exporter-2)
    1.61 +                               (defrecord ReimportMe [a b])
    1.62 +                               (ns importer)
    1.63 +                               (import exporter-2.ReimportMe)
    1.64 +                               (ReimportMe. 1 2)))))))))
    1.65 +
    1.66 +(deftest naming-types
    1.67 +  (testing "you cannot use a name already referred from another namespace"
    1.68 +    (is (thrown? IllegalStateException
    1.69 +                 #"String already refers to: class java.lang.String"
    1.70 +                 (definterface String)))
    1.71 +    (is (thrown? IllegalStateException
    1.72 +                 #"StringBuffer already refers to: class java.lang.StringBuffer"
    1.73 +                 (deftype StringBuffer [])))
    1.74 +    (is (thrown? IllegalStateException
    1.75 +                 #"Integer already refers to: class java.lang.Integer"
    1.76 +                 (defrecord Integer [])))))
    1.77 +
    1.78 +(deftest refer-error-messages
    1.79 +  (let [temp-ns (gensym)]
    1.80 +    (binding [*ns* *ns*]
    1.81 +      (in-ns temp-ns)
    1.82 +      (eval '(def ^{:private true} hidden-var)))
    1.83 +    (testing "referring to something that does not exist"
    1.84 +      (is (thrown-with-msg? IllegalAccessError #"nonexistent-var does not exist"
    1.85 +            (refer temp-ns :only '(nonexistent-var)))))
    1.86 +    (testing "referring to something non-public"
    1.87 +      (is (thrown-with-msg? IllegalAccessError #"hidden-var is not public"
    1.88 +            (refer temp-ns :only '(hidden-var)))))))