-
-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathclojure-mode-indentation-test.el
839 lines (698 loc) · 18.1 KB
/
clojure-mode-indentation-test.el
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
;;; clojure-mode-indentation-test.el --- Clojure Mode: indentation tests -*- lexical-binding: t; -*-
;; Copyright (C) 2015-2021 Bozhidar Batsov <[email protected]>
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; The unit test suite of Clojure Mode
;;; Code:
(require 'clojure-mode)
(require 'cl-lib)
(require 'buttercup)
(require 's nil t) ;Don't burp if it's missing during compilation.
(require 'test-helper "test/utils/test-helper")
(defmacro when-indenting-with-point-it (description before after)
"Return a buttercup spec.
Check whether the swift indentation command will correctly change the buffer.
Will also check whether point is moved to the expected position.
BEFORE is the buffer string before indenting, where a pipe (|) represents
point.
AFTER is the expected buffer string after indenting, where a pipe (|)
represents the expected position of point.
DESCRIPTION is a string with the description of the spec."
(declare (indent 1))
`(it ,description
(let* ((after ,after)
(clojure-indent-style 'always-align)
(expected-cursor-pos (1+ (s-index-of "|" after)))
(expected-state (delete ?| after)))
(with-clojure-buffer ,before
(goto-char (point-min))
(search-forward "|")
(delete-char -1)
(font-lock-ensure)
(indent-according-to-mode)
(expect (buffer-string) :to-equal expected-state)
(expect (point) :to-equal expected-cursor-pos)))))
;; Backtracking indent
(defmacro when-indenting-it (description &optional style &rest forms)
"Return a buttercup spec.
Check that all FORMS correspond to properly indented sexps.
STYLE allows overriding the default clojure-indent-style 'always-align.
DESCRIPTION is a string with the description of the spec."
(declare (indent 1))
(when (stringp style)
(setq forms (cons style forms))
(setq style '(quote always-align)))
`(it ,description
(progn
,@(mapcar (lambda (form)
`(with-temp-buffer
(clojure-mode)
(insert "\n" ,form);,(replace-regexp-in-string "\n +" "\n " form))
(let ((clojure-indent-style ,style))
(indent-region (point-min) (point-max)))
(expect (buffer-string) :to-equal ,(concat "\n" form))))
forms))))
(defmacro when-aligning-it (description &rest forms)
"Return a buttercup spec.
Check that all FORMS correspond to properly indented sexps.
DESCRIPTION is a string with the description of the spec."
(declare (indent defun))
`(it ,description
(let ((clojure-align-forms-automatically t)
(clojure-align-reader-conditionals t))
,@(mapcar (lambda (form)
`(with-temp-buffer
(clojure-mode)
(insert "\n" ,(replace-regexp-in-string " +" " " form))
(indent-region (point-min) (point-max))
(should (equal (buffer-substring-no-properties (point-min) (point-max))
,(concat "\n" form)))))
forms))
(let ((clojure-align-forms-automatically nil))
,@(mapcar (lambda (form)
`(with-temp-buffer
(clojure-mode)
(insert "\n" ,(replace-regexp-in-string " +" " " form))
;; This is to check that we did NOT align anything. Run
;; `indent-region' and then check that no extra spaces
;; where inserted besides the start of the line.
(indent-region (point-min) (point-max))
(goto-char (point-min))
(should-not (search-forward-regexp "\\([^\s\n]\\) +" nil 'noerror))))
forms))))
;; Provide font locking for easier test editing.
(font-lock-add-keywords
'emacs-lisp-mode
`((,(rx "(" (group "when-indenting-with-point-it") eow)
(1 font-lock-keyword-face))
(,(rx "("
(group "when-indenting-with-point-it") (+ space)
(group bow (+ (not space)) eow)
)
(1 font-lock-keyword-face)
(2 font-lock-function-name-face))))
(describe "indentation"
(it "should not hang on end of buffer"
(with-clojure-buffer "(let [a b]"
(goto-char (point-max))
(expect
(with-timeout (2)
(newline-and-indent)
t))))
(when-indenting-with-point-it "should have no indentation at top level"
"|x"
"|x")
(when-indenting-with-point-it "should indent cond"
"
(cond
|x)"
"
(cond
|x)")
(when-indenting-with-point-it "should indent cond-> with a namespaced map"
"
(cond-> #:a{:b 1}
|x 1)"
"
(cond-> #:a{:b 1}
|x 1)")
(when-indenting-with-point-it "should indent cond-> with a namespaced map 2"
"
(cond-> #::a{:b 1}
|x 1)"
"
(cond-> #::a{:b 1}
|x 1)")
(when-indenting-with-point-it "should indent threading macro with expression on first line"
"
(->> expr
|ala)"
"
(->> expr
|ala)")
(when-indenting-with-point-it "should indent threading macro with expression on second line"
"
(->>
|expr)"
"
(->>
|expr)")
(when-indenting-with-point-it "should not indent for def string"
"(def foo \"hello|\")"
"(def foo \"hello|\")")
(when-indenting-with-point-it "should indent doc strings"
"
(defn some-fn
|\"some doc string\")"
"
(defn some-fn
|\"some doc string\")")
(when-indenting-with-point-it "should not indent doc strings when correct indent already specified"
"
(defn some-fn
|\"some doc string\")"
"
(defn some-fn
|\"some doc string\")")
(when-indenting-with-point-it "should handle doc strings with additional indent specified"
"
(defn some-fn
|\"some doc string
- some note\")"
"
(defn some-fn
|\"some doc string
- some note\")")
(describe "specify different indentation for symbol with some ns prefix"
(put-clojure-indent 'bala 0)
(put-clojure-indent 'ala/bala 1)
(when-indenting-with-point-it "should handle a symbol without ns"
"
(bala
|one)"
"
(bala
|one)")
(when-indenting-with-point-it "should handle a symbol with ns"
"
(ala/bala top
|one)"
"
(ala/bala top
|one)"))
(describe "specify an indentation for symbol"
(put-clojure-indent 'cala 1)
(when-indenting-with-point-it "should handle a symbol with ns"
"
(cala top
|one)"
"
(cala top
|one)")
(when-indenting-with-point-it "should handle special arguments"
"
(cala
|top
one)"
"
(cala
|top
one)"))
(describe "should respect special argument indentation"
:var (clojure-special-arg-indent-factor)
(before-each
(setq clojure-special-arg-indent-factor 1))
(after-each
(setq clojure-special-arg-indent-factor 2))
(put-clojure-indent 'cala 1)
(when-indenting-with-point-it "should handle a symbol with ns"
"
(cala top
|one)"
"
(cala top
|one)")
(when-indenting-with-point-it "should handle special arguments"
"
(cala
|top
one)"
"
(cala
|top
one)"))
(describe "we can pass a lambda to explicitly set the column"
(put-clojure-indent 'arsymbol (lambda (_indent-point _state) 0))
(when-indenting-with-point-it "should handle a symbol with lambda"
"
(arsymbol
|one)"
"
(arsymbol
|one)"))
(when-indenting-with-point-it "should indent a form with metadata"
"
(ns ^:doc app.core
|(:gen-class))"
"
(ns ^:doc app.core
|(:gen-class))")
(when-indenting-with-point-it "should handle multiline sexps"
"
[[
2] a
|x]"
"
[[
2] a
|x]")
(when-indenting-with-point-it "should indent reader conditionals"
"
#?(:clj :foo
|:cljs :bar)"
"
#?(:clj :foo
|:cljs :bar)")
(when-indenting-with-point-it "should handle backtracking with aliases"
"
(clojure.core/letfn [(twice [x]
|(* x 2))]
:a)"
"
(clojure.core/letfn [(twice [x]
|(* x 2))]
:a)")
(when-indenting-with-point-it "should handle fixed-normal-indent"
"
(cond
(or 1
2) 3
|:else 4)"
"
(cond
(or 1
2) 3
|:else 4)")
(when-indenting-with-point-it "should handle fixed-normal-indent-2"
"
(fact {:spec-type
:charnock-column-id} #{\"charnock\"}
|{:spec-type
:charnock-column-id} #{\"current_charnock\"})"
"
(fact {:spec-type
:charnock-column-id} #{\"charnock\"}
|{:spec-type
:charnock-column-id} #{\"current_charnock\"})")
(when-indenting-it "closing-paren"
"
(ns ca
(:gen-class)
)")
(when-indenting-it "default-is-not-a-define"
"
(default a
b
b)"
"
(some.namespace/default a
b
b)")
(when-indenting-it "should handle extend-type with multiarity"
"
(extend-type Banana
Fruit
(subtotal
([item]
(* 158 (:qty item)))
([item a]
(* a (:qty item)))))"
"
(extend-protocol Banana
Fruit
(subtotal
([item]
(* 158 (:qty item)))
([item a]
(* a (:qty item)))))")
(when-indenting-it "should handle deftype with multiarity"
"
(deftype Banana []
Fruit
(subtotal
([item]
(* 158 (:qty item)))
([item a]
(* a (:qty item)))))")
(when-indenting-it "should handle defprotocol"
"
(defprotocol IFoo
(foo [this]
\"Why is this over here?\")
(foo-2
[this]
\"Why is this over here?\"))")
(when-indenting-it "should handle definterface"
"
(definterface IFoo
(foo [this]
\"Why is this over here?\")
(foo-2
[this]
\"Why is this over here?\"))")
(when-indenting-it "should handle specify"
"
(specify obj
ISwap
(-swap!
([this f] (reset! this (f @this)))
([this f a] (reset! this (f @this a)))
([this f a b] (reset! this (f @this a b)))
([this f a b xs] (reset! this (apply f @this a b xs)))))")
(when-indenting-it "should handle specify!"
"
(specify! obj
ISwap
(-swap!
([this f] (reset! this (f @this)))
([this f a] (reset! this (f @this a)))
([this f a b] (reset! this (f @this a b)))
([this f a b xs] (reset! this (apply f @this a b xs)))))")
(when-indenting-it "should handle non-symbol at start"
"
{\"1\" 2
*3 4}")
(when-indenting-it "should handle non-symbol at start 2"
"
(\"1\" 2
*3 4)")
(when-indenting-it "should handle defrecord"
"
(defrecord TheNameOfTheRecord
[a pretty long argument list]
SomeType
(assoc [_ x]
(.assoc pretty x 10)))")
(when-indenting-it "should handle defrecord 2"
"
(defrecord TheNameOfTheRecord [a pretty long argument list]
SomeType (assoc [_ x]
(.assoc pretty x 10)))")
(when-indenting-it "should handle defrecord with multiarity"
"
(defrecord Banana []
Fruit
(subtotal
([item]
(* 158 (:qty item)))
([item a]
(* a (:qty item)))))")
(when-indenting-it "should handle letfn"
"
(letfn [(f [x]
(* x 2))
(f [x]
(* x 2))]
(a b
c) (d)
e)")
(when-indenting-it "should handle reify"
"
(reify Object
(x [_]
1))"
"
(reify
om/IRender
(render [this]
(let [indent-test :fail]
...))
om/IRender
(render [this]
(let [indent-test :fail]
...)))")
(when-indenting-it "proxy"
"
(proxy [Writer] []
(close [] (.flush ^Writer this))
(write
([x]
(with-out-binding [out messages]
(.write out x)))
([x ^Integer off ^Integer len]
(with-out-binding [out messages]
(.write out x off len))))
(flush []
(with-out-binding [out messages]
(.flush out))))")
(when-indenting-it "should handle reader conditionals"
"#?@ (:clj []
:cljs [])")
(when-indenting-it "should handle an empty close paren"
"
(let [x]
)"
"
(ns ok
)"
"
(ns ^{:zen :dikar}
ok
)")
(when-indenting-it "should handle unfinished sexps"
"
(letfn [(tw [x]
dd")
(when-indenting-it "should handle symbols ending in crap"
"
(msg? ExceptionInfo
10)"
"
(thrown-with-msg? ExceptionInfo
#\"Storage must be initialized before use\"
(f))"
"
(msg' 1
10)")
(when-indenting-it "should handle let, when and while forms"
"(let-alist [x 1]\n ())"
"(while-alist [x 1]\n ())"
"(when-alist [x 1]\n ())"
"(if-alist [x 1]\n ())"
"(indents-like-fn-when-let-while-if-are-not-the-start [x 1]\n ())")
(defun indent-cond (indent-point state)
(goto-char (elt state 1))
(let ((pos -1)
(base-col (current-column)))
(forward-char 1)
;; `forward-sexp' will error if indent-point is after
;; the last sexp in the current sexp.
(condition-case nil
(while (and (<= (point) indent-point)
(not (eobp)))
(clojure-forward-logical-sexp 1)
(cl-incf pos))
;; If indent-point is _after_ the last sexp in the
;; current sexp, we detect that by catching the
;; `scan-error'. In that case, we should return the
;; indentation as if there were an extra sexp at point.
(scan-error (cl-incf pos)))
(+ base-col (if (cl-evenp pos) 0 2))))
(put-clojure-indent 'test-cond #'indent-cond)
(defun indent-cond-0 (_indent-point _state) 0)
(put-clojure-indent 'test-cond-0 #'indent-cond-0)
(when-indenting-it "should handle function spec"
"
(when me
(test-cond
x
1
2
3))"
"
(when me
(test-cond-0
x
1
2
3))")
(when-indenting-it "should respect indent style 'align-arguments"
'align-arguments
"
(some-function
10
1
2)"
"
(some-function 10
1
2)")
(when-indenting-it "should respect indent style 'always-indent"
'always-indent
"
(some-function
10
1
2)"
"
(some-function 10
1
2)")
(when-aligning-it "should basic forms"
"
{:this-is-a-form b
c d}"
"
{:this-is b
c d}"
"
{:this b
c d}"
"
{:a b
c d}"
"
(let [this-is-a-form b
c d])"
"
(let [this-is b
c d])"
"
(let [this b
c d])"
"
(let [a b
c d])")
(when-aligning-it "should handle a blank line"
"
(let [this-is-a-form b
c d
another form
k g])"
"
{:this-is-a-form b
c d
:another form
k g}")
(when-aligning-it "should handle basic forms (reversed)"
"
{c d
:this-is-a-form b}"
"
{c d
:this-is b}"
"
{c d
:this b}"
"
{c d
:a b}"
"
(let [c d
this-is-a-form b])"
"
(let [c d
this-is b])"
"
(let [c d
this b])"
"
(let [c d
a b])")
(when-aligning-it "should handle incomplete sexps"
"
(cond aa b
casodkas )"
"
(cond aa b
casodkas)"
"
(cond aa b
casodkas "
"
(cond aa b
casodkas"
"
(cond aa b
casodkas a)"
"
(cond casodkas a
aa b)"
"
(cond casodkas
aa b)")
(when-aligning-it "should handle multiple words"
"
(cond this is just
a test of
how well
multiple words will work)")
(when-aligning-it "should handle nested maps"
"
{:a {:a :a
:bbbb :b}
:bbbb :b}")
(when-aligning-it "should regard end as a marker"
"
{:a {:a :a
:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :a}
:b {:a :a
:aa :a}}")
(when-aligning-it "should handle trailing commas"
"
{:a {:a :a,
:aa :a},
:b {:a :a,
:aa :a}}")
(when-aligning-it "should handle standard reader conditionals"
"
#?(:clj 2
:cljs 2)")
(when-aligning-it "should handle splicing reader conditional"
"
#?@(:clj [2]
:cljs [2])")
(when-aligning-it "should handle sexps broken up by line comments"
"
(let [x 1
;; comment
xx 1]
xx)"
"
{:x 1
;; comment
:xxx 2}"
"
(case x
:aa 1
;; comment
:a 2)")
(when-aligning-it "should work correctly when margin comments appear after nested, multi-line, non-terminal sexps"
"
(let [x {:a 1
:b 2} ; comment
xx 3]
x)"
"
{:aa {:b 1
:cc 2} ;; comment
:a 1}}"
"
(case x
:a (let [a 1
aa (+ a 1)]
aa); comment
:aa 2)")
(it "should handle improperly indented content"
(let ((content "(let [a-long-name 10\nb 20])")
(aligned-content "(let [a-long-name 10\n b 20])"))
(with-clojure-buffer content
(call-interactively #'clojure-align)
(expect (buffer-string) :to-equal aligned-content))))
(it "should not align reader conditionals by default"
(let ((content "#?(:clj 2\n :cljs 2)"))
(with-clojure-buffer content
(call-interactively #'clojure-align)
(expect (buffer-string) :to-equal content))))
(it "should align reader conditionals when clojure-align-reader-conditionals is true"
(let ((content "#?(:clj 2\n :cljs 2)"))
(with-clojure-buffer content
(setq-local clojure-align-reader-conditionals t)
(call-interactively #'clojure-align)
(expect (buffer-string) :not :to-equal content))))
(it "should remove extra commas"
(with-clojure-buffer "{:a 2, ,:c 4}"
(call-interactively #'clojure-align)
(expect (string= (buffer-string) "{:a 2, :c 4}")))))
(provide 'clojure-mode-indentation-test)
;;; clojure-mode-indentation-test.el ends here