summaryrefslogtreecommitdiff
path: root/test/lisp/emacs-lisp/backtrace-tests.el
blob: 75da468494b4364965b0db1089b1b8de06c8ba57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
;;; backtrace-tests.el --- Tests for emacs-lisp/backtrace.el  -*- lexical-binding: t; -*-

;; Copyright (C) 2018 Free Software Foundation, Inc.

;; Author: Gemini Lasswell

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'backtrace)
(require 'ert)
(require 'seq)

;; Create a backtrace frames list with several frames.
;; TODO load this from an el file in backtrace-resources/ so the tests
;; can be byte-compiled.
(defvar backtrace-tests--frames nil)

(defun backtrace-tests--func1 (arg1 arg2)
  (setq backtrace-tests--frames (backtrace-get-frames nil))
  (list arg1 arg2))

(defun backtrace-tests--func2 (arg)
  (list arg))

(defun backtrace-tests--func3 (arg)
  (let ((foo (list 'a arg 'b)))
    (list foo (backtrace-tests--func2 arg) (backtrace-tests--func1 arg 0))))

(defun backtrace-tests--create-backtrace-frames ()
  (backtrace-tests--func3 "string")
  ;; Discard frames before this one.
  (let (this-index)
    (dotimes (index (length backtrace-tests--frames))
      (when (eq (backtrace-frame-fun (nth index backtrace-tests--frames))
                'backtrace-tests--create-backtrace-frames)
        (setq this-index index)))
    (setq backtrace-tests--frames (seq-subseq backtrace-tests--frames
                                              0 (1+ this-index)))))

(backtrace-tests--create-backtrace-frames)

;; TODO check that debugger-batch-max-lines still works

(defun backtrace-tests--insert-header ()
  (insert "Test header\n"))

(defmacro backtrace-tests--with-buffer (&rest body)
  `(with-temp-buffer
     (backtrace-mode)
     (setq backtrace-frames backtrace-tests--frames)
     (setq backtrace-insert-header-function #'backtrace-tests--insert-header)
     (backtrace-print)
     ,@body))

;;; Tests
(ert-deftest backtrace-tests--to-string ()
  (should (string= (backtrace-to-string backtrace-tests--frames)
                    "  backtrace-get-frames(nil)
  (setq backtrace-tests--frames (backtrace-get-frames nil))
  backtrace-tests--func1(\"string\" 0)
  (list foo (backtrace-tests--func2 arg) (backtrace-tests--func1 arg 0))
  (let ((foo (list 'a arg 'b))) (list foo (backtrace-tests--func2 arg) (backtrace-tests--func1 arg 0)))
  backtrace-tests--func3(\"string\")
  backtrace-tests--create-backtrace-frames()
")))

(provide 'backtrace-tests)

;; These tests expect to see non-byte compiled stack frames.
;; Local Variables:
;; no-byte-compile: t
;; End:

;;; backtrace-tests.el ends here