Skip to content

Commit ade71c3

Browse files
committed
some tests
1 parent 675ac2c commit ade71c3

2 files changed

Lines changed: 211 additions & 3 deletions

File tree

src/test/clojure/clojure/tools/analyzer/jvm/core_test.clj

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
[clojure.tools.analyzer.passes.elide-meta :refer [elides elide-meta]]
77
[clojure.tools.analyzer.ast :refer [postwalk]]
88
[clojure.tools.reader :as r]
9-
[clojure.test :refer [deftest is]]))
9+
[clojure.test :refer [deftest is]])
10+
(:import (java.io File)))
1011

1112
(defprotocol p (f [_]))
1213
(defn f1 [^long x])
@@ -115,3 +116,77 @@
115116

116117
(deftest array_class
117118
(is (ana (r/read-string "(fn [^{:tag int/2} x] (instance? int/2 x))"))))
119+
120+
(deftest macroexpander-qualified-methods-test
121+
(is (= (list '. Integer (symbol "-MAX_VALUE"))
122+
(mexpand Integer/MAX_VALUE)))
123+
124+
(is (= 'String/1 (mexpand String/1)))
125+
126+
(is (= 'String/.length (mexpand String/.length)))
127+
(is (= 'Integer/.intValue (mexpand Integer/.intValue)))
128+
129+
(is (= 'String/new (mexpand String/new)))
130+
131+
(is (= 'String/valueOf (mexpand String/valueOf)))
132+
(is (= 'Integer/parseInt (mexpand Integer/parseInt)))
133+
134+
(let [expanded (mexpand (String/new "hello"))]
135+
(is (= 'new (first expanded)))
136+
(is (= java.lang.String (second expanded))))
137+
138+
(let [expanded (mexpand (String/.substring "hello" 1 3))]
139+
(is (= '. (first expanded)))
140+
(is (= '(do "hello") (second expanded)))
141+
(is (= String (:tag (meta (second expanded)))))
142+
(is (= 'substring (first (nth expanded 2)))))
143+
144+
(let [expanded (mexpand (String/.length "hello"))]
145+
(is (= '. (first expanded)))
146+
(is (= 'length (nth expanded 2))))
147+
148+
(let [expanded (mexpand (Integer/parseInt "2"))]
149+
(is (= '. (first expanded)))
150+
(is (= java.lang.Integer (second expanded)))))
151+
152+
(deftest analyzer-qualified-methods-test
153+
(let [a (ast1 File/.getName)]
154+
(is (= :method-value (:op a)))
155+
(is (= :instance (:kind a)))
156+
(is (= 'getName (:method a)))
157+
(is (= java.io.File (:class a))))
158+
159+
(let [a (ast1 String/valueOf)]
160+
(is (= :method-value (:op a)))
161+
(is (= :static (:kind a)))
162+
(is (= 'valueOf (:method a)))
163+
(is (= String (:class a))))
164+
165+
(let [a (ast1 File/new)]
166+
(is (= :method-value (:op a)))
167+
(is (= :ctor (:kind a)))
168+
(is (= java.io.File (:class a))))
169+
170+
(let [a (ast1 Integer/MAX_VALUE)]
171+
(is (= :static-field (:op a)))
172+
(is (= Integer (:class a))))
173+
174+
(let [a (ana (r/read-string "String/1"))]
175+
(is (= :const (:op a)))
176+
(is (= :class (:type a)))
177+
(is (.isArray ^Class (:val a))))
178+
179+
(let [a (ast1 (File/new "."))]
180+
(is (= :new (:op a))))
181+
182+
(let [a (ast1 (String/.length "hello"))]
183+
(is (= :instance-call (:op a)))
184+
(is (= 'length (:method a))))
185+
186+
(let [a (ast1 (String/.substring "hello" 1 3))]
187+
(is (= :instance-call (:op a)))
188+
(is (= 'substring (:method a))))
189+
190+
(let [a (ast1 (Integer/parseInt "7"))]
191+
(is (= :static-call (:op a)))
192+
(is (= 'parseInt (:method a)))))

src/test/clojure/clojure/tools/analyzer/jvm/passes_test.clj

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
[clojure.set :as set]
99
[clojure.tools.analyzer.passes.add-binding-atom :refer [add-binding-atom]]
1010
[clojure.tools.analyzer.passes.collect-closed-overs :refer [collect-closed-overs]]
11-
[clojure.tools.analyzer.jvm.core-test :refer [ast ast1 e f f1]]
11+
[clojure.tools.reader :as r]
12+
[clojure.tools.analyzer.jvm.core-test :refer [ast ast1 ana e f f1]]
1213
[clojure.tools.analyzer.passes.jvm.emit-form
1314
:refer [emit-form emit-hygienic-form]]
1415
[clojure.tools.analyzer.passes.jvm.validate :as v]
@@ -22,7 +23,8 @@
2223
[clojure.tools.analyzer.passes.jvm.classify-invoke :refer [classify-invoke]])
2324
(:import (clojure.lang Keyword Var Symbol AFunction
2425
PersistentVector PersistentArrayMap PersistentHashSet ISeq)
25-
java.util.regex.Pattern))
26+
java.util.regex.Pattern
27+
(java.io File)))
2628

2729
(defn validate [ast]
2830
(env/with-env (ana.jvm/global-env)
@@ -161,3 +163,134 @@
161163
{:passes-opts (merge ana.jvm/default-passes-opts
162164
{:validate/wrong-tag-handler (fn [t ast]
163165
{t nil})})})))
166+
167+
(deftest method-value-emit-form-test
168+
(is (= 'java.io.File/.getName (emit-form (ast1 File/.getName))))
169+
170+
(is (= 'java.lang.String/valueOf (emit-form (ast1 String/valueOf))))
171+
172+
(is (= 'java.io.File/new (emit-form (ast1 File/new))))
173+
174+
(let [emitted (emit-form (ana (r/read-string "^[long] String/valueOf")))]
175+
(is (= 'java.lang.String/valueOf emitted))
176+
(is (= '[long] (:param-tags (meta emitted)))))
177+
178+
(let [emitted (emit-form (ana (r/read-string "^[int int] String/.substring")))]
179+
(is (= 'java.lang.String/.substring emitted))
180+
(is (= '[int int] (:param-tags (meta emitted))))))
181+
182+
(deftest method-value-validate-test
183+
(let [a (ast1 File/.getName)]
184+
(is (= :method-value (:op a)))
185+
(is (:validated? a))
186+
(is (= java.io.File (:class a))))
187+
188+
(let [a (ast1 String/valueOf)]
189+
(is (= :method-value (:op a)))
190+
(is (:validated? a))
191+
(is (pos? (count (:methods a)))))
192+
193+
(let [a (ast1 File/new)]
194+
(is (= :method-value (:op a)))
195+
(is (:validated? a))
196+
(is (= :ctor (:kind a))))
197+
198+
(let [a (ana (r/read-string "^[long] String/valueOf"))]
199+
(is (= :method-value (:op a)))
200+
(is (:validated? a))
201+
(is (= 1 (count (:methods a)))))
202+
203+
(let [a (ana (r/read-string "^[int int] String/.substring"))]
204+
(is (= :method-value (:op a)))
205+
(is (:validated? a))
206+
(is (= 1 (count (:methods a))))))
207+
208+
(deftest method-value-kinds-test
209+
(let [a (ast1 File/.isDirectory)]
210+
(is (= :instance (:kind a)))
211+
(is (= 'isDirectory (:method a))))
212+
213+
(let [a (ast1 Character/isDigit)]
214+
(is (= :method-value (:op a)))
215+
(is (= :static (:kind a)))
216+
(is (= 'isDigit (:method a)))
217+
(is (< 1 (count (:methods a)))))
218+
219+
(let [a (ast1 String/new)]
220+
(is (= :ctor (:kind a)))
221+
(is (= String (:class a))))
222+
223+
(let [a (ast1 File/.getName)]
224+
(is (= AFunction (:o-tag a)))))
225+
226+
(deftest method-value-field-overload-test
227+
(let [a (ast1 Integer/MAX_VALUE)]
228+
(is (= :static-field (:op a))))
229+
230+
(let [a (ast1 Boolean/TRUE)]
231+
(is (= :static-field (:op a)))))
232+
233+
(deftest qualified-method-invocation-test
234+
(let [a (ast1 (File/new "."))]
235+
(is (= :new (:op a))))
236+
237+
(let [a (ast1 (String/.length "hello"))]
238+
(is (= :instance-call (:op a)))
239+
(is (= 'length (:method a)))
240+
(is (:validated? a)))
241+
242+
(let [a (ast1 (String/.substring "hello" 1 3))]
243+
(is (= :instance-call (:op a)))
244+
(is (= 'substring (:method a)))
245+
(is (= 2 (count (:args a)))))
246+
247+
(let [a (ast1 (Integer/parseInt "7"))]
248+
(is (= :static-call (:op a)))
249+
(is (= 'parseInt (:method a)))
250+
(is (:validated? a)))
251+
252+
(let [a (ast1 (File/.isDirectory (File. ".")))]
253+
(is (= :instance-call (:op a)))
254+
(is (= 'isDirectory (:method a)))))
255+
256+
(deftest param-tags-invocation-test
257+
(let [a (ana (r/read-string "(^[long] String/valueOf 42)"))]
258+
(is (= :static-call (:op a)))
259+
(is (:validated? a))
260+
(is (= '[long] (:param-tags a))))
261+
262+
(let [a (ana (r/read-string "(^[int int] String/.substring \"hello\" 1 3)"))]
263+
(is (= :instance-call (:op a)))
264+
(is (:validated? a))
265+
(is (= '[int int] (:param-tags a))))
266+
267+
(let [a (ana (r/read-string "(^[int _] String/.substring \"hello\" 1 3)"))]
268+
(is (= :instance-call (:op a)))
269+
(is (:validated? a))
270+
(is (= '[int _] (:param-tags a))))
271+
272+
(let [a (ana (r/read-string "^[int/1] java.util.Arrays/sort"))]
273+
(is (= :method-value (:op a)))
274+
(is (= :static (:kind a)))
275+
(is (= 1 (count (:methods a))))))
276+
277+
(deftest existing-interop-unchanged-test
278+
(let [a (ast1 (.length "hello"))]
279+
(is (= :instance-call (:op a)))
280+
(is (:validated? a)))
281+
282+
(let [a (ast1 (String. "foo"))]
283+
(is (= :new (:op a)))
284+
(is (:validated? a)))
285+
286+
(is (= Void/TYPE (:tag (ast1 (.println System/out "foo")))))
287+
288+
(let [a (ast1 (Integer/parseInt "7"))]
289+
(is (= :static-call (:op a)))
290+
(is (:validated? a)))
291+
292+
(let [a (ast1 Integer/MAX_VALUE)]
293+
(is (= :static-field (:op a))))
294+
295+
(let [a (ast1 Boolean/TYPE)]
296+
(is (= :static-field (:op a)))))

0 commit comments

Comments
 (0)