semantic-ia-utest.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. ;;; semantic-ia-utest.el --- Analyzer unit tests
  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. ;; Use marked-up files in the test directory and run the analyzer
  18. ;; on them. Make sure the answers are correct.
  19. ;;
  20. ;; Each file has cursor keys in them of the form:
  21. ;; // -#- ("ans1" "ans2" )
  22. ;; where # is 1, 2, 3, etc, and some sort of answer list.
  23. ;;; Code:
  24. (require 'semantic)
  25. (require 'semantic/analyze)
  26. (require 'semantic/analyze/refs)
  27. (require 'semantic/symref)
  28. (require 'semantic/symref/filter)
  29. (load-file "cedet-utests.el")
  30. (defvar semantic-ia-utest-file-list
  31. '(
  32. "tests/testdoublens.cpp"
  33. "tests/testsubclass.cpp"
  34. "tests/testtypedefs.cpp"
  35. "tests/testfriends.cpp"
  36. "tests/testnsp.cpp"
  37. "tests/testsppcomplete.c"
  38. "tests/testvarnames.c"
  39. "tests/testjavacomp.java"
  40. )
  41. "List of files with analyzer completion test points.")
  42. (defvar semantic-ia-utest-error-log-list nil
  43. "List of errors occurring during a run.")
  44. ;;;###autoload
  45. (defun semantic-ia-utest (&optional arg)
  46. "Run the semantic ia unit test against stored sources.
  47. Argument ARG specifies which set of tests to run.
  48. 1 - ia utests
  49. 2 - regs utests
  50. 3 - symrefs utests
  51. 4 - symref count utests"
  52. (interactive "P")
  53. (save-excursion
  54. (let ((fl semantic-ia-utest-file-list)
  55. (semantic-ia-utest-error-log-list nil)
  56. )
  57. (cedet-utest-log-setup "ANALYZER")
  58. (set-buffer (semantic-find-file-noselect
  59. (or (locate-library "semantic-ia-utest.el")
  60. "semantic-ia-utest.el")))
  61. (while fl
  62. ;; Make sure we have the files we think we have.
  63. (when (not (file-exists-p (car fl)))
  64. (error "Cannot find unit test file: %s" (car fl)))
  65. ;; Run the tests.
  66. (let ((fb (find-buffer-visiting (car fl)))
  67. (b (semantic-find-file-noselect (car fl) t)))
  68. ;; Run the test on it.
  69. (save-excursion
  70. (set-buffer b)
  71. ;; This line will also force the include, scope, and typecache.
  72. (semantic-clear-toplevel-cache)
  73. ;; Force tags to be parsed.
  74. (semantic-fetch-tags)
  75. (semantic-ia-utest-log " ** Starting tests in %s"
  76. (buffer-name))
  77. (when (or (not arg) (= arg 1))
  78. (semantic-ia-utest-buffer))
  79. (when (or (not arg) (= arg 2))
  80. (set-buffer b)
  81. (semantic-ia-utest-buffer-refs))
  82. (when (or (not arg) (= arg 3))
  83. (set-buffer b)
  84. (semantic-sr-utest-buffer-refs))
  85. (when (or (not arg) (= arg 4))
  86. (set-buffer b)
  87. (semantic-src-utest-buffer-refs))
  88. (semantic-ia-utest-log " ** Completed tests in %s\n"
  89. (buffer-name))
  90. )
  91. ;; If it wasn't already in memory, whack it.
  92. (when (not fb)
  93. (kill-buffer b))
  94. )
  95. (setq fl (cdr fl)))
  96. (cedet-utest-log-shutdown
  97. "ANALYZER"
  98. (when semantic-ia-utest-error-log-list
  99. (format "%s Failures found."
  100. (length semantic-ia-utest-error-log-list))))
  101. (when semantic-ia-utest-error-log-list
  102. (error "Failures found during analyzer unit tests"))
  103. ))
  104. )
  105. (defun semantic-ia-utest-buffer ()
  106. "Run analyzer completion unit-test pass in the current buffer."
  107. (let* ((idx 1)
  108. (regex-p nil)
  109. (regex-a nil)
  110. (p nil)
  111. (a nil)
  112. (pass nil)
  113. (fail nil)
  114. (actual nil)
  115. (desired nil)
  116. ;; Exclude unpredictable system files in the
  117. ;; header include list.
  118. (semanticdb-find-default-throttle
  119. (remq 'system semanticdb-find-default-throttle))
  120. )
  121. ;; Keep looking for test points until we run out.
  122. (while (save-excursion
  123. (setq regex-p (concat "//\\s-*-" (number-to-string idx) "-" )
  124. regex-a (concat "//\\s-*#" (number-to-string idx) "#" ))
  125. (goto-char (point-min))
  126. (save-match-data
  127. (when (re-search-forward regex-p nil t)
  128. (setq p (match-beginning 0))))
  129. (save-match-data
  130. (when (re-search-forward regex-a nil t)
  131. (setq a (match-end 0))))
  132. (and p a))
  133. (save-excursion
  134. (goto-char p)
  135. (let* ((ctxt (semantic-analyze-current-context))
  136. (acomp
  137. (condition-case nil
  138. (semantic-analyze-possible-completions ctxt)
  139. (error nil))))
  140. (setq actual (mapcar 'semantic-tag-name acomp)))
  141. (goto-char a)
  142. (let ((bss (buffer-substring-no-properties (point) (point-at-eol))))
  143. (condition-case nil
  144. (setq desired (read bss))
  145. (error (setq desired (format " FAILED TO PARSE: %S"
  146. bss)))))
  147. (if (equal actual desired)
  148. (setq pass (cons idx pass))
  149. (setq fail (cons idx fail))
  150. (semantic-ia-utest-log
  151. " Failed %d. Desired: %S Actual %S"
  152. idx desired actual)
  153. (add-to-list 'semantic-ia-utest-error-log-list
  154. (list (buffer-name) idx desired actual)
  155. )
  156. )
  157. )
  158. (setq p nil a nil)
  159. (setq idx (1+ idx)))
  160. (if fail
  161. (progn
  162. (semantic-ia-utest-log
  163. " Unit tests (completions) failed tests %S"
  164. (reverse fail))
  165. )
  166. (semantic-ia-utest-log " Unit tests (completions) passed (%d total)"
  167. (- idx 1)))
  168. ))
  169. (defun semantic-ia-utest-buffer-refs ()
  170. "Run an analyze-refs unit-test pass in the current buffer."
  171. (let* ((idx 1)
  172. (regex-p nil)
  173. (p nil)
  174. (pass nil)
  175. (fail nil)
  176. ;; Exclude unpredictable system files in the
  177. ;; header include list.
  178. (semanticdb-find-default-throttle
  179. (remq 'system semanticdb-find-default-throttle))
  180. )
  181. ;; Keep looking for test points until we run out.
  182. (while (save-excursion
  183. (setq regex-p (concat "//\\s-*\\^" (number-to-string idx) "^" )
  184. )
  185. (goto-char (point-min))
  186. (save-match-data
  187. (when (re-search-forward regex-p nil t)
  188. (setq p (match-beginning 0))))
  189. p)
  190. (save-excursion
  191. (goto-char p)
  192. (forward-char -1)
  193. (let* ((ct (semantic-current-tag))
  194. (refs (semantic-analyze-tag-references ct))
  195. (impl (semantic-analyze-refs-impl refs t))
  196. (proto (semantic-analyze-refs-proto refs t))
  197. (pf nil)
  198. )
  199. (setq
  200. pf
  201. (catch 'failed
  202. (if (and impl proto (car impl) (car proto))
  203. (let (ct2 ref2 impl2 proto2
  204. newstart)
  205. (cond
  206. ((semantic-equivalent-tag-p (car impl) ct)
  207. ;; We are on an IMPL. Go To the proto, and find matches.
  208. (semantic-go-to-tag (car proto))
  209. (setq newstart (car proto))
  210. )
  211. ((semantic-equivalent-tag-p (car proto) ct)
  212. ;; We are on a PROTO. Go to the imple, and find matches
  213. (semantic-go-to-tag (car impl))
  214. (setq newstart (car impl))
  215. )
  216. (t
  217. ;; No matches is a fail.
  218. (throw 'failed t)
  219. ))
  220. ;; Get the new tag, does it match?
  221. (setq ct2 (semantic-current-tag))
  222. ;; Does it match?
  223. (when (not (semantic-equivalent-tag-p ct2 newstart))
  224. (throw 'failed t))
  225. ;; Can we double-jump?
  226. (setq ref2 (semantic-analyze-tag-references ct)
  227. impl2 (semantic-analyze-refs-impl ref2 t)
  228. proto2 (semantic-analyze-refs-proto ref2 t))
  229. (when (or (not (and impl2 proto2))
  230. (not
  231. (and (semantic-equivalent-tag-p
  232. (car impl) (car impl2))
  233. (semantic-equivalent-tag-p
  234. (car proto) (car proto2)))))
  235. (throw 'failed t))
  236. )
  237. ;; Else, no matches at all, so another fail.
  238. (throw 'failed t)
  239. )))
  240. (if (not pf)
  241. ;; We passed
  242. (setq pass (cons idx pass))
  243. ;; We failed.
  244. (setq fail (cons idx fail))
  245. (semantic-ia-utest-log
  246. " Failed %d. For %s (Num impls %d) (Num protos %d)"
  247. idx (if ct (semantic-tag-name ct) "<No tag found>")
  248. (length impl) (length proto))
  249. (add-to-list 'semantic-ia-utest-error-log-list
  250. (list (buffer-name) idx)
  251. )
  252. ))
  253. (setq p nil)
  254. (setq idx (1+ idx))
  255. ))
  256. (if fail
  257. (progn
  258. (semantic-ia-utest-log
  259. " Unit tests (refs) failed tests")
  260. )
  261. (semantic-ia-utest-log " Unit tests (refs) passed (%d total)"
  262. (- idx 1)))
  263. ))
  264. (defun semantic-sr-utest-buffer-refs ()
  265. "Run a symref unit-test pass in the current buffer."
  266. ;; This line will also force the include, scope, and typecache.
  267. (semantic-clear-toplevel-cache)
  268. ;; Force tags to be parsed.
  269. (semantic-fetch-tags)
  270. (let* ((idx 1)
  271. (tag nil)
  272. (regex-p nil)
  273. (desired nil)
  274. (actual-result nil)
  275. (actual nil)
  276. (pass nil)
  277. (fail nil)
  278. (symref-tool-used nil)
  279. ;; Exclude unpredictable system files in the
  280. ;; header include list.
  281. (semanticdb-find-default-throttle
  282. (remq 'system semanticdb-find-default-throttle))
  283. )
  284. ;; Keep looking for test points until we run out.
  285. (while (save-excursion
  286. (setq regex-p (concat "//\\s-*\\%" (number-to-string idx) "%" )
  287. )
  288. (goto-char (point-min))
  289. (save-match-data
  290. (when (re-search-forward regex-p nil t)
  291. (setq tag (semantic-current-tag))
  292. (goto-char (match-end 0))
  293. (setq desired (read (buffer-substring (point) (point-at-eol))))
  294. ))
  295. tag)
  296. (setq actual-result (semantic-symref-find-references-by-name
  297. (semantic-tag-name tag) 'target
  298. 'symref-tool-used))
  299. (if (not actual-result)
  300. (progn
  301. (setq fail (cons idx fail))
  302. (semantic-ia-utest-log
  303. " Failed FNames %d: No results." idx)
  304. (semantic-ia-utest-log
  305. " Failed Tool: %s" (object-name symref-tool-used))
  306. (add-to-list 'semantic-ia-utest-error-log-list
  307. (list (buffer-name) idx)
  308. )
  309. )
  310. (setq actual (list (sort (mapcar
  311. 'file-name-nondirectory
  312. (semantic-symref-result-get-files actual-result))
  313. 'string<)
  314. (sort
  315. (mapcar
  316. 'semantic-format-tag-canonical-name
  317. (semantic-symref-result-get-tags actual-result))
  318. 'string<)))
  319. (if (equal desired actual)
  320. ;; We passed
  321. (setq pass (cons idx pass))
  322. ;; We failed.
  323. (setq fail (cons idx fail))
  324. (when (not (equal (car actual) (car desired)))
  325. (semantic-ia-utest-log
  326. " Failed FNames %d: Actual: %S Desired: %S"
  327. idx (car actual) (car desired))
  328. (semantic-ia-utest-log
  329. " Failed Tool: %s" (object-name symref-tool-used))
  330. )
  331. (when (not (equal (car (cdr actual)) (car (cdr desired))))
  332. (semantic-ia-utest-log
  333. " Failed TNames %d: Actual: %S Desired: %S"
  334. idx (car (cdr actual)) (car (cdr desired)))
  335. (semantic-ia-utest-log
  336. " Failed Tool: %s" (object-name symref-tool-used))
  337. )
  338. (add-to-list 'semantic-ia-utest-error-log-list
  339. (list (buffer-name) idx)
  340. )
  341. ))
  342. (setq idx (1+ idx))
  343. (setq tag nil))
  344. (if fail
  345. (progn
  346. (semantic-ia-utest-log
  347. " Unit tests (symrefs) failed tests")
  348. )
  349. (semantic-ia-utest-log " Unit tests (symrefs) passed (%d total)"
  350. (- idx 1)))
  351. ))
  352. (defun semantic-symref-test-count-hits-in-tag ()
  353. "Lookup in the current tag the symbol under point.
  354. Then count all the other references to the same symbol within the
  355. tag that contains point, and return that."
  356. (interactive)
  357. (let* ((ctxt (semantic-analyze-current-context))
  358. (target (car (reverse (oref ctxt prefix))))
  359. (tag (semantic-current-tag))
  360. (start (current-time))
  361. (Lcount 0))
  362. (when (semantic-tag-p target)
  363. (semantic-symref-hits-in-region
  364. target (lambda (start end prefix) (setq Lcount (1+ Lcount)))
  365. (semantic-tag-start tag)
  366. (semantic-tag-end tag))
  367. (when (interactive-p)
  368. (message "Found %d occurrences of %s in %.2f seconds"
  369. Lcount (semantic-tag-name target)
  370. (semantic-elapsed-time start (current-time))))
  371. Lcount)))
  372. (defun semantic-src-utest-buffer-refs ()
  373. "Run a sym-ref counting unit-test pass in the current buffer."
  374. ;; This line will also force the include, scope, and typecache.
  375. (semantic-clear-toplevel-cache)
  376. ;; Force tags to be parsed.
  377. (semantic-fetch-tags)
  378. (let* ((idx 1)
  379. (start nil)
  380. (regex-p nil)
  381. (desired nil)
  382. (actual nil)
  383. (pass nil)
  384. (fail nil)
  385. ;; Exclude unpredictable system files in the
  386. ;; header include list.
  387. (semanticdb-find-default-throttle
  388. (remq 'system semanticdb-find-default-throttle))
  389. )
  390. ;; Keep looking for test points until we run out.
  391. (while (save-excursion
  392. (setq regex-p (concat "//\\s-*@"
  393. (number-to-string idx)
  394. "@\\s-+\\(\\w+\\)" ))
  395. (goto-char (point-min))
  396. (save-match-data
  397. (when (re-search-forward regex-p nil t)
  398. (goto-char (match-beginning 1))
  399. (setq desired (read (buffer-substring (point) (point-at-eol))))
  400. (setq start (match-beginning 0))
  401. (goto-char start)
  402. (setq actual (semantic-symref-test-count-hits-in-tag))
  403. start)))
  404. (if (not actual)
  405. (progn
  406. (setq fail (cons idx fail))
  407. (semantic-ia-utest-log
  408. " Failed symref count %d: No results." idx)
  409. (add-to-list 'semantic-ia-utest-error-log-list
  410. (list (buffer-name) idx)
  411. )
  412. )
  413. (if (equal desired actual)
  414. ;; We passed
  415. (setq pass (cons idx pass))
  416. ;; We failed.
  417. (setq fail (cons idx fail))
  418. (when (not (equal actual desired))
  419. (semantic-ia-utest-log
  420. " Failed symref count %d: Actual: %S Desired: %S"
  421. idx actual desired)
  422. )
  423. (add-to-list 'semantic-ia-utest-error-log-list
  424. (list (buffer-name) idx)
  425. )
  426. ))
  427. (setq idx (1+ idx))
  428. )
  429. (if fail
  430. (progn
  431. (semantic-ia-utest-log
  432. " Unit tests (symrefs counter) failed tests")
  433. )
  434. (semantic-ia-utest-log " Unit tests (symrefs counter) passed (%d total)"
  435. (- idx 1)))
  436. ))
  437. (defun semantic-ia-utest-start-log ()
  438. "Start up a testlog for a run."
  439. ;; Redo w/ CEDET utest framework.
  440. (cedet-utest-log-start "semantic: analyzer tests"))
  441. (defun semantic-ia-utest-log (&rest args)
  442. "Log some test results.
  443. Pass ARGS to format to create the log message."
  444. ;; Forward to CEDET utest framework.
  445. (apply 'cedet-utest-log args))
  446. (provide 'semantic-ia-utest)
  447. ;;; semantic-ia-utest.el ends here