|
8 | 8 | [clojure.set :as set] |
9 | 9 | [clojure.tools.analyzer.passes.add-binding-atom :refer [add-binding-atom]] |
10 | 10 | [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]] |
12 | 13 | [clojure.tools.analyzer.passes.jvm.emit-form |
13 | 14 | :refer [emit-form emit-hygienic-form]] |
14 | 15 | [clojure.tools.analyzer.passes.jvm.validate :as v] |
|
22 | 23 | [clojure.tools.analyzer.passes.jvm.classify-invoke :refer [classify-invoke]]) |
23 | 24 | (:import (clojure.lang Keyword Var Symbol AFunction |
24 | 25 | PersistentVector PersistentArrayMap PersistentHashSet ISeq) |
25 | | - java.util.regex.Pattern)) |
| 26 | + java.util.regex.Pattern |
| 27 | + (java.io File))) |
26 | 28 |
|
27 | 29 | (defn validate [ast] |
28 | 30 | (env/with-env (ana.jvm/global-env) |
|
161 | 163 | {:passes-opts (merge ana.jvm/default-passes-opts |
162 | 164 | {:validate/wrong-tag-handler (fn [t ast] |
163 | 165 | {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