-
Notifications
You must be signed in to change notification settings - Fork 79
/
typescript-mode-general-tests.el
1125 lines (1003 loc) · 46.2 KB
/
typescript-mode-general-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
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; typescript-mode-general-tests --- This file contains general tests for typescript-mode.el
;;; Commentary:
;; To know how to run the tests, see typescript-mode-tests.el
;;; Code:
(require 'ert)
(require 'typescript-mode)
(require 'cl-lib)
(require 'typescript-mode-test-utilities)
(defun typescript-test-get-doc ()
(buffer-substring-no-properties (point-min) (point-max)))
(defun typescript-test-indent-all ()
(delete-trailing-whitespace)
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max)))
(ert-deftest auto-mode-alist-ts ()
(find-file (make-temp-file load-file-name nil ".ts"))
(should (string-equal "typescript-mode" major-mode)))
(ert-deftest auto-mode-alist-tsx ()
(find-file (make-temp-file load-file-name nil ".tsx"))
(should (string-equal "typescript-mode" major-mode)))
(ert-deftest indentation-reference-document-is-reflowed-correctly ()
(with-temp-buffer
(insert-file-contents "test-files/indentation-reference-document.ts")
;; double ensure mode is active
(typescript-mode)
(let ((test-reference (typescript-test-get-doc)))
(typescript-test-indent-all)
(should (string-equal test-reference
(typescript-test-get-doc)))
(let ((typescript-indent-switch-clauses nil))
(typescript-test-indent-all)
(should (string-equal test-reference
(typescript-test-get-doc)))))))
(ert-deftest switch-case-indent-default ()
(with-temp-buffer
(insert-file-contents "test-files/switch-case-indent-default.ts")
(typescript-mode)
(let ((test-reference (typescript-test-get-doc)))
(typescript-test-indent-all)
(should (string-equal test-reference
(typescript-test-get-doc))))))
(ert-deftest switch-case-indent-disabled ()
(with-temp-buffer
(insert-file-contents "test-files/switch-case-indent-disabled.ts")
(let ((typescript-indent-switch-clauses nil))
(typescript-mode)
(let ((test-reference (typescript-test-get-doc)))
(typescript-test-indent-all)
(should (string-equal test-reference
(typescript-test-get-doc)))))))
(ert-deftest list-items-indent-default ()
(with-temp-buffer
(insert-file-contents "test-files/list-items-indent-default.ts")
(typescript-mode)
(let ((test-reference (typescript-test-get-doc)))
(typescript-test-indent-all)
(should (string= test-reference (typescript-test-get-doc))))))
(ert-deftest list-items-indent-default-not-comma-first ()
(with-temp-buffer
(insert-file-contents "test-files/list-items-indent-comma-first.ts")
(typescript-mode)
(let ((test-reference (typescript-test-get-doc)))
(typescript-test-indent-all)
(should-not (string= test-reference (typescript-test-get-doc))))))
(ert-deftest list-items-indent-comma-first ()
(with-temp-buffer
(insert-file-contents "test-files/list-items-indent-comma-first.ts")
(typescript-mode)
(let ((test-reference (typescript-test-get-doc))
(typescript-indent-list-items nil))
(typescript-test-indent-all)
(should (string= test-reference (typescript-test-get-doc))))))
(ert-deftest list-items-indent-comma-first-not-default ()
(with-temp-buffer
(insert-file-contents "test-files/list-items-indent-default.ts")
(typescript-mode)
(let ((test-reference (typescript-test-get-doc))
(typescript-indent-list-items nil))
(typescript-test-indent-all)
(should-not (string= test-reference (typescript-test-get-doc))))))
(defun get-all-matched-strings (to-match)
(let (result)
(dotimes (x (/ (length (match-data)) 2))
(setq result (nconc result (list (match-string x to-match)))))
result))
(ert-deftest typescript-tslint-report-regexp-matches ()
"typescript-tslint-report-regexp matches a line that does not
have a rule name or a severity."
(let* ((to-match
"src/modules/authenticator.ts[1, 83]: ' should be \"")
(match (string-match typescript-tslint-report-regexp
to-match))
(matches (and match (get-all-matched-strings to-match))))
(should match)
(should-not (nth 1 matches))
(should-not (nth 2 matches))
(should (string-equal (nth 3 matches)
"src/modules/authenticator.ts"))
(should (string-equal (nth 4 matches) "1"))
(should (string-equal (nth 5 matches) "83"))))
(ert-deftest typescript-tslint-report-regexp-matches-with-name ()
"typescript-tslint-report-regexp matches a line that has
a rule name, no severity."
(let* ((to-match
"(quotemark) src/modules/authenticator.ts[1, 83]: ' should be \"")
(match (string-match typescript-tslint-report-regexp
to-match))
(matches (and match (get-all-matched-strings to-match))))
(should match)
(should-not (nth 1 matches))
(should (string-equal (nth 2 matches) "(quotemark) "))
(should (string-equal (nth 3 matches)
"src/modules/authenticator.ts"))
(should (string-equal (nth 4 matches) "1"))
(should (string-equal (nth 5 matches) "83"))))
(ert-deftest typescript-tslint-report-regexp-matches-with-error ()
"typescript-tslint-report-regexp matches a line that has
a severity set to ERROR, no rule name."
(let* ((to-match
"ERROR: src/modules/authenticator.ts[1, 83]: ' should be \"")
(match (string-match typescript-tslint-report-regexp
to-match))
(matches (and match (get-all-matched-strings to-match))))
(should match)
(should-not (nth 1 matches))
(should-not (nth 2 matches))
(should (string-equal (nth 3 matches)
"src/modules/authenticator.ts"))
(should (string-equal (nth 4 matches) "1"))
(should (string-equal (nth 5 matches) "83"))))
(ert-deftest typescript-tslint-report-regexp-matches-with-warning ()
"typescript-tslint-report-regexp matches a line that has
a severity set to WARNING, no rule name."
(let* ((to-match
"WARNING: src/modules/authenticator.ts[1, 83]: ' should be \"")
(match (string-match typescript-tslint-report-regexp
to-match))
(matches (and match (get-all-matched-strings to-match))))
(should match)
(should (string-equal (nth 1 matches) "WARNING"))
(should-not (nth 2 matches))
(should (string-equal (nth 3 matches)
"src/modules/authenticator.ts"))
(should (string-equal (nth 4 matches) "1"))
(should (string-equal (nth 5 matches) "83"))))
(ert-deftest typescript--number-literal-re-matches-numbers ()
"`typescript--number-literal-re' matches numbers."
(dolist (to-match '("NaN" "Infinity" "-Infinity" "-1" "1" "0.1" ".1" "-.1" "8e23"
"9E-2" ".1e23" "0b1" "-0B1" "0o7" "-0O13" "0xaf" "-0XAF"))
(should (string-match typescript--number-literal-re to-match))
;; The regular expression does not begin with ^ and end with $ so
;; we need to check ourselves that the whole string is matched.
(should (string-equal (match-string 0 to-match) to-match))))
(ert-deftest typescript--number-literal-re-does-not-match-non-numbers ()
"`typescript--number-literal-re' does not match non-numbers."
(dolist (to-match '("NaNa" "Inf" "1." "." "0xPQ" "e" "2.3e2.4"))
;; For the same reason as for the positive test above, what we want is either no match
;; or a match that fails to match the whole string.
(should-not (and (string-match typescript--number-literal-re to-match)
(string-equal (match-string 0 to-match) to-match)))))
(ert-deftest correctly-indents-lines-with-wide-chars ()
"Otsuka Ai and other multi-char users should be a happy to write typescript."
(with-temp-buffer
(ignore-errors (typescript-mode))
(insert "let x = '大塚愛'")
(let ((pos1 (current-column)))
(typescript-indent-line)
(let ((pos2 (current-column)))
(should (= pos1 pos2))))))
(ert-deftest correctly-indents-lines-with-tabs ()
(with-temp-buffer
(ignore-errors (typescript-mode))
(insert "class Example {")
(newline-and-indent)
(insert "constructor() {")
(newline-and-indent)
(insert "const a = new Promise")
(should (= 29 (current-column)))
(typescript-indent-line)
(should (= 29 (current-column)))
;; verify tab was used
(move-beginning-of-line nil)
(should (= 0 (current-column)))
(forward-char 1)
(should (= 8 (current-column)))))
(ert-deftest correctly-indents-dot-dot-after-exclamation ()
(with-temp-buffer
(ignore-errors (typescript-mode))
(insert "situation('8/8/8/8/8/8/8/R3K2R w - - 0 1')?.")
(forward-char -1)
(newline-and-indent)
(should (= 4 (current-column)))))
(ert-deftest indentation-does-not-hang-on-multiline-string ()
"Testcase for https://github.com/ananthakumaran/typescript.el/issues/20"
(with-temp-buffer
(typescript-mode)
(insert "let multiLineString = \"line 1")
(newline-and-indent)
(insert "// and so we continue")
(newline-and-indent)
;; completing and not locking up is test-success!
))
(ert-deftest typescript--forward-expression-on-multiline-indented-string ()
"Testcase for https://github.com/emacs-typescript/typescript.el/issues/105"
(with-temp-buffer
(typescript-mode)
(insert
"fetch('http://localhost:8529/_db/_system/land', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({
query: `{
query GetElement {
element(id: \"0000\") {
collection
id
name
description
}
}
}`,
}),
})
.then(r => r.json())
.then(data => console.log('data returned:', data));")
(goto-char (point-min))
(typescript--forward-expression)
;; completing and not locking up is test-success!
;; Should there be a time-out? Or it is handled by external tool?
;; Check that `typescript--forward-expression' jumped to the right position.
(should (= 434 (point)))))
(defun test-re-search (searchee contents offset)
(with-temp-buffer
(typescript-mode)
(insert contents)
(goto-char (- (point-max) offset))
(should (= 5 (typescript--re-search-backward-inner searchee nil 1)))))
(ert-deftest re-search-backwards-skips-single-line-strings ()
(test-re-search "token" "let token = \"token in string-thing\";" 2))
(ert-deftest re-search-backwards-skips-multi-line-strings ()
(test-re-search "token" "let token = \"token in\n multi-line token string\";" 2))
(ert-deftest re-search-backwards-skips-single-line-comments ()
(test-re-search "token" "let token; // token in comment" 0))
(ert-deftest re-search-backwards-skips-multi-line-comments ()
(test-re-search "token" "let token; /* token in \nmulti-line token comment" 0))
(setq font-lock-contents
" * @param {Something} bar A parameter. References [[moo]] and [[foo]].
* @param second May hold ``x`` or ``y``.")
(ert-deftest font-lock/interface-builtin-key-context-unfontify ()
"Builtins should not be fontified when they are in object or
interface key context."
(test-with-fontified-buffer
"interface Foo { type: number; unknown: string; foo: boolean }"
(should (eq (get-face-at "type") 'default))
(should (eq (get-face-at "unknown") 'default)))
(test-with-fontified-buffer
"const x = { type: 4; unknown: 'bar'; foo: true }"
(should (eq (get-face-at "type") 'default))
(should (eq (get-face-at "unknown") 'default))))
(ert-deftest font-lock/documentation-in-documentation-comments ()
"Documentation in documentation comments should be fontified as
documentation."
(font-lock-test
(concat "/**\n" font-lock-contents "\n*/")
'((1 . font-lock-comment-delimiter-face)
(5 . font-lock-comment-face)
("@param" . typescript-jsdoc-tag)
("{Something}" . typescript-jsdoc-type)
("bar" . typescript-jsdoc-value)
("\\[\\[moo\\]\\]" . typescript-jsdoc-value)
("\\[\\[foo\\]\\]" . typescript-jsdoc-value)
("``x``" . typescript-jsdoc-value)
("``y``" . typescript-jsdoc-value))))
(ert-deftest font-lock/no-documentation-in-non-documentation-comments ()
"Documentation tags that are not in documentation comments
should not be fontified as documentation."
(test-with-fontified-buffer
(concat "/*\n" font-lock-contents "\n*/\n")
(let ((loc 3))
;; Make sure we start with the right face.
(should (eq (get-face-at loc) font-lock-comment-face))
(should (eq (text-property-not-all loc (point-max) 'face font-lock-comment-face)
(1- (point-max)))))))
(ert-deftest font-lock/no-documentation-in-strings ()
"Documentation tags that are not in strings should not be
fontified as documentation."
(test-with-fontified-buffer
(concat "const x = \"/**" font-lock-contents "*/\";")
(let ((loc (search-forward "\"")))
;; Make sure we start with the right face.
(should (eq (get-face-at loc) font-lock-string-face))
;; Make sure the face does not change later.
(should (eq (text-property-not-all loc (point-max) 'face font-lock-string-face)
(1- (point-max)))))))
(ert-deftest font-lock/immediate-doc ()
"Tests that it is not necessary to have the documentation tag on a
new line after the start of '/**'."
(font-lock-test
;; We have 4 comments here because we need to cover the multiple
;; regexes that deal with the different types of jsdoc tags.
"/** @type {foo} */\n
/** @alias bar */\n
/** @author me */\n
/** @param meow */"
'((1 . font-lock-comment-delimiter-face)
("@type" . typescript-jsdoc-tag)
("{foo}" . typescript-jsdoc-type)
("@alias" . typescript-jsdoc-tag)
("bar" . typescript-jsdoc-value)
("@author" . typescript-jsdoc-tag)
("me" . font-lock-comment-face)
("@param" . typescript-jsdoc-tag)
("meow" . typescript-jsdoc-value))))
(ert-deftest font-lock/function-definition-prefixes ()
"Tests that function names are highlighted in definitions, even
when prefixed with module modifiers."
(font-lock-test
"function basicDefn(x0: xty0, y0: yty0): ret0 {}\n
export function exportedDefn(x1: xty1, y1: yty1): ret1 {}\n
export default function exportedDefaultDefn(x2: xty2, y2: yty2): ret2 {}\n
declare function declareFunctionDefn(x3: xty3, y3: yty3): ret3;"
'(("basicDefn" . font-lock-function-name-face)
("exportedDefn" . font-lock-function-name-face)
("exportedDefaultDefn" . font-lock-function-name-face)
("declareFunctionDefn" . font-lock-function-name-face)
(("x0" "x1" "x2" "x3") . font-lock-variable-name-face)
(("\\by0" "\\by1" "\\by2" "\\by3") . font-lock-variable-name-face)
(("ret0" "ret1" "ret2" "ret3") . nil))))
(ert-deftest font-lock/level-four ()
"Tests the level four font lock highlights."
(font-lock-test
"@decorator\n
class Foo<T> extends Bar {\n
private async innerExecuteAsync<TResponse extends Response, TValue>(endpoint: string, data?: any): Promise<TResponse> {\n
innerExecuteAsync(x: string, y: boolean, z: number, j?: any): Promise<FResponse> {\n
console.log(this.methodCall());\n
snake_cased_function(1, 2, 3)"
'(("@decorator" . font-lock-function-name-face)
("Foo" . font-lock-type-face)
("private" . typescript-access-modifier-face)
("innerExecuteAsync" . font-lock-function-name-face)
(("TResponse" "FResponse" "Response" "TValue") . font-lock-type-face)
("console" . font-lock-type-face)
("this" . typescript-this-face)
("methodCall" . font-lock-function-name-face)
("snake_cased_function" . font-lock-function-name-face)
(("string" "boolean" "number" "any") . typescript-primitive-face)
(("endpoint" "data") . font-lock-variable-name-face)
(("<" ">" ",") . nil))))
(ert-deftest font-lock/method-call-with-keyword-name ()
"If the name of the method is a keyword, it should still be highlighted as function."
(test-with-fontified-buffer
"const app = express();
app.get()
app.post()
app.delete()
if (true) {}
// for (abc) {}"
(should (eq (get-face-at "get") 'font-lock-function-name-face))
(should (eq (get-face-at "post") 'font-lock-function-name-face))
(should (eq (get-face-at "delete") 'font-lock-function-name-face))
(should (eq (get-face-at "if") 'font-lock-keyword-face))
(should (eq (get-face-at "for") 'font-lock-comment-face))))
(ert-deftest font-lock/generics ()
"Tests that type hints within generics are highlighted properly."
(font-lock-test
"const map = new Map<string, number>()\n
function foo<Z, Y, Z & Y, Z | Y | Z, Y<X<X, Y>>>()\n"
'((("string" "number") . typescript-primitive-face)
("foo" . font-lock-function-name-face)
(("Z" "Y" "X") . font-lock-type-face)
(("<" ">" "," "&" "|") . nil))))
(ert-deftest font-lock/tsx ()
"Tests that tsx blocks are not considered generics by virtue of the <."
(font-lock-test
"<div>test</div>"
'((("div" . nil)))))
(ert-deftest font-lock/regexp ()
"Regular expressions should be fontified as string constant."
(let ((content "=/foo/ (/bar/ ,/baz/ :/buzz/"))
(font-lock-test content
'(("=" . nil) ("/foo/" . font-lock-string-face)
("(" . nil) ("/bar/" . font-lock-string-face)
("," . nil) ("/baz/" . font-lock-string-face)
(":" . nil) ("/buzz/" . font-lock-string-face))))
;; Make sure that escaped forward slashes are handled too.
(font-lock-test "var a = /flip\\/flop/;"
'(("=" . nil)
(("/flip" "\\\\" "/" "flop/") . font-lock-string-face)
(";" . nil)))
;; Make sure a forward slash in a character class is handled fine.
;; It must not terminate the regular expression.
(font-lock-test "var a = /[/]/;"
'(("=" . nil)
(("/" "\\[/" "\\]/") . font-lock-string-face)
(";" . nil)))
;; Make sure an open bracket in a character class does not
;; throw off fontification.
(font-lock-test "var a = /[[]/;"
'(("=" . nil)
(("/" "\\[\\[\\]" "/") . font-lock-string-face)
(";" . nil)))
;; A sequence of two forward slashes is never a regex, so there is
;; no such thing as an \"empty regex\" when we use the forward slash
;; notation.
(font-lock-test "=//g something // comment"
'(("g something" . font-lock-comment-face))))
(ert-deftest font-lock/yield ()
"`yield' and `yield*' should be fontified as keywords."
(font-lock-test
"function* gen(x0: xty0, y0: yty0): ret0 {
yield 123;
yield* subIter;
}"
'(("yield 123" . font-lock-keyword-face)
("yield\\*" . font-lock-keyword-face)
("\\* subIter" . font-lock-keyword-face))))
(ert-deftest font-lock/yielder ()
"`yielder' should not be fontified as a keyword."
(font-lock-test
"function* gen(x0: xty0, y0: yty0): ret0 {
const yielder = 123;
yield abc;
return yielder;
}"
'(("yielder =" . font-lock-variable-name-face)
("yielder;" . nil))))
(ert-deftest font-lock/text-after-trailing-regexp-delim-should-not-be-fontified ()
"Text after trailing regular expression delimiter should not be fontified."
(test-with-fontified-buffer
"=/foo/g something // comment"
(should (eq (get-face-at "g something") nil)))
(test-with-fontified-buffer
"=/foo\\bar/g something // comment"
(should (eq (get-face-at "g something") nil)))
(test-with-fontified-buffer
"=/foo\\\\bar/g something // comment"
(should (eq (get-face-at "g something") nil)))
(test-with-fontified-buffer
"=/foo\\\\/g something // comment"
(should (eq (get-face-at "g something") nil))))
(ert-deftest font-lock/type-names ()
"Type names should be highlighted in definitions."
;; Typical case.
(test-with-fontified-buffer
"export class Foo extends Bar implements Qux {}"
(should (eq (get-face-at "Foo") 'font-lock-type-face))
(should (eq (get-face-at "Bar") 'font-lock-type-face))
(should (eq (get-face-at "Qux") 'font-lock-type-face)))
(test-with-fontified-buffer
"export class Foo extends Bar implements Qux, Ajx {}"
(should (eq (get-face-at "Foo") 'font-lock-type-face))
(should (eq (get-face-at "Bar") 'font-lock-type-face))
(should (eq (get-face-at "Qux") 'font-lock-type-face))
(should (eq (get-face-at ",") 'nil))
(should (eq (get-face-at "Ajx") 'font-lock-type-face)))
(test-with-fontified-buffer
"export class Foo extends Bar implements Qux, Ajx, Psd {}"
(should (eq (get-face-at "Foo") 'font-lock-type-face))
(should (eq (get-face-at "Bar") 'font-lock-type-face))
(should (eq (get-face-at "Qux") 'font-lock-type-face))
(should (eq (get-face-at ",") 'nil))
(should (eq (get-face-at "Ajx") 'font-lock-type-face))
(should (eq (get-face-at ",") 'nil))
(should (eq (get-face-at "Psd") 'font-lock-type-face)))
;; Ensure we require symbol boundaries.
(test-with-fontified-buffer
"Notclass Foo"
(should-not (eq (get-face-at "Foo") 'font-lock-type-face)))
;; Other common ways of defining types.
(test-with-fontified-buffer
"interface Thing {}"
(should (eq (get-face-at "Thing") 'font-lock-type-face)))
(test-with-fontified-buffer
"enum Thing {}"
(should (eq (get-face-at "Thing") 'font-lock-type-face)))
(test-with-fontified-buffer
"type Thing = number;"
(should (eq (get-face-at "Thing") 'font-lock-type-face))))
(ert-deftest font-lock/fontify-type-guard ()
"The type guard syntax
var is Type
should be fontified as variable, keyword and type."
(test-with-fontified-buffer
"function test(var: unknown): var is RetType {\n}"
(should (eq (get-face-at 30) 'font-lock-variable-name-face))
(should (eq (get-face-at "is") 'font-lock-keyword-face))
(should (eq (get-face-at "RetType") 'font-lock-type-face))))
(ert-deftest font-lock/type-names-level4 ()
"Typenames should be highlighted in declarations"
(test-with-fontified-buffer
"function test(var1: Type1, var2: Type2): RetType {\n}"
(should-not (eq (get-face-at "var1") 'font-lock-type-face))
(should (eq (get-face-at "Type1") 'font-lock-type-face))
(should-not (eq (get-face-at "var2") 'font-lock-type-face))
(should (eq (get-face-at "Type2") 'font-lock-type-face))
(should (eq (get-face-at "RetType") 'font-lock-type-face)))
(test-with-fontified-buffer
"class foo { test(var1: Type1, var2: Type2): RetType {\n} }"
(should-not (eq (get-face-at "var1") 'font-lock-type-face))
(should (eq (get-face-at "Type1") 'font-lock-type-face))
(should-not (eq (get-face-at "var2") 'font-lock-type-face))
(should (eq (get-face-at "Type2") 'font-lock-type-face))
(should (eq (get-face-at "RetType") 'font-lock-type-face)))
(test-with-fontified-buffer
"let a: SomeType;"
(should (eq (get-face-at "SomeType") 'font-lock-type-face)))
(test-with-fontified-buffer
"private b: SomeType;"
(should (eq (get-face-at "SomeType") 'font-lock-type-face)))
(test-with-fontified-buffer
"private someArray: SomeType[];"
(should (eq (get-face-at "SomeType") 'font-lock-type-face)))
(test-with-fontified-buffer
"private generic: SomeType<Foo>;"
(should (eq (get-face-at "SomeType") 'font-lock-type-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face)))
(test-with-fontified-buffer
"private genericArray: SomeType<Foo>[];"
(should (eq (get-face-at "SomeType") 'font-lock-type-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face)))
(test-with-fontified-buffer
"private genericArray2: SomeType<Foo[]>;"
(should (eq (get-face-at "SomeType") 'font-lock-type-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face)))
(test-with-fontified-buffer
"private genericArray3: SomeType<Foo[]>[];"
(should (eq (get-face-at "SomeType") 'font-lock-type-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face)))
(test-with-fontified-buffer
"const f: () => SomeType = () => {}"
(should (eq (get-face-at "SomeType") 'font-lock-type-face))))
(ert-deftest font-lock/type-names-level4-namespaces ()
"Namespaced Typenames should be highlighted in declarations"
(test-with-fontified-buffer
"private b: Namespaced.ClassName;"
(should (eq (get-face-at "Namespaced") 'font-lock-type-face))
(should (eq (get-face-at "ClassName") 'font-lock-type-face)))
(test-with-fontified-buffer
"function test(var1: Namespaced.ClassName): RetType {\n}"
(should (eq (get-face-at "Namespaced") 'font-lock-type-face))
(should (eq (get-face-at "ClassName") 'font-lock-type-face)))
(test-with-fontified-buffer
"class Foo { test(var1: Namespaced.ClassName): RetType {\n}"
(should (eq (get-face-at "Namespaced") 'font-lock-type-face))
(should (eq (get-face-at "ClassName") 'font-lock-type-face)))
(test-with-fontified-buffer
"function test(var1: Type): Namespaced.ClassName {\n}"
(should (eq (get-face-at "Namespaced") 'font-lock-type-face))
(should (eq (get-face-at "ClassName") 'font-lock-type-face)))
(test-with-fontified-buffer
"class Foo { test(var1: Type): Namespaced.ClassName {\n}"
(should (eq (get-face-at "Namespaced") 'font-lock-type-face))
(should (eq (get-face-at "ClassName") 'font-lock-type-face))))
(ert-deftest font-lock/funargs--function--multiline-with-types ()
"Variables should be highlighted in multiline declarations with types."
(test-with-fontified-buffer
"function test(
var1: Promise<U1, V1>,
var2: (xxx: Foo) => Bar,
var3: Type3,
): RetType {\n}"
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))
(should (eq (get-face-at "var3") 'font-lock-variable-name-face))
(should (eq (get-face-at "xxx") 'font-lock-variable-name-face))
(should (eq (get-face-at "Promise") 'font-lock-type-face))
(should (eq (get-face-at "U1") 'font-lock-type-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face))
(should (eq (get-face-at "Type3") 'font-lock-type-face))))
(ert-deftest font-lock/funargs--function--multiline-without-types ()
"Variables should be highlighted in multiline declarations without types."
(test-with-fontified-buffer
"function test(
var1,
var2,
): RetType {\n}"
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--function--multiline-hanging-paren ()
"Variables should be highlighted in multiline declarations with hanging paren."
(test-with-fontified-buffer
"function test(
var1,
var2): RetType {\n}"
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--function--multiline-ending-comma-hanging-paren ()
"Variables should be highlighted in multiline declarations with hanging paren and trailing comma."
(test-with-fontified-buffer
"function test(
var1,
var2,): RetType {\n}"
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--function--singleline-ending-comma-no-hanging-paren ()
"Variables should be highlighted in singleline declarations with no hanging paren and trailing comma."
(test-with-fontified-buffer
"function test(var1,var2,
): RetType {\n}"
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--function--singleline-with-types ()
"Variables should be highlighted in singleline declarations with types."
(test-with-fontified-buffer
"function test(var1: Foo, var2: Bar,): RetType {\n}"
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face))
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
(ert-deftest font-lock/funargs--function--singleline-ending-comma-hanging-paren ()
"Variables should be highlighted in singleline declarations with hanging paren and trailing comma."
(test-with-fontified-buffer
"function test(var1,var2,): RetType {\n}"
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--function--keywords-as-variables ()
"Keywords when used as variables should have variable face"
(test-with-fontified-buffer
"function test(type, unknown): void {}"
(should (eq (get-face-at "type") 'font-lock-variable-name-face))
(should (eq (get-face-at "unknown") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--keywords-as-variables ()
"Keywords when used as variables should have variable face"
(test-with-fontified-buffer
"const test = (type, unknown): void => {}"
(should (eq (get-face-at "type") 'font-lock-variable-name-face))
(should (eq (get-face-at "unknown") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--single-line--no-type ()
(test-with-fontified-buffer
"const test = (aaa, bbb, ccc): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--single-line--no-type--no-return-type ()
(test-with-fontified-buffer
"const test = (aaa, bbb, ccc) => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--single-line--no-type--trailing-comma ()
(test-with-fontified-buffer
"const test = (aaa, bbb, ccc,): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--single-line--no-type--optional ()
(test-with-fontified-buffer
"const test = (aaa, bbb?): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb?") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--multiline--no-type ()
(test-with-fontified-buffer
"const test = (aaa, bbb,
ccc, ddd): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))
(should (eq (get-face-at "ddd") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--multiline--no-type--newline-after-last ()
(test-with-fontified-buffer
"const test = (aaa, bbb,
ccc, ddd
): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))
(should (eq (get-face-at "ddd") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--multiline--no-type--newline-before-first ()
(test-with-fontified-buffer
"const test = (
aaa, bbb,
ccc, ddd
): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))
(should (eq (get-face-at "ddd") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--multiline--no-type--with-comment ()
(test-with-fontified-buffer
"const test = (
aaa, bbb, // comment
ccc, ddd // comment
): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))
(should (eq (get-face-at "ddd") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--single--mixed-type--newline-before-first ()
(test-with-fontified-buffer
"const test = (aaa, bbb: Promise, ccc: number, ddd): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))
(should (eq (get-face-at "ddd") 'font-lock-variable-name-face))
(should (eq (get-face-at "Promise") 'font-lock-type-face))
(should (eq (get-face-at "number") 'typescript-primitive-face))))
(ert-deftest font-lock/funargs--arrow--single--with-type--complex-type ()
(test-with-fontified-buffer
"const test = (aaa: Promise<U, V, (xxx: A) => Foo>, bbb): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "xxx") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--arrow--multiline--with-type--newline-before-first-after-last ()
(test-with-fontified-buffer
"const test = (
aaa: Foo,
bbb: Bar
): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face))
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
(ert-deftest font-lock/funargs--arrow--multiline--with-type--newline-before-first-after-last--hanging-comma ()
(test-with-fontified-buffer
"const test = (
aaa: Foo,
bbb: Bar,
): void => {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face))
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
(ert-deftest font-lock/backticks--expr-fontification--with-variable ()
(test-with-fontified-buffer
"const x = `hello ${world}`"
(should (eq (get-face-at "${") 'font-lock-keyword-face))
(should (eq (get-face-at "world") 'default))
(should (eq (get-face-at "}") 'font-lock-keyword-face))))
(ert-deftest font-lock/backticks--expr-fontification--not-in-regular-string ()
(test-with-fontified-buffer
"const x = 'hello ${world}'"
(should (eq (get-face-at "${") 'font-lock-string-face))
(should (eq (get-face-at "world") 'font-lock-string-face))
(should (eq (get-face-at "}") 'font-lock-string-face))))
(ert-deftest font-lock/backticks--expr-fontification--with-funcall ()
"For now function calls or any other expressions are fontified as
if a simple variable token in its entirety. When/if this is
implemented better, this test should be adjusted to capture the
new functionality."
(test-with-fontified-buffer
"const x = `hello ${parseInt(foobar)}`"
(should (eq (get-face-at "${") 'font-lock-keyword-face))
(should (eq (get-face-at "parseInt(foobar)") 'default))
(should (eq (get-face-at "}") 'font-lock-keyword-face))))
(ert-deftest font-lock/funargs--method--multiline--with-type ()
(test-with-fontified-buffer
"class Foo { foo(
aaa: Foo,
bbb: Bar,
): void {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face))
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
(ert-deftest font-lock/funargs--method--single-line--with-type ()
(test-with-fontified-buffer
"class Foo { foo(aaa: Foo,bbb: Bar,): void {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face))
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
(ert-deftest font-lock/funargs--method--single-line--no-type ()
(test-with-fontified-buffer
"class Foo { foo(aaa, bbb): void {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--method--single-line--no-return-type ()
(test-with-fontified-buffer
"class Foo { foo(aaa, bbb) {}"
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))))
(ert-deftest font-lock/funargs--method--no-fontification-in-ternary ()
"Do not apply fontification on a function call inside a ternary
operator, which might look like method with return type
declaration."
(test-with-fontified-buffer
"true ? funcall(helloWorld) : false"
(should (eq (get-face-at "helloWorld") nil))))
(ert-deftest font-lock/funargs--method--no-fontification-in-special-form ()
"Do not apply fontification inside a special form paren-form,
such as inside of if/while/switch etc. These look like method
declarations without a return type annotation but are not."
(test-with-fontified-buffer
"if (hello && world) { }"
(should (eq (get-face-at "world") nil))))
(defun flyspell-predicate-test (search-for)
"This function runs a test on
`typescript--flyspell-mode-predicate'. `SEARCH-FOR' is a string
to search for in the current buffer before running
`typescript--flyspell-mode-predicate'. This test checks that the
point has not moved. It returns the value of returned by the
invocation of `typescript--flyspell-mode-predicate'."
(search-forward search-for)
(let ((point-before (point)))
(prog1
(typescript--flyspell-mode-predicate)
;; We should not have moved.
(should (eq (point) point-before)))
))
(ert-deftest flyspell-mode-predicate-skips-what-it-should ()
"Check that the custom flyspell predicate filters strings in
import... from...."
(let (flyspell-generic-progmode-verify)
(fset 'flyspell-generic-progmode-verify (lambda () t))
;; In the following searches we search for the starting quote of the strings
;; to avoid hitting keywords. Moreover, the end position of the search is important.
;; Flyspell puts point at the end of the word before calling the predicate. We must
;; replicate that behavior here.
(test-with-fontified-buffer
"import 'a';\nimport { x } from 'b';\nconst foo = 'c';import { x }\nfrom 'd';"
(should-not (flyspell-predicate-test "'a"))
(should-not (flyspell-predicate-test "'b"))
(should (flyspell-predicate-test "'c"))
(should-not (flyspell-predicate-test "'d")))
(test-with-fontified-buffer
;; This is valid TypeScript.
"const from = 'a';"
(should (flyspell-predicate-test "'a")))
(test-with-fontified-buffer
;; TypeScript does not allow a function named "import" but object
;; members may be named "import". So this *can* be valid
;; TypeScript.
"x.import('a');"
(should (flyspell-predicate-test "'a")))))
(ert-deftest typescript--move-to-end-of-plain-string ()
"Unit tests for `typescript--move-to-end-of-plain-string'."
(cl-flet
((should-fail ()
(let ((point-before (point)))
(should-not (typescript--move-to-end-of-plain-string))
(should (eq (point) point-before))))
(should-not-fail (expected)
(let ((result (typescript--move-to-end-of-plain-string)))
(should (eq result expected))
(should (eq (point) expected)))))
;;
;; The tests below are structured as follows. For each case:
;;
;; 1. Move point to a new location in the buffer.
;;
;; 2. Check whether typescript--move-to-end-of-plain-string returns the value we expected
;; and changes (point) when successful.
;;
;; Cases often start with a check right away: (point) equal to
;; (point-min) for those cases.
;;
(dolist (delimiter '("'" "\""))
(test-with-temp-buffer
(replace-regexp-in-string "'" delimiter "const a = 'not terminated")
(should-fail)
(re-search-forward delimiter)
(should-fail))
(test-with-temp-buffer
(replace-regexp-in-string "'" delimiter "const a = 'terminated'")
(should-fail)
;; This checks that the function works when invoked on the start delimiter of
;; a terminated string.
(re-search-forward delimiter)
(should-not-fail (1- (point-max)))
(goto-char (point-min))
(re-search-forward "term")
(should-not-fail (1- (point-max)))
;; This checks that the function works when invoked on the end delimiter of
;; a terminated string.
(goto-char (1- (point-max)))
(should-not-fail (1- (point-max))))
(test-with-temp-buffer
(replace-regexp-in-string "'" delimiter "const a = 'terminated aaa';\n
const b = 'not terminated bbb")
(should-fail)
(re-search-forward "term")
(should-not-fail (save-excursion (re-search-forward "aaa")))
(re-search-forward "const b")
(should-fail)
(re-search-forward "not terminated")
(should-fail))
;; Case with escaped delimiter.
(test-with-temp-buffer
(replace-regexp-in-string "'" delimiter "const a = 'terminat\\'ed aaa';\n
const b = 'not terminated bbb")
(re-search-forward "term")
(should-not-fail (save-excursion (re-search-forward "aaa"))))
;; Delimiters in comments.
(test-with-temp-buffer
(replace-regexp-in-string "'" delimiter "const a = 'terminated aaa';\n