view src/clojure/test_clojure/genclass.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 (ns ^{:doc "Tests for clojure.core/gen-class"
10 :author "Stuart Halloway, Daniel Solano Gómez"}
11 clojure.test-clojure.genclass
12 (:use clojure.test clojure.test-clojure.helpers)
13 (:import [clojure.test_clojure.genclass.examples ExampleClass
14 ExampleAnnotationClass]
15 [java.lang.annotation ElementType
16 Retention
17 RetentionPolicy
18 Target]))
20 (deftest arg-support
21 (let [example (ExampleClass.)
22 o (Object.)]
23 (is (= "foo with o, o" (.foo example o o)))
24 (is (= "foo with o, i" (.foo example o (int 1))))
25 (is (thrown? java.lang.UnsupportedOperationException (.foo example o)))))
27 (deftest name-munging
28 (testing "mapping from Java fields to Clojure vars"
29 (is (= #'clojure.test-clojure.genclass.examples/-foo-Object-int
30 (get-field ExampleClass 'foo_Object_int__var)))
31 (is (= #'clojure.test-clojure.genclass.examples/-toString
32 (get-field ExampleClass 'toString__var)))))
34 (deftest test-annotations
35 (let [annot-class ExampleAnnotationClass
36 foo-method (.getDeclaredMethod annot-class "foo" (into-array [String]))]
37 (testing "Class annotations:"
38 (is (= 2 (count (.getDeclaredAnnotations annot-class))))
39 (testing "@Deprecated"
40 (let [deprecated (.getAnnotation annot-class Deprecated)]
41 (is deprecated)))
42 (testing "@Target([])"
43 (let [resource (.getAnnotation annot-class Target)]
44 (is (= 0 (count (.value resource)))))))
45 (testing "Method annotations:"
46 (testing "@Deprecated void foo(String):"
47 (is (= 1 (count (.getDeclaredAnnotations foo-method))))
48 (is (.getAnnotation foo-method Deprecated))))
49 (testing "Parameter annotations:"
50 (let [param-annots (.getParameterAnnotations foo-method)]
51 (is (= 1 (alength param-annots)))
52 (let [first-param-annots (aget param-annots 0)]
53 (is (= 2 (alength first-param-annots)))
54 (testing "void foo(@Retention(…) String)"
55 (let [retention (aget first-param-annots 0)]
56 (is (instance? Retention retention))
57 (= RetentionPolicy/SOURCE (.value retention))))
58 (testing "void foo(@Target(…) String)"
59 (let [target (aget first-param-annots 1)]
60 (is (instance? Target target))
61 (is (= [ElementType/TYPE ElementType/PARAMETER] (seq (.value target)))))))))))
63 (deftest genclass-option-validation
64 (is (fails-with-cause? IllegalArgumentException #"Not a valid method name: has-hyphen"
65 (@#'clojure.core/validate-generate-class-options {:methods '[[fine [] void] [has-hyphen [] void]]}))))