srecode-tests.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. ;;; srecode-tests.el --- Some tests for CEDET's srecode
  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. ;; Extracted from srecode-fields.el and srecode-document.el in the
  17. ;; CEDET distribution.
  18. ;;; Code:
  19. ;;; From srecode-fields:
  20. (require 'srecode/fields)
  21. (defvar srecode-field-utest-text
  22. "This is a test buffer.
  23. It is filled with some text."
  24. "Text for tests.")
  25. (defun srecode-field-utest ()
  26. "Test the srecode field manager."
  27. (interactive)
  28. (if (featurep 'xemacs)
  29. (message "There is no XEmacs support for SRecode Fields.")
  30. (srecode-field-utest-impl)))
  31. (defun srecode-field-utest-impl ()
  32. "Implementation of the SRecode field utest."
  33. (save-excursion
  34. (find-file "/tmp/srecode-field-test.txt")
  35. (erase-buffer)
  36. (goto-char (point-min))
  37. (insert srecode-field-utest-text)
  38. (set-buffer-modified-p nil)
  39. ;; Test basic field generation.
  40. (let ((srecode-field-archive nil)
  41. (f nil))
  42. (end-of-line)
  43. (forward-word -1)
  44. (setq f (srecode-field "Test"
  45. :name "TEST"
  46. :start 6
  47. :end 8))
  48. (when (or (not (slot-boundp f 'overlay)) (not (oref f overlay)))
  49. (error "Field test: Overlay info not created for field"))
  50. (when (and (overlay-p (oref f overlay))
  51. (not (overlay-get (oref f overlay) 'srecode-init-only)))
  52. (error "Field creation overlay is not tagged w/ init flag"))
  53. (srecode-overlaid-activate f)
  54. (when (or (not (overlay-p (oref f overlay)))
  55. (overlay-get (oref f overlay) 'srecode-init-only))
  56. (error "New field overlay not created during activation"))
  57. (when (not (= (length srecode-field-archive) 1))
  58. (error "Field test: Incorrect number of elements in the field archive"))
  59. (when (not (eq f (car srecode-field-archive)))
  60. (error "Field test: Field did not auto-add itself to the field archive"))
  61. (when (not (overlay-get (oref f overlay) 'keymap))
  62. (error "Field test: Overlay keymap not set"))
  63. (when (not (string= "is" (srecode-overlaid-text f)))
  64. (error "Field test: Expected field text 'is', not %s"
  65. (srecode-overlaid-text f)))
  66. ;; Test deletion.
  67. (srecode-delete f)
  68. (when (slot-boundp f 'overlay)
  69. (error "Field test: Overlay not deleted after object delete"))
  70. )
  71. ;; Test basic region construction.
  72. (let* ((srecode-field-archive nil)
  73. (reg nil)
  74. (fields
  75. (list
  76. (srecode-field "Test1" :name "TEST-1" :start 5 :end 10)
  77. (srecode-field "Test2" :name "TEST-2" :start 15 :end 20)
  78. (srecode-field "Test3" :name "TEST-3" :start 25 :end 30)
  79. (srecode-field "Test4" :name "TEST-4" :start 35 :end 35))
  80. ))
  81. (when (not (= (length srecode-field-archive) 4))
  82. (error "Region Test: Found %d fields. Expected 4"
  83. (length srecode-field-archive)))
  84. (setq reg (srecode-template-inserted-region "REG"
  85. :start 4
  86. :end 40))
  87. (srecode-overlaid-activate reg)
  88. ;; Make sure it was cleared.
  89. (when srecode-field-archive
  90. (error "Region Test: Did not clear field archive"))
  91. ;; Auto-positioning.
  92. (when (not (eq (point) 5))
  93. (error "Region Test: Did not reposition on first field"))
  94. ;; Active region
  95. (when (not (eq (srecode-active-template-region) reg))
  96. (error "Region Test: Active region not set"))
  97. ;; Various sizes
  98. (mapc (lambda (T)
  99. (if (string= (object-name-string T) "Test4")
  100. (progn
  101. (when (not (srecode-empty-region-p T))
  102. (error "Field %s is not empty"
  103. (object-name T)))
  104. )
  105. (when (not (= (srecode-region-size T) 5))
  106. (error "Calculated size of %s was not 5"
  107. (object-name T)))))
  108. fields)
  109. ;; Make sure things stay up after a 'command'.
  110. (srecode-field-post-command)
  111. (when (not (eq (srecode-active-template-region) reg))
  112. (error "Region Test: Active region did not stay up"))
  113. ;; Test field movement.
  114. (when (not (eq (srecode-overlaid-at-point 'srecode-field)
  115. (nth 0 fields)))
  116. (error "Region Test: Field %s not under point"
  117. (object-name (nth 0 fields))))
  118. (srecode-field-next)
  119. (when (not (eq (srecode-overlaid-at-point 'srecode-field)
  120. (nth 1 fields)))
  121. (error "Region Test: Field %s not under point"
  122. (object-name (nth 1 fields))))
  123. (srecode-field-prev)
  124. (when (not (eq (srecode-overlaid-at-point 'srecode-field)
  125. (nth 0 fields)))
  126. (error "Region Test: Field %s not under point"
  127. (object-name (nth 0 fields))))
  128. ;; Move cursor out of the region and have everything cleaned up.
  129. (goto-char 42)
  130. (srecode-field-post-command)
  131. (when (srecode-active-template-region)
  132. (error "Region Test: Active region did not clear on move out"))
  133. (mapc (lambda (T)
  134. (when (slot-boundp T 'overlay)
  135. (error "Overlay did not clear off of field %s"
  136. (object-name T))))
  137. fields)
  138. ;; End of LET
  139. )
  140. ;; Test variable linkage.
  141. (let* ((srecode-field-archive nil)
  142. (f1 (srecode-field "Test1" :name "TEST" :start 6 :end 8))
  143. (f2 (srecode-field "Test2" :name "TEST" :start 28 :end 30))
  144. (f3 (srecode-field "Test3" :name "NOTTEST" :start 35 :end 40))
  145. (reg (srecode-template-inserted-region "REG" :start 4 :end 40))
  146. )
  147. (srecode-overlaid-activate reg)
  148. (when (not (string= (srecode-overlaid-text f1)
  149. (srecode-overlaid-text f2)))
  150. (error "Linkage Test: Init strings are not ="))
  151. (when (string= (srecode-overlaid-text f1)
  152. (srecode-overlaid-text f3))
  153. (error "Linkage Test: Init string on dissimilar fields is now the same"))
  154. (goto-char 7)
  155. (insert "a")
  156. (when (not (string= (srecode-overlaid-text f1)
  157. (srecode-overlaid-text f2)))
  158. (error "Linkage Test: mid-insert strings are not ="))
  159. (when (string= (srecode-overlaid-text f1)
  160. (srecode-overlaid-text f3))
  161. (error "Linkage Test: mid-insert string on dissimilar fields is now the same"))
  162. (goto-char 9)
  163. (insert "t")
  164. (when (not (string= (srecode-overlaid-text f1) "iast"))
  165. (error "Linkage Test: tail-insert failed to captured added char"))
  166. (when (not (string= (srecode-overlaid-text f1)
  167. (srecode-overlaid-text f2)))
  168. (error "Linkage Test: tail-insert strings are not ="))
  169. (when (string= (srecode-overlaid-text f1)
  170. (srecode-overlaid-text f3))
  171. (error "Linkage Test: tail-insert string on dissimilar fields is now the same"))
  172. (goto-char 6)
  173. (insert "b")
  174. (when (not (string= (srecode-overlaid-text f1) "biast"))
  175. (error "Linkage Test: tail-insert failed to captured added char"))
  176. (when (not (string= (srecode-overlaid-text f1)
  177. (srecode-overlaid-text f2)))
  178. (error "Linkage Test: tail-insert strings are not ="))
  179. (when (string= (srecode-overlaid-text f1)
  180. (srecode-overlaid-text f3))
  181. (error "Linkage Test: tail-insert string on dissimilar fields is now the same"))
  182. ;; Cleanup
  183. (srecode-delete reg)
  184. )
  185. (set-buffer-modified-p nil)
  186. (message " All field tests passed.")
  187. ))
  188. ;;; From srecode-document:
  189. (require 'srecode/doc)
  190. (defun srecode-document-function-comment-extract-test ()
  191. "Test old comment extraction.
  192. Dump out the extracted dictionary."
  193. (interactive)
  194. (srecode-load-tables-for-mode major-mode)
  195. (srecode-load-tables-for-mode major-mode 'document)
  196. (if (not (srecode-table))
  197. (error "No template table found for mode %s" major-mode))
  198. (let* ((temp (srecode-template-get-table (srecode-table)
  199. "function-comment"
  200. "declaration"
  201. 'document))
  202. (fcn-in (semantic-current-tag)))
  203. (if (not temp)
  204. (error "No templates for function comments"))
  205. ;; Try to figure out the tag we want to use.
  206. (when (or (not fcn-in)
  207. (not (semantic-tag-of-class-p fcn-in 'function)))
  208. (error "No tag of class 'function to insert comment for"))
  209. (let ((lextok (semantic-documentation-comment-preceeding-tag fcn-in 'lex))
  210. )
  211. (when (not lextok)
  212. (error "No comment to attempt an extraction"))
  213. (let ((s (semantic-lex-token-start lextok))
  214. (e (semantic-lex-token-end lextok))
  215. (extract nil))
  216. (pulse-momentary-highlight-region s e)
  217. ;; Extract text from the existing comment.
  218. (setq extract (srecode-extract temp s e))
  219. (with-output-to-temp-buffer "*SRECODE DUMP*"
  220. (princ "EXTRACTED DICTIONARY FOR ")
  221. (princ (semantic-tag-name fcn-in))
  222. (princ "\n--------------------------------------------\n")
  223. (srecode-dump extract))))))
  224. ;;; srecode-tests.el ends here