/
compilation.clj
141 lines (111 loc) · 6.01 KB
/
compilation.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
; Author: Frantisek Sodomka
(ns clojure.test-clojure.compilation
(:use clojure.test
[clojure.test-helper :only (should-not-reflect should-print-err-message)]))
; http://clojure.org/compilation
; compile
; gen-class, gen-interface
(deftest test-compiler-metadata
(let [m (meta #'when)]
(are [x y] (= x y)
(list? (:arglists m)) true
(> (count (:arglists m)) 0) true
(string? (:doc m)) true
(> (.Length (:doc m)) 0) true ;;; .length
(string? (:file m)) true
(> (.Length (:file m)) 0) true ;;; .length
;(integer? (:start-line (:source-span m))) true ;;; (integer? (:line m)) true
;(> (:start-line (:source-span m)) 0) true ;;; (> (:line m) 0) true
(:macro m) true
(:name m) 'when )))
;;;(deftest test-embedded-constants
;;; (testing "Embedded constants"
;;; (is (eval `(= Boolean/TYPE ~Boolean/TYPE)))
;;; (is (eval `(= Byte/TYPE ~Byte/TYPE)))
;;; (is (eval `(= Character/TYPE ~Character/TYPE)))
;;; (is (eval `(= Double/TYPE ~Double/TYPE)))
;;; (is (eval `(= Float/TYPE ~Float/TYPE)))
;;; (is (eval `(= Integer/TYPE ~Integer/TYPE)))
;;; (is (eval `(= Long/TYPE ~Long/TYPE)))
;;; (is (eval `(= Short/TYPE ~Short/TYPE)))))
(deftest test-compiler-resolution
(testing "resolve nonexistent class create should return nil (assembla #262)"
(is (nil? (resolve 'NonExistentClass.)))))
(deftest test-no-recur-across-try
(testing "don't recur to function from inside try"
(is (thrown? Exception (eval '(fn [x] (try (recur 1)))))))
(testing "don't recur to loop from inside try"
(is (thrown? Exception (eval '(loop [x 3] (try (recur 1)))))))
(testing "don't get confused about what the recur is targeting"
(is (thrown? Exception (eval '(loop [x 3] (try (fn [x]) (recur 1)))))))
(testing "don't allow recur accross binding"
(is (thrown? Exception (eval '(fn [x] (binding [+ *] (recur 1)))))))
(testing "allow loop/recur inside try"
(is (try
(eval '(try (loop [x 3] (if (zero? x) x (recur (dec x))))))
(catch Exception _))))
(testing "allow fn/recur inside try"
(is (try
(eval '(try
((fn [x]
(if (zero? x)
x
(recur (dec x))))
3)))
(catch Exception _)))))
;; disabled until build box can call java from mvn
#_(deftest test-numeric-dispatch
(is (= "(int, int)" (TestDispatch/someMethod (int 1) (int 1))))
(is (= "(int, long)" (TestDispatch/someMethod (int 1) (long 1))))
(is (= "(long, long)" (TestDispatch/someMethod (long 1) (long 1)))))
(deftest test-CLJ-671-regression
(testing "that the presence of hints does not cause the compiler to infinitely loop"
(letfn [(gcd [x y]
(loop [x (long x) y (long y)]
(if (== y 0)
x
(recur y ^Int64 (rem x y)))))] ;;; ^Long
(is (= 4 (gcd 8 100))))))
;; ensure proper use of hints / type decls
(defn hinted
(^String [])
(^Exception [a]) ;;; ^Integer
(^System.Collections.IList [a & args])) ;;; ^java.util.List
;; fn names need to be fully-qualified because should-not-reflect evals its arg in a throwaway namespace
(deftest recognize-hinted-arg-vector
(should-not-reflect #(.Substring (clojure.test-clojure.compilation/hinted) 0)) ;;; .substring
(should-not-reflect #(.Data (clojure.test-clojure.compilation/hinted "arg"))) ;;; .floatValue
(should-not-reflect #(.Count (clojure.test-clojure.compilation/hinted :many :rest :args :here)))) ;;; .size
(defn ^String hinting-conflict ^Exception []) ;;; ^Integer
(deftest calls-use-arg-vector-hint
(should-not-reflect #(.Data (clojure.test-clojure.compilation/hinting-conflict))) ;;; .floatValue
(should-print-err-message #"(?s)Reflection warning.*"
#(.Substring (clojure.test-clojure.compilation/hinting-conflict) 0))) ;;; .substring
(deftest deref-uses-var-tag
(should-not-reflect #(.Substring clojure.test-clojure.compilation/hinting-conflict 0)) ;;; .substring
(should-print-err-message #"(?s)Reflection warning.*"
#(.Data clojure.test-clojure.compilation/hinting-conflict))) ;;; .floatValue
(defn ^String legacy-hinting [])
(deftest legacy-call-hint
(should-not-reflect #(.Substring (clojure.test-clojure.compilation/legacy-hinting) 0))) ;;; .substring
(defprotocol HintedProtocol
(hintedp ^String [a]
^Exception [a b])) ;;; ^Integer
(deftest hinted-protocol-arg-vector
(should-not-reflect #(.Substring (clojure.test-clojure.compilation/hintedp "") 0)) ;;; .substring
(should-not-reflect #(.Data (clojure.test-clojure.compilation/hintedp :a :b)))) ;;; .floatValue
(defn primfn
(^long [])
(^double [a]))
(deftest primitive-return-decl
(should-not-reflect #(loop [k 5] (recur (clojure.test-clojure.compilation/primfn))))
(should-not-reflect #(loop [k 5.0] (recur (clojure.test-clojure.compilation/primfn 0))))
(should-print-err-message #"(?s).*k is not matching primitive.*"
#(loop [k (clojure.test-clojure.compilation/primfn)] (recur :foo))))