semantic-tests.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. ;;; semantic-utest.el --- Miscellaneous Semantic tests.
  2. ;;; Copyright (C) 2003-2004, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  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. ;; Originally, there are many test functions scattered among the
  17. ;; Semantic source files. This file consolidates them.
  18. (require 'data-debug)
  19. ;;; From semantic-complete
  20. (require 'semantic/complete)
  21. (defun semantic-complete-test ()
  22. "Test completion mechanisms."
  23. (interactive)
  24. (message "%S"
  25. (semantic-format-tag-prototype
  26. (semantic-complete-read-tag-project "Symbol: "))))
  27. ;;; From semanticdb-ebrowse
  28. (require 'semantic/db-ebrowse)
  29. (defun semanticdb-ebrowse-run-tests ()
  30. "Run some tests of the semanticdb-ebrowse system.
  31. All systems are different. Ask questions along the way."
  32. (interactive)
  33. (let ((doload nil))
  34. (when (y-or-n-p "Create a system database to test with? ")
  35. (call-interactively 'semanticdb-create-ebrowse-database)
  36. (setq doload t))
  37. ;; Should we load in caches
  38. (when (if doload
  39. (y-or-n-p "New database created. Reload system databases? ")
  40. (y-or-n-p "Load in all system databases? "))
  41. (semanticdb-load-ebrowse-caches)))
  42. ;; Ok, databases were created. Let's try some searching.
  43. (when (not (or (eq major-mode 'c-mode)
  44. (eq major-mode 'c++-mode)))
  45. (error "Please make your default buffer be a C or C++ file, then
  46. run the test again")))
  47. (defun semanticdb-ebrowse-dump ()
  48. "Find the first loaded ebrowse table, and dump out the contents."
  49. (interactive)
  50. (let ((db semanticdb-database-list)
  51. (ab nil))
  52. (while db
  53. (when (semanticdb-project-database-ebrowse-p (car db))
  54. (setq ab (data-debug-new-buffer "*EBROWSE Database*"))
  55. (data-debug-insert-thing (car db) "*" "")
  56. (setq db nil)
  57. )
  58. (setq db (cdr db)))))
  59. ;;; From semanticdb-global:
  60. (require 'semantic/db-global)
  61. (defvar semanticdb-test-gnu-global-startfile "~/src/global-5.7.3/global/global.c"
  62. "File to use for testing.")
  63. (defun semanticdb-test-gnu-global (searchfor &optional standardfile)
  64. "Test the GNU Global semanticdb.
  65. Argument SEARCHFOR is the text to search for.
  66. If optional arg STANDARDFILE is non-nil, use a standard file w/ global enabled."
  67. (interactive "sSearch For Tag: \nP")
  68. (require 'data-debug)
  69. (save-excursion
  70. (when standardfile
  71. (save-match-data
  72. (set-buffer (find-file-noselect semanticdb-test-gnu-global-startfile))))
  73. (condition-case err
  74. (semanticdb-enable-gnu-global-in-buffer)
  75. (error (if standardfile
  76. (error err)
  77. (save-match-data
  78. (set-buffer (find-file-noselect semanticdb-test-gnu-global-startfile)))
  79. (semanticdb-enable-gnu-global-in-buffer))))
  80. (let* ((db (semanticdb-project-database-global "global"))
  81. (tab (semanticdb-file-table db (buffer-file-name)))
  82. (result (semanticdb-deep-find-tags-for-completion-method tab searchfor))
  83. )
  84. (data-debug-new-buffer "*SemanticDB Gnu Global Result*")
  85. (data-debug-insert-thing result "?" ""))))
  86. ;;; From semantic-format
  87. (require 'semantic/format)
  88. (defun semantic-test-all-format-tag-functions (&optional arg)
  89. "Test all outputs from `semantic-format-tag-functions'.
  90. Output is generated from the function under `point'.
  91. Optional argument ARG specifies not to use color."
  92. (interactive "P")
  93. (semantic-fetch-tags)
  94. (let* ((tag (semantic-current-tag))
  95. (par (semantic-current-tag-parent))
  96. (fns semantic-format-tag-functions))
  97. (with-output-to-temp-buffer "*format-tag*"
  98. (princ "Tag->format function tests:")
  99. (while fns
  100. (princ "\n")
  101. (princ (car fns))
  102. (princ ":\n ")
  103. (let ((s (funcall (car fns) tag par (not arg))))
  104. (save-excursion
  105. (set-buffer "*format-tag*")
  106. (goto-char (point-max))
  107. (insert s)))
  108. (setq fns (cdr fns))))
  109. ))
  110. ;;; From semantic-fw:
  111. (require 'semantic/fw)
  112. (defun semantic-test-data-cache ()
  113. "Test the data cache."
  114. (interactive)
  115. (let ((data '(a b c)))
  116. (save-excursion
  117. (set-buffer (get-buffer-create " *semantic-test-data-cache*"))
  118. (erase-buffer)
  119. (insert "The Moose is Loose")
  120. (goto-char (point-min))
  121. (semantic-cache-data-to-buffer (current-buffer) (point) (+ (point) 5)
  122. data 'moose 'exit-cache-zone)
  123. (if (equal (semantic-get-cache-data 'moose) data)
  124. (message "Successfully retrieved cached data.")
  125. (error "Failed to retrieve cached data")))))
  126. (defun semantic-test-throw-on-input ()
  127. "Test that throw on input will work."
  128. (interactive)
  129. (semantic-throw-on-input 'done-die)
  130. (message "Exit Code: %s"
  131. (semantic-exit-on-input 'testing
  132. (let ((inhibit-quit nil)
  133. (message-log-max nil))
  134. (while t
  135. (message "Looping ... press a key to test")
  136. (semantic-throw-on-input 'test-inner-loop))
  137. 'exit)))
  138. (when (input-pending-p)
  139. (if (fboundp 'read-event)
  140. (read-event)
  141. (read-char))))
  142. ;;; From semantic-idle:
  143. (require 'semantic/idle)
  144. (defun semantic-idle-pnf-test ()
  145. "Test `semantic-idle-scheduler-work-parse-neighboring-files' and time it."
  146. (interactive)
  147. (let ((start (current-time))
  148. (junk (semantic-idle-scheduler-work-parse-neighboring-files))
  149. (end (current-time)))
  150. (message "Work took %.2f seconds." (semantic-elapsed-time start end))))
  151. ;;; From semantic-lex:
  152. (require 'semantic/lex)
  153. (defun semantic-lex-test-full-depth (arg)
  154. "Test the semantic lexer in the current buffer parsing through lists.
  155. Usually the lexer parses.
  156. If universal argument ARG, then try the whole buffer."
  157. (interactive "P")
  158. (let* ((start (current-time))
  159. (result (semantic-lex
  160. (if arg (point-min) (point))
  161. (point-max)
  162. 100))
  163. (end (current-time)))
  164. (message "Elapsed Time: %.2f seconds."
  165. (semantic-elapsed-time start end))
  166. (pop-to-buffer "*Lexer Output*")
  167. (require 'pp)
  168. (erase-buffer)
  169. (insert (pp-to-string result))
  170. (goto-char (point-min))))
  171. (defun semantic-lex-test-region (beg end)
  172. "Test the semantic lexer in the current buffer.
  173. Analyze the area between BEG and END."
  174. (interactive "r")
  175. (let ((result (semantic-lex beg end)))
  176. (pop-to-buffer "*Lexer Output*")
  177. (require 'pp)
  178. (erase-buffer)
  179. (insert (pp-to-string result))
  180. (goto-char (point-min))))
  181. ;;; From semantic-lex-spp:
  182. (require 'semantic/lex-spp)
  183. (defun semantic-lex-spp-write-test ()
  184. "Test the semantic tag writer against the current buffer."
  185. (interactive)
  186. (with-output-to-temp-buffer "*SPP Write Test*"
  187. (semantic-lex-spp-table-write-slot-value
  188. (semantic-lex-spp-save-table))))
  189. (defun semantic-lex-spp-write-utest ()
  190. "Unit test using the test spp file to test the slot write fcn."
  191. (interactive)
  192. (let* ((sem (locate-library "semantic-lex-spp.el"))
  193. (dir (file-name-directory sem)))
  194. (save-excursion
  195. (set-buffer (find-file-noselect
  196. (expand-file-name "tests/testsppreplace.c"
  197. dir)))
  198. (semantic-lex-spp-write-test))))
  199. ;;; From semantic-tag-write:
  200. ;;; TESTING.
  201. (require 'semantic/tag-write)
  202. (defun semantic-tag-write-test ()
  203. "Test the semantic tag writer against the tag under point."
  204. (interactive)
  205. (with-output-to-temp-buffer "*Tag Write Test*"
  206. (semantic-tag-write-one-tag (semantic-current-tag))))
  207. (defun semantic-tag-write-list-test ()
  208. "Test the semantic tag writer against the tag under point."
  209. (interactive)
  210. (with-output-to-temp-buffer "*Tag Write Test*"
  211. (semantic-tag-write-tag-list (semantic-fetch-tags))))
  212. ;;; From semantic-symref-filter:
  213. (require 'semantic/symref/filter)
  214. (defun semantic-symref-test-count-hits-in-tag ()
  215. "Lookup in the current tag the symbol under point.
  216. Then count all the other references to the same symbol within the
  217. tag that contains point, and return that."
  218. (interactive)
  219. (let* ((ctxt (semantic-analyze-current-context))
  220. (target (car (reverse (oref ctxt prefix))))
  221. (tag (semantic-current-tag))
  222. (start (current-time))
  223. (Lcount 0))
  224. (when (semantic-tag-p target)
  225. (semantic-symref-hits-in-region
  226. target (lambda (start end prefix) (setq Lcount (1+ Lcount)))
  227. (semantic-tag-start tag)
  228. (semantic-tag-end tag))
  229. (when (interactive-p)
  230. (message "Found %d occurrences of %s in %.2f seconds"
  231. Lcount (semantic-tag-name target)
  232. (semantic-elapsed-time start (current-time))))
  233. Lcount)))
  234. ;;; From bovine-gcc:
  235. (require 'semantic/bovine/gcc)
  236. ;; Example output of "gcc -v"
  237. (defvar semantic-gcc-test-strings
  238. '(;; My old box:
  239. "Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
  240. Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
  241. Thread model: posix
  242. gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
  243. ;; Alex Ott:
  244. "Using built-in specs.
  245. Target: i486-linux-gnu
  246. Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.1-9ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
  247. Thread model: posix
  248. gcc version 4.3.1 (Ubuntu 4.3.1-9ubuntu1)"
  249. ;; My debian box:
  250. "Using built-in specs.
  251. Target: x86_64-unknown-linux-gnu
  252. Configured with: ../../../sources/gcc/configure --prefix=/usr/local/glibc-2.3.6/x86_64/apps/gcc-4.2.3 --with-gmp=/usr/local/gcc/gmp --with-mpfr=/usr/local/gcc/mpfr --enable-languages=c,c++,fortran --with-as=/usr/local/glibc-2.3.6/x86_64/apps/gcc-4.2.3/bin/as --with-ld=/usr/local/glibc-2.3.6/x86_64/apps/gcc-4.2.3/bin/ld --disable-multilib
  253. Thread model: posix
  254. gcc version 4.2.3"
  255. ;; My mac:
  256. "Using built-in specs.
  257. Target: i686-apple-darwin8
  258. Configured with: /private/var/tmp/gcc/gcc-5341.obj~1/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=powerpc-apple-darwin8 --with-arch=pentium-m --with-tune=prescott --program-prefix= --host=i686-apple-darwin8 --target=i686-apple-darwin8
  259. Thread model: posix
  260. gcc version 4.0.1 (Apple Computer, Inc. build 5341)"
  261. ;; Ubuntu Intrepid
  262. "Using built-in specs.
  263. Target: x86_64-linux-gnu
  264. Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.2-1ubuntu12' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
  265. Thread model: posix
  266. gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu12)"
  267. ;; Red Hat EL4
  268. "Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
  269. Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
  270. Thread model: posix
  271. gcc version 3.4.6 20060404 (Red Hat 3.4.6-10)"
  272. ;; Red Hat EL5
  273. "Using built-in specs.
  274. Target: x86_64-redhat-linux
  275. Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
  276. Thread model: posix
  277. gcc version 4.1.2 20080704 (Red Hat 4.1.2-44)"
  278. ;; David Engster's german gcc on ubuntu 4.3
  279. "Es werden eingebaute Spezifikationen verwendet.
  280. Ziel: i486-linux-gnu
  281. Konfiguriert mit: ../src/configure -v --with-pkgversion='Ubuntu 4.3.2-1ubuntu12' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
  282. Thread-Modell: posix
  283. gcc-Version 4.3.2 (Ubuntu 4.3.2-1ubuntu12)"
  284. ;; Damien Deville bsd
  285. "Using built-in specs.
  286. Target: i386-undermydesk-freebsd
  287. Configured with: FreeBSD/i386 system compiler
  288. Thread model: posix
  289. gcc version 4.2.1 20070719 [FreeBSD]"
  290. )
  291. "A bunch of sample gcc -v outputs from different machines.")
  292. (defvar semantic-gcc-test-strings-fail
  293. '(;; A really old solaris box I found
  294. "Reading specs from /usr/local/gcc-2.95.2/lib/gcc-lib/sparc-sun-solaris2.6/2.95.2/specs
  295. gcc version 2.95.2 19991024 (release)"
  296. )
  297. "A bunch of sample gcc -v outputs that fail to provide the info we want.")
  298. (defun semantic-gcc-test-output-parser ()
  299. "Test the output parser against some collected strings."
  300. (interactive)
  301. (let ((fail nil))
  302. (dolist (S semantic-gcc-test-strings)
  303. (let* ((fields (semantic-gcc-fields S))
  304. (v (cdr (assoc 'version fields)))
  305. (h (or (cdr (assoc 'target fields))
  306. (cdr (assoc '--target fields))
  307. (cdr (assoc '--host fields))))
  308. (p (cdr (assoc '--prefix fields)))
  309. )
  310. ;; No longer test for prefixes.
  311. (when (not (and v h))
  312. (let ((strs (split-string S "\n")))
  313. (message "Test failed on %S\nV H P:\n%S %S %S" (car strs) v h p))
  314. (setq fail t))
  315. ))
  316. (dolist (S semantic-gcc-test-strings-fail)
  317. (let* ((fields (semantic-gcc-fields S))
  318. (v (cdr (assoc 'version fields)))
  319. (h (or (cdr (assoc '--host fields))
  320. (cdr (assoc 'target fields))))
  321. (p (cdr (assoc '--prefix fields)))
  322. )
  323. (when (and v h p)
  324. (message "Negative test failed on %S" S)
  325. (setq fail t))
  326. ))
  327. (if (not fail) (message "Tests passed."))
  328. ))
  329. (defun semantic-gcc-test-output-parser-this-machine ()
  330. "Test the output parser against the machine currently running Emacs."
  331. (interactive)
  332. (let ((semantic-gcc-test-strings (list (semantic-gcc-query "gcc" "-v"))))
  333. (semantic-gcc-test-output-parser))
  334. )