Give '$' punctuation syntax in make-mode (Bug#24477)
[emacs.git] / test / lisp / emacs-lisp / edebug-tests.el
blob85f6bd47db2c09f379760a30ce2e1fe01e8983f8
1 ;;; edebug-tests.el --- Edebug test suite -*- lexical-binding:t -*-
3 ;; Copyright (C) 2017-2018 Free Software Foundation, Inc.
5 ;; Author: Gemini Lasswell
7 ;; This file is part of GNU Emacs.
9 ;; This program is free software: you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation, either version 3 of the
12 ;; License, or (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; These tests focus on Edebug's user interface for setting
25 ;; breakpoints, stepping through and tracing code, and evaluating
26 ;; values used by the code. In addition there are some tests of
27 ;; Edebug's reader. There are large parts of Edebug's functionality
28 ;; not covered by these tests, including coverage testing, macro
29 ;; specifications, and the eval list buffer.
31 ;;; Code:
33 (require 'cl-lib)
34 (require 'ert)
35 (require 'ert-x)
36 (require 'edebug)
37 (require 'kmacro)
39 ;; Use `eval-and-compile' because this is used by the macro
40 ;; `edebug-tests-deftest'.
41 (eval-and-compile
42 (defvar edebug-tests-sample-code-file
43 (expand-file-name
44 "edebug-resources/edebug-test-code.el"
45 (file-name-directory (or (bound-and-true-p byte-compile-current-file)
46 load-file-name
47 buffer-file-name)))
48 "Name of file containing code samples for Edebug tests."))
50 (defvar edebug-tests-temp-file nil
51 "Name of temp file containing sample code stripped of stop point symbols.")
52 (defvar edebug-tests-stop-points nil
53 "An alist of alists mapping function symbol -> stop point name -> marker.
54 Used by the tests to refer to locations in `edebug-tests-temp-file'.")
55 (defvar edebug-tests-messages nil
56 "Messages collected during execution of the current test.")
58 (defvar edebug-tests-@-result 'no-result
59 "Return value of `edebug-tests-func', or no-result if there isn't one yet.")
61 (defvar edebug-tests-failure-in-post-command nil
62 "An error trapped in `edebug-tests-post-command'.
63 Since `should' failures which happen inside `post-command-hook' will
64 be trapped by the command loop, this preserves them until we get
65 back to the top level.")
67 (defvar edebug-tests-keymap
68 (let ((map (make-sparse-keymap)))
69 (define-key map "@" 'edebug-tests-call-instrumented-func)
70 (define-key map "C-u" 'universal-argument)
71 (define-key map "C-p" 'previous-line)
72 (define-key map "C-n" 'next-line)
73 (define-key map "C-b" 'backward-char)
74 (define-key map "C-a" 'move-beginning-of-line)
75 (define-key map "C-e" 'move-end-of-line)
76 (define-key map "C-k" 'kill-line)
77 (define-key map "M-x" 'execute-extended-command)
78 (define-key map "C-M-x" 'eval-defun)
79 (define-key map "C-x X b" 'edebug-set-breakpoint)
80 (define-key map "C-x X w" 'edebug-where)
81 map)
82 "Keys used by the keyboard macros in Edebug's tests.")
84 ;;; Macros for defining tests:
86 (defmacro edebug-tests-with-default-config (&rest body)
87 "Create a consistent environment for an Edebug test BODY to run in."
88 (declare (debug (body)))
89 `(cl-letf* (
90 ;; These defcustoms are set to their original value.
91 (edebug-setup-hook nil)
92 (edebug-all-defs nil)
93 (edebug-all-forms nil)
94 (edebug-eval-macro-args nil)
95 (edebug-save-windows t)
96 (edebug-save-displayed-buffer-points nil)
97 (edebug-initial-mode 'step)
98 (edebug-trace nil)
99 (edebug-test-coverage nil)
100 (edebug-print-length 50)
101 (edebug-print-level 50)
102 (edebug-print-circle t)
103 (edebug-unwrap-results nil)
104 (edebug-on-error t)
105 (edebug-on-quit t)
106 (edebug-global-break-condition nil)
107 (edebug-sit-for-seconds 1)
109 ;; sit-on interferes with keyboard macros.
110 (edebug-sit-on-break nil)
111 (edebug-continue-kbd-macro t))
112 ,@body))
114 (defmacro edebug-tests-with-normal-env (&rest body)
115 "Set up the environment for an Edebug test BODY, run it, and clean up."
116 (declare (debug (body)))
117 `(edebug-tests-with-default-config
118 (let ((edebug-tests-failure-in-post-command nil)
119 (edebug-tests-temp-file (make-temp-file "edebug-tests-" nil ".el")))
120 (edebug-tests-setup-code-file edebug-tests-temp-file)
121 (ert-with-message-capture
122 edebug-tests-messages
123 (unwind-protect
124 (with-current-buffer (find-file edebug-tests-temp-file)
125 (read-only-mode)
126 (setq lexical-binding t)
127 (eval-buffer)
128 ,@body
129 (when edebug-tests-failure-in-post-command
130 (signal (car edebug-tests-failure-in-post-command)
131 (cdr edebug-tests-failure-in-post-command))))
132 (unload-feature 'edebug-test-code)
133 (with-current-buffer (find-file-noselect edebug-tests-temp-file)
134 (set-buffer-modified-p nil))
135 (ignore-errors (kill-buffer (find-file-noselect
136 edebug-tests-temp-file)))
137 (ignore-errors (delete-file edebug-tests-temp-file)))))))
139 ;; The following macro and its support functions implement an extension
140 ;; to keyboard macros to allow interleaving of keyboard macro
141 ;; events with evaluation of Lisp expressions. The Lisp expressions
142 ;; are called from within `post-command-hook', which is a strategy
143 ;; inspired by `kmacro-step-edit-macro'.
145 ;; Some of the details necessary to get this to work with Edebug are:
146 ;; -- ERT's `should' macros raise errors, and errors within
147 ;; `post-command-hook' are trapped by the command loop. The
148 ;; workaround is to trap and save an error inside the hook
149 ;; function and reraise it after the macro exits.
150 ;; -- `edebug-continue-kbd-macro' must be non-nil.
151 ;; -- Edebug calls `exit-recursive-edit' which turns off keyboard
152 ;; macro execution. Solved with an advice wrapper for
153 ;; `exit-recursive-edit' which preserves the keyboard macro state.
155 (defmacro edebug-tests-run-kbd-macro (&rest macro)
156 "Run a MACRO consisting of both keystrokes and test assertions.
157 MACRO should be a list, where each item is either a keyboard
158 macro segment (in string or vector form) or a Lisp expression.
159 Convert the macro segments into keyboard macros and execute them.
160 After the execution of the last event of each segment, evaluate
161 the Lisp expressions following the segment."
162 (let ((prepared (edebug-tests-prepare-macro macro)))
163 `(edebug-tests-run-macro ,@prepared)))
165 ;; Make support functions for edebug-tests-run-kbd-macro
166 ;; available at compile time.
167 (eval-and-compile
168 (defun edebug-tests-prepare-macro (macro)
169 "Prepare a MACRO for execution.
170 MACRO should be a list containing strings, vectors, and Lisp
171 forms. Convert the strings and vectors to keyboard macros in
172 vector representation and concatenate them to make a single
173 keyboard macro. Also build a list of the same length as the
174 number of events in the keyboard macro. Each item in that list
175 will contain the code to evaluate after the corresponding event
176 in the keyboard macro, either nil or a thunk built from the forms
177 in the original list. Return a list containing the keyboard
178 macro as the first item, followed by the list of thunks and/or
179 nils."
180 (cl-loop
181 for item = (pop macro)
182 while item
183 for segment = (read-kbd-macro item)
184 for thunk = (edebug-tests-wrap-thunk
185 (cl-loop
186 for form in macro
187 until (or (stringp form) (vectorp form))
188 collect form
189 do (pop macro)))
190 vconcat segment into segments
191 append (edebug-tests-pad-thunk-list (length segment) thunk)
192 into thunk-list
194 finally return (cons segments thunk-list)))
196 (defun edebug-tests-wrap-thunk (body)
197 "If BODY is non-nil, wrap it with a lambda form."
198 (when body
199 `(lambda () ,@body)))
201 (defun edebug-tests-pad-thunk-list (length thunk)
202 "Return a list with LENGTH elements with THUNK in the last position.
203 All other elements will be nil."
204 (let ((thunk-seg (make-list length nil)))
205 (setf (car (last thunk-seg)) thunk)
206 thunk-seg)))
208 ;;; Support for test execution:
210 (defvar edebug-tests-thunks nil
211 "List containing thunks to run after each command in a keyboard macro.")
212 (defvar edebug-tests-kbd-macro-index nil
213 "Index into `edebug-tests-run-unpacked-kbd-macro's current keyboard macro.")
215 (defun edebug-tests-run-macro (kbdmac &rest thunks)
216 "Run a keyboard macro and execute a thunk after each command in it.
217 KBDMAC should be a vector of events and THUNKS a list of the
218 same length containing thunks and/or nils. Run the macro, and
219 after the execution of every command in the macro (which may not
220 be the same as every keystroke) execute the thunk at the same
221 index."
222 (let* ((edebug-tests-thunks thunks)
223 (edebug-tests-kbd-macro-index 0)
224 saved-local-map)
225 (with-current-buffer (find-file-noselect edebug-tests-temp-file)
226 (setq saved-local-map overriding-local-map)
227 (setq overriding-local-map edebug-tests-keymap)
228 (add-hook 'post-command-hook 'edebug-tests-post-command))
229 (advice-add 'exit-recursive-edit
230 :around 'edebug-tests-preserve-keyboard-macro-state)
231 (unwind-protect
232 (kmacro-call-macro nil nil nil kbdmac)
233 (advice-remove 'exit-recursive-edit
234 'edebug-tests-preserve-keyboard-macro-state)
235 (with-current-buffer (find-file-noselect edebug-tests-temp-file)
236 (setq overriding-local-map saved-local-map)
237 (remove-hook 'post-command-hook 'edebug-tests-post-command)))))
239 (defun edebug-tests-preserve-keyboard-macro-state (orig &rest args)
240 "Call ORIG with ARGS preserving the value of `executing-kbd-macro'.
241 Useful to prevent `exit-recursive-edit' from stopping the current
242 keyboard macro."
243 (let ((executing-kbd-macro executing-kbd-macro))
244 (apply orig args)))
246 (defun edebug-tests-post-command ()
247 "Run the thunk from `edebug-tests-thunks' matching the keyboard macro index."
248 (when (and edebug-tests-kbd-macro-index
249 (> executing-kbd-macro-index edebug-tests-kbd-macro-index))
250 (let ((thunk (nth (1- executing-kbd-macro-index) edebug-tests-thunks)))
251 (when thunk
252 (condition-case err
253 (funcall thunk)
254 (error
255 (setq edebug-tests-failure-in-post-command err)
256 (signal (car err) (cdr err)))))
257 (setq edebug-tests-kbd-macro-index executing-kbd-macro-index))))
259 (defvar edebug-tests-func nil
260 "Instrumented function used to launch Edebug.")
261 (defvar edebug-tests-args nil
262 "Arguments for `edebug-tests-func'.")
264 (defun edebug-tests-setup-@ (def-name args edebug-it)
265 "Set up the binding for @ in `edebug-tests-keymap'.
266 Find a definition for DEF-NAME in the current buffer and evaluate it.
267 Set globals so that `edebug-tests-call-instrumented-func' which
268 is bound to @ for edebug-tests' keyboard macros will call it with
269 ARGS. EDEBUG-IT is passed through to `eval-defun'."
270 (edebug-tests-locate-def def-name)
271 (eval-defun edebug-it)
272 (let* ((full-name (concat "edebug-test-code-" def-name))
273 (sym (intern-soft full-name)))
274 (should (and sym (fboundp sym)))
275 (setq edebug-tests-func sym
276 edebug-tests-args args)
277 (setq edebug-tests-@-result 'no-result)))
279 (defun edebug-tests-call-instrumented-func ()
280 "Call `edebug-tests-func' with `edebug-tests-args' and save the results."
281 (interactive)
282 (let ((result (apply edebug-tests-func edebug-tests-args)))
283 (should (eq edebug-tests-@-result 'no-result))
284 (setq edebug-tests-@-result result)))
286 (defun edebug-tests-should-be-at (def-name point-name)
287 "Require that point be at the location in DEF-NAME named POINT-NAME.
288 DEF-NAME should be the suffix of a definition in the code samples
289 file (the part after \"edebug-tests\")."
290 (let ((stop-point (edebug-tests-get-stop-point def-name point-name)))
291 (should (eq (current-buffer) (find-file-noselect edebug-tests-temp-file)))
292 (should (eql (point) stop-point))))
294 (defun edebug-tests-get-stop-point (def-name point-name)
295 "Return the position in DEF-NAME of the stop point named POINT-NAME.
296 DEF-NAME should be the suffix of a definition in the code samples
297 file (the part after \"edebug-tests\")."
298 (let* ((full-name (concat "edebug-test-code-" def-name))(stop-point
299 (cdr (assoc point-name
300 (cdr (assoc full-name edebug-tests-stop-points))))))
301 (unless stop-point
302 (ert-fail (format "%s not found in %s" point-name full-name)))
303 stop-point))
305 (defun edebug-tests-should-match-result-in-messages (value)
306 "Require that VALUE (a string) match an Edebug result in *Messages*.
307 Then clear edebug-tests' saved messages."
308 (should (string-match-p (concat "Result: " (regexp-quote value) "$")
309 edebug-tests-messages))
310 (setq edebug-tests-messages ""))
312 (defun edebug-tests-locate-def (def-name)
313 "Search for a definition of DEF-NAME from the start of the current buffer.
314 Place point at the end of DEF-NAME in the buffer."
315 (goto-char (point-min))
316 (re-search-forward (concat "def\\S-+ edebug-test-code-" def-name)))
318 (defconst edebug-tests-start-of-next-def-regexp "^(\\S-*def\\S-+ \\(\\S-+\\)"
319 "Regexp used to match the start of a definition.")
320 (defconst edebug-tests-stop-point-regexp "!\\(\\S-+?\\)!"
321 "Regexp used to match a stop point annotation in the sample code.")
323 ;;; Set up buffer containing code samples:
325 (defmacro edebug-tests-deduplicate (name names-and-numbers)
326 "Return a unique variation on NAME.
327 NAME should be a string and NAMES-AND-NUMBERS an alist which can
328 be used by this macro to retain state. If NAME for example is
329 \"symbol\" then the first and subsequent uses of this macro will
330 evaluate to \"symbol\", \"symbol-1\", \"symbol-2\", etc."
331 (let ((g-name (gensym))
332 (g-duplicate (gensym)))
333 `(let* ((,g-name ,name)
334 (,g-duplicate (assoc ,g-name ,names-and-numbers)))
335 (if (null ,g-duplicate)
336 (progn
337 (push (cons ,g-name 0) ,names-and-numbers)
338 ,g-name)
339 (cl-incf (cdr ,g-duplicate))
340 (format "%s-%s" ,g-name (cdr ,g-duplicate))))))
342 (defun edebug-tests-setup-code-file (tmpfile)
343 "Extract stop points and loadable code from the sample code file.
344 Write the loadable code to a buffer for TMPFILE, and set
345 `edebug-tests-stop-points' to a map from defined symbols to stop
346 point names to positions in the file."
347 (with-current-buffer (find-file-noselect edebug-tests-sample-code-file)
348 (let ((marked-up-code (buffer-string)))
349 (with-temp-file tmpfile
350 (insert marked-up-code))))
352 (with-current-buffer (find-file-noselect tmpfile)
353 (let ((stop-points
354 ;; Delete all the !name! annotations from the code, but remember
355 ;; their names and where they were in an alist.
356 (cl-loop
357 initially (goto-char (point-min))
358 while (re-search-forward edebug-tests-stop-point-regexp nil t)
359 for name = (match-string-no-properties 1)
360 do (replace-match "")
361 collect (cons name (point))))
362 names-and-numbers)
364 ;; Now build an alist mapping definition names to annotation
365 ;; names and positions.
366 ;; If duplicate symbols exist in the file, enter them in the
367 ;; alist as symbol, symbol-1, symbol-2 etc.
368 (setq edebug-tests-stop-points
369 (cl-loop
370 initially (goto-char (point-min))
371 while (re-search-forward edebug-tests-start-of-next-def-regexp
372 nil t)
373 for name =
374 (edebug-tests-deduplicate (match-string-no-properties 1)
375 names-and-numbers)
376 for end-of-def =
377 (save-match-data
378 (save-excursion
379 (re-search-forward edebug-tests-start-of-next-def-regexp
380 nil 0)
381 (point)))
382 collect (cons name
383 (cl-loop
384 while (and stop-points
385 (< (cdar stop-points) end-of-def))
386 collect (pop stop-points))))))))
388 ;;; Tests
390 (ert-deftest edebug-tests-check-keymap ()
391 "Verify that `edebug-mode-map' is compatible with these tests.
392 If this test fails, one of two things is true. Either your
393 customizations modify `edebug-mode-map', in which case starting
394 Emacs with the -Q flag should fix the problem, or
395 `edebug-mode-map' has changed in edebug.el, in which case this
396 test and possibly others should be updated."
397 ;; The reason verify-keybinding is a macro instead of a function is
398 ;; that in the event of a failure, it makes the keybinding that
399 ;; failed show up in ERT's output.
400 (cl-macrolet ((verify-keybinding (key binding)
401 `(should (eq (lookup-key edebug-mode-map ,key)
402 ,binding))))
403 (verify-keybinding " " 'edebug-step-mode)
404 (verify-keybinding "n" 'edebug-next-mode)
405 (verify-keybinding "g" 'edebug-go-mode)
406 (verify-keybinding "G" 'edebug-Go-nonstop-mode)
407 (verify-keybinding "t" 'edebug-trace-mode)
408 (verify-keybinding "T" 'edebug-Trace-fast-mode)
409 (verify-keybinding "c" 'edebug-continue-mode)
410 (verify-keybinding "C" 'edebug-Continue-fast-mode)
411 (verify-keybinding "f" 'edebug-forward-sexp)
412 (verify-keybinding "h" 'edebug-goto-here)
413 (verify-keybinding "I" 'edebug-instrument-callee)
414 (verify-keybinding "i" 'edebug-step-in)
415 (verify-keybinding "o" 'edebug-step-out)
416 (verify-keybinding "q" 'top-level)
417 (verify-keybinding "Q" 'edebug-top-level-nonstop)
418 (verify-keybinding "a" 'abort-recursive-edit)
419 (verify-keybinding "S" 'edebug-stop)
420 (verify-keybinding "b" 'edebug-set-breakpoint)
421 (verify-keybinding "u" 'edebug-unset-breakpoint)
422 (verify-keybinding "B" 'edebug-next-breakpoint)
423 (verify-keybinding "x" 'edebug-set-conditional-breakpoint)
424 (verify-keybinding "X" 'edebug-set-global-break-condition)
425 (verify-keybinding "r" 'edebug-previous-result)
426 (verify-keybinding "e" 'edebug-eval-expression)
427 (verify-keybinding "\C-x\C-e" 'edebug-eval-last-sexp)
428 (verify-keybinding "E" 'edebug-visit-eval-list)
429 (verify-keybinding "w" 'edebug-where)
430 (verify-keybinding "v" 'edebug-view-outside) ;; maybe obsolete??
431 (verify-keybinding "p" 'edebug-bounce-point)
432 (verify-keybinding "P" 'edebug-view-outside) ;; same as v
433 (verify-keybinding "W" 'edebug-toggle-save-windows)
434 (verify-keybinding "?" 'edebug-help)
435 (verify-keybinding "d" 'edebug-backtrace)
436 (verify-keybinding "-" 'negative-argument)
437 (verify-keybinding "=" 'edebug-temp-display-freq-count)))
439 (ert-deftest edebug-tests-stop-point-at-start-of-first-instrumented-function ()
440 "Edebug stops at the beginning of an instrumented function."
441 (edebug-tests-with-normal-env
442 (edebug-tests-setup-@ "fac" '(0) t)
443 (edebug-tests-run-kbd-macro
444 "@" (edebug-tests-should-be-at "fac" "start")
445 "SPC" (edebug-tests-should-be-at "fac" "step")
446 "g" (should (equal edebug-tests-@-result 1)))))
448 (ert-deftest edebug-tests-step-showing-evaluation-results ()
449 "Edebug prints expression evaluation results to the echo area."
450 (edebug-tests-with-normal-env
451 (edebug-tests-setup-@ "concat" '("x" "y" nil) t)
452 (edebug-tests-run-kbd-macro
453 "@" (edebug-tests-should-be-at "concat" "start")
454 "SPC" (edebug-tests-should-be-at "concat" "flag")
455 (edebug-tests-should-match-result-in-messages "nil")
456 "SPC" (edebug-tests-should-be-at "concat" "else-start")
457 "SPC" (edebug-tests-should-be-at "concat" "else-b")
458 (edebug-tests-should-match-result-in-messages "\"y\"")
459 "SPC" (edebug-tests-should-be-at "concat" "else-a")
460 (edebug-tests-should-match-result-in-messages "\"x\"")
461 "SPC" (edebug-tests-should-be-at "concat" "else-concat")
462 (edebug-tests-should-match-result-in-messages "\"yx\"")
463 "SPC" (edebug-tests-should-be-at "concat" "if")
464 (edebug-tests-should-match-result-in-messages "\"yx\"")
465 "SPC" (should (equal edebug-tests-@-result "yx")))))
467 (ert-deftest edebug-tests-set-breakpoint-at-point ()
468 "Edebug can set a breakpoint at point."
469 (edebug-tests-with-normal-env
470 (edebug-tests-setup-@ "concat" '("x" "y" t) t)
471 (edebug-tests-run-kbd-macro
472 "@" (edebug-tests-should-be-at "concat" "start")
473 "C-n C-e b C-n" ; Move down, set a breakpoint and move away.
474 "g" (edebug-tests-should-be-at "concat" "then-concat")
475 (edebug-tests-should-match-result-in-messages "\"xy\"")
476 "g" (should (equal edebug-tests-@-result "xy")))))
478 (ert-deftest edebug-tests-set-temporary-breakpoint-at-point ()
479 "Edebug can set a temporary breakpoint at point."
480 (edebug-tests-with-normal-env
481 (edebug-tests-setup-@ "range" '(3) t)
482 (edebug-tests-run-kbd-macro
483 "@" (edebug-tests-should-be-at "range" "start")
484 "C-n C-n C-n C-e" ; Move down to the end of a sexp in the loop.
485 "C-u b" ; Set a temporary breakpoint.
486 "C-n" ; Move away.
487 "g" (edebug-tests-should-be-at "range" "loop")
488 (edebug-tests-should-match-result-in-messages "(0)")
489 "g" (should (equal edebug-tests-@-result '(0 1 2))))))
491 (ert-deftest edebug-tests-clear-breakpoint ()
492 "Edebug can clear a breakpoint."
493 (edebug-tests-with-normal-env
494 (edebug-tests-setup-@ "range" '(3) t)
495 (edebug-tests-run-kbd-macro
497 (message "after @")
498 (edebug-tests-should-be-at "range" "start")
499 "C-n C-n C-n C-e b C-n" ; Move down, set a breakpoint and move away.
500 "g" (edebug-tests-should-be-at "range" "loop")
501 (edebug-tests-should-match-result-in-messages "(0)")
502 "g" (edebug-tests-should-be-at "range" "loop")
503 (edebug-tests-should-match-result-in-messages "(1 0)")
504 "u" ; Unset the breakpoint.
505 "g" (should (equal edebug-tests-@-result '(0 1 2))))))
507 (ert-deftest edebug-tests-move-point-to-next-breakpoint ()
508 "Edebug can move point to the next breakpoint."
509 (edebug-tests-with-normal-env
510 (edebug-tests-setup-@ "concat" '("a" "b" nil) t)
511 (edebug-tests-run-kbd-macro
512 "@" (edebug-tests-should-be-at "concat" "start")
513 "C-n C-e b" ; Move down, set a breakpoint.
514 "C-n b" ; Set another breakpoint on the next line.
515 "C-p C-p C-p" ; Move back up.
516 "B" (edebug-tests-should-be-at "concat" "then-concat")
517 "B" (edebug-tests-should-be-at "concat" "else-concat")
518 "G" (should (equal edebug-tests-@-result "ba")))))
520 (ert-deftest edebug-tests-move-point-back-to-stop-point ()
521 "Edebug can move point back to a stop point."
522 (edebug-tests-with-normal-env
523 (let ((test-buffer (get-buffer-create "edebug-tests-temp")))
524 (edebug-tests-setup-@ "fac" '(4) t)
525 (edebug-tests-run-kbd-macro
526 "@" (edebug-tests-should-be-at "fac" "start")
527 "C-n w" (edebug-tests-should-be-at "fac" "start")
528 (pop-to-buffer test-buffer)
529 "C-x X w" (edebug-tests-should-be-at "fac" "start")
530 "g" (should (equal edebug-tests-@-result 24)))
531 (ignore-errors (kill-buffer test-buffer)))))
533 (ert-deftest edebug-tests-jump-to-point ()
534 "Edebug can stop at a temporary breakpoint at point."
535 (edebug-tests-with-normal-env
536 (edebug-tests-setup-@ "range" '(3) t)
537 (edebug-tests-run-kbd-macro
538 "@" (edebug-tests-should-be-at "range" "start")
539 "C-n C-n C-n C-e" ; Move down to the end of a sexp in the loop.
540 "h" (edebug-tests-should-be-at "range" "loop")
541 (edebug-tests-should-match-result-in-messages "(0)")
542 "g" (should (equal edebug-tests-@-result '(0 1 2))))))
544 (ert-deftest edebug-tests-jump-forward-one-sexp ()
545 "Edebug can run the program for one expression."
546 (edebug-tests-with-normal-env
547 (edebug-tests-setup-@ "range" '(3) t)
548 (edebug-tests-run-kbd-macro
549 "@" (edebug-tests-should-be-at "range" "start")
550 "SPC SPC f" (edebug-tests-should-be-at "range" "test")
551 "g" (should (equal edebug-tests-@-result '(0 1 2))))))
553 (ert-deftest edebug-tests-run-out-of-containing-sexp ()
554 "Edebug can run the program until the end of the containing sexp."
555 (edebug-tests-with-normal-env
556 (edebug-tests-setup-@ "range" '(3) t)
557 (edebug-tests-run-kbd-macro
558 "@" (edebug-tests-should-be-at "range" "start")
559 "SPC SPC f" (edebug-tests-should-be-at "range" "test")
560 "o" (edebug-tests-should-be-at "range" "end-loop")
561 (edebug-tests-should-match-result-in-messages "nil")
562 "g" (should (equal edebug-tests-@-result '(0 1 2))))))
564 (ert-deftest edebug-tests-observe-breakpoint-in-source ()
565 "Edebug will stop at a breakpoint embedded in source code."
566 (edebug-tests-with-normal-env
567 (edebug-tests-setup-@ "choices" '(8) t)
568 (edebug-tests-run-kbd-macro
569 "@" (edebug-tests-should-be-at "choices" "start")
570 "g" (edebug-tests-should-be-at "choices" "edebug")
571 "g" (should (equal edebug-tests-@-result nil)))))
573 (ert-deftest edebug-tests-set-conditional-breakpoint ()
574 "Edebug can set and observe a conditional breakpoint."
575 (edebug-tests-with-normal-env
576 (edebug-tests-setup-@ "fac" '(5) t)
577 (edebug-tests-run-kbd-macro
578 "@" (edebug-tests-should-be-at "fac" "start")
579 ;; Set conditional breakpoint at end of next line.
580 "C-n C-e x (eql SPC n SPC 3) RET"
581 "g" (edebug-tests-should-be-at "fac" "mult")
582 (edebug-tests-should-match-result-in-messages "6 (#o6, #x6, ?\\C-f)")
583 "g" (should (equal edebug-tests-@-result 120)))))
585 (ert-deftest edebug-tests-error-trying-to-set-breakpoint-in-uninstrumented-code
587 "Edebug refuses to set a breakpoint in uninstrumented code."
588 (edebug-tests-with-normal-env
589 (edebug-tests-setup-@ "fac" '(5) t)
590 (let* ((debug-on-error nil)
591 (edebug-on-error nil)
592 error-message
593 (command-error-function (lambda (&rest args)
594 (setq error-message (cadar args)))))
595 (edebug-tests-run-kbd-macro
596 "@" (edebug-tests-should-be-at "fac" "start")
597 "C-u 10 C-n" ; Move down and out of instrumented function.
598 "b" (should (string-match-p "Not inside instrumented form"
599 error-message))
600 ;; The error stopped the keyboard macro. Start it again.
601 (should-not executing-kbd-macro)
602 (setq executing-kbd-macro t)
603 "g"))))
605 (ert-deftest edebug-tests-set-and-break-on-global-condition ()
606 "Edebug can break when a global condition becomes true."
607 (edebug-tests-with-normal-env
608 (edebug-tests-setup-@ "multiply" '(5 3) t)
609 (edebug-tests-run-kbd-macro
610 "@" (edebug-tests-should-be-at "multiply" "start")
611 "X (> SPC edebug-test-code-total SPC 10) RET"
612 (should edebug-global-break-condition)
613 "g" (edebug-tests-should-be-at "multiply" "setq")
614 (should (eql (symbol-value 'edebug-test-code-total) 12))
615 "X C-a C-k nil RET" ; Remove suggestion before entering nil.
616 "g" (should (equal edebug-tests-@-result 15)))))
618 (ert-deftest edebug-tests-trace-showing-results-at-stop-points ()
619 "Edebug can trace execution, showing results at stop points."
620 (edebug-tests-with-normal-env
621 (edebug-tests-setup-@ "concat" '("x" "y" nil) t)
622 (edebug-tests-run-kbd-macro
623 "@" (edebug-tests-should-be-at "concat" "start")
624 "T" (should (string-match-p
625 (concat "Result: nil\n.*?"
626 "Result: \"y\"\n.*?"
627 "Result: \"x\"\n.*?"
628 "Result: \"yx\"\n.*?"
629 "Result: \"yx\"\n")
630 edebug-tests-messages))
631 (should (equal edebug-tests-@-result "yx")))))
633 (ert-deftest edebug-tests-trace-showing-results-at-breakpoints ()
634 "Edebug can trace execution, showing results at breakpoints."
635 (edebug-tests-with-normal-env
636 (edebug-tests-locate-def "format-vector-node")
637 (edebug-tests-run-kbd-macro "C-u C-M-x C-n C-n C-e C-x X b")
638 (edebug-tests-locate-def "format-list-node")
639 (edebug-tests-run-kbd-macro "C-u C-M-x C-n C-n C-e C-x X b")
640 (edebug-tests-setup-@ "format-node" '(([a b] [c d])) t)
641 (edebug-tests-run-kbd-macro
642 "@" (edebug-tests-should-be-at "format-node" "start")
643 "C" (should (string-match-p
644 (concat "Result: \"ab\"\n.*?"
645 "Result: \"cd\"\n.*?"
646 "Result: \"\\[ab]\\[cd]\"\n")
647 edebug-tests-messages))
648 (should (equal edebug-tests-@-result "{[ab][cd]}")))))
650 (ert-deftest edebug-tests-trace-function-call-and-return ()
651 "Edebug can create a trace of function calls and returns."
652 (edebug-tests-with-normal-env
653 (edebug-tests-locate-def "format-vector-node")
654 (eval-defun t)
655 (edebug-tests-locate-def "format-list-node")
656 (eval-defun t)
657 (edebug-tests-setup-@ "format-node" '((a [b])) t)
658 (let ((edebug-trace t)
659 (trace-start (with-current-buffer
660 (get-buffer-create edebug-trace-buffer) (point-max))))
661 (edebug-tests-run-kbd-macro
662 "@" (edebug-tests-should-be-at "format-node" "start")
663 "g" (should (equal edebug-tests-@-result "{a[b]}")))
664 (with-current-buffer edebug-trace-buffer
665 (should (string=
666 "{ edebug-test-code-format-node args: ((a [b]))
667 :{ edebug-test-code-format-list-node args: ((a [b]))
668 ::{ edebug-test-code-format-node args: (a)
669 ::} edebug-test-code-format-node result: a
670 ::{ edebug-test-code-format-node args: ([b])
671 :::{ edebug-test-code-format-vector-node args: ([b])
672 ::::{ edebug-test-code-format-node args: (b)
673 ::::} edebug-test-code-format-node result: b
674 :::} edebug-test-code-format-vector-node result: [b]
675 ::} edebug-test-code-format-node result: [b]
676 :} edebug-test-code-format-list-node result: {a[b]}
677 } edebug-test-code-format-node result: {a[b]}
678 " (buffer-substring trace-start (point-max))))))))
680 (ert-deftest edebug-tests-evaluate-expressions ()
681 "Edebug can evaluate an expression in the context outside of itself."
682 (edebug-tests-with-normal-env
683 (edebug-tests-setup-@ "range" '(2) t)
684 (edebug-tests-run-kbd-macro
685 "@" (edebug-tests-should-be-at "range" "start")
686 "SPC SPC f" (edebug-tests-should-be-at "range" "test")
687 (edebug-tests-should-match-result-in-messages "t")
688 "e (- SPC num SPC index) RET"
689 ;; Edebug just prints the result without "Result:"
690 (should (string-match-p
691 (regexp-quote "2 (#o2, #x2, ?\\C-b)")
692 edebug-tests-messages))
693 "g" (should (equal edebug-tests-@-result '(0 1))))
695 ;; Do it again with lexical-binding turned off.
696 (setq lexical-binding nil)
697 (eval-buffer)
698 (should-not lexical-binding)
699 (edebug-tests-setup-@ "range" '(2) t)
700 (edebug-tests-run-kbd-macro
701 "@" (edebug-tests-should-be-at "range" "start")
702 "SPC SPC f" (edebug-tests-should-be-at "range" "test")
703 (edebug-tests-should-match-result-in-messages "t")
704 "e (- SPC num SPC index) RET"
705 ;; Edebug just prints the result without "Result:"
706 (should (string-match-p
707 (regexp-quote "2 (#o2, #x2, ?\\C-b)")
708 edebug-tests-messages))
709 "g" (should (equal edebug-tests-@-result '(0 1))))))
711 (ert-deftest edebug-tests-step-into-function ()
712 "Edebug can step into a function."
713 (edebug-tests-with-normal-env
714 (edebug-tests-setup-@ "format-node" '([b]) t)
715 (edebug-tests-run-kbd-macro
716 "@" (edebug-tests-should-be-at "format-node" "start")
717 "SPC SPC SPC SPC"
718 (edebug-tests-should-be-at "format-node" "vbefore")
719 "i" (edebug-tests-should-be-at "format-vector-node" "start")
720 "g" (should (equal edebug-tests-@-result "[b]")))))
722 (ert-deftest edebug-tests-error-stepping-into-subr ()
723 "Edebug refuses to step into a C function."
724 (edebug-tests-with-normal-env
725 (edebug-tests-setup-@ "format-node" '([b]) t)
726 (let* ((debug-on-error nil)
727 (edebug-on-error nil)
728 error-message
729 (command-error-function (lambda (&rest args)
730 (setq error-message (cl-cadar args)))))
731 (edebug-tests-run-kbd-macro
732 "@" (edebug-tests-should-be-at "format-node" "start")
733 "SPC" (edebug-tests-should-be-at "format-node" "vectorp")
734 "i" (should (string-match-p "vectorp is a built-in function"
735 error-message))
736 ;; The error stopped the keyboard macro. Start it again.
737 (should-not executing-kbd-macro)
738 (setq executing-kbd-macro t)
739 "g" (should (equal edebug-tests-@-result "[b]"))))))
741 (ert-deftest edebug-tests-step-into-macro-error ()
742 "Edebug gives an error on trying to step into a macro (Bug#26847)."
743 :expected-result :failed
744 (ert-fail "Forcing failure because letting this test run aborts the others.")
745 (edebug-tests-with-normal-env
746 (edebug-tests-setup-@ "try-flavors" nil t)
747 (let* ((debug-on-error nil)
748 (edebug-on-error nil)
749 (error-message "")
750 (command-error-function (lambda (&rest args)
751 (setq error-message (cl-cadar args)))))
752 (edebug-tests-run-kbd-macro
753 "@ SPC SPC SPC SPC SPC"
754 (edebug-tests-should-be-at "try-flavors" "macro")
755 "i" (should (string-match-p "edebug-test-code-try-flavors is a macro"
756 error-message))
757 ;; The error stopped the keyboard macro. Start it again.
758 (should-not executing-kbd-macro)
759 (setq executing-kbd-macro t)
760 "g" (should (equal edebug-tests-@-result
761 '("chocolate" "strawberry")))))))
763 (ert-deftest edebug-tests-step-into-generic-method ()
764 "Edebug can step into a generic method (Bug#22294)."
765 (edebug-tests-with-normal-env
766 (edebug-tests-setup-@ "use-methods" nil t)
767 (edebug-tests-run-kbd-macro
768 "@ SPC" (edebug-tests-should-be-at "use-methods" "number")
769 "i" (edebug-tests-should-be-at "emphasize-1" "start")
770 "gg" (should (equal edebug-tests-@-result
771 '("The number is not 101 or 99, but 100!"
772 "***yes***"))))))
774 (ert-deftest edebug-tests-break-in-lambda-out-of-defining-context ()
775 "Edebug observes a breakpoint in a lambda executed out of defining context."
776 (edebug-tests-with-normal-env
777 (edebug-tests-locate-def "make-lambda")
778 (eval-defun t)
779 (goto-char (edebug-tests-get-stop-point "make-lambda" "x"))
780 (edebug-set-breakpoint t)
781 (edebug-tests-setup-@ "use-lambda" nil t)
782 (edebug-tests-run-kbd-macro
783 "@g" (edebug-tests-should-be-at "make-lambda" "x")
784 (edebug-tests-should-match-result-in-messages "1 (#o1, #x1, ?\\C-a)")
785 "g" (should (equal edebug-tests-@-result '(11 12 13))))))
787 (ert-deftest edebug-tests-respects-initial-mode ()
788 "Edebug can stop first at breakpoint instead of first instrumented function."
789 (edebug-tests-with-normal-env
790 (edebug-tests-setup-@ "fac" '(4) t)
791 (goto-char (edebug-tests-get-stop-point "fac" "mult"))
792 (edebug-set-breakpoint t)
793 (setq edebug-initial-mode 'go)
794 (edebug-tests-run-kbd-macro
795 "@" (edebug-tests-should-be-at "fac" "mult")
796 (edebug-tests-should-match-result-in-messages "1 (#o1, #x1, ?\\C-a)")
797 "G" (should (equal edebug-tests-@-result 24)))))
799 (ert-deftest edebug-tests-step-through-non-definition ()
800 "Edebug can step through a non-defining form."
801 (edebug-tests-with-normal-env
802 (goto-char (edebug-tests-get-stop-point "try-flavors" "end-unless"))
803 (edebug-tests-run-kbd-macro
804 "C-u C-M-x"
805 "SPC SPC" (edebug-tests-should-be-at "try-flavors" "nutty")
806 (edebug-tests-should-match-result-in-messages "nil")
807 "SPC" (edebug-tests-should-be-at "try-flavors" "setq")
808 "f" (edebug-tests-should-be-at "try-flavors" "end-setq")
809 (edebug-tests-should-match-result-in-messages "\"chocolate\"")
810 "g")))
812 (ert-deftest edebug-tests-conditional-breakpoints-can-use-lexical-variables ()
813 "Edebug can set a conditional breakpoint using a lexical variable. Bug#12685"
814 (edebug-tests-with-normal-env
815 (should lexical-binding)
816 (edebug-tests-setup-@ "fac" '(5) t)
817 (edebug-tests-run-kbd-macro
818 "@" (edebug-tests-should-be-at "fac" "start")
819 ;; Set conditional breakpoint at end of next line.
820 "C-n C-e x (eql SPC n SPC 3) RET"
821 "g" (edebug-tests-should-be-at "fac" "mult")
822 (edebug-tests-should-match-result-in-messages
823 "6 (#o6, #x6, ?\\C-f)"))))
825 (ert-deftest edebug-tests-writable-buffer-state-is-preserved ()
826 "On Edebug exit writable buffers are still writable (Bug#14144)."
827 (edebug-tests-with-normal-env
828 (edebug-tests-setup-@ "choices" '(0) t)
829 (read-only-mode -1)
830 (edebug-tests-run-kbd-macro
831 "@g" (should (equal edebug-tests-@-result "zero")))
832 (barf-if-buffer-read-only)))
834 (ert-deftest edebug-tests-list-containing-empty-string-result-printing ()
835 "Edebug correctly prints a list containing only an empty string (Bug#17934)."
836 (edebug-tests-with-normal-env
837 (edebug-tests-setup-@ "empty-string-list" nil t)
838 (edebug-tests-run-kbd-macro
839 "@ SPC" (edebug-tests-should-be-at
840 "empty-string-list" "step")
841 (edebug-tests-should-match-result-in-messages "(\"\")")
842 "g")))
844 (ert-deftest edebug-tests-evaluation-of-current-buffer-bug-19611 ()
845 "Edebug can evaluate `current-buffer' in correct context. (Bug#19611)."
846 (edebug-tests-with-normal-env
847 (edebug-tests-setup-@ "current-buffer" nil t)
848 (edebug-tests-run-kbd-macro
849 "@" (edebug-tests-should-be-at
850 "current-buffer" "start")
851 "SPC SPC SPC" (edebug-tests-should-be-at
852 "current-buffer" "body")
853 "e (current-buffer) RET"
854 ;; Edebug just prints the result without "Result:"
855 (should (string-match-p
856 (regexp-quote "*edebug-test-code-buffer*")
857 edebug-tests-messages))
858 "g" (should (equal edebug-tests-@-result
859 "current-buffer: *edebug-test-code-buffer*")))))
861 (ert-deftest edebug-tests-trivial-backquote ()
862 "Edebug can instrument a trivial backquote expression (Bug#23651)."
863 (edebug-tests-with-normal-env
864 (read-only-mode -1)
865 (delete-region (point-min) (point-max))
866 (insert "`1")
867 (read-only-mode)
868 (edebug-eval-defun nil)
869 (should (string-match-p (regexp-quote "1 (#o1, #x1, ?\\C-a)")
870 edebug-tests-messages))
871 (setq edebug-tests-messages "")
873 (setq edebug-initial-mode 'go)
874 ;; In Bug#23651 Edebug would hang reading `1.
875 (edebug-eval-defun t)))
877 (ert-deftest edebug-tests-trivial-comma ()
878 "Edebug can read a trivial comma expression (Bug#23651)."
879 (edebug-tests-with-normal-env
880 (read-only-mode -1)
881 (delete-region (point-min) (point-max))
882 (insert ",1")
883 (read-only-mode)
884 (should-error (edebug-eval-defun t))))
886 (ert-deftest edebug-tests-circular-read-syntax ()
887 "Edebug can instrument code using circular read object syntax (Bug#23660)."
888 (edebug-tests-with-normal-env
889 (edebug-tests-setup-@ "circular-read-syntax" nil t)
890 (edebug-tests-run-kbd-macro
891 "@" (should (eql (car edebug-tests-@-result)
892 (cdr edebug-tests-@-result))))))
894 (ert-deftest edebug-tests-hash-read-syntax ()
895 "Edebug can instrument code which uses # read syntax (Bug#25068)."
896 (edebug-tests-with-normal-env
897 (edebug-tests-setup-@ "hash-read-syntax" nil t)
898 (edebug-tests-run-kbd-macro
899 "@g" (should (equal edebug-tests-@-result
900 '(#("abcd" 1 3 (face italic)) 511))))))
902 (ert-deftest edebug-tests-dotted-forms ()
903 "Edebug can instrument code matching the tail of a dotted spec (Bug#6415)."
904 (edebug-tests-with-normal-env
905 (edebug-tests-setup-@ "use-destructuring-bind" nil t)
906 (edebug-tests-run-kbd-macro
907 "@ SPC SPC SPC SPC SPC SPC"
908 (edebug-tests-should-be-at "use-destructuring-bind" "x")
909 (edebug-tests-should-match-result-in-messages "2 (#o2, #x2, ?\\C-b)")
910 "SPC"
911 (edebug-tests-should-be-at "use-destructuring-bind" "y")
912 (edebug-tests-should-match-result-in-messages "3 (#o3, #x3, ?\\C-c)")
914 (should (equal edebug-tests-@-result 5)))))
916 (provide 'edebug-tests)
917 ;;; edebug-tests.el ends here