forked from JuliaEditorSupport/julia-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
julia-mode-tests.el
609 lines (546 loc) · 13.4 KB
/
julia-mode-tests.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
;;; julia-mode-tests.el --- Tests for julia-mode.el
;; Copyright (C) 2009-2014 Julia contributors
;; URL: https://github.com/JuliaLang/julia
;; Version: 0.3
;; Keywords: languages
;;; Usage:
;; From command line:
;;
;; emacs -batch -L . -l ert -l julia-mode-tests.el -f ert-run-tests-batch-and-exit
;;; Commentary:
;; Contains ert tests for julia-mode.el
;;; License:
;; Permission is hereby granted, free of charge, to any person obtaining
;; a copy of this software and associated documentation files (the
;; "Software"), to deal in the Software without restriction, including
;; without limitation the rights to use, copy, modify, merge, publish,
;; distribute, sublicense, and/or sell copies of the Software, and to
;; permit persons to whom the Software is furnished to do so, subject to
;; the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;;; Code:
;; We can't use cl-lib whilst supporting Emacs 23 users who don't use
;; ELPA.
(with-no-warnings
(require 'cl)) ;; incf, decf, plusp
(require 'julia-mode)
(require 'ert)
(defmacro julia--should-indent (from to)
"Assert that we indent text FROM producing text TO in `julia-mode'."
`(with-temp-buffer
(let ((julia-indent-offset 4))
(julia-mode)
(insert ,from)
(indent-region (point-min) (point-max))
(should (equal (buffer-substring-no-properties (point-min) (point-max))
,to)))))
(defmacro julia--should-font-lock (text pos face)
"Assert that TEXT at position POS gets font-locked with FACE in `julia-mode'."
`(with-temp-buffer
(julia-mode)
(insert ,text)
(if (fboundp 'font-lock-ensure)
(font-lock-ensure (point-min) (point-max))
(with-no-warnings
(font-lock-fontify-buffer)))
(should (eq ,face (get-text-property ,pos 'face)))))
(defmacro julia--should-move-point (text fun from to &optional end arg)
"With TEXT in `julia-mode', after calling FUN, the point should move FROM\
to TO. If FROM is a string, move the point to matching string before calling
function FUN. If TO is a string, match resulting point to point a beginning of
matching line or end of match if END is non-nil. Optional ARG is passed to FUN."
(declare (indent defun))
`(with-temp-buffer
(julia-mode)
(insert ,text)
(indent-region (point-min) (point-max))
(goto-char (point-min))
(if (stringp ,from)
(re-search-forward ,from)
(goto-char ,from))
(funcall ,fun ,arg)
(should (eq (point) (if (stringp ,to)
(progn (goto-char (point-min))
(re-search-forward ,to)
(if ,end (goto-char (match-end 0))
(goto-char (match-beginning 0))
(point-at-bol)))
,to)))))
(ert-deftest julia--test-indent-if ()
"We should indent inside if bodies."
(julia--should-indent
"
if foo
bar
end"
"
if foo
bar
end"))
(ert-deftest julia--test-indent-else ()
"We should indent inside else bodies."
(julia--should-indent
"
if foo
bar
else
baz
end"
"
if foo
bar
else
baz
end"))
(ert-deftest julia--test-indent-toplevel ()
"We should not indent toplevel expressions. "
(julia--should-indent
"
foo()
bar()"
"
foo()
bar()"))
(ert-deftest julia--test-indent-nested-if ()
"We should indent for each level of indentation."
(julia--should-indent
"
if foo
if bar
bar
end
end"
"
if foo
if bar
bar
end
end"))
(ert-deftest julia--test-indent-module-keyword ()
"Module should not increase indentation at any level."
(julia--should-indent
"
module
begin
a = 1
end
end"
"
module
begin
a = 1
end
end")
(julia--should-indent
"
begin
module
foo
end
end"
"
begin
module
foo
end
end"))
(ert-deftest julia--test-indent-function ()
"We should indent function bodies."
(julia--should-indent
"
function foo()
bar
end"
"
function foo()
bar
end"))
(ert-deftest julia--test-indent-begin ()
"We should indent after a begin keyword."
(julia--should-indent
"
@async begin
bar
end"
"
@async begin
bar
end"))
(ert-deftest julia--test-indent-paren ()
"We should indent to line up with the text after an open paren."
(julia--should-indent
"
foobar(bar,
baz)"
"
foobar(bar,
baz)"))
(ert-deftest julia--test-indent-paren-space ()
"We should indent to line up with the text after an open
paren, even if there are additional spaces."
(julia--should-indent
"
foobar( bar,
baz )"
"
foobar( bar,
baz )"))
(ert-deftest julia--test-indent-paren-newline ()
"python-mode-like indentation."
(julia--should-indent
"
foobar(
bar,
baz)"
"
foobar(
bar,
baz)")
(julia--should-indent
"
foobar(
bar,
baz
)"
"
foobar(
bar,
baz
)"))
(ert-deftest julia--test-indent-equals ()
"We should increase indent on a trailing =."
(julia--should-indent
"
foo() =
bar"
"
foo() =
bar"))
(ert-deftest julia--test-indent-operator ()
"We should increase indent after the first trailing operator
but not again after that."
(julia--should-indent
"
foo() |>
bar |>
baz
qux"
"
foo() |>
bar |>
baz
qux"))
(ert-deftest julia--test-indent-ignores-blank-lines ()
"Blank lines should not affect indentation of non-blank lines."
(julia--should-indent
"
if foo
bar
end"
"
if foo
bar
end"))
(ert-deftest julia--test-indent-comment-equal ()
"`=` at the end of comment should not increase indent level."
(julia--should-indent
"
# a =
# b =
c"
"
# a =
# b =
c"))
(ert-deftest julia--test-indent-leading-paren ()
"`(` at the beginning of a line should not affect indentation."
(julia--should-indent
"
\(1)"
"
\(1)"))
(ert-deftest julia--test-top-level-following-paren-indent ()
"`At the top level, a previous line indented due to parens should not affect indentation."
(julia--should-indent
"y1 = f(x,
z)
y2 = g(x)"
"y1 = f(x,
z)
y2 = g(x)"))
(ert-deftest julia--test-indentation-of-multi-line-strings ()
"Indentation should only affect the first line of a multi-line string."
(julia--should-indent
" a = \"\"\"
description
begin
foo
bar
end
\"\"\""
"a = \"\"\"
description
begin
foo
bar
end
\"\"\""))
(ert-deftest julia--test-indent-of-end-in-brackets ()
"Ignore end keyword in brackets for the purposes of indenting blocks."
(julia--should-indent
"begin
begin
arr[1: end - 1]
end
end"
"begin
begin
arr[1: end - 1]
end
end"))
(ert-deftest julia--test-indent-after-commented-keyword ()
"Ignore keywords in comments when indenting."
(julia--should-indent
"# if foo
a = 1"
"# if foo
a = 1"))
(ert-deftest julia--test-indent-after-commented-end ()
"Ignore `end` in comments when indenting."
(julia--should-indent
"if foo
a = 1
#end
b = 1
end"
"if foo
a = 1
#end
b = 1
end"))
(ert-deftest julia--test-indent-import-export-using ()
"Toplevel using, export, and import."
(julia--should-indent
"export bar, baz,
quux"
"export bar, baz,
quux")
(julia--should-indent
"using Foo: bar ,
baz,
quux
notpartofit"
"using Foo: bar ,
baz,
quux
notpartofit"))
(ert-deftest julia--test-indent-anonymous-function ()
"indentation for function(args...)"
(julia--should-indent
"function f(x)
function(y)
x+y
end
end"
"function f(x)
function(y)
x+y
end
end"))
(ert-deftest julia--test-backslash-indent ()
"indentation for function(args...)"
(julia--should-indent
"(\)
1
(:\)
1"
"(\)
1
(:\)
1"))
(ert-deftest julia--test-indent-keyword-paren ()
"indentation for ( following keywords"
"if( a>0 )
end
function( i=1:2 )
for( j=1:2 )
for( k=1:2 )
end
end
end"
"if( a>0 )
end
function( i=1:2 )
for( j=1:2 )
for( k=1:2 )
end
end
end")
(ert-deftest julia--test-symbol-font-locking-at-bol ()
"Symbols get font-locked at beginning or line."
(julia--should-font-lock
":a in keys(Dict(:a=>1))" 1 'julia-quoted-symbol-face))
(ert-deftest julia--test-symbol-font-locking-after-backslash ()
"Even with a \ before the (, it is recognized as matching )."
(let ((string "function \\(a, b)"))
(julia--should-font-lock string (1- (length string)) nil)))
(ert-deftest julia--test-function-assignment-font-locking ()
(julia--should-font-lock
"f(x) = 1" 1 'font-lock-function-name-face)
(julia--should-font-lock
"Base.f(x) = 1" 6 'font-lock-function-name-face)
(julia--should-font-lock
"f(x) where T = 1" 1 'font-lock-function-name-face)
(julia--should-font-lock
"f(x) where{T} = 1" 1 'font-lock-function-name-face)
(dolist (def '("f(x)::T = 1" "f(x) :: T = 1" "f(x::X)::T where X = x"))
(julia--should-font-lock def 1 'font-lock-function-name-face)))
(ert-deftest julia--test-where-keyword-font-locking ()
(julia--should-font-lock
"f(x) where T = 1" 6 'font-lock-keyword-face)
(dolist (pos '(22 30))
(julia--should-font-lock
"function f(::T, ::Z) where T where Z
1
end"
pos 'font-lock-keyword-face)))
(ert-deftest julia--test-escaped-strings-dont-terminate-string ()
"Symbols get font-locked at beginning or line."
(let ((string "\"\\\"\"; function"))
(dolist (pos '(1 2 3 4))
(julia--should-font-lock string pos font-lock-string-face))
(julia--should-font-lock string (length string) font-lock-keyword-face)))
;;; Movement
(ert-deftest julia--test-beginning-of-defun-assn-1 ()
"Point moves to beginning of single-line assignment function."
(julia--should-move-point
"f() = \"a + b\"" 'beginning-of-defun "a \\+" 1))
(ert-deftest julia--test-beginning-of-defun-assn-2 ()
"Point moves to beginning of multi-line assignment function."
(julia--should-move-point
"f(x)=
x*
x" 'beginning-of-defun "\\*\nx" 1))
(ert-deftest julia--test-beginning-of-defun-assn-3 ()
"Point moves to beginning of multi-line assignment function adjoining
another function."
(julia--should-move-point
"f( x
)::Int16 = x / 2
f2(y)=
y*y" 'beginning-of-defun "2" 1))
(ert-deftest julia--test-beginning-of-defun-assn-4 ()
"Point moves to beginning of 2nd multi-line assignment function adjoining
another function."
(julia--should-move-point
"f( x
)::Int16 =
x /
2
f2(y) =
y*y" 'beginning-of-defun "\\*y" "f2"))
(ert-deftest julia--test-beginning-of-defun-assn-5 ()
"Point moves to beginning of 1st multi-line assignment function adjoining
another function with prefix arg."
(julia--should-move-point
"f( x
)::Int16 =
x /
2
f2(y) =
y*y" 'beginning-of-defun "y\\*y" 1 nil 2))
(ert-deftest julia--test-beginning-of-macro ()
"Point moves to beginning of macro."
(julia--should-move-point
"macro current_module()
return VERSION >= v\"0.7-\" :(@__MODULE__) : :(current_module())))
end" 'beginning-of-defun "@" 1))
(ert-deftest julia--test-beginning-of-defun-1 ()
"Point moves to beginning of defun in 'function's."
(julia--should-move-point
"function f(a, b)
a + b
end" 'beginning-of-defun "f(" 1))
(ert-deftest julia--test-beginning-of-defun-nested-1 ()
"Point moves to beginning of nested function."
(julia--should-move-point
"function f(x)
function fact(n)
if n == 0
return 1
else
return n * fact(n-1)
end
end
return fact(x)
end" 'beginning-of-defun "fact(n" "function fact"))
(ert-deftest julia--test-beginning-of-defun-nested-2 ()
"Point moves to beginning of outermost function with prefix arg."
(julia--should-move-point
"function f(x)
function fact(n)
if n == 0
return 1
else
return n * fact(n-1)
end
end
return fact(x)
end" 'beginning-of-defun "n \\*" 1 nil 2))
(ert-deftest julia--test-beginning-of-defun-no-move ()
"Point shouldn't move if there is no previous function."
(julia--should-move-point
"1 + 1
f(x) = x + 1" 'beginning-of-defun "\\+" 4))
(ert-deftest julia--test-end-of-defun-assn-1 ()
"Point should move to end of assignment function."
(julia--should-move-point
"f(x)::Int8 =
x *x" 'end-of-defun "(" "*x" 'end))
(ert-deftest julia--test-end-of-defun-nested-1 ()
"Point should move to end of inner function when called from inner."
(julia--should-move-point
"function f(x)
function fact(n)
if n == 0
return 1
else
return n * fact(n-1)
end
end
return fact(x)
end" 'end-of-defun "n == 0" "end[ \n]+end\n" 'end))
(ert-deftest julia--test-end-of-defun-nested-2 ()
"Point should move to end of outer function when called from inner with prefix."
(julia--should-move-point
"function f(x)
function fact(n)
if n == 0
return 1
else
return n * fact(n-1)
end
end
return fact(x)
end" 'end-of-defun "n == 0" "return fact(x)[ \n]+end" 'end 2))
(defun julia--run-tests ()
(interactive)
(if (featurep 'ert)
(ert-run-tests-interactively "julia--test")
(message "Can't run julia-mode-tests because ert is not available.")))
(provide 'julia-mode-tests)
;; Local Variables:
;; coding: utf-8
;; byte-compile-warnings: (not obsolete)
;; End:
;;; julia-mode-tests.el ends here