|
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) |
|
68 | 70 | (deftest annotate-tag-test |
69 | 71 | (is (= PersistentVector (-> {:op :const :form [] :val []} annotate-tag :tag))) |
70 | 72 | (is (= PersistentVector (-> (ast []) annotate-tag :tag))) |
71 | | - (is (= PersistentArrayMap(-> (ast {}) annotate-tag :tag))) |
| 73 | + (is (= PersistentArrayMap (-> (ast {}) annotate-tag :tag))) |
72 | 74 | (is (= PersistentHashSet (-> (ast #{}) annotate-tag :tag))) |
73 | 75 | (is (= Class (-> {:op :const :type :class :form Object :val Object} |
74 | | - annotate-tag :tag))) |
| 76 | + annotate-tag :tag))) |
75 | 77 | (is (= String (-> (ast "foo") annotate-tag :tag))) |
76 | 78 | (is (= Keyword (-> (ast :foo) annotate-tag :tag))) |
77 | 79 | (is (= Character/TYPE (-> (ast \f) annotate-tag :tag))) |
|
80 | 82 | (is (= Var (-> (ast #'+) annotate-tag :tag))) |
81 | 83 | (is (= Boolean (-> (ast true) annotate-tag :tag))) |
82 | 84 | (let [b-ast (-> (ast (let [a 1] a)) add-binding-atom |
83 | | - (postwalk annotate-tag))] |
| 85 | + (postwalk annotate-tag))] |
84 | 86 | (is (= Long/TYPE (-> b-ast :body :ret :tag))))) |
85 | 87 |
|
86 | 88 | (deftest classify-invoke-test |
87 | 89 | (is (= :keyword-invoke (-> (ast (:foo {})) classify-invoke :op))) |
88 | 90 | (is (= :invoke (-> (ast (:foo {} 1)) classify-invoke :op))) |
89 | 91 | (is (= :protocol-invoke (-> (ast (f nil)) classify-invoke :op))) |
90 | 92 | (is (= :instance? (-> (ast (instance? String "")) |
91 | | - (prewalk analyze-host-expr) classify-invoke :op))) |
| 93 | + (prewalk analyze-host-expr) classify-invoke :op))) |
92 | 94 | (is (= :prim-invoke (-> (ast (f1 1)) (prewalk infer-tag) classify-invoke :op)))) |
93 | 95 |
|
94 | 96 | (deftest annotate-host-info-test |
|
100 | 102 | ;; TODO: test primitives, tag matching, throwing validation, method validation |
101 | 103 | (deftest validate-test |
102 | 104 | (is (= Exception (-> (ast (try (catch Exception e))) |
103 | | - (prewalk (comp validate analyze-host-expr)) :catches first :class :val))) |
| 105 | + (prewalk (comp validate analyze-host-expr)) :catches first :class :val))) |
104 | 106 | (is (-> (ast (set! *warn-on-reflection* true)) validate)) |
105 | 107 | (is (= true (-> (ast (String. "foo")) (postwalk (comp validate annotate-tag analyze-host-expr)) |
106 | | - :validated?))) |
| 108 | + :validated?))) |
107 | 109 |
|
108 | 110 | (let [s-ast (-> (ast (Integer/parseInt "7")) (prewalk annotate-tag) analyze-host-expr validate)] |
109 | 111 | (is (:validated? s-ast)) |
|
134 | 136 |
|
135 | 137 | (is (= String (-> (ast1 String) :val))) |
136 | 138 | (is (= 'String (-> (ast1 String) :form))) |
137 | | -(is (= PersistentVector (-> (ast1 '[]) :tag))) |
138 | | -(is (= ISeq (-> (ast1 '()) :tag))) |
| 139 | + (is (= PersistentVector (-> (ast1 '[]) :tag))) |
| 140 | + (is (= ISeq (-> (ast1 '()) :tag))) |
139 | 141 |
|
140 | 142 | (let [d-ast (ast1 (Double/isInfinite 2))] |
141 | 143 | (is (= Boolean/TYPE (-> d-ast :tag))) |
|
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