unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 5450d211b1a85e2eed71d2723aaa1c451dc56c99 12844 bytes (raw)
name: test/lisp/speedbar-tests.el 	 # note: path name is non-authoritative(*)

  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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
 
;;; speedbar-tests.el --- Tests for speedbar.el  -*- lexical-binding: t -*-

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

;; 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/>.

;;; Commentary:

;;; Code:

(require 'ert)
(require 'eieio-base)
(require 'eieio-speedbar)

(defclass speedbar-tests-container (eieio-named eieio-speedbar-file-button)
  ((child-items :initarg :child-items
                :type list))
  "An expandable Speedbar item which can contain other items.")

(cl-defmethod eieio-speedbar-object-children ((item speedbar-tests-container))
  "Return the list of child items for ITEM."
  (slot-value item 'child-items))

(defclass speedbar-tests-item (eieio-named eieio-speedbar)
  nil
  "A Speedbar item which cannot contain other items.")

(defun speedbar-tests--make-object (item-spec)
  "Return an object representing a Speedbar item.

The object is constructed based on the specification ITEM-SPEC which
should be a cons pair of the form (NAME . CHILD-ITEMS).  NAME is a
string which will be used for display purposes.  CHILD-ITEMS is a list
of additional ITEM-SPEC values which will be referenced as children."
  (let ((name (car item-spec))
        (child-items (cdr item-spec)))
    (unless (stringp name)
      (error "Item name must be a string"))
    (unless (listp child-items)
      (error "Child-items must be a list"))
    (if child-items
        (speedbar-tests-container
         :object-name name
         :child-items (mapcar #'speedbar-tests--make-object
                              child-items))
      (speedbar-tests-item
       :object-name name))))

(defvar speedbar-tests--setup-strings nil
  "An alist of strings which represents a hierarchy of Speedbar items.")

(defvar speedbar-tests--object-hierarchy nil
  "The current object hierarchy for the Speedbar being tested.")

(defun speedbar-tests--base-items (_directory)
  "Return the list of top-level objects for the Speedbar."
  (setq speedbar-tests--object-hierarchy
        (mapcar #'speedbar-tests--make-object
                speedbar-tests--setup-strings)))

(eieio-speedbar-create #'eieio-speedbar-make-map
		       'eieio-speedbar-key-map
		       'eieio-speedbar-menu
		       "Test"
		       #'speedbar-tests--base-items)

(defun speedbar-tests--clean-up ()
  "Clean-up after Speedbar test."
  (when (framep speedbar-frame)
    (delete-frame speedbar-frame)))

(defun speedbar-tests--initialize ()
  "Initialize a Speedbar for testing."
  (speedbar-get-focus)
  (speedbar-change-initial-expansion-list "Test"))

(defun speedbar-tests--object-name-expanded (object)
  "Return the string name of OBJECT when it is expanded."
  (let ((name (eieio-speedbar-object-buttonname object)))
    (if (slot-value object 'expanded)
        (concat name "+")
      name)))

(defvar speedbar-tests--object-name-function
  #'eieio-speedbar-object-buttonname
  "The function which returns the string representation of an object.")

(defun speedbar-tests--objects-as-strings (object-list)
  "Return the object hierarchy OBJECT-LIST as an alist of strings.

The string used to represent the object is determined by the function
bound to `speedbar-tests--object-name-function' is a function, which
should accept the object as the only argument and return a string to use
as the name."
  (mapcar (lambda (object)
            (let ((name (funcall speedbar-tests--object-name-function
                                 object))
                  (child-items (eieio-speedbar-object-children
                                object)))
              (cons name (speedbar-tests--objects-as-strings
                          child-items))))
          object-list))

(cl-defmacro speedbar-tests--state-test
    ((&optional &key setup final name-function) &rest body)
  "Evaluate BODY and verify the Speedbar is in an expected state.

`:setup' specifies an alist of strings which will be used to create an
object hierarchy used for the Speedbar display.

`:final' specifies an alist of strings which should represent the final
Speedbar state once BODY has been evaluated and the object hierarchy has
been converted back to an alist of strings.  `:name-function' specifies
the function to use to generate a string from an object, which should
accept the object as an argument and return a string which represents
the object as well as its state."
  (declare (indent 1))
  (let ((let-vars `((speedbar-tests--setup-strings ',setup))))
    (when name-function
      (push `(speedbar-tests--object-name-function #',name-function)
            let-vars))
    `(unwind-protect
         (let ,let-vars
           (speedbar-tests--initialize)
           (should (equal (speedbar-tests--objects-as-strings
                           speedbar-tests--object-hierarchy)
                          ',setup))
           ,@body
           (should (equal (speedbar-tests--objects-as-strings
                           speedbar-tests--object-hierarchy)
                          ',final)))
       (speedbar-tests--clean-up))))

(ert-deftest speedbar-tests--expand-descendants-single ()
  "Expand the first item."
  (skip-when noninteractive)
  (speedbar-tests--state-test
      ( :setup (("A"  . (("A1"))))
        :final (("A+" . (("A1"))))
        :name-function speedbar-tests--object-name-expanded)
    (with-current-buffer speedbar-buffer
      (goto-char (point-min))
      (should (string-equal "A" (speedbar-line-text)))
      (speedbar-expand-line-descendants 'nocache))))

(ert-deftest speedbar-tests--expand-descendants-nested ()
  "Expand the first item and its only child."
  (skip-when noninteractive)
  (speedbar-tests--state-test
      ( :setup (("A"  . (("A1"  . (("A1A"))))))
        :final (("A+" . (("A1+" . (("A1A"))))))
        :name-function speedbar-tests--object-name-expanded)
    (with-current-buffer speedbar-buffer
      (goto-char (point-min))
      (should (string-equal "A" (speedbar-line-text)))
      (speedbar-expand-line-descendants 'nocache))))

(ert-deftest speedbar-tests--expand-descendants-nested-wide ()
  "Expand all descendants of first item which has multiple children."
  (skip-when noninteractive)
  (speedbar-tests--state-test
      ( :setup (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A"))))))
        :final (("A+" . (("A1+" . (("A1A")))
                         ("A2+" . (("A2A"))))))
        :name-function speedbar-tests--object-name-expanded)
    (with-current-buffer speedbar-buffer
      (goto-char (point-min))
      (should (string-equal "A" (speedbar-line-text)))
      (speedbar-expand-line-descendants 'nocache))))

(ert-deftest speedbar-tests--expand-descendants-of-first ()
  "Expand the first item and all descendants."
  (skip-when noninteractive)
  (speedbar-tests--state-test
      ( :setup (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A")))))
                ("B"  . (("B1"  . (("B1B")))
                         ("B2"  . (("B2B"))))))
        :final (("A+" . (("A1+" . (("A1A")))
                         ("A2+" . (("A2A")))))
                ("B"  . (("B1"  . (("B1B")))
                         ("B2"  . (("B2B"))))))
        :name-function speedbar-tests--object-name-expanded)
    (with-current-buffer speedbar-buffer
      (goto-char (point-min))
      (should (string-equal "A" (speedbar-line-text)))
      (speedbar-expand-line-descendants 'nocache))))

(ert-deftest speedbar-tests--expand-descendants-of-first-expanded ()
  "Expand the already expanded first item and all descendants."
  (skip-when noninteractive)
  (speedbar-tests--state-test
      ( :setup (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A")))))
                ("B"  . (("B1"  . (("B1B")))
                         ("B2"  . (("B2B"))))))
        :final (("A+" . (("A1+" . (("A1A")))
                         ("A2+" . (("A2A")))))
                ("B"  . (("B1"  . (("B1B")))
                         ("B2"  . (("B2B"))))))
        :name-function speedbar-tests--object-name-expanded)
    (with-current-buffer speedbar-buffer
      (goto-char (point-min))
      (should (string-equal "A" (speedbar-line-text)))
      (speedbar-expand-line 'nocache)
      (speedbar-expand-line-descendants 'nocache))))

(ert-deftest speedbar-tests--expand-descendants-of-last ()
  "Expand the last item and all descendants."
  (skip-when noninteractive)
  (speedbar-tests--state-test
      ( :setup (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A")))))
                ("B"  . (("B1"  . (("B1B")))
                         ("B2"  . (("B2B"))))))
        :final (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A")))))
                ("B+" . (("B1+" . (("B1B")))
                         ("B2+" . (("B2B"))))))
        :name-function speedbar-tests--object-name-expanded)
    (with-current-buffer speedbar-buffer
      (goto-char (point-min))
      (forward-line)
      (should (string-equal "B" (speedbar-line-text)))
      (speedbar-expand-line-descendants 'nocache))))

(ert-deftest speedbar-tests--expand-descendants-of-last-expanded ()
  "Expand the already expanded last item and all descendants."
  (skip-when noninteractive)
  (speedbar-tests--state-test
      ( :setup (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A")))))
                ("B"  . (("B1"  . (("B1B")))
                         ("B2"  . (("B2B"))))))
        :final (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A")))))
                ("B+" . (("B1+" . (("B1B")))
                         ("B2+" . (("B2B"))))))
        :name-function speedbar-tests--object-name-expanded)
    (with-current-buffer speedbar-buffer
      (goto-char (point-min))
      (save-excursion
        (forward-line)
        (should (string-equal "B" (speedbar-line-text)))
        (speedbar-expand-line 'nocache))
      (save-excursion
        (forward-line)
        (should (string-equal "B" (speedbar-line-text)))
        (speedbar-expand-line-descendants 'nocache)))))

(ert-deftest speedbar-tests--expand-descendants-of-middle ()
  "Expand the middle item and all descendants."
  (skip-when noninteractive)
  (speedbar-tests--state-test
      ( :setup (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A")))))
                ("B"  . (("B1"  . (("B1B")))
                         ("B2"  . (("B2B")))))
                ("C"  . (("C1"  . (("C1C")))
                         ("C2"  . (("C2C"))))))
        :final (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A")))))
                ("B+" . (("B1+" . (("B1B")))
                         ("B2+" . (("B2B")))))
                ("C"  . (("C1"  . (("C1C")))
                         ("C2"  . (("C2C"))))))
        :name-function speedbar-tests--object-name-expanded)
    (with-current-buffer speedbar-buffer
      (goto-char (point-min))
      (goto-char (point-min))
      (forward-line)
      (should (string-equal "B" (speedbar-line-text)))
      (speedbar-expand-line-descendants 'nocache))))

(ert-deftest speedbar-tests--expand-descendants-of-middle-expanded ()
  "Expand the already expanded middle item and all descendants."
  (skip-when noninteractive)
  (speedbar-tests--state-test
      ( :setup (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A")))))
                ("B"  . (("B1"  . (("B1B")))
                         ("B2"  . (("B2B")))))
                ("C"  . (("C1"  . (("C1C")))
                         ("C2"  . (("C2C"))))))
        :final (("A"  . (("A1"  . (("A1A")))
                         ("A2"  . (("A2A")))))
                ("B+" . (("B1+" . (("B1B")))
                         ("B2+" . (("B2B")))))
                ("C"  . (("C1"  . (("C1C")))
                         ("C2"  . (("C2C"))))))
        :name-function speedbar-tests--object-name-expanded)
    (with-current-buffer speedbar-buffer
      (goto-char (point-min))
      (save-excursion
        (forward-line)
        (should (string-equal "B" (speedbar-line-text)))
        (speedbar-expand-line 'nocache))
      (save-excursion
        (forward-line)
        (should (string-equal "B" (speedbar-line-text)))
        (speedbar-expand-line-descendants 'nocache)))))

(provide 'speedbar-tests)
;;; speedbar-tests.el ends here

debug log:

solving 5450d211b1a ...
found 5450d211b1a in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).