1 ;;; cedet-utests.el --- Run all unit tests in the CEDET suite.
3 ;; Copyright (C) 2008-2013 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Remembering to run all the unit tests available in CEDET one at a
25 ;; time is a bit time consuming. This links all the tests together
30 (defvar cedet-utest-test-alist
37 ("inversion" . inversion-unit-test
)
40 ("ezimage associations" . ezimage-image-association-dump
)
41 ("ezimage images" . ezimage-image-dump
)
44 ("pulse interactive test" .
(lambda () (pulse-test t
)))
47 ("cedet file conversion" . cedet-files-utest
)
52 ("eieio" .
(lambda () (let ((lib (locate-library "eieio-tests.el"
55 ("eieio: browser" . eieio-browse
)
56 ("eieio: custom" .
(lambda ()
57 (require 'eieio-custom
)
58 (customize-variable 'eieio-widget-test
)))
59 ("eieio: chart" .
(lambda ()
60 (if (cedet-utest-noninteractive)
61 (message " ** Skipping test in noninteractive mode.")
62 (chart-test-it-all))))
67 ;; @todo - Currently handled in the integration tests. Need
68 ;; some simpler unit tests here.
73 ("semantic: lex spp table write" . semantic-lex-spp-write-utest
)
74 ("semantic: multi-lang parsing" . semantic-utest-main
)
75 ("semantic: C preprocessor" . semantic-utest-c
)
76 ("semantic: analyzer tests" . semantic-ia-utest
)
77 ("semanticdb: data cache" . semantic-test-data-cache
)
78 ("semantic: throw-on-input" .
80 (if (cedet-utest-noninteractive)
81 (message " ** Skipping test in noninteractive mode.")
82 (semantic-test-throw-on-input))))
84 ("semantic: gcc: output parse test" . semantic-gcc-test-output-parser
)
88 ("srecode: fields" . srecode-field-utest
)
89 ("srecode: templates" . srecode-utest-template-output
)
90 ("srecode: show maps" . srecode-get-maps
)
91 ("srecode: getset" . srecode-utest-getset-output
)
93 "Alist of all the tests in CEDET we should run.")
95 (defvar cedet-running-master-tests nil
96 "Non-nil when CEDET-utest is running all the tests.")
98 (defun cedet-utest (&optional exit-on-error
)
99 "Run the CEDET unit tests.
100 EXIT-ON-ERROR causes the test suite to exit on an error, instead
101 of just logging the error."
103 (if (or (not (featurep 'semanticdb-mode
))
104 (not (semanticdb-minor-mode-p)))
105 (error "CEDET Tests require: M-x semantic-load-enable-minimum-features"))
106 (cedet-utest-log-setup "ALL TESTS")
107 (let ((tl cedet-utest-test-alist
)
110 (start (current-time))
112 (cedet-running-master-tests t
)
115 (cedet-utest-add-log-item-start (car T
))
116 (setq notes nil err nil
)
122 (setq err
(format "ERROR: %S" Cerr
))
123 ;;(message "Error caught: %s" Cerr)
126 ;; Cleanup stray input and events that are in the way.
127 ;; Not doing this causes sit-for to not refresh the screen.
128 ;; Doing this causes the user to need to press keys more frequently.
129 (when (and (interactive-p) (input-pending-p))
130 (if (fboundp 'read-event
)
134 (cedet-utest-add-log-item-done notes err
)
135 (when (and exit-on-error err
)
136 (message "to debug this test point, execute:")
137 (message "%S" (cdr T
))
138 (message "\n ** Exiting Test Suite. ** \n")
139 (throw 'cedet-utest-exit-on-error t
)
142 (setq end
(current-time))
143 (cedet-utest-log-shutdown-msg "ALL TESTS" start end
)
146 (defun cedet-utest-noninteractive ()
147 "Return non-nil if running non-interactively."
148 (if (featurep 'xemacs
)
153 (defun cedet-utest-batch ()
154 "Run the CEDET unit test in BATCH mode."
155 (unless (cedet-utest-noninteractive)
156 (error "`cedet-utest-batch' is to be used only with -batch"))
158 (when (catch 'cedet-utest-exit-on-error
159 ;; Get basic semantic features up.
160 (semantic-load-enable-minimum-features)
161 ;; Disables all caches related to semantic DB so all
162 ;; tests run as if we have bootstrapped CEDET for the
164 (setq-default semanticdb-new-database-class
'semanticdb-project-database
)
165 (message "Disabling existing Semantic Database Caches.")
167 ;; Disabling the srecoder map, we won't load a pre-existing one
168 ;; and will be forced to bootstrap a new one.
169 (setq srecode-map-save-file nil
)
176 (error "Error in unit test harness:\n %S" err
))
182 (defvar cedet-utest-frame nil
183 "Frame used during cedet unit test logging.")
184 (defvar cedet-utest-buffer nil
185 "Frame used during cedet unit test logging.")
186 (defvar cedet-utest-frame-parameters
187 '((name .
"CEDET-UTEST")
191 "Frame parameters used for the cedet utest log frame.")
193 (defvar cedet-utest-last-log-item nil
194 "Remember the last item we were logging for.")
196 (defvar cedet-utest-log-timer nil
197 "During a test, track the start time.")
199 (defun cedet-utest-log-setup (&optional title
)
200 "Setup a frame and buffer for unit testing.
201 Optional argument TITLE is the title of this testing session."
202 (setq cedet-utest-log-timer
(current-time))
203 (if (cedet-utest-noninteractive)
204 (message "\n>> Setting up %s tests to run @ %s\n"
206 (current-time-string))
208 ;; Interactive mode needs a frame and buffer.
209 (when (or (not cedet-utest-frame
) (not (frame-live-p cedet-utest-frame
)))
210 (setq cedet-utest-frame
(make-frame cedet-utest-frame-parameters
)))
211 (when (or (not cedet-utest-buffer
) (not (buffer-live-p cedet-utest-buffer
)))
212 (setq cedet-utest-buffer
(get-buffer-create "*CEDET utest log*")))
214 (set-buffer cedet-utest-buffer
)
215 (setq cedet-utest-last-log-item nil
)
216 (when (not cedet-running-master-tests
)
218 (insert "\n\nSetting up "
220 " tests to run @ " (current-time-string) "\n\n"))
221 (let ((oframe (selected-frame)))
224 (select-frame cedet-utest-frame
)
225 (switch-to-buffer cedet-utest-buffer t
))
226 (select-frame oframe
)))
229 (defun cedet-utest-elapsed-time (start end
)
230 "Copied from elp.el. Was elp-elapsed-time.
231 Argument START and END bound the time being calculated."
232 (+ (* (- (car end
) (car start
)) 65536.0)
233 (- (car (cdr end
)) (car (cdr start
)))
234 (/ (- (car (cdr (cdr end
))) (car (cdr (cdr start
)))) 1000000.0)))
236 (defun cedet-utest-log-shutdown (title &optional errorcondition
)
237 "Shut-down a larger test suite.
238 TITLE is the section that is done.
239 ERRORCONDITION is some error that may have occurred during testing."
240 (let ((endtime (current-time))
242 (cedet-utest-log-shutdown-msg title cedet-utest-log-timer endtime
)
243 (setq cedet-utest-log-timer nil
)
246 (defun cedet-utest-log-shutdown-msg (title startime endtime
)
247 "Show a shutdown message with TITLE, STARTIME, and ENDTIME."
248 (if (cedet-utest-noninteractive)
250 (message "\n>> Test Suite %s ended at @ %s"
252 (format-time-string "%c" endtime
))
253 (message " Elapsed Time %.2f Seconds\n"
254 (cedet-utest-elapsed-time startime endtime
)))
257 (set-buffer cedet-utest-buffer
)
258 (goto-char (point-max))
259 (insert "\n>> Test Suite " title
" ended at @ "
260 (format-time-string "%c" endtime
) "\n"
263 (cedet-utest-elapsed-time startime endtime
))
267 (defun cedet-utest-show-log-end ()
268 "Show the end of the current unit test log."
269 (unless (cedet-utest-noninteractive)
270 (let* ((cb (current-buffer))
271 (cf (selected-frame))
272 (bw (or (get-buffer-window cedet-utest-buffer t
)
273 (get-buffer-window (switch-to-buffer cedet-utest-buffer
) t
)))
274 (lf (window-frame bw
))
278 (goto-char (point-max))
283 (defun cedet-utest-post-command-hook ()
284 "Hook run after the current log command was run."
285 (if (cedet-utest-noninteractive)
288 (set-buffer cedet-utest-buffer
)
289 (goto-char (point-max))
291 (setq cedet-utest-last-log-item nil
)
292 (remove-hook 'post-command-hook
'cedet-utest-post-command-hook
)
295 (defun cedet-utest-add-log-item-start (item)
296 "Add ITEM into the log as being started."
297 (unless (equal item cedet-utest-last-log-item
)
298 (setq cedet-utest-last-log-item item
)
299 ;; This next line makes sure we clear out status during logging.
300 (add-hook 'post-command-hook
'cedet-utest-post-command-hook
)
302 (if (cedet-utest-noninteractive)
303 (message " - Running %s ..." item
)
305 (set-buffer cedet-utest-buffer
)
306 (goto-char (point-max))
307 (when (not (bolp)) (insert "\n"))
308 (insert "Running " item
" ... ")
311 (cedet-utest-show-log-end)
314 (defun cedet-utest-add-log-item-done (&optional notes err precr
)
315 "Add into the log that the last item is done.
316 Apply NOTES to the doneness of the log.
317 Apply ERR if there was an error in previous item.
318 Optional argument PRECR indicates to prefix the done msg w/ a newline."
319 (if (cedet-utest-noninteractive)
320 ;; Non-interactive-mode - show a message.
322 (message " * %s {%s}" (or err
"done") notes
)
323 (message " * %s" (or err
"done")))
324 ;; Interactive-mode - insert into the buffer.
326 (set-buffer cedet-utest-buffer
)
327 (goto-char (point-max))
328 (when precr
(insert "\n"))
332 (when notes
(insert " (" notes
")")))
334 (setq cedet-utest-last-log-item nil
)
338 ;;; INDIVIDUAL TEST API
340 ;; Use these APIs to start and log information.
342 ;; The other fcns will be used to log across all the tests at once.
343 (defun cedet-utest-log-start (testname)
344 "Setup the log for the test TESTNAME."
345 ;; Make sure we have a log buffer.
346 (save-window-excursion
347 (when (or (not cedet-utest-buffer
)
348 (not (buffer-live-p cedet-utest-buffer
))
349 (not (get-buffer-window cedet-utest-buffer t
))
351 (cedet-utest-log-setup))
352 ;; Add our startup message.
353 (cedet-utest-add-log-item-start testname
)
356 (defun cedet-utest-log(format &rest args
)
357 "Log the text string FORMAT.
358 The rest of the ARGS are used to fill in FORMAT with `format'."
359 (if (cedet-utest-noninteractive)
360 (apply 'message format args
)
362 (set-buffer cedet-utest-buffer
)
363 (goto-char (point-max))
364 (when (not (bolp)) (insert "\n"))
365 (insert (apply 'format format args
))
369 (cedet-utest-show-log-end)
374 (defun inversion-unit-test ()
375 "Test inversion to make sure it can identify different version strings."
377 (let ((c1 (inversion-package-version 'inversion
))
378 (c1i (inversion-package-incompatibility-version 'inversion
))
379 (c2 (inversion-decode-version "1.3alpha2"))
380 (c3 (inversion-decode-version "1.3beta4"))
381 (c4 (inversion-decode-version "1.3 beta5"))
382 (c5 (inversion-decode-version "1.3.4"))
383 (c6 (inversion-decode-version "2.3alpha"))
384 (c7 (inversion-decode-version "1.3"))
385 (c8 (inversion-decode-version "1.3pre1"))
386 (c9 (inversion-decode-version "2.4 (patch 2)"))
387 (c10 (inversion-decode-version "2.4 (patch 3)"))
388 (c11 (inversion-decode-version "2.4.2.1"))
389 (c12 (inversion-decode-version "2.4.2.2"))
411 (inversion-< c10 c11
)
412 (inversion-< c11 c12
)
414 (not (inversion-< c3 c2
))
415 (not (inversion-< c4 c3
))
416 (not (inversion-< c5 c4
))
417 (not (inversion-< c6 c5
))
418 (not (inversion-< c7 c2
))
419 (not (inversion-< c7 c8
))
420 (not (inversion-< c12 c11
))
421 ;; Test the tester on inversion
422 (not (inversion-test 'inversion inversion-version
))
423 ;; Test that we throw an error
424 (inversion-test 'inversion
"0.0.0")
425 (inversion-test 'inversion
"1000.0")
427 (error "Inversion tests failed")
428 (message "Inversion tests passed."))))
430 ;;; cedet-files unit test
432 (defvar cedet-files-utest-list
434 ( "/home/me/src/myproj/src/foo.c" .
"!home!me!src!myproj!src!foo.c" )
435 ( "c:/work/myproj/foo.el" .
"!drive_c!work!myproj!foo.el" )
436 ( "//windows/proj/foo.java" .
"!!windows!proj!foo.java" )
437 ( "/home/me/proj!bang/foo.c" .
"!home!me!proj!!bang!foo.c" )
439 "List of different file names to test.
440 Each entry is a cons cell of ( FNAME . CONVERTED )
441 where FNAME is some file name, and CONVERTED is what it should be
444 (defun cedet-files-utest ()
445 "Test out some file name conversions."
448 (dolist (FT cedet-files-utest-list
)
452 (let ((dir->file
(cedet-directory-name-to-file-name (car FT
) t
))
453 (file->dir
(cedet-file-name-to-directory-name (cdr FT
) t
))
456 (unless (string= (cdr FT
) dir-
>file
)
457 (error "Failed: %d. Found: %S Wanted: %S"
458 idx dir-
>file
(cdr FT
))
461 (unless (string= file-
>dir
(car FT
))
462 (error "Failed: %d. Found: %S Wanted: %S"
463 idx file-
>dir
(car FT
)))))))
467 (defun pulse-test (&optional no-error
)
468 "Test the lightening function for pulsing a line.
469 When optional NO-ERROR don't throw an error if we can't run tests."
471 (if (or (not pulse-flag
) (not (pulse-available-p)))
474 (error (concat "Pulse test only works on versions of Emacs"
475 " that support pulsing")))
477 (when (interactive-p)
478 (message "<Press a key> Pulse one line.")
480 (pulse-momentary-highlight-one-line (point))
481 (when (interactive-p)
482 (message "<Press a key> Pulse a region.")
484 (pulse-momentary-highlight-region (point)
490 (when (interactive-p)
491 (message "<Press a key> Pulse line a specific color.")
493 (pulse-momentary-highlight-one-line (point) 'modeline
)
494 (when (interactive-p)
495 (message "<Press a key> Pulse a pre-existing overlay.")
497 (let* ((start (point-at-bol))
503 (o (make-overlay start end
))
505 (pulse-momentary-highlight-overlay o
)
506 (if (overlay-buffer o
)
508 (error "Non-temporary overlay was deleted!"))
510 (when (interactive-p)
513 (provide 'cedet-utests
)
515 ;;; cedet-utests.el ends here