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-goto-testfile.el
360 lines (307 loc) · 12.4 KB
/
ensime-goto-testfile.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
;;; ensime-goto-testfile.el -- Navigate to test classes
(eval-when-compile
(require 'cl)
(require 'ensime-macros))
(require 'cl-lib)
(require 'scala-mode2-syntax)
(defun ensime-goto-test (&optional other-window-p)
"Locate the test file that corresponds to the class around the point,
and visit that file. If the test file doesn't exist, it is created and
filled with a stub test class.
With an argument, open the test file in another window."
(interactive "P")
(block nil
(when (ensime-is-test-file buffer-file-name)
(message "This isn't an implementation file")
(return))
(let* ((impl-class
(or (ensime-top-level-class-closest-to-point)
(return (message "Could not find top-level class"))))
(test-class-info
(or (ensime-get-test-class-info impl-class)
(return (message "Could not determine test class for %s"
impl-class)))))
(cond
((equal impl-class "<none>.<none>")
(message "Hit ensime race condition, please try again"))
((listp test-class-info) (ensime-goto-source-location test-class-info))
((stringp test-class-info)
(ensime-create-test-file test-class-info impl-class other-window-p))))))
(defun ensime-goto-impl (&optional other-window-p)
"If the point is inside a test class, go to the corresponding
implementation class. With an argument, open the test file in another window."
(interactive "P")
(block nil
(unless (ensime-is-test-file buffer-file-name)
(message "This isn't a test file")
(return))
(let ((test-class (ensime-top-level-class-closest-to-point)))
(cond
((equal test-class "<none>.<none>")
(message "Hit ensime race condition, please try again"))
((null test-class)
(message "Could not find top-level class"))
(t
(ensime-goto-source-location (ensime-get-impl-class-info test-class)))))))
(defun ensime-get-goto-test-config (key)
(let* ((module-name (plist-get (ensime-config (ensime-connection)) :name))
(case-fold-search nil)
(module-params
(cdr
(find module-name ensime-goto-test-configs
:test (lambda (m p) (string-match-p (car p) m))))))
(if (plist-member module-params key)
(plist-get module-params key)
(plist-get ensime-goto-test-config-defaults key))))
(defun ensime-source-base-dir-for-file (file-name)
"Return the source base directory for the current buffer, as defined in the
ensime configuration."
(let* ((all-sources
(mapcar #'expand-file-name (ensime-source-roots-from-config)))
(dir
(find-if
(lambda (dir) (ensime-path-includes-dir-p file-name dir))
all-sources)))
(when dir (file-name-as-directory (expand-file-name dir)))))
(defun ensime-is-test-file (file-name)
"Return true if the given file name is part of the project's test sources"
(funcall (ensime-get-goto-test-config :is-test-dir-fn)
(ensime-source-base-dir-for-file file-name)))
(defun ensime-top-level-class-closest-to-point ()
"Return the name of first class, trait or object enclosing the point,
or (if the point isn't inside a class definition) the class that follows
the point. Return nil if no class can be found."
;; TODO use an RPC call instead of this cheesy search
(cl-labels
((pos-of-top-level-class (&optional last-try)
(save-excursion
(save-restriction
(widen)
(let ((top-level-sexp (point)))
;; Try to go up a sexp until we get an error
(condition-case nil
(while t
(setq top-level-sexp (point))
(backward-up-list))
(error nil))
(goto-char top-level-sexp)
(re-search-backward "}\\|\\<object\\>\\|\\<class\\>\\|\\<trait\\>" nil t)
(let ((class-re
(concat "\\<\\(object\\|class\\|trait\\)[ \t\n]+\\("
scala-syntax:id-re
"\\)")))
(if (re-search-forward class-re nil t)
(match-beginning 2)
(unless last-try
(pos-of-top-level-class t)))))))))
(let ((pos (pos-of-top-level-class)))
(when pos
(save-excursion
(goto-char pos)
(replace-regexp-in-string
"\\$$" ""
(plist-get (ensime-rpc-get-type-at-point) :full-name)))))))
(defun ensime-get-test-class-info (impl-class)
"Return information for the test class that correspondss to IMPL-CLASS.
If the return value is
- a list: it contains the position of an existing class within the project.
- a string: it names a new test class that must be created
- nil: a test class could not be determined."
(let* ((candidates (funcall (ensime-get-goto-test-config :test-class-names-fn)
impl-class))
(positions (mapcar
(lambda (c)
(plist-get
(or
(ensime-rpc-get-type-by-name c)
(ensime-rpc-get-type-by-name (concat c "$")))
:pos))
candidates))
(found-position
(find-if (lambda (pos)
(and pos
(ensime-pos-file pos)
(ensime-is-test-file (ensime-pos-file pos))))
positions)))
(or found-position
(first candidates))))
(defun ensime-get-impl-class-info (test-class)
"Return the location the implementation class that correspondss to TEST-CLASS.
Return a position list, or nil if the implementation class couldn't be
determined."
(let* ((candidate (funcall (ensime-get-goto-test-config :impl-class-name-fn)
test-class))
(position (and candidate
(plist-get
(or
(ensime-rpc-get-type-by-name candidate)
(ensime-rpc-get-type-by-name (concat candidate "$")))
:pos))))
(when (and position
(ensime-pos-file position)
(not (ensime-is-test-file (ensime-pos-file position))))
position)))
(defun ensime-create-test-file (test-class-name impl-class-name &optional other-window-p)
"Create a file for the class TEST-CLASS-NAME and switch to it. Populate
the file with stub code. if the file already exists, simply visit it."
(block nil
(let ((test-file-name (ensime-get-test-file-name test-class-name))
(impl-coding buffer-file-coding-system))
(unless test-file-name
(message "Could not determine test file name for %s" test-class-name)
(return))
(make-directory (file-name-directory test-file-name) t)
(if other-window-p
(find-file-other-window test-file-name)
(find-file test-file-name))
(unless (file-exists-p test-file-name)
(ensime-generate-test-stub test-class-name impl-class-name)
(set-buffer-file-coding-system impl-coding)))))
(defun ensime-get-test-file-name (test-class-name)
"Return the name of the file that should contain the test class
TEST-CLASS-NAME. The current buffer must be the file that contains the
implementation class."
(let* ((impl-base-dir (ensime-source-base-dir-for-file buffer-file-name))
(impl-extension (file-name-extension buffer-file-name t))
(test-relative-path
(concat
(replace-regexp-in-string "\\." "/" test-class-name)
impl-extension)))
(when impl-base-dir
(let ((test-base-dir
(funcall (ensime-get-goto-test-config :impl-to-test-dir-fn)
impl-base-dir)))
(when test-base-dir
(expand-file-name test-relative-path test-base-dir))))))
(defun ensime-generate-test-stub (test-class-name impl-class-name)
"Insert stub test code in the current buffer, for the class TEST-CLASS-NAME"
(let (test-package test-class impl-package impl-class template
(case-fold-search nil))
(string-match "^\\(\\(.*\\)\\.\\)?\\([^.]+\\)$" test-class-name)
(setq test-package (match-string 2 test-class-name))
(setq test-class (match-string 3 test-class-name))
(string-match "^\\(\\(.*\\)\\.\\)?\\([^.]+\\)$" impl-class-name)
(setq impl-package (match-string 2 impl-class-name))
(setq impl-class (match-string 3 impl-class-name))
(setq template (funcall (ensime-get-goto-test-config :test-template-fn)))
(setq template (replace-regexp-in-string "%TESTPACKAGE%" test-package template t))
(setq template (replace-regexp-in-string "%IMPLPACKAGE%" impl-package template t))
(setq template (replace-regexp-in-string "%TESTCLASS%" test-class template t))
(setq template (replace-regexp-in-string "%IMPLCLASS%" impl-class template t))
(insert template)))
(defun ensime-goto-test--test-class-names (impl-class)
(let ((suffixes (ensime-get-goto-test-config :test-class-suffixes)))
(mapcar
(lambda (s)
(replace-regexp-in-string "^\\(.*\\)$"
(concat "\\1" s)
impl-class t))
suffixes)))
(defun ensime-goto-test--impl-class-name (test-class)
(let ((suffixes (ensime-get-goto-test-config :test-class-suffixes))
(case-fold-search nil))
(dolist (s suffixes)
(when (string-match-p (concat s "$") test-class)
(return
(replace-regexp-in-string (concat s "$") "" test-class t))))))
(defun ensime-goto-test--impl-to-test-dir (impl-dir)
(let ((conf (ensime-config (ensime-connection)))
(is-test-dir-fn (ensime-get-goto-test-config :is-test-dir-fn)))
(dolist (module (plist-get conf :subprojects))
(let ((module-sources
(mapcar (lambda (s)
(file-name-as-directory (expand-file-name s)))
(plist-get module :source-roots))))
(when (find impl-dir module-sources :test #'equal)
(return (find-if is-test-dir-fn module-sources)))))))
(defun ensime-goto-test--is-test-dir (dir)
(let ((case-fold-search nil))
(or
(string-match-p "src/test/scala/$" dir)
(string-match-p "src/it/scala/$" dir)
(string-match-p "src/fun/scala/$" dir)
(string-match-p "/tests?/$" dir))))
(defun ensime-goto-test--test-template-default ()
""
"package %TESTPACKAGE%
/*
This is a stub test class. To learn how to customize it,
see the documentation for `ensime-goto-test-configs'
*/
class %TESTCLASS% {
}
")
(defun ensime-goto-test--test-template-scalatest-2 ()
""
"package %TESTPACKAGE%
import org.scalatest.FunSpec
import org.scalatest.Matchers
class %TESTCLASS% extends FunSpec with Matchers {
describe (\"%IMPLPACKAGE%.%IMPLCLASS%\") {
it(\"should have a test!\") {
assert(1 === 0)
}
}
}
")
(defun ensime-goto-test--test-template-scalatest-1 ()
""
"package %TESTPACKAGE%
import org.scalatest.FunSpec
import org.scalatest.matchers.ShouldMatchers
class %TESTCLASS% extends FunSpec with ShouldMatchers {
describe (\"%IMPLPACKAGE%.%IMPLCLASS%\") {
it(\"should have a test!\") {
assert(1 === 0)
}
}
}
")
(defun ensime-goto-test--test-template-scalatest-wordspec ()
"ENSIME template for a ScalaCheck WordSpec style test."
"package %TESTPACKAGE%
import org.scalatest._
class %TESTCLASS% extends WordSpec with Matchers {
\"%IMPLCLASS%\" should {
\"have a test!\" in {
fail(\"no test\")
}
}
}
")
(defun ensime-goto-test--test-template-scalatest-flatspec ()
"ENSIME template for a ScalaCheck FlatSpec style test."
"package %TESTPACKAGE%
import org.scalatest._
class %TESTCLASS% extends FlatSpec with Matchers {
\"%IMPLCLASS%\" should \"have a test!\" in {
fail(\"no test\")
}
}
")
(defun ensime-goto-test--test-template-scalacheck ()
""
"package %TESTPACKAGE%
import org.scalacheck.Properties
import org.scalacheck.Prop.forAll
import %IMPLPACKAGE%.%IMPLCLASS%
object %TESTCLASS% extends Properties(\"%IMPLPACKAGE%.%IMPLCLASS%\") {
property(\"test\") = forAll { x: %IMPLCLASS% =>
l == 0
}
}")
(defun ensime-goto-test--test-template-specs2 ()
""
"package %TESTPACKAGE%
import org.specs2.mutable._
import %IMPLPACKAGE%.%IMPLCLASS%
class %TESTCLASS% extends Specification {
\"A %IMPLPACKAGE%.%IMPLCLASS%\" should {
\"have a test\" in {
1 must beEqualTo(0)
}
}
}")
(provide 'ensime-goto-testfile)
;; Local Variables:
;; End: