1
1
; ;; lispy-clojure.clj --- lispy support for Clojure.
2
2
3
- ; ; Copyright (C) 2015-2017 Oleh Krehel
3
+ ; ; Copyright (C) 2015-2018 Oleh Krehel
4
4
5
5
; ; This file is not part of GNU Emacs
6
6
21
21
(:require [clojure.repl :as repl]
22
22
[clojure.pprint]
23
23
[clojure.java.io :as io])
24
- (:use [clojure.test :only (is deftest )]
25
- [cemerick.pomegranate :only (add-dependencies )])
24
+ (:use [cemerick.pomegranate :only (add-dependencies )])
26
25
(:import (java.io File LineNumberReader InputStreamReader
27
26
PushbackReader FileInputStream)
28
27
(clojure.lang RT Reflector)))
@@ -200,10 +199,6 @@ malleable to refactoring."
200
199
((= (first func-def) 'def)
201
200
(get-func-args-def func-def n-args))))
202
201
203
- (deftest get-func-args-test
204
- (is (= (get-func-args (symbol-function 'string?) 1 ) '[x]))
205
- (is (= (get-func-args (symbol-function 'to-array) 1 ) '[coll])))
206
-
207
202
(defn debug-step-in
208
203
" Evaluate the function call arugments and sub them into function arguments."
209
204
[expr]
@@ -224,8 +219,9 @@ malleable to refactoring."
224
219
(let [vals-map (eval eval-form)]
225
220
`(do
226
221
(in-ns '~(symbol (str func-ns)))
227
- ~@(map (fn [s] `(def ~s ~ ((keyword s) vals-map)))
228
- func-args))))))
222
+ ~@(map (fn [s] `(def ~s ~((keyword s) vals-map)))
223
+ func-args)
224
+ ~vals-map)))))
229
225
230
226
(defn object-methods [sym]
231
227
(distinct
@@ -245,9 +241,6 @@ malleable to refactoring."
245
241
(concat (object-fields sym)
246
242
(object-methods sym)))
247
243
248
- (deftest object-members-test
249
- (is ((into #{} (object-members Thread)) " run" )))
250
-
251
244
(defn get-meth [obj method-name]
252
245
(first (filter #(= (.getName %) method-name)
253
246
(.getMethods (type obj)))))
@@ -310,41 +303,6 @@ malleable to refactoring."
310
303
" is uncallable" )
311
304
args))])))
312
305
313
- (deftest dest-test
314
- (is (= (eval (dest '[[x y] (list 1 2 3 )]))
315
- {:x 1 , :y 2 }))
316
- (is (= (eval (dest '[[x & y] [1 2 3 ]]))
317
- {:x 1 , :y '(2 3 )}))
318
- (is (= (eval (dest '[[x y] (list 1 2 3 ) [a b] [y x]]))
319
- {:x 1 , :y 2 , :a 2 , :b 1 }))
320
- (is (= (eval (dest '[[x y z] [1 2 ]]))
321
- {:x 1 , :y 2 , :z nil }))
322
- (is (= (eval (dest '[[x & tail :as all] [1 2 3 ]]))
323
- {:x 1 ,
324
- :tail '(2 3 ),
325
- :all [1 2 3 ]}))
326
- (is (= (eval (dest '[[x & tail :as all] " Clojure" ]))
327
- {:x \C,
328
- :tail '(\l \o \j \u \r \e),
329
- :all " Clojure" }))
330
- (is (= (eval (dest '[{x 1 y 2 } {1 " one" 2 " two" 3 " three" }]))
331
- {:x " one" , :y " two" }))
332
- (is (= (eval (dest '[{x 1 y 2 :or {x " one" y " two" } :as all} {2 " three" }]))
333
- {:all {2 " three" },
334
- :x " one" ,
335
- :y " three" }))
336
- (is (= (eval (dest '[{:keys [x y]} {:x " one" :z " two" }]))
337
- {:x " one" , :y nil }))
338
- (is (= (eval (dest '[{:strs [x y]} {" x" " one" " z" " two" }]))
339
- {:x " one" , :y nil }))
340
- (is (= (eval (dest '[{:syms [x y]} {'x " one" 'z " two" }]))
341
- {:x " one" , :y nil })))
342
-
343
- (deftest debug-step-in-test
344
- (is (= (eval (debug-step-in
345
- '(expand-home (str " /foo" " /bar" ))))
346
- {:path " /foo/bar" })))
347
-
348
306
(defmacro ok
349
307
" On getting an Exception, just print it."
350
308
[& body]
@@ -379,10 +337,6 @@ malleable to refactoring."
379
337
(catch Exception e
380
338
(= a b))))
381
339
382
- (deftest reader=-test
383
- (is (= (reader= '(map #(* % %) '(1 2 3 ))
384
- '(map #(* % %) '(1 2 3 ))))))
385
-
386
340
(defn position [x coll equality]
387
341
(letfn [(iter [i coll]
388
342
(xcond
@@ -393,12 +347,6 @@ malleable to refactoring."
393
347
(recur (inc i) (rest coll)))))]
394
348
(iter 0 coll)))
395
349
396
- (deftest position-test
397
- (let [x (read-string " (map #(* % %) as)" )
398
- c (read-string " [as (range 10) bs (map #(* % %) as)]" )]
399
- (is (= (position x c =) nil ))
400
- (is (= (position x c reader=) 3 ))))
401
-
402
350
(defn guess-intent [expr context]
403
351
(if (not (or (list? expr)
404
352
(vector? expr)))
@@ -438,18 +386,20 @@ malleable to refactoring."
438
386
(= 'defn (first expr))
439
387
file line)
440
388
(let [arglist-pos (first (keep-indexed
441
- (fn [i x] (if (vector? x) i))
389
+ (fn [i x] (if (or
390
+ (vector? x)
391
+ (list? x)) i))
442
392
expr))
443
393
expr-head (take arglist-pos expr)
444
394
expr-tail (drop arglist-pos expr)
445
395
expr-doc (or (first (filter string? expr-head)) " " )
446
396
expr-map (or (first (filter map? expr-head)) {})]
447
- `(defn ~(nth expr 1 )
448
- ~expr-doc
449
- ~(merge {:l-file file
450
- :l-line line}
451
- expr-map)
452
- ~@expr-tail))))
397
+ `(~' defn ~(nth expr 1 )
398
+ ~expr-doc
399
+ ~(merge {:l-file file
400
+ :l-line line}
401
+ expr-map)
402
+ ~@expr-tail))))
453
403
454
404
(defn reval [e-str context-str & {:keys [file line]}]
455
405
(let [expr (read-string e-str)
@@ -479,34 +429,6 @@ malleable to refactoring."
479
429
(clojure.pprint/pprint
480
430
expr)))
481
431
482
- (deftest guess-intent-test
483
- (is (= (guess-intent '(defproject ) nil ) '(lispy-clojure/fetch-packages)))
484
- (is (= (guess-intent 'x '[x y]) 'x))
485
- (is (= (guess-intent '*ns* '*ns*) '*ns*)))
486
-
487
- (deftest reval-test
488
- (let [s " (->> 5
489
- (range)
490
- (map (fn [x] (* x x)))
491
- (map (fn [x] (+ x x))))" ]
492
- (is (= (reval " (range)" s)
493
- '(0 1 2 3 4 )))
494
- (is (= (reval " (map (fn [x] (* x x)))" s)
495
- '(0 1 4 9 16 )))
496
- (is (= (reval " (map (fn [x] (+ x x)))" s)
497
- '(0 2 8 18 32 ))))
498
- (is (= (reval " x (+ 2 2)" " [x (+ 2 2)]" ) {:x 4 }))
499
- (is (= (reval " (+ 2 2)" " [x (+ 2 2)]" ) {:x 4 }))
500
- (is (= (reval " (+ 2 2)" " [x (+ 1 2) y (+ 2 2)]" ) {:y 4 }))
501
- (is (= (reval " (range 5)" " [[x & y] (range 5)]" )
502
- {:x 0 , :y '(1 2 3 4 )}))
503
- (is (= (reval " [c [(* 2 21) 0]]" " (doseq [c [(* 2 21) 0]]\n ())" ) {:c 42 }))
504
- (is (= (reval " [i 3]" " (dotimes [i 3])" ) {:i 0 }))
505
- (is (= (reval " [a b] (range 5)" " [[a b] (range 5)]" ) {:a 0 , :b 1 }))
506
- (let [js " (doto (new java.util.HashMap) (.put \" a\" 1) (.put \" b\" 2))" ]
507
- (is (= (reval " (.put \" a\" 1)" js) {" a" 1 }))
508
- (is (= (reval " (.put \" b\" 2)" js) {" a" 1 , " b" 2 }))))
509
-
510
432
(defn all-docs [ns ]
511
433
(clojure.string/join
512
434
" ::"
@@ -523,5 +445,3 @@ malleable to refactoring."
523
445
(compliment/completions
524
446
prefix
525
447
{:context :same :plain-candidates true }))
526
-
527
- (clojure.test/run-tests 'lispy-clojure)
0 commit comments