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 ; Authors: Frantisek Sodomka, Stuart Halloway
|
rlm@10
|
10
|
rlm@10
|
11 (ns clojure.test-clojure.ns-libs
|
rlm@10
|
12 (:use clojure.test))
|
rlm@10
|
13
|
rlm@10
|
14 ; http://clojure.org/namespaces
|
rlm@10
|
15
|
rlm@10
|
16 ; in-ns ns create-ns
|
rlm@10
|
17 ; alias import intern refer
|
rlm@10
|
18 ; all-ns find-ns
|
rlm@10
|
19 ; ns-name ns-aliases ns-imports ns-interns ns-map ns-publics ns-refers
|
rlm@10
|
20 ; resolve ns-resolve namespace
|
rlm@10
|
21 ; ns-unalias ns-unmap remove-ns
|
rlm@10
|
22
|
rlm@10
|
23
|
rlm@10
|
24 ; http://clojure.org/libs
|
rlm@10
|
25
|
rlm@10
|
26 ; require use
|
rlm@10
|
27 ; loaded-libs
|
rlm@10
|
28
|
rlm@10
|
29 (deftest test-require
|
rlm@10
|
30 (is (thrown? Exception (require :foo)))
|
rlm@10
|
31 (is (thrown? Exception (require))))
|
rlm@10
|
32
|
rlm@10
|
33 (deftest test-use
|
rlm@10
|
34 (is (thrown? Exception (use :foo)))
|
rlm@10
|
35 (is (thrown? Exception (use))))
|
rlm@10
|
36
|
rlm@10
|
37 (deftest reimporting-deftypes
|
rlm@10
|
38 (let [inst1 (binding [*ns* *ns*]
|
rlm@10
|
39 (eval '(do (ns exporter)
|
rlm@10
|
40 (defrecord ReimportMe [a])
|
rlm@10
|
41 (ns importer)
|
rlm@10
|
42 (import exporter.ReimportMe)
|
rlm@10
|
43 (ReimportMe. 1))))
|
rlm@10
|
44 inst2 (binding [*ns* *ns*]
|
rlm@10
|
45 (eval '(do (ns exporter)
|
rlm@10
|
46 (defrecord ReimportMe [a b])
|
rlm@10
|
47 (ns importer)
|
rlm@10
|
48 (import exporter.ReimportMe)
|
rlm@10
|
49 (ReimportMe. 1 2))))]
|
rlm@10
|
50 (testing "you can reimport a changed class and see the changes"
|
rlm@10
|
51 (is (= [:a] (keys inst1)))
|
rlm@10
|
52 (is (= [:a :b] (keys inst2))))
|
rlm@10
|
53 (testing "you cannot import same local name from a different namespace"
|
rlm@10
|
54 (is (thrown? clojure.lang.Compiler$CompilerException
|
rlm@10
|
55 #"ReimportMe already refers to: class exporter.ReimportMe in namespace: importer"
|
rlm@10
|
56 (binding [*ns* *ns*]
|
rlm@10
|
57 (eval '(do (ns exporter-2)
|
rlm@10
|
58 (defrecord ReimportMe [a b])
|
rlm@10
|
59 (ns importer)
|
rlm@10
|
60 (import exporter-2.ReimportMe)
|
rlm@10
|
61 (ReimportMe. 1 2)))))))))
|
rlm@10
|
62
|
rlm@10
|
63 (deftest naming-types
|
rlm@10
|
64 (testing "you cannot use a name already referred from another namespace"
|
rlm@10
|
65 (is (thrown? IllegalStateException
|
rlm@10
|
66 #"String already refers to: class java.lang.String"
|
rlm@10
|
67 (definterface String)))
|
rlm@10
|
68 (is (thrown? IllegalStateException
|
rlm@10
|
69 #"StringBuffer already refers to: class java.lang.StringBuffer"
|
rlm@10
|
70 (deftype StringBuffer [])))
|
rlm@10
|
71 (is (thrown? IllegalStateException
|
rlm@10
|
72 #"Integer already refers to: class java.lang.Integer"
|
rlm@10
|
73 (defrecord Integer [])))))
|
rlm@10
|
74
|
rlm@10
|
75 (deftest refer-error-messages
|
rlm@10
|
76 (let [temp-ns (gensym)]
|
rlm@10
|
77 (binding [*ns* *ns*]
|
rlm@10
|
78 (in-ns temp-ns)
|
rlm@10
|
79 (eval '(def ^{:private true} hidden-var)))
|
rlm@10
|
80 (testing "referring to something that does not exist"
|
rlm@10
|
81 (is (thrown-with-msg? IllegalAccessError #"nonexistent-var does not exist"
|
rlm@10
|
82 (refer temp-ns :only '(nonexistent-var)))))
|
rlm@10
|
83 (testing "referring to something non-public"
|
rlm@10
|
84 (is (thrown-with-msg? IllegalAccessError #"hidden-var is not public"
|
rlm@10
|
85 (refer temp-ns :only '(hidden-var)))))))
|