-
Notifications
You must be signed in to change notification settings - Fork 52
/
malabar-test.el
131 lines (117 loc) · 5.43 KB
/
malabar-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
;;; malabar-test.el --- Test handling for malabar-mode
;;
;; Copyright (c) 2009 Espen Wiborg <espenhw@grumblesmurf.org>
;;
;; 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., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301 USA.
;;
(require 'malabar-misc)
(require 'malabar-util)
(defcustom malabar-test-class-suffix "Test"
"The suffix of a test class."
:group 'malabar-mode
:type 'string)
(defvar malabar-failed-maven-test-re "^ \\([[:alnum:]]+\\)(\\([[:alnum:].]+\\))$")
(defvar malabar-failed-junit-test-re "^ Failure point: \\([^:]+\\):\\([0-9]+\\)$")
(defun malabar-test-class-buffer-p (buffer)
(let* ((type-tag (car (semantic-brute-find-tag-by-class 'type buffer)))
(superclasses (semantic-tag-type-superclasses type-tag)))
(or (member "TestCase" superclasses)
(member "junit.framework.TestCase" superclasses)
(member "TestSuite" superclasses)
(member "junit.framework.TestSuite" superclasses)
(some (lambda (member-tag)
(remove-if-not (lambda (m)
(and (string= "@" (substring m 0 1))
(string-ends-with m "Test")))
(semantic-tag-modifiers member-tag)))
(semantic-tag-type-members type-tag)))))
(defun malabar-find-test-class-from-error ()
(let* ((class-name (match-string-no-properties 2))
(class-file (malabar-class-name-to-filename class-name)))
(list
(locate-file class-file
malabar-compilation-project-test-source-directories))))
(defun malabar-find-test-class-from-junit-failure ()
(let ((class-file (match-string-no-properties 1)))
(list (or (malabar--locate-in-path class-file malabar-compilation-project-test-source-directories)
class-file))))
(defun malabar-corresponding-test-class-name (buffer)
(let ((package (malabar-get-package-name buffer))
(type-tag (car (semantic-brute-find-tag-by-class 'type buffer))))
(let ((class (concat (semantic-tag-name type-tag) malabar-test-class-suffix)))
(if package
(concat package "." class)
class))))
(defun malabar-visit-corresponding-test (&optional buffer silent)
(interactive)
(let ((buffer (or buffer (current-buffer))))
(if (malabar-test-class-buffer-p buffer)
buffer
(let ((class-file (malabar-class-name-to-filename
(malabar-corresponding-test-class-name buffer)
(file-name-extension (buffer-file-name buffer) t)))
(test-source-directories (malabar-project-test-source-directories
(malabar-find-project-file buffer))))
(funcall
(if silent #'find-file-noselect #'find-file)
(or (locate-file class-file test-source-directories)
(expand-file-name class-file (car test-source-directories))))))))
(defun malabar-run-test-internal (test-starter &optional requires-qualification)
(with-current-buffer (malabar-visit-corresponding-test (current-buffer) t)
(malabar-setup-compilation-buffer)
(setq malabar-compilation-project-test-source-directories
(malabar-project-test-source-directories malabar-compilation-project-file))
(display-buffer malabar-groovy-compilation-buffer-name t)
(malabar-groovy-eval-as-compilation
(format test-starter
(if requires-qualification
(malabar-qualified-class-name-of-buffer (current-buffer))
(malabar-unqualified-class-name-of-buffer (current-buffer)))))))
(defun malabar-run-junit-test ()
"Runs the current buffer (or its corresponding test) as a
standalone JUnit test."
(interactive)
(let ((cur-buf (current-buffer)))
(malabar-compile-file)
(with-current-buffer (malabar-visit-corresponding-test cur-buf t)
(unless (eq cur-buf (current-buffer))
(malabar-compile-file))
(malabar-run-test-internal
(format "%s.runJunit('%%s')"
(malabar-project cur-buf))
t))))
(defun malabar-run-test ()
"Runs the current buffer (or its corresponding test) as a test,
using 'mvn test -Dtestname'."
(interactive)
(malabar-run-test-internal
(format "%s.runtest('%%s')"
(malabar-project (current-buffer)))))
(defun malabar-run-all-tests ()
"Runs all project tests ('mvn test')."
(interactive)
(malabar-build-project 'test))
(add-to-list 'compilation-error-regexp-alist
(list malabar-failed-maven-test-re ;; RE
'malabar-find-test-class-from-error)) ;; FILE
(add-to-list 'compilation-error-regexp-alist
(list malabar-failed-junit-test-re ;; RE
'malabar-find-test-class-from-junit-failure ;; FILE
2)) ;; LINE
(provide 'malabar-test)
;; Local Variables:
;; byte-compile-warnings:(not cl-functions)
;; End: