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