diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/test_clojure/genclass.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,65 @@
     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 +(ns ^{:doc "Tests for clojure.core/gen-class"
    1.13 +      :author "Stuart Halloway, Daniel Solano Gómez"}
    1.14 +  clojure.test-clojure.genclass
    1.15 +  (:use clojure.test clojure.test-clojure.helpers)
    1.16 +  (:import [clojure.test_clojure.genclass.examples ExampleClass
    1.17 +                                                   ExampleAnnotationClass]
    1.18 +           [java.lang.annotation ElementType
    1.19 +                                 Retention
    1.20 +                                 RetentionPolicy
    1.21 +                                 Target]))
    1.22 +
    1.23 +(deftest arg-support
    1.24 +  (let [example (ExampleClass.)
    1.25 +        o (Object.)]
    1.26 +    (is (= "foo with o, o" (.foo example o o)))
    1.27 +    (is (= "foo with o, i" (.foo example o (int 1))))
    1.28 +    (is (thrown? java.lang.UnsupportedOperationException (.foo example o)))))
    1.29 +
    1.30 +(deftest name-munging
    1.31 +  (testing "mapping from Java fields to Clojure vars"
    1.32 +    (is (= #'clojure.test-clojure.genclass.examples/-foo-Object-int
    1.33 +           (get-field ExampleClass 'foo_Object_int__var)))
    1.34 +    (is (= #'clojure.test-clojure.genclass.examples/-toString
    1.35 +           (get-field ExampleClass 'toString__var)))))
    1.36 +
    1.37 +(deftest test-annotations
    1.38 +  (let [annot-class ExampleAnnotationClass
    1.39 +        foo-method          (.getDeclaredMethod annot-class "foo" (into-array [String]))]
    1.40 +    (testing "Class annotations:"
    1.41 +      (is (= 2 (count (.getDeclaredAnnotations annot-class))))
    1.42 +      (testing "@Deprecated"
    1.43 +        (let [deprecated (.getAnnotation annot-class Deprecated)]
    1.44 +          (is deprecated)))
    1.45 +      (testing "@Target([])"
    1.46 +        (let [resource (.getAnnotation annot-class Target)]
    1.47 +          (is (= 0 (count (.value resource)))))))
    1.48 +    (testing "Method annotations:"
    1.49 +      (testing "@Deprecated void foo(String):"
    1.50 +        (is (= 1 (count (.getDeclaredAnnotations foo-method))))
    1.51 +        (is (.getAnnotation foo-method Deprecated))))
    1.52 +    (testing "Parameter annotations:"
    1.53 +      (let [param-annots (.getParameterAnnotations foo-method)]
    1.54 +        (is (= 1 (alength param-annots)))
    1.55 +        (let [first-param-annots (aget param-annots 0)]
    1.56 +          (is (= 2 (alength first-param-annots)))
    1.57 +          (testing "void foo(@Retention(…) String)"
    1.58 +            (let [retention (aget first-param-annots 0)]
    1.59 +              (is (instance? Retention retention))
    1.60 +              (= RetentionPolicy/SOURCE (.value retention))))
    1.61 +          (testing "void foo(@Target(…) String)"
    1.62 +            (let [target (aget first-param-annots 1)]
    1.63 +              (is (instance? Target target))
    1.64 +              (is (= [ElementType/TYPE ElementType/PARAMETER] (seq (.value target)))))))))))
    1.65 +
    1.66 +(deftest genclass-option-validation
    1.67 +  (is (fails-with-cause? IllegalArgumentException #"Not a valid method name: has-hyphen"
    1.68 +        (@#'clojure.core/validate-generate-class-options {:methods '[[fine [] void] [has-hyphen [] void]]}))))