summaryrefslogtreecommitdiff
path: root/test/cider-tests.el
blob: 063be21c086b174bba388380ec21b07113044731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
;;; cider-tests.el

;; Copyright © 2012-2016 Tim King, Bozhidar Batsov

;; Author: Tim King <kingtim@gmail.com>
;;         Bozhidar Batsov <bozhidar@batsov.com>
;;         Artur Malabarba <bruce.connor.am@gmail.com>

;; This file is NOT part of GNU Emacs.

;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see `http://www.gnu.org/licenses/'.

;;; Commentary:

;; This file is part of CIDER

;; To run these tests:
;;   All tests: M-x ert t
;;
;;; Code:

(require 'ert)
(require 'cider)
(require 'noflet)


;;;; generic

(ert-deftest test-debug-prompt ()
  (should (equal-including-properties
           (cider--debug-prompt '("a" "b" "c"))
           #("a b c\n"
             0 1 (face cider-debug-prompt-face)
             1 2 (face default)
             2 3 (face cider-debug-prompt-face)
             3 4 (face default)
             4 5 (face cider-debug-prompt-face)
             5 6 (face default))))
  (should (equal-including-properties
           (cider--debug-prompt '("a" "bc"))
           #("a bc\n"
             0 1 (face cider-debug-prompt-face)
             1 2 (face default)
             2 3 (face cider-debug-prompt-face)
             3 5 (face default))))
  (should (equal-including-properties
           (cider--debug-prompt '("abc"))
           #("abc\n" 0 1 (face cider-debug-prompt-face) 1 4 (face default)))))

(ert-deftest test-debug-move-point ()
  (with-temp-buffer
    (clojure-mode)
    (save-excursion (insert "(defn a [] (let [x 1] (inc x)) {:a 1, :b 2})"))
    (cider--debug-move-point '(3 2 1))
    (should (string= (thing-at-point 'symbol) "x"))
    (goto-char (point-min))
    (cider--debug-move-point '(3 1 1))
    (should (string= (thing-at-point 'symbol) "1"))
    (goto-char (point-min))
    (cider--debug-move-point '(2))
    (should (looking-back (rx "[]")))
    (goto-char (point-min))
    (cider--debug-move-point '(4 ":b"))
    (message "%S" (point))
    (should (string= (thing-at-point 'symbol) "2"))))

(ert-deftest test-debug-move-point-syntax-quote ()
  (with-temp-buffer
    (clojure-mode)
    (save-excursion (insert "(let [b 1] `((~b)))"))
    (cider--debug-move-point '(2 1 1 1 1 1 1))
    (should (string= (buffer-substring (point-min) (point)) "(let [b 1] `((~b"))
    (goto-char (point-min))
    (cider--debug-move-point '(2 1 1 1 1))
    (should (string= (buffer-substring (point-min) (point)) "(let [b 1] `((~b)")))

  (with-temp-buffer
    (clojure-mode)
    (save-excursion (insert "`((~a))"))
    (cider--debug-move-point '(1 1 1 1 1 1))
    (should (string= (buffer-substring (point-min) (point)) "`((~a"))
    (goto-char (point-min))
    (cider--debug-move-point '(1 1 1 1))
    (should (string= (buffer-substring (point-min) (point)) "`((~a)")))

  (with-temp-buffer
    (clojure-mode)
    (save-excursion (insert "`#{(~c)}"))
    (cider--debug-move-point '(2 1 1 1 1 1))
    (should (string= (buffer-substring (point-min) (point)) "`#{(~c"))
    (goto-char (point-min))
    (cider--debug-move-point '(2 1 1 1))
    (should (string= (buffer-substring (point-min) (point)) "`#{(~c)")))

  (with-temp-buffer
    (clojure-mode)
    (save-excursion (insert "`[(~d)]"))
    (cider--debug-move-point '(2 1 1 1 1 1))
    (should (string= (buffer-substring (point-min) (point)) "`[(~d"))
    (goto-char (point-min))
    (cider--debug-move-point '(2 1 1 1))
    (should (string= (buffer-substring (point-min) (point)) "`[(~d)"))))

(ert-deftest test-debug-move-point-metadata ()
  (with-temp-buffer
    (clojure-mode)
    (save-excursion (insert "(defn a [] (let [x 1] ^{:y z} (inc x))"))
    (cider--debug-move-point '(3 2 1))
    (should (string= (thing-at-point 'symbol) "x"))))

(ert-deftest test-debug-move-point-data-reader ()
  (with-temp-buffer
    (clojure-mode)
    (save-excursion (insert "(defn a [] (let [x 1] #bar (inc #foo x))"))
    (cider--debug-move-point '(3 2 1))
    (should (string= (thing-at-point 'symbol) "x"))))

(ert-deftest test-debug-move-point-data-reader-and-metadata ()
  (with-temp-buffer
    (clojure-mode)
    (save-excursion (insert "(defn a [] (let [x 1] #break ^{foo (foo x)} (inc x))"))
    (cider--debug-move-point '(3 2 1))
    (should (string= (thing-at-point 'symbol) "x"))))

(ert-deftest test-cider-connection-buffer-name ()
  (setq-local nrepl-endpoint '("localhost" 1))
  (let ((nrepl-hide-special-buffers nil))
    (should (equal (nrepl-connection-buffer-name) "*nrepl-connection localhost*")))
  (let ((nrepl-hide-special-buffers t))
    (should (equal (nrepl-connection-buffer-name) " *nrepl-connection localhost*"))))

(ert-deftest test-cider-server-buffer-name ()
  (setq-local nrepl-endpoint '("localhost" 1))
  (let ((nrepl-hide-special-buffers nil))
    (should (equal (nrepl-server-buffer-name) "*nrepl-server localhost*")))
  (let ((nrepl-hide-special-buffers t))
    (should (equal (nrepl-server-buffer-name) " *nrepl-server localhost*"))))

(ert-deftest test-cider-repl--banner ()
  (noflet ((pkg-info-version-info (library) "0.11.0")
           (cider--java-version () "1.8.0_31")
           (cider--clojure-version () "1.8.0")
           (cider--nrepl-version () "0.2.12")
           (cider--connection-host (conn) "localhost")
           (cider--connection-port (conn) "54018"))
    (let ((cider-version "0.11.0")
          (cider-codename "Victory"))
      (should (equal (cider-repl--banner)
                     ";; Connected to nREPL server - nrepl://localhost:54018
;; CIDER 0.11.0 (Victory), nREPL 0.2.12
;; Clojure 1.8.0, Java 1.8.0_31
;;     Docs: (doc function-name)
;;           (find-doc part-of-name)
;;   Source: (source function-name)
;;  Javadoc: (javadoc java-object-or-class)
;;     Exit: C-c C-q
;;  Results: Stored in vars *1, *2, *3, an exception in *e;")))))

(ert-deftest test-cider-repl--banner-version-fallback ()
  (noflet ((pkg-info-version-info (library) (error "No package version"))
           (cider--java-version () "1.8.0_31")
           (cider--clojure-version () "1.8.0")
           (cider--nrepl-version () "0.2.12")
           (cider--connection-host (conn) "localhost")
           (cider--connection-port (conn) "54018"))
    (let ((cider-version "0.11.0")
          (cider-codename "Victory"))
      (should (equal (cider-repl--banner)
                     ";; Connected to nREPL server - nrepl://localhost:54018
;; CIDER 0.11.0 (Victory), nREPL 0.2.12
;; Clojure 1.8.0, Java 1.8.0_31
;;     Docs: (doc function-name)
;;           (find-doc part-of-name)
;;   Source: (source function-name)
;;  Javadoc: (javadoc java-object-or-class)
;;     Exit: C-c C-q
;;  Results: Stored in vars *1, *2, *3, an exception in *e;")))))

(ert-deftest test-cider-var-info ()
  (noflet ((cider-nrepl-send-sync-request (list)
                                          '(dict
                                            "arglists" "([] [x] [x & ys])"
                                            "ns" "clojure.core"
                                            "name" "str"
                                            "column" 1
                                            "added" "1.0"
                                            "static" "true"
                                            "doc" "stub"
                                            "line" 504
                                            "file" "jar:file:/clojure-1.5.1.jar!/clojure/core.clj"
                                            "tag" "class java.lang.String"
                                            "status" ("done")))
           (cider-ensure-op-supported (op) t)
           (cider-current-session () nil)
           (cider-current-ns () "user"))
    (should (equal "stub" (nrepl-dict-get (cider-var-info "str") "doc")))
    (should (null (cider-var-info "")))))

(ert-deftest test-nrepl-dict-get ()
  (let ((var-info '(dict "doc" "var doc" "arglists" "var arglists")))
    (should (equal (nrepl-dict-get var-info "doc") "var doc"))
    (should (equal (nrepl-dict-get var-info "arglists") "var arglists"))))

(defmacro cider-test-with-buffers (buffer-names &rest body)
  (let ((create (lambda (b) (list b `(generate-new-buffer " *temp*")))))
    `(let (,@(mapcar create buffer-names))
       (unwind-protect
           ,@body
         (mapc 'kill-buffer (list ,@buffer-names))))))

(ert-deftest test-cider-make-connection-default ()
  (let ((connections (cider-connections)))
    (cider-test-with-buffers
     (a b)
     (should (get-buffer a))
     (should (get-buffer b))
     ;; Add one connection
     (cider-make-connection-default a)
     (should (equal (cons a connections)
                    (cider-connections)))
     (should (equal a (cider-default-connection)))
     ;; Add second connection
     (cider-make-connection-default b)
     (should (equal (append (list b a) connections)
                    (cider-connections)))
     (should (equal b (cider-default-connection)))
     ;; Re-add first connection
     (cider-make-connection-default a)
     (should (equal (append (list a b) connections)
                    (cider-connections)))
     (should (equal a (cider-default-connection))))))

(ert-deftest test-cider-connections ()
  (let ((connections (cider-connections)))
    (cider-test-with-buffers
     (a b)
     (cider-make-connection-default a)
     (cider-make-connection-default b)
     ;; killing a buffer should see it purged from the connection list
     (kill-buffer a)
     (should (equal (cons b connections)
                    (cider-connections)))
     (should (equal b (cider-default-connection))))))

(ert-deftest test-cider-rotate-connecton-buffer ()
  (noflet ((nrepl--connection-info (connection-buffer-name)))
    (cider-test-with-buffers
     (a b c)
     (let ((cider-connections (list a b c)))
       (noflet ((cider--java-version () "")
                (cider--clojure-version () "")
                (cider--nrepl-version () ""))
         (should (equal a (cider-default-connection)))
         (cider-rotate-default-connection)
         (should (equal b (cider-default-connection)))
         (cider-rotate-default-connection)
         (should (equal c (cider-default-connection)))
         (cider-rotate-default-connection)
         (should (equal a (cider-default-connection))))))))

(ert-deftest test-cider--connection-info ()
  (with-temp-buffer
    (noflet ((cider--java-version () "1.7")
             (cider--clojure-version () "1.5.1")
             (cider--nrepl-version () "0.2.1"))
      (setq-local nrepl-endpoint '("localhost" 4005))
      (setq-local nrepl-project-dir "proj")
      (should (string= (cider--connection-info (current-buffer))
                       "CLJ proj@localhost:4005 (Java 1.7, Clojure 1.5.1, nREPL 0.2.1)")))))

(ert-deftest test-cider-connection-info-no-project ()
  (with-temp-buffer
    (noflet ((cider--java-version () "1.7")
             (cider--clojure-version () "1.5.1")
             (cider--nrepl-version () "0.2.1"))
      (setq-local nrepl-endpoint '("localhost" 4005))
      (should (string= (cider--connection-info (current-buffer))
                       "CLJ <no project>@localhost:4005 (Java 1.7, Clojure 1.5.1, nREPL 0.2.1)")))))

(ert-deftest test-cider--close-connection-buffer ()
  (let ((connections (cider-connections)))
    (cider-test-with-buffers
     (a b)
     (cider-make-connection-default a)
     (cider-make-connection-default b)
     ;; closing a buffer should see it removed from the connection list
     (cider--close-connection-buffer a)
     (should (not (buffer-live-p a)))
     (should (equal (cons b connections)
                    (cider-connections)))
     (should (equal b (cider-default-connection))))))

(ert-deftest test-cider--var-namespace ()
  (should (string= (cider--var-namespace "#'a/var-two") "a"))
  (should (string= (cider--var-namespace "#'a-two/var") "a-two"))
  (should (string= (cider--var-namespace "#'a.two-three.b/var-c") "a.two-three.b"))
  (should (string= (cider--var-namespace "a/var-two") "a"))
  (should (string= (cider--var-namespace "a-two/var") "a-two"))
  (should (string= (cider--var-namespace "a.two-three.b/var-c") "a.two-three.b")))

(ert-deftest test-cider-connection-type-for-buffer ()
  (with-temp-buffer
    (clojurescript-mode)
    (should (string= (cider-connection-type-for-buffer) "cljs")))
  (with-temp-buffer
    (clojure-mode)
    (should (string= (cider-connection-type-for-buffer) "clj")))
  (with-temp-buffer
    (let ((cider-repl-type nil))
      (should (string= (cider-connection-type-for-buffer) "clj")))
    (let ((cider-repl-type "clj"))
      (should (string= (cider-connection-type-for-buffer) "clj")))
    (let ((cider-repl-type "cljs"))
      (should (string= (cider-connection-type-for-buffer) "cljs")))))


;;; response handling
(ert-deftest test-cider-dbind-response ()
  (should (equal '("2" "39f630b9-9545-4ea0-860e-9846681d0741" ("done"))
                 (nrepl-dbind-response
                     '(dict
                       "id" "2"
                       "new-session" "531acc73-bce4-4e77-a82b-537beeb581e9"
                       "session" "39f630b9-9545-4ea0-860e-9846681d0741"
                       "status" ("done"))
                     (id session status)
                   (list id session status)))))

(ert-deftest cider-nrepl-send-unhandled-request ()
  (noflet ((process-send-string (process string) nil))
    (with-temp-buffer
      (setq-local nrepl-pending-requests (make-hash-table :test 'equal))
      (setq-local nrepl-completed-requests (make-hash-table :test 'equal))
      (let* ((cider-connections (list (current-buffer)))
             (id (cider-nrepl-send-unhandled-request '("op" "t" "extra" "me"))))
        (should (not (gethash id nrepl-pending-requests)))
        (should (gethash id nrepl-completed-requests))
        (should (eq (gethash id nrepl-completed-requests)
                    #'ignore)))))
  (ignore-errors
    (kill-buffer "*nrepl-messages*")))


;;; connection browser

(ert-deftest test-cider-connections-buffer ()
  (with-temp-buffer
    (rename-buffer "*cider-repl test1*")
    (let ((b1 (current-buffer)))
      (setq-local nrepl-endpoint '("localhost" 4005))
      (setq-local nrepl-project-dir "proj")
      (with-temp-buffer
        (rename-buffer "*cider-repl test2*")
        (let ((b2 (current-buffer)))
          (setq-local nrepl-endpoint '("123.123.123.123" 4006))
          (let ((cider-connections (list b1 b2)))
            (cider-connection-browser)
            (with-current-buffer "*cider-connections*"
              (should (equal "  REPL                           Host             Port    Project          Type

* *cider-repl test1*             localhost         4005   proj             Clojure
  *cider-repl test2*             123.123.123.123   4006   -                Clojure\n\n"
                             (buffer-string)))
              (goto-line 4)         ; somewhere in the second connection listed
              (cider-connections-make-default)
              (should (equal b2 (car cider-connections)))
              (message "%s" (cider-connections))
              (should (equal "  REPL                           Host             Port    Project          Type

  *cider-repl test1*             localhost         4005   proj             Clojure
* *cider-repl test2*             123.123.123.123   4006   -                Clojure\n\n"
                             (buffer-string)))
              (goto-line 4)         ; somewhere in the second connection listed
              (cider-connections-close-connection)
              (should (equal (list b1) cider-connections))
              (should (equal "  REPL                           Host             Port    Project          Type

* *cider-repl test1*             localhost         4005   proj             Clojure\n\n"
                             (buffer-string)))
              (cider-connections-goto-connection)
              (should (equal b1 (current-buffer)))
              (kill-buffer "*cider-connections*"))))))))

(ert-deftest test-nrepl-format-buffer-name-template ()
  (should (equal "*template designation-foo*"
                 (nrepl-format-buffer-name-template "*template%s*" "designation-foo"))))

(ert-deftest test-nrepl-format-buffer-name-template-use-separator ()
  (let ((nrepl-buffer-name-separator "_"))
    (should (equal "*template_designation-foo*"
                   (nrepl-format-buffer-name-template "*template%s*" "designation-foo")))))

(ert-deftest test-nrepl-format-buffer-name-template-handle-nil-designation ()
  (should (equal "*template*"
                 (nrepl-format-buffer-name-template "*template%s*" nil))))

(ert-deftest test-nrepl-format-buffer-name-template-handle-empty-designation ()
  (should (equal "*template*"
                 (nrepl-format-buffer-name-template "*template%s*" ""))))

(ert-deftest test-nrepl-make-buffer-name ()
  (with-temp-buffer
    (setq-local nrepl-endpoint '("localhost" 1))
    (let ((b1 (current-buffer)))
      (should
       (equal (nrepl-make-buffer-name "*buff-name%s*") "*buff-name localhost*")))))

(ert-deftest test-nrepl-make-buffer-name-based-on-project ()
  (with-temp-buffer
    (let ((b1 (current-buffer)))
      (setq-local nrepl-project-dir "proj")
      (should
       (equal (nrepl-make-buffer-name "*buff-name%s*") "*buff-name proj*")))))

(ert-deftest test-nrepl-buffer-name-separator ()
  (with-temp-buffer
    (let ((b1 (current-buffer)))
      (setq-local nrepl-project-dir "proj")
      (let ((nrepl-buffer-name-separator "X"))
        (should
         (equal (nrepl-make-buffer-name "*buff-name%s*") "*buff-nameXproj*"))))))

(ert-deftest test-nrepl-buffer-name-show-port-t ()
  (with-temp-buffer
    (setq-local nrepl-buffer-name-show-port t)
    (setq-local nrepl-endpoint '("localhost" 4009))
    (should
     (equal (nrepl-make-buffer-name "*buff-name%s*") "*buff-name localhost:4009*"))))

(ert-deftest test-nrepl-buffer-name-show-port-nil ()
  (with-temp-buffer
    (setq-local nrepl-buffer-name-show-port nil)
    (setq-local nrepl-endpoint '("localhost" 4009))
    (should
     (equal (nrepl-make-buffer-name "*buff-name%s*") "*buff-name localhost*"))))

(ert-deftest test-nrepl-buffer-name-based-on-project-and-port ()
  (with-temp-buffer
    (setq-local nrepl-buffer-name-show-port t)
    (setq-local nrepl-project-dir "proj")
    (setq-local nrepl-endpoint '("localhost" 4009))
    (should
     (equal (nrepl-make-buffer-name "*buff-name%s*") "*buff-name proj:4009*"))))

(ert-deftest test-nrepl-make-buffer-name-two-buffers-same-project ()
  (with-temp-buffer
    (setq-local nrepl-project-dir "proj")
    (let* ((cider-new-buffer (nrepl-make-buffer-name "*buff-name%s*")))
      (get-buffer-create cider-new-buffer)
      (should
       (equal cider-new-buffer "*buff-name proj*"))
      (with-temp-buffer
        (setq-local nrepl-project-dir "proj")
        (should
         (equal (nrepl-make-buffer-name "*buff-name%s*") "*buff-name proj*<2>"))
        (kill-buffer cider-new-buffer)))))

(ert-deftest test-nrepl-buffer-name-duplicate-proj-port ()
  (with-temp-buffer
    (setq-local nrepl-buffer-name-show-port t)
    (setq-local nrepl-project-dir "proj")
    (setq-local nrepl-endpoint '("localhost" 4009))
    (let* ((cider-new-buffer (nrepl-make-buffer-name "*buff-name%s*")))
      (get-buffer-create cider-new-buffer)
      (should
       (equal cider-new-buffer "*buff-name proj:4009*"))
      (with-temp-buffer
        (setq-local nrepl-buffer-name-show-port t)
        (setq-local nrepl-project-dir "proj")
        (setq-local nrepl-endpoint '("localhost" 4009))
        (should
         (equal (nrepl-make-buffer-name "*buff-name%s*") "*buff-name proj:4009*<2>"))
        (kill-buffer cider-new-buffer)))))

(ert-deftest test-cider-clojure-buffer-name ()
  (with-temp-buffer
    (setq-local nrepl-endpoint '("localhost" 1))
    (let ((b1 (current-buffer)))
      (let ((cider-connections (list b1)))
        (should
         (equal (cider-repl-buffer-name) "*cider-repl localhost*"))))))

(ert-deftest test-cider-clojure-buffer-name-w/project ()
  (with-temp-buffer
    (let ((b1 (current-buffer)))
      (let ((cider-connections (list b1))
            (nrepl-project-dir "/a/test/directory/project"))
        (should
         (equal (cider-repl-buffer-name) "*cider-repl project*"))))))

(ert-deftest test-cider--find-rest-args-position ()
  (should (= (cider--find-rest-args-position ["fmt" "&" "arg"]) 1))
  (should (equal (cider--find-rest-args-position ["fmt" "arg"]) nil)))

(ert-deftest test-cider-change-buffers-designation ()
  (with-temp-buffer
    (let ((server-buffer (current-buffer)))
      (with-temp-buffer
        (let* ((connection-buffer (current-buffer))
               (cider-connections (list connection-buffer)))
          (setq-local nrepl-server-buffer server-buffer)
          (noflet ((read-string (dontcare) "bob"))
            (cider-change-buffers-designation)
            (should (equal "*cider-repl bob*" (buffer-name connection-buffer)))
            (should (equal "*nrepl-server bob*" (buffer-name server-buffer))))
          (with-current-buffer connection-buffer
            (should (equal "*cider-repl bob*" (buffer-name)))))))))

(ert-deftest test-cider-change-buffers-designation-to-existing-designation-has-no-effect ()
  (with-temp-buffer
    (let ((server-buffer (current-buffer)))
      (with-temp-buffer
        (let* ((connection-buffer (current-buffer))
               (cider-connections (list connection-buffer)))
          (with-temp-buffer
            (rename-buffer "*cider-repl bob*") ;; Make a buffer that already has the designation
            (with-temp-buffer
              (let* ((repl-buffer (current-buffer))
                     (before-repl-buffer-name (buffer-name repl-buffer))
                     (before-connection-buffer-name (buffer-name connection-buffer))
                     (before-server-buffer-name (buffer-name server-buffer)))

                (with-current-buffer connection-buffer
                  (setq-local nrepl-repl-buffer repl-buffer)
                  (setq-local nrepl-server-buffer server-buffer))

                (noflet ((read-string (dontcare) "bob"))
                  (should-error
                   (cider-change-buffers-designation))
                  (should (equal before-repl-buffer-name (buffer-name repl-buffer)))
                  (should (equal before-connection-buffer-name (buffer-name connection-buffer)))
                  (should (equal before-server-buffer-name (buffer-name server-buffer))))))))))))

(ert-deftest cider-extract-designation-from-current-repl-buffer ()
  (with-temp-buffer
    (let* ((cider-connections (list (current-buffer))))
      (rename-buffer "*cider-repl bob*")
      (with-temp-buffer
        (should (equal "bob" (cider-extract-designation-from-current-repl-buffer)))
        (rename-buffer "*cider-repl apa*")
        (push (current-buffer) cider-connections)
        (should (equal "apa" (cider-extract-designation-from-current-repl-buffer)))
        (setq-local cider-connections (list (current-buffer)))
        (should (equal "apa" (cider-extract-designation-from-current-repl-buffer)))))))

(ert-deftest cider-extract-designation-from-current-repl-buffer-no-designation ()
  (with-temp-buffer
    (let* ((connection-buffer (current-buffer))
           (cider-connections (list connection-buffer)))
      (with-temp-buffer
        (let ((repl-buffer (current-buffer)))
          (rename-buffer "*cider-repl*")
          (with-temp-buffer
            (with-current-buffer connection-buffer
              (setq-local nrepl-repl-buffer repl-buffer))
            (should (equal "<no designation>" (cider-extract-designation-from-current-repl-buffer)))))))))


(ert-deftest cider-symbol-at-point-look-back ()
  (with-temp-buffer
    (insert "some-symbol    ")
    (should (not (cider-symbol-at-point)))
    (should (string= (cider-symbol-at-point 'look-back) "some-symbol"))))

(ert-deftest cider-symbol-at-point-no-symbol ()
  (noflet ((thing-at-point (thing) nil))
    (should (not (cider-symbol-at-point)))))

(ert-deftest cider-symbol-at-point-at-repl-prompt ()
  (noflet ((thing-at-point (thing) (propertize "user>" 'field 'cider-repl-prompt)))
    (should (not (cider-symbol-at-point))))
  (noflet ((thing-at-point (thing) (propertize "boogie>" 'field 'cider-repl-prompt)))
    (should (not (cider-symbol-at-point))))
  (noflet ((thing-at-point (thing) "boogie>"))
    (should (string= (cider-symbol-at-point) "boogie>"))))

(ert-deftest cider-sexp-at-point ()
  (with-temp-buffer
    (clojure-mode)
    (insert "a\n\n,")
    (save-excursion (insert "(defn ...)\n\nb"))
    (should (string= (cider-sexp-at-point) "(defn ...)"))
    (delete-char -1)
    (insert "@")
    (should (string= (cider-sexp-at-point) "(defn ...)"))
    (delete-char -1)
    (insert "'")
    (should (string= (cider-sexp-at-point) "(defn ...)"))
    (should (equal (cider-sexp-at-point 'bounds) '(5 15)))))

(ert-deftest cider-defun-at-point ()
  (with-temp-buffer
    (clojure-mode)
    (insert "a\n\n(defn ...)")
    (save-excursion (insert "\n\nb"))
    (should (string= (cider-defun-at-point) "(defn ...)\n"))
    (forward-sexp -1)
    (should (string= (cider-defun-at-point) "(defn ...)\n"))
    (should (equal (cider-defun-at-point 'bounds) '(4 15)))
    (forward-sexp 1)
    (should (equal (cider-defun-at-point 'bounds) '(4 15)))))

(ert-deftest cider-repl-prompt-function ()
  (should (equal (cider-repl-prompt-default "some.pretty.long.namespace.name")
                 "some.pretty.long.namespace.name> "))
  (should (equal (cider-repl-prompt-lastname "some.pretty.long.namespace.name")
                 "name> "))
  (should (equal (cider-repl-prompt-abbreviated "some.pretty.long.namespace.name")
                 "s.p.l.n.name> ")))

(ert-deftest test-cider-repl--emit-output-at-pos-with-ansi-code ()
  (with-temp-buffer
    (let* ((ansi-color-names-vector ["black" "red3" "green3" "yellow3" "blue2" "magenta3" "cyan3" "gray90"])
           (ansi-color-map (ansi-color-make-color-map)))
      (cider-repl-reset-markers)

      (cider-repl--emit-output-at-pos (current-buffer) "a" 'cider-repl-stdout-face (point))
      (cider-repl--emit-output-at-pos (current-buffer) "b" 'cider-repl-stdout-face (point))
      (cider-repl--emit-output-at-pos (current-buffer) "c" 'cider-repl-stdout-face (point))
      (cider-repl--emit-output-at-pos (current-buffer) "d" 'cider-repl-stdout-face (point))

      (should (string= (buffer-string) "a\nb\nc\nd\n"))
      (should (equal (get-text-property 1 'font-lock-face) '(foreground-color . "black")))
      (should (equal (get-text-property 3 'font-lock-face) 'cider-repl-stdout-face))
      (should (equal (get-text-property 5 'font-lock-face) '(foreground-color . "red3")))
      (should (equal (get-text-property 7 'font-lock-face) '(foreground-color . "red3"))))))

(ert-deftest test-cider--url-to-file ()
  (should (equal "/space test" (cider--url-to-file "file:/space%20test")))
  (should (equal "C:/space test" (cider--url-to-file "file:/C:/space%20test"))))

(ert-deftest test-cider-eldoc-beginning-of-sexp ()
  (with-temp-buffer
    (save-excursion
      (insert "(a (b b) (c c) d)"))
    (search-forward "d")

    (let ((cider-eldoc-max-num-sexps-to-skip nil))
      (save-excursion
        (should (eq (cider-eldoc-beginning-of-sexp) 4))
        (should (eq (point) 2))))

    (let ((cider-eldoc-max-num-sexps-to-skip 4))
      (save-excursion
        (should (eq (cider-eldoc-beginning-of-sexp) 4))
        (should (eq (point) 2))))

    (let ((cider-eldoc-max-num-sexps-to-skip 3))
      (save-excursion
        (should (eq (cider-eldoc-beginning-of-sexp) nil))
        (should (eq (point) 2))))

    (let ((cider-eldoc-max-num-sexps-to-skip 2))
      (save-excursion
        (should (eq (cider-eldoc-beginning-of-sexp) nil))
        (should (eq (point) 4))))))



;;; Cygwin tests

(ert-deftest cider-translate-filenames ()
  (let ((windows-file-name "C:/foo/bar")
        (unix-file-name "/cygdrive/c/foo/bar"))
    (if (eq system-type 'cygwin)
        (and (should (equal (funcall cider-from-nrepl-filename-function windows-file-name) unix-file-name))
             (should (equal (funcall cider-to-nrepl-filename-function unix-file-name) windows-file-name)))
      (and (should (eq (funcall cider-from-nrepl-filename-function unix-file-name) unix-file-name))
           (should (eq (funcall cider-to-nrepl-filename-function unix-file-name) unix-file-name))))))


;;; Util tests

(ert-deftest cider-namespace-qualified-p-test ()
  (should (cider-namespace-qualified-p "a/a"))
  (should (cider-namespace-qualified-p "a.a/a"))
  (should (cider-namespace-qualified-p "a-a/a"))
  (should (cider-namespace-qualified-p "a.a-a/a-a"))
  (should (not (cider-namespace-qualified-p "/")))
  (should (not (cider-namespace-qualified-p "/a"))))

(ert-deftest cider--kw-to-symbol ()
  (should (equal (cider--kw-to-symbol "symbol") "symbol"))
  (should (equal (cider--kw-to-symbol ":clj.core/str") "clj.core/str"))
  (should (equal (cider--kw-to-symbol "::keyword") "keyword"))
  (should (equal (cider--kw-to-symbol nil) nil)))

(ert-deftest cider--deep-vector-to-list ()
  (should (equal (cider--deep-vector-to-list '[1 2 3]) '(1 2 3)))
  (should (equal (cider--deep-vector-to-list '(1 2 3)) '(1 2 3)))
  (should (equal (cider--deep-vector-to-list '[[1] [2] [[3]]]) '((1) (2) ((3)))))
  (should (equal (cider--deep-vector-to-list '(1 [2] [([3])])) '(1 (2) (((3))))))
  (should (equal (cider--deep-vector-to-list 'bug) 'bug))
  (should (equal (cider--deep-vector-to-list '[bug]) '(bug)))
  (should (equal (cider--deep-vector-to-list '(bug)) '(bug))))

(ert-deftest cider-project-name ()
  (should (equal (cider-project-name nil) "-"))
  (should (equal (cider-project-name "") "-"))
  (should (equal (cider-project-name "path/to/project") "project"))
  (should (equal (cider-project-name "path/to/project/") "project")))

(ert-deftest cider-inject-jack-in-dependencies ()
  (let ((cider-jack-in-dependencies '(("org.clojure/tools.nrepl" "0.2.12")))
        (cider-jack-in-nrepl-middlewares '("cider.nrepl/cider-middleware"))
        (cider-jack-in-lein-plugins '(("cider/cider-nrepl" "0.10.0-SNAPSHOT"))))
    (should (string= (cider-inject-jack-in-dependencies "repl :headless" "lein")
                     "update-in :dependencies conj \\[org.clojure/tools.nrepl\\ \\\"0.2.12\\\"\\] -- update-in :plugins conj \\[cider/cider-nrepl\\ \\\"0.10.0-SNAPSHOT\\\"\\] -- repl :headless"))
    (should (string= (cider-inject-jack-in-dependencies "repl -s wait" "boot")
                     "-d org.clojure/tools.nrepl\\:0.2.12 -d cider/cider-nrepl\\:0.10.0-SNAPSHOT repl -m cider.nrepl/cider-middleware -s wait"))
    (should (string= (cider-inject-jack-in-dependencies "--no-daemon clojureRepl" "gradle") "--no-daemon clojureRepl")))
  (let ((cider-jack-in-dependencies '(("org.clojure/tools.nrepl" "0.2.12")))
        (cider-jack-in-nrepl-middlewares '("cider.nrepl/cider-middleware"))
        (cider-jack-in-lein-plugins '(("cider/cider-nrepl" "0.10.2"))))
    (should (string= (cider-inject-jack-in-dependencies "repl :headless" "lein")
                     "update-in :dependencies conj \\[org.clojure/tools.nrepl\\ \\\"0.2.12\\\"\\] -- update-in :plugins conj \\[cider/cider-nrepl\\ \\\"0.10.2\\\"\\] -- repl :headless"))
    (should (string= (cider-inject-jack-in-dependencies "repl -s wait" "boot")
                     "-d org.clojure/tools.nrepl\\:0.2.12 -d cider/cider-nrepl\\:0.10.2 repl -m cider.nrepl/cider-middleware -s wait"))
    (should (string= (cider-inject-jack-in-dependencies "--no-daemon clojureRepl" "gradle") "--no-daemon clojureRepl"))))

(ert-deftest cider-inject-jack-in-dependencies-add-refactor-nrepl ()
  (let ((cider-jack-in-lein-plugins '(("refactor-nrepl" "2.0.0") ("cider/cider-nrepl" "0.11.0")))
        (cider-jack-in-nrepl-middlewares '("refactor-nrepl.middleware/wrap-refactor" "cider.nrepl/cider-middleware")))
    (should (string= (cider-inject-jack-in-dependencies "repl :headless" "lein")
                     "update-in :dependencies conj \\[org.clojure/tools.nrepl\\ \\\"0.2.12\\\"\\] -- update-in :plugins conj \\[refactor-nrepl\\ \\\"2.0.0\\\"\\] -- update-in :plugins conj \\[cider/cider-nrepl\\ \\\"0.11.0\\\"\\] -- repl :headless"))
    (should (string= (cider-inject-jack-in-dependencies "repl -s wait" "boot")
                     "-d org.clojure/tools.nrepl\\:0.2.12 -d refactor-nrepl\\:2.0.0 -d cider/cider-nrepl\\:0.11.0 repl -m refactor-nrepl.middleware/wrap-refactor -m cider.nrepl/cider-middleware -s wait"))))

(ert-deftest cider-manual-url ()
  (let ((cider-version "0.11.0"))
    (should (string= (cider-manual-url) "https://github.com/clojure-emacs/cider/blob/v0.11.0/README.md")))
  (let ((cider-version "0.11.0-snapshot"))
    (should (string= (cider-manual-url) "https://github.com/clojure-emacs/cider/blob/master/README.md"))))

(ert-deftest cider-refcard-url ()
  (let ((cider-version "0.11.0"))
    (should (string= (cider-refcard-url) "https://github.com/clojure-emacs/cider/raw/v0.11.0/doc/cider-refcard.pdf")))
  (let ((cider-version "0.11.0-snapshot"))
    (should (string= (cider-refcard-url) "https://github.com/clojure-emacs/cider/raw/master/doc/cider-refcard.pdf"))))

(ert-deftest cider-ensure-connected ()
  (noflet ((cider-connected-p () t))
    (should-not (cider-ensure-connected)))
  (noflet ((cider-connected-p () nil))
    (should-error (cider-ensure-connected) :type 'user-error)))

(ert-deftest cider-ensure-op-supported ()
  (noflet ((cider-nrepl-op-supported-p (op) t))
    (should-not (cider-ensure-op-supported "foo")))
  (noflet ((cider-nrepl-op-supported-p (op) nil))
    (should-error (cider-ensure-op-supported "foo") :type 'user-error)))

(ert-deftest cider-refresh-not-connected ()
  (noflet ((cider-connected-p () nil))
    (should-error (cider-refresh) :type 'user-error)))

(ert-deftest cider-quit-not-connected ()
  (noflet ((cider-connected-p () nil))
    (should-error (cider-quit) :type 'user-error)))

(ert-deftest cider-restart-not-connected ()
  (noflet ((cider-connected-p () nil))
    (should-error (cider-restart) :type 'user-error)))

(ert-deftest cider-find-ns-not-connected ()
  (noflet ((cider-connected-p () nil))
    (should-error (cider-find-ns) :type 'user-error)))

(ert-deftest cider-find-ns-unsupported-op ()
  (noflet ((cider-ensure-op-supported (op) nil))
    (should-error (cider-find-ns) :type 'user-error)))

(ert-deftest cider-expected-ns ()
  (noflet ((cider-ensure-connected () t)
           (cider-sync-request:classpath () '("/a" "/b" "/c" "/c/inner")))
    (should (string= (cider-expected-ns "/a/foo/bar/baz_utils.clj")
                     "foo.bar.baz-utils"))
    (should (string= (cider-expected-ns "/b/foo.clj")
                     "foo"))
    (should (string= (cider-expected-ns "/not/in/classpath.clj")
                     nil))
    (should (string= (cider-expected-ns "/c/inner/foo/bar.clj")
                     ;; NOT inner.foo.bar
                     "foo.bar"))
    (should (string= (cider-expected-ns "/c/foo/bar/baz")
                     "foo.bar.baz"))))