| 1 |
;;; ert-buffer.el --- Support functions for running ert tests on buffers -*- lexical-binding: t -*- |
| 2 |
|
| 3 |
;; Copyright (C) 2010-2012 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
;; Author: Stefan Merten <[email protected]>, |
| 6 |
|
| 7 |
;; This file is part of GNU Emacs. |
| 8 |
|
| 9 |
;; GNU Emacs is free software: you can redistribute it and/or modify |
| 10 |
;; it under the terms of the GNU General Public License as published by |
| 11 |
;; the Free Software Foundation, either version 3 of the License, or |
| 12 |
;; (at your option) any later version. |
| 13 |
|
| 14 |
;; GNU Emacs is distributed in the hope that it will be useful, |
| 15 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
;; GNU General Public License for more details. |
| 18 |
|
| 19 |
;; You should have received a copy of the GNU General Public License |
| 20 |
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
;;; Commentary: |
| 23 |
;; |
| 24 |
;; Some functions need a buffer to run on. They may use the buffer content as |
| 25 |
;; well as point and mark as input and may modify all of them. In addition |
| 26 |
;; they may return some result. Here are some support functions to test such |
| 27 |
;; functions using `ert'. |
| 28 |
;; |
| 29 |
;; Use `ert-equal-buffer' and/or `ert-equal-buffer-return' for your `should' |
| 30 |
;; forms. |
| 31 |
;; |
| 32 |
;; You may use the constants `ert-Buf-point-char' and `ert-Buf-mark-char' in |
| 33 |
;; constructing comparison strings to represent point or mark, respectively. |
| 34 |
;; |
| 35 |
;; Examples: |
| 36 |
;; |
| 37 |
;; (should (ert-equal-buffer '(insert "foo") |
| 38 |
;; ; Insertion of "foo"... |
| 39 |
;; (concat ert-Buf-point-char ert-Buf-mark-char) |
| 40 |
;; ; ...into an empty buffer with point and mark... |
| 41 |
;; (concat ert-Buf-mark-char "foo" |
| 42 |
;; ert-Buf-point-char))) |
| 43 |
;; ; ...should result in a buffer containing "foo" |
| 44 |
;; ; with point and mark moved appropriately. |
| 45 |
;; |
| 46 |
;; (should (ert-equal-buffer '(delete-region) |
| 47 |
;; ; Deleting region... |
| 48 |
;; `(,ert-Buf-mark-char "foo" ,ert-Buf-point-char) |
| 49 |
;; ; ...in a region spanning the whole buffer... |
| 50 |
;; (concat ert-Buf-point-char ert-Buf-mark-char) |
| 51 |
;; ; ...should result in an empty buffer... |
| 52 |
;; t)) |
| 53 |
;; ; ...when called interactively. |
| 54 |
;; |
| 55 |
;; (should (ert-equal-buffer-return '(point) |
| 56 |
;; ; Returning the point... |
| 57 |
;; ert-Buf-point-char |
| 58 |
;; ; ...in an empty buffer... |
| 59 |
;; t |
| 60 |
;; ; ...without changing the result buffer... |
| 61 |
;; 1)) |
| 62 |
;; ; ...should return 1. |
| 63 |
|
| 64 |
;;; Code: |
| 65 |
|
| 66 |
(eval-when-compile |
| 67 |
(require 'cl)) |
| 68 |
(require 'ert) |
| 69 |
|
| 70 |
;; **************************************************************************** |
| 71 |
;; `ert-Buf' and related functions |
| 72 |
|
| 73 |
(defconst ert-Buf-point-char "\^@" |
| 74 |
"Special character used to mark the position of point in a `ert-Buf'.") |
| 75 |
|
| 76 |
(defconst ert-Buf-mark-char "\^?" |
| 77 |
"Special character used to mark the position of mark in a `ert-Buf'.") |
| 78 |
|
| 79 |
(defstruct (ert-Buf |
| 80 |
(:constructor nil) ; No default constructor. |
| 81 |
(:constructor ert-Buf-from-string |
| 82 |
(string |
| 83 |
&aux |
| 84 |
(analysis (ert-Buf--parse-string string)) |
| 85 |
(content (car analysis)) |
| 86 |
(point (cadr analysis)) |
| 87 |
(mark (caddr analysis)))) |
| 88 |
(:constructor ert-Buf-from-buffer |
| 89 |
(&aux |
| 90 |
(content (buffer-substring-no-properties |
| 91 |
(point-min) (point-max))) |
| 92 |
(point (point)) |
| 93 |
(mark (mark t)) |
| 94 |
(string |
| 95 |
(ert-Buf--create-string content point mark))))) |
| 96 |
"Structure to hold comparable information about a buffer. |
| 97 |
`ert-Buf-from-string' constructs a structure from a given STRING. |
| 98 |
`ert-Buf-from-buffer' constructs a structure from the current |
| 99 |
buffer." |
| 100 |
(content nil :read-only t) ; Pure string content without any special markup. |
| 101 |
(point nil :read-only t) ; Position of point. |
| 102 |
(mark nil :read-only t) ; Position of mark. |
| 103 |
(string nil :read-only t) ; String representation. |
| 104 |
) |
| 105 |
|
| 106 |
(defun ert-Buf--parse-string (string) |
| 107 |
"Parse STRING and return clean results. |
| 108 |
Return a list consisting of the cleaned content, the position of |
| 109 |
point if `ert-Buf-point-char' was found and the the position of mark |
| 110 |
if `ert-Buf-mark-char' was found." |
| 111 |
(with-temp-buffer |
| 112 |
(let ((case-fold-search nil) |
| 113 |
fnd point-fnd mark-fnd) |
| 114 |
(insert string) |
| 115 |
(goto-char (point-min)) |
| 116 |
(while (re-search-forward |
| 117 |
(concat "[" ert-Buf-point-char ert-Buf-mark-char "]") nil t) |
| 118 |
(setq fnd (match-string 0)) |
| 119 |
(replace-match "") |
| 120 |
(cond |
| 121 |
((equal fnd ert-Buf-point-char) |
| 122 |
(if point-fnd |
| 123 |
(error "Duplicate point")) |
| 124 |
(setq point-fnd (point))) |
| 125 |
((equal fnd ert-Buf-mark-char) |
| 126 |
(if mark-fnd |
| 127 |
(error "Duplicate mark")) |
| 128 |
(setq mark-fnd (point))) |
| 129 |
(t |
| 130 |
(error "Unexpected marker found")))) |
| 131 |
(list (buffer-substring-no-properties (point-min) (point-max)) |
| 132 |
point-fnd mark-fnd)))) |
| 133 |
|
| 134 |
(defun ert-Buf--create-string (content point mark) |
| 135 |
"Create a string representation from CONTENT, POINT and MARK." |
| 136 |
(with-temp-buffer |
| 137 |
(insert content) |
| 138 |
(let (pnt-chs) |
| 139 |
(if point |
| 140 |
(setq pnt-chs (nconc pnt-chs (list (cons point ert-Buf-point-char))))) |
| 141 |
(if mark |
| 142 |
(setq pnt-chs (nconc pnt-chs (list (cons mark ert-Buf-mark-char))))) |
| 143 |
;; Sort pairs so the highest position is last. |
| 144 |
(setq pnt-chs (sort pnt-chs (lambda (el1 el2) (> (car el1) (car el2))))) |
| 145 |
(while pnt-chs |
| 146 |
(goto-char (caar pnt-chs)) |
| 147 |
(insert (cdar pnt-chs)) |
| 148 |
(setq pnt-chs (cdr pnt-chs))) |
| 149 |
(buffer-substring-no-properties (point-min) (point-max))))) |
| 150 |
|
| 151 |
(defun ert-Buf--to-buffer (buf) |
| 152 |
"Set current buffer according to BUF." |
| 153 |
(insert (ert-Buf-content buf)) |
| 154 |
(if (ert-Buf-point buf) |
| 155 |
(goto-char (ert-Buf-point buf))) |
| 156 |
(if (ert-Buf-mark buf) |
| 157 |
(set-mark (ert-Buf-mark buf)))) |
| 158 |
|
| 159 |
(defun ert-Buf--from-argument (arg other) |
| 160 |
"Interpret ARG as input for an `ert-Buf', convert it and return the `ert-Buf'. |
| 161 |
ARG may be one of the types described in |
| 162 |
`ert-equal-buffer-return' or nil which is also returned." |
| 163 |
(cond |
| 164 |
((not arg) |
| 165 |
nil) |
| 166 |
((eq arg t) |
| 167 |
(when (or (not other) (eq other t)) |
| 168 |
(error "First argument to `ert-Buf--from-argument' t requires a non-nil, non-t second argument")) |
| 169 |
(ert-Buf--from-argument other nil)) |
| 170 |
((characterp arg) |
| 171 |
(ert-Buf-from-string (char-to-string arg))) |
| 172 |
((stringp arg) |
| 173 |
(ert-Buf-from-string arg)) |
| 174 |
((ert-Buf-p arg) |
| 175 |
arg) |
| 176 |
((listp arg) |
| 177 |
(ert-Buf-from-string (apply 'concat arg))) |
| 178 |
(t |
| 179 |
(error "Unknown type for `ert-Buf--from-argument'")))) |
| 180 |
|
| 181 |
;; **************************************************************************** |
| 182 |
;; Runners |
| 183 |
|
| 184 |
(defvar ert--inputs nil |
| 185 |
"Variable to hold the strings to give successively to `ert-completing-read'.") |
| 186 |
|
| 187 |
(defadvice completing-read (around ert-completing-read first |
| 188 |
(prompt collection &optional predicate |
| 189 |
require-match initial-input hist |
| 190 |
def inherit-input-method)) |
| 191 |
"Advice `completing-read' to accept input from `ert--inputs'." |
| 192 |
(if (not ert--inputs) |
| 193 |
(error "No more input strings in `ert--inputs'")) |
| 194 |
(let* ((input (pop ert--inputs))) |
| 195 |
(setq ad-return-value |
| 196 |
(cond |
| 197 |
((eq (try-completion input collection predicate) t) ;; Perfect match. |
| 198 |
input) |
| 199 |
((not require-match) ;; Non-matching input allowed. |
| 200 |
input) |
| 201 |
((and (equal input "") |
| 202 |
(eq require-match t)) ;; Empty input and this is allowed. |
| 203 |
input) |
| 204 |
(t |
| 205 |
(error |
| 206 |
"Input '%s' is not allowed for `completing-read' expecting %s" |
| 207 |
input collection)))))) |
| 208 |
|
| 209 |
(defadvice read-string (around ert-read-string first |
| 210 |
(prompt &optional initial-input history |
| 211 |
default-value inherit-input-method)) |
| 212 |
"Advice `read-string' to accept input from `ert--inputs'." |
| 213 |
(if (not ert--inputs) |
| 214 |
(error "No more input strings in `ert--inputs'")) |
| 215 |
(let* ((input (pop ert--inputs))) |
| 216 |
(setq ad-return-value |
| 217 |
(if (and (equal input "") default-value) |
| 218 |
default-value |
| 219 |
input)))) |
| 220 |
|
| 221 |
(defadvice read-number (around ert-read-number first |
| 222 |
(prompt &optional default)) |
| 223 |
"Advice `read-number' to accept input from `ert--inputs'." |
| 224 |
(if (not ert--inputs) |
| 225 |
(error "No more input strings in `ert--inputs'")) |
| 226 |
(let* ((input (pop ert--inputs))) |
| 227 |
(setq ad-return-value |
| 228 |
(if (and (equal input "") default) |
| 229 |
default |
| 230 |
input)))) |
| 231 |
|
| 232 |
(defun ert--run-test-with-buffer (buf form interactive) |
| 233 |
"With a buffer filled with `ert-Buf' BUF evaluate function form FORM. |
| 234 |
Return a cons consisting of the return value and a `ert-Buf'. If |
| 235 |
INTERACTIVE is non-nil FORM is evaluated in an interactive |
| 236 |
environment." |
| 237 |
(with-temp-buffer |
| 238 |
(ert-Buf--to-buffer buf) |
| 239 |
(let ((act-return |
| 240 |
(cond |
| 241 |
((not interactive) |
| 242 |
(eval form)) |
| 243 |
((eq interactive t) |
| 244 |
(let ((current-prefix-arg (cadr form))) |
| 245 |
(call-interactively (car form)))) |
| 246 |
((listp interactive) |
| 247 |
(setq ert--inputs interactive) |
| 248 |
(ad-activate 'read-string) |
| 249 |
(ad-activate 'read-number) |
| 250 |
(ad-activate 'completing-read) |
| 251 |
(unwind-protect |
| 252 |
(let ((current-prefix-arg (cadr form))) |
| 253 |
(call-interactively (car form))) |
| 254 |
(progn |
| 255 |
(ad-deactivate 'completing-read) |
| 256 |
(ad-deactivate 'read-number) |
| 257 |
(ad-deactivate 'read-string))) |
| 258 |
(if ert--inputs |
| 259 |
(error "%d input strings left over" |
| 260 |
(length ert--inputs)))))) |
| 261 |
(act-buf (ert-Buf-from-buffer))) |
| 262 |
(cons act-return act-buf)))) |
| 263 |
|
| 264 |
(defun ert--compare-test-with-buffer (result buf ignore-return exp-return) |
| 265 |
"Compare RESULT of test with expected buffer BUF. |
| 266 |
RESULT is a return value from `ert--run-test-with-buffer'. |
| 267 |
Return a list of booleans where t stands for a successful test of |
| 268 |
this kind: |
| 269 |
|
| 270 |
* Content of output buffer |
| 271 |
* Point in output buffer |
| 272 |
* Return value |
| 273 |
|
| 274 |
IGNORE-RETURN, EXP-RETURN are described in `ert--equal-buffer'." |
| 275 |
(let ((act-return (car result)) |
| 276 |
(act-buf (cdr result))) |
| 277 |
(list |
| 278 |
(or (not buf) |
| 279 |
(equal (ert-Buf-content act-buf) (ert-Buf-content buf))) |
| 280 |
(or |
| 281 |
(not buf) |
| 282 |
(not (ert-Buf-point buf)) |
| 283 |
(equal (ert-Buf-point act-buf) (ert-Buf-point buf))) |
| 284 |
(or ignore-return |
| 285 |
(equal act-return exp-return))))) |
| 286 |
|
| 287 |
(defun ert--equal-buffer (form input exp-output ignore-return exp-return interactive) |
| 288 |
"Run tests for `ert-equal-buffer-return' and `ert-equal-buffer'. |
| 289 |
FORM, INPUT and EXP-OUTPUT are as described for |
| 290 |
`ert-equal-buffer-return'. Ignore return value if IGNORE-RETURN |
| 291 |
or compare the return value to EXP-RETURN. INTERACTIVE is as |
| 292 |
described for `ert-equal-buffer-return'. Return t if equal." |
| 293 |
(catch 'return |
| 294 |
(dolist (elem (ert--compare-test-with-buffer |
| 295 |
(ert--run-test-with-buffer |
| 296 |
(ert-Buf--from-argument input exp-output) form interactive) |
| 297 |
(ert-Buf--from-argument exp-output input) |
| 298 |
ignore-return exp-return) t) |
| 299 |
(unless elem |
| 300 |
(throw 'return nil))))) |
| 301 |
|
| 302 |
(defun ert-equal-buffer-return (form input exp-output exp-return &optional interactive) |
| 303 |
"Evaluate function form FORM with a buffer and compare results. |
| 304 |
The buffer is filled with INPUT. Compare the buffer content to |
| 305 |
EXP-OUTPUT if this is non-nil. Compare the return value to |
| 306 |
EXP-RETURN. Return t if buffer and return value are equal to the |
| 307 |
expected values. |
| 308 |
|
| 309 |
INPUT and EXP-OUTPUT represent the input buffer or the expected |
| 310 |
output buffer, respectively. They can be one of the following: |
| 311 |
|
| 312 |
* nil in which case the respective buffer is not used. Makes |
| 313 |
sense only for EXP-OUTPUT. |
| 314 |
* t in which case the other buffer is used unchanged. The other |
| 315 |
buffer must not be nil or t in this case. |
| 316 |
* A character which is converted to a one character string. |
| 317 |
* A string. |
| 318 |
* A list of strings which are concatenated using `concat'. This |
| 319 |
can be used to shorten the form describing the buffer when used |
| 320 |
with quote or backquote. |
| 321 |
* An `ert-Buf' object. |
| 322 |
|
| 323 |
All input variants which end up in a string are parsed by |
| 324 |
`ert-Buf-from-string'. |
| 325 |
|
| 326 |
If INTERACTIVE is nil FORM is evaluated with no special context. |
| 327 |
If INTERACTIVE is non-nil FORM is evaluated interactively and |
| 328 |
`current-prefix-arg' is set to the cadr of FORM (i.e\. the first |
| 329 |
argument in FORM) and thus must comply to the format of |
| 330 |
`current-prefix-arg'. If INTERACTIVE is t `call-interactively' |
| 331 |
is used normally. If INTERACTIVE is a list of strings the |
| 332 |
elements of the list are given to (advised forms of) functions |
| 333 |
reading from the minibuffer as user input strings. This allows |
| 334 |
simulating interactive user input. |
| 335 |
|
| 336 |
FORM usually needs to be quoted. |
| 337 |
|
| 338 |
Return t if buffer and return value equal the expected values." |
| 339 |
(ert--equal-buffer form input exp-output nil exp-return interactive)) |
| 340 |
|
| 341 |
(defun ert-equal-buffer (form input exp-output &optional interactive) |
| 342 |
"Like `ert-equal-buffer-return' but the return value of FORM is ignored. |
| 343 |
INPUT, EXP-OUTPUT and INTERACTIVE are described in |
| 344 |
`ert-equal-buffer-return'." |
| 345 |
(ert--equal-buffer form input exp-output t nil interactive)) |
| 346 |
|
| 347 |
;; **************************************************************************** |
| 348 |
;; Explainers |
| 349 |
|
| 350 |
(defun ert--equal-buffer-explain (form input exp-output ignore-return exp-return interactive) |
| 351 |
"Explain why `ert--equal-buffer' failed with these parameters. |
| 352 |
Return the explanation. FORM, INPUT, EXP-OUTPUT, |
| 353 |
IGNORE-RETURN, EXP-RETURN, INTERACTIVE are described in |
| 354 |
`ert--equal-buffer'." |
| 355 |
(let ((test-result (ert--run-test-with-buffer |
| 356 |
(ert-Buf--from-argument input exp-output) |
| 357 |
form interactive)) |
| 358 |
(exp-buf (ert-Buf--from-argument exp-output input))) |
| 359 |
(destructuring-bind (ok-string ok-point ok-return) |
| 360 |
(ert--compare-test-with-buffer |
| 361 |
test-result |
| 362 |
(ert-Buf--from-argument exp-output input) ignore-return exp-return) |
| 363 |
(let (result) |
| 364 |
(if (not ok-return) |
| 365 |
(push (list 'different-return-values |
| 366 |
(ert--explain-not-equal (car test-result) exp-return)) |
| 367 |
result)) |
| 368 |
(if (not ok-point) |
| 369 |
(push (list 'different-points |
| 370 |
(ert-Buf-string (cdr test-result)) |
| 371 |
(ert-Buf-string exp-buf)) |
| 372 |
result)) |
| 373 |
(if (not ok-string) |
| 374 |
(push (list 'different-buffer-contents |
| 375 |
(ert--explain-not-equal |
| 376 |
(ert-Buf-content (cdr test-result)) |
| 377 |
(ert-Buf-content exp-buf))) |
| 378 |
result)) |
| 379 |
result)))) |
| 380 |
|
| 381 |
(defun ert-equal-buffer-return-explain (form input exp-output exp-return &optional interactive) |
| 382 |
"Explain why `ert-equal-buffer-return' failed with these parameters. |
| 383 |
Return the explanation. FORM, INPUT, EXP-OUTPUT, EXP-RETURN, |
| 384 |
INTERACTIVE are described in `ert--equal-buffer'." |
| 385 |
(ert--equal-buffer-explain |
| 386 |
form input exp-output nil exp-return interactive)) |
| 387 |
|
| 388 |
(put 'ert-equal-buffer-return 'ert-explainer 'ert-equal-buffer-return-explain) |
| 389 |
|
| 390 |
(defun ert-equal-buffer-explain (form input exp-output &optional interactive) |
| 391 |
"Explain why `ert-equal-buffer' failed with these parameters. |
| 392 |
Return the explanation. FORM, INPUT, EXP-OUTPUT, EXP-RETURN, |
| 393 |
INTERACTIVE are described in `ert--equal-buffer'." |
| 394 |
(ert--equal-buffer-explain |
| 395 |
form input exp-output t nil interactive)) |
| 396 |
|
| 397 |
(put 'ert-equal-buffer 'ert-explainer 'ert-equal-buffer-explain) |
| 398 |
|
| 399 |
; LocalWords: foo minibuffer |
| 400 |
|
| 401 |
;; Local Variables: |
| 402 |
;; sentence-end-double-space: t |
| 403 |
;; End: |
| 404 |
|
| 405 |
(provide 'ert-buffer) |
| 406 |
|
| 407 |
;;; ert-buffer.el ends here |