cedet-utests.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. ;;; cedet-utests.el --- Run all unit tests in the CEDET suite.
  2. ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Remembering to run all the unit tests available in CEDET one at a
  18. ;; time is a bit time consuming. This links all the tests together
  19. ;; into one command.
  20. (require 'cedet)
  21. ;;; Code:
  22. (defvar cedet-utest-test-alist
  23. '(
  24. ;;
  25. ;; COMMON
  26. ;;
  27. ;; Test inversion
  28. ("inversion" . inversion-unit-test)
  29. ;; EZ Image dumping.
  30. ("ezimage associations" . ezimage-image-association-dump)
  31. ("ezimage images" . ezimage-image-dump)
  32. ;; Pulse
  33. ("pulse interactive test" . (lambda () (pulse-test t)))
  34. ;; Files
  35. ("cedet file conversion" . cedet-files-utest)
  36. ;;
  37. ;; EIEIO
  38. ;;
  39. ("eieio" . (lambda () (let ((lib (locate-library "eieio-tests.el"
  40. t)))
  41. (load-file lib))))
  42. ("eieio: browser" . eieio-browse)
  43. ("eieio: custom" . (lambda ()
  44. (require 'eieio-custom)
  45. (customize-variable 'eieio-widget-test)))
  46. ("eieio: chart" . (lambda ()
  47. (if (cedet-utest-noninteractive)
  48. (message " ** Skipping test in noninteractive mode.")
  49. (chart-test-it-all))))
  50. ;;
  51. ;; EDE
  52. ;;
  53. ;; @todo - Currently handled in the integration tests. Need
  54. ;; some simpler unit tests here.
  55. ;;
  56. ;; SEMANTIC
  57. ;;
  58. ("semantic: lex spp table write" . semantic-lex-spp-write-utest)
  59. ("semantic: multi-lang parsing" . semantic-utest-main)
  60. ("semantic: C preprocessor" . semantic-utest-c)
  61. ("semantic: analyzer tests" . semantic-ia-utest)
  62. ("semanticdb: data cache" . semantic-test-data-cache)
  63. ("semantic: throw-on-input" .
  64. (lambda ()
  65. (if (cedet-utest-noninteractive)
  66. (message " ** Skipping test in noninteractive mode.")
  67. (semantic-test-throw-on-input))))
  68. ("semantic: gcc: output parse test" . semantic-gcc-test-output-parser)
  69. ;;
  70. ;; SRECODE
  71. ;;
  72. ("srecode: fields" . srecode-field-utest)
  73. ("srecode: templates" . srecode-utest-template-output)
  74. ("srecode: show maps" . srecode-get-maps)
  75. ("srecode: getset" . srecode-utest-getset-output)
  76. )
  77. "Alist of all the tests in CEDET we should run.")
  78. (defvar cedet-running-master-tests nil
  79. "Non-nil when CEDET-utest is running all the tests.")
  80. (defun cedet-utest (&optional exit-on-error)
  81. "Run the CEDET unit tests.
  82. EXIT-ON-ERROR causes the test suite to exit on an error, instead
  83. of just logging the error."
  84. (interactive)
  85. (if (or (not (featurep 'semanticdb-mode))
  86. (not (semanticdb-minor-mode-p)))
  87. (error "CEDET Tests require: M-x semantic-load-enable-minimum-features"))
  88. (cedet-utest-log-setup "ALL TESTS")
  89. (let ((tl cedet-utest-test-alist)
  90. (notes nil)
  91. (err nil)
  92. (start (current-time))
  93. (end nil)
  94. (cedet-running-master-tests t)
  95. )
  96. (dolist (T tl)
  97. (cedet-utest-add-log-item-start (car T))
  98. (setq notes nil err nil)
  99. (condition-case Cerr
  100. (progn
  101. (funcall (cdr T))
  102. )
  103. (error
  104. (setq err (format "ERROR: %S" Cerr))
  105. ;;(message "Error caught: %s" Cerr)
  106. ))
  107. ;; Cleanup stray input and events that are in the way.
  108. ;; Not doing this causes sit-for to not refresh the screen.
  109. ;; Doing this causes the user to need to press keys more frequently.
  110. (when (and (interactive-p) (input-pending-p))
  111. (if (fboundp 'read-event)
  112. (read-event)
  113. (read-char)))
  114. (cedet-utest-add-log-item-done notes err)
  115. (when (and exit-on-error err)
  116. (message "to debug this test point, execute:")
  117. (message "%S" (cdr T))
  118. (message "\n ** Exiting Test Suite. ** \n")
  119. (throw 'cedet-utest-exit-on-error t)
  120. )
  121. )
  122. (setq end (current-time))
  123. (cedet-utest-log-shutdown-msg "ALL TESTS" start end)
  124. nil))
  125. (defun cedet-utest-noninteractive ()
  126. "Return non-nil if running non-interactively."
  127. (if (featurep 'xemacs)
  128. (noninteractive)
  129. noninteractive))
  130. ;;;###autoload
  131. (defun cedet-utest-batch ()
  132. "Run the CEDET unit test in BATCH mode."
  133. (unless (cedet-utest-noninteractive)
  134. (error "`cedet-utest-batch' is to be used only with -batch"))
  135. (condition-case err
  136. (when (catch 'cedet-utest-exit-on-error
  137. ;; Get basic semantic features up.
  138. (semantic-load-enable-minimum-features)
  139. ;; Disables all caches related to semantic DB so all
  140. ;; tests run as if we have bootstrapped CEDET for the
  141. ;; first time.
  142. (setq-default semanticdb-new-database-class 'semanticdb-project-database)
  143. (message "Disabling existing Semantic Database Caches.")
  144. ;; Disabling the srecoder map, we won't load a pre-existing one
  145. ;; and will be forced to bootstrap a new one.
  146. (setq srecode-map-save-file nil)
  147. ;; Run the tests
  148. (cedet-utest t)
  149. )
  150. (kill-emacs 1))
  151. (error
  152. (error "Error in unit test harness:\n %S" err))
  153. )
  154. )
  155. ;;; Logging utility.
  156. ;;
  157. (defvar cedet-utest-frame nil
  158. "Frame used during cedet unit test logging.")
  159. (defvar cedet-utest-buffer nil
  160. "Frame used during cedet unit test logging.")
  161. (defvar cedet-utest-frame-parameters
  162. '((name . "CEDET-UTEST")
  163. (width . 80)
  164. (height . 25)
  165. (minibuffer . t))
  166. "Frame parameters used for the cedet utest log frame.")
  167. (defvar cedet-utest-last-log-item nil
  168. "Remember the last item we were logging for.")
  169. (defvar cedet-utest-log-timer nil
  170. "During a test, track the start time.")
  171. (defun cedet-utest-log-setup (&optional title)
  172. "Setup a frame and buffer for unit testing.
  173. Optional argument TITLE is the title of this testing session."
  174. (setq cedet-utest-log-timer (current-time))
  175. (if (cedet-utest-noninteractive)
  176. (message "\n>> Setting up %s tests to run @ %s\n"
  177. (or title "")
  178. (current-time-string))
  179. ;; Interactive mode needs a frame and buffer.
  180. (when (or (not cedet-utest-frame) (not (frame-live-p cedet-utest-frame)))
  181. (setq cedet-utest-frame (make-frame cedet-utest-frame-parameters)))
  182. (when (or (not cedet-utest-buffer) (not (buffer-live-p cedet-utest-buffer)))
  183. (setq cedet-utest-buffer (get-buffer-create "*CEDET utest log*")))
  184. (save-excursion
  185. (set-buffer cedet-utest-buffer)
  186. (setq cedet-utest-last-log-item nil)
  187. (when (not cedet-running-master-tests)
  188. (erase-buffer))
  189. (insert "\n\nSetting up "
  190. (or title "")
  191. " tests to run @ " (current-time-string) "\n\n"))
  192. (let ((oframe (selected-frame)))
  193. (unwind-protect
  194. (progn
  195. (select-frame cedet-utest-frame)
  196. (switch-to-buffer cedet-utest-buffer t))
  197. (select-frame oframe)))
  198. ))
  199. (defun cedet-utest-elapsed-time (start end)
  200. "Copied from elp.el. Was elp-elapsed-time.
  201. Argument START and END bound the time being calculated."
  202. (+ (* (- (car end) (car start)) 65536.0)
  203. (- (car (cdr end)) (car (cdr start)))
  204. (/ (- (car (cdr (cdr end))) (car (cdr (cdr start)))) 1000000.0)))
  205. (defun cedet-utest-log-shutdown (title &optional errorcondition)
  206. "Shut-down a larger test suite.
  207. TITLE is the section that is done.
  208. ERRORCONDITION is some error that may have occurred during testing."
  209. (let ((endtime (current-time))
  210. )
  211. (cedet-utest-log-shutdown-msg title cedet-utest-log-timer endtime)
  212. (setq cedet-utest-log-timer nil)
  213. ))
  214. (defun cedet-utest-log-shutdown-msg (title startime endtime)
  215. "Show a shutdown message with TITLE, STARTIME, and ENDTIME."
  216. (if (cedet-utest-noninteractive)
  217. (progn
  218. (message "\n>> Test Suite %s ended at @ %s"
  219. title
  220. (format-time-string "%c" endtime))
  221. (message " Elapsed Time %.2f Seconds\n"
  222. (cedet-utest-elapsed-time startime endtime)))
  223. (save-excursion
  224. (set-buffer cedet-utest-buffer)
  225. (goto-char (point-max))
  226. (insert "\n>> Test Suite " title " ended at @ "
  227. (format-time-string "%c" endtime) "\n"
  228. " Elapsed Time "
  229. (number-to-string
  230. (cedet-utest-elapsed-time startime endtime))
  231. " Seconds\n * "))
  232. ))
  233. (defun cedet-utest-show-log-end ()
  234. "Show the end of the current unit test log."
  235. (unless (cedet-utest-noninteractive)
  236. (let* ((cb (current-buffer))
  237. (cf (selected-frame))
  238. (bw (or (get-buffer-window cedet-utest-buffer t)
  239. (get-buffer-window (switch-to-buffer cedet-utest-buffer) t)))
  240. (lf (window-frame bw))
  241. )
  242. (select-frame lf)
  243. (select-window bw)
  244. (goto-char (point-max))
  245. (select-frame cf)
  246. (set-buffer cb)
  247. )))
  248. (defun cedet-utest-post-command-hook ()
  249. "Hook run after the current log command was run."
  250. (if (cedet-utest-noninteractive)
  251. (message "")
  252. (save-excursion
  253. (set-buffer cedet-utest-buffer)
  254. (goto-char (point-max))
  255. (insert "\n\n")))
  256. (setq cedet-utest-last-log-item nil)
  257. (remove-hook 'post-command-hook 'cedet-utest-post-command-hook)
  258. )
  259. (defun cedet-utest-add-log-item-start (item)
  260. "Add ITEM into the log as being started."
  261. (unless (equal item cedet-utest-last-log-item)
  262. (setq cedet-utest-last-log-item item)
  263. ;; This next line makes sure we clear out status during logging.
  264. (add-hook 'post-command-hook 'cedet-utest-post-command-hook)
  265. (if (cedet-utest-noninteractive)
  266. (message " - Running %s ..." item)
  267. (save-excursion
  268. (set-buffer cedet-utest-buffer)
  269. (goto-char (point-max))
  270. (when (not (bolp)) (insert "\n"))
  271. (insert "Running " item " ... ")
  272. (sit-for 0)
  273. ))
  274. (cedet-utest-show-log-end)
  275. ))
  276. (defun cedet-utest-add-log-item-done (&optional notes err precr)
  277. "Add into the log that the last item is done.
  278. Apply NOTES to the doneness of the log.
  279. Apply ERR if there was an error in previous item.
  280. Optional argument PRECR indicates to prefix the done msg w/ a newline."
  281. (if (cedet-utest-noninteractive)
  282. ;; Non-interactive-mode - show a message.
  283. (if notes
  284. (message " * %s {%s}" (or err "done") notes)
  285. (message " * %s" (or err "done")))
  286. ;; Interactive-mode - insert into the buffer.
  287. (save-excursion
  288. (set-buffer cedet-utest-buffer)
  289. (goto-char (point-max))
  290. (when precr (insert "\n"))
  291. (if err
  292. (insert err)
  293. (insert "done")
  294. (when notes (insert " (" notes ")")))
  295. (insert "\n")
  296. (setq cedet-utest-last-log-item nil)
  297. (sit-for 0)
  298. )))
  299. ;;; INDIVIDUAL TEST API
  300. ;;
  301. ;; Use these APIs to start and log information.
  302. ;;
  303. ;; The other fcns will be used to log across all the tests at once.
  304. (defun cedet-utest-log-start (testname)
  305. "Setup the log for the test TESTNAME."
  306. ;; Make sure we have a log buffer.
  307. (save-window-excursion
  308. (when (or (not cedet-utest-buffer)
  309. (not (buffer-live-p cedet-utest-buffer))
  310. (not (get-buffer-window cedet-utest-buffer t))
  311. )
  312. (cedet-utest-log-setup))
  313. ;; Add our startup message.
  314. (cedet-utest-add-log-item-start testname)
  315. ))
  316. (defun cedet-utest-log(format &rest args)
  317. "Log the text string FORMAT.
  318. The rest of the ARGS are used to fill in FORMAT with `format'."
  319. (if (cedet-utest-noninteractive)
  320. (apply 'message format args)
  321. (save-excursion
  322. (set-buffer cedet-utest-buffer)
  323. (goto-char (point-max))
  324. (when (not (bolp)) (insert "\n"))
  325. (insert (apply 'format format args))
  326. (insert "\n")
  327. (sit-for 0)
  328. ))
  329. (cedet-utest-show-log-end)
  330. )
  331. ;;; Inversion tests
  332. (defun inversion-unit-test ()
  333. "Test inversion to make sure it can identify different version strings."
  334. (interactive)
  335. (let ((c1 (inversion-package-version 'inversion))
  336. (c1i (inversion-package-incompatibility-version 'inversion))
  337. (c2 (inversion-decode-version "1.3alpha2"))
  338. (c3 (inversion-decode-version "1.3beta4"))
  339. (c4 (inversion-decode-version "1.3 beta5"))
  340. (c5 (inversion-decode-version "1.3.4"))
  341. (c6 (inversion-decode-version "2.3alpha"))
  342. (c7 (inversion-decode-version "1.3"))
  343. (c8 (inversion-decode-version "1.3pre1"))
  344. (c9 (inversion-decode-version "2.4 (patch 2)"))
  345. (c10 (inversion-decode-version "2.4 (patch 3)"))
  346. (c11 (inversion-decode-version "2.4.2.1"))
  347. (c12 (inversion-decode-version "2.4.2.2"))
  348. )
  349. (if (not (and
  350. (inversion-= c1 c1)
  351. (inversion-< c1i c1)
  352. (inversion-< c2 c3)
  353. (inversion-< c3 c4)
  354. (inversion-< c4 c5)
  355. (inversion-< c5 c6)
  356. (inversion-< c2 c4)
  357. (inversion-< c2 c5)
  358. (inversion-< c2 c6)
  359. (inversion-< c3 c5)
  360. (inversion-< c3 c6)
  361. (inversion-< c7 c6)
  362. (inversion-< c4 c7)
  363. (inversion-< c2 c7)
  364. (inversion-< c8 c6)
  365. (inversion-< c8 c7)
  366. (inversion-< c4 c8)
  367. (inversion-< c2 c8)
  368. (inversion-< c9 c10)
  369. (inversion-< c10 c11)
  370. (inversion-< c11 c12)
  371. ;; Negatives
  372. (not (inversion-< c3 c2))
  373. (not (inversion-< c4 c3))
  374. (not (inversion-< c5 c4))
  375. (not (inversion-< c6 c5))
  376. (not (inversion-< c7 c2))
  377. (not (inversion-< c7 c8))
  378. (not (inversion-< c12 c11))
  379. ;; Test the tester on inversion
  380. (not (inversion-test 'inversion inversion-version))
  381. ;; Test that we throw an error
  382. (inversion-test 'inversion "0.0.0")
  383. (inversion-test 'inversion "1000.0")
  384. ))
  385. (error "Inversion tests failed")
  386. (message "Inversion tests passed."))))
  387. ;;; cedet-files unit test
  388. (defvar cedet-files-utest-list
  389. '(
  390. ( "/home/me/src/myproj/src/foo.c" . "!home!me!src!myproj!src!foo.c" )
  391. ( "c:/work/myproj/foo.el" . "!drive_c!work!myproj!foo.el" )
  392. ( "//windows/proj/foo.java" . "!!windows!proj!foo.java" )
  393. ( "/home/me/proj!bang/foo.c" . "!home!me!proj!!bang!foo.c" )
  394. )
  395. "List of different file names to test.
  396. Each entry is a cons cell of ( FNAME . CONVERTED )
  397. where FNAME is some file name, and CONVERTED is what it should be
  398. converted into.")
  399. (defun cedet-files-utest ()
  400. "Test out some file name conversions."
  401. (interactive)
  402. (let ((idx 0))
  403. (dolist (FT cedet-files-utest-list)
  404. (setq idx (+ idx 1))
  405. (let ((dir->file (cedet-directory-name-to-file-name (car FT) t))
  406. (file->dir (cedet-file-name-to-directory-name (cdr FT) t))
  407. )
  408. (unless (string= (cdr FT) dir->file)
  409. (error "Failed: %d. Found: %S Wanted: %S"
  410. idx dir->file (cdr FT))
  411. )
  412. (unless (string= file->dir (car FT))
  413. (error "Failed: %d. Found: %S Wanted: %S"
  414. idx file->dir (car FT)))))))
  415. ;;; pulse test
  416. (defun pulse-test (&optional no-error)
  417. "Test the lightening function for pulsing a line.
  418. When optional NO-ERROR don't throw an error if we can't run tests."
  419. (interactive)
  420. (if (or (not pulse-flag) (not (pulse-available-p)))
  421. (if no-error
  422. nil
  423. (error (concat "Pulse test only works on versions of Emacs"
  424. " that support pulsing")))
  425. ;; Run the tests
  426. (when (interactive-p)
  427. (message "<Press a key> Pulse one line.")
  428. (read-char))
  429. (pulse-momentary-highlight-one-line (point))
  430. (when (interactive-p)
  431. (message "<Press a key> Pulse a region.")
  432. (read-char))
  433. (pulse-momentary-highlight-region (point)
  434. (save-excursion
  435. (condition-case nil
  436. (forward-char 30)
  437. (error nil))
  438. (point)))
  439. (when (interactive-p)
  440. (message "<Press a key> Pulse line a specific color.")
  441. (read-char))
  442. (pulse-momentary-highlight-one-line (point) 'modeline)
  443. (when (interactive-p)
  444. (message "<Press a key> Pulse a pre-existing overlay.")
  445. (read-char))
  446. (let* ((start (point-at-bol))
  447. (end (save-excursion
  448. (end-of-line)
  449. (when (not (eobp))
  450. (forward-char 1))
  451. (point)))
  452. (o (make-overlay start end))
  453. )
  454. (pulse-momentary-highlight-overlay o)
  455. (if (overlay-buffer o)
  456. (delete-overlay o)
  457. (error "Non-temporary overlay was deleted!"))
  458. )
  459. (when (interactive-p)
  460. (message "Done!"))))
  461. (provide 'cedet-utests)
  462. ;;; cedet-utests.el ends here