This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
ensime-test.el
2135 lines (1877 loc) · 79 KB
/
ensime-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
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
;;; ensime-test.el --- Regression tests for ENSIME
;;
;;;; License
;;
;; Copyright (C) 2010 Aemon Cannon
;;
;; 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 2 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, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
(eval-and-compile (require 'ensime))
(defvar ensime-testing-buffer "*ensime-tests*"
"Contains the output of all the tests. Also tracks all the testing-specific
buffer-local variables.")
(defvar ensime-test-queue '()
"The queue of tests yet to be run.")
(make-variable-buffer-local 'ensime-test-queue)
(defvar ensime-async-handler-stack '()
"Asynchronous event handlers waiting for signals. See 'ensime-test-sig'.")
(make-variable-buffer-local 'ensime-async-handler-stack)
(defvar ensime-shared-test-state '()
"A state dump for anyone who wants to use it. Useful for async tests.")
(make-variable-buffer-local 'ensime-shared-test-state)
(defvar ensime-test-dev-home
(expand-file-name "./")
"The local development root.")
(defvar ensime-test-env-classpath '()
"Extra jars to include on testing classpath")
(defvar ensime--test-had-failures nil)
(defvar ensime--test-exit-on-finish noninteractive)
(defvar ensime--test-pending-rpcs nil)
(put 'ensime-test-assert-failed
'error-conditions '(error ensime-test-assert-failed))
(put 'ensime-test-assert-failed 'error-message "Assertion Failed")
(put 'ensime-test-interrupted
'error-conditions '(error ensime-test-interrupted))
(put 'ensime-test-interrupted 'error-message "Test Interrupted")
(defun ensime--extract-scala-library-jar ()
(with-temp-buffer
(insert-file-contents
(ensime--classpath-file ensime--test-scala-version))
(--first
(string-match "[/\\]scala-library.*\\.jar$" it )
(split-string (buffer-string) ensime--classpath-separator 'omit-nulls))))
(defun ensime-test-concat-lines (&rest lines)
(mapconcat #'identity lines "\n"))
(defun ensime-create-file (file-name contents)
"Create file named file-name. Write contents to the file. Return file's name."
(make-directory (file-name-directory file-name) t)
(with-temp-file file-name
(insert contents))
file-name)
(defmacro* ensime-with-tmp-file ((name prefix contents) &rest body)
"Create temporary file with given prefix. Bind the file to
name and evaluate body."
`(let ((,name (make-temp-file ,prefix)))
(with-temp-file ,name
(insert ,contents))
(unwind-protect
(progn ,@body)
(delete-file ,name))))
(defvar ensime--test-scala-version
(getenv "SCALA_VERSION"))
(defun ensime--test-scala-major-version ()
(mapconcat 'int-to-string
(-take 2 (version-to-list ensime--test-scala-version))
"."))
(defun ensime-create-tmp-project (src-files &optional extra-config)
"Create a temporary project directory. Populate with config, source files.
Return a plist describing the project. Note: Delete such projects with
ensime-cleanup-tmp-project."
(let* ((root-dir (make-temp-file "ensime_test_proj_" t))
(cache-dir (expand-file-name "cache" root-dir))
(src-dir (expand-file-name "src/main/scala" root-dir))
(target-dir (expand-file-name
(concat "target/scala-"
(ensime--test-scala-major-version) "/classes" )
root-dir))
(test-target-dir (expand-file-name "test-target" root-dir))
(scala-jar (ensime--extract-scala-library-jar))
(config (append
extra-config
`(:root-dir ,root-dir
:cache-dir ,cache-dir
:name "test"
:scala-version ,ensime--test-scala-version
:java-home ,(getenv "JDK_HOME")
:java-flags ("-Xmx1g" "-Xss2m" "-XX:MaxPermSize=128m")
:subprojects
((:name "test"
:module-name "test"
:source-roots (,src-dir)
:depends-on-modules nil
:compile-deps (,scala-jar)
:target ,target-dir
:test-target ,test-target-dir)))))
(conf-file (ensime-create-file
(expand-file-name ".ensime" root-dir)
(format "%S" config))))
(mkdir src-dir t)
(mkdir cache-dir t)
(mkdir target-dir t)
(mkdir test-target-dir t)
(when ensime--test-cached-project
(message "Copying %s to %s"
(plist-get ensime--test-cached-project :cache-dir)
cache-dir)
(copy-directory (plist-get ensime--test-cached-project :cache-dir)
cache-dir nil t t)
(when (file-exists-p (expand-file-name "port" cache-dir))
(delete-file (expand-file-name "port" cache-dir))))
(let* ((src-file-names
(mapcar
(lambda (f) (ensime-create-file
(expand-file-name (plist-get f :name) src-dir)
(plist-get f :contents)))
src-files)))
(list
:src-files src-file-names
:root-dir root-dir
:cache-dir cache-dir
:conf-file conf-file
:src-dir src-dir
:target target-dir))))
(defvar ensime-tmp-project-hello-world
`((:name
"hello_world.scala"
:contents ,(ensime-test-concat-lines
"package com.helloworld"
"class HelloWorld{"
"}"
"object HelloWorld {"
"def main(args: Array[String]) = {"
"Console.println(\"Hello, world!\")"
"}"
"def foo(a:Int, b:Int):Int = {"
"a + b"
"}"
"}"))))
(defun ensime-test-compile-java-proj (proj arguments)
"Compile java sources of given temporary test project."
(let* ((root (plist-get proj :root-dir))
(src-files (plist-get proj :src-files))
(target (plist-get proj :target))
(args (append
arguments
(list "-d" target)
src-files)))
(assert (executable-find "javac"))
(assert (= 0 (apply 'call-process "javac" nil "*javac*" nil args)))))
(defun ensime-cleanup-tmp-project (proj &optional no-del)
"Destroy a temporary project directory, kill all buffers visiting
source files in the project."
;; TODO Should delete all buffers that weren't there at the test start
(let ((src-files (plist-get proj :src-files))
(root-dir (plist-get proj :root-dir)))
(dolist (f src-files)
(cond ((file-exists-p f)
(let ((buf (find-buffer-visiting f)))
(when buf
(switch-to-buffer buf)
(set-buffer-modified-p nil)
(kill-buffer nil))))
((get-buffer f)
(progn
(switch-to-buffer (get-buffer f))
(kill-buffer nil)))
(t)))
(when (and (not no-del)
root-dir
;; a bit of paranoia..
(or (integerp (string-match "^/tmp/" root-dir))
(integerp (string-match "/Temp/" root-dir))))
;; ..before we wipe away the project dir
(message "Deleting %s" root-dir)
(with-current-buffer ensime-testing-buffer
(delete-directory root-dir t)))))
(defun ensime-kill-all-ensime-servers ()
"Kill all inferior ensime server buffers."
(dolist (b (buffer-list))
(when (string-match "^\\*inferior-ensime-server.*" (buffer-name b))
(kill-buffer b))))
(defmacro ensime-test-var-put (var val)
"Helper for writing to shared testing state."
`(with-current-buffer ensime-testing-buffer
(setq ensime-shared-test-state
(plist-put ensime-shared-test-state ,var ,val))))
(defmacro ensime-test-var-get (var)
"Helper for reading from shared testing state."
`(with-current-buffer ensime-testing-buffer
(plist-get ensime-shared-test-state ,var)))
(defun ensime--return-event-handler (value id)
(setq ensime--test-pending-rpcs
(remove id ensime--test-pending-rpcs)))
(defun ensime--send-event-handler (value id)
(push id ensime--test-pending-rpcs))
(defun ensime-test-sig (event value)
"Driver for asynchonous tests. This function is invoked from ensime core,
signaling events to events handlers installed by asynchronous tests."
(when (buffer-live-p (get-buffer ensime-testing-buffer))
(message "pending rpcs: %s" ensime--test-pending-rpcs)
(when (> (length ensime--test-pending-rpcs) 1)
(message "WARNING more than one pending message"))
(message "Received test event: %s" event)
(with-current-buffer ensime-testing-buffer
(when (not (null ensime-async-handler-stack))
(let* ((ensime-prefer-noninteractive t)
(handler (car ensime-async-handler-stack))
(guard-func (plist-get handler :guard-func))
(handler-events (plist-get handler :events)))
(cond
(ensime-stack-eval-tags
(message "Got %s/%s while in a synchronous call, ignoring"
event value))
((and (equal (list event) handler-events)
(or (null guard-func) (funcall guard-func value)))
(let ((handler-func (plist-get handler :func))
(is-last (plist-get handler :is-last)))
(message "Handling test event: %s/%s" event handler)
(pop ensime-async-handler-stack)
(save-excursion
(condition-case signal
(funcall handler-func value)
(ensime-test-interrupted
(message
"Error executing test: %s, moving to next." signal)
(setq is-last t))))
(when is-last
(setq ensime-async-handler-stack nil)
(pop ensime-test-queue)
(ensime-run-next-test))))
((and (> (length handler-events) 1)
(member event handler-events))
(message "Received %s/%s expecting %s with guard func %s. Waiting for the rest"
event value handler-events guard-func)
(setq handler-events (cl-remove event handler-events :count 1))
(setq handler (plist-put handler :events handler-events))
(setcar ensime-async-handler-stack handler))
(t
(message "Got %s/%s, expecting %s with guard %s. Ignoring event."
event value handler-events guard-func))))))))
(defun ensime-run-suite (suite)
"Run a sequence of tests."
(switch-to-buffer ensime-testing-buffer)
(erase-buffer)
(setq ensime-test-queue suite)
(let ((ensime--test-cached-project nil))
(ensime-run-next-test)))
(defmacro ensime-test-suite (&rest tests)
"Define a sequence of tests to execute.
Tests may be synchronous or asynchronous."
`(list ,@tests))
(defmacro ensime-test (title &rest body)
"Define a synchronous test."
`(list :title ,title :async nil
:func (lambda ()
(ensime-test-run-with-handlers
,title
,@body))))
(defmacro ensime-test-run-with-handlers (context &rest body)
"Evaluate body in the context of an error handler. Handle errors by
writing to the testing output buffer."
`(save-excursion
(condition-case signal
(progn ,@body)
(ensime-test-assert-failed
(setq ensime--test-had-failures t)
(message "Assertion failed at '%s': %s" ,context signal)
(signal
'ensime-test-interrupted
(format "Test interrupted: %s." signal))))))
(defmacro* ensime-async-test (title trigger &rest handlers)
"Define an asynchronous test. Tests have the following structure:
(ensime-async-test
title
trigger-expression
[handler]*
)
Where:
title is a string that describes the test.
trigger-expression is some expression that either constitutes the entire
test, or (as is more common) invokes some process that will yield an
asynchronous event.
handler can be of one of two forms:
1) (event-types body...) where:
event-types is a list keywords that identifies event classes ; or a single keyword
body... is a list of arbitrary expressions
2) (event-type value-name guard-expr body...) where
event-type is a single keyword that identifies the expected event class
value-name is the symbol to which to bind the event payload
guard-expression is an expression evaluated with value-name bound to
the payload.
body... is a list of arbitrary expressions evaluated with value-name bound to the
payload of the event.
When the test is executed, trigger-expression is evaluated. The test then
waits for an asynchronous test event. When an event is signalled, the next
handler in line is considered.
- If the type of the event doesn't belong to the event-types of the handler head,
it is ignored
- Otherwise the event is recorded as being seen
- Once all the events in the handler's event-types have been seen, the corresponding
handler body is executed. The order in which the events are seen doesn't matter.
Handlers must be executed in order, they cannot be skipped. The test will
wait in an unfinished state until an event is signalled that matches the
next handler in line.
"
(let* ((last-handler (car (last handlers)))
(handler-structs
(mapcar
(lambda (h)
(if (listp (car h))
(list
:events (car h)
:func `(lambda (,(gensym)) (ensime-test-run-with-handlers ,title ,@(cdr h)))
:is-last (eq h last-handler))
(let ((value-sym (nth 1 h)))
(list
:events (list (car h))
:guard-func `(lambda (,value-sym) ,(nth 2 h))
:func `(lambda (,value-sym) (ensime-test-run-with-handlers ,title ,@(nthcdr 3 h)))
:is-last (eq h last-handler)))))
handlers))
(trigger-func
`(lambda ()
(ensime-test-run-with-handlers
,title
,trigger))))
`(list :title ,title :async t
:trigger ,trigger-func
:handlers ',handler-structs)))
(defun ensime-run-next-test ()
"Run the next test from the test queue."
(ensime-kill-all-ensime-servers)
(with-current-buffer ensime-testing-buffer
(if ensime-test-queue
(let ((ensime-prefer-noninteractive t)
(test (car ensime-test-queue)))
(setq ensime-shared-test-state '())
(setq ensime-async-handler-stack '())
(message "\n")
(message "Starting test: '%s'" (plist-get test :title))
(if (plist-get test :async)
;; Asynchronous test
(let ((handlers (reverse (plist-get test :handlers))))
(dolist (h handlers)
(push h ensime-async-handler-stack))
(funcall (plist-get test :trigger)))
;; Synchronous test
(progn
(pop ensime-test-queue)
(save-excursion
(condition-case signal
(funcall (plist-get test :func))
(ensime-test-interrupted
(message "Error executing test, moving to next."))))
(ensime-run-next-test))))
(progn
(goto-char (point-max))
(let* ((status (if ensime--test-had-failures 1 0))
(msg (format "Finished suite with status %s." status)))
(message msg)
(when ensime--test-cached-project
(ensime-cleanup-tmp-project ensime--test-cached-project))
(when ensime--test-exit-on-finish
(kill-emacs status)))))))
(defmacro ensime-assert (pred)
`(let ((val ,pred))
(if (not val)
(with-current-buffer ensime-testing-buffer
(signal 'ensime-test-assert-failed
(format "Expected truth of %s." ',pred))))))
(defmacro ensime-assert-equal (a b)
`(let ((val-a ,a)
(val-b ,b))
(if (equal val-a val-b) t
(with-current-buffer ensime-testing-buffer
(signal 'ensime-test-assert-failed
(format "Expected %s to equal %S, but was %S." ',a val-b val-a))))))
(defun ensime-assert-file-contains-string (f str)
(with-temp-buffer
(insert-file-contents-literally f)
(goto-char (point-min))
(ensime-assert (search-forward str nil t))))
(defun ensime-stop-tests ()
"Forcibly stop all tests in progress."
(interactive)
(with-current-buffer ensime-testing-buffer
(setq ensime-async-handler-stack nil)
(setq ensime-test-queue nil))
(switch-to-buffer ensime-testing-buffer))
(defun ensime-test-eat-label (mark)
(goto-char (point-min))
(when (search-forward-regexp (concat "/\\*" mark "\\*/") nil t)
(kill-backward-chars (+ 4 (length mark)))))
(defun ensime-test-after-label (mark)
(save-excursion
(goto-char (point-min))
(when (search-forward-regexp (concat "/\\*" mark "\\*/") nil t)
(point))))
(defun ensime-test-before-label (mark)
(- (ensime-test-after-label mark) (+ 5 (length mark))))
(defmacro* ensime-test-with-proj ((proj-name src-files-name) &rest body)
"Evaluate body in a context where the current test project is bound
to proj-name, the src-files of the project are bound to src-files-name,
and the active buffer is visiting the first file in src-files."
`(let* ((,proj-name (ensime-test-var-get :proj))
(,src-files-name (plist-get ,proj-name :src-files))
(existing (remove-if-not #'file-exists-p ,src-files-name)))
(when existing
(find-file (car existing)))
,@body))
(defun ensime-test-init-proj (proj &optional no-init)
"Store the project in a test var. Load the source files, switch to
the first source file, and optionally init ensime."
(let ((src-files (plist-get proj :src-files)))
(ensime-test-var-put :proj proj)
(ensime-test-var-put :pending-rpcs nil)
(find-file (car src-files))
(unless no-init (ensime))))
(defun ensime-test-cleanup (proj &optional no-del)
"Delete temporary project files. Kill ensime buffers."
(when ensime--test-pending-rpcs
(message "WARNING no response to messages: %s . Waiting some more."
ensime--test-pending-rpcs)
(sleep-for 10))
(if ensime--test-pending-rpcs
(progn
(message "ERROR no response to messages: %s"
ensime--test-pending-rpcs)
(setq ensime--test-had-failures t))
(message "OK no unreplied messages"))
(ensime-kill-all-ensime-servers)
; In Windows, we can't delete cache files until the server process has exited
(sleep-for 1)
(ensime-cleanup-tmp-project proj no-del))
;;;;;;;;;;;;;;;;;;
;; ENSIME Tests ;;
;;;;;;;;;;;;;;;;;;
(defvar ensime-fast-suite
(ensime-test-suite
(ensime-test
"Test loading a simple config."
(ensime-with-tmp-file
(file "ensime_test_conf_"
(format "%S"
'( :server-cmd
"bin/server.sh"
:dependendency-dirs ("hello" "world"))))
(let ((conf (ensime-config-load file)))
(ensime-assert (equal (plist-get conf :server-cmd) "bin/server.sh"))
(ensime-assert (equal (plist-get conf :dependendency-dirs)
'("hello" "world")))
)))
(ensime-test
"Test loading a broken(syntactically) config file."
(ensime-with-tmp-file
(file "ensime_test_conf_" "(lkjsdfkjskfjs")
(let ((conf
(condition-case er
(ensime-config-load file)
(error nil))))
(ensime-assert (null conf)))))
(ensime-test
"Encoding a UTF-8 string for SWANK"
(ensime-assert (equal "000001" (ensime-net-encode-length "$" 'utf-8)))
(ensime-assert (equal "000002" (ensime-net-encode-length "£" 'utf-8)))
(ensime-assert (equal "000003" (ensime-net-encode-length "€" 'utf-8)))
(ensime-assert (equal "00000a" (ensime-net-encode-length " $ £ € " 'utf-8)))
(ensime-assert (equal "000001" (ensime-net-encode-length "$" nil)))
(ensime-assert (equal "000001" (ensime-net-encode-length "£" nil)))
(ensime-assert (equal "000001" (ensime-net-encode-length "€" nil)))
(ensime-assert (equal "000007" (ensime-net-encode-length " $ £ € " nil))))
(ensime-test
"Reading a UTF-8 encoded S-Expression from SWANK"
(with-temp-buffer
(insert "000001$\n")
(ensime-assert (string-equal "$" (ensime-net-read))))
(with-temp-buffer
(insert "000002£\n")
(ensime-assert (string-equal "£" (ensime-net-read))))
(with-temp-buffer
(insert "000003€\n")
(ensime-assert (string-equal "€" (ensime-net-read))))
(with-temp-buffer
(insert "00000e\"hello world!\"\n")
(ensime-assert (string-equal "hello world!" (ensime-net-read))))
(with-temp-buffer
(insert "00000c( $ £ € )\n")
(ensime-assert (equal '($ £ €) (ensime-net-read)))))
(ensime-test
"Test name partitioning..."
(ensime-with-name-parts
"java.util.List" (p o n)
(ensime-assert-equal (list p o n)
(list "java.util" nil "List")))
(ensime-with-name-parts
"scala.tools.nsc.symtab.Types$Type" (p o n)
(ensime-assert-equal (list p o n)
(list "scala.tools.nsc.symtab" "Types" "Type")))
(ensime-with-name-parts
"scala.tools.nsc.symtab.Types" (p o n)
(ensime-assert-equal (list p o n)
(list "scala.tools.nsc.symtab" nil "Types")))
(ensime-with-name-parts
"scala.tools.nsc.symtab.Types$Dude$AbsType" (p o n)
(ensime-assert-equal
(list p o n)
(list "scala.tools.nsc.symtab" "Types$Dude" "AbsType")))
(ensime-with-name-parts
"scala.tools.nsc.symtab.Types$$Type$" (p o n)
(ensime-assert-equal (list p o n)
(list "scala.tools.nsc.symtab" "Types$" "Type$")))
(ensime-with-name-parts
"Types$$Type$" (p o n)
(ensime-assert-equal (list p o n)
(list "" "Types$" "Type$"))))
(ensime-test
"Test course name partitioning..."
(ensime-with-path-and-name
"java.util.List" (p n)
(ensime-assert-equal (list p n)
(list "java.util" "List")))
(ensime-with-path-and-name
"scala.tools.nsc.symtab.Types$Type" (p n)
(ensime-assert-equal (list p n)
(list "scala.tools.nsc.symtab" "Types$Type")))
(ensime-with-path-and-name
"scala.tools.nsc.symtab.Types" (p n)
(ensime-assert-equal (list p n)
(list "scala.tools.nsc.symtab" "Types")))
(ensime-with-path-and-name
"scala.tools.nsc.symtab.Types$Dude$AbsType" (p n)
(ensime-assert-equal (list p n)
(list "scala.tools.nsc.symtab" "Types$Dude$AbsType")))
(ensime-with-path-and-name
"scala.tools.nsc.symtab.Types$$Type$" (p n)
(ensime-assert-equal (list p n)
(list "scala.tools.nsc.symtab" "Types$$Type$")))
(ensime-with-path-and-name
"Types$$Type$" (p n)
(ensime-assert-equal (list p n)
(list "" "Types$$Type$")))
(ensime-with-path-and-name
"java.uti" (p n)
(ensime-assert-equal (list p n)
(list "java" "uti")))
(ensime-with-path-and-name
"uti" (p n)
(ensime-assert-equal (list p n)
(list "" "uti"))))
(ensime-test
"Test is source file predicate..."
(ensime-assert (ensime-source-file-p "dude.scala"))
(ensime-assert (ensime-source-file-p "dude.java"))
(ensime-assert (not (ensime-source-file-p "dude.javap"))))
(ensime-test
"Test relativization of paths..."
(ensime-assert-equal
"./rabbits.txt"
(ensime-relativise-path "/home/aemon/rabbits.txt" "/home/aemon/"))
(ensime-assert-equal
"./a/b/d.txt"
(ensime-relativise-path "/home/aemon/a/b/d.txt" "/home/aemon/"))
(ensime-assert-equal
"./a/b/d.txt"
(ensime-relativise-path "c:/home/aemon/a/b/d.txt" "c:/home/aemon/"))
(ensime-assert-equal
"c:/home/blamon/a/b/d.txt"
(ensime-relativise-path "c:/home/blamon/a/b/d.txt" "c:/home/aemon/")))
(ensime-test
"Test ensime-sem-high-internalize-syms in Unix mode"
(with-temp-buffer
(let* ((contents "a\nbc\nd\nef\ngh")
(num-chars (length contents))
(last-offset num-chars)
syms
internalized-syms
expected)
(insert contents)
(dotimes (i last-offset)
(push (list 'a i last-offset) syms)
(push (list 'a
(ensime-internalize-offset i)
(ensime-internalize-offset last-offset))
expected))
(setf expected (sort expected (lambda (a b) (< (nth 1 a) (nth 1 b)))))
(setf internalized-syms
(sort (ensime-sem-high-internalize-syms syms)
(lambda (a b) (< (nth 1 a) (nth 1 b)))))
(ensime-assert-equal internalized-syms expected))))
(ensime-test
"Test ensime-sem-high-internalize-syms in DOS mode"
(with-temp-buffer
(setf buffer-file-coding-system 'undecided-dos)
(let* ((contents "a\nbc\nd\nef\ngh")
(num-chars (length contents))
(last-offset (+ 5 num-chars))
syms
internalized-syms
expected)
(insert contents)
(dotimes (i last-offset)
(push (list 'a i last-offset) syms)
(push (list 'a
(ensime-internalize-offset i)
(ensime-internalize-offset last-offset))
expected))
(setf expected (sort expected (lambda (a b) (< (nth 1 a) (nth 1 b)))))
(setf internalized-syms
(sort (ensime-sem-high-internalize-syms syms)
(lambda (a b) (< (nth 1 a) (nth 1 b)))))
(ensime-assert-equal internalized-syms expected))))
(ensime-test
"Test ensime-insert-import with no package or import statement"
(with-temp-buffer
(set-visited-file-name "/tmp/fake/dir/abc.scala" t)
(insert (ensime-test-concat-lines
"class C {"
" def f = 1 /*1*/"
"}"))
(goto-char (ensime-test-after-label "1"))
(ensime-insert-import "org.example")
(set-buffer-modified-p nil)
(ensime-assert-equal
(buffer-substring-no-properties (point-min) (point-max))
(ensime-test-concat-lines
"import org.example"
""
"class C {"
" def f = 1 /*1*/"
"}"))))
(ensime-test
"Test ensime-insert-import with package statement"
(with-temp-buffer
(set-visited-file-name "/tmp/fake/dir/abc.scala" t)
(insert (ensime-test-concat-lines
"package com.example"
"class C {"
" def f = 1 /*1*/"
"}"))
(goto-char (ensime-test-after-label "1"))
(ensime-insert-import "org.example")
(set-buffer-modified-p nil)
(ensime-assert-equal
(buffer-substring-no-properties (point-min) (point-max))
(ensime-test-concat-lines
"package com.example"
""
"import org.example"
""
"class C {"
" def f = 1 /*1*/"
"}"))))
(ensime-test
"Test ensime-insert-import with import statement"
(with-temp-buffer
(set-visited-file-name "/tmp/fake/dir/abc.scala" t)
(insert (ensime-test-concat-lines
"import m"
""
""
"import n"
""
"import p"
"class C {"
" def f = 1 /*1*/"
"}"))
(goto-char (ensime-test-after-label "1"))
(ensime-insert-import "org.example")
(set-buffer-modified-p nil)
(ensime-assert-equal
(buffer-substring-no-properties (point-min) (point-max))
(ensime-test-concat-lines
"import m"
""
""
"import n"
"import org.example"
""
"import p"
"class C {"
" def f = 1 /*1*/"
"}"))))
(ensime-test
"Test ensime-insert-import stays above point"
(with-temp-buffer
(set-visited-file-name "/tmp/fake/dir/abc.scala" t)
(insert (ensime-test-concat-lines
"class C {"
" import example._ /*1*/"
" def f = 1"
"}"))
(goto-char (ensime-test-after-label "1"))
(ensime-insert-import "org.example")
(set-buffer-modified-p nil)
(ensime-assert-equal
(buffer-substring-no-properties (point-min) (point-max))
(ensime-test-concat-lines
"class C {"
" import org.example"
" import example._ /*1*/"
" def f = 1"
"}"))))
(ensime-test
"Test completion prefix lexing."
(with-temp-buffer
(insert (ensime-test-concat-lines
"package/*12*/ com.example"
"class C {"
" def main {"
" val rat = dog/*1*/"
" rat ++=/*2*/"
" rat !/*3*/"
" rat CaptainCrun/*4*/"
" rat.++=/*5*/"
" rat.toSt/*6*/"
" x/*7*/"
" /*hello*/dog/*8*/"
" while (moose/*9*/)prin/*10*/"
" case _ =>r/*11*/"
" dfkjsdfj/*13*/"
" dfkjsdfj132/*14*/"
" dfkjsdfj132:+/*15*/"
" dfkjsdfj132_:+/*16*/"
" _MAX_LEN_/*17*/"
" `yield`/*18*/"
" αρετη/*19*/"
" _y/*20*/"
" dot_product_*/*21*/"
" __system/*22*/"
" empty_?/*23*/"
" ./*24*/"
" .kjsdf/*25*/"
" kjsdf/*26*/"
" \"abc $hell/*27*/\""
" abc_*=efg/*28*/"
" }"
"}"))
(dotimes (i 28)
(ensime-test-eat-label (int-to-string (1+ i)))
(ensime-assert-equal
(ensime-completion-prefix-at-point)
(nth i '("dog"
"++="
"!"
"CaptainCrun"
"++="
"toSt"
"x"
"dog"
"moose"
"prin"
"r"
"package"
"dfkjsdfj"
"dfkjsdfj132"
":+"
"dfkjsdfj132_:+"
"_MAX_LEN_"
"`yield`"
"αρετη"
"_y"
"dot_product_*"
"__system"
"empty_?"
""
"kjsdf"
"kjsdf"
"hell"
"efg"
))))
))
(ensime-test
"Test ensime-short-local-name"
(ensime-assert-equal (ensime-short-local-name "Junk") "Junk")
(ensime-assert-equal (ensime-short-local-name "Foo$$Junk") "Junk")
(ensime-assert-equal (ensime-short-local-name "Foo$$Junk$") "Junk"))
(ensime-test
"Test ensime-strip-dollar-signs"
(ensime-assert-equal (ensime-strip-dollar-signs "com.example.Foo$")
"com.example.Foo")
(ensime-assert-equal (ensime-strip-dollar-signs "com.example.Foo$$Junk")
"com.example.Foo.Junk"))
(ensime-test
"Test ensime-path-includes-dir-p"
(unless (find system-type '(windows-nt cygwin))
(let ((d (make-temp-file "ensime_test_proj" t)))
(make-directory (concat d "/proj/src/main") t)
(make-directory (concat d "/proj/src/main/java") t)
(ensime-create-file (concat d "/proj/src/main/java/Test.java") "import java.util.bla")
(make-directory (concat d "/tmp/scala_misc") t)
(ensime-create-file (concat d "/tmp/scala_misc/Test.scala") "import java.util.bla")
(ensime-create-file (concat d "/tmp/other_misc/Things.scala") "import bla bla")
(make-symbolic-link (concat d "/tmp/scala_misc") (concat d "/proj/src/main/scala"))
(make-symbolic-link (concat d "/tmp/other_misc/Things.scala") (concat d "/proj/src/main/scala/Things.scala"))
(assert (file-exists-p (concat d "/proj/src/main/java/Test.java")))
(assert (file-exists-p (concat d "/proj/")))
(assert (ensime-path-includes-dir-p (concat d "/proj/src/main/java/Test.java")
(concat d "/proj")))
(assert (ensime-path-includes-dir-p (concat d "/proj/src/main/scala/Test.scala")
(concat d "/proj")))
(assert (ensime-path-includes-dir-p (concat d "/proj/src/main/scala/Test.scala")
(concat d "/proj/src")))
(assert (ensime-path-includes-dir-p (concat d "/proj/src/main/scala/Test.scala")
(concat d "/proj/src/main")))
(assert (ensime-path-includes-dir-p (concat d "/proj/src/main/scala/Test.scala")
(concat d "/")))
(assert (ensime-path-includes-dir-p (concat d "/proj/src/main/scala/Things.scala")
(concat d "/proj/src/main/scala")))
(assert (ensime-path-includes-dir-p (concat d "/proj/src/main/scala/Things.scala")
(concat d "/proj/src/")))
(assert (ensime-path-includes-dir-p (concat d "/proj/src/main/scala/Things.scala")
(concat d "/proj/src")))
(assert (ensime-path-includes-dir-p (concat d "/proj/src/main/scala/Things.scala")
(concat d "/tmp/scala_misc")))
(assert (not (ensime-path-includes-dir-p (concat d "/proj/src/main/scala/Things.scala")
(concat d "/proj/x"))))
;; intentionally not in an unwind-protect so it exists on failure
(delete-directory d t))))
(ensime-test
"Test ensime-replace-keywords"
(ensime-assert-equal (ensime-replace-keywords '("str1" :key1 "str2" "str3" :key2 :key3)
'(:key1 "foo" :key2 ("a" "b" "c") :key3 "bar"))
'("str1" "foo" "str2" "str3" "a" "b" "c" "bar")))
(ensime-test
"Test ensime--scan-classpath"
(ensime-assert-equal (ensime--scan-classpath
(ensime--build-classpath '("/x/y/foo-1.2.jar" "/x/y/bar-3.4.jar" "/x/y/baz-2.3.jar"))
"\\(foo\\|baz\\)-[.[:digit:]]+\\.jar$")
'("/x/y/foo-1.2.jar" "/x/y/baz-2.3.jar")))
(ensime-test
"Test ensime-inf-repl-config"
(let ((test-config
'(:scala-version "test-inf-repl-config"
:java-home "/x/y/jdk" :target "a" :compile-deps ("b" "c") :runtime-deps ("d" "e")
:java-flags ("flag1" "flag2")
:subprojects
((:target "f" :compile-deps ("g") :runtime-deps ("h"))
(:target "i" :compile-deps ("j" "k") :runtime-deps ("l" "m"))))))
(unwind-protect
(progn
(ensime-write-to-file (ensime--classpath-file "test-inf-repl-config")
(mapconcat #'identity
'("/x/y/scala-compiler-2.11.5.jar"
"/x/y/something-else-1.2.jar"
"/x/y/scala-reflect-2.11.5.jar")
ensime--classpath-separator))
(ensime-assert-equal (ensime-inf-repl-config test-config)
`(:java "/x/y/jdk/bin/java"
:java-flags ("flag1" "flag2")
:classpath ,(ensime--build-classpath
'("/x/y/scala-compiler-2.11.5.jar" "/x/y/scala-reflect-2.11.5.jar"
"a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m")))))
(delete-file (ensime--classpath-file "test-inf-repl-config")))))
(ensime-test
"Test ensime-stacktrace-pick-lines-to-fold"
(with-temp-buffer
(insert (concat "java.util.NoSuchElementException: None.get\n"
"\tat scala.None$.get(Option.scala:347)\n"
"\tat scala.None$.get(Option.scala:345)\n"
"\tat akka.actor.ActorCell.invoke(ActorCell.scala:487)\n"
"\tat akka.dispatch.Mailbox.processMailbox(Mailbox.scala:254)\n"
"\tat akka.dispatch.Mailbox.run(Mailbox.scala:221)\n"
"\tat akka.dispatch.Mailbox.exec(Mailbox.scala:231)\n"
"\tat scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)\n"
"\tat scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.pollAndExecAll(ForkJoinPool.java:1253)\n"
"\tat scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1346)\n"
"\tat scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)\n"))
(let ((lines-to-fold (ensime-stacktrace-pick-lines-to-fold '("at akka\\.*"))))
(ensime-assert-equal '(7 6 5 4) lines-to-fold))))
(ensime-test
"Test ensime-stacktrace-groups-lines-to-fold"
(let ((grouped-lines (ensime-stacktrace-group-lines-to-fold '(10 9 8 6 5 3 1))))
(ensime-assert-equal '((1) (3) (5 6) (8 9 10)) grouped-lines)))))