aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/lisp/cedet/srecode-utest-template.el379
1 files changed, 379 insertions, 0 deletions
diff --git a/test/lisp/cedet/srecode-utest-template.el b/test/lisp/cedet/srecode-utest-template.el
new file mode 100644
index 00000000000..d804db70b36
--- /dev/null
+++ b/test/lisp/cedet/srecode-utest-template.el
@@ -0,0 +1,379 @@
1;;; srecode/test.el --- SRecode Core Template tests.
2
3;; Copyright (C) 2008-2019 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software: you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
19
20;;; Commentary:
21;;
22;; Tests of SRecode template insertion routines and tricks.
23;;
24
25
26(require 'srecode/map)
27(require 'srecode/insert)
28(require 'srecode/dictionary)
29
30
31;;; Code:
32
33;;; MAP DUMP TESTING
34(defun srecode-utest-map-reset ()
35 "Reset, then dump the map of SRecoder templates.
36Probably should be called 'describe-srecode-maps'."
37 (interactive)
38 (message "SRecode Template Path: %S" srecode-map-load-path)
39 ;; Interactive call allows us to dump.
40 (call-interactively 'srecode-get-maps)
41 (switch-to-buffer "*SRECODE MAP*")
42 (message (buffer-string))
43 )
44
45;;; OUTPUT TESTING
46;;
47(defclass srecode-utest-output ()
48 ((point :initarg :point
49 :type string
50 :documentation
51 "Name of this test point.")
52 (name :initarg :name
53 :type string
54 :documentation
55 "Name of the template tested.")
56 (output :initarg :output
57 :type string
58 :documentation
59 "Expected output of the template.")
60 (dict-entries :initarg :dict-entries
61 :initform nil
62 :type list
63 :documentation
64 "Additional dictionary entries to specify.")
65 (pre-fill :initarg :pre-fill
66 :type (or null string)
67 :initform nil
68 :documentation
69 "Text to prefill a buffer with.
70Place cursor on the ! and delete it.
71If there is a second !, the put the mark there."))
72 "A single template test.")
73
74(cl-defmethod srecode-utest-test ((o srecode-utest-output))
75 "Perform the insertion and test the output.
76Assumes that the current buffer is the testing buffer.
77Return NIL on success, or a diagnostic on failure."
78 (let ((fail nil))
79 (catch 'fail-early
80 (with-slots (name (output-1 output) dict-entries pre-fill) o
81 ;; Prepare buffer: erase content and maybe insert pre-fill
82 ;; content.
83 (erase-buffer)
84 (insert (or pre-fill ""))
85 (goto-char (point-min))
86 (let ((start nil))
87 (when (re-search-forward "!" nil t)
88 (goto-char (match-beginning 0))
89 (setq start (point))
90 (replace-match ""))
91 (when (re-search-forward "!" nil t)
92 (push-mark (match-beginning 0) t t)
93 (replace-match ""))
94 (when start (goto-char start)))
95
96 ;; Find a template, perform an insertion and validate the output.
97 (let ((dict (srecode-create-dictionary))
98 (temp (or (srecode-template-get-table
99 (srecode-table) name "test" 'tests)
100 (progn
101 (srecode-map-update-map)
102 (srecode-template-get-table
103 (srecode-table) name "test" 'tests))
104 (progn
105 (setq fail (format "Test template \"%s\" for `%s' not loaded!"
106 name major-mode))
107 (throw 'fail-early t)
108 )))
109 (srecode-handle-region-when-non-active-flag t))
110
111 ;; RESOLVE AND INSERT
112 (let ((entry dict-entries))
113 (while entry
114 (srecode-dictionary-set-value
115 dict (nth 0 entry) (nth 1 entry))
116 (setq entry (nthcdr 1 entry))))
117
118 (srecode-insert-fcn temp dict)
119
120 ;; COMPARE THE OUTPUT
121 (let ((actual (buffer-substring-no-properties
122 (point-min) (point-max))))
123 (if (string= output-1 actual)
124 nil
125
126 (goto-char (point-max))
127 (insert "\n\n ------------- ^^ actual ^^ ------------\n\n
128 ------------- vv expected vv ------------\n\n"
129 output-1)
130 (setq fail
131 (list (format "Entry %s failed:" (oref o point))
132 (buffer-string))
133 )))))
134 )
135 fail))
136
137;;; ARG HANDLER
138;;
139(defun srecode-semantic-handle-:utest (dict)
140 "Add macros into the dictionary DICT for unit testing purposes."
141 (srecode-dictionary-set-value dict "UTESTVAR1" "ARG HANDLER ONE")
142 (srecode-dictionary-set-value dict "UTESTVAR2" "ARG HANDLER TWO")
143 )
144
145(defun srecode-semantic-handle-:utestwitharg (dict)
146 "Add macros into the dictionary DICT based on other vars in DICT."
147 (let ((val1 (srecode-dictionary-lookup-name dict "UTWA"))
148 (nval1 nil))
149 ;; If there is a value, mutate it
150 (if (and val1 (stringp val1))
151 (setq nval1 (upcase val1))
152 ;; No value, make stuff up
153 (setq nval1 "NO VALUE"))
154
155 (srecode-dictionary-set-value dict "UTESTARGXFORM" nval1))
156
157 (let ((dicts (srecode-dictionary-lookup-name dict "UTLOOP")))
158 (dolist (D dicts)
159 ;; For each dictionary, lookup NAME, and transform into
160 ;; something in DICT instead.
161 (let ((sval (srecode-dictionary-lookup-name D "NAME")))
162 (srecode-dictionary-set-value dict (concat "FOO_" sval) sval)
163 )))
164 )
165
166;;; TEST POINTS
167;;
168(defvar srecode-utest-output-entries
169 (list
170 (srecode-utest-output
171 :point "test1" :name "test"
172 :output (concat ";; " (user-full-name) "\n"
173 ";; " (upcase (user-full-name))) )
174 (srecode-utest-output
175 :point "subs" :name "subs"
176 :output ";; Before Loop
177;; After Loop" )
178 (srecode-utest-output
179 :point "firstlast" :name "firstlast"
180 :output "
181;; << -- FIRST
182;; I'm First
183;; I'm Not Last
184;; -- >>
185
186;; << -- MIDDLE
187;; I'm Not First
188;; I'm Not Last
189;; -- >>
190
191;; << -- LAST
192;; I'm Not First
193;; I'm Last
194;; -- >>
195" )
196 (srecode-utest-output
197 :point "gapsomething" :name "gapsomething"
198 :output ";; First Line
199### ALL ALONE ON A LINE ###
200;;Second Line"
201 :pre-fill ";; First Line
202!;;Second Line")
203 (srecode-utest-output
204 :point "wrapsomething" :name "wrapsomething"
205 :output ";; Put this line in front:
206;; First Line
207;; Put this line at the end:"
208 :pre-fill "!;; First Line
209!")
210 (srecode-utest-output
211 :point "inlinetext" :name "inlinetext"
212 :output ";; A big long comment XX*In the middle*XX with cursor in middle"
213 :pre-fill ";; A big long comment XX!XX with cursor in middle")
214
215 (srecode-utest-output
216 :point "wrapinclude-basic" :name "wrapinclude-basic"
217 :output ";; An includable we could use.
218;;
219;; Text after a point inserter."
220 )
221 (srecode-utest-output
222 :point "wrapinclude-basic2" :name "wrapinclude-basic"
223 :output ";; An includable MOOSE we could use.
224;;
225;; Text after a point inserter."
226 :dict-entries '("COMMENT" "MOOSE")
227 )
228 (srecode-utest-output
229 :point "wrapinclude-around" :name "wrapinclude-around"
230 :output ";; An includable we could use.
231;; [VAR]Intermediate Comments
232;; Text after a point inserter."
233 )
234 (srecode-utest-output
235 :point "wrapinclude-around1" :name "wrapinclude-around"
236 :output ";; An includable PENGUIN we could use.
237;; [VAR]Intermediate Comments
238;; Text after a point inserter."
239 :dict-entries '("COMMENT" "PENGUIN")
240 )
241 (srecode-utest-output
242 :point "complex-subdict" :name "complex-subdict"
243 :output ";; I have a cow and a dog.")
244 (srecode-utest-output
245 :point "wrap-new-template" :name "wrap-new-template"
246 :output "template newtemplate
247\"A nice doc string goes here.\"
248----
249Random text in the new template
250----
251bind \"a\""
252 :dict-entries '( "NAME" "newtemplate" "KEY" "a" )
253 )
254 (srecode-utest-output
255 :point "column-data" :name "column-data"
256 :output "Table of Values:
257Left Justified | Right Justified
258FIRST | FIRST
259VERY VERY LONG STRIN | VERY VERY LONG STRIN
260MIDDLE | MIDDLE
261S | S
262LAST | LAST")
263 (srecode-utest-output
264 :point "custom-arg-handler" :name "custom-arg-handler"
265 :output "OUTSIDE SECTION: ARG HANDLER ONE
266INSIDE SECTION: ARG HANDLER ONE")
267 (srecode-utest-output
268 :point "custom-arg-w-arg none" :name "custom-arg-w-arg"
269 :output "Value of xformed UTWA: NO VALUE")
270 (srecode-utest-output
271 :point "custom-arg-w-arg upcase" :name "custom-arg-w-arg"
272 :dict-entries '( "UTWA" "uppercaseme" )
273 :output "Value of xformed UTWA: UPPERCASEME")
274 (srecode-utest-output
275 :point "custom-arg-w-subdict" :name "custom-arg-w-subdict"
276 :output "All items here: item1 item2 item3")
277
278 ;; Test cases for new "section ... end" dictionary syntax
279 (srecode-utest-output
280 :point "nested-dictionary-syntax-flat"
281 :name "nested-dictionary-syntax-flat"
282 :output "sub item1")
283 (srecode-utest-output
284 :point "nested-dictionary-syntax-nesting"
285 :name "nested-dictionary-syntax-nesting"
286 :output "item11-item11-item21-item31 item21-item11-item21-item31 item31-item311-item321 ")
287 (srecode-utest-output
288 :point "nested-dictionary-syntax-mixed"
289 :name "nested-dictionary-syntax-mixed"
290 :output "item1 item2"))
291 "Test point entries for the template output tests.")
292
293;;; Master Harness
294;;
295(defvar srecode-utest-testfile
296 (expand-file-name (concat (make-temp-name "srecode-utest-") ".srt") temporary-file-directory)
297 "File used to do testing.")
298
299(ert-deftest srecode-utest-template-output ()
300 "Test various template insertion options."
301 (save-excursion
302 (let ((testbuff (find-file-noselect srecode-utest-testfile)))
303
304 (set-buffer testbuff)
305
306 (srecode-load-tables-for-mode major-mode)
307 (srecode-load-tables-for-mode major-mode 'tests)
308
309 (should (srecode-table major-mode))
310
311 ;; Loop over the output testpoints.
312
313 (dolist (p srecode-utest-output-entries)
314 (set-buffer testbuff) ;; XEmacs causes a buffer switch. I don't know why
315 (should-not (srecode-utest-test p))
316 )
317
318 ))
319 (when (file-exists-p srecode-utest-testfile)
320 (delete-file srecode-utest-testfile)))
321
322;;; Project test
323;;
324;; Test that "project" specification works ok.
325
326(ert-deftest srecode-utest-project ()
327 "Test thta project filtering works."
328 (save-excursion
329 (let ((testbuff (find-file-noselect srecode-utest-testfile))
330 (temp nil))
331
332 (set-buffer testbuff)
333 (erase-buffer)
334
335 ;; Load the basics, and test that we can't find the application templates.
336 (srecode-load-tables-for-mode major-mode)
337
338 (should (srecode-table major-mode))
339
340 (setq temp (srecode-template-get-table (srecode-table)
341 "test-project"
342 "test"
343 'tests
344 ))
345 (when temp
346 (should-not "App Template Loaded when not specified."))
347
348 ;; Load the application templates, and make sure we can find them.
349 (srecode-load-tables-for-mode major-mode 'tests)
350
351 (setq temp (srecode-template-get-table (srecode-table)
352 "test-project"
353 "test"
354 'tests
355 ))
356
357 (when (not temp)
358 (should-not "Failed to load app specific template when available."))
359
360 ;; Temporarily change the home of this file. This will make the
361 ;; project template go out of scope.
362 (let ((default-directory (expand-file-name "~/")))
363
364 (setq temp (srecode-template-get-table (srecode-table)
365 "test-project"
366 "test"
367 'tests
368 ))
369
370 (when temp
371 (should-not "Project specific template available when in wrong directory."))
372
373 )))
374 (when (file-exists-p srecode-utest-testfile)
375 (delete-file srecode-utest-testfile)))
376
377
378(provide 'cedet/srecode-utest-template)
379;;; srecode-utest-template.el ends here