Command for toggling GFM task list items
[markdown-mode.git] / tests / markdown-test.el
blob4bda697a6ca25cedd5be2f1c2c4ecebff0074e8f
1 ;;;; markdown-test.el --- Tests for markdown-mode
3 ;; Copyright (C) 2013-2015 Jason R. Blevins <jrblevin@sdf.org>
4 ;; Copyright (C) 2013 Makoto Motohashi <mkt.motohashi@gmail.com>
5 ;; Copyright (C) 2015 Google, Inc. (Contributor: Samuel Freilich <sfreilich@google.com>)
7 ;; This file is not part of GNU Emacs.
9 ;; This program 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 2, or (at your option)
12 ;; any later version.
14 ;; This program 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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program; if not, write to the Free Software
21 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
24 ;;; Commentary:
26 ;; This file contains the `markdown-mode' test suite. To run the tests:
28 ;; M-x load-file RET markdown-test.el RET
29 ;; M-x markdown-test RET
31 ;;; Code:
33 (require 'markdown-mode)
34 (require 'ert)
35 (require 'cl-lib)
37 (defconst markdown-test-dir
38 (expand-file-name (file-name-directory
39 (or load-file-name buffer-file-name))))
41 (defconst markdown-test-font-lock-function
42 (if (and noninteractive (fboundp 'font-lock-ensure))
43 #'font-lock-ensure #'font-lock-fontify-buffer))
45 (defmacro markdown-test-string-mode (mode string &rest body)
46 "Run BODY in a temporary buffer containing STRING in MODE."
47 (declare (indent 2))
48 `(let ((win (selected-window)))
49 (unwind-protect
50 (with-temp-buffer
51 (set-window-buffer win (current-buffer) t)
52 (erase-buffer)
53 (funcall ,mode)
54 (setq-default indent-tabs-mode nil)
55 (insert ,string)
56 (goto-char (point-min))
57 (funcall markdown-test-font-lock-function)
58 (prog1 ,@body (kill-buffer))))))
60 (defmacro markdown-test-file-mode (mode file &rest body)
61 "Open FILE from `markdown-test-dir' in MODE and execute BODY."
62 (declare (indent 2))
63 `(let ((fn (concat markdown-test-dir ,file)))
64 (save-window-excursion
65 (with-temp-buffer
66 (insert-file-contents fn)
67 (funcall ,mode)
68 (goto-char (point-min))
69 (funcall markdown-test-font-lock-function)
70 ,@body))))
72 (defmacro markdown-test-string (string &rest body)
73 "Run body in a temporary buffer containing STRING in `markdown-mode'."
74 (declare (indent 1))
75 `(markdown-test-string-mode 'markdown-mode ,string ,@body))
76 (def-edebug-spec markdown-test-string (form body))
78 (defmacro markdown-test-file (file &rest body)
79 "Open FILE from `markdown-test-dir' in `markdown-mode' and execute BODY."
80 (declare (indent 1))
81 `(markdown-test-file-mode 'markdown-mode ,file ,@body))
82 (def-edebug-spec markdown-test-file (form body))
84 (defmacro markdown-test-string-gfm (string &rest body)
85 "Run body in a temporary buffer containing STRING in `gfm-mode'."
86 (declare (indent 1))
87 `(markdown-test-string-mode 'gfm-mode ,string ,@body))
88 (def-edebug-spec markdown-test-string-gfm (form body))
90 (defmacro markdown-test-file-gfm (file &rest body)
91 "Open FILE from `markdown-test-dir' in `gfm-mode' and execute BODY."
92 (declare (indent 1))
93 `(markdown-test-file-mode 'gfm-mode ,file ,@body))
94 (def-edebug-spec markdown-test-file-gfm (form body))
96 (defmacro markdown-test-temp-file (file &rest body)
97 "Open FILE from `markdown-test-dir' visiting temp file and execute body.
98 This file is not saved."
99 (declare (indent 1))
100 `(let ((fn (concat markdown-test-dir ,file))
101 (tmp (make-temp-file "markdown-test" nil ".text"))
102 buf)
103 (save-window-excursion
104 (unwind-protect
105 (progn
106 (setq buf (find-file tmp))
107 (insert-file-contents fn)
108 (markdown-mode)
109 (goto-char (point-min))
110 (funcall markdown-test-font-lock-function)
111 ,@body
112 (set-buffer-modified-p nil))
113 (when (buffer-live-p buf) (kill-buffer buf))
114 (delete-file tmp)))))
115 (def-edebug-spec markdown-test-temp-file (form body))
117 (defun markdown-test-report-property-range (begin end prop)
118 "Report buffer substring and property PROP from BEGIN to END."
119 (message "Buffer substring: %s" (buffer-substring begin (1+ end)))
120 (message "Properties in range are as follows:")
121 (dolist (loc (number-sequence begin end))
122 (message "%d: %s" loc (get-char-property loc prop))))
124 (defun markdown-test-range-has-property (begin end prop value)
125 "Verify that range BEGIN to END has PROP equal to or containing VALUE."
126 (let (vals fail-loc)
127 (setq fail-loc
128 (catch 'fail
129 (dolist (loc (number-sequence begin end))
130 (setq vals (get-char-property loc prop))
131 (if (and vals (listp vals))
132 (unless (memq value vals)
133 (throw 'fail loc))
134 (unless (eq vals value)
135 (throw 'fail loc))))))
136 (when fail-loc
137 (message "Testing range (%d,%d) for property %s equal to %s."
138 begin end prop value)
139 (message "Expected value (%s) not found in property (%s) at location %d" value prop fail-loc)
140 (markdown-test-report-property-range begin end prop))
141 (should-not fail-loc)))
143 (defun markdown-test-range-property-equals (begin end prop value)
144 "Verify that range BEGIN to END has property PROP equal to VALUE."
145 (let ((fail-loc
146 (catch 'fail
147 (dolist (loc (number-sequence begin end))
148 (unless (eq (get-char-property loc prop) value)
149 (throw 'fail loc))))))
150 (when fail-loc
151 (message "Testing range (%d,%d) for property %s equal to %s."
152 begin end prop value)
153 (message "Expected value (%s) not found in property (%s) at location %d" value prop fail-loc)
154 (markdown-test-report-property-range begin end prop))
155 (should-not fail-loc)))
157 (defun markdown-test-range-has-face (begin end face)
158 "Verify that the range from BEGIN to END has face FACE."
159 (markdown-test-range-has-property begin end 'face face))
161 (defun markdown-test-range-face-equals (begin end face)
162 "Verify that the range from BEGIN to END has face equal to FACE."
163 (markdown-test-range-property-equals begin end 'face face))
165 (defun markdown-test-goto-heading (title)
166 "Move the point to section with TITLE."
167 (let ((regexp (format "\\(^#+ %s\\( #+\\)?\\|^%s\n[=-]+\n\\)" title title)))
168 (if (re-search-forward regexp nil t)
169 (goto-char (match-end 0)))))
171 (defun markdown-test ()
172 "Run all defined tests for `markdown-mode'."
173 (interactive)
174 (ert "markdown"))
176 ;;; Example tests:
178 (ert-deftest test-markdown-example/string ()
179 "An example string test using the `ert' framework."
180 (markdown-test-string "foo *bar* baz"
181 (goto-char 5)
182 (delete-char 1)
183 (should (looking-at "bar"))))
185 (ert-deftest test-markdown-example/file ()
186 "An example file test using the `ert' framework."
187 (markdown-test-file "inline.text"
188 (goto-char 9)
189 (should (looking-at "\*"))))
191 ;;; Basic mode tests:
193 (ert-deftest test-markdown-mode/variables ()
194 "Test `markdown-mode' variables."
195 (markdown-test-file "inline.text"
196 (should (= tab-width 4))
197 (should (eq font-lock-multiline t))
198 (should (eq major-mode 'markdown-mode))))
200 ;;; Element insertion tests:
202 (ert-deftest test-markdown-insertion/blank-line-before-1 ()
203 "Test function `markdown-ensure-blank-line-before' at beginning of line."
204 (markdown-test-file "syntax.text"
205 (search-forward "as plain text")
206 (should (= (point) 1556))
207 (beginning-of-line)
208 (should (= (point) 1505))
209 (should (looking-back "A Markdown-formatted\n" nil))
210 (should (not (markdown-prev-line-blank-p)))
211 (markdown-ensure-blank-line-before)
212 (should (looking-back "A Markdown-formatted\n\n" nil))
213 (should (markdown-prev-line-blank-p))))
215 (ert-deftest test-markdown-insertion/blank-line-before-2 ()
216 "Test function `markdown-ensure-blank-line-before' in middle of line."
217 (markdown-test-file "syntax.text"
218 (search-forward "as plain text")
219 (should (= (point) 1556))
220 (should (looking-back "as plain text" nil))
221 (should (not (markdown-prev-line-blank-p)))
222 (markdown-ensure-blank-line-before)
223 (should (looking-back "as plain text\n\n" nil))
224 (should (markdown-prev-line-blank-p))))
226 (ert-deftest test-markdown-insertion/blank-line-before-3 ()
227 "Test function `markdown-ensure-blank-line-before' with blank line before."
228 (markdown-test-file "syntax.text"
229 (search-forward "web.\n\nMarkdown is not a replacement for HTML")
230 (beginning-of-line)
231 (should (= (point) 2704))
232 (should (looking-back "web.\n\n" nil))
233 (should (markdown-prev-line-blank-p))
234 (markdown-ensure-blank-line-before)
235 (should (= (point) 2704))
236 (should (looking-back "web.\n\n" nil))
237 (should (markdown-prev-line-blank-p))))
239 (ert-deftest test-markdown-insertion/blank-line-before-4 ()
240 "Test function `markdown-ensure-blank-line-before' at beginning of buffer."
241 (markdown-test-string "first line"
242 (beginning-of-line)
243 (should (bobp))
244 (should (= (point-max) 11))
245 (markdown-ensure-blank-line-before)
246 (should (= (point-max) 11))
247 (should (string-equal (buffer-substring (point-min) (point-max))
248 "first line"))
249 (forward-word)
250 (markdown-ensure-blank-line-before)
251 (should (string-equal (buffer-substring (point-min) (point-max))
252 "first\n\n line"))))
254 (ert-deftest test-markdown-insertion/blank-line-after-1 ()
255 "Test function `markdown-ensure-blank-line-after' at end of line."
256 (markdown-test-file "syntax.text"
257 (search-forward "as plain text")
258 (should (= (point) 1556))
259 (end-of-line)
260 (should (= (point) 1573))
261 (should (looking-at "\nlike it's been"))
262 (should (not (markdown-next-line-blank-p)))
263 (markdown-ensure-blank-line-after)
264 (should (looking-at "\n\nlike it's been"))
265 (should (markdown-next-line-blank-p))))
267 (ert-deftest test-markdown-insertion/blank-line-after-2 ()
268 "Test function `markdown-ensure-blank-line-after' in middle of line."
269 (markdown-test-file "syntax.text"
270 (search-forward "as plain text")
271 (should (= (point) 1556))
272 (should (looking-at ", without looking"))
273 (should (not (markdown-next-line-blank-p)))
274 (markdown-ensure-blank-line-after)
275 (should (looking-at "\n\n, without looking"))
276 (should (markdown-next-line-blank-p))))
278 (ert-deftest test-markdown-insertion/blank-line-after-3 ()
279 "Test function `markdown-ensure-blank-line-after' with blank line after."
280 (markdown-test-file "syntax.text"
281 (search-forward "*writing* for the web.")
282 (should (= (point) 2702))
283 (should (looking-at "\n\nMarkdown is not a replacement for HTML"))
284 (should (markdown-next-line-blank-p))
285 (markdown-ensure-blank-line-after)
286 (should (= (point) 2702))
287 (should (looking-at "\n\nMarkdown is not a replacement for HTML"))
288 (should (markdown-next-line-blank-p))))
290 (ert-deftest test-markdown-insertion/blank-line-after-4 ()
291 "Test function `markdown-ensure-blank-line-after' at end of buffer."
292 (markdown-test-string "last line"
293 (end-of-line)
294 (should (eobp))
295 (should (= (point-max) 10))
296 (markdown-ensure-blank-line-after)
297 (should (= (point-max) 10))
298 (should (string-equal (buffer-substring (point-min) (point-max))
299 "last line"))
300 (backward-word)
301 (markdown-ensure-blank-line-after)
302 (should (string-equal (buffer-substring (point-min) (point-max))
303 "last \n\nline"))))
305 (ert-deftest test-markdown-insertion/point-after-unwrap ()
306 "Test new point position calculations after unwrap operations."
307 (markdown-test-string "line **one**\n"
308 (let ((prefix (cons 6 8)) (suffix (cons 11 13)))
309 ;; Prefix
310 (should (eq (markdown-point-after-unwrap 6 prefix suffix) 6))
311 (should (eq (markdown-point-after-unwrap 7 prefix suffix) 6))
312 ;; Word
313 (should (eq (markdown-point-after-unwrap 8 prefix suffix) 6))
314 (should (eq (markdown-point-after-unwrap 9 prefix suffix) 7))
315 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 8))
316 ;; Suffix
317 (should (eq (markdown-point-after-unwrap 11 prefix suffix) 9))
318 (should (eq (markdown-point-after-unwrap 12 prefix suffix) 9))
319 ;; Immediately after
320 (should (eq (markdown-point-after-unwrap 13 prefix suffix) 9))))
321 (markdown-test-string "line _one_\n"
322 (let ((prefix (cons 6 7)) (suffix (cons 10 11)))
323 ;; Prefix
324 (should (eq (markdown-point-after-unwrap 6 prefix suffix) 6))
325 ;; Word
326 (should (eq (markdown-point-after-unwrap 7 prefix suffix) 6))
327 (should (eq (markdown-point-after-unwrap 8 prefix suffix) 7))
328 (should (eq (markdown-point-after-unwrap 9 prefix suffix) 8))
329 ;; Suffix
330 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 9))
331 ;; Immediately after
332 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 9)))))
334 (ert-deftest test-markdown-insertion/unwrap-thing-at-point-italic ()
335 "Test function `markdown-unwrap-thing-at-point' on italics."
336 (markdown-test-file "syntax.text"
337 ;; Unwrap *not*
338 (goto-char 2859)
339 (should (thing-at-point-looking-at markdown-regex-italic))
340 (should (equal (markdown-unwrap-thing-at-point
341 markdown-regex-italic 1 3)
342 (cons 2859 2862)))
343 (should (= (point) 2859))
344 ;; Unwrap *publishing*
345 (goto-char 3064)
346 (should (thing-at-point-looking-at markdown-regex-italic))
347 (should (equal (markdown-unwrap-thing-at-point
348 markdown-regex-italic 1 3)
349 (cons 3060 3070)))
350 (should (= (point) 3063))
351 ;; Unwrap *writing*
352 (goto-char 3101)
353 (should (thing-at-point-looking-at markdown-regex-italic))
354 (should (equal (markdown-unwrap-thing-at-point
355 markdown-regex-italic 1 3)
356 (cons 3093 3100)))
357 (should (= (point) 3100))))
359 (ert-deftest test-markdown-insertion/unwrap-things-in-region-italic ()
360 "Test function `markdown-unwrap-things-in-region' on italics."
361 (markdown-test-file "syntax.text"
362 (should (equal (markdown-unwrap-things-in-region
363 2704 3207 markdown-regex-italic 1 3)
364 (cons 2704 3201)))))
366 (ert-deftest test-markdown-insertion/unwrap-things-in-region-bound ()
367 "Ensure that `markdown-unwrap-things-in-region' respects end bound"
368 (markdown-test-string "**a** **b** **c** **d** **e** **f**"
369 ;; Set region to unrwap a, b, c, and d only. If endpoint is not
370 ;; respected (i.e, not adjusted for character removal), the
371 ;; function will unwrap e and f also.
372 (should (equal (markdown-unwrap-things-in-region
373 1 24 markdown-regex-bold 2 4)
374 (cons 1 8)))
375 (should (string-equal (buffer-string) "a b c d **e** **f**"))))
377 (ert-deftest test-markdown-insertion/unwrap-things-in-region-links ()
378 "Test function `markdown-unwrap-things-in-region' on inline links."
379 (markdown-test-string "a [link](http://jblevins.org/) or [two](/).\n"
380 (should (equal (markdown-unwrap-things-in-region
381 (point-min) (point-max) markdown-regex-link-inline 0 3)
382 (cons 1 16)))
383 (should (string-equal (buffer-string) "a link or two.\n"))))
385 (ert-deftest test-markdown-insertion/toggle-bold ()
386 "Test toggling functionality of `markdown-insert-bold'."
387 (markdown-test-string "one **two** three"
388 (forward-word 2)
389 (markdown-insert-bold)
390 (should (string-equal (buffer-string) "one two three"))
391 (should (= (point) 8))
392 (forward-word)
393 (markdown-insert-bold)
394 (should (= (point) 16))
395 (should (string-equal (buffer-string) "one two **three**"))))
397 (ert-deftest test-markdown-insertion/toggle-italic ()
398 "Test toggling functionality of `markdown-insert-italic'."
399 (markdown-test-string "one *two* three"
400 (forward-word 2)
401 (markdown-insert-italic)
402 (should (string-equal (buffer-string) "one two three"))
403 (should (= (point) 8))
404 (forward-word)
405 (markdown-insert-italic)
406 (should (string-equal (buffer-string) "one two *three*"))
407 (should (= (point) 15))))
409 (ert-deftest test-markdown-insertion/toggle-code ()
410 "Test toggling functionality of `markdown-insert-code'."
411 (markdown-test-string "one `two` three"
412 (forward-word 2)
413 (markdown-insert-code)
414 (should (string-equal (buffer-string) "one two three"))
415 (should (= (point) 8))
416 (forward-word)
417 (markdown-insert-code)
418 (should (string-equal (buffer-string) "one two `three`"))
419 (should (= (point) 15))))
421 (ert-deftest test-markdown-insertion/toggle-kbd ()
422 "Test toggling functionality of `markdown-insert-code'."
423 (markdown-test-string "test <kbd>C-c C-s k</kbd> toggle"
424 (forward-word 2)
425 (markdown-insert-kbd)
426 (should (string-equal (buffer-string) "test C-c C-s k toggle"))
427 (should (= (point) 6))
428 (backward-word)
429 (markdown-insert-kbd)
430 (should (string-equal (buffer-string) "<kbd>test</kbd> C-c C-s k toggle"))
431 (should (= (point) 6))))
433 (ert-deftest test-markdown-insertion/toggle-wiki-link-alias-first ()
434 "Test toggling of `markdown-insert-wiki-link' with alias first.
435 Test point position upon removal and insertion."
436 (let ((markdown-wiki-link-alias-first t))
437 (markdown-test-string "[[text|page]]"
438 (goto-char 5) ; point in interior of alias text, at 'x'
439 (call-interactively 'markdown-insert-wiki-link)
440 (should (= (point) 3)) ; leave point at, at 'x'
441 (should (string-equal (buffer-string) "text"))
442 (call-interactively 'markdown-insert-wiki-link)
443 (should (= (point) 5)) ; leave point at, at 'x'
444 (should (string-equal (buffer-string) "[[text]]")))
445 (markdown-test-string "[[text|page]]"
446 (goto-char 10) ; point in interior of link text, at 'g'
447 (call-interactively 'markdown-insert-wiki-link)
448 (should (= (point) 5)) ; leave point at end of alias text
449 (should (string-equal (buffer-string) "text"))
450 (call-interactively 'markdown-insert-wiki-link)
451 (should (= (point) 7)) ; leave point at end of alias text
452 (should (string-equal (buffer-string) "[[text]]")))))
454 (ert-deftest test-markdown-insertion/toggle-wiki-link-alias-last ()
455 "Test toggling of `markdown-insert-wiki-link' with alias last.
456 Test point position upon removal and insertion."
457 (let ((markdown-wiki-link-alias-first nil))
458 (markdown-test-string "[[page|text]]"
459 (goto-char 10) ; point in interior of alias text, at 'x'
460 (call-interactively 'markdown-insert-wiki-link)
461 (goto-char 3) ; leave point at, at 'x'
462 (should (string-equal (buffer-string) "text"))
463 (call-interactively 'markdown-insert-wiki-link)
464 (should (= (point) 5)) ; leave point at, at 'x'
465 (should (string-equal (buffer-string) "[[text]]")))
466 (markdown-test-string "[[page|text]]"
467 (goto-char 3) ; point in interior of link text, at 'g'
468 (call-interactively 'markdown-insert-wiki-link)
469 (should (= (point) 1)) ; leave point at beginning of alias text
470 (should (string-equal (buffer-string) "text"))
471 (call-interactively 'markdown-insert-wiki-link)
472 (should (= (point) 3)) ; leave point at beginning of alias text
473 (should (string-equal (buffer-string) "[[text]]")))))
475 (ert-deftest test-markdown-insertion/bold-region ()
476 "Test region functionality of `markdown-insert-bold'."
477 (markdown-test-string "one two three"
478 (push-mark (point) t t)
479 (forward-word 2)
480 (markdown-insert-bold)
481 (should (string-equal (buffer-string) "**one two** three"))
482 (should (= (point) 10))))
484 (ert-deftest test-markdown-insertion/italic-region ()
485 "Test region functionality of `markdown-insert-italic'."
486 (markdown-test-string "one two three"
487 (transient-mark-mode)
488 (push-mark (point) t t)
489 (forward-word 2)
490 (markdown-insert-italic)
491 (should (string-equal (buffer-string) "*one two* three"))
492 (should (= (point) 9))))
494 (ert-deftest test-markdown-insertion/code-region ()
495 "Test region functionality of `markdown-insert-code'."
496 (markdown-test-string "one two three"
497 (transient-mark-mode)
498 (push-mark (point) t t)
499 (forward-word 2)
500 (markdown-insert-code)
501 (should (string-equal (buffer-string) "`one two` three"))
502 (should (= (point) 9))))
504 (ert-deftest test-markdown-insertion/kbd-region ()
505 "Test region functionality of `markdown-insert-kbd'."
506 (markdown-test-string "one two three"
507 (transient-mark-mode)
508 (push-mark (point) t t)
509 (forward-word 2)
510 (markdown-insert-kbd)
511 (should (string-equal (buffer-string) "<kbd>one two</kbd> three"))
512 (should (= (point) 13))))
514 (ert-deftest test-markdown-insertion/atx-line ()
515 "Test ATX header insertion without region."
516 (markdown-test-string "line one\nline two\n"
517 (forward-word)
518 (markdown-insert-header-atx-1)
519 (should (= (point) 11))
520 (should (string-equal (buffer-substring (point-min) (point-max))
521 "# line one #\n\nline two\n"))
522 (forward-line 2)
523 (markdown-insert-header-atx-2)
524 (should (= (point) 26))
525 (should (string-equal (buffer-substring (point-min) (point-max))
526 "# line one #\n\n## line two ##\n\n"))))
528 (ert-deftest test-markdown-insertion/atx-region ()
529 "Test ATX header insertion with region."
530 (markdown-test-string "line one\nline two\n"
531 (transient-mark-mode)
532 (forward-char 5)
533 (push-mark (point) t t)
534 (forward-word)
535 (should (string-equal (buffer-substring (region-beginning) (region-end))
536 "one"))
537 (markdown-insert-header-atx-4)
538 (should (= (point) 16))
539 (should (string-equal (buffer-substring (point-min) (point-max))
540 "line \n\n#### one ####\n\nline two\n"))))
542 (ert-deftest test-markdown-insertion/atx-blank ()
543 "Test ATX header insertion on blank line."
544 (markdown-test-string "line one\n\nline two\n"
545 (forward-line)
546 (markdown-insert-header-atx-3)
547 (should (string-equal (buffer-substring (point-min) (point-max))
548 "line one\n\n### ###\n\nline two\n"))
549 (should (= (point) 15))
550 (should (looking-at " ###\n"))))
552 (ert-deftest test-markdown-insertion/atx-region-whitespace ()
553 "Test ATX header insertion using a region with whitespace."
554 (markdown-test-string " line one\n\nline two\n \n"
555 (transient-mark-mode)
556 (push-mark (point) t t)
557 (goto-char (point-max))
558 (markdown-insert-header-atx-2)
559 (should (string-equal (buffer-substring (point-min) (point-max))
560 "## line one line two ##"))
561 (should (= (point) 21))
562 (should (looking-at " ##"))))
564 (ert-deftest test-markdown-insertion/atx-line-whitespace ()
565 "Test ATX header insertion using current line with whitespace."
566 (markdown-test-string " line one \n\nline two\n"
567 (goto-char (line-end-position))
568 (markdown-insert-header-atx-3)
569 (should (string-equal (buffer-substring (point-min) (point-max))
570 "### line one ###\n\nline two\n"))
571 (should (= (point) 13))
572 (should (looking-at " ###\n"))))
574 (ert-deftest test-markdown-insertion/atx-replace-atx ()
575 "Test ATX header insertion when replacing an existing ATX header."
576 (markdown-test-string "## replace ##\n"
577 (markdown-insert-header-atx-4)
578 (should (string-equal (buffer-string) "#### replace ####\n\n"))
579 (should (looking-at " ####\n"))))
581 (ert-deftest test-markdown-insertion/atx-replace-setext-1 ()
582 "Test ATX header insertion when replacing an existing setext header."
583 (markdown-test-string "replace\n=======\n"
584 (markdown-insert-header-atx-2)
585 (should (string-equal (buffer-string) "## replace ##\n\n"))
586 (should (looking-at " ##\n"))))
588 (ert-deftest test-markdown-insertion/atx-replace-setext-2 ()
589 "Test ATX header insertion when replacing an existing setext header."
590 (markdown-test-string "replace\n-------\n"
591 (markdown-insert-header-atx-5)
592 (should (string-equal (buffer-string) "##### replace #####\n\n"))
593 (should (looking-at " #####\n"))))
595 (ert-deftest test-markdown-insertion/atx-asymmetric-point ()
596 "Test point after ATX header insertion with `markdown-asymmetric-header'."
597 (markdown-test-string
598 "Test"
599 (let ((markdown-asymmetric-header t))
600 (markdown-insert-header-atx-5)
601 (should (= (point) 11))
602 (should (string-equal (buffer-string) "##### Test")))))
604 (ert-deftest test-markdown-insertion/setext-line ()
605 "Test setext header insertion without region."
606 (markdown-test-string "line one\nline two\n"
607 (forward-word)
608 (markdown-insert-header-setext-1)
609 (should (string-equal (buffer-substring (point-min) (point-max))
610 "line one\n========\n\nline two\n"))
611 (forward-line 3)
612 (markdown-insert-header-setext-2)
613 (should (string-equal (buffer-substring (point-min) (point-max))
614 "line one\n========\n\nline two\n--------\n\n"))))
616 (ert-deftest test-markdown-insertion/setext-region ()
617 "Test setext header insertion with region."
618 (markdown-test-string "line one\nline two\n"
619 (transient-mark-mode)
620 (forward-char 5)
621 (push-mark (point) t t)
622 (forward-word)
623 (should (string-equal (buffer-substring (region-beginning) (region-end))
624 "one"))
625 (markdown-insert-header-setext-1)
626 (should (string-equal (buffer-substring (point-min) (point-max))
627 "line \n\none\n===\n\nline two\n"))))
629 (ert-deftest test-markdown-insertion/setext-blank ()
630 "Test setext header insertion on blank line."
631 (markdown-test-string "line one\n\nline two\n"
632 (forward-line)
633 (markdown-insert-header 2 "foo" t)
634 (should (string-equal (buffer-substring (point-min) (point-max))
635 "line one\n\nfoo\n---\n\nline two\n"))
636 (should (= (point) 14))
637 (should (looking-at "\n---"))))
639 (ert-deftest test-markdown-insertion/setext-region-whitespace ()
640 "Test setext header insertion using a region with whitespace."
641 (markdown-test-string " line one\n\nline two\n \n"
642 (transient-mark-mode)
643 (push-mark (point) t t)
644 (goto-char (point-max))
645 (markdown-insert-header-setext-1)
646 (should (string-equal (buffer-substring (point-min) (point-max))
647 "line one line two\n================="))
648 (should (= (point) 18))
649 (should (looking-at "\n===="))))
651 (ert-deftest test-markdown-insertion/setext-line-whitespace ()
652 "Test setext header insertion using current line with whitespace."
653 (markdown-test-string " line one \n\nline two\n"
654 (goto-char (line-end-position))
655 (markdown-insert-header-setext-2)
656 (should (string-equal (buffer-substring (point-min) (point-max))
657 "line one\n--------\n\nline two\n"))
658 (should (= (point) 9))
659 (should (looking-at "\n---"))))
661 (ert-deftest test-markdown-insertion/setext-replace-atx ()
662 "Test setext header insertion when replacing an existing ATX header."
663 (markdown-test-string "## replace ##\n"
664 (markdown-insert-header-setext-1)
665 (should (string-equal (buffer-string) "replace\n=======\n\n"))
666 (should (looking-at "\n==="))))
668 (ert-deftest test-markdown-insertion/setext-replace-setext-1 ()
669 "Test setext header insertion when replacing an existing setext title."
670 (markdown-test-string "replace\n=======\n"
671 (markdown-insert-header-setext-2)
672 (should (string-equal (buffer-string) "replace\n-------\n\n"))
673 (should (looking-at "\n---"))))
675 (ert-deftest test-markdown-insertion/setext-replace-setext-2 ()
676 "Test setext header insertion when replacing an existing setext section."
677 (markdown-test-string "replace\n-------\n"
678 (markdown-insert-header-setext-1)
679 (should (string-equal (buffer-string) "replace\n=======\n\n"))
680 (should (looking-at "\n==="))))
682 (ert-deftest test-markdown-insertion/header-dwim ()
683 "Test 'do what I mean' header insertion."
684 (markdown-test-file "outline.text"
685 (call-interactively 'markdown-insert-header-dwim)
686 (should (looking-at " #$"))
687 (end-of-defun 2)
688 (call-interactively 'markdown-insert-header-dwim)
689 (beginning-of-line)
690 (should (looking-at "^# #$"))
691 (end-of-defun 3)
692 (call-interactively 'markdown-insert-header-dwim)
693 (beginning-of-line)
694 (should (looking-at "^### ###$"))))
696 (ert-deftest test-markdown-insertion/header-dwim-prefix ()
697 "Test 'do what I mean' header insertion with prefix arguments."
698 (let ((tests (list '(nil . "## abc ##")
699 '(1 . "# abc #")
700 '(2 . "## abc ##")
701 '(3 . "### abc ###")
702 '(4 . "#### abc ####")
703 '(5 . "##### abc #####")
704 '(6 . "###### abc ######")
705 '((4) . "# abc #")
706 '((16) . "### abc ###"))))
707 (dolist (test tests)
708 (markdown-test-string "## atx\n\nabc"
709 (goto-char (point-max))
710 (let ((current-prefix-arg (car test)))
711 (call-interactively 'markdown-insert-header-dwim)
712 (should (string-equal
713 (buffer-substring (line-beginning-position) (line-end-position))
714 (cdr test))))))))
716 (ert-deftest test-markdown-insertion/header-setext-dwim-prefix ()
717 "Test 'do what I mean' header insertion with prefix arguments."
718 (let ((tests (list '(nil . "abc\n---")
719 '(1 . "abc\n===")
720 '(2 . "abc\n---")
721 '(3 . "### abc ###")
722 '(4 . "#### abc ####")
723 '(5 . "##### abc #####")
724 '(6 . "###### abc ######")
725 '((4) . "abc\n===")
726 '((16) . "### abc ###"))))
727 (dolist (test tests)
728 (markdown-test-string "atx\n---\n\nabc"
729 (goto-char (point-max))
730 (let ((current-prefix-arg (car test)))
731 (call-interactively 'markdown-insert-header-setext-dwim)
732 (should (string-equal
733 (buffer-substring (line-beginning-position) (line-end-position 2))
734 (cdr test))))))))
736 (ert-deftest test-markdown-insertion/header-setext-dwim ()
737 "Test 'do what I mean' header insertion with setext headers."
738 (markdown-test-string
739 "asdfasfasfdsadfasdfasdf\n======="
740 (goto-char 12)
741 (call-interactively 'markdown-insert-header-dwim)
742 (should (string-equal
743 (buffer-string)
744 "asdfasfasfdsadfasdfasdf\n======================="))))
746 (ert-deftest test-markdown-insertion/remove-header ()
747 "Test ATX and setext header."
748 (markdown-test-string
749 "# atx1\n\n## atx2 ##\n\nsetext1\n=======\n\nsetext2\n-------\n"
750 (should (equal (markdown-remove-header) (cons 1 5)))
751 (forward-line)
752 (should (not (markdown-remove-header)))
753 (forward-line)
754 (should (equal (markdown-remove-header) (cons 7 11)))
755 (forward-line)
756 (should (not (markdown-remove-header)))
757 (forward-line)
758 (should (equal (markdown-remove-header) (cons 13 20)))
759 (forward-line)
760 (should (not (markdown-remove-header)))
761 (forward-line)
762 (should (equal (markdown-remove-header) (cons 22 29)))
763 (should (string-equal (buffer-string)
764 "atx1\n\natx2\n\nsetext1\n\nsetext2\n"))))
766 (ert-deftest test-markdown-insertion/italic-unwrap-region ()
767 "A test of inserting italics with italic text in the region."
768 (markdown-test-string "*foo* bar *baz*"
769 (transient-mark-mode)
770 (push-mark (point) t t)
771 (end-of-line)
772 (markdown-insert-italic)
773 (should (string-equal (buffer-string) "*foo bar baz*"))))
775 (ert-deftest test-markdown-insertion/bold-unwrap-region ()
776 "A test of inserting bold with italic text in the region."
777 (markdown-test-string "*foo* **bar** *baz*"
778 (transient-mark-mode)
779 (push-mark (point) t t)
780 (end-of-line)
781 (markdown-insert-bold)
782 (should (string-equal (buffer-string) "***foo* bar *baz***"))))
784 (ert-deftest test-markdown-insertion/code-unwrap-region ()
785 "A test of inserting code with code already in the region."
786 (markdown-test-string "`foo` *bar* `baz`"
787 (transient-mark-mode)
788 (push-mark (point) t t)
789 (end-of-line)
790 (markdown-insert-code)
791 (should (string-equal (buffer-string) "`foo *bar* baz`"))))
793 (ert-deftest test-markdown-insertion/hr-order ()
794 "Test inserting horizontal rules."
795 (dotimes (n (length markdown-hr-strings))
796 (markdown-test-string ""
797 (let ((current-prefix-arg n))
798 (call-interactively 'markdown-insert-hr))
799 (should (string-equal (buffer-string) (nth (1- n) markdown-hr-strings))))))
801 (ert-deftest test-markdown-insertion/hr-prefix ()
802 "Test inserting horizontal rule with C-u prefix."
803 (markdown-test-string ""
804 (let ((current-prefix-arg '(4)))
805 (call-interactively 'markdown-insert-hr))
806 (should (string-equal (buffer-string) (car (last markdown-hr-strings))))))
808 (ert-deftest test-markdown-insertion/hr-bob ()
809 "Test inserting horizontal rule at beginning of buffer."
810 (markdown-test-string "one line\n"
811 (call-interactively 'markdown-insert-hr)
812 (should (string-equal (buffer-string)
813 (concat (car markdown-hr-strings)
814 "\n\none line\n")))))
816 (ert-deftest test-markdown-insertion/hr-eob ()
817 "Test inserting horizontal rule at end of buffer."
818 (markdown-test-string "one line\n"
819 (forward-line)
820 (call-interactively 'markdown-insert-hr)
821 (should (string-equal (buffer-string)
822 (concat "one line\n\n" (car markdown-hr-strings))))))
824 (ert-deftest test-markdown-insertion/hr-mob ()
825 "Test inserting horizontal rule in middle of buffer."
826 (markdown-test-string "one line\n"
827 (forward-word)
828 (let ((markdown-hr-strings '("----------")))
829 (call-interactively 'markdown-insert-hr)
830 (should (string-equal (buffer-string)
831 (concat "one\n\n" (car markdown-hr-strings)
832 "\n\n line\n"))))))
834 (ert-deftest test-markdown-insertion/pre-region-1 ()
835 "Test `markdown-pre-region'."
836 ;; Simple test as non-interactive command
837 (markdown-test-string "line one\nline two\n"
838 (markdown-pre-region (line-beginning-position) (line-end-position))
839 (should (string-equal (buffer-string) " line one\n\nline two\n")))
840 ;; Test removal of whitespace before and after region
841 (markdown-test-string "line one abc\nline two\n"
842 (markdown-pre-region 6 9)
843 (should (string-equal (buffer-string) "line\n\n one\n\nabc\nline two\n")))
844 ;; Simple test as interactive command
845 (markdown-test-string "line one\nline two\n"
846 (push-mark (point) t t)
847 (forward-line 2)
848 (call-interactively 'markdown-pre-region)
849 (should (string-equal (buffer-string) " line one\n line two\n\n"))))
851 (ert-deftest test-markdown-insertion/blockquote-region-1 ()
852 "Test `markdown-blockquote-region'."
853 ;; Simple test as non-interactive command
854 (markdown-test-string "line one\nline two\n"
855 (markdown-blockquote-region (line-beginning-position) (line-end-position))
856 (should (string-equal (buffer-string) "> line one\n\nline two\n")))
857 ;; Test removal of whitespace before and after region
858 (markdown-test-string "line one abc\nline two\n"
859 (markdown-blockquote-region 6 9)
860 (should (string-equal (buffer-string) "line\n\n> one\n\nabc\nline two\n")))
861 ;; Simple test as interactive command
862 (markdown-test-string "line one\nline two\n"
863 (push-mark (point) t t)
864 (forward-line 2)
865 (call-interactively 'markdown-blockquote-region)
866 (should (string-equal (buffer-string) "> line one\n> line two\n\n"))))
868 (ert-deftest test-markdown-insertion/pre-nested-lists ()
869 "Test `markdown-pre-indentation' and `markdown-insert-pre' with nested list."
870 (markdown-test-string "* item\n * item\n"
871 ;; before the first item
872 (should (string-equal (markdown-pre-indentation (point)) " "))
873 (markdown-insert-pre)
874 (beginning-of-line)
875 (should (markdown-prev-line-blank-p))
876 (should (looking-at "^ $"))
877 (should (markdown-next-line-blank-p))
878 ;; before the second item
879 (forward-line 3)
880 (should (string-equal (markdown-pre-indentation (point)) " "))
881 (markdown-insert-pre)
882 (beginning-of-line)
883 (should (markdown-prev-line-blank-p))
884 (should (looking-at "^ $"))
885 (should (markdown-next-line-blank-p))
886 ;; after the second item
887 (forward-line 3)
888 (should (string-equal (markdown-pre-indentation (point)) " "))
889 (markdown-insert-pre)
890 (beginning-of-line)
891 (should (markdown-prev-line-blank-p))
892 (should (looking-at "^ $"))
893 (should (markdown-next-line-blank-p))))
895 (ert-deftest test-markdown-insertion/pre-faux-list ()
896 "Test `markdown-pre-indentation' following a list-marker in a pre block."
897 (markdown-test-string " * pre block, not a list item\n"
898 (should (string-equal (markdown-pre-indentation (point-max)) " "))))
900 (ert-deftest test-markdown-insertion/blockquote-nested-lists ()
901 "Test blockquote insertion in a nested list context."
902 (markdown-test-string "* item\n * item\n"
903 ;; before the first item
904 (should (string-equal (markdown-blockquote-indentation (point)) ""))
905 (markdown-insert-blockquote)
906 (beginning-of-line)
907 (should (markdown-prev-line-blank-p))
908 (should (looking-at "^> $"))
909 (should (markdown-next-line-blank-p))
910 ;; before the second item
911 (forward-line 3)
912 (should (string-equal (markdown-blockquote-indentation (point)) " "))
913 (markdown-insert-blockquote)
914 (beginning-of-line)
915 (should (markdown-prev-line-blank-p))
916 (should (looking-at "^ > $"))
917 (should (markdown-next-line-blank-p))
918 ;; after the second item
919 (forward-line 3)
920 (should (string-equal (markdown-blockquote-indentation (point)) " "))
921 (markdown-insert-blockquote)
922 (beginning-of-line)
923 (should (markdown-prev-line-blank-p))
924 (should (looking-at "^ > $"))
925 (should (markdown-next-line-blank-p))))
927 (ert-deftest test-markdown-insertion/empty-italic ()
928 "Test `markdown-insert-italic' with no word at point and no region."
929 (markdown-test-string ""
930 (call-interactively 'markdown-insert-italic)
931 (should (string-equal (buffer-string) "**"))
932 (should (= (point) 2))))
934 (ert-deftest test-markdown-insertion/empty-bold ()
935 "Test `markdown-insert-bold' with no word at point and no region."
936 (markdown-test-string ""
937 (call-interactively 'markdown-insert-bold)
938 (should (string-equal (buffer-string) "****"))
939 (should (= (point) 3))))
941 (ert-deftest test-markdown-insertion/uri ()
942 "Test `markdown-insert-uri'."
943 (markdown-test-string "http://jblevins.org/projects/markdown-mode/"
944 (call-interactively 'markdown-insert-uri)
945 (should (string-equal (buffer-string) "<http://jblevins.org/projects/markdown-mode/>"))
946 (should (= (point) 2))
947 (call-interactively 'markdown-insert-uri)
948 (should (string-equal (buffer-string) "http://jblevins.org/projects/markdown-mode/"))
949 (should (= (point) 1))
950 (erase-buffer)
951 (call-interactively 'markdown-insert-uri)
952 (should (string-equal (buffer-string) "<>"))
953 (should (= (point) 2))))
955 (ert-deftest test-markdown-insertion/list-item ()
956 "Test `markdown-insert-list-item' on several lists."
957 ;; No existing list
958 (markdown-test-string "abc"
959 (goto-char (point-max))
960 (call-interactively 'markdown-insert-list-item)
961 (should (string-equal (buffer-string) "abc\n * "))
962 (should (= (point) 9)))
963 ;; Following a list item, on the same line
964 (markdown-test-string " * foo"
965 (goto-char (point-max))
966 (call-interactively 'markdown-insert-list-item)
967 (should (string-equal (buffer-string) " * foo\n * ")))
968 ;; Following a list item, on the next line
969 (markdown-test-string "- foo\n"
970 (goto-char (point-max))
971 (call-interactively 'markdown-insert-list-item)
972 (should (string-equal (buffer-string) "- foo\n- ")))
973 ;; Following a list item, after a blank line
974 (markdown-test-string "- foo\n\n"
975 (goto-char (point-max))
976 (call-interactively 'markdown-insert-list-item)
977 (should (string-equal (buffer-string) "- foo\n\n- ")))
978 ;; Preceding a list item
979 (markdown-test-string "- foo\n"
980 (goto-char (point-min))
981 (call-interactively 'markdown-insert-list-item)
982 (should (string-equal (buffer-string) "- \n- foo\n")))
983 ;; Preceding a list item and a blank line
984 (markdown-test-string "\n\n- foo\n"
985 (goto-char (point-min))
986 (call-interactively 'markdown-insert-list-item)
987 (should (string-equal (buffer-string) "- \n\n- foo\n")))
988 ;; In the middle of a list item
989 (markdown-test-string "- foo bar\n"
990 (forward-word)
991 (call-interactively 'markdown-insert-list-item)
992 (should (string-equal (buffer-string) "- foo\n- bar\n")))
993 ;; Before a list marker, but not at beginning of line
994 (markdown-test-string " - foo\n"
995 (forward-char 2)
996 (call-interactively 'markdown-insert-list-item)
997 (should (string-equal (buffer-string) " - \n - foo\n")))
998 ;; Following an ordered list item
999 (markdown-test-string "6. foo"
1000 (goto-char (point-max))
1001 (call-interactively 'markdown-insert-list-item)
1002 (should (string-equal (buffer-string) "6. foo\n7. ")))
1003 ;; Following a fancy list item, on the next line
1004 (markdown-test-string "#. foo"
1005 (goto-char (point-max))
1006 (call-interactively 'markdown-insert-list-item)
1007 (should (string-equal (buffer-string) "#. foo\n#. ")))
1008 ;; Following a nested ordered list item
1009 (markdown-test-string "6. foo\n 1. bar"
1010 (goto-char (point-max))
1011 (call-interactively 'markdown-insert-list-item)
1012 (should (string-equal (buffer-string) "6. foo\n 1. bar\n 2. ")))
1013 ;; Preceding an ordered list item
1014 (markdown-test-string "\n1. foo\n2. bar"
1015 (goto-char (point-min))
1016 (call-interactively 'markdown-insert-list-item)
1017 (should (string-equal (buffer-string) "1. \n1. foo\n2. bar")))
1018 ;; Preserve previous spacing in ordered list
1019 (markdown-test-string "1. foo"
1020 (goto-char (point-max))
1021 (call-interactively 'markdown-insert-list-item)
1022 (should (string-equal (buffer-string) "1. foo\n2. ")))
1023 ;; Adjust spacing for number width changes (e.g., 9 -> 10)
1024 (markdown-test-string "9. foo"
1025 (goto-char (point-max))
1026 (call-interactively 'markdown-insert-list-item)
1027 (should (string-equal (buffer-string) "9. foo\n10. ")))
1028 ;; Don't adjust spacing for number width changes if no extra whitespace
1029 (markdown-test-string "99. foo"
1030 (goto-char (point-max))
1031 (call-interactively 'markdown-insert-list-item)
1032 (should (string-equal (buffer-string) "99. foo\n100. ")))
1033 ;; Don't adjust spacing if tabs are used as whitespace
1034 (markdown-test-string "9.\tfoo"
1035 (goto-char (point-max))
1036 (call-interactively 'markdown-insert-list-item)
1037 (should (string-equal (buffer-string) "9.\tfoo\n10.\t"))))
1039 (ert-deftest test-markdown-insertion/nested-list-marker ()
1040 "Test marker detection for `markdown-insert-list-item'."
1041 (markdown-test-string
1042 "1. A\n * AA\n 1. AAA"
1043 (goto-char (point-max))
1044 (let ((current-prefix-arg '(4)))
1045 (call-interactively 'markdown-insert-list-item))
1046 (should (eq (point) 36))
1047 (should (looking-back "\* "))
1048 (should (string-equal
1049 (buffer-string)
1050 "1. A\n * AA\n 1. AAA\n * "))
1051 (let ((current-prefix-arg '(4)))
1052 (call-interactively 'markdown-insert-list-item))
1053 (should (eq (point) 40))
1054 (should (looking-back "2\. "))
1055 (should (string-equal
1056 (buffer-string)
1057 "1. A\n * AA\n 1. AAA\n * \n2. "))
1058 (let ((current-prefix-arg '(4)))
1059 (call-interactively 'markdown-insert-list-item))
1060 (should (eq (point) 44))
1061 (should (looking-back "3\. "))
1062 (should (string-equal
1063 (buffer-string)
1064 "1. A\n * AA\n 1. AAA\n * \n2. \n3. "))))
1066 (ert-deftest test-markdown-insertion/reference-link ()
1067 "Basic tests for `markdown-insert-reference-link'."
1068 ;; Test optional parameters (leave point after link)
1069 (markdown-test-string ""
1070 (markdown-insert-reference-link "abc" "1")
1071 (should (string-equal (buffer-string) "[abc][1]"))
1072 (should (= (point) 9)))
1073 ;; Full link without title (leave point after link)
1074 (markdown-test-string ""
1075 (markdown-insert-reference-link "link" "label" "http://jblevins.org/")
1076 (should (string-equal (buffer-string) "[link][label]\n\n[label]: http://jblevins.org/\n"))
1077 (should (= (point) 14)))
1078 ;; Full link without label or title (leave point after link)
1079 (markdown-test-string ""
1080 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
1081 (should (string-equal (buffer-string) "[link][]\n\n[link]: http://jblevins.org/\n"))
1082 (should (= (point) 9)))
1083 ;; Link only with no label, URL, or title (leave point after link)
1084 (markdown-test-string ""
1085 (markdown-insert-reference-link "link" "")
1086 (should (string-equal (buffer-string) "[link][]"))
1087 (should (= (point) 9))))
1089 (ert-deftest test-markdown-insertion/reference-link-end ()
1090 "Basic reference link insertion test for 'end location."
1091 (let ((markdown-reference-location 'end))
1092 (markdown-test-string "first para\n\nsecond para\n"
1093 (end-of-line)
1094 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
1095 (should (= (point) 19))
1096 (goto-char (point-min))
1097 (forward-line 4)
1098 (should (looking-at "\\[link\\]: http://jblevins.org/")))))
1100 (ert-deftest test-markdown-insertion/reference-link-immediately ()
1101 "Basic reference link insertion test for 'immediately location."
1102 (let ((markdown-reference-location 'immediately))
1103 (markdown-test-string "first para\n\nsecond para\n"
1104 (end-of-line)
1105 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
1106 (should (= (point) 19))
1107 (goto-char (point-min))
1108 (forward-line 2)
1109 (should (looking-at "\\[link\\]: http://jblevins.org/")))))
1111 (ert-deftest test-markdown-insertion/reference-link-header ()
1112 "Basic reference link and definition insertion test for 'header location."
1113 (let ((markdown-reference-location 'header))
1114 (markdown-test-string "par one\n\npar two\n\n### header\n"
1115 (end-of-line)
1116 (markdown-insert-reference-link "link" "")
1117 (markdown-insert-reference-definition "link")
1118 (should (= (point) 35))
1119 (should (looking-back "\\[link\\]: " nil)))))
1121 (ert-deftest test-markdown-insertion/inline-to-reference-link ()
1122 "Inline link to reference link conversion."
1123 (markdown-test-string "[text](http://jblevins.org/ \"title\")"
1124 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-reference-link-dwim RET 1 RET"))
1125 (should (string-equal (buffer-string) "[text][1]\n\n[1]: http://jblevins.org/ \"title\"\n")))
1126 (markdown-test-string "[text](http://jblevins.org/)"
1127 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-reference-link-dwim RET 1 RET"))
1128 (should (string-equal (buffer-string) "[text][1]\n\n[1]: http://jblevins.org/\n"))))
1130 (ert-deftest test-markdown-insertion/inline-to-reference-link-2 ()
1131 "Inline link to reference link conversion with existing reference links.
1132 Regression test: adding a new reference link with
1133 `markdown-insert-reference-link-dwim' should not throw an 'args
1134 out of range' error when the existing reference label is a single
1135 character."
1136 (markdown-test-string "[text](http://jblevins.org/ \"title\")\n\n[1]: https://www.gnu.org"
1137 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-reference-link-dwim RET 2 RET"))))
1139 (ert-deftest test-markdown-insertion/inline-link-empty ()
1140 "Test `markdown-insert-inline-link-dwim' for empty markup insertion.
1141 Point should be left inside square brackets."
1142 (markdown-test-string "abc "
1143 (end-of-line)
1144 (call-interactively 'markdown-insert-inline-link-dwim)
1145 (should (string-equal (buffer-string) "abc []()"))
1146 (should (= (point) 6))))
1148 (ert-deftest test-markdown-insertion/inline-link-word-at-point ()
1149 "Test `markdown-insert-inline-link-dwim' with word at point.
1150 Point should be left inside parentheses."
1151 (markdown-test-string "abc def ghi"
1152 (forward-word 2)
1153 (call-interactively 'markdown-insert-inline-link-dwim)
1154 (should (string-equal (buffer-string) "abc [def]() ghi"))
1155 (should (= (point) 11))))
1157 (ert-deftest test-markdown-insertion/inline-link-angle-url-at-point ()
1158 "Test `markdown-insert-inline-link-dwim' with angle URL at point.
1159 Point should be left inside square brackets."
1160 (markdown-test-string "<https://www.gnu.org/>"
1161 (forward-line)
1162 (call-interactively 'markdown-insert-inline-link-dwim)
1163 (should (string-equal (buffer-string) "[](https://www.gnu.org/)"))
1164 (should (= (point) 2))))
1166 (ert-deftest test-markdown-insertion/inline-link-plain-url-at-point ()
1167 "Test `markdown-insert-inline-link-dwim' with plain URL at point.
1168 Point should be left inside square brackets."
1169 (markdown-test-string "https://www.gnu.org/"
1170 (forward-line)
1171 (call-interactively 'markdown-insert-inline-link-dwim)
1172 (should (string-equal (buffer-string) "[](https://www.gnu.org/)"))
1173 (should (= (point) 2))))
1175 (ert-deftest test-markdown-insertion/inline-link-reference-link-at-point ()
1176 "Test `markdown-insert-inline-link-dwim' with reference link at point."
1177 (markdown-test-string ""
1178 (markdown-insert-reference-link "link" "label" "http://jblevins.org/")
1179 (call-interactively 'markdown-insert-inline-link-dwim)
1180 (should (string-equal (buffer-substring 1 29) "[link](http://jblevins.org/)"))
1181 (should (= (point) 29))))
1183 (ert-deftest test-markdown-insertion/inline-link-active-region ()
1184 "Test `markdown-insert-inline-link-dwim' with active region.
1185 Point should be left inside parentheses."
1186 (markdown-test-string "abc def ghi"
1187 (let ((tmm-orig transient-mark-mode))
1188 (transient-mark-mode 1)
1189 (push-mark (point) t t)
1190 (forward-word 2)
1191 (call-interactively 'markdown-insert-inline-link-dwim)
1192 (should (string-equal (buffer-string) "[abc def]() ghi"))
1193 (should (= (point) 11))
1194 (transient-mark-mode tmm-orig))))
1196 ;;; Footnote tests:
1198 (ert-deftest test-markdown-footnote/basic-end ()
1199 "Basic footnote insertion and deletion tests for 'end location."
1200 (let ((markdown-footnote-location 'end))
1201 (markdown-test-string "first line\nsecond line\n"
1202 ;; new buffer with no footnotes
1203 (should (= markdown-footnote-counter 0))
1204 ;; footnote insertion
1205 (end-of-line)
1206 (markdown-insert-footnote)
1207 (should (= (point) 35))
1208 (should (= markdown-footnote-counter 1))
1209 (should (looking-back "\\[^1\\]: " nil))
1210 ;; kill with point in footnote definition
1211 (insert "footnote text")
1212 (let (kill-ring)
1213 (markdown-footnote-kill))
1214 (should (= (point) 24))
1215 (should (bolp))
1216 (should (string-equal (buffer-string) "first line\nsecond line\n"))
1217 ;; insertion, counter should increment
1218 (goto-char (point-min))
1219 (end-of-line)
1220 (markdown-insert-footnote)
1221 (should (= (point) 35))
1222 (should (= markdown-footnote-counter 2))
1223 (should (looking-back "\\[^2\\]: " nil))
1224 (insert "footnote text")
1225 ;; return to marker
1226 (markdown-footnote-return)
1227 (should (= (point) 15))
1228 (should (looking-back "\\[^2\\]" nil))
1229 ;; kill with point at marker
1230 (let (kill-ring)
1231 (markdown-footnote-kill))
1232 (should (= (point) 11))
1233 (should (eolp))
1234 (should (string-equal (buffer-string) "first line\nsecond line\n")))))
1236 (ert-deftest test-markdown-footnote/basic-immediately ()
1237 "Basic footnote insertion and deletion tests for 'immediately location."
1238 (let ((markdown-footnote-location 'immediately))
1239 (markdown-test-string "first paragraph\n\nsecond paragraph\n"
1240 ;; new buffer with no footnotes
1241 (should (= markdown-footnote-counter 0))
1242 ;; footnote insertion
1243 (end-of-line)
1244 (markdown-insert-footnote)
1245 (should (= (point) 28))
1246 (should (= markdown-footnote-counter 1))
1247 (should (looking-back "\\[^1\\]: " nil))
1248 ;; kill with point in footnote definition
1249 (insert "footnote text")
1250 (let (kill-ring)
1251 (markdown-footnote-kill))
1252 (should (= (point) 18))
1253 (should (bolp))
1254 (should (string-equal (buffer-string)
1255 "first paragraph\n\nsecond paragraph\n")))))
1257 (ert-deftest test-markdown-footnote/basic-header ()
1258 "Basic footnote insertion and deletion tests for 'header location."
1259 (let ((markdown-footnote-location 'header))
1260 (markdown-test-string "par one\n\npar two\n\n### header\n"
1261 ;; new buffer with no footnotes
1262 (should (= markdown-footnote-counter 0))
1263 ;; footnote insertion
1264 (end-of-line)
1265 (markdown-insert-footnote)
1266 (should (= (point) 29))
1267 (should (= markdown-footnote-counter 1))
1268 (should (looking-back "\\[^1\\]: " nil))
1269 ;; kill with point in footnote definition
1270 (insert "footnote text")
1271 (let (kill-ring)
1272 (markdown-footnote-kill))
1273 (should (= (point) 19))
1274 (should (bolp))
1275 (should (string-equal (buffer-string)
1276 "par one\n\npar two\n\n### header\n"))
1277 ;; insertion, counter should increment
1278 (goto-char (point-min))
1279 (end-of-line)
1280 (markdown-insert-footnote)
1281 (should (= (point) 29))
1282 (should (= markdown-footnote-counter 2))
1283 (should (looking-back "\\[^2\\]: " nil))
1284 (insert "footnote text")
1285 ;; return to marker
1286 (markdown-footnote-return)
1287 (should (= (point) 12))
1288 (should (looking-back "\\[^2\\]" nil))
1289 ;; kill with point at marker
1290 (let (kill-ring)
1291 (markdown-footnote-kill))
1292 (should (= (point) 8))
1293 (should (eolp))
1294 (should (string-equal (buffer-string)
1295 "par one\n\npar two\n\n### header\n")))))
1297 (ert-deftest test-markdown-footnote/kill-empty-text ()
1298 "Test killing a footnote with marker but no text."
1299 (markdown-test-string "no text[^1]\n\n[^1]: \n"
1300 (end-of-line)
1301 (markdown-footnote-goto-text)
1302 (should (looking-back "\\[^1\\]: " nil))
1303 (let (kill-ring)
1304 (markdown-footnote-kill))
1305 (should (string-equal (buffer-string) "no text\n"))))
1307 (ert-deftest test-markdown-footnote/kill-empty-after ()
1308 "Test killing an empty footnote after one with text (previously killed the
1309 footnote with text above)."
1310 (markdown-test-string "[^with-text][^no-text]\n\n[^with-text]: Text\n[^no-text]:"
1311 (let (kill-ring)
1312 (forward-line 3)
1313 (should (looking-at "\\[\\^no-text\\]:$"))
1314 (markdown-footnote-kill)
1315 (should (string-equal (current-kill 0) "")))))
1317 (ert-deftest test-markdown-footnote/kill-hanging-paras ()
1318 "Test killing a footnote where block text starts after the label (previously
1319 killed the footnote above)."
1320 (markdown-test-string "[^1][^2]\n\n[^1]: Foo\n\n[^2]:\n Text\n\n More text\n\n\nNot indented"
1321 (let (kill-ring)
1322 (forward-line 4)
1323 (should (looking-at "\\[\\^2\\]:$"))
1324 (markdown-footnote-kill)
1325 ;; We want to include the leading space on hanging footnote paragraphs,
1326 ;; even if a hanging paragraph is the first item in the footnote.
1327 (should (string-equal (current-kill 0) "Text\n\n More text\n")))))
1329 (ert-deftest test-markdown-footnote/text-positions-buffer-top ()
1330 "Test markdown-footnote-text-positions on footnote adjacent to buffer top
1331 (was infinite loop)."
1332 (markdown-test-string "[^label]: text\n more text"
1333 (should (equal (markdown-footnote-text-positions) (list "^label" 1 29)))))
1335 (ert-deftest test-markdown-footnote/text-positions-buffer-top-one-line ()
1336 "Test markdown-footnote-text-positions on one-line footnote adjacent to
1337 buffer top (failed to find positions)."
1338 (markdown-test-string "[^label]: text\n"
1339 (should (equal (markdown-footnote-text-positions) (list "^label" 1 16)))))
1341 (ert-deftest test-markdown-footnote/text-positions-buffer-top-not-footnote ()
1342 "Test markdown-footnote-text-positions on plain paragraph adjacent to buffer
1343 top (was infinite loop)."
1344 (markdown-test-string "text\n more text\n"
1345 (should (eq (markdown-footnote-text-positions) nil))))
1347 (ert-deftest test-markdown-footnote/text-positions-buffer-bottom ()
1348 "Test markdown-footnote-text-positions on footnote adjacent to buffer bottom
1349 (was infinite loop)."
1350 (markdown-test-string "\n[^label]: text\n more text"
1351 (forward-line 1)
1352 (should (equal (markdown-footnote-text-positions) (list "^label" 2 30)))))
1354 (ert-deftest test-markdown-footnote/kill-adjacent-footnote ()
1355 "Test killing a footnote adjacent to other one-line footnotes (previously
1356 killed the wrong one)."
1357 (markdown-test-string "Text[^1] with[^2] footnotes[^3]\n\n[^1]: foo\n[^2]: bar\n[^3]: baz"
1358 (let (kill-ring)
1359 (forward-line 3)
1360 (should (looking-at "\\[\\^2\\]: bar"))
1361 (markdown-footnote-kill)
1362 (should (string-equal (current-kill 0) "bar\n")))))
1364 (ert-deftest test-markdown-footnote/kill-adjacent-markers ()
1365 "Test killing a footnote where the labels are adjacent (previously, the wrong
1366 footnote would be killed because the attempt to jump to the marker would jump to
1367 the opening bracket of [^2], and then subsequent functions would kill [^2])."
1368 (markdown-test-string "Text with footnotes[^1][^2]\n\n[^1]: foo\n\n[^2]: bar\n"
1369 (let (kill-ring)
1370 (forward-line 2)
1371 (should (looking-at "\\[\\^1\\]: foo"))
1372 (markdown-footnote-kill)
1373 (should (string-equal (current-kill 0) "foo\n")))))
1375 (when (version< emacs-version "24.2")
1376 ;; fix segfault on 24.1 with the normal implementation of this function. isn't
1377 ;; exactly correct, but should make tests work the same
1378 (defadvice kill-buffer-and-window (around markdown-test-fix-segfault activate)
1379 (kill-buffer)
1380 (select-window (previous-window))))
1382 (ert-deftest test-markdown-footnote-reference/jump ()
1383 "Test `markdown-jump' for footnotes and reference links."
1384 (markdown-test-string
1385 "body[^1], [link 1][ref],
1386 [link 2][ref]
1388 [^1]: footnote
1390 [ref]: https://duckduckgo.com/"
1391 (goto-char 5) ; start of [^1]
1392 (markdown-jump) ; markdown-footnote-goto-text
1393 (should (looking-at "footnote"))
1394 (markdown-jump) ; markdown-footnote-return
1395 (should (= (point) 9)) ; just after [^1]
1396 (markdown-next-link) ; beginning of [link 1][]
1397 (markdown-jump)
1398 (should (looking-at "https://duckduckgo.com/"))
1399 (should (equal (markdown-reference-find-links "ref")
1400 (list (list "link 2" 26 2) (list "link 1" 11 1))))
1401 (markdown-jump) ; opens a reference link buffer
1402 (should (string= (buffer-string) "Links using reference ref:\n\nlink 1 (line 1)\nlink 2 (line 2)\n"))
1403 (should (looking-at "link 1")) ; in reference link popop buffer
1404 (execute-kbd-macro (read-kbd-macro "RET")) ; jump to "link 1"
1405 (should (looking-at "\\[link 1\\]")) ; back in main buffer
1406 (should (= (point) 11))))
1408 ;;; Element removal tests:
1410 (ert-deftest test-markdown-kill/simple ()
1411 "Simple tests for `markdown-kill-thing-at-point'."
1412 (let ((kill-ring nil)
1413 (tests (list '("`foo`" . "foo")
1414 '("## foo ##" . "foo")
1415 '("## foo" . "foo")
1416 '("foo\n---" . "foo")
1417 '("foo\n===" . "foo")
1418 '("* * * * *" . "* * * * *")
1419 '("[foo](http://bar.com/)" . "foo")
1420 '("![foo](http://bar.com/)" . "foo")
1421 '("[foo][bar]" . "foo")
1422 '("![foo][bar]" . "foo")
1423 '("<http://foo.com/>" . "http://foo.com/")
1424 '("<foo@bar.com>" . "foo@bar.com")
1425 '("**foo**" . "foo")
1426 '("__foo__" . "foo")
1427 '("*foo*" . "foo")
1428 '("_foo_" . "foo")
1429 '(" [foo]: http://bar.com/" . "http://bar.com/")
1430 '(" [foo]: http://bar.com/ \"title\"" . "http://bar.com/")
1431 '("foo[^bar]\n\n[^bar]: baz" . "baz")
1432 '("[^bar]: baz" . "baz")
1433 '(" * foo\n bar" . " * foo\n bar"))))
1434 (dolist (test tests)
1435 ;; Load test string (the car), move to end of first line, kill
1436 ;; thing at point, and then verify that the kill ring contains cdr.
1437 (markdown-test-string (car test)
1438 (end-of-line)
1439 (call-interactively 'markdown-kill-thing-at-point)
1440 (should (string-equal (current-kill 0) (cdr test)))))))
1442 (ert-deftest test-markdown-kill/footnote-text ()
1443 "Test killing a footnote with point at footnote text."
1444 (markdown-test-string "some text[^1]\n\n[^1]: footnote\n"
1445 (end-of-line)
1446 (markdown-footnote-goto-text)
1447 (let (kill-ring)
1448 (markdown-footnote-kill))
1449 (should (string-equal (buffer-string) "some text\n"))))
1451 (ert-deftest test-markdown-kill/code ()
1452 "Test killing with code regex.."
1453 (let ((kill-ring nil))
1454 (markdown-test-string "Lorem `ipsum` dolor `sit` `amet`."
1455 (goto-char 22) ; position point at s in `sit`
1456 (call-interactively 'markdown-kill-thing-at-point)
1457 (should (string-equal (current-kill 0) "sit")))))
1459 ;;; Completion:
1461 (ert-deftest test-markdown-complete/atx-header-incomplete ()
1462 "Test `markdown-incomplete-atx-p'."
1463 (markdown-test-string "### ###"
1464 (should (looking-at markdown-regex-header-atx))
1465 (should-not (markdown-incomplete-atx-p)))
1466 (markdown-test-string "###abc###"
1467 (should-not (looking-at markdown-regex-header-atx)))
1468 (markdown-test-string "### ###"
1469 (should (looking-at markdown-regex-header-atx))
1470 (should (markdown-incomplete-atx-p))))
1472 (ert-deftest test-markdown-complete/atx-header ()
1473 "Test `markdown-complete' for atx headers."
1474 (markdown-test-string "##### test"
1475 (call-interactively 'markdown-complete)
1476 (should (string-equal (buffer-string) "##### test #####"))))
1478 (ert-deftest test-markdown-complete/setext-header-incomplete ()
1479 "Test `markdown-incomplete-setext-p'."
1480 (markdown-test-string "abc\n===\n"
1481 (should (looking-at markdown-regex-header-setext))
1482 (should-not (markdown-incomplete-setext-p)))
1483 (markdown-test-string "abc\n==\n"
1484 (should (looking-at markdown-regex-header-setext))
1485 (should (markdown-incomplete-setext-p)))
1486 (markdown-test-string "abc\n====\n"
1487 (should (looking-at markdown-regex-header-setext))
1488 (should (markdown-incomplete-setext-p))))
1490 (ert-deftest test-markdown-complete/setext-header ()
1491 "Test `markdown-complete' for setext headers."
1492 (markdown-test-string "test \n=="
1493 (call-interactively 'markdown-complete)
1494 (should (string-equal (buffer-string) "test\n===="))))
1496 (ert-deftest test-markdown-complete/hr-incomplete ()
1497 "Test `markdown-incomplete-hr-p'."
1498 (dolist (i (number-sequence 0 (1- (length markdown-hr-strings))))
1499 (markdown-test-string (nth i markdown-hr-strings)
1500 (should (looking-at markdown-regex-hr))
1501 (should-not (markdown-incomplete-hr-p))
1502 (should-error (call-interactively 'markdown-complete)))))
1504 (ert-deftest test-markdown-complete/hr ()
1505 "Test completion via `markdown-complete' for horizontal rules."
1506 (markdown-test-string "- - - - -"
1507 (call-interactively 'markdown-complete)
1508 (should (string-equal (buffer-string) (car markdown-hr-strings)))))
1510 (ert-deftest test-markdown-complete/buffer-setext-2 ()
1511 "Test `markdown-complete-buffer' for level two setext heading."
1512 ;; Ensure markdown-complete-buffer doesn't mistake this for a horizontal rule
1513 (markdown-test-string "Subheading\n--\n"
1514 (call-interactively 'markdown-complete-buffer)
1515 (should (string-equal (buffer-string) "Subheading\n----------\n\n")))
1516 (markdown-test-string "Abc\n--\n\nDef\n--\n"
1517 (call-interactively 'markdown-complete-buffer)
1518 (should (string-equal (buffer-string) "Abc\n---\n\nDef\n---\n\n"))))
1520 ;;; Promotion and demotion tests:
1522 (ert-deftest test-markdown-promote/atx-header ()
1523 "Test `markdown-promote' for atx headers."
1524 (markdown-test-string "###### test ######"
1525 (markdown-promote)
1526 (should (string-equal (buffer-string) "##### test #####"))
1527 (markdown-promote)
1528 (should (string-equal (buffer-string) "#### test ####"))
1529 (markdown-promote)
1530 (should (string-equal (buffer-string) "### test ###"))
1531 (markdown-promote)
1532 (should (string-equal (buffer-string) "## test ##"))
1533 (markdown-promote)
1534 (should (string-equal (buffer-string) "# test #"))))
1536 (ert-deftest test-markdown-demote/atx-header ()
1537 "Test `markdown-demote' for atx headers."
1538 (markdown-test-string "# test #"
1539 (markdown-demote)
1540 (should (string-equal (buffer-string) "## test ##"))
1541 (markdown-demote)
1542 (should (string-equal (buffer-string) "### test ###"))
1543 (markdown-demote)
1544 (should (string-equal (buffer-string) "#### test ####"))
1545 (markdown-demote)
1546 (should (string-equal (buffer-string) "##### test #####"))
1547 (markdown-demote)
1548 (should (string-equal (buffer-string) "###### test ######"))))
1550 (ert-deftest test-markdown-promote/setext-header ()
1551 "Test `markdown-promote' for setext headers."
1552 (markdown-test-string "test\n----"
1553 (markdown-promote)
1554 (should (string-equal (buffer-string) "test\n===="))))
1556 (ert-deftest test-markdown-demote/setext-header ()
1557 "Test `markdown-demote' for setext headers."
1558 (markdown-test-string "test\n===="
1559 (markdown-demote)
1560 (should (string-equal (buffer-string) "test\n----"))
1561 (markdown-demote)
1562 (should (string-equal (buffer-string) "### test ###"))
1563 (markdown-demote)
1564 (should (string-equal (buffer-string) "#### test ####"))
1565 (markdown-demote)
1566 (should (string-equal (buffer-string) "##### test #####"))
1567 (markdown-demote)
1568 (should (string-equal (buffer-string) "###### test ######"))))
1570 (ert-deftest test-markdown-promote/hr ()
1571 "Test `markdown-promote' for horizontal rules."
1572 (markdown-test-string (car (reverse markdown-hr-strings))
1573 (dolist (n (number-sequence 4 0 -1))
1574 (markdown-promote)
1575 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))
1577 (ert-deftest test-markdown-demote/hr ()
1578 "Test `markdown-demote' for horizontal rules."
1579 (markdown-test-string (car markdown-hr-strings)
1580 (dolist (n (number-sequence 1 5))
1581 (markdown-demote)
1582 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))
1584 (ert-deftest test-markdown-promote/bold ()
1585 "Test `markdown-promote' for bold markup."
1586 (markdown-test-string "__bold__"
1587 (call-interactively 'markdown-promote)
1588 (should (string-equal (buffer-string) "**bold**"))))
1590 (ert-deftest test-markdown-demote/bold ()
1591 "Test `markdown-demote' for bold markup."
1592 (markdown-test-string "**bold**"
1593 (call-interactively 'markdown-promote)
1594 (should (string-equal (buffer-string) "__bold__"))))
1596 (ert-deftest test-markdown-promote/italic ()
1597 "Test `markdown-promote' for italic markup."
1598 (markdown-test-string "_italic_"
1599 (call-interactively 'markdown-promote)
1600 (should (string-equal (buffer-string) "*italic*"))))
1602 (ert-deftest test-markdown-demote/italic ()
1603 "Test `markdown-demote' for italic markup."
1604 (markdown-test-string "*italic*"
1605 (call-interactively 'markdown-promote)
1606 (should (string-equal (buffer-string) "_italic_"))))
1608 ;;; Subtree editing tests:
1610 (ert-deftest test-markdown-subtree/promote ()
1611 "Test `markdown-promote-subtree'."
1612 (markdown-test-string "# h1 #\n\n## h2 ##\n\n### h3 ###\n\n## h2 ##\n\n# h1 #\n"
1613 ;; The first h1 should get promoted away.
1614 ;; The second h1 should not be promoted.
1615 (markdown-promote-subtree)
1616 (should (string-equal (buffer-string) "h1\n\n# h2 #\n\n## h3 ##\n\n# h2 #\n\n# h1 #\n"))
1617 ;; Second call should do nothing since point is no longer at a heading.
1618 (markdown-promote-subtree)
1619 (should (string-equal (buffer-string) "h1\n\n# h2 #\n\n## h3 ##\n\n# h2 #\n\n# h1 #\n"))
1620 ;; Move to h2 and promote again.
1621 (forward-line 2)
1622 (markdown-promote-subtree)
1623 (should (string-equal (buffer-string) "h1\n\nh2\n\n# h3 #\n\n# h2 #\n\n# h1 #\n"))))
1625 (ert-deftest test-markdown-subtree/demote ()
1626 "Test `markdown-demote-subtree'."
1627 (markdown-test-string "# h1 #\n\n## h2 ##\n\n### h3 ###\n\n## h2 ##\n\n# h1 #\n"
1628 ;; The second h1 should not be demoted
1629 (markdown-demote-subtree)
1630 (should (string-equal (buffer-string) "## h1 ##\n\n### h2 ###\n\n#### h3 ####\n\n### h2 ###\n\n# h1 #\n"))
1631 (markdown-demote-subtree)
1632 (should (string-equal (buffer-string) "### h1 ###\n\n#### h2 ####\n\n##### h3 #####\n\n#### h2 ####\n\n# h1 #\n"))
1633 (markdown-demote-subtree)
1634 (should (string-equal (buffer-string) "#### h1 ####\n\n##### h2 #####\n\n###### h3 ######\n\n##### h2 #####\n\n# h1 #\n"))
1635 ;; Stop demoting at level six
1636 (markdown-demote-subtree)
1637 (should (string-equal (buffer-string) "##### h1 #####\n\n###### h2 ######\n\n###### h3 ######\n\n###### h2 ######\n\n# h1 #\n"))
1638 (markdown-demote-subtree)
1639 (should (string-equal (buffer-string) "###### h1 ######\n\n###### h2 ######\n\n###### h3 ######\n\n###### h2 ######\n\n# h1 #\n"))))
1641 (ert-deftest test-markdown-subtree/move-up ()
1642 "Test `markdown-move-subtree-up'."
1643 ;; Note that prior to Emacs 24.5, this does not work for the last subtree in
1644 ;; the buffer due to Emacs bug #19102:
1645 ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19102
1646 ;; https://github.com/emacs-mirror/emacs/commit/b3910f
1647 ;; That also corrects the type of the "Cannot move pase superior level" error
1648 ;; from 'error to 'user-error.
1649 (markdown-test-string "# 1 #\n\n## 1.1 ##\n\n### 1.1.1 ###\n\n## 1.2 ##\n\n### 1.2.1 ###\n\n# 2 #\n# Extra\n"
1650 (re-search-forward "^# 2")
1651 (markdown-move-subtree-up)
1652 (should (string-equal (buffer-string) "# 2 #\n# 1 #\n\n## 1.1 ##\n\n### 1.1.1 ###\n\n## 1.2 ##\n\n### 1.2.1 ###\n\n# Extra\n"))
1653 ;; Second attempt should fail, leaving buffer unchanged.
1654 ;; (This way of asserting the contents of the error
1655 ;; message is a bit convoluted and more fragile than
1656 ;; ideal. But prior to Emacs 24.5, the type of this
1657 ;; error is just 'error, and a bare "should-error" is
1658 ;; really overly broad.)
1659 (should (string-equal
1660 "Cannot move past superior level"
1661 (cl-second (should-error (markdown-move-subtree-up)))))))
1663 (ert-deftest test-markdown-subtree/move-down ()
1664 "Test `markdown-move-subtree-down'."
1665 (markdown-test-string "# 1 #\n\n## 1.1 ##\n\n### 1.1.1 ###\n\n## 1.2 ##\n\n### 1.2.1 ###\n\n# 2 #\n"
1666 (re-search-forward "^## 1\.1")
1667 (markdown-move-subtree-down)
1668 (should (string-equal (buffer-string) "# 1 #\n\n## 1.2 ##\n\n### 1.2.1 ###\n\n## 1.1 ##\n\n### 1.1.1 ###\n\n# 2 #\n"))))
1670 ;(ert-deftest test-markdown-subtree/move-down ()
1672 ;;; Cycling:
1674 (ert-deftest test-markdown-cycle/atx-header ()
1675 "Test `markdown-demote' cycling for atx headers."
1676 (markdown-test-string "##### test"
1677 (call-interactively 'markdown-demote)
1678 (should (string-equal (buffer-string) "###### test ######"))
1679 (call-interactively 'markdown-demote)
1680 (should (string-equal (buffer-string) "# test #"))
1681 (call-interactively 'markdown-demote)
1682 (should (string-equal (buffer-string) "## test ##"))))
1684 (ert-deftest test-markdown-cycle/setext-header ()
1685 "Test `markdown-demote' cycling for setext headers."
1686 (markdown-test-string "test\n===="
1687 (call-interactively 'markdown-demote)
1688 (should (string-equal (buffer-string) "test\n----"))
1689 (call-interactively 'markdown-demote)
1690 (should (string-equal (buffer-string) "### test ###"))
1691 (call-interactively 'markdown-demote)
1692 (should (string-equal (buffer-string) "#### test ####"))
1693 (call-interactively 'markdown-demote)
1694 (should (string-equal (buffer-string) "##### test #####"))
1695 (call-interactively 'markdown-demote)
1696 (should (string-equal (buffer-string) "###### test ######"))
1697 (call-interactively 'markdown-demote)
1698 (should (string-equal (buffer-string) "# test #"))))
1700 (ert-deftest test-markdown-cycle/hr ()
1701 "Test cycling of horizontal rules."
1702 ;; Cycle using markdown-demote
1703 (markdown-test-string (car markdown-hr-strings)
1704 (dolist (n (number-sequence 1 5))
1705 (call-interactively 'markdown-demote)
1706 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
1707 (call-interactively 'markdown-demote)
1708 (should (string-equal (buffer-string) (car markdown-hr-strings))))
1709 ;; Cycle using markdown-promote
1710 (markdown-test-string (car (reverse markdown-hr-strings))
1711 (dolist (n (number-sequence 4 0 -1))
1712 (call-interactively 'markdown-promote)
1713 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
1714 (call-interactively 'markdown-promote)
1715 (should (string-equal (buffer-string) (car (reverse markdown-hr-strings))))))
1717 (ert-deftest test-markdown-cycle/bold ()
1718 "Test cycling of bold markup."
1719 (markdown-test-string "**bold**"
1720 (call-interactively 'markdown-demote)
1721 (should (string-equal (buffer-string) "__bold__"))
1722 (call-interactively 'markdown-demote)
1723 (should (string-equal (buffer-string) "**bold**"))))
1725 (ert-deftest test-markdown-cycle/italic ()
1726 "Test cycling of italic markup."
1727 (markdown-test-string "*italic*"
1728 (call-interactively 'markdown-demote)
1729 (should (string-equal (buffer-string) "_italic_"))
1730 (call-interactively 'markdown-demote)
1731 (should (string-equal (buffer-string) "*italic*"))))
1733 ;;; Indentation tests:
1735 (ert-deftest test-markdown-indentation/calc-indents ()
1736 "Test `markdown-calc-indents' a nested list context."
1737 (markdown-test-file "nested-list.text"
1738 (goto-char (point-max))
1739 (let ((indents (markdown-calc-indents)))
1740 (should (= (car indents) 17)) ; indentation of previous line first
1741 (should (equal (sort indents '<)
1742 (list
1743 0 ; beginning of line
1744 3 ; first-level list marker
1745 7 ; second-level list marker
1746 11 ; third-level list marker
1747 13 ; previous list item text
1748 16 ; pre-block indentation
1749 17 ; indentation of previous line
1750 21 ; previous line plus tab-width
1751 ))))))
1753 (ert-deftest test-markdown-indentation/indent-region ()
1754 "Test `markdown-indent-region'."
1755 ;; Basic test with multiple lines
1756 (markdown-test-string "abc\ndef\nghi\n"
1757 (markdown-indent-region (point-min) (point-max) nil)
1758 (should (string-equal (buffer-string) " abc\n def\n ghi\n")))
1759 ;; Following a list item
1760 (markdown-test-string " * abc\ndef\n"
1761 (forward-line)
1762 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
1763 (should (string-equal (buffer-string) " * abc\n def\n"))
1764 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
1765 (should (string-equal (buffer-string) " * abc\n def\n"))))
1767 (ert-deftest test-markdown-indentation/indent-list-hanging ()
1768 "Test `markdown-indent-line' with hanging list item."
1769 (markdown-test-string
1770 "- list
1771 - nested list with long lines which need to be
1772 hard wrapped"
1773 (goto-char (point-max))
1774 (markdown-enter-key)
1775 (should (eq (point) 78))))
1777 (ert-deftest test-markdown-indentation/indent-list-single ()
1778 "Test `markdown-indent-line' with single list item."
1779 (markdown-test-string
1780 " * item 1"
1781 (end-of-line)
1782 (markdown-enter-key)
1783 (should (string-equal (buffer-string) " * item 1\n "))
1784 (should (eq (point) 14))))
1786 (ert-deftest test-markdown-indentation/indent-pre ()
1787 "Test `markdown-indent-line' with a pre block."
1788 (markdown-test-string
1789 "I'm gonna write a code block:
1791 my first line of code"
1792 (goto-char (point-max))
1793 (markdown-enter-key)
1794 (should (eq (point) 62))
1795 (should (looking-back "^ "))))
1797 ;;; Font lock tests:
1799 (ert-deftest test-markdown-font-lock/italics-1 ()
1800 "A simple italics test."
1801 (markdown-test-file "inline.text"
1802 (goto-char 9)
1803 (should (looking-at "\*"))
1804 ;; Check face of char before leading asterisk
1805 (markdown-test-range-has-face 8 8 nil)
1806 ;; Check face of italic range
1807 (markdown-test-range-has-face 9 9 markdown-markup-face)
1808 (markdown-test-range-has-face 10 16 markdown-italic-face)
1809 (markdown-test-range-has-face 17 17 markdown-markup-face)
1810 ;; Check face of point past leading asterisk
1811 (markdown-test-range-has-face 18 18 nil)))
1813 (ert-deftest test-markdown-font-lock/italics-2 ()
1814 "Test space after leading asterisk or underscore."
1815 (markdown-test-string
1816 "This is * not italic*, nor _ is this_."
1817 (markdown-test-range-has-face (point-min) (point-max) nil)))
1819 (ert-deftest test-markdown-font-lock/italics-3 ()
1820 "Test that slash inside asterisks is not italic."
1821 (markdown-test-string
1822 "not italic *\\*"
1823 (markdown-test-range-has-face (point-min) (point-max) nil)))
1825 (ert-deftest test-markdown-font-lock/italics-4 ()
1826 "Test that escaped asterisk inside italics is not bold."
1827 (markdown-test-string
1828 "italic **\\**"
1829 (markdown-test-range-has-face 1 7 nil)
1830 (markdown-test-range-has-face 8 8 markdown-markup-face)
1831 (markdown-test-range-has-face 9 11 markdown-italic-face)
1832 (markdown-test-range-has-face 12 12 markdown-markup-face)))
1834 (ert-deftest test-markdown-font-lock/italics-5 ()
1835 "Test italic single letter."
1836 (markdown-test-string
1837 "*a*"
1838 (markdown-test-range-has-face 1 1 markdown-markup-face)
1839 (markdown-test-range-has-face 2 2 markdown-italic-face)
1840 (markdown-test-range-has-face 3 3 markdown-markup-face)))
1842 (ert-deftest test-markdown-font-lock/italics-6 ()
1843 "Test multiline italics across list items."
1844 (markdown-test-string
1845 "* something about function foo_bar
1846 * something else about foo_bar"
1847 (markdown-test-range-has-face 31 34 nil)
1848 (markdown-test-range-has-face 38 62 nil))
1849 (markdown-test-string
1850 "* something about function
1851 foo_bar
1852 * something else about
1853 foo_bar"
1854 (markdown-test-range-has-face 30 36 nil)
1855 (markdown-test-range-has-face 63 69 nil))
1856 (markdown-test-string
1857 "foo_bar
1858 * foo_bar"
1859 (markdown-test-range-has-face 4 7 nil)
1860 (markdown-test-range-has-face 11 14 nil)))
1862 (ert-deftest test-markdown-font-lock/italics-7 ()
1863 "Underscores in URLs should not trigger italics."
1864 :expected-result :failed
1865 (markdown-test-string
1866 "<http://jblevins.org/research/centroid/cd_z_path.m>"
1867 (markdown-test-range-face-equals 2 50 markdown-link-face))
1868 (markdown-test-string
1869 "[1]: http://jblevins.org/research/centroid/cd_z_path.m"
1870 (markdown-test-range-face-equals 6 54 markdown-url-face))
1871 (markdown-test-string
1872 "[cd\\_z\\_path.m](http://jblevins.org/research/centroid/cd_z_path.m)"
1873 (markdown-test-range-face-equals 17 65 markdown-url-face)))
1875 (ert-deftest test-markdown-font-lock/italics-after-hr ()
1876 "Test italics after a horizontal rule with asterisks."
1877 (markdown-test-string "* * *\n\n*italic*\n"
1878 (markdown-test-range-has-face 1 5 markdown-header-delimiter-face)
1879 (markdown-test-range-has-face 8 8 markdown-markup-face)
1880 (markdown-test-range-has-face 9 14 markdown-italic-face)
1881 (markdown-test-range-has-face 15 15 markdown-markup-face)))
1883 (ert-deftest test-markdown-font-lock/italics-in-heading ()
1884 "Test italic overlay in a heading."
1885 (markdown-test-string
1886 "# *Italics* in a Heading"
1887 (markdown-test-range-has-face 3 3 markdown-markup-face)
1888 (markdown-test-range-has-face 4 10 markdown-italic-face)
1889 (markdown-test-range-has-face 11 11 markdown-markup-face)))
1891 (ert-deftest test-markdown-font-lock/italics-link ()
1892 "Test italic overlay in an inline link."
1893 (markdown-test-string
1894 "*[italic link](http://www.link.com/)*"
1895 (markdown-test-range-has-face 1 1 markdown-markup-face)
1896 (markdown-test-range-has-face 2 36 markdown-italic-face)
1897 (markdown-test-range-has-face 37 37 markdown-markup-face))
1898 (markdown-test-string
1899 "[*italic link*](http://www.link.com/)"
1900 (markdown-test-range-has-face 2 2 markdown-markup-face)
1901 (markdown-test-range-has-face 3 13 markdown-italic-face)
1902 (markdown-test-range-has-face 14 14 markdown-markup-face)))
1904 (ert-deftest test-markdown-font-lock/italics-in-blockquote ()
1905 "Test italics overlay in a blockquote."
1906 (markdown-test-string
1907 "> *italics* inside a blockquote"
1908 (markdown-test-range-has-face 3 3 markdown-markup-face)
1909 (markdown-test-range-has-face 4 10 markdown-italic-face)
1910 (markdown-test-range-has-face 11 11 markdown-markup-face)))
1912 (ert-deftest test-markdown-font-lock/italics-in-pre ()
1913 "Test italics overlay in a blockquote."
1914 (markdown-test-string
1915 " *italics* inside a pre block"
1916 (markdown-test-range-has-face (point-min) (1- (point-max))
1917 markdown-pre-face)))
1919 (ert-deftest test-markdown-font-lock/italics-and-code ()
1920 "Test seeming italics mixed with code."
1921 (markdown-test-string
1922 "define `var_1` and `var_2` inline code"
1923 (markdown-test-range-has-face 9 13 markdown-inline-code-face)
1924 (markdown-test-range-has-face 21 25 markdown-inline-code-face))
1925 (markdown-test-string
1926 "`var_1` and var_2"
1927 (markdown-test-range-has-face 2 6 markdown-inline-code-face)
1928 (markdown-test-range-has-face 8 17 nil))
1929 (markdown-test-string
1930 "var_1 and `var_2`"
1931 (markdown-test-range-has-face 1 10 nil)
1932 (markdown-test-range-has-face 12 16 markdown-inline-code-face)))
1934 (ert-deftest test-markdown-font-lock/italics-and-code ()
1935 "Test seeming italics mixed with code."
1936 (markdown-test-string
1937 "[lg]: twilight_sm.png\n[sm]: twilight_lg.png"
1938 (markdown-test-range-has-face 7 21 markdown-url-face)
1939 (markdown-test-range-has-face 22 22 nil)
1940 (markdown-test-range-has-face 29 43 markdown-url-face)
1941 (markdown-test-range-has-face 28 28 nil)))
1943 (ert-deftest test-markdown-font-lock/bold-1 ()
1944 "A simple bold test."
1945 (markdown-test-file "inline.text"
1946 (goto-char 27)
1947 (should (looking-at "\*\*"))
1948 ;; Check face of char before leading asterisk
1949 (markdown-test-range-has-face 26 26 nil)
1950 ;; Check face of opening asterisks
1951 (markdown-test-range-has-face 27 28 markdown-markup-face)
1952 ;; Check face of bold range
1953 (markdown-test-range-has-face 29 33 markdown-bold-face)
1954 ;; Check face of closing asterisks
1955 (markdown-test-range-has-face 34 35 markdown-markup-face)
1956 ;; Check face of point past leading asterisk
1957 (markdown-test-range-has-face 36 36 nil)))
1959 (ert-deftest test-markdown-font-lock/bold-2 ()
1960 "Test space after leading asterisks or underscores."
1961 (markdown-test-string
1962 "This is ** not bold**, nor __ is this__ (but they match italics)."
1963 (markdown-test-range-has-face 1 8 nil)
1964 (markdown-test-range-has-face 9 9 markdown-markup-face)
1965 (markdown-test-range-has-face 10 19 markdown-italic-face)
1966 (markdown-test-range-has-face 20 20 markdown-markup-face)
1967 (markdown-test-range-has-face 21 27 nil)
1968 (markdown-test-range-has-face 28 28 markdown-markup-face)
1969 (markdown-test-range-has-face 29 37 markdown-italic-face)
1970 (markdown-test-range-has-face 38 38 markdown-markup-face)
1971 (markdown-test-range-has-face 39 (point-max) nil)))
1973 (ert-deftest test-markdown-font-lock/bold-3 ()
1974 "Test escaped asterisk inside bold."
1975 (markdown-test-string
1976 "bold **\\***"
1977 (markdown-test-range-has-face 1 5 nil)
1978 (markdown-test-range-has-face 6 7 markdown-markup-face)
1979 (markdown-test-range-has-face 8 9 markdown-bold-face)
1980 (markdown-test-range-has-face 10 11 markdown-markup-face)))
1982 (ert-deftest test-markdown-font-lock/bold-4 ()
1983 "Test bold single letter."
1984 (markdown-test-string
1985 "**a**"
1986 (markdown-test-range-has-face 1 2 markdown-markup-face)
1987 (markdown-test-range-has-face 3 3 markdown-bold-face)
1988 (markdown-test-range-has-face 4 5 markdown-markup-face)))
1990 (ert-deftest test-markdown-font-lock/bold-after-hr ()
1991 "Test bold after a horizontal rule with asterisks."
1992 (markdown-test-string "* * *\n\n**bold**\n"
1993 (markdown-test-range-has-face 1 5 markdown-header-delimiter-face)
1994 (markdown-test-range-has-face 8 9 markdown-markup-face)
1995 (markdown-test-range-has-face 10 13 markdown-bold-face)
1996 (markdown-test-range-has-face 14 15 markdown-markup-face)))
1998 (ert-deftest test-markdown-font-lock/bold-link ()
1999 "Test bold overlay in an inline link."
2000 (markdown-test-string
2001 "**[bold link](http://www.link.com/)**"
2002 (markdown-test-range-has-face 1 2 markdown-markup-face)
2003 (markdown-test-range-has-face 3 35 markdown-bold-face)
2004 (markdown-test-range-has-face 36 37 markdown-markup-face))
2005 (markdown-test-string
2006 "[**bold link**](http://www.link.com/)"
2007 (markdown-test-range-has-face 2 3 markdown-markup-face)
2008 (markdown-test-range-has-face 4 12 markdown-bold-face)
2009 (markdown-test-range-has-face 13 14 markdown-markup-face)))
2011 (ert-deftest test-markdown-font-lock/bold-in-blockquote ()
2012 "Test bold overlay in a blockquote."
2013 (markdown-test-string
2014 "> **bold** inside a blockquote"
2015 (markdown-test-range-has-face 3 4 markdown-markup-face)
2016 (markdown-test-range-has-face 5 8 markdown-bold-face)
2017 (markdown-test-range-has-face 9 10 markdown-markup-face)))
2019 (ert-deftest test-markdown-font-lock/bold-in-pre ()
2020 "Test bold overlay in a blockquote."
2021 (markdown-test-string
2022 " **bold** inside a pre block"
2023 (markdown-test-range-has-face (point-min) (1- (point-max))
2024 markdown-pre-face)))
2026 (ert-deftest test-markdown-font-lock/no-bold-in-code ()
2027 "Bold markers in inline code should not trigger bold."
2028 (markdown-test-string
2029 "`def __init__(self):`"
2030 (markdown-test-range-has-face 8 11 markdown-inline-code-face))
2031 (markdown-test-string
2032 "`**foo` bar `baz**`"
2033 (markdown-test-range-face-equals 2 6 markdown-inline-code-face)
2034 (markdown-test-range-face-equals 9 11 nil)
2035 (markdown-test-range-face-equals 14 18 markdown-inline-code-face)))
2037 (ert-deftest test-markdown-font-lock/no-bold-in-math ()
2038 "Bold markers in math should not trigger bold."
2039 (markdown-test-file "math.text"
2040 (markdown-toggle-math t)
2041 (funcall markdown-test-font-lock-function)
2042 (markdown-test-range-has-face 279 299 markdown-math-face)
2043 (markdown-test-range-has-face 301 308 nil)
2044 (markdown-test-range-has-face 310 312 markdown-math-face)))
2046 (ert-deftest test-markdown-font-lock/code-1 ()
2047 "A simple inline code test."
2048 (markdown-test-file "inline.text"
2049 (goto-char 45)
2050 (should (looking-at "`"))
2051 ;; Regular code span
2052 (markdown-test-range-has-face 45 45 markdown-markup-face)
2053 (markdown-test-range-has-face 46 49 markdown-inline-code-face)
2054 (markdown-test-range-has-face 50 50 markdown-markup-face)
2055 ;; Code containing backticks
2056 (markdown-test-range-has-face 61 62 markdown-markup-face)
2057 (markdown-test-range-has-face 63 87 markdown-inline-code-face)
2058 (markdown-test-range-has-face 88 89 markdown-markup-face)
2059 ;; Seven backquotes in a row
2060 (markdown-test-range-has-face 119 125 nil)
2061 ;; Backquotes at beginning or end
2062 (markdown-test-range-has-face 228 229 markdown-markup-face)
2063 (markdown-test-range-has-face 230 237 markdown-inline-code-face)
2064 (markdown-test-range-has-face 238 239 markdown-markup-face)
2065 (markdown-test-range-has-face 341 342 markdown-markup-face)
2066 (markdown-test-range-has-face 343 349 markdown-inline-code-face)
2067 (markdown-test-range-has-face 350 351 markdown-markup-face)
2068 ;; Backslash as final character
2069 (markdown-test-range-has-face 460 460 markdown-markup-face)
2070 (markdown-test-range-has-face 461 467 markdown-inline-code-face)
2071 (markdown-test-range-has-face 468 468 markdown-markup-face)
2072 ;; Escaping of leading backquotes
2073 (markdown-test-range-has-face 586 592 nil)
2074 (markdown-test-range-has-face 597 603 nil)
2075 ;; A code span crossing lines
2076 (markdown-test-range-has-face 652 656 nil)
2077 (markdown-test-range-has-face 657 657 markdown-markup-face)
2078 (markdown-test-range-has-face 658 665 markdown-inline-code-face)
2079 (markdown-test-range-has-face 666 666 markdown-markup-face)
2080 ;; Three backquotes: same line, across lines, not across blocks
2081 (markdown-test-range-has-face 695 748 nil)
2082 (markdown-test-range-has-face 749 750 markdown-markup-face)
2083 (markdown-test-range-has-face 751 755 markdown-inline-code-face)
2084 (markdown-test-range-has-face 756 757 markdown-markup-face)
2085 (markdown-test-range-has-face 758 805 nil)
2086 (markdown-test-range-has-face 806 807 markdown-markup-face)
2087 (markdown-test-range-has-face 808 812 markdown-inline-code-face)
2088 (markdown-test-range-has-face 813 814 markdown-markup-face)
2089 (markdown-test-range-has-face 815 891 nil)
2092 (ert-deftest test-markdown-font-lock/code-2 ()
2093 "Multiple code spans in a row and on different lines."
2094 (markdown-test-string "`foo` `bar` `baz`"
2095 (markdown-test-range-has-face 1 1 markdown-markup-face)
2096 (markdown-test-range-has-face 2 4 markdown-inline-code-face)
2097 (markdown-test-range-has-face 5 5 markdown-markup-face)
2098 (markdown-test-range-has-face 6 6 nil)
2099 (markdown-test-range-has-face 7 7 markdown-markup-face)
2100 (markdown-test-range-has-face 8 10 markdown-inline-code-face)
2101 (markdown-test-range-has-face 11 11 markdown-markup-face)
2102 (markdown-test-range-has-face 12 12 nil)
2103 (markdown-test-range-has-face 13 13 markdown-markup-face)
2104 (markdown-test-range-has-face 14 16 markdown-inline-code-face)
2105 (markdown-test-range-has-face 17 17 markdown-markup-face))
2106 (markdown-test-string "`a`\n`b`\n`c`\n"
2107 (markdown-test-range-has-face 1 1 markdown-markup-face)
2108 (markdown-test-range-has-face 2 2 markdown-inline-code-face)
2109 (markdown-test-range-has-face 3 3 markdown-markup-face)
2110 (markdown-test-range-has-face 4 4 nil)
2111 (markdown-test-range-has-face 5 5 markdown-markup-face)
2112 (markdown-test-range-has-face 6 6 markdown-inline-code-face)
2113 (markdown-test-range-has-face 7 7 markdown-markup-face)
2114 (markdown-test-range-has-face 8 8 nil)
2115 (markdown-test-range-has-face 9 9 markdown-markup-face)
2116 (markdown-test-range-has-face 10 10 markdown-inline-code-face)
2117 (markdown-test-range-has-face 11 11 markdown-markup-face)
2118 (markdown-test-range-has-face 12 12 nil))
2119 (markdown-test-string "a`foo`b`bar`c`baz`d"
2120 (markdown-test-range-has-face 1 1 nil)
2121 (markdown-test-range-has-face 2 2 markdown-markup-face)
2122 (markdown-test-range-has-face 3 5 markdown-inline-code-face)
2123 (markdown-test-range-has-face 6 6 markdown-markup-face)
2124 (markdown-test-range-has-face 7 7 nil)
2125 (markdown-test-range-has-face 8 8 markdown-markup-face)
2126 (markdown-test-range-has-face 9 11 markdown-inline-code-face)
2127 (markdown-test-range-has-face 12 12 markdown-markup-face)
2128 (markdown-test-range-has-face 13 13 nil)
2129 (markdown-test-range-has-face 14 14 markdown-markup-face)
2130 (markdown-test-range-has-face 15 17 markdown-inline-code-face)
2131 (markdown-test-range-has-face 18 18 markdown-markup-face)
2132 (markdown-test-range-has-face 19 19 nil)))
2134 (ert-deftest test-markdown-font-lock/code-3 ()
2135 "Backslashes don't escape backticks inside of inline code strings."
2136 (markdown-test-string
2137 "`foo\\`bar`"
2138 (markdown-test-range-has-face 1 1 markdown-markup-face)
2139 (markdown-test-range-has-face 2 5 markdown-inline-code-face)
2140 (markdown-test-range-has-face 6 6 markdown-markup-face)
2141 (markdown-test-range-has-face 7 10 nil)))
2143 (ert-deftest test-markdown-font-lock/code-link-precedence ()
2144 "Test that inline code takes precedence over inline links.
2145 Test currently fails because this case isn't handled properly."
2146 :expected-result :failed
2147 (markdown-test-string
2148 "[not a `link](/foo`)"
2149 (markdown-test-range-has-face 1 7 nil)
2150 (markdown-test-range-has-face 8 8 markdown-markup-face)
2151 (markdown-test-range-has-face 9 18 markdown-inline-code-face)
2152 (markdown-test-range-has-face 19 19 markdown-markup-face)
2153 (markdown-test-range-has-face 20 20 nil)))
2155 (ert-deftest test-markdown-font-lock/kbd ()
2156 "Test font lock for <kbd> tags."
2157 (markdown-test-string "<kbd>C-c <</kbd>"
2158 (markdown-test-range-has-face 1 5 markdown-markup-face)
2159 (markdown-test-range-has-face 6 10 markdown-inline-code-face)
2160 (markdown-test-range-has-face 11 16 markdown-markup-face))
2161 (markdown-test-string "To quit Emacs, press <kbd>C-x C-c</kbd>."
2162 (markdown-test-range-has-face 1 21 nil)
2163 (markdown-test-range-has-face 22 26 markdown-markup-face)
2164 (markdown-test-range-has-face 27 33 markdown-inline-code-face)
2165 (markdown-test-range-has-face 34 39 markdown-markup-face)
2166 (markdown-test-range-has-face 40 40 nil)))
2168 (ert-deftest test-markdown-font-lock/lists-1 ()
2169 "A simple list marker font lock test."
2170 (markdown-test-file "lists.text"
2171 (dolist (loc (list 1063 1283 1659 1830 1919 2150 2393 2484
2172 2762 2853 3097 3188 3700 3903 4009))
2173 (goto-char loc)
2174 (should (looking-at "[*+-]"))
2175 (markdown-test-range-has-face loc loc markdown-list-face))))
2177 (ert-deftest test-markdown-font-lock/definition-list ()
2178 "A simple definition list marker font lock test."
2179 (markdown-test-file "definition-list.text"
2180 (markdown-test-range-has-face 7 7 'markdown-list-face)
2181 (markdown-test-range-has-face 29 52 'markdown-pre-face)
2182 (markdown-test-range-has-face 55 55 'markdown-list-face)))
2184 (ert-deftest test-markdown-font-lock/pre-1 ()
2185 "Nested list and pre block font lock test."
2186 (markdown-test-file "nested-list.text"
2187 (dolist (loc (list 4 29 194 224 491 525))
2188 (markdown-test-range-has-face loc loc markdown-list-face))
2189 (markdown-test-range-has-face 6 25 nil)
2190 (markdown-test-range-has-face 31 83 nil)
2191 (markdown-test-range-has-face 85 154 markdown-pre-face)
2192 (markdown-test-range-has-face 157 189 nil)
2193 (markdown-test-range-has-face 196 215 nil)
2194 (markdown-test-range-has-face 226 403 nil)
2195 (markdown-test-range-has-face 405 481 markdown-pre-face)
2196 (markdown-test-range-has-face 493 512 nil)
2197 (markdown-test-range-has-face 527 546 nil)
2198 (markdown-test-range-has-face 548 580 markdown-pre-face)))
2200 (ert-deftest test-markdown-font-lock/pre-2 ()
2201 (markdown-test-string "* item\n\nreset baseline\n\n pre block\n"
2202 (markdown-test-range-has-face 1 1 markdown-list-face)
2203 (markdown-test-range-has-face 2 23 nil)
2204 (markdown-test-range-has-face 29 37 markdown-pre-face)))
2206 (ert-deftest test-markdown-font-lock/pre-3 ()
2207 (markdown-test-string "It is interesting to see what happens when one queries
2208 `social upheaval` and `protopalatial era`.
2210 * `social upheaval`: the follwing queries have been tried:
2212 social upheaval subClassOf"
2213 (markdown-test-range-has-face 160 190 nil)))
2215 (ert-deftest test-markdown-font-lock/pre-4 ()
2216 "Pre blocks must be preceded by a blank line"
2217 (markdown-test-string "Paragraph
2218 for (var i = 0; i < 10; i++) {
2219 console.log(i);
2221 (markdown-test-range-has-face (point-min) (point-max) nil)))
2223 (ert-deftest test-markdown-font-lock/fenced-1 ()
2224 "Test fenced code blocks containing four-space indents."
2225 (markdown-test-string "Fenced code block
2228 if (x)
2229 foo();
2231 if (y)
2232 bar();
2235 (markdown-test-range-has-face 1 19 nil)
2236 (markdown-test-range-has-face 20 22 markdown-markup-face)
2237 (markdown-test-range-has-face 24 60 markdown-pre-face)
2238 (markdown-test-range-has-face 61 63 markdown-markup-face)))
2240 (ert-deftest test-markdown-font-lock/gfm-fenced-1 ()
2241 "Test GFM-style fenced code blocks (1)."
2242 (markdown-test-string "```ruby
2243 require 'redcarpet'
2244 markdown = Redcarpet.new('Hello World!')
2245 puts markdown.to_html
2246 ```"
2247 (markdown-test-range-has-face 1 3 markdown-markup-face) ; ```
2248 (markdown-test-range-has-face 4 7 markdown-language-keyword-face) ; ruby
2249 (markdown-test-range-has-face 9 90 markdown-pre-face) ; code
2250 (markdown-test-range-has-face 92 94 markdown-markup-face))) ; ```
2252 (ert-deftest test-markdown-font-lock/gfm-fenced-2 ()
2253 "Test GFM-style fenced code blocks (2)."
2254 (markdown-test-string "```{r sum}\n2+2\n```"
2255 (markdown-test-range-has-face 1 3 markdown-markup-face) ; ```
2256 (markdown-test-range-has-face 4 10 markdown-language-keyword-face) ; {r sum}
2257 (markdown-test-range-has-face 12 14 markdown-pre-face) ; 2+2
2258 (markdown-test-range-has-face 16 18 markdown-markup-face))) ; ```
2260 (ert-deftest test-markdown-font-lock/gfm-fenced-3 ()
2261 "GFM-style code blocks need not be preceded by a blank line."
2262 (markdown-test-string "Paragraph
2263 ```js
2264 for (var i = 0; i < 10; i++) {
2265 console.log(i);
2267 ```"
2268 (markdown-test-range-has-face 1 10 nil) ; Paragraph
2269 (markdown-test-range-has-face 11 13 markdown-markup-face) ; ```
2270 (markdown-test-range-has-face 14 15 markdown-language-keyword-face) ; js
2271 (markdown-test-range-has-face 17 68 markdown-pre-face)
2272 (markdown-test-range-has-face 70 72 markdown-markup-face)))
2274 (ert-deftest test-markdown-font-lock/atx-no-spaces ()
2275 "Test font-lock for atx headers with no spaces."
2276 (markdown-test-string "##abc##"
2277 (markdown-test-range-has-face 1 7 nil))
2278 (markdown-test-string "##"
2279 (markdown-test-range-has-face 1 2 nil))
2280 (markdown-test-string "###"
2281 (markdown-test-range-has-face 1 3 nil)))
2283 (ert-deftest test-markdown-font-lock/setext-1-letter ()
2284 "An edge case for level-one setext headers."
2285 (markdown-test-string "a\n=\n"
2286 (markdown-test-range-has-face 1 1 markdown-header-face-1)
2287 (markdown-test-range-has-face 3 3 markdown-header-rule-face)))
2289 (ert-deftest test-markdown-font-lock/setext-2-letter ()
2290 "An edge case for level-two setext headers."
2291 (markdown-test-string "b\n-\n"
2292 (markdown-test-range-has-face 1 1 markdown-header-face-2)
2293 (markdown-test-range-has-face 3 3 markdown-header-rule-face)))
2295 (ert-deftest test-markdown-font-lock/inline-links ()
2296 "Test font lock for inline links."
2297 (markdown-test-file "inline.text"
2298 (markdown-test-range-has-face 925 925 markdown-markup-face)
2299 (markdown-test-range-has-face 926 929 markdown-link-face)
2300 (markdown-test-range-has-face 930 931 markdown-markup-face)
2301 (markdown-test-range-has-face 932 949 markdown-url-face)
2302 (markdown-test-range-has-face 951 957 markdown-link-title-face)
2303 (markdown-test-range-has-face 958 958 markdown-markup-face)))
2305 (ert-deftest test-markdown-font-lock/pre-comment ()
2306 "Test comments inside of a pre block."
2307 (markdown-test-string " <!-- pre, not comment -->"
2308 (markdown-test-range-has-face (point-min) (1- (point-max)) markdown-pre-face)))
2310 (ert-deftest test-markdown-font-lock/inline-code-comment ()
2311 "Test comments inside of a pre block."
2312 (markdown-test-string "`<h1> <!-- HTML comment inside inline code -->`"
2313 (markdown-test-range-has-face (1+ (point-min)) (- (point-max) 2) markdown-inline-code-face)))
2315 (ert-deftest test-markdown-font-lock/comment-hanging-indent ()
2316 "Test comments with hanging indentation."
2317 (markdown-test-string "<!-- This comment has\n hanging indentation -->"
2318 (markdown-test-range-has-face (point-min) (1- (point-max)) markdown-comment-face)))
2320 (ert-deftest test-markdown-font-lock/comment-multiple ()
2321 "Test multiple single-line comments in arow."
2322 (markdown-test-string "<!-- This is a comment -->\n<!-- And so is this -->"
2323 (markdown-test-range-has-face
2324 (point-at-bol) (1- (point-at-eol)) markdown-comment-face)
2325 (forward-line)
2326 (markdown-test-range-has-face
2327 (point-at-bol) (1- (point-at-eol)) markdown-comment-face)))
2329 (ert-deftest test-markdown-font-lock/comment-list-items ()
2330 "Test comment with list inside."
2331 (markdown-test-string
2332 "<!--
2333 - note 1;
2334 - note 2.
2335 -->"
2336 (markdown-test-range-face-equals (point-min) (1- (point-max))
2337 markdown-comment-face)))
2339 (ert-deftest test-markdown-font-lock/comment-angle-bracket ()
2340 "Regression test for GH-117."
2341 (markdown-test-string "<!-- > test -->"
2342 (markdown-test-range-face-equals (point-min) (1- (point-max))
2343 markdown-comment-face)))
2345 (ert-deftest test-markdown-font-lock/footnote-markers-links ()
2346 "Test an edge case involving footnote markers and inline reference links."
2347 (markdown-test-string "Harvard[^1] [tuition][]"
2348 (markdown-test-range-has-face 1 7 nil)
2349 (markdown-test-range-has-face 8 8 markdown-markup-face)
2350 (markdown-test-range-has-face 10 10 markdown-footnote-face)
2351 (markdown-test-range-has-face 11 11 markdown-markup-face)
2352 (markdown-test-range-has-face 12 12 nil)
2353 (markdown-test-range-has-face 13 13 markdown-markup-face)
2354 (markdown-test-range-has-face 14 20 markdown-link-face)
2355 (markdown-test-range-has-face 21 21 markdown-markup-face)
2356 (markdown-test-range-has-face 22 23 markdown-markup-face)))
2358 (ert-deftest test-markdown-font-lock/mmd-metadata ()
2359 "Basic MultMarkdown metadata tests."
2360 (markdown-test-string "Title: peg-multimarkdown User's Guide
2361 Author: Fletcher T. Penney
2362 Base Header Level: 2"
2363 (markdown-test-range-has-face 1 5 markdown-metadata-key-face)
2364 (markdown-test-range-has-face 6 6 markdown-markup-face)
2365 (markdown-test-range-has-face 8 37 markdown-metadata-value-face)
2366 (markdown-test-range-has-face 39 44 markdown-metadata-key-face)
2367 (markdown-test-range-has-face 46 46 markdown-markup-face)
2368 (markdown-test-range-has-face 47 64 markdown-metadata-value-face)
2369 (markdown-test-range-has-face 66 82 markdown-metadata-key-face)
2370 (markdown-test-range-has-face 83 83 markdown-markup-face)
2371 (markdown-test-range-has-face 85 85 markdown-metadata-value-face))
2372 ;; Avoid triggering when a title contains a colon (e.g., Markdown: Syntax)
2373 (markdown-test-file "syntax.text"
2374 (markdown-test-range-has-face 1 16 markdown-header-face-1)))
2376 (ert-deftest test-markdown-font-lock/mmd-metadata-after-header ()
2377 "Ensure that similar lines are not matched after the header."
2378 (markdown-test-string "Title: peg-multimarkdown User's Guide
2380 Author: Fletcher T. Penney
2381 Base Header Level: 2"
2382 (markdown-test-range-has-face 1 5 markdown-metadata-key-face)
2383 (markdown-test-range-has-face 6 6 markdown-markup-face)
2384 (markdown-test-range-has-face 8 37 markdown-metadata-value-face)
2385 (markdown-test-range-has-face 40 65 nil)
2386 (markdown-test-range-has-face 67 86 nil)))
2388 (ert-deftest test-markdown-font-lock/pandoc-metadata ()
2389 "Basic Pandoc metadata tests."
2390 (markdown-test-string "% title
2391 two-line title
2392 % first author;
2393 second author
2394 % date
2396 body"
2397 (markdown-test-range-has-face 1 1 markdown-markup-face)
2398 (markdown-test-range-has-face 3 24 markdown-metadata-value-face)
2399 (markdown-test-range-has-face 26 26 markdown-markup-face)
2400 (markdown-test-range-has-face 28 56 markdown-metadata-value-face)
2401 (markdown-test-range-has-face 58 58 markdown-markup-face)
2402 (markdown-test-range-has-face 60 63 markdown-metadata-value-face)
2403 (markdown-test-range-has-face 64 69 nil)))
2405 (ert-deftest test-markdown-font-lock/yaml-metadata ()
2406 "Basic YAML metadata tests."
2407 (markdown-test-string
2408 "---
2409 layout: post
2410 date: 2015-08-13 11:35:25 EST
2413 (markdown-test-range-has-face 1 3 markdown-markup-face)
2414 (markdown-test-range-has-face 5 10 markdown-metadata-key-face)
2415 (markdown-test-range-has-face 11 11 markdown-markup-face)
2416 (markdown-test-range-has-face 13 16 markdown-metadata-value-face)
2417 (markdown-test-range-has-face 18 21 markdown-metadata-key-face)
2418 (markdown-test-range-has-face 22 22 markdown-markup-face)
2419 (markdown-test-range-has-face 24 46 markdown-metadata-value-face)
2420 (markdown-test-range-has-face 48 50 markdown-markup-face)))
2422 (ert-deftest test-markdown-font-lock/toml-metadata ()
2423 "Basic TOML metadata tests."
2424 (markdown-test-string
2425 "---
2426 layout = post
2427 date = 2015-08-13 11:35:25 EST
2430 (markdown-test-range-has-face 1 3 markdown-markup-face)
2431 (markdown-test-range-has-face 5 10 markdown-metadata-key-face)
2432 (markdown-test-range-has-face 12 12 markdown-markup-face)
2433 (markdown-test-range-has-face 14 17 markdown-metadata-value-face)
2434 (markdown-test-range-has-face 19 22 markdown-metadata-key-face)
2435 (markdown-test-range-has-face 24 24 markdown-markup-face)
2436 (markdown-test-range-has-face 26 48 markdown-metadata-value-face)
2437 (markdown-test-range-has-face 50 52 markdown-markup-face)))
2439 (ert-deftest test-markdown-font-lock/pandoc-yaml-metadata ()
2440 "Basic yaml metadata tests, with pandoc syntax."
2441 (let ((markdown-use-pandoc-style-yaml-metadata t))
2442 (markdown-test-string
2443 "some text
2446 layout: post
2447 date: 2015-08-13 11:35:25 EST
2450 more text
2453 layout: post
2454 date: 2015-08-13 11:35:25 EST
2457 But this is merely a code block
2461 layout: post
2462 date: 2015-08-13 11:35:25 EST
2466 ;; first section
2467 (markdown-test-range-has-face 12 14 markdown-markup-face)
2468 (markdown-test-range-has-face 16 21 markdown-metadata-key-face)
2469 (markdown-test-range-has-face 22 22 markdown-markup-face)
2470 (markdown-test-range-has-face 24 27 markdown-metadata-value-face)
2471 (markdown-test-range-has-face 29 32 markdown-metadata-key-face)
2472 (markdown-test-range-has-face 33 33 markdown-markup-face)
2473 (markdown-test-range-has-face 35 57 markdown-metadata-value-face)
2474 (markdown-test-range-has-face 59 61 markdown-markup-face)
2475 ;; second section
2476 (markdown-test-range-has-face 75 77 markdown-markup-face)
2477 (markdown-test-range-has-face 79 84 markdown-metadata-key-face)
2478 (markdown-test-range-has-face 85 85 markdown-markup-face)
2479 (markdown-test-range-has-face 87 90 markdown-metadata-value-face)
2480 (markdown-test-range-has-face 92 95 markdown-metadata-key-face)
2481 (markdown-test-range-has-face 96 96 markdown-markup-face)
2482 (markdown-test-range-has-face 98 120 markdown-metadata-value-face)
2483 (markdown-test-range-has-face 122 124 markdown-markup-face)
2484 ;; third section
2485 (markdown-test-range-has-face 160 162 markdown-markup-face)
2486 (markdown-test-range-has-face 164 213 markdown-pre-face)
2487 (markdown-test-range-has-face 215 217 markdown-markup-face))))
2489 (ert-deftest test-markdown-font-lock/line-break ()
2490 "Basic line break tests."
2491 (markdown-test-string " \nasdf \n"
2492 (markdown-test-range-has-face 1 9 nil)
2493 (markdown-test-range-has-face 10 11 markdown-line-break-face)))
2495 (ert-deftest test-markdown-font-lock/blockquote-bold ()
2496 "Test font lock for bold inside of a blockquote."
2497 (markdown-test-string
2498 "> **bold**"
2499 (markdown-test-range-has-face 2 10 markdown-blockquote-face)
2500 (markdown-test-range-has-face 5 8 markdown-bold-face)))
2502 (ert-deftest test-markdown-font-lock/blockquote-italic ()
2503 "Test font lock for italic inside of a blockquote."
2504 (markdown-test-string
2505 "> *italic*"
2506 (markdown-test-range-has-face 2 10 markdown-blockquote-face)
2507 (markdown-test-range-has-face 4 9 markdown-italic-face)))
2509 (ert-deftest test-markdown-font-lock/blockquote-link ()
2510 "Test font lock for links inside of a blockquote.
2511 This test will fail until font lock for inline links inside
2512 blockquotes is implemented (at present, the blockquote face
2513 takes precedence)."
2514 :expected-result :failed
2515 (markdown-test-string
2516 "> [link](url)"
2517 (markdown-test-range-has-face 1 13 markdown-blockquote-face)
2518 (markdown-test-range-has-face 3 8 markdown-link-face)
2519 (markdown-test-range-has-face 9 13 markdown-url-face)))
2521 (ert-deftest test-markdown-font-lock/blockquote-comment ()
2522 "Test font lock for comments inside of a blockquote."
2523 (markdown-test-string
2524 "> <!-- comment -->"
2525 (markdown-test-range-has-face 1 1 markdown-markup-face)
2526 (markdown-test-range-has-face 3 18 markdown-comment-face)))
2528 (ert-deftest test-markdown-font-lock/pre-override ()
2529 "Test that font lock for pre blocks overrides everything else."
2530 (markdown-test-string
2531 " **bold**
2532 _italic_
2533 <!-- comment -->
2534 [link](url)
2535 * list"
2536 (markdown-test-range-has-face 1 73 markdown-pre-face)))
2538 (ert-deftest test-markdown-font-lock/gfm-code-block-font-lock ()
2539 "GFM code block font lock test. Now in base markdown-mode as well!"
2540 (markdown-test-file "gfm.text"
2541 (markdown-test-range-has-face 2639 2641 markdown-markup-face) ; ```
2542 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face) ; lang
2543 (markdown-test-range-has-face 2647 2728 markdown-pre-face) ; code
2544 (markdown-test-range-has-face 2730 2732 markdown-markup-face))) ; ```
2546 (ert-deftest test-markdown-font-lock/reference-definition ()
2547 "Reference definitions should not include ]."
2548 (markdown-test-string "[1]: http://daringfireball.net/ \"title\""
2549 (markdown-test-range-has-face 2 2 markdown-reference-face) ; 1
2550 (markdown-test-range-has-face 6 31 markdown-url-face) ; URL
2551 (markdown-test-range-has-face 34 38 markdown-link-title-face)) ; title
2552 (markdown-test-string "[foo][1] and [bar][2]: not a reference definition"
2553 (markdown-test-range-has-face 2 4 markdown-link-face) ; foo
2554 (markdown-test-range-has-face 7 7 markdown-reference-face) ; 1
2555 (markdown-test-range-has-face 9 13 nil) ; [ ]and[ ]
2556 (markdown-test-range-has-face 15 17 markdown-link-face) ; bar
2557 (markdown-test-range-has-face 20 20 markdown-reference-face) ; 2
2558 (markdown-test-range-has-face 22 49 nil))) ; [ ]and[ ]
2560 ;;; Markdown Parsing Functions:
2562 (ert-deftest test-markdown-parsing/extend-region-function ()
2563 "Test `markdown-syntax-propertize-extend-region'.
2564 Should return a cons (NEW-START . NEW-END) or nil if no
2565 adjustment should be made. Function is called repeatedly until it
2566 returns nil."
2567 (markdown-test-file
2568 "inline.text"
2569 (should (equal (markdown-syntax-propertize-extend-region 1 17)
2570 (cons 1 91)))
2571 (should (equal (markdown-syntax-propertize-extend-region 2 17)
2572 (cons 1 91)))
2573 (should (equal (markdown-syntax-propertize-extend-region 1 91)
2574 nil))
2575 (should (equal (markdown-syntax-propertize-extend-region 93 157)
2576 nil))
2577 (should (equal (markdown-syntax-propertize-extend-region 496 502)
2578 (cons 486 510)))
2579 (should (equal (markdown-syntax-propertize-extend-region 486 510)
2580 nil))
2581 ;; Region that begins and ends with \n\n should not be extended
2582 (should (equal (markdown-syntax-propertize-extend-region 157 355)
2583 nil))))
2585 (defun markdown-test-check-match-limits (prop num begin end &optional pos)
2586 (let* ((posn (or pos (point)))
2587 (props (get-text-property posn prop)))
2588 (save-match-data
2589 (set-match-data props)
2590 (and (match-beginning num) (match-end num)
2591 (= (match-beginning num) begin)
2592 (= (match-end num) end)))))
2594 (ert-deftest test-markdown-parsing/syntax-with-adjacent-code-blocks ()
2595 "Test `markdown-syntax-propertize-fenced-code-blocks' with adjacent blocks."
2596 (markdown-test-string
2597 "~~~ shell
2598 #!/bin/sh
2600 echo \"Hello, world!\"
2603 ~~~ shell
2604 #!/bin/sh
2606 echo \"Hello, world v2!\"
2609 (let ((start-top-1 (make-marker)) (end-top-1 (make-marker))
2610 (start-lang-1 (make-marker)) (end-lang-1 (make-marker))
2611 (start-mid-1 (make-marker)) (end-mid-1 (make-marker))
2612 (start-bottom-1 (make-marker)) (end-bottom-1 (make-marker))
2613 (between (make-marker))
2614 (start-top-2 (make-marker)) (end-top-2 (make-marker))
2615 (start-lang-2 (make-marker)) (end-lang-2 (make-marker))
2616 (start-mid-2 (make-marker)) (end-mid-2 (make-marker))
2617 (start-bottom-2 (make-marker)) (end-bottom-2 (make-marker)))
2618 ;; First code block
2619 (set-marker start-top-1 1)
2620 (set-marker end-top-1 4)
2621 (set-marker start-lang-1 5)
2622 (set-marker end-lang-1 10)
2623 (set-marker start-mid-1 11)
2624 (set-marker end-mid-1 43)
2625 (set-marker start-bottom-1 43)
2626 (set-marker end-bottom-1 46)
2627 ;; check top tildes
2628 (should (markdown-test-check-match-limits
2629 'markdown-tilde-fence-begin 1 (marker-position start-top-1)
2630 (marker-position end-top-1) (marker-position start-top-1)))
2631 ;; check top language specifier
2632 (should (markdown-test-check-match-limits
2633 'markdown-tilde-fence-begin 2 (marker-position start-lang-1)
2634 (marker-position end-lang-1) (marker-position start-lang-1)))
2635 ;; check text in between
2636 (should (markdown-test-check-match-limits
2637 'markdown-fenced-code 0 (marker-position start-mid-1)
2638 (marker-position end-mid-1) (marker-position start-mid-1)))
2639 ;; check bottom tildes
2640 (should (markdown-test-check-match-limits
2641 'markdown-tilde-fence-end 1 (marker-position start-bottom-1)
2642 (marker-position end-bottom-1) (marker-position start-bottom-1)))
2643 ;; Point between code blocks
2644 (set-marker between 47)
2645 (should (equal (get-text-property between 'markdown-fenced-code)
2646 nil))
2647 ;; Second code block
2648 (set-marker start-top-2 48)
2649 (set-marker end-top-2 51)
2650 (set-marker start-lang-2 52)
2651 (set-marker end-lang-2 57)
2652 (set-marker start-mid-2 58)
2653 (set-marker end-mid-2 93)
2654 (set-marker start-bottom-2 93)
2655 (set-marker end-bottom-2 96)
2656 (should (markdown-test-check-match-limits
2657 'markdown-tilde-fence-begin 1 (marker-position start-top-2)
2658 (marker-position end-top-2) (marker-position start-top-2)))
2659 (should (markdown-test-check-match-limits
2660 'markdown-tilde-fence-begin 2 (marker-position start-lang-2)
2661 (marker-position end-lang-2) (marker-position start-lang-2)))
2662 (should (markdown-test-check-match-limits
2663 'markdown-fenced-code 0 (marker-position start-mid-2)
2664 (marker-position end-mid-2) (marker-position start-mid-2)))
2665 (should (markdown-test-check-match-limits
2666 'markdown-tilde-fence-end 1 (marker-position start-bottom-2)
2667 (marker-position end-bottom-2) (marker-position start-bottom-2)))
2668 ;; ;; Move point between code blocks and insert a character
2669 (goto-char between)
2670 (insert "x")
2671 ;; Re-propertize region after change
2672 (let ((range (markdown-syntax-propertize-extend-region (1- between) (point-max))))
2673 (markdown-syntax-propertize (car range) (cdr range)))
2674 ;; Re-check first code block
2675 (should (markdown-test-check-match-limits
2676 'markdown-tilde-fence-begin 1 (marker-position start-top-1)
2677 (marker-position end-top-1) (marker-position start-top-1)))
2678 (should (markdown-test-check-match-limits
2679 'markdown-tilde-fence-begin 2 (marker-position start-lang-1)
2680 (marker-position end-lang-1) (marker-position start-lang-1)))
2681 (should (markdown-test-check-match-limits
2682 'markdown-fenced-code 0 (marker-position start-mid-1)
2683 (marker-position end-mid-1) (marker-position start-mid-1)))
2684 (should (markdown-test-check-match-limits
2685 'markdown-tilde-fence-end 1 (marker-position start-bottom-1)
2686 (marker-position end-bottom-1) (marker-position start-bottom-1)))
2687 ;; Re-check point between code blocks
2688 (should (equal (get-text-property between 'markdown-fenced-code)
2689 nil))
2690 ;; Re-check second code block
2691 (should (markdown-test-check-match-limits
2692 'markdown-tilde-fence-begin 1 (marker-position start-top-2)
2693 (marker-position end-top-2) (marker-position start-top-2)))
2694 (should (markdown-test-check-match-limits
2695 'markdown-tilde-fence-begin 2 (marker-position start-lang-2)
2696 (marker-position end-lang-2) (marker-position start-lang-2)))
2697 (should (markdown-test-check-match-limits
2698 'markdown-fenced-code 0 (marker-position start-mid-2)
2699 (marker-position end-mid-2) (marker-position start-mid-2)))
2700 (should (markdown-test-check-match-limits
2701 'markdown-tilde-fence-end 1 (marker-position start-bottom-2)
2702 (marker-position end-bottom-2)
2703 (marker-position start-bottom-2))))))
2705 (ert-deftest test-markdown-parsing/propertize-fenced-in-between ()
2706 "Test whether `markdown-syntax-propertize-fenced-block-constructs' handles the
2707 case when it can't propertize both the start and end of a fenced block within a
2708 single pass (the end of the block is past the END argument)."
2709 (markdown-test-string
2710 "~~~ shell
2711 #!/bin/sh
2713 echo \"Hello, world!\"
2716 (set-text-properties (point-min) (point-max) nil)
2717 ;; syntax-propertize up to right after hashbang
2718 (markdown-syntax-propertize-fenced-block-constructs (point-min) 21)
2719 ;; ~~~ shell should be propertized, but nothing else
2720 ;; check tildes
2721 (should (markdown-test-check-match-limits
2722 'markdown-tilde-fence-begin 1 1 4 1))
2723 ;; check language
2724 (should (markdown-test-check-match-limits
2725 'markdown-tilde-fence-begin 2 5 10 5))
2726 ;; middle should not be propertized
2727 (should-not (get-text-property 11 'markdown-fenced-code))
2728 ;; neither should end
2729 (should-not (get-text-property 43 'markdown-tilde-fence-end))
2730 (markdown-syntax-propertize-fenced-block-constructs 21 (point-max))
2731 ;; everything should be propertized now
2732 ;; re-check top
2733 (should (markdown-test-check-match-limits
2734 'markdown-tilde-fence-begin 1 1 4 1))
2735 (should (markdown-test-check-match-limits
2736 'markdown-tilde-fence-begin 2 5 10 5))
2737 ;; check middle
2738 (should (markdown-test-check-match-limits 'markdown-fenced-code 0 10 43 10))
2739 ;; check ending tildes
2740 (should (markdown-test-check-match-limits
2741 'markdown-tilde-fence-end 1 43 46 43))))
2743 (ert-deftest test-markdown-parsing/get-code-block-at-pos ()
2744 "Test whether `markdown-code-block-at-pos' works in all situations. All
2745 situations are:
2746 1. pre block
2747 2. tilde block
2748 3. gfm block
2749 4. yaml metadata block"
2750 (let ((markdown-use-pandoc-style-yaml-metadata t))
2751 (markdown-test-string
2753 ~~~ ruby
2754 some_ruby_fun()
2758 a: b
2761 ``` {.bash}
2762 #!/bin/sh
2763 echo hey
2766 pre code
2767 random stuff
2768 more preformatted code
2771 data: pandoc
2774 ;; start/mid/end at tilde block
2775 (should (equal (markdown-code-block-at-pos 2) (list 2 30)))
2776 (should (equal (markdown-code-block-at-pos 11) (list 2 30)))
2777 (should (equal (markdown-code-block-at-pos 27) (list 2 30)))
2778 ;; yaml metadata block
2779 (should (equal (markdown-code-block-at-pos 32) (list 32 44)))
2780 (should (equal (markdown-code-block-at-pos 36) (list 32 44)))
2781 (should (equal (markdown-code-block-at-pos 41) (list 32 44)))
2782 ;; gfm block
2783 (should (equal (markdown-code-block-at-pos 46) (list 46 80)))
2784 (should (equal (markdown-code-block-at-pos 58) (list 46 80)))
2785 (should (equal (markdown-code-block-at-pos 77) (list 46 80)))
2786 ;; pre block
2787 (should (equal (markdown-code-block-at-pos 82) (list 82 138)))
2788 (should (equal (markdown-code-block-at-pos 99) (list 82 138)))
2789 (should (equal (markdown-code-block-at-pos 137) (list 82 138)))
2790 ;; pandoc yaml metadata block (should work if yaml above works)
2791 (should (equal (markdown-code-block-at-pos 140) (list 140 160)))
2792 (should (equal (markdown-code-block-at-pos 142) (list 140 160)))
2793 (should (equal (markdown-code-block-at-pos 144) (list 140 160)))
2794 (should (equal (markdown-code-block-at-pos 157) (list 140 160)))
2795 (should (equal (markdown-code-block-at-pos 159) (list 140 160))))))
2797 (ert-deftest test-markdown-parsing/syntax-get-fenced-blocks ()
2798 "Test whether *-get-fenced-block-* functions work in the case where a block is
2799 only partially propertized."
2800 (save-match-data
2801 (markdown-test-string
2802 "~~~
2804 (should (equal (markdown-syntax-propertize-extend-region
2805 (point-min) (point-max))
2806 nil))
2807 (goto-char 1)
2808 (set-match-data (markdown-text-property-at-point
2809 'markdown-tilde-fence-begin))
2810 (should (equal (markdown-get-fenced-block-from-start
2811 'markdown-tilde-fence-begin)
2812 nil)))
2813 (markdown-test-string
2814 "~~~
2815 ~~~"
2816 (goto-char 1)
2817 (set-match-data (markdown-text-property-at-point
2818 'markdown-tilde-fence-begin))
2819 (should (equal (markdown-get-fenced-block-from-start
2820 'markdown-tilde-fence-begin)
2821 (list 1 8)))
2822 (should (equal (markdown-code-block-at-point) (list 1 8)))
2823 (goto-char 5)
2824 (set-match-data (markdown-text-property-at-point
2825 'markdown-tilde-fence-end))
2826 (should (equal (markdown-get-fenced-block-from-end
2827 'markdown-tilde-fence-end)
2828 (list 1 8)))
2829 (should (equal (markdown-code-block-at-point) (list 1 8))))
2830 (markdown-test-string
2831 "~~~
2833 ~~~"
2834 (goto-char 1)
2835 (set-match-data (markdown-text-property-at-point
2836 'markdown-tilde-fence-begin))
2837 (should (equal (markdown-get-fenced-block-from-start
2838 'markdown-tilde-fence-begin)
2839 (list 1 9)))
2840 (should (equal (markdown-code-block-at-point) (list 1 9)))
2841 (goto-char 5)
2842 (set-match-data (markdown-text-property-at-point 'markdown-fenced-code))
2843 (should (equal (markdown-get-fenced-block-from-middle
2844 'markdown-fenced-code)
2845 (list 1 9)))
2846 (should (equal (markdown-code-block-at-point) (list 1 9)))
2847 (goto-char 6)
2848 (set-match-data (markdown-text-property-at-point
2849 'markdown-tilde-fence-end))
2850 (should (equal (markdown-get-fenced-block-from-end
2851 'markdown-tilde-fence-end)
2852 (list 1 9)))
2853 (should (equal (markdown-code-block-at-point) (list 1 9))))))
2855 (ert-deftest test-markdown-parsing/reference-definition-basic ()
2856 "Test reference definition function."
2857 (markdown-test-file "syntax.text"
2858 ;; Test accuracy of returned text and bounds
2859 (should (equal (markdown-reference-definition "1")
2860 (list "http://docutils.sourceforge.net/mirror/setext.html" 1942 1992)))
2861 (should (equal (markdown-reference-definition "2")
2862 (list "http://www.aaronsw.com/2002/atx/" 2000 2032)))
2863 ;; Test that match data remains intact
2864 (should (string-equal (match-string 5) "http://www.aaronsw.com/2002/atx/"))
2865 ;; Test anchor-only relative URL
2866 (should (equal (markdown-reference-definition "bq")
2867 (list "#blockquote" 7536 7547)))
2868 ;; Example references that appear in pre blocks in the text
2869 (should (not (markdown-reference-definition "")))
2870 (should (not (markdown-reference-definition "id")))
2871 (should (not (markdown-reference-definition "foo")))
2872 (should (not (markdown-reference-definition "A")))
2873 (should (not (markdown-reference-definition "Google")))
2874 ;; Test that we don't pick up other text in square brackets
2875 (should (not (markdown-reference-definition "blockquoting")))
2876 (should (not (markdown-reference-definition "square brackets")))
2877 ;; Test case insensitivity
2878 (should (equal (markdown-reference-definition "SRC")
2879 (list "/projects/markdown/syntax.text" 1245 1275)))))
2881 (ert-deftest test-markdown-parsing/get-defined-references ()
2882 "Test `markdown-get-defined-references'."
2883 (markdown-test-file "syntax.text"
2884 (should (equal (markdown-get-defined-references)
2885 '("src" "1" "2" "3" "4" "5" "6" "bq" "l"))))
2886 (markdown-test-file "outline.text"
2887 (should (equal (markdown-get-defined-references) nil)))
2888 (markdown-test-file "wiki-links.text"
2889 (should (equal (markdown-get-defined-references) nil))))
2891 (defun markdown-test-test-region (beg end)
2892 (goto-char (1- beg))
2893 (should-not (markdown-code-at-point-p))
2894 (goto-char (1+ end))
2895 (should-not (markdown-code-at-point-p))
2896 (dolist (loc (number-sequence beg end))
2897 (goto-char loc)
2898 (should (markdown-code-at-point-p))
2899 (should (equal (match-beginning 0) beg))
2900 (should (equal (match-end 0) end))))
2902 (ert-deftest test-markdown-parsing/code-at-point-inline ()
2903 "Test `markdown-code-at-point-p'."
2904 (markdown-test-file "inline.text"
2905 (markdown-test-test-region 45 51) ; Regular code span
2906 (markdown-test-test-region 61 90) ; Code containing backticks
2907 (markdown-test-test-region 228 240) ; Backquotes at beginning
2908 (markdown-test-test-region 341 352) ; Backquotes at end
2909 (markdown-test-test-region 460 469) ; Backslash as final character
2910 (markdown-test-test-region 657 667) ; A code span crossing lines
2911 (markdown-test-test-region 749 758) ; Three backquotes on same line
2912 (markdown-test-test-region 806 815) ; Three backquotes across lines
2915 (ert-deftest test-markdown-parsing/code-at-point-one-space ()
2916 "Test `markdown-code-at-point-p' with multiple code spans in a row."
2917 (markdown-test-string "`foo` `bar` `baz`"
2918 (dolist (loc (number-sequence 1 6))
2919 (goto-char loc)
2920 (should (markdown-code-at-point-p))
2921 (should (equal (match-data) (list 1 6 1 2 2 5 5 6))))
2922 (dolist (loc (number-sequence 7 12))
2923 (goto-char loc)
2924 (should (markdown-code-at-point-p))
2925 (should (equal (match-data) (list 7 12 7 8 8 11 11 12))))
2926 (dolist (loc (number-sequence 13 18))
2927 (goto-char loc)
2928 (should (markdown-code-at-point-p))
2929 (should (equal (match-data) (list 13 18 13 14 14 17 17 18))))))
2931 (ert-deftest test-markdown-parsing/code-at-point-no-space ()
2932 "Test `markdown-code-at-point-p' with multiple code spans in a row."
2933 (markdown-test-string "a`foo`b`bar`c`baz`d"
2934 (goto-char 1) ; "a"
2935 (should-not (markdown-code-at-point-p))
2936 (dolist (loc (number-sequence 2 7)) ; "`foo`b"
2937 (goto-char loc)
2938 (should (markdown-code-at-point-p))
2939 (should (equal (match-data) (list 2 7 2 3 3 6 6 7))))
2940 (dolist (loc (number-sequence 8 13)) ; "`bar`c"
2941 (goto-char loc)
2942 (should (markdown-code-at-point-p))
2943 (should (equal (match-data) (list 8 13 8 9 9 12 12 13))))
2944 (dolist (loc (number-sequence 14 19)) ; "`baz`d"
2945 (goto-char loc)
2946 (should (markdown-code-at-point-p))
2947 (should (equal (match-data) (list 14 19 14 15 15 18 18 19))))))
2949 (ert-deftest test-markdown-parsing/code-at-point-blank-line ()
2950 "Test `markdown-code-at-point-p' at beginning of block."
2951 (markdown-test-string "----------\n\n## foo\n"
2952 (should-not (markdown-code-at-point-p))
2953 (forward-line)
2954 (should-not (markdown-code-at-point-p))
2955 (forward-line)
2956 (should-not (markdown-code-at-point-p))))
2958 (ert-deftest test-markdown-parsing/match-comments ()
2959 "Test `markdown-match-comments'."
2960 (markdown-test-string
2961 "HTML <!-- foo --> comment"
2962 (should (markdown-match-comments (point-max)))
2963 (should (eq (point) 18))
2964 (should (equal (match-data) (list 6 18)))
2965 (should-not (markdown-match-comments (point-max)))))
2967 (ert-deftest test-markdown-parsing/range-property-any ()
2968 "Test behavior of `markdown-range-property-any'."
2969 (markdown-test-file
2970 "inline.text"
2971 (should (markdown-range-property-any
2972 (point-min) (point-at-eol)
2973 'face (list markdown-markup-face
2974 markdown-italic-face)))
2975 (should-not (markdown-range-property-any
2976 (point-min) (point-at-eol)
2977 'face (list markdown-bold-face)))))
2979 (ert-deftest test-markdown-parsing/inline-code ()
2980 "Don't cause infinite loop for inline code just after metadata block
2981 Detail: https://github.com/jrblevin/markdown-mode/issues/115"
2982 (markdown-test-string "---
2983 x: x
2987 (should (= (markdown-match-code (point-max)) (point-max)))))
2989 ;;; Reference Checking:
2991 (ert-deftest test-markdown-references/goto-line-button ()
2992 "Create and test a goto line button."
2993 (markdown-test-string "line 1\nline 2\n"
2994 ;; Store the temporary buffer with the text
2995 (let ((target (current-buffer)))
2996 ;; Create a new buffer for inserting
2997 (with-temp-buffer
2998 ;; Verify that point is in a different buffer
2999 (should (not (equal (current-buffer) target)))
3000 ;; Insert and press the button
3001 (insert-button "goto line 2"
3002 :type 'markdown-goto-line-button
3003 'target-buffer target
3004 'target-line 2)
3005 (should (string-equal (buffer-string) "goto line 2"))
3006 (backward-button 1)
3007 (call-interactively 'push-button)
3008 ;; Verify that point is on line 2 of target buffer
3009 (should (= (line-number-at-pos) 2))
3010 (should (looking-at "line 2"))
3011 (should (equal (current-buffer) target))))))
3013 (ert-deftest test-markdown-references/button-map ()
3014 "Verify that button-buffer-map is used for check references buffer."
3015 (markdown-test-string "[undefined][ref]\n"
3016 (let* ((target (buffer-name))
3017 (check (format "*Undefined references for %s*" target)))
3018 (markdown-check-refs)
3019 (with-current-buffer (get-buffer check)
3020 (should (equal (local-key-binding (kbd "TAB")) 'forward-button))
3021 (should (equal (local-key-binding (kbd "<backtab>")) 'backward-button))))))
3023 ;;; Lists:
3025 (ert-deftest test-markdown-lists/levels-1 ()
3026 "Test list levels function `markdown-calculate-list-levels'."
3027 (markdown-test-file "nested-list.text"
3028 (let ((values '(((1 . 1) . nil) ((2 . 13) . (3)) ((14 . 23) . (7 3))
3029 ((24 . 26) . (11 7 3)))))
3030 (cl-loop for (range . value) in values
3031 do (goto-char (point-min))
3032 (forward-line (1- (car range)))
3033 (dotimes (n (- (cdr range) (car range)))
3034 (should (equal (markdown-calculate-list-levels) value))
3035 (forward-line))))))
3037 (ert-deftest test-markdown-lists/levels-2 ()
3038 "Test list levels function `markdown-calculate-list-levels'."
3039 (markdown-test-file "syntax.text"
3040 (let ((values '(((1 . 13) . nil) ((14 . 14) . (0)) ((15 . 17) . (4 0))
3041 ((18 . 18) . (0)) ((19 . 24) . (4 0)) ((25 . 25) . (0))
3042 ((26 . 29) . (4 0)) ((30 . 30) . (0)) ((31 . 33) . (4 0))
3043 ((34 . 588) . nil) ((589 . 595) . (0)) ((596 . 814) . nil)
3044 ((815 . 820) . (0)) ((821 . 898) . nil))))
3045 (cl-loop for (range . value) in values
3046 do (goto-char (point-min))
3047 (forward-line (1- (car range)))
3048 (dotimes (n (- (cdr range) (car range)))
3049 (should (equal (markdown-calculate-list-levels) value))
3050 (forward-line))))))
3052 (ert-deftest test-markdown-lists/levels-interior ()
3053 "Test `markdown-calculate-list-levels' from inside a list item."
3054 (markdown-test-file "nested-list.text"
3055 (goto-char 36)
3056 (should (equal (markdown-calculate-list-levels) (list 3)))
3057 (goto-char 267)
3058 (should (equal (markdown-calculate-list-levels) (list 7 3)))
3059 (goto-char 540)
3060 (should (equal (markdown-calculate-list-levels) (list 11 7 3)))))
3062 (ert-deftest test-markdown-lists/bounds-1 ()
3063 "Test list item bounds function `markdown-cur-list-item-bounds'."
3064 (markdown-test-file "lists.text"
3065 (markdown-test-goto-heading "Case 9")
3066 (forward-line)
3067 (should (eq (point) 3699))
3068 (markdown-next-list-item 4)
3069 (should (eq (point) 3700))
3070 (should (equal (markdown-cur-list-item-bounds)
3071 (list 3700 3901 0 4 "- ")))
3072 (markdown-next-list-item 4)
3073 (should (eq (point) 3903))
3074 (should (equal (markdown-cur-list-item-bounds)
3075 (list 3903 3937 0 4 "* ")))))
3077 (ert-deftest test-markdown-lists/bounds-2 ()
3078 "Function `markdown-cur-list-item-bounds' should return nil outside of list items."
3079 (markdown-test-string "line one\n\n* item\n"
3080 (should (null (markdown-cur-list-item-bounds)))
3081 (forward-line)
3082 (should (null (markdown-cur-list-item-bounds)))
3083 (forward-line)
3084 (should (markdown-cur-list-item-bounds))))
3086 (ert-deftest test-markdown-lists/promotion-and-demotion ()
3087 "Test function `markdown-promote-list-item'."
3088 (markdown-test-file "nested-list.text"
3089 (forward-line)
3090 (should (looking-at " - List level 1 item 2
3092 Second paragraph of item 2
3094 Nested pre block in item 2
3095 Four spaces past the marker
3097 Another paragraph of item 2"))
3098 (markdown-demote-list-item)
3099 (should (looking-at " - List level 1 item 2
3101 Second paragraph of item 2
3103 Nested pre block in item 2
3104 Four spaces past the marker
3106 Another paragraph of item 2"))
3107 (markdown-promote-list-item)
3108 (should (looking-at " - List level 1 item 2
3110 Second paragraph of item 2
3112 Nested pre block in item 2
3113 Four spaces past the marker
3115 Another paragraph of item 2"))
3116 (goto-char (point-min))
3117 (forward-line 22)
3118 (should (looking-at " - List level 3 item 1
3120 Nested pre block"))
3121 (markdown-demote-list-item)
3122 (should (looking-at " - List level 3 item 1
3124 Nested pre block"))
3125 (markdown-promote-list-item)
3126 (should (looking-at " - List level 3 item 1
3128 Nested pre block"))))
3130 (ert-deftest test-markdown-lists/promotion-and-demotion-custom ()
3131 "Test custom variable `markdown-list-indent-width'."
3132 (markdown-test-file "nested-list.text"
3133 (forward-line)
3134 (should (looking-at " - List level 1 item 2
3136 Second paragraph of item 2
3138 Nested pre block in item 2
3139 Four spaces past the marker
3141 Another paragraph of item 2"))
3142 (let ((markdown-list-indent-width 2))
3143 (markdown-demote-list-item))
3144 (should (looking-at " - List level 1 item 2
3146 Second paragraph of item 2
3148 Nested pre block in item 2
3149 Four spaces past the marker
3151 Another paragraph of item 2"))))
3153 (ert-deftest test-markdown-lists/toggle-gfm-checkbox ()
3154 (markdown-test-string " - [X] GFM task list item"
3155 (markdown-toggle-gfm-checkbox)
3156 (should (string-equal (buffer-string) " - [ ] GFM task list item"))
3157 (markdown-toggle-gfm-checkbox)
3158 (should (string-equal (buffer-string) " - [x] GFM task list item"))))
3160 ;;; Outline minor mode tests:
3162 (ert-deftest test-markdown-outline/navigation ()
3163 "Test outline navigation functions."
3164 (markdown-test-file "outline.text"
3165 ;; Navigate to the first visible heading
3166 (markdown-next-visible-heading 1)
3167 (should (eq (point) 19))
3168 (should (looking-at "^# A top-level header"))
3169 ;; Navigate forward at the same level
3170 (markdown-forward-same-level 1)
3171 (should (eq (point) 377))
3172 (should (looking-at "^=+$"))
3173 ;; Navigate backward by four visible headings
3174 (markdown-previous-visible-heading 4)
3175 (should (eq (point) 69))
3176 (should (looking-at "^## A second-level header$"))))
3178 (ert-deftest test-markdown-outline/navigation-with-code ()
3179 "Test outline navigation functions with code blocks."
3180 (markdown-test-file "outline-code.text"
3181 ;; Navigate forward at the same level
3182 (markdown-forward-same-level 1)
3183 (should (eq (point) 159))
3184 (should (looking-at "^# Level one again"))))
3186 (ert-deftest test-markdown-outline/visibility-atx ()
3187 "Test outline visibility cycling for ATX-style headers."
3188 (markdown-test-file "outline.text"
3189 (let (last-command this-command)
3190 ;; Navigate to the second visible heading
3191 (markdown-next-visible-heading 2)
3192 (should (eq (point) 69))
3193 (should (looking-at "^## A second-level header$"))
3194 ;; Cycle visibility of this subtree
3195 (setq this-command 'markdown-cycle)
3196 (markdown-cycle)
3197 (setq last-command 'markdown-cycle)
3198 (should (eq (point) 69))
3199 (should (looking-at "^## A second-level header$"))
3200 ;; Test that the entire subtree is invisible
3201 (markdown-test-range-has-property 93 349 'invisible 'outline)
3202 ;; Cycle visibility of this subtree again
3203 (markdown-cycle)
3204 (should (eq (point) 69))
3205 (should (looking-at "^## A second-level header$"))
3206 ;; Test that text is visible
3207 (markdown-test-range-has-property 95 121 'invisible nil)
3208 ;; Test that subheadings are visible
3209 (markdown-test-range-has-property 123 141 'invisible nil)
3210 ;; Cycle visibility of this subtree again
3211 (markdown-cycle)
3212 (should (eq (point) 69))
3213 (should (looking-at "^## A second-level header$"))
3214 ;; Verify that entire subtree is visible
3215 (markdown-test-range-has-property 93 349 'invisible nil))))
3217 (ert-deftest test-markdown-outline/visibility-setext ()
3218 "Test outline visibility cycling for setext-style headers."
3219 (markdown-test-file "outline.text"
3220 ;; Navigate to the sixth visible heading
3221 (markdown-next-visible-heading 7)
3222 (markdown-previous-visible-heading 1)
3223 (should (looking-at markdown-regex-header))
3224 (should (string-equal (match-string-no-properties 1) "An underline-style header"))
3225 (should (string-equal (match-string-no-properties 2) "========================="))
3226 ;; Cycle visibility subtree, test that it's invisible
3227 (markdown-cycle)
3228 (markdown-test-range-has-property 404 515 'invisible 'outline)
3229 ;; Cycle visibility subtree, test that text and headers are visible
3230 (markdown-cycle)
3231 (markdown-test-range-has-property 404 417 'invisible nil)
3232 (markdown-test-range-has-property 420 451 'invisible nil)))
3234 (ert-deftest test-markdown-outline/visibility-with-code ()
3235 "Test outline visibility cycling with code blocks."
3236 (markdown-test-file "outline-code.text"
3237 (let (last-command this-command)
3238 ;; Cycle global visibility to "overview" mode
3239 (setq this-command 'markdown-cycle)
3240 (markdown-cycle t)
3241 (setq last-command 'markdown-cycle)
3242 (should (eq (point) (point-min)))
3243 (should (looking-at "^# Level one"))
3244 ;; Test that the code block is invisible
3245 (markdown-test-range-has-property 83 157 'invisible 'outline)
3246 ;; Check subsequent headings
3247 (outline-next-visible-heading 1)
3248 (should (eq (point) 69))
3249 (should (looking-at "^## Level two"))
3250 (outline-next-visible-heading 1)
3251 (should (eq (point) 159))
3252 (should (looking-at "^# Level one again")))))
3254 (ert-deftest test-markdown-outline/visibility-with-metadata ()
3255 "Test outline visibility cycling with metadata blocks."
3256 (markdown-test-string
3257 "---
3258 layout = post
3259 date = 2015-08-13 11:35:25 EST
3262 (let (last-command this-command)
3263 ;; Cycle global visibility to "overview" mode
3264 (setq this-command 'markdown-cycle)
3265 (markdown-cycle t)
3266 ;; Check that text is visible
3267 (markdown-test-range-has-property (point-min) (point-max) 'invisible nil))))
3269 ;;; Movement tests:
3271 (ert-deftest test-markdown-movement/defun ()
3272 "Test defun navigation."
3273 (markdown-test-file "outline.text"
3274 ;; end-of-defun should go to point-max
3275 (end-of-defun 10)
3276 (should (= (point) (point-max)))
3277 ;; end-of-defun should stop just before the next header
3278 (goto-char (point-min))
3279 (end-of-defun)
3280 (should (looking-at "\n# A top-level header"))
3281 (end-of-defun)
3282 (should (looking-at "\n## A second-level header"))
3283 (end-of-defun)
3284 (should (looking-at "\n### Third level ###"))
3285 (end-of-defun)
3286 (should (looking-at "\n### Third level number two ###"))
3287 ;; beginning-of-defun should move to the start of the previous header
3288 (beginning-of-defun)
3289 (should (looking-at "### Third level ###"))
3290 (beginning-of-defun)
3291 (should (looking-at "## A second-level header"))
3292 (beginning-of-defun)
3293 (should (looking-at "# A top-level header"))
3294 (beginning-of-defun)
3295 ;; beginning-of-defun should move up to point-min
3296 (should (= (point) (point-min)))
3297 ;; (beginning-of-defun -1) should move to the start of the next header
3298 (forward-line 2)
3299 (beginning-of-defun -1)
3300 (should (looking-at "## A second-level header"))
3301 (beginning-of-defun -1)
3302 (should (looking-at "### Third level ###"))
3303 (beginning-of-defun -1)
3304 (should (looking-at "### Third level number two ###"))))
3306 (ert-deftest test-markdown-movement/block ()
3307 "Test block movement."
3308 (markdown-test-file "outline.text"
3309 (markdown-end-of-block)
3310 (should (looking-at "\n# A top-level header"))
3311 (markdown-end-of-block)
3312 (should (looking-at "\nfollowed by some body text"))
3313 (markdown-end-of-block)
3314 (should (looking-at "\n## A second-level header"))
3315 (markdown-end-of-block)
3316 (should (looking-at "\nfollowed by some body text"))
3317 (markdown-end-of-block)
3318 (should (looking-at "\n### Third level ###"))
3319 (markdown-end-of-block)
3320 (should (looking-at "\n\\* A list item"))
3321 (markdown-end-of-block)
3322 (should (looking-at "\n### Third level number two ###"))
3323 (markdown-end-of-block)
3324 (should (looking-at "\n### Level two again"))
3325 (markdown-end-of-block)
3326 (should (looking-at "\nfollowed by some body text"))
3328 (markdown-test-goto-heading "Level two")
3329 (markdown-end-of-block)
3330 (should (looking-at "\nbar"))
3331 (markdown-end-of-block)
3332 (should (= (point) (point-max)))
3333 (markdown-beginning-of-block)
3334 (should (looking-at "bar"))
3335 (markdown-beginning-of-block)
3336 (should (looking-at "## Level two"))
3337 (markdown-beginning-of-block)
3338 (should (looking-at "foo"))
3339 (markdown-beginning-of-block)
3340 (should (looking-at "# Level one"))
3341 (markdown-beginning-of-block)
3342 (should (looking-at "* With"))
3343 (markdown-beginning-of-block)
3344 (should (looking-at "And a level two underline header"))
3346 (goto-char (point-min))
3347 (markdown-test-goto-heading "A top-level header")
3348 (beginning-of-line)
3349 (markdown-beginning-of-block)
3350 (should (= (point) (point-min)))))
3352 (ert-deftest test-markdown-movement/blockquote-paragraphs ()
3353 "Test filling of blockquotes containing multiple paragraphs."
3354 (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n>\n> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n"
3355 (forward-paragraph)
3356 (should (looking-at "^>$"))
3357 (should (= (point) 128))
3358 (forward-paragraph)
3359 (should (= (point) (point-max)))))
3361 (ert-deftest test-markdown-movement/reference-definition ()
3362 "Test jumping to reference definitions."
3363 ;; Jumping to explicit reference definition
3364 (markdown-test-string "[a][ref]\n\n[ref]: gopher://localhost/\n"
3365 (markdown-reference-goto-definition)
3366 (should (= (point) 18)))
3367 ;; Jumping to implicit reference definition
3368 (markdown-test-string "[a][]\n\n[a]: ftp://localhost/\n"
3369 (markdown-reference-goto-definition)
3370 (should (= (point) 13)))
3371 ;; Creating non-existent reference definition
3372 (markdown-test-string "[a][]\n"
3373 (markdown-reference-goto-definition)
3374 (should (= (point) 13))
3375 (should (string-equal (buffer-string) "[a][]\n\n[a]: \n"))))
3377 (ert-deftest test-markdown-movement/back-to-same-level-over-code-block ()
3378 "`markdown-backward-same-level' over code block which contains header
3379 like statement. Detail: https://github.com/jrblevin/markdown-mode/issues/75"
3380 (markdown-test-string "
3381 ## Header 2-1
3383 ## Header 2-2
3385 ```R
3386 # Header Like Statement
3389 ## Header 2-3
3391 (search-forward "## Header 2-3")
3392 (let ((last-header-pos (point)))
3393 (forward-line -1)
3394 (call-interactively #'markdown-backward-same-level)
3395 (should (looking-at-p "## Header 2-1"))
3397 (goto-char last-header-pos)
3398 (call-interactively #'markdown-backward-same-level)
3399 (should (looking-at-p "## Header 2-2"))
3401 (goto-char last-header-pos)
3402 (markdown-backward-same-level 2)
3403 (should (looking-at-p "## Header 2-1"))
3405 (search-forward "# Header Like Statement")
3406 (call-interactively #'markdown-backward-same-level)
3407 (should (looking-at-p "## Header 2-1")))))
3409 ;;; Link tests:
3411 (ert-deftest test-markdown-link/follow ()
3412 "Test link following in a browser and in Emacs."
3413 (markdown-test-string "[text](http://path?query=foo#id)"
3414 (let* ((opened-url nil)
3415 (browse-url-browser-function
3416 (lambda (url &rest args) (setq opened-url url))))
3417 (markdown-follow-thing-at-point nil)
3418 (should (equal opened-url "http://path?query=foo#id"))))
3419 (when (featurep 'url-parse)
3420 (markdown-test-string "[text](path?query=foo#id)"
3421 (markdown-follow-thing-at-point nil)
3422 (should (equal (file-name-nondirectory (buffer-file-name)) "path"))
3423 (kill-buffer))))
3425 ;;; Wiki link tests:
3427 (ert-deftest test-markdown-wiki-link/file-local-variables ()
3428 "Test enabling wiki links via file-local variables."
3429 (markdown-test-file "wiki-links.text"
3430 (should-not markdown-enable-wiki-links)
3431 (hack-local-variables)
3432 (should markdown-enable-wiki-links)))
3434 (ert-deftest test-markdown-wiki-link/aliasing ()
3435 "Test filename extraction for aliased wiki links."
3436 (let ((markdown-enable-wiki-links t))
3437 (markdown-test-file "wiki-links.text"
3438 ;; Confirm location of first wiki link
3439 (should (eq (markdown-next-link) 8))
3440 ;; Confirm location of second wiki link
3441 (should (eq (markdown-next-link) 73))
3442 ;; Test predicate function
3443 (should (markdown-wiki-link-p))
3444 ;; Test alias-first filename extraction
3445 (setq markdown-wiki-link-alias-first t)
3446 (should (string-equal (markdown-wiki-link-link) "second"))
3447 ;; Test alias-second filename extraction
3448 (setq markdown-wiki-link-alias-first nil)
3449 (should (string-equal (markdown-wiki-link-link) "first")))))
3451 (ert-deftest test-markdown-wiki-link/navigation ()
3452 "Test wiki link navigation."
3453 (let ((markdown-enable-wiki-links t))
3454 (markdown-test-file "wiki-links.text"
3455 ;; Advance to first link
3456 (should (eq (markdown-next-link) 8))
3457 ;; Advance to second link
3458 (should (eq (markdown-next-link) 73))
3459 ;; Avance to final link
3460 (should (eq (markdown-next-link) 155))
3461 ;; Return nil and don't advance point
3462 (should (eq (markdown-next-link) nil))
3463 (should (eq (point) 155))
3464 ;; Move back to second link
3465 (should (eq (markdown-previous-link) 73))
3466 ;; Move back to first link
3467 (should (eq (markdown-previous-link) 8))
3468 ;; Return nil and don't move point
3469 (should (eq (markdown-previous-link) nil))
3470 (should (eq (point) 8)))))
3472 (ert-deftest test-markdown-wiki-link/font-lock ()
3473 "Test font lock faces for wiki links."
3474 (markdown-test-temp-file "wiki-links.text"
3475 (let* ((fn (concat (file-name-directory buffer-file-name)
3476 "inline.text"))
3477 (markdown-enable-wiki-links t))
3478 ;; Create inline.text in the same temp directory, refontify
3479 (write-region "" nil fn nil 1)
3480 (markdown-fontify-buffer-wiki-links)
3481 ;; Confirm location of first wiki link
3482 (should (eq (markdown-next-link) 8))
3483 ;; First wiki link doesn't have a corresponding file
3484 (markdown-test-range-has-property 8 20 'font-lock-face markdown-missing-link-face)
3485 ;; Second wiki link doesn't have a corresponding file
3486 (should (eq (markdown-next-link) 73))
3487 (markdown-test-range-has-property 73 88 'font-lock-face markdown-missing-link-face)
3488 ;; Move to third wiki link, and create the missing file
3489 (should (eq (markdown-next-link) 155))
3490 (should (string-equal (markdown-wiki-link-link) "inline"))
3491 (markdown-test-range-has-property 155 164 'font-lock-face markdown-link-face)
3492 ;; Check wiki links in code blocks
3493 (markdown-test-range-has-face 360 395 markdown-pre-face)
3494 ;; Remove temporary files
3495 (delete-file fn)
3498 (ert-deftest test-markdown-wiki-link/kill ()
3499 "Simple tests for `markdown-kill-thing-at-point' for wiki links."
3500 (let ((kill-ring nil)
3501 (markdown-enable-wiki-links t)
3502 (tests (list '("[[foo]]" . "foo")
3503 '("[[foo|bar]]" . "bar"))))
3504 (dolist (test tests)
3505 ;; Load test string (the car), move to end of first line, kill
3506 ;; thing at point, and then verify that the kill ring contains cdr.
3507 (markdown-test-string (car test)
3508 (end-of-line)
3509 (call-interactively 'markdown-kill-thing-at-point)
3510 (should (string-equal (current-kill 0) (cdr test)))))))
3512 ;;; Filling tests:
3514 (ert-deftest test-markdown-filling/blockquote ()
3515 "Test filling of blockquotes.
3516 See `adaptive-fill-first-line-regexp'."
3517 (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
3518 (fill-paragraph)
3519 (should (string-equal (buffer-string) "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\n> eiusmod tempor incididunt ut labore et dolore magna aliqua."))))
3521 (ert-deftest test-markdown-filling/blockquote-paragraphs ()
3522 "Test filling of blockquotes containing multiple paragraphs."
3523 (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n>\n> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n"
3524 (forward-paragraph)
3525 (fill-paragraph)
3526 (should (string-equal (buffer-string) "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n>\n> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\n> nisi ut aliquip ex ea commodo consequat.\n"))))
3528 (ert-deftest test-markdown-filling/space-after-list-marker ()
3529 "`fill-paragraph' should preserve more than one space after a list marker,
3530 since users may wish to indent their lists more than one space more than the
3531 width of the marker. The examples on the Markdown Syntax page have three
3532 spaces after the list marker for a total indentation of four."
3533 (let ((str "\n\n* List item indented four spaces.\n* Also four spaces."))
3534 (markdown-test-string str
3535 (forward-line 2)
3536 (fill-paragraph)
3537 (should (string-equal (buffer-string) str)))))
3539 (ert-deftest test-markdown-filling/multi-line-list-with-more-space ()
3540 "`fill-paragraph' should preserve more than one space after a list marker
3541 (see `test-preserve-space-after-list-marker')."
3542 (let ((str "* This list item is continued on\n the next line"))
3543 (markdown-test-string str
3544 ;; The first line is exactly 35 columns
3545 (let ((fill-column 35))
3546 (fill-paragraph)
3547 (should (string-equal (buffer-string) str))))))
3549 (ert-deftest test-markdown-filling/definition-list-add-leading-spaces ()
3550 "`fill-paragraph' should adapt to spaces after list marker."
3551 (markdown-test-string
3552 ": This list item is continued on the next line"
3553 (let ((fill-column 35))
3554 (fill-paragraph)
3555 (should (string-equal
3556 (buffer-string)
3557 ": This list item is continued on\n the next line")))))
3559 (ert-deftest test-markdown-filling/definition-list-preserve-leading-spaces ()
3560 "`fill-paragraph' should preserve spaces after list marker."
3561 (let ((str ": This list item is continued on\n the next line")
3562 (fill-column 35))
3563 (markdown-test-string
3564 str (fill-paragraph)
3565 (should (string-equal (buffer-string) str)))))
3567 (ert-deftest test-markdown-filling/list-item-plus ()
3568 "Test filling of list items with plus sign markers.
3569 See `adaptive-fill-regexp'."
3570 (markdown-test-string " + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
3571 (fill-paragraph)
3572 (should (string-equal (buffer-string) " + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\n eiusmod tempor incididunt ut labore et dolore magna aliqua."))))
3574 (ert-deftest test-markdown-filling/list-item-plus-in-blockquote ()
3575 "Test filling of list items with plus sign markers inside blockquote.
3576 See `adaptive-fill-regexp'."
3577 (markdown-test-string "> + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
3578 (fill-paragraph)
3579 (should (string-equal (buffer-string) "> + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\n> eiusmod tempor incididunt ut labore et dolore magna aliqua."))))
3581 (ert-deftest test-markdown-filling/line-break ()
3582 "Test filling of paragraphs with hard line breaks.
3583 See `paragraph-separate'."
3584 (markdown-test-string "Lorem ipsum dolor sit amet, \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
3585 (let ((fill-column 70))
3586 (fill-paragraph)
3587 (should (string-equal (buffer-string) "Lorem ipsum dolor sit amet, \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut\nlabore et dolore magna aliqua.")))))
3589 (ert-deftest test-markdown-filling/decimal-number-at-beginning ()
3590 "Test filling when a number with a decimal appears at the beginning of a line."
3591 (markdown-test-string "The circumference of a circle divided by it's radius is around\n3.14."
3592 (fill-paragraph)
3593 (should (string-equal (buffer-string) "The circumference of a circle divided by it's radius is around 3.14."))))
3595 (ert-deftest test-markdown-filling/avoid-unintended-list-item ()
3596 "Avoid breaking lines where it would result in an unintended list item."
3597 (markdown-test-string "Lorem ipsum dolor sit 4. amet"
3598 (let ((fill-column 22))
3599 (fill-paragraph)
3600 (should (string-equal (buffer-string) "Lorem ipsum dolor\nsit 4. amet")))))
3602 (ert-deftest test-markdown-filling/no-break-link-reference ()
3603 "Shouldn't break line between label and url, or combine two link references."
3604 (let ((str "[label1]: http://long-url.example.com\n[label2]: http://another-long-url.example.com/"))
3605 (markdown-test-string str
3606 (let ((fill-column 15)) ; after end of label, before end of URL
3607 (fill-paragraph)
3608 (should (string-equal (buffer-string) str))))))
3610 (ert-deftest test-markdown-filling/no-break-before-list-item ()
3611 "There's no point in putting the first item of a list on the next line,
3612 indented the same amount."
3613 :expected-result :failed
3614 (let ((str "* [Link](http://way-too-long.example.com)\n"))
3615 (markdown-test-string str
3616 (auto-fill-mode 1)
3617 (let ((fill-column 10))
3618 (end-of-line)
3619 (funcall auto-fill-function)
3620 (should (string-equal (buffer-string) str))))))
3622 (ert-deftest test-markdown-filling/break-within-list-item ()
3623 "This doesn't suppress auto-fill within a multi-word list item."
3624 :expected-result :failed
3625 (markdown-test-string "* [Link](http://example.com/) more text"
3626 (auto-fill-mode 1)
3627 (let ((fill-column 10))
3628 (end-of-line)
3629 (funcall auto-fill-function)
3630 (should (string-equal
3631 (buffer-string)
3632 "* [Link](http://example.com/)\n more text")))))
3634 (ert-deftest test-markdown-filling/preserve-next-line-footnote ()
3635 "Footnote block can be after label"
3636 (let ((str "[^label1]:\n Footnote block\n more footnote")) ; six spaces
3637 (markdown-test-string str
3638 (let ((fill-column 20)) ; could fit "footnote" after label, but shouldn't
3639 (fill-paragraph)
3640 (should (string-equal (buffer-string) str))))))
3642 (ert-deftest test-markdown-filling/wrap-same-line-footnote ()
3643 "Additional lines must be indented one level (four spaces) when wrapped."
3644 (markdown-test-string "[^label]: Long line should be wrapped"
3645 (let ((fill-column 25)) ; wrap before end of "should"
3646 (fill-paragraph)
3647 (should (string-equal (buffer-string) "[^label]: Long line\n should be wrapped")))))
3649 (ert-deftest test-markdown-filling/wrap-extra-hanging-indentation ()
3650 "Additional lines must be indented one level (four spaces) when wrapped."
3651 (markdown-test-string "[^label]: Long line\n should be wrapped"
3652 (let ((fill-column 25)) ; wrap before end of "should"
3653 (fill-paragraph)
3654 (should (string-equal (buffer-string) "[^label]: Long line\n should be wrapped")))))
3656 (ert-deftest test-markdown-filling/full-justification ()
3657 "Test paragraph detection with lines with lots of whitespace."
3658 (markdown-test-string "Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Dolor Sit Amet Consectetur http://very-long-url.lorem.ipsum.sic.dolor.sit.amet.com"
3659 (setq default-justification 'full)
3660 (fill-paragraph)
3661 (should (string-equal (buffer-string) "Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem\nDolor Sit Amet Consectetur\nhttp://very-long-url.lorem.ipsum.sic.dolor.sit.amet.com"))
3662 (backward-paragraph)
3663 (forward-paragraph)
3664 (should (= (point) 198))))
3666 (ert-deftest test-markdown-filling/list-line ()
3667 "Test fill-paragraph for list line. Don't insert bullet automatically.
3668 Detail: https://github.com/jrblevin/markdown-mode/issues/79"
3669 (markdown-test-string "* foo foo *foo* foo foo foo foo foo foo"
3670 (let ((fill-column 10))
3671 (fill-paragraph)
3672 (fill-paragraph)
3673 (forward-line 2)
3674 (back-to-indentation)
3675 (should-not (looking-at-p "\\*foo"))
3676 (forward-line 1)
3677 (back-to-indentation)
3678 (should-not (looking-at-p "\\*foo")))))
3680 (ert-deftest test-markdown-filling/ignore-header ()
3681 "# Test fill-paragraph for containing header line paragraph.
3682 https://github.com/jrblevin/markdown-mode/issues/159"
3683 (markdown-test-string "# this is header line
3684 this is not header line
3686 (let ((fill-column 10))
3687 (fill-paragraph)
3688 (should (string= (buffer-substring (point) (line-end-position)) "# this is header line")))))
3690 (ert-deftest test-markdown-filling/unclosed-square-bracket ()
3691 "Test fill-paragraph following an unclosed square bracket."
3692 (markdown-test-string "```\n[3\n```\n\naaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbb"
3693 (let ((fill-column 20))
3694 (forward-line 4)
3695 (fill-paragraph)
3696 (should (looking-at "aaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbb")))))
3698 ;;; Export tests:
3700 (ert-deftest test-markdown-hook/xhtml-standalone ()
3701 "Test `markdown-xhtml-standalone-regexp' and `markdown-output-standalone-p'."
3702 (should (string-match markdown-xhtml-standalone-regexp
3703 "<?xml version='1.0' encoding='UTF-8'?>"))
3704 (should (string-match markdown-xhtml-standalone-regexp
3705 "<!DOCTYPE html>"))
3706 (should (string-match markdown-xhtml-standalone-regexp
3707 "<html>"))
3708 (should-not (string-match markdown-xhtml-standalone-regexp
3709 "<h1>title</h1>"))
3710 (should-not (string-match markdown-xhtml-standalone-regexp
3711 "<div id=\"name\">")))
3713 ;;; Hook tests:
3715 (ert-deftest test-markdown-hook/before-export ()
3716 "Test hook run before export XHTML."
3717 (markdown-test-temp-file "lists.text"
3718 (let* ((before-hook-run nil)
3719 (orig-point (point))
3720 (func (lambda ()
3721 ;; Change value of a variable
3722 (setq before-hook-run t)
3723 ;; Insert some text
3724 (goto-char (point-min))
3725 (insert "#")
3726 ;; Deliberately move the point
3727 (end-of-line)
3728 ;; Verify changes
3729 (should (looking-back "^## List Cases" nil))
3730 (should-not (= (point) orig-point))))
3731 (ofile (progn
3732 ;; Register hook
3733 (add-hook 'markdown-before-export-hook func)
3734 ;; Export XHTML and return filename
3735 (markdown-export)))
3736 (obuffer (get-file-buffer ofile)))
3737 ;; Test side effects of hook
3738 (should (eq before-hook-run t))
3739 ;; Test position of point
3740 (should (= (point) orig-point))
3741 ;; Test that buffer was restored to original state
3742 (goto-char (point-min))
3743 (should (looking-at "^# List Cases"))
3744 ;; Clean
3745 (remove-hook 'markdown-before-export-hook func)
3746 (kill-buffer obuffer)
3747 (delete-file ofile))))
3749 (ert-deftest test-markdown-hook/after-export ()
3750 "Test hook run after export XHTML."
3751 (markdown-test-temp-file "lists.text"
3752 (let* ((after-hook-run nil)
3753 (func (lambda ()
3754 ;; Change variable value
3755 (setq after-hook-run t)
3756 ;; Add comment to output buffer
3757 (goto-char (point-min))
3758 (insert "<!-- after-export-hook -->\n")))
3759 (ofile (progn
3760 ;; Register hook
3761 (add-hook 'markdown-after-export-hook func)
3762 ;; Export XHTML and return filename
3763 (markdown-export)))
3764 (obuffer (get-file-buffer ofile)))
3765 (message "obuffer = %S" obuffer)
3766 ;; Test that variable was changed
3767 (should (eq after-hook-run t))
3768 ;; Test that output buffer remains open
3769 (should (get-buffer obuffer))
3770 ;; Test that output buffer modification remains
3771 (with-current-buffer obuffer
3772 (goto-char (point-min))
3773 (should (looking-at "<!-- after-export-hook -->\n")))
3774 ;; Test that buffer modification was saved
3775 (should-not (buffer-modified-p obuffer))
3776 ;; Clean up
3777 (remove-hook 'markdown-after-export-hook func)
3778 (kill-buffer obuffer)
3779 (delete-file ofile))))
3781 ;;; Extension: math support
3783 (ert-deftest test-markdown-math/file-local-variable ()
3784 "Test enabling math mode via `hack-local-variables-hook'."
3785 (markdown-test-file "math.text"
3786 (should-not markdown-enable-math)
3787 (hack-local-variables)
3788 (should markdown-enable-math)))
3790 (ert-deftest test-markdown-math/reload ()
3791 "Test enabling math mode via function `markdown-enable-math'."
3792 (markdown-test-file "math.text"
3793 (markdown-toggle-math t)
3794 ;; Flag should be set to t
3795 (should markdown-enable-math)
3796 ;; Font-lock keywords should be updated
3797 (should (member (cons markdown-regex-math-display '((1 markdown-markup-face prepend)
3798 (2 markdown-math-face append)
3799 (3 markdown-markup-face prepend)))
3800 markdown-mode-font-lock-keywords))))
3802 (ert-deftest test-markdown-math/font-lock ()
3803 "Test markdown math mode."
3804 (markdown-test-file "math.text"
3805 (markdown-toggle-math t)
3806 (funcall markdown-test-font-lock-function)
3807 (markdown-test-range-has-face 1 32 nil)
3808 (markdown-test-range-has-face 33 33 markdown-markup-face)
3809 (markdown-test-range-has-face 34 45 markdown-math-face)
3810 (markdown-test-range-has-face 46 46 markdown-markup-face)
3811 (markdown-test-range-has-face 47 49 nil)
3812 (markdown-test-range-has-face 50 51 markdown-markup-face)
3813 (markdown-test-range-has-face 52 63 markdown-math-face)
3814 (markdown-test-range-has-face 64 65 markdown-markup-face)
3815 (markdown-test-range-has-face 66 98 nil)
3816 (markdown-test-range-has-face 99 100 markdown-markup-face)
3817 (markdown-test-range-has-face 101 112 markdown-math-face)
3818 (markdown-test-range-has-face 113 114 markdown-markup-face)
3819 (markdown-test-range-has-face 113 114 markdown-markup-face)
3820 (markdown-test-range-has-face 117 117 markdown-header-delimiter-face)
3821 (markdown-test-range-has-face 119 152 markdown-header-face-1)
3822 (markdown-test-range-has-face 129 129 markdown-markup-face)
3823 (markdown-test-range-has-face 136 136 markdown-markup-face)
3824 (markdown-test-range-has-face 174 177 markdown-markup-face)
3825 (markdown-test-range-has-face 179 188 markdown-language-keyword-face)
3826 (markdown-test-range-has-face 190 211 markdown-pre-face)
3827 (markdown-test-range-has-face 212 215 markdown-markup-face)
3828 (markdown-test-range-has-face 218 218 markdown-markup-face)
3829 (markdown-test-range-has-face 219 223 markdown-math-face)
3830 (markdown-test-range-has-face 224 224 markdown-markup-face)
3831 (markdown-test-range-has-face 350 351 markdown-markup-face)
3832 (markdown-test-range-has-face 352 356 markdown-math-face)
3833 (markdown-test-range-has-face 357 358 markdown-markup-face)
3834 (markdown-test-range-has-face 359 391 nil)
3835 (markdown-test-range-has-face 392 393 markdown-markup-face)
3836 (markdown-test-range-has-face 394 398 markdown-math-face)
3837 (markdown-test-range-has-face 399 400 markdown-markup-face)))
3839 (ert-deftest test-markdown-math/font-lock-italics ()
3840 "Test markdown math mode with underscores."
3841 (markdown-test-file "math.text"
3842 (markdown-toggle-math t)
3843 (funcall markdown-test-font-lock-function)
3844 (markdown-test-range-has-face 227 227 markdown-markup-face)
3845 (markdown-test-range-has-face 228 233 markdown-math-face)
3846 (markdown-test-range-has-face 234 234 markdown-markup-face)
3847 (markdown-test-range-has-face 235 270 nil)
3848 (markdown-test-range-has-face 271 271 markdown-markup-face)
3849 (markdown-test-range-has-face 272 274 markdown-math-face)
3850 (markdown-test-range-has-face 275 275 markdown-markup-face)))
3852 ;;; gfm-mode tests:
3854 (ert-deftest test-markdown-gfm/pre-1 ()
3855 "GFM pre block font lock test."
3856 (markdown-test-file-gfm "gfm.text"
3857 (markdown-test-range-has-face 2626 2637 nil)
3858 (markdown-test-range-has-face 2639 2641 markdown-markup-face)
3859 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face)
3860 (markdown-test-range-has-face 2647 2728 markdown-pre-face)
3861 (markdown-test-range-has-face 2730 2732 markdown-markup-face)))
3863 (ert-deftest test-markdown-gfm/italic-1 ()
3864 "GFM italic font lock test."
3865 (markdown-test-file-gfm "gfm.text"
3866 (markdown-test-range-has-face 1483 1483 markdown-markup-face)
3867 (markdown-test-range-has-face 1484 1487 markdown-italic-face)
3868 (markdown-test-range-has-face 1488 1488 markdown-markup-face)
3869 (markdown-test-range-has-face 1729 1790 nil)))
3871 (ert-deftest test-markdown-gfm/strike-through-1 ()
3872 "GFM strike through font lock test."
3873 (markdown-test-string-gfm "one ~~two~~ three"
3874 (markdown-test-range-has-face 1 4 nil)
3875 (markdown-test-range-has-face 5 6 markdown-markup-face)
3876 (markdown-test-range-has-face 7 9 markdown-strike-through-face)
3877 (markdown-test-range-has-face 10 11 markdown-markup-face)
3878 (markdown-test-range-has-face 12 17 nil)))
3880 (ert-deftest test-markdown-gfm/toggle-strike-through ()
3881 "Test toggling functionality of `markdown-insert-strike-through'."
3882 (markdown-test-string-gfm "one ~~two~~ three"
3883 (forward-word 2)
3884 (markdown-insert-strike-through)
3885 (should (string-equal (buffer-string) "one two three"))
3886 (should (= (point) 8))
3887 (forward-word)
3888 (markdown-insert-strike-through)
3889 (should (= (point) 16))
3890 (should (string-equal (buffer-string) "one two ~~three~~"))))
3892 (ert-deftest test-markdown-gfm/insert-code-block ()
3893 "GFM code block insertion test."
3894 ;; Test empty markup insertion
3895 (markdown-test-string-gfm "line 1\nline 2\n"
3896 (end-of-line)
3897 (markdown-insert-gfm-code-block "elisp")
3898 (should (equal (car markdown-gfm-used-languages) "elisp"))
3899 (should (equal (car (markdown-gfm-get-corpus)) "elisp"))
3900 (should (string-equal (buffer-string)
3901 "line 1\n\n``` elisp\n\n```\n\nline 2\n")))
3902 ;; Test with active region
3903 (markdown-test-string-gfm "line 1\nline 2\nline 3\n"
3904 (forward-line)
3905 (transient-mark-mode)
3906 (push-mark (point) t t)
3907 (end-of-line)
3908 (should (markdown-use-region-p))
3909 (markdown-insert-gfm-code-block "elisp")
3910 (should (string-equal (buffer-string)
3911 "line 1\n\n``` elisp\nline 2\n```\n\nline 3\n"))))
3913 (ert-deftest test-markdown-gfm/gfm-parse-buffer-for-languages ()
3914 "Parse buffer for existing languages for `markdown-gfm-used-languages' test."
3915 (markdown-test-string-gfm "``` MADEUP\n\n```\n``` LANGUAGES\n\n```\n```MaDeUp\n\n```\n```\n\n```\n``` \n\n```\n"
3916 (markdown-gfm-parse-buffer-for-languages)
3917 (should (equal markdown-gfm-used-languages
3918 (list "MaDeUp" "LANGUAGES" "MADEUP")))
3919 (should (equal (car markdown-gfm-used-languages) "MaDeUp"))
3920 (should (equal (car (markdown-gfm-get-corpus)) "MaDeUp"))
3921 (goto-char (point-max))
3922 (markdown-insert-gfm-code-block "newlang")
3923 (should (equal markdown-gfm-used-languages
3924 (list "newlang" "MaDeUp" "LANGUAGES" "MADEUP")))
3925 (should (equal (car markdown-gfm-used-languages) "newlang"))
3926 (should (equal (car (markdown-gfm-get-corpus)) "newlang"))
3927 (let ((markdown-gfm-downcase-languages nil))
3928 (should
3929 (equal (markdown-gfm-get-corpus)
3930 (append markdown-gfm-used-languages
3931 markdown-gfm-additional-languages
3932 markdown-gfm-recognized-languages))))
3933 (let ((markdown-gfm-downcase-languages t))
3934 (should
3935 (equal
3936 (markdown-gfm-get-corpus)
3937 (append markdown-gfm-used-languages
3938 (cl-mapcar #'downcase
3939 (append markdown-gfm-additional-languages
3940 markdown-gfm-recognized-languages))))))))
3942 (ert-deftest test-markdown-gfm/code-block-font-lock ()
3943 "GFM code block font lock test."
3944 (markdown-test-file-gfm "gfm.text"
3945 (markdown-test-range-has-face 2639 2641 markdown-markup-face) ; ```
3946 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face) ; lang
3947 (markdown-test-range-has-face 2647 2728 markdown-pre-face) ; code
3948 (markdown-test-range-has-face 2730 2732 markdown-markup-face))) ; ```
3950 (ert-deftest test-markdown-gfm/code-block-font-lock-2 ()
3951 "GFM code block font lock test without language identifier."
3952 (markdown-test-string-gfm "Plain code block:\n\n```\nfoo\n```\n"
3953 (markdown-test-range-has-face 20 22 markdown-markup-face)
3954 (markdown-test-range-has-face 24 26 markdown-pre-face)
3955 (markdown-test-range-has-face 28 30 markdown-markup-face)))
3957 ;;; Tests for other extensions:
3959 (ert-deftest test-markdown-ext/pandoc-fancy-lists ()
3960 "Test basic support for font lock and filling of Pandoc 'fancy lists'."
3961 (markdown-test-string " #. abc\ndef\n"
3962 ;; font lock
3963 (markdown-test-range-has-face 1 1 nil)
3964 (markdown-test-range-has-face 2 3 markdown-list-face)
3965 (markdown-test-range-has-face 4 11 nil)
3966 ;; filling
3967 (forward-line)
3968 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
3969 (should (string-equal (buffer-string) " #. abc\n def\n"))
3970 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
3971 (should (string-equal (buffer-string) " #. abc\n def\n"))))
3973 (ert-deftest test-markdown-ext/ikiwiki ()
3974 (let ((markdown-enable-wiki-links t)
3975 (markdown-wiki-link-fontify-missing t)
3976 (markdown-wiki-link-search-parent-directories t))
3977 (progn
3978 (find-file "ikiwiki/root")
3979 (unwind-protect
3980 (progn
3981 (markdown-mode)
3982 ;; font lock
3983 (markdown-test-range-has-property 1 11 'font-lock-face markdown-link-face)
3984 (markdown-test-range-has-property 14 33 'font-lock-face markdown-missing-link-face))
3985 (kill-buffer)))
3986 (progn
3987 (find-file "ikiwiki/sub/foo")
3988 (unwind-protect
3989 (progn
3990 (markdown-mode)
3991 ;; font lock
3992 (markdown-test-range-has-property 1 16 'font-lock-face markdown-missing-link-face)
3993 (markdown-test-range-has-property 19 26 'font-lock-face markdown-link-face))
3994 (kill-buffer)))))
3996 (defadvice markdown-live-preview-window-eww
3997 (around markdown-test-create-fake-eww disable)
3998 (setq ad-return-value (get-buffer-create "*eww*")))
4000 (defmacro markdown-test-fake-eww (&rest body)
4001 `(progn
4002 ,@(if (and (fboundp 'libxml-parse-html-region) (require 'eww nil t)) body
4003 `((ad-enable-advice #'markdown-live-preview-window-eww
4004 'around 'markdown-test-create-fake-eww)
4005 (ad-activate #'markdown-live-preview-window-eww)
4006 ,@body
4007 (ad-disable-advice #'markdown-live-preview-window-eww
4008 'around 'markdown-test-create-fake-eww)
4009 (ad-activate #'markdown-live-preview-window-eww)))))
4011 (defmacro markdown-test-eww-or-nothing (test &rest body)
4012 (if (and (fboundp 'libxml-parse-html-region) (require 'eww nil t)
4013 (executable-find markdown-command))
4014 `(progn ,@body)
4015 (message "no eww, no libxml2, or no %s found: skipping %s" markdown-command test)
4016 nil))
4018 (ert-deftest test-markdown-ext/live-preview-exports ()
4019 (markdown-test-temp-file "inline.text"
4020 (unless (and (fboundp 'libxml-parse-html-region) (require 'eww nil t))
4021 (should-error (markdown-live-preview-mode)))
4022 (markdown-test-fake-eww
4023 (markdown-live-preview-mode)
4024 (should (buffer-live-p markdown-live-preview-buffer))
4025 (should (eq (current-buffer)
4026 (with-current-buffer markdown-live-preview-buffer
4027 markdown-live-preview-source-buffer)))
4028 (kill-buffer markdown-live-preview-buffer)
4029 (should (null markdown-live-preview-buffer))
4030 (set-buffer-modified-p t)
4031 (save-buffer) ; should create new export
4032 (should (buffer-live-p markdown-live-preview-buffer)))))
4034 (ert-deftest test-markdown-ext/live-preview-delete-exports ()
4035 (markdown-test-fake-eww
4036 (let ((markdown-live-preview-delete-export 'delete-on-destroy)
4037 file-output)
4038 (markdown-test-temp-file "inline.text"
4039 (markdown-live-preview-mode)
4040 (setq file-output (markdown-export-file-name)))
4041 (should-not (file-exists-p file-output)))
4042 (let ((markdown-live-preview-delete-export 'delete-on-export)
4043 file-output)
4044 (markdown-test-temp-file "inline.text"
4045 (markdown-live-preview-mode)
4046 (setq file-output (markdown-export-file-name))
4047 (should-not (file-exists-p file-output))))
4048 (let ((markdown-live-preview-delete-export nil)
4049 file-output)
4050 (unwind-protect
4051 (markdown-test-temp-file "inline.text"
4052 (markdown-live-preview-mode)
4053 (setq file-output (markdown-export-file-name))
4054 (should (file-exists-p file-output)))
4055 (delete-file file-output)))))
4057 (ert-deftest test-markdown-ext/live-preview-follow-min-max ()
4058 (markdown-test-eww-or-nothing "live-preview-follow-min-max"
4059 (markdown-test-temp-file "inline.text"
4060 (markdown-live-preview-mode)
4061 (should (buffer-live-p markdown-live-preview-buffer))
4062 (should (window-live-p (get-buffer-window markdown-live-preview-buffer)))
4063 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
4064 (goto-char (point-min)))
4065 (goto-char (point-min))
4066 (insert "a test ")
4067 (markdown-live-preview-export)
4068 (let (final-pt final-win-st-diff)
4069 ;; test that still starts at point-min
4070 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
4071 (should (= (window-point) 1))
4072 (should (= (markdown-visual-lines-between-points
4073 (window-start) (window-point))
4075 (set-window-point (selected-window) (point-max))
4076 (setq final-pt (window-point)
4077 final-win-st-diff (markdown-visual-lines-between-points
4078 (window-start) (window-point))))
4079 (goto-char (point-min))
4080 (insert "this is ")
4081 (markdown-live-preview-export)
4082 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
4083 (should (= (window-point) (+ final-pt (length "this is "))))
4084 (should (= (markdown-visual-lines-between-points
4085 (window-start) (window-point))
4086 final-win-st-diff))
4087 ;; test that still starts at point-max, with correct line difference
4088 (goto-char (floor (/ (float (- (point-max) (point-min))) 2)))
4089 (setq final-pt (window-point)
4090 final-win-st-diff (markdown-visual-lines-between-points
4091 (window-start) final-pt)))
4092 (markdown-live-preview-export)
4093 ;; test that still starts at same point, with correct line difference
4094 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
4095 (should (= (window-point) final-pt))
4096 (should (= (markdown-visual-lines-between-points
4097 (window-start) (window-point))
4098 final-win-st-diff)))))))
4100 ;; Tests for imenu
4102 (ert-deftest test-markdown-imenu/metadata ()
4103 "Don't correct header like statement in metadata.
4104 https://github.com/jrblevin/markdown-mode/issues/145"
4105 (markdown-test-string "---
4106 title = \"Blah\"
4107 comments = false
4110 # Header1
4112 ## Header2
4114 (let ((headers (mapcar #'car (markdown-imenu-create-flat-index))))
4115 (should (member "Header1" headers))
4116 (should (member "Header2" headers))
4117 (should-not (member "comments = false" headers)))))
4119 (provide 'markdown-test)
4121 ;;; markdown-test.el ends here