Skip to content

Commit b616ae6

Browse files
cskkscbbatsov
authored andcommitted
[#1650] Migrate some tests to buttercup (#1676)
Tests migrated: - cider-client-tests.el - cider-overlay-tests.el - cider-stacktrace-tests.el - cider-util-tests.el
1 parent 93651b7 commit b616ae6

File tree

4 files changed

+271
-183
lines changed

4 files changed

+271
-183
lines changed

test/cider-client-tests.el

Lines changed: 143 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
(require 'buttercup)
12
(require 'cider)
23
(require 'cider-client)
3-
(require 'ert)
44

55
;;; cider-client tests
66

@@ -19,97 +19,146 @@ SYMBOL is locally let-bound to the current buffer."
1919
(,symbol (current-buffer)))
2020
,@body)))
2121

22-
(ert-deftest cider-current-connection-nil-case ()
23-
(let ((cider-connections nil))
24-
(should-not (cider-current-connection))
25-
(should-not (cider-current-connection "clj"))
26-
(should-not (cider-current-connection "cljs"))))
27-
28-
(ert-deftest cider-current-connection-follow-type-argument ()
29-
(with-connection-buffer "clj" b1
30-
(should (equal (cider-current-connection) b1))
31-
(should (equal (cider-current-connection "clj") b1))
32-
(should-not (cider-current-connection "cljs"))
33-
(with-connection-buffer "cljs" b2
34-
(should (equal (cider-current-connection) b2))
35-
(should (equal (cider-current-connection "cljs") b2))
36-
(should (equal (cider-current-connection "clj") b1))
37-
(with-connection-buffer "cljs" b3
38-
(should (equal (cider-current-connection) b3))
39-
(should (equal (cider-current-connection "cljs") b3))
40-
(should (equal (cider-current-connection "clj") b1)))
41-
(with-connection-buffer "clj" b4
42-
(should (equal (cider-current-connection) b4))
43-
(should (equal (cider-current-connection "cljs") b2))
44-
(should (equal (cider-current-connection "clj") b4))))))
45-
46-
(ert-deftest cider-current-connection-follow-file-extension ()
47-
;; A single connection buffer.
48-
(with-connection-buffer "clj" b1
49-
(with-temp-buffer
50-
(setq major-mode 'clojure-mode)
51-
(should (equal (cider-current-connection) b1))
52-
(should (equal (cider-current-connection "clj") b1))
53-
(should-not (cider-current-connection "cljs")))
54-
(with-temp-buffer
55-
;; No cljs repl exists, but the TYPE argument is nil, so the
56-
;; next-best-thing is returned.
57-
(setq major-mode 'clojurescript-mode)
58-
(should (equal (cider-current-connection) b1))
59-
(should (equal (cider-current-connection "clj") b1))
60-
(should-not (cider-current-connection "cljs"))))
61-
(with-connection-buffer "cljs" b1
62-
(with-temp-buffer
63-
(setq major-mode 'clojure-mode)
64-
;; No clj repl exists, but the TYPE argument is nil, so the
65-
;; next-best-thing is returned.
66-
(should (equal (cider-current-connection) b1))
67-
(should (equal (cider-current-connection "cljs") b1))
68-
(should-not (cider-current-connection "clj")))
69-
(with-temp-buffer
70-
(setq major-mode 'clojurescript-mode)
71-
(should (equal (cider-current-connection) b1))
72-
(should (equal (cider-current-connection "cljs") b1))
73-
(should-not (cider-current-connection "clj"))))
74-
;; Multiple connection buffers.
75-
(with-connection-buffer "clj" bb1
76-
(with-connection-buffer "cljs" bb2
77-
;; The two buffers above are not used. We just want to ensure we are
78-
;; returning the most recent connection in case of ambiguity.
22+
23+
(describe "cider-current-connection"
24+
25+
(describe "when there are no active connections"
26+
:var (cider-connections)
27+
(it "returns nil"
28+
(setq cider-connections nil)
29+
(expect (cider-current-connection) :not :to-be-truthy)
30+
(expect (cider-current-connection "clj") :not :to-be-truthy)
31+
(expect (cider-current-connection "cljs") :not :to-be-truthy)))
32+
33+
34+
(describe "when active connections are available"
35+
36+
(it "always returns the latest connection"
37+
(with-connection-buffer "clj" bb1
38+
(with-connection-buffer "cljs" bb2
39+
(with-connection-buffer "clj" b1
40+
(with-connection-buffer "cljs" b2
41+
(expect (cider-current-connection) :to-equal b2)
42+
43+
;; follows type arguments
44+
(expect (cider-current-connection "clj") :to-equal b1)
45+
(expect (cider-current-connection "cljs") :to-equal b2)
46+
47+
;; follows file type
48+
(with-temp-buffer
49+
(setq major-mode 'clojure-mode)
50+
(expect (cider-current-connection) :to-equal b1))
51+
52+
(with-temp-buffer
53+
(setq major-mode 'clojurescript-mode)
54+
(expect (cider-current-connection) :to-equal b2)))))))
55+
56+
(describe "when type argument is given"
57+
(describe "when connection of that type exists"
58+
(it "returns that connection buffer"
59+
;; for clj
60+
(with-connection-buffer "clj" b1
61+
(with-connection-buffer "cljs" b2
62+
(expect (cider-current-connection "clj") :to-equal b1)))
63+
;; for cljs
64+
(with-connection-buffer "cljs" b1
65+
(with-connection-buffer "clj" b2
66+
(expect (cider-current-connection "cljs") :to-equal b1)))))
67+
68+
(describe "when connection of that type doesn't exists"
69+
(it "returns nil"
70+
;; for clj
71+
(with-connection-buffer "cljs" b1
72+
(expect (cider-current-connection "clj") :to-equal nil))
73+
74+
;; for cljs
75+
(with-connection-buffer "clj" b2
76+
(expect (cider-current-connection "cljs") :to-equal nil)))))
77+
78+
(describe "when type argument is not given"
79+
(describe "when a connection matching current file extension exists"
80+
(it "returns that connection buffer"
81+
;; for clj
82+
(with-connection-buffer "clj" b1
83+
(with-connection-buffer "cljs" b2
84+
(with-temp-buffer
85+
(setq major-mode 'clojure-mode)
86+
(expect (cider-current-connection) :to-equal b1))))
87+
88+
;; for cljs
89+
(with-connection-buffer "cljs" b1
90+
(with-connection-buffer "clj" b2
91+
(with-temp-buffer
92+
(setq major-mode 'clojurescript-mode)
93+
(expect (cider-current-connection) :to-equal b1))))))
94+
95+
(describe "when a connection matching current file extension doesn't exist"
96+
(it "returns the latest connection buffer"
97+
;; for clj
98+
(with-connection-buffer "clj" b1
99+
(with-temp-buffer
100+
(setq major-mode 'clojurescript-mode)
101+
(expect (cider-current-connection) :to-equal b1)))
102+
103+
;; for cljs
104+
(with-connection-buffer "cljs" b2
105+
(with-temp-buffer
106+
(setq major-mode 'clojure-mode)
107+
(expect (cider-current-connection) :to-equal b2))))))))
108+
109+
110+
(describe "cider-other-connection"
111+
(describe "when there are no active connections"
112+
:var (cider-connections)
113+
(it "returns nil"
114+
(setq cider-connections nil)
115+
(expect (cider-other-connection) :to-equal nil)))
116+
117+
(describe "when there is only 1 active connection"
118+
(it "returns nil"
119+
;; for clj
79120
(with-connection-buffer "clj" b1
80-
(with-connection-buffer "cljs" b2
81-
(with-temp-buffer
82-
(setq major-mode 'clojure-mode)
83-
;; TYPE argument is nil, a clj connection exists and we're in
84-
;; clojure-mode, so we return the clj connection even though the top
85-
;; connection is cljs.
86-
(should (equal (cider-current-connection) b1))
87-
(should (equal (cider-current-connection "clj") b1))
88-
(should (equal (cider-current-connection "cljs") b2)))
89-
(with-temp-buffer
90-
(setq major-mode 'clojurescript-mode)
91-
;; A cljs repl exists, and the TYPE argument is nil.
92-
(should (equal (cider-current-connection) b2))
93-
(should (equal (cider-current-connection "clj") b1))
94-
(should (equal (cider-current-connection "cljs") b2))))))))
95-
96-
(ert-deftest cider-other-connection ()
97-
(let ((cider-connections nil))
98-
(should-not (cider-other-connection))
99-
(with-connection-buffer "clj" b1
100-
(should-not (cider-other-connection))
101-
(should-not (cider-other-connection b1))
102-
(with-connection-buffer "cljs" b2
103-
(should (equal (cider-other-connection) b1))
104-
(should (equal (cider-other-connection b1) b2))
105-
(should (equal (cider-other-connection b2) b1))
106-
(with-connection-buffer "cljs" b3
107-
(should (equal (cider-other-connection) b1))
108-
(should (equal (cider-other-connection b1) b3))
109-
(should (equal (cider-other-connection b3) b1))
110-
(should (equal (cider-other-connection b2) b1)))
111-
(with-connection-buffer "clj" b4
112-
(should (equal (cider-other-connection) b2))
113-
(should (equal (cider-other-connection b4) b2))
114-
(should (equal (cider-other-connection b2) b4))
115-
(should (equal (cider-other-connection b1) b2)))))))
121+
(expect (cider-other-connection) :to-equal nil)
122+
(expect (cider-other-connection b1) :to-equal nil))
123+
;; for cljs
124+
(with-connection-buffer "cljs" b1
125+
(expect (cider-other-connection) :to-equal nil)
126+
(expect (cider-other-connection b1) :to-equal nil))))
127+
128+
(describe "when active connections are available"
129+
(describe "when a connection of other type doesn't exist"
130+
(it "returns nil"
131+
;; for clj
132+
(with-connection-buffer "clj" b1
133+
(with-connection-buffer "clj" b2
134+
(expect (cider-other-connection) :to-equal nil)
135+
(expect (cider-other-connection b1) :to-equal nil)
136+
(expect (cider-other-connection b2) :to-equal nil)))
137+
;; for cljs
138+
(with-connection-buffer "cljs" b1
139+
(with-connection-buffer "cljs" b2
140+
(expect (cider-other-connection) :to-equal nil)
141+
(expect (cider-other-connection b1) :to-equal nil)
142+
(expect (cider-other-connection b2) :to-equal nil)))))
143+
144+
(describe "when a connection of other type exists"
145+
(it "returns that connection"
146+
(with-connection-buffer "clj" b1
147+
(with-connection-buffer "cljs" b2
148+
(expect (cider-other-connection) :to-equal b1)
149+
(expect (cider-other-connection b1) :to-equal b2)
150+
(expect (cider-other-connection b2) :to-equal b1)))))
151+
152+
(describe "when there are multiple active connections"
153+
(it "always returns the latest connection"
154+
155+
(with-connection-buffer "clj" bb1
156+
(with-connection-buffer "cljs" bb2
157+
(with-connection-buffer "clj" b1
158+
(with-connection-buffer "cljs" b2
159+
(expect (cider-other-connection) :to-equal b1)
160+
(expect (cider-other-connection b1) :to-equal b2)
161+
(expect (cider-other-connection b2) :to-equal b1)
162+
;; older connections still work
163+
(expect (cider-other-connection bb1) :to-equal b2)
164+
(expect (cider-other-connection bb2) :to-equal b1)))))))))

test/cider-overlay-tests.el

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,59 @@
1919

2020
;;; Code:
2121

22-
(require 'ert)
22+
(require 'buttercup)
2323
(require 'cider-overlays)
2424

25-
(ert-deftest cider--make-result-overlay ()
26-
;; First, just don't error.
27-
(with-temp-buffer
28-
(insert "garbage")
29-
(save-excursion (insert "\nmore trash"))
30-
(cider--make-result-overlay "ok")
31-
(should (overlays-at (point-min)))
32-
;; Then do some tests.
33-
(mapc #'cider--delete-overlay (overlays-at (point-min)))
34-
(let ((o1 (remove nil (mapcar #'overlay-start
35-
(overlays-at (point-min))))))
36-
(should-not o1))
37-
38-
;; Duration
39-
(cider--make-result-overlay "ok" :duration 'command)
40-
(run-hooks 'post-command-hook)
41-
(run-hooks 'post-command-hook)
42-
(let ((o2 (remove nil (mapcar #'overlay-start
43-
(overlays-at (point-min))))))
44-
(should-not o2))
45-
46-
(let ((this-command nil))
47-
(cider--make-result-overlay "ok" :duration 'command))
48-
(run-hooks 'post-command-hook)
49-
(let ((o3 (remove nil (mapcar #'overlay-start
50-
(overlays-at (point-min))))))
51-
(should-not o3))
52-
53-
(cider--make-result-overlay "ok" :duration 1.5)
54-
(sleep-for 1)
55-
(let ((o4 (remove nil (mapcar #'overlay-start
56-
(overlays-at (point-min))))))
57-
(should o4))
58-
(sleep-for 1)
59-
(let ((o5 (remove nil (mapcar #'overlay-start
60-
(overlays-at (point-min))))))
61-
(should-not o5))))
25+
(defmacro cider--with-overlay (overlay-args &rest body)
26+
"Run BODY in a temp buffer, with overlays created."
27+
(declare (indent 1)
28+
(debug (sexp sexp &rest form)))
29+
`(with-temp-buffer
30+
(insert "garbage")
31+
(save-excursion (insert "\nmore trash"))
32+
(cider--make-result-overlay ,@overlay-args)
33+
,@body))
6234

6335

36+
(describe "cider--make-result-overlay"
37+
:var (overlay-position this-command)
38+
39+
(before-all
40+
(fset 'overlay-position (lambda ()
41+
(mapcar #'overlay-start
42+
(overlays-at (point-min))))))
43+
44+
(it "can create overlays"
45+
(cider--with-overlay ("ok")
46+
(expect (overlays-at (point-min)) :to-be-truthy)))
47+
48+
(describe "when overlay duration is `command`"
49+
(it "erases overlays after the next command is executed"
50+
(cider--with-overlay ("ok" :duration 'command)
51+
(run-hooks 'post-command-hook)
52+
(run-hooks 'post-command-hook)
53+
(expect (overlay-position) :to-equal nil))
54+
55+
(cider--with-overlay ("ok" :duration 'command)
56+
(setq this-command nil)
57+
(run-hooks 'post-command-hook)
58+
(expect (overlay-position) :to-equal nil))))
59+
60+
(describe "when overlay duration is given in secs"
61+
(it "erases overlays after that duration"
62+
(cider--with-overlay ("ok" :duration 1.5)
63+
(sleep-for 1)
64+
(expect (overlay-position) :not :to-equal nil)
65+
(sleep-for 1)
66+
(expect (overlay-position) :to-equal nil)))))
67+
68+
(describe "cider--delete-overlay"
69+
:var (overlay-position)
70+
(it "deletes overlays"
71+
(cider--with-overlay ("ok")
72+
(mapc #'cider--delete-overlay (overlays-at (point-min)))
73+
(setq overlay-position (mapcar #'overlay-start (overlays-at (point-min))))
74+
(expect overlay-position :to-equal nil))))
75+
6476
(provide 'cider-overlay-tests)
6577
;;; cider-overlay-tests.el ends here

0 commit comments

Comments
 (0)