Skip to content

Commit b68784d

Browse files
committed
lispy_clojure_test.clj: Split away the tests
1 parent d2442b2 commit b68784d

File tree

4 files changed

+170
-96
lines changed

4 files changed

+170
-96
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ elisp:
2929
$(QEMACS) lispy.el
3030

3131
clojure:
32-
clojure -e '(load-file "lispy-clojure.clj")'
32+
clojure -C:test -e '(load-file "lispy_clojure_test.clj")'
3333

3434
clean:
3535
rm -f *.elc

deps.edn

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{:deps {org.clojure/clojure {:mvn/version "1.9.0"}
22
;; get rid of weird warnings on startup
33
org.slf4j/slf4j-simple {:mvn/version "1.6.2"}
4-
com.cemerick/pomegranate {:mvn/version "0.4.0"}}}
4+
com.cemerick/pomegranate {:mvn/version "0.4.0"}}
5+
:aliases
6+
{:test
7+
{:extra-paths ["."]}}}

lispy-clojure.clj lispy_clojure.clj

+14-94
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;;; lispy-clojure.clj --- lispy support for Clojure.
22

3-
;; Copyright (C) 2015-2017 Oleh Krehel
3+
;; Copyright (C) 2015-2018 Oleh Krehel
44

55
;; This file is not part of GNU Emacs
66

@@ -21,8 +21,7 @@
2121
(:require [clojure.repl :as repl]
2222
[clojure.pprint]
2323
[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)])
2625
(:import (java.io File LineNumberReader InputStreamReader
2726
PushbackReader FileInputStream)
2827
(clojure.lang RT Reflector)))
@@ -200,10 +199,6 @@ malleable to refactoring."
200199
((= (first func-def) 'def)
201200
(get-func-args-def func-def n-args))))
202201

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-
207202
(defn debug-step-in
208203
"Evaluate the function call arugments and sub them into function arguments."
209204
[expr]
@@ -224,8 +219,9 @@ malleable to refactoring."
224219
(let [vals-map (eval eval-form)]
225220
`(do
226221
(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)))))
229225

230226
(defn object-methods [sym]
231227
(distinct
@@ -245,9 +241,6 @@ malleable to refactoring."
245241
(concat (object-fields sym)
246242
(object-methods sym)))
247243

248-
(deftest object-members-test
249-
(is ((into #{} (object-members Thread)) "run")))
250-
251244
(defn get-meth [obj method-name]
252245
(first (filter #(= (.getName %) method-name)
253246
(.getMethods (type obj)))))
@@ -310,41 +303,6 @@ malleable to refactoring."
310303
"is uncallable")
311304
args))])))
312305

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-
348306
(defmacro ok
349307
"On getting an Exception, just print it."
350308
[& body]
@@ -379,10 +337,6 @@ malleable to refactoring."
379337
(catch Exception e
380338
(= a b))))
381339

382-
(deftest reader=-test
383-
(is (= (reader= '(map #(* % %) '(1 2 3))
384-
'(map #(* % %) '(1 2 3))))))
385-
386340
(defn position [x coll equality]
387341
(letfn [(iter [i coll]
388342
(xcond
@@ -393,12 +347,6 @@ malleable to refactoring."
393347
(recur (inc i) (rest coll)))))]
394348
(iter 0 coll)))
395349

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-
402350
(defn guess-intent [expr context]
403351
(if (not (or (list? expr)
404352
(vector? expr)))
@@ -438,18 +386,20 @@ malleable to refactoring."
438386
(= 'defn (first expr))
439387
file line)
440388
(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))
442392
expr))
443393
expr-head (take arglist-pos expr)
444394
expr-tail (drop arglist-pos expr)
445395
expr-doc (or (first (filter string? expr-head)) "")
446396
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))))
453403

454404
(defn reval [e-str context-str & {:keys [file line]}]
455405
(let [expr (read-string e-str)
@@ -479,34 +429,6 @@ malleable to refactoring."
479429
(clojure.pprint/pprint
480430
expr)))
481431

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-
510432
(defn all-docs [ns]
511433
(clojure.string/join
512434
"::"
@@ -523,5 +445,3 @@ malleable to refactoring."
523445
(compliment/completions
524446
prefix
525447
{:context :same :plain-candidates true}))
526-
527-
(clojure.test/run-tests 'lispy-clojure)

lispy_clojure_test.clj

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
;;; lispy-clojure-test.clj --- lispy support for Clojure.
2+
3+
;; Copyright (C) 2018 Oleh Krehel
4+
5+
;; This file is not part of GNU Emacs
6+
7+
;; This file is free software; you can redistribute it and/or modify
8+
;; it under the terms of the GNU General Public License as published by
9+
;; the Free Software Foundation; either version 3, or (at your option)
10+
;; any later version.
11+
12+
;; This program is distributed in the hope that it will be useful,
13+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
;; GNU General Public License for more details.
16+
17+
;; For a full copy of the GNU General Public License
18+
;; see <http://www.gnu.org/licenses/>.
19+
20+
(ns lispy-clojure-test
21+
(:require [lispy-clojure :as l])
22+
(:use [clojure.test :only [is deftest]]
23+
[lispy-clojure :only [add-location-to-defn
24+
debug-step-in
25+
dest
26+
expand-home
27+
get-func-args
28+
guess-intent
29+
object-members
30+
position
31+
reader=
32+
reval
33+
symbol-function]]))
34+
35+
(deftest get-func-args-test
36+
(is (= (get-func-args (symbol-function 'string?) 1) '[x]))
37+
(is (= (get-func-args (symbol-function 'to-array) 1) '[coll])))
38+
39+
(deftest object-members-test
40+
(is (= ((into #{} (object-members Thread)) "run"))))
41+
42+
(deftest dest-test
43+
(is (= (eval (dest '[[x y] (list 1 2 3)]))
44+
{:x 1, :y 2}))
45+
(is (= (eval (dest '[[x & y] [1 2 3]]))
46+
{:x 1, :y '(2 3)}))
47+
(is (= (eval (dest '[[x y] (list 1 2 3) [a b] [y x]]))
48+
{:x 1, :y 2, :a 2, :b 1}))
49+
(is (= (eval (dest '[[x y z] [1 2]]))
50+
{:x 1, :y 2, :z nil}))
51+
(is (= (eval (dest '[[x & tail :as all] [1 2 3]]))
52+
{:x 1,
53+
:tail '(2 3),
54+
:all [1 2 3]}))
55+
(is (= (eval (dest '[[x & tail :as all] "Clojure"]))
56+
{:x \C,
57+
:tail '(\l \o \j \u \r \e),
58+
:all "Clojure"}))
59+
(is (= (eval (dest '[{x 1 y 2} {1 "one" 2 "two" 3 "three"}]))
60+
{:x "one", :y "two"}))
61+
(is (= (eval (dest '[{x 1 y 2 :or {x "one" y "two"} :as all} {2 "three"}]))
62+
{:all {2 "three"},
63+
:x "one",
64+
:y "three"}))
65+
(is (= (eval (dest '[{:keys [x y]} {:x "one" :z "two"}]))
66+
{:x "one", :y nil}))
67+
(is (= (eval (dest '[{:strs [x y]} {"x" "one" "z" "two"}]))
68+
{:x "one", :y nil}))
69+
(is (= (eval (dest '[{:syms [x y]} {'x "one" 'z "two"}]))
70+
{:x "one", :y nil})))
71+
72+
(deftest debug-step-in-test
73+
(is (= (eval (debug-step-in
74+
'(expand-home (str "/foo" "/bar"))))
75+
{:path "/foo/bar"})))
76+
77+
(deftest reader=-test
78+
(is (= (reader= '(map #(* % %) '(1 2 3))
79+
'(map #(* % %) '(1 2 3))))))
80+
81+
(deftest position-test
82+
(let [x (read-string "(map #(* % %) as)")
83+
c (read-string "[as (range 10) bs (map #(* % %) as)]")]
84+
(is (= (position x c =) nil))
85+
(is (= (position x c reader=) 3))))
86+
87+
(deftest add-location-to-defn-test
88+
(is (= (add-location-to-defn
89+
'(defn fun
90+
"doc"
91+
([x1 x2])
92+
([x1]))
93+
"/foo/bar.clj" 42)
94+
'(defn fun
95+
"doc"
96+
{:l-file "/foo/bar.clj",
97+
:l-line 42}
98+
([x1 x2])
99+
([x1]))))
100+
(is (= (add-location-to-defn
101+
'(defn fun
102+
"doc"
103+
[x]
104+
x)
105+
"/foo/bar.clj" 42)
106+
'(defn fun
107+
"doc"
108+
{:l-file "/foo/bar.clj",
109+
:l-line 42}
110+
[x]
111+
x)))
112+
(is (= (add-location-to-defn
113+
'(defn fun [x]
114+
x)
115+
"/foo/bar.clj" 42)
116+
'(defn fun
117+
""
118+
{:l-file "/foo/bar.clj",
119+
:l-line 42}
120+
[x]
121+
x))))
122+
123+
(deftest guess-intent-test
124+
(is (= (guess-intent '(defproject) nil) '(lispy-clojure/fetch-packages)))
125+
(is (= (guess-intent 'x '[x y]) 'x))
126+
(is (= (guess-intent '*ns* '*ns*) '*ns*)))
127+
128+
(deftest reval-test
129+
(let [s "(->> 5
130+
(range)
131+
(map (fn [x] (* x x)))
132+
(map (fn [x] (+ x x))))"]
133+
(is (= (reval "(range)" s)
134+
'(0 1 2 3 4)))
135+
(is (= (reval "(map (fn [x] (* x x)))" s)
136+
'(0 1 4 9 16)))
137+
(is (= (reval "(map (fn [x] (+ x x)))" s)
138+
'(0 2 8 18 32))))
139+
(is (= (reval "x (+ 2 2)" "[x (+ 2 2)]") {:x 4}))
140+
(is (= (reval "(+ 2 2)" "[x (+ 2 2)]") {:x 4}))
141+
(is (= (reval "(+ 2 2)" "[x (+ 1 2) y (+ 2 2)]") {:y 4}))
142+
(is (= (reval "(range 5)" "[[x & y] (range 5)]")
143+
{:x 0, :y '(1 2 3 4)}))
144+
(is (= (reval "[c [(* 2 21) 0]]" "(doseq [c [(* 2 21) 0]]\n ())") {:c 42}))
145+
(is (= (reval "[i 3]" "(dotimes [i 3])") {:i 0}))
146+
(is (= (reval "[a b] (range 5)" "[[a b] (range 5)]") {:a 0, :b 1}))
147+
(let [js "(doto (new java.util.HashMap) (.put \"a\" 1) (.put \"b\" 2))"]
148+
(is (= (reval "(.put \"a\" 1)" js) {"a" 1}))
149+
(is (= (reval "(.put \"b\" 2)" js) {"a" 1, "b" 2}))))
150+
151+
(clojure.test/run-tests 'lispy-clojure-test)

0 commit comments

Comments
 (0)