1 ;;; semantic-utest.el --- Miscellaneous Semantic tests.
3 ;;; Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
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 ;; Originally, there are many test functions scattered among the
25 ;; Semantic source files. This file consolidates them.
29 ;;; From semantic-complete
31 (require 'semantic
/complete
)
33 (defun semantic-complete-test ()
34 "Test completion mechanisms."
37 (semantic-format-tag-prototype
38 (semantic-complete-read-tag-project "Symbol: "))))
40 ;;; From semanticdb-ebrowse
42 (require 'semantic
/db-ebrowse
)
44 (defun semanticdb-ebrowse-run-tests ()
45 "Run some tests of the semanticdb-ebrowse system.
46 All systems are different. Ask questions along the way."
49 (when (y-or-n-p "Create a system database to test with? ")
50 (call-interactively 'semanticdb-create-ebrowse-database
)
52 ;; Should we load in caches
54 (y-or-n-p "New database created. Reload system databases? ")
55 (y-or-n-p "Load in all system databases? "))
56 (semanticdb-load-ebrowse-caches)))
57 ;; Ok, databases were creatd. Lets try some searching.
58 (when (not (or (eq major-mode
'c-mode
)
59 (eq major-mode
'c
++-mode
)))
60 (error "Please make your default buffer be a C or C++ file, then
61 run the test again")))
63 (defun semanticdb-ebrowse-dump ()
64 "Find the first loaded ebrowse table, and dump out the contents."
66 (let ((db semanticdb-database-list
)
69 (when (semanticdb-project-database-ebrowse-p (car db
))
70 (setq ab
(data-debug-new-buffer "*EBROWSE Database*"))
71 (data-debug-insert-thing (car db
) "*" "")
76 ;;; From semanticdb-global:
78 (require 'semantic
/db-global
)
80 (defvar semanticdb-test-gnu-global-startfile
"~/src/global-5.7.3/global/global.c"
81 "File to use for testing.")
83 (defun semanticdb-test-gnu-global (searchfor &optional standardfile
)
84 "Test the GNU Global semanticdb.
85 Argument SEARCHFOR is the text to search for.
86 If optional arg STANDARDFILE is non-nil, use a standard file w/ global enabled."
87 (interactive "sSearch For Tag: \nP")
93 (set-buffer (find-file-noselect semanticdb-test-gnu-global-startfile
))))
96 (semanticdb-enable-gnu-global-in-buffer)
97 (error (if standardfile
100 (set-buffer (find-file-noselect semanticdb-test-gnu-global-startfile
)))
101 (semanticdb-enable-gnu-global-in-buffer))))
103 (let* ((db (semanticdb-project-database-global "global"))
104 (tab (semanticdb-file-table db
(buffer-file-name)))
105 (result (semanticdb-deep-find-tags-for-completion-method tab searchfor
))
107 (data-debug-new-buffer "*SemanticDB Gnu Global Result*")
108 (data-debug-insert-thing result
"?" ""))))
110 ;;; From semantic-format
112 (require 'semantic
/format
)
114 (defun semantic-test-all-format-tag-functions (&optional arg
)
115 "Test all outputs from `semantic-format-tag-functions'.
116 Output is generated from the function under `point'.
117 Optional argument ARG specifies not to use color."
119 (semantic-fetch-tags)
120 (let* ((tag (semantic-current-tag))
121 (par (semantic-current-tag-parent))
122 (fns semantic-format-tag-functions
))
123 (with-output-to-temp-buffer "*format-tag*"
124 (princ "Tag->format function tests:")
129 (let ((s (funcall (car fns
) tag par
(not arg
))))
131 (set-buffer "*format-tag*")
132 (goto-char (point-max))
134 (setq fns
(cdr fns
))))
137 ;;; From semantic-fw:
139 (require 'semantic
/fw
)
141 (defun semantic-test-data-cache ()
142 "Test the data cache."
144 (let ((data '(a b c
)))
146 (set-buffer (get-buffer-create " *semantic-test-data-cache*"))
148 (insert "The Moose is Loose")
149 (goto-char (point-min))
150 (semantic-cache-data-to-buffer (current-buffer) (point) (+ (point) 5)
151 data
'moose
'exit-cache-zone
)
152 (if (equal (semantic-get-cache-data 'moose
) data
)
153 (message "Successfully retrieved cached data.")
154 (error "Failed to retrieve cached data")))))
156 (defun semantic-test-throw-on-input ()
157 "Test that throw on input will work."
159 (semantic-throw-on-input 'done-die
)
160 (message "Exit Code: %s"
161 (semantic-exit-on-input 'testing
162 (let ((inhibit-quit nil
)
163 (message-log-max nil
))
165 (message "Looping ... press a key to test")
166 (semantic-throw-on-input 'test-inner-loop
))
168 (when (input-pending-p)
169 (if (fboundp 'read-event
)
173 ;;; From semantic-idle:
175 (require 'semantic
/idle
)
177 (defun semantic-idle-pnf-test ()
178 "Test `semantic-idle-scheduler-work-parse-neighboring-files' and time it."
180 (let ((start (current-time))
181 (junk (semantic-idle-scheduler-work-parse-neighboring-files))
182 (end (current-time)))
183 (message "Work took %.2f seconds." (semantic-elapsed-time start end
))))
185 ;;; From semantic-lex:
187 (require 'semantic
/lex
)
189 (defun semantic-lex-test-full-depth (arg)
190 "Test the semantic lexer in the current buffer parsing through lists.
191 Usually the lexer parses.
192 If universal argument ARG, then try the whole buffer."
194 (let* ((start (current-time))
195 (result (semantic-lex
196 (if arg
(point-min) (point))
199 (end (current-time)))
200 (message "Elapsed Time: %.2f seconds."
201 (semantic-elapsed-time start end
))
202 (pop-to-buffer "*Lexer Output*")
205 (insert (pp-to-string result
))
206 (goto-char (point-min))))
208 (defun semantic-lex-test-region (beg end
)
209 "Test the semantic lexer in the current buffer.
210 Analyze the area between BEG and END."
212 (let ((result (semantic-lex beg end
)))
213 (pop-to-buffer "*Lexer Output*")
216 (insert (pp-to-string result
))
217 (goto-char (point-min))))
219 ;;; From semantic-lex-spp:
221 (require 'semantic
/lex-spp
)
223 (defun semantic-lex-spp-write-test ()
224 "Test the semantic tag writer against the current buffer."
226 (with-output-to-temp-buffer "*SPP Write Test*"
227 (semantic-lex-spp-table-write-slot-value
228 (semantic-lex-spp-save-table))))
230 (defun semantic-lex-spp-write-utest ()
231 "Unit test using the test spp file to test the slot write fcn."
233 (let* ((sem (locate-library "semantic-lex-spp.el"))
234 (dir (file-name-directory sem
)))
236 (set-buffer (find-file-noselect
237 (expand-file-name "tests/testsppreplace.c"
239 (semantic-lex-spp-write-test))))
241 ;;; From semantic-tag-write:
245 (require 'semantic
/tag-write
)
247 (defun semantic-tag-write-test ()
248 "Test the semantic tag writer against the tag under point."
250 (with-output-to-temp-buffer "*Tag Write Test*"
251 (semantic-tag-write-one-tag (semantic-current-tag))))
253 (defun semantic-tag-write-list-test ()
254 "Test the semantic tag writer against the tag under point."
256 (with-output-to-temp-buffer "*Tag Write Test*"
257 (semantic-tag-write-tag-list (semantic-fetch-tags))))
259 ;;; From semantic-symref-filter:
261 (require 'semantic
/symref
/filter
)
263 (defun semantic-symref-test-count-hits-in-tag ()
264 "Lookup in the current tag the symbol under point.
265 Then count all the other references to the same symbol within the
266 tag that contains point, and return that."
268 (let* ((ctxt (semantic-analyze-current-context))
269 (target (car (reverse (oref ctxt prefix
))))
270 (tag (semantic-current-tag))
271 (start (current-time))
273 (when (semantic-tag-p target
)
274 (semantic-symref-hits-in-region
275 target
(lambda (start end prefix
) (setq Lcount
(1+ Lcount
)))
276 (semantic-tag-start tag
)
277 (semantic-tag-end tag
))
278 (when (interactive-p)
279 (message "Found %d occurrences of %s in %.2f seconds"
280 Lcount
(semantic-tag-name target
)
281 (semantic-elapsed-time start
(current-time))))
286 (require 'semantic
/bovine
/gcc
)
288 ;; Example output of "gcc -v"
289 (defvar semantic-gcc-test-strings
291 "Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
292 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
294 gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
296 "Using built-in specs.
297 Target: i486-linux-gnu
298 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
300 gcc version 4.3.1 (Ubuntu 4.3.1-9ubuntu1)"
302 "Using built-in specs.
303 Target: x86_64-unknown-linux-gnu
304 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
308 "Using built-in specs.
309 Target: i686-apple-darwin8
310 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
312 gcc version 4.0.1 (Apple Computer, Inc. build 5341)"
314 "Using built-in specs.
315 Target: x86_64-linux-gnu
316 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
318 gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu12)"
320 "Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
321 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
323 gcc version 3.4.6 20060404 (Red Hat 3.4.6-10)"
325 "Using built-in specs.
326 Target: x86_64-redhat-linux
327 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
329 gcc version 4.1.2 20080704 (Red Hat 4.1.2-44)"
330 ;; David Engster's german gcc on ubuntu 4.3
331 "Es werden eingebaute Spezifikationen verwendet.
333 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
335 gcc-Version 4.3.2 (Ubuntu 4.3.2-1ubuntu12)"
336 ;; Damien Deville bsd
337 "Using built-in specs.
338 Target: i386-undermydesk-freebsd
339 Configured with: FreeBSD/i386 system compiler
341 gcc version 4.2.1 20070719 [FreeBSD]"
343 "A bunch of sample gcc -v outputs from different machines.")
345 (defvar semantic-gcc-test-strings-fail
346 '(;; A really old solaris box I found
347 "Reading specs from /usr/local/gcc-2.95.2/lib/gcc-lib/sparc-sun-solaris2.6/2.95.2/specs
348 gcc version 2.95.2 19991024 (release)"
350 "A bunch of sample gcc -v outputs that fail to provide the info we want.")
352 (defun semantic-gcc-test-output-parser ()
353 "Test the output parser against some collected strings."
356 (dolist (S semantic-gcc-test-strings
)
357 (let* ((fields (semantic-gcc-fields S
))
358 (v (cdr (assoc 'version fields
)))
359 (h (or (cdr (assoc 'target fields
))
360 (cdr (assoc '--target fields
))
361 (cdr (assoc '--host fields
))))
362 (p (cdr (assoc '--prefix fields
)))
364 ;; No longer test for prefixes.
365 (when (not (and v h
))
366 (let ((strs (split-string S
"\n")))
367 (message "Test failed on %S\nV H P:\n%S %S %S" (car strs
) v h p
))
370 (dolist (S semantic-gcc-test-strings-fail
)
371 (let* ((fields (semantic-gcc-fields S
))
372 (v (cdr (assoc 'version fields
)))
373 (h (or (cdr (assoc '--host fields
))
374 (cdr (assoc 'target fields
))))
375 (p (cdr (assoc '--prefix fields
)))
378 (message "Negative test failed on %S" S
)
381 (if (not fail
) (message "Tests passed."))
384 (defun semantic-gcc-test-output-parser-this-machine ()
385 "Test the output parser against the machine currently running Emacs."
387 (let ((semantic-gcc-test-strings (list (semantic-gcc-query "gcc" "-v"))))
388 (semantic-gcc-test-output-parser))
391 ;; arch-tag: 91686489-4ab3-4392-8277-b3831269ddbb