Add test cases for nested font lock
[markdown-mode.git] / tests / markdown-test.el
blobcf9645ea2f03119ca2ad8bd911fd498a3604dfbe
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 `(let ((win (selected-window)))
48 (unwind-protect
49 (with-temp-buffer
50 (set-window-buffer win (current-buffer) t)
51 (erase-buffer)
52 (funcall ,mode)
53 (setq-default indent-tabs-mode nil)
54 (insert ,string)
55 (goto-char (point-min))
56 (funcall markdown-test-font-lock-function)
57 (prog1 ,@body (kill-buffer))))))
59 (defmacro markdown-test-file-mode (mode file &rest body)
60 "Open FILE from `markdown-test-dir' in MODE and execute BODY."
61 `(let ((fn (concat markdown-test-dir ,file)))
62 (save-window-excursion
63 (with-temp-buffer
64 (insert-file-contents fn)
65 (funcall ,mode)
66 (goto-char (point-min))
67 (funcall markdown-test-font-lock-function)
68 ,@body))))
70 (defmacro markdown-test-string (string &rest body)
71 "Run body in a temporary buffer containing STRING in `markdown-mode'."
72 `(markdown-test-string-mode 'markdown-mode ,string ,@body))
73 (def-edebug-spec markdown-test-string (form body))
75 (defmacro markdown-test-file (file &rest body)
76 "Open FILE from `markdown-test-dir' in `markdown-mode' and execute BODY."
77 `(markdown-test-file-mode 'markdown-mode ,file ,@body))
78 (def-edebug-spec markdown-test-file (form body))
80 (defmacro markdown-test-string-gfm (string &rest body)
81 "Run body in a temporary buffer containing STRING in `gfm-mode'."
82 `(markdown-test-string-mode 'gfm-mode ,string ,@body))
83 (def-edebug-spec markdown-test-string-gfm (form body))
85 (defmacro markdown-test-file-gfm (file &rest body)
86 "Open FILE from `markdown-test-dir' in `gfm-mode' and execute BODY."
87 `(markdown-test-file-mode 'gfm-mode ,file ,@body))
88 (def-edebug-spec markdown-test-file-gfm (form body))
90 (defmacro markdown-test-temp-file (file &rest body)
91 "Open FILE from `markdown-test-dir' visiting temp file and execute body.
92 This file is not saved."
93 `(let ((fn (concat markdown-test-dir ,file))
94 (tmp (make-temp-file "markdown-test" nil ".text"))
95 buf)
96 (save-window-excursion
97 (setq buf (find-file tmp))
98 (insert-file-contents fn)
99 (markdown-mode)
100 (goto-char (point-min))
101 (funcall markdown-test-font-lock-function)
102 ,@body
103 (set-buffer-modified-p nil)
104 (kill-buffer buf)
105 (delete-file tmp))))
106 (def-edebug-spec markdown-test-temp-file (form body))
108 (defun markdown-test-range-has-property (begin end prop value)
109 "Verify that the range from BEGIN to END has property PROP equal to VALUE."
110 (let (loc props)
111 (dolist (loc (number-sequence begin end))
112 (setq props (get-char-property loc prop))
113 (cond ((and props (listp props))
114 (should (memq value props)))
116 (should (eq props value)))))))
118 (defun markdown-test-range-has-face (begin end face)
119 "Verify that the range from BEGIN to END has face equal to FACE."
120 (markdown-test-range-has-property begin end 'face face))
122 (defun markdown-test-goto-heading (title)
123 "Move the point to section with TITLE."
124 (let ((regexp (format "\\(^#+ %s\\( #+\\)?\\|^%s\n[=-]+\n\\)" title title)))
125 (if (re-search-forward regexp nil t)
126 (goto-char (match-end 0)))))
128 (defun markdown-test ()
129 "Run all defined tests for `markdown-mode'."
130 (interactive)
131 (ert "markdown"))
133 ;;; Example tests:
135 (ert-deftest test-markdown-example/string ()
136 "An example string test using the `ert' framework."
137 (markdown-test-string "foo *bar* baz"
138 (goto-char 5)
139 (delete-char 1)
140 (should (looking-at "bar"))))
142 (ert-deftest test-markdown-example/file ()
143 "An example file test using the `ert' framework."
144 (markdown-test-file "inline.text"
145 (goto-char 9)
146 (should (looking-at "\*"))))
148 ;;; Basic mode tests:
150 (ert-deftest test-markdown-mode/variables ()
151 "Test `markdown-mode' variables."
152 (markdown-test-file "inline.text"
153 (should (= tab-width 4))
154 (should (eq font-lock-multiline t))
155 (should (eq major-mode 'markdown-mode))))
157 ;;; Element insertion tests:
159 (ert-deftest test-markdown-insertion/blank-line-before-1 ()
160 "Test function `markdown-ensure-blank-line-before' at beginning of line."
161 (markdown-test-file "syntax.text"
162 (search-forward "as plain text")
163 (should (= (point) 1556))
164 (beginning-of-line)
165 (should (= (point) 1505))
166 (should (looking-back "A Markdown-formatted\n" nil))
167 (should (not (markdown-prev-line-blank-p)))
168 (markdown-ensure-blank-line-before)
169 (should (looking-back "A Markdown-formatted\n\n" nil))
170 (should (markdown-prev-line-blank-p))))
172 (ert-deftest test-markdown-insertion/blank-line-before-2 ()
173 "Test function `markdown-ensure-blank-line-before' in middle of line."
174 (markdown-test-file "syntax.text"
175 (search-forward "as plain text")
176 (should (= (point) 1556))
177 (should (looking-back "as plain text" nil))
178 (should (not (markdown-prev-line-blank-p)))
179 (markdown-ensure-blank-line-before)
180 (should (looking-back "as plain text\n\n" nil))
181 (should (markdown-prev-line-blank-p))))
183 (ert-deftest test-markdown-insertion/blank-line-before-3 ()
184 "Test function `markdown-ensure-blank-line-before' with blank line before."
185 (markdown-test-file "syntax.text"
186 (search-forward "web.\n\nMarkdown is not a replacement for HTML")
187 (beginning-of-line)
188 (should (= (point) 2704))
189 (should (looking-back "web.\n\n" nil))
190 (should (markdown-prev-line-blank-p))
191 (markdown-ensure-blank-line-before)
192 (should (= (point) 2704))
193 (should (looking-back "web.\n\n" nil))
194 (should (markdown-prev-line-blank-p))))
196 (ert-deftest test-markdown-insertion/blank-line-before-4 ()
197 "Test function `markdown-ensure-blank-line-before' at beginning of buffer."
198 (markdown-test-string "first line"
199 (beginning-of-line)
200 (should (bobp))
201 (should (= (point-max) 11))
202 (markdown-ensure-blank-line-before)
203 (should (= (point-max) 11))
204 (should (string-equal (buffer-substring (point-min) (point-max))
205 "first line"))
206 (forward-word)
207 (markdown-ensure-blank-line-before)
208 (should (string-equal (buffer-substring (point-min) (point-max))
209 "first\n\n line"))))
211 (ert-deftest test-markdown-insertion/blank-line-after-1 ()
212 "Test function `markdown-ensure-blank-line-after' at end of line."
213 (markdown-test-file "syntax.text"
214 (search-forward "as plain text")
215 (should (= (point) 1556))
216 (end-of-line)
217 (should (= (point) 1573))
218 (should (looking-at "\nlike it's been"))
219 (should (not (markdown-next-line-blank-p)))
220 (markdown-ensure-blank-line-after)
221 (should (looking-at "\n\nlike it's been"))
222 (should (markdown-next-line-blank-p))))
224 (ert-deftest test-markdown-insertion/blank-line-after-2 ()
225 "Test function `markdown-ensure-blank-line-after' in middle of line."
226 (markdown-test-file "syntax.text"
227 (search-forward "as plain text")
228 (should (= (point) 1556))
229 (should (looking-at ", without looking"))
230 (should (not (markdown-next-line-blank-p)))
231 (markdown-ensure-blank-line-after)
232 (should (looking-at "\n\n, without looking"))
233 (should (markdown-next-line-blank-p))))
235 (ert-deftest test-markdown-insertion/blank-line-after-3 ()
236 "Test function `markdown-ensure-blank-line-after' with blank line after."
237 (markdown-test-file "syntax.text"
238 (search-forward "*writing* for the web.")
239 (should (= (point) 2702))
240 (should (looking-at "\n\nMarkdown is not a replacement for HTML"))
241 (should (markdown-next-line-blank-p))
242 (markdown-ensure-blank-line-after)
243 (should (= (point) 2702))
244 (should (looking-at "\n\nMarkdown is not a replacement for HTML"))
245 (should (markdown-next-line-blank-p))))
247 (ert-deftest test-markdown-insertion/blank-line-after-4 ()
248 "Test function `markdown-ensure-blank-line-after' at end of buffer."
249 (markdown-test-string "last line"
250 (end-of-line)
251 (should (eobp))
252 (should (= (point-max) 10))
253 (markdown-ensure-blank-line-after)
254 (should (= (point-max) 10))
255 (should (string-equal (buffer-substring (point-min) (point-max))
256 "last line"))
257 (backward-word)
258 (markdown-ensure-blank-line-after)
259 (should (string-equal (buffer-substring (point-min) (point-max))
260 "last \n\nline"))))
262 (ert-deftest test-markdown-insertion/point-after-unwrap ()
263 "Test new point position calculations after unwrap operations."
264 (markdown-test-string "line **one**\n"
265 (let ((prefix (cons 6 8)) (suffix (cons 11 13)))
266 ;; Prefix
267 (should (eq (markdown-point-after-unwrap 6 prefix suffix) 6))
268 (should (eq (markdown-point-after-unwrap 7 prefix suffix) 6))
269 ;; Word
270 (should (eq (markdown-point-after-unwrap 8 prefix suffix) 6))
271 (should (eq (markdown-point-after-unwrap 9 prefix suffix) 7))
272 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 8))
273 ;; Suffix
274 (should (eq (markdown-point-after-unwrap 11 prefix suffix) 9))
275 (should (eq (markdown-point-after-unwrap 12 prefix suffix) 9))
276 ;; Immediately after
277 (should (eq (markdown-point-after-unwrap 13 prefix suffix) 9))))
278 (markdown-test-string "line _one_\n"
279 (let ((prefix (cons 6 7)) (suffix (cons 10 11)))
280 ;; Prefix
281 (should (eq (markdown-point-after-unwrap 6 prefix suffix) 6))
282 ;; Word
283 (should (eq (markdown-point-after-unwrap 7 prefix suffix) 6))
284 (should (eq (markdown-point-after-unwrap 8 prefix suffix) 7))
285 (should (eq (markdown-point-after-unwrap 9 prefix suffix) 8))
286 ;; Suffix
287 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 9))
288 ;; Immediately after
289 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 9)))))
291 (ert-deftest test-markdown-insertion/unwrap-thing-at-point-italic ()
292 "Test function `markdown-unwrap-thing-at-point' on italics."
293 (markdown-test-file "syntax.text"
294 ;; Unwrap *not*
295 (goto-char 2859)
296 (should (thing-at-point-looking-at markdown-regex-italic))
297 (should (equal (markdown-unwrap-thing-at-point
298 markdown-regex-italic 1 3)
299 (cons 2859 2862)))
300 (should (= (point) 2859))
301 ;; Unwrap *publishing*
302 (goto-char 3064)
303 (should (thing-at-point-looking-at markdown-regex-italic))
304 (should (equal (markdown-unwrap-thing-at-point
305 markdown-regex-italic 1 3)
306 (cons 3060 3070)))
307 (should (= (point) 3063))
308 ;; Unwrap *writing*
309 (goto-char 3101)
310 (should (thing-at-point-looking-at markdown-regex-italic))
311 (should (equal (markdown-unwrap-thing-at-point
312 markdown-regex-italic 1 3)
313 (cons 3093 3100)))
314 (should (= (point) 3100))))
316 (ert-deftest test-markdown-insertion/unwrap-things-in-region-italic ()
317 "Test function `markdown-unwrap-things-in-region' on italics."
318 (markdown-test-file "syntax.text"
319 (should (equal (markdown-unwrap-things-in-region
320 2704 3207 markdown-regex-italic 1 3)
321 (cons 2704 3201)))))
323 (ert-deftest test-markdown-insertion/unwrap-things-in-region-bound ()
324 "Ensure that `markdown-unwrap-things-in-region' respects end bound"
325 (markdown-test-string "**a** **b** **c** **d** **e** **f**"
326 ;; Set region to unrwap a, b, c, and d only. If endpoint is not
327 ;; respected (i.e, not adjusted for character removal), the
328 ;; function will unwrap e and f also.
329 (should (equal (markdown-unwrap-things-in-region
330 1 24 markdown-regex-bold 2 4)
331 (cons 1 8)))
332 (should (string-equal (buffer-string) "a b c d **e** **f**"))))
334 (ert-deftest test-markdown-insertion/unwrap-things-in-region-links ()
335 "Test function `markdown-unwrap-things-in-region' on inline links."
336 (markdown-test-string "a [link](http://jblevins.org/) or [two](/).\n"
337 (should (equal (markdown-unwrap-things-in-region
338 (point-min) (point-max) markdown-regex-link-inline 0 3)
339 (cons 1 16)))
340 (should (string-equal (buffer-string) "a link or two.\n"))))
342 (ert-deftest test-markdown-insertion/toggle-bold ()
343 "Test toggling functionality of `markdown-insert-bold'."
344 (markdown-test-string "one **two** three"
345 (forward-word 2)
346 (markdown-insert-bold)
347 (should (string-equal (buffer-string) "one two three"))
348 (should (= (point) 8))
349 (forward-word)
350 (markdown-insert-bold)
351 (should (= (point) 16))
352 (should (string-equal (buffer-string) "one two **three**"))))
354 (ert-deftest test-markdown-insertion/toggle-italic ()
355 "Test toggling functionality of `markdown-insert-italic'."
356 (markdown-test-string "one *two* three"
357 (forward-word 2)
358 (markdown-insert-italic)
359 (should (string-equal (buffer-string) "one two three"))
360 (should (= (point) 8))
361 (forward-word)
362 (markdown-insert-italic)
363 (should (string-equal (buffer-string) "one two *three*"))
364 (should (= (point) 15))))
366 (ert-deftest test-markdown-insertion/toggle-code ()
367 "Test toggling functionality of `markdown-insert-code'."
368 (markdown-test-string "one `two` three"
369 (forward-word 2)
370 (markdown-insert-code)
371 (should (string-equal (buffer-string) "one two three"))
372 (should (= (point) 8))
373 (forward-word)
374 (markdown-insert-code)
375 (should (string-equal (buffer-string) "one two `three`"))
376 (should (= (point) 15))))
378 (ert-deftest test-markdown-insertion/toggle-wiki-link-alias-first ()
379 "Test toggling of `markdown-insert-wiki-link' with alias first.
380 Test point position upon removal and insertion."
381 (let ((markdown-wiki-link-alias-first t))
382 (markdown-test-string "[[text|page]]"
383 (goto-char 5) ; point in interior of alias text, at 'x'
384 (call-interactively 'markdown-insert-wiki-link)
385 (should (= (point) 3)) ; leave point at, at 'x'
386 (should (string-equal (buffer-string) "text"))
387 (call-interactively 'markdown-insert-wiki-link)
388 (should (= (point) 5)) ; leave point at, at 'x'
389 (should (string-equal (buffer-string) "[[text]]")))
390 (markdown-test-string "[[text|page]]"
391 (goto-char 10) ; point in interior of link text, at 'g'
392 (call-interactively 'markdown-insert-wiki-link)
393 (should (= (point) 5)) ; leave point at end of alias text
394 (should (string-equal (buffer-string) "text"))
395 (call-interactively 'markdown-insert-wiki-link)
396 (should (= (point) 7)) ; leave point at end of alias text
397 (should (string-equal (buffer-string) "[[text]]")))))
399 (ert-deftest test-markdown-insertion/toggle-wiki-link-alias-last ()
400 "Test toggling of `markdown-insert-wiki-link' with alias last.
401 Test point position upon removal and insertion."
402 (let ((markdown-wiki-link-alias-first nil))
403 (markdown-test-string "[[page|text]]"
404 (goto-char 10) ; point in interior of alias text, at 'x'
405 (call-interactively 'markdown-insert-wiki-link)
406 (goto-char 3) ; leave point at, at 'x'
407 (should (string-equal (buffer-string) "text"))
408 (call-interactively 'markdown-insert-wiki-link)
409 (should (= (point) 5)) ; leave point at, at 'x'
410 (should (string-equal (buffer-string) "[[text]]")))
411 (markdown-test-string "[[page|text]]"
412 (goto-char 3) ; point in interior of link text, at 'g'
413 (call-interactively 'markdown-insert-wiki-link)
414 (should (= (point) 1)) ; leave point at beginning of alias text
415 (should (string-equal (buffer-string) "text"))
416 (call-interactively 'markdown-insert-wiki-link)
417 (should (= (point) 3)) ; leave point at beginning of alias text
418 (should (string-equal (buffer-string) "[[text]]")))))
420 (ert-deftest test-markdown-insertion/bold-region ()
421 "Test region functionality of `markdown-insert-bold'."
422 (markdown-test-string "one two three"
423 (push-mark (point) t t)
424 (forward-word 2)
425 (markdown-insert-bold)
426 (should (string-equal (buffer-string) "**one two** three"))
427 (should (= (point) 10))))
429 (ert-deftest test-markdown-insertion/italic-region ()
430 "Test region functionality of `markdown-insert-italic'."
431 (markdown-test-string "one two three"
432 (transient-mark-mode)
433 (push-mark (point) t t)
434 (forward-word 2)
435 (markdown-insert-italic)
436 (should (string-equal (buffer-string) "*one two* three"))
437 (should (= (point) 9))))
439 (ert-deftest test-markdown-insertion/code-region ()
440 "Test region functionality of `markdown-insert-code'."
441 (markdown-test-string "one two three"
442 (transient-mark-mode)
443 (push-mark (point) t t)
444 (forward-word 2)
445 (markdown-insert-code)
446 (should (string-equal (buffer-string) "`one two` three"))
447 (should (= (point) 9))))
449 (ert-deftest test-markdown-insertion/atx-line ()
450 "Test ATX header insertion without region."
451 (markdown-test-string "line one\nline two\n"
452 (forward-word)
453 (markdown-insert-header-atx-1)
454 (should (string-equal (buffer-substring (point-min) (point-max))
455 "# line one #\n\nline two\n"))
456 (forward-line 2)
457 (markdown-insert-header-atx-2)
458 (should (string-equal (buffer-substring (point-min) (point-max))
459 "# line one #\n\n## line two ##\n\n"))))
461 (ert-deftest test-markdown-insertion/atx-region ()
462 "Test ATX header insertion with region."
463 (markdown-test-string "line one\nline two\n"
464 (transient-mark-mode)
465 (forward-char 5)
466 (push-mark (point) t t)
467 (forward-word)
468 (should (string-equal (buffer-substring (region-beginning) (region-end))
469 "one"))
470 (markdown-insert-header-atx-4)
471 (should (string-equal (buffer-substring (point-min) (point-max))
472 "line \n\n#### one ####\n\nline two\n"))))
474 (ert-deftest test-markdown-insertion/atx-blank ()
475 "Test ATX header insertion on blank line."
476 (markdown-test-string "line one\n\nline two\n"
477 (forward-line)
478 (markdown-insert-header-atx-3)
479 (should (string-equal (buffer-substring (point-min) (point-max))
480 "line one\n\n### ###\n\nline two\n"))
481 (should (= (point) 15))
482 (should (looking-at " ###\n"))))
484 (ert-deftest test-markdown-insertion/atx-region-whitespace ()
485 "Test ATX header insertion using a region with whitespace."
486 (markdown-test-string " line one\n\nline two\n \n"
487 (transient-mark-mode)
488 (push-mark (point) t t)
489 (goto-char (point-max))
490 (markdown-insert-header-atx-2)
491 (should (string-equal (buffer-substring (point-min) (point-max))
492 "## line one line two ##"))
493 (should (= (point) 21))
494 (should (looking-at " ##"))))
496 (ert-deftest test-markdown-insertion/atx-line-whitespace ()
497 "Test ATX header insertion using current line with whitespace."
498 (markdown-test-string " line one \n\nline two\n"
499 (goto-char (line-end-position))
500 (markdown-insert-header-atx-3)
501 (should (string-equal (buffer-substring (point-min) (point-max))
502 "### line one ###\n\nline two\n"))
503 (should (= (point) 13))
504 (should (looking-at " ###\n"))))
506 (ert-deftest test-markdown-insertion/atx-replace-atx ()
507 "Test ATX header insertion when replacing an existing ATX header."
508 (markdown-test-string "## replace ##\n"
509 (markdown-insert-header-atx-4)
510 (should (string-equal (buffer-string) "#### replace ####\n\n"))
511 (should (looking-at " ####\n"))))
513 (ert-deftest test-markdown-insertion/atx-replace-setext-1 ()
514 "Test ATX header insertion when replacing an existing setext header."
515 (markdown-test-string "replace\n=======\n"
516 (markdown-insert-header-atx-2)
517 (should (string-equal (buffer-string) "## replace ##\n\n"))
518 (should (looking-at " ##\n"))))
520 (ert-deftest test-markdown-insertion/atx-replace-setext-2 ()
521 "Test ATX header insertion when replacing an existing setext header."
522 (markdown-test-string "replace\n-------\n"
523 (markdown-insert-header-atx-5)
524 (should (string-equal (buffer-string) "##### replace #####\n\n"))
525 (should (looking-at " #####\n"))))
527 (ert-deftest test-markdown-insertion/setext-line ()
528 "Test setext header insertion without region."
529 (markdown-test-string "line one\nline two\n"
530 (forward-word)
531 (markdown-insert-header-setext-1)
532 (should (string-equal (buffer-substring (point-min) (point-max))
533 "line one\n========\n\nline two\n"))
534 (forward-line 3)
535 (markdown-insert-header-setext-2)
536 (should (string-equal (buffer-substring (point-min) (point-max))
537 "line one\n========\n\nline two\n--------\n\n"))))
539 (ert-deftest test-markdown-insertion/setext-region ()
540 "Test setext header insertion with region."
541 (markdown-test-string "line one\nline two\n"
542 (transient-mark-mode)
543 (forward-char 5)
544 (push-mark (point) t t)
545 (forward-word)
546 (should (string-equal (buffer-substring (region-beginning) (region-end))
547 "one"))
548 (markdown-insert-header-setext-1)
549 (should (string-equal (buffer-substring (point-min) (point-max))
550 "line \n\none\n===\n\nline two\n"))))
552 (ert-deftest test-markdown-insertion/setext-blank ()
553 "Test setext header insertion on blank line."
554 (markdown-test-string "line one\n\nline two\n"
555 (forward-line)
556 (markdown-insert-header 2 "foo" t)
557 (should (string-equal (buffer-substring (point-min) (point-max))
558 "line one\n\nfoo\n---\n\nline two\n"))
559 (should (= (point) 14))
560 (should (looking-at "\n---"))))
562 (ert-deftest test-markdown-insertion/setext-region-whitespace ()
563 "Test setext header insertion using a region with whitespace."
564 (markdown-test-string " line one\n\nline two\n \n"
565 (transient-mark-mode)
566 (push-mark (point) t t)
567 (goto-char (point-max))
568 (markdown-insert-header-setext-1)
569 (should (string-equal (buffer-substring (point-min) (point-max))
570 "line one line two\n================="))
571 (should (= (point) 18))
572 (should (looking-at "\n===="))))
574 (ert-deftest test-markdown-insertion/setext-line-whitespace ()
575 "Test setext header insertion using current line with whitespace."
576 (markdown-test-string " line one \n\nline two\n"
577 (goto-char (line-end-position))
578 (markdown-insert-header-setext-2)
579 (should (string-equal (buffer-substring (point-min) (point-max))
580 "line one\n--------\n\nline two\n"))
581 (should (= (point) 9))
582 (should (looking-at "\n---"))))
584 (ert-deftest test-markdown-insertion/setext-replace-atx ()
585 "Test setext header insertion when replacing an existing ATX header."
586 (markdown-test-string "## replace ##\n"
587 (markdown-insert-header-setext-1)
588 (should (string-equal (buffer-string) "replace\n=======\n\n"))
589 (should (looking-at "\n==="))))
591 (ert-deftest test-markdown-insertion/setext-replace-setext-1 ()
592 "Test setext header insertion when replacing an existing setext title."
593 (markdown-test-string "replace\n=======\n"
594 (markdown-insert-header-setext-2)
595 (should (string-equal (buffer-string) "replace\n-------\n\n"))
596 (should (looking-at "\n---"))))
598 (ert-deftest test-markdown-insertion/setext-replace-setext-2 ()
599 "Test setext header insertion when replacing an existing setext section."
600 (markdown-test-string "replace\n-------\n"
601 (markdown-insert-header-setext-1)
602 (should (string-equal (buffer-string) "replace\n=======\n\n"))
603 (should (looking-at "\n==="))))
605 (ert-deftest test-markdown-insertion/header-dwim ()
606 "Test 'do what I mean' header insertion."
607 (markdown-test-file "outline.text"
608 (call-interactively 'markdown-insert-header-dwim)
609 (should (looking-at " #$"))
610 (end-of-defun 2)
611 (call-interactively 'markdown-insert-header-dwim)
612 (beginning-of-line)
613 (should (looking-at "^# #$"))
614 (end-of-defun 3)
615 (call-interactively 'markdown-insert-header-dwim)
616 (beginning-of-line)
617 (should (looking-at "^### ###$"))))
619 (ert-deftest test-markdown-insertion/header-dwim-prefix ()
620 "Test 'do what I mean' header insertion with prefix arguments."
621 (let ((tests (list '(nil . "## abc ##")
622 '(1 . "# abc #")
623 '(2 . "## abc ##")
624 '(3 . "### abc ###")
625 '(4 . "#### abc ####")
626 '(5 . "##### abc #####")
627 '(6 . "###### abc ######")
628 '((4) . "# abc #")
629 '((16) . "### abc ###"))))
630 (dolist (test tests)
631 (markdown-test-string "## atx\n\nabc"
632 (goto-char (point-max))
633 (let ((current-prefix-arg (car test)))
634 (call-interactively 'markdown-insert-header-dwim)
635 (should (string-equal
636 (buffer-substring (line-beginning-position) (line-end-position))
637 (cdr test))))))))
639 (ert-deftest test-markdown-insertion/header-setext-dwim-prefix ()
640 "Test 'do what I mean' header insertion with prefix arguments."
641 (let ((tests (list '(nil . "abc\n---")
642 '(1 . "abc\n===")
643 '(2 . "abc\n---")
644 '(3 . "### abc ###")
645 '(4 . "#### abc ####")
646 '(5 . "##### abc #####")
647 '(6 . "###### abc ######")
648 '((4) . "abc\n===")
649 '((16) . "### abc ###"))))
650 (dolist (test tests)
651 (markdown-test-string "atx\n---\n\nabc"
652 (goto-char (point-max))
653 (let ((current-prefix-arg (car test)))
654 (call-interactively 'markdown-insert-header-setext-dwim)
655 (should (string-equal
656 (buffer-substring (line-beginning-position) (line-end-position 2))
657 (cdr test))))))))
659 (ert-deftest test-markdown-insertion/remove-header ()
660 "Test ATX and setext header."
661 (markdown-test-string
662 "# atx1\n\n## atx2 ##\n\nsetext1\n=======\n\nsetext2\n-------\n"
663 (should (equal (markdown-remove-header) (cons 1 5)))
664 (forward-line)
665 (should (not (markdown-remove-header)))
666 (forward-line)
667 (should (equal (markdown-remove-header) (cons 7 11)))
668 (forward-line)
669 (should (not (markdown-remove-header)))
670 (forward-line)
671 (should (equal (markdown-remove-header) (cons 13 20)))
672 (forward-line)
673 (should (not (markdown-remove-header)))
674 (forward-line)
675 (should (equal (markdown-remove-header) (cons 22 29)))
676 (should (string-equal (buffer-string)
677 "atx1\n\natx2\n\nsetext1\n\nsetext2\n"))))
679 (ert-deftest test-markdown-insertion/italic-unwrap-region ()
680 "A test of inserting italics with italic text in the region."
681 (markdown-test-string "*foo* bar *baz*"
682 (transient-mark-mode)
683 (push-mark (point) t t)
684 (end-of-line)
685 (markdown-insert-italic)
686 (should (string-equal (buffer-string) "*foo bar baz*"))))
688 (ert-deftest test-markdown-insertion/bold-unwrap-region ()
689 "A test of inserting bold with italic text in the region."
690 (markdown-test-string "*foo* **bar** *baz*"
691 (transient-mark-mode)
692 (push-mark (point) t t)
693 (end-of-line)
694 (markdown-insert-bold)
695 (should (string-equal (buffer-string) "***foo* bar *baz***"))))
697 (ert-deftest test-markdown-insertion/code-unwrap-region ()
698 "A test of inserting code with code already in the region."
699 (markdown-test-string "`foo` *bar* `baz`"
700 (transient-mark-mode)
701 (push-mark (point) t t)
702 (end-of-line)
703 (markdown-insert-code)
704 (should (string-equal (buffer-string) "`foo *bar* baz`"))))
706 (ert-deftest test-markdown-insertion/hr-order ()
707 "Test inserting horizontal rules."
708 (dotimes (n (length markdown-hr-strings))
709 (markdown-test-string ""
710 (let ((current-prefix-arg n))
711 (call-interactively 'markdown-insert-hr))
712 (should (string-equal (buffer-string) (nth (1- n) markdown-hr-strings))))))
714 (ert-deftest test-markdown-insertion/hr-prefix ()
715 "Test inserting horizontal rule with C-u prefix."
716 (markdown-test-string ""
717 (let ((current-prefix-arg '(4)))
718 (call-interactively 'markdown-insert-hr))
719 (should (string-equal (buffer-string) (car (last markdown-hr-strings))))))
721 (ert-deftest test-markdown-insertion/hr-bob ()
722 "Test inserting horizontal rule at beginning of buffer."
723 (markdown-test-string "one line\n"
724 (call-interactively 'markdown-insert-hr)
725 (should (string-equal (buffer-string)
726 (concat (car markdown-hr-strings)
727 "\n\none line\n")))))
729 (ert-deftest test-markdown-insertion/hr-eob ()
730 "Test inserting horizontal rule at end of buffer."
731 (markdown-test-string "one line\n"
732 (forward-line)
733 (call-interactively 'markdown-insert-hr)
734 (should (string-equal (buffer-string)
735 (concat "one line\n\n" (car markdown-hr-strings))))))
737 (ert-deftest test-markdown-insertion/hr-mob ()
738 "Test inserting horizontal rule in middle of buffer."
739 (markdown-test-string "one line\n"
740 (forward-word)
741 (let ((markdown-hr-strings '("----------")))
742 (call-interactively 'markdown-insert-hr)
743 (should (string-equal (buffer-string)
744 (concat "one\n\n" (car markdown-hr-strings)
745 "\n\n line\n"))))))
747 (ert-deftest test-markdown-insertion/pre-region-1 ()
748 "Test `markdown-pre-region'."
749 ;; Simple test as non-interactive command
750 (markdown-test-string "line one\nline two\n"
751 (markdown-pre-region (line-beginning-position) (line-end-position))
752 (should (string-equal (buffer-string) " line one\n\nline two\n")))
753 ;; Test removal of whitespace before and after region
754 (markdown-test-string "line one abc\nline two\n"
755 (markdown-pre-region 6 9)
756 (should (string-equal (buffer-string) "line\n\n one\n\nabc\nline two\n")))
757 ;; Simple test as interactive command
758 (markdown-test-string "line one\nline two\n"
759 (push-mark (point) t t)
760 (forward-line 2)
761 (call-interactively 'markdown-pre-region)
762 (should (string-equal (buffer-string) " line one\n line two\n\n"))))
764 (ert-deftest test-markdown-insertion/blockquote-region-1 ()
765 "Test `markdown-blockquote-region'."
766 ;; Simple test as non-interactive command
767 (markdown-test-string "line one\nline two\n"
768 (markdown-blockquote-region (line-beginning-position) (line-end-position))
769 (should (string-equal (buffer-string) "> line one\n\nline two\n")))
770 ;; Test removal of whitespace before and after region
771 (markdown-test-string "line one abc\nline two\n"
772 (markdown-blockquote-region 6 9)
773 (should (string-equal (buffer-string) "line\n\n> one\n\nabc\nline two\n")))
774 ;; Simple test as interactive command
775 (markdown-test-string "line one\nline two\n"
776 (push-mark (point) t t)
777 (forward-line 2)
778 (call-interactively 'markdown-blockquote-region)
779 (should (string-equal (buffer-string) "> line one\n> line two\n\n"))))
781 (ert-deftest test-markdown-insertion/pre-nested-lists ()
782 "Test `markdown-pre-indentation' and `markdown-insert-pre' with nested list."
783 (markdown-test-string "* item\n * item\n"
784 ;; before the first item
785 (should (string-equal (markdown-pre-indentation (point)) " "))
786 (markdown-insert-pre)
787 (beginning-of-line)
788 (should (markdown-prev-line-blank-p))
789 (should (looking-at "^ $"))
790 (should (markdown-next-line-blank-p))
791 ;; before the second item
792 (forward-line 3)
793 (should (string-equal (markdown-pre-indentation (point)) " "))
794 (markdown-insert-pre)
795 (beginning-of-line)
796 (should (markdown-prev-line-blank-p))
797 (should (looking-at "^ $"))
798 (should (markdown-next-line-blank-p))
799 ;; after the second item
800 (forward-line 3)
801 (should (string-equal (markdown-pre-indentation (point)) " "))
802 (markdown-insert-pre)
803 (beginning-of-line)
804 (should (markdown-prev-line-blank-p))
805 (should (looking-at "^ $"))
806 (should (markdown-next-line-blank-p))))
808 (ert-deftest test-markdown-insertion/pre-faux-list ()
809 "Test `markdown-pre-indentation' following a list-marker in a pre block."
810 (markdown-test-string " * pre block, not a list item\n"
811 (should (string-equal (markdown-pre-indentation (point-max)) " "))))
813 (ert-deftest test-markdown-insertion/blockquote-nested-lists ()
814 "Test blockquote insertion in a nested list context."
815 (markdown-test-string "* item\n * item\n"
816 ;; before the first item
817 (should (string-equal (markdown-blockquote-indentation (point)) ""))
818 (markdown-insert-blockquote)
819 (beginning-of-line)
820 (should (markdown-prev-line-blank-p))
821 (should (looking-at "^> $"))
822 (should (markdown-next-line-blank-p))
823 ;; before the second item
824 (forward-line 3)
825 (should (string-equal (markdown-blockquote-indentation (point)) " "))
826 (markdown-insert-blockquote)
827 (beginning-of-line)
828 (should (markdown-prev-line-blank-p))
829 (should (looking-at "^ > $"))
830 (should (markdown-next-line-blank-p))
831 ;; after the second item
832 (forward-line 3)
833 (should (string-equal (markdown-blockquote-indentation (point)) " "))
834 (markdown-insert-blockquote)
835 (beginning-of-line)
836 (should (markdown-prev-line-blank-p))
837 (should (looking-at "^ > $"))
838 (should (markdown-next-line-blank-p))))
840 (ert-deftest test-markdown-insertion/empty-italic ()
841 "Test `markdown-insert-italic' with no word at point and no region."
842 (markdown-test-string ""
843 (call-interactively 'markdown-insert-italic)
844 (should (string-equal (buffer-string) "**"))
845 (should (= (point) 2))))
847 (ert-deftest test-markdown-insertion/empty-bold ()
848 "Test `markdown-insert-bold' with no word at point and no region."
849 (markdown-test-string ""
850 (call-interactively 'markdown-insert-bold)
851 (should (string-equal (buffer-string) "****"))
852 (should (= (point) 3))))
854 (ert-deftest test-markdown-insertion/uri ()
855 "Test `markdown-insert-uri'."
856 (markdown-test-string "http://jblevins.org/projects/markdown-mode/"
857 (call-interactively 'markdown-insert-uri)
858 (should (string-equal (buffer-string) "<http://jblevins.org/projects/markdown-mode/>"))
859 (should (= (point) 2))
860 (call-interactively 'markdown-insert-uri)
861 (should (string-equal (buffer-string) "http://jblevins.org/projects/markdown-mode/"))
862 (should (= (point) 1))
863 (erase-buffer)
864 (call-interactively 'markdown-insert-uri)
865 (should (string-equal (buffer-string) "<>"))
866 (should (= (point) 2))))
868 (ert-deftest test-markdown-insertion/list-item ()
869 "Test `markdown-insert-list-item' on several lists."
870 ;; No existing list
871 (markdown-test-string "abc"
872 (goto-char (point-max))
873 (call-interactively 'markdown-insert-list-item)
874 (should (string-equal (buffer-string) "abc\n * "))
875 (should (= (point) 9)))
876 ;; Following a list item, on the same line
877 (markdown-test-string " * foo"
878 (goto-char (point-max))
879 (call-interactively 'markdown-insert-list-item)
880 (should (string-equal (buffer-string) " * foo\n * ")))
881 ;; Following a list item, on the next line
882 (markdown-test-string "- foo\n"
883 (goto-char (point-max))
884 (call-interactively 'markdown-insert-list-item)
885 (should (string-equal (buffer-string) "- foo\n- ")))
886 ;; Following a list item, after a blank line
887 (markdown-test-string "- foo\n\n"
888 (goto-char (point-max))
889 (call-interactively 'markdown-insert-list-item)
890 (should (string-equal (buffer-string) "- foo\n\n- ")))
891 ;; Preceding a list item
892 (markdown-test-string "- foo\n"
893 (goto-char (point-min))
894 (call-interactively 'markdown-insert-list-item)
895 (should (string-equal (buffer-string) "- \n- foo\n")))
896 ;; Preceding a list item and a blank line
897 (markdown-test-string "\n\n- foo\n"
898 (goto-char (point-min))
899 (call-interactively 'markdown-insert-list-item)
900 (should (string-equal (buffer-string) "- \n\n- foo\n")))
901 ;; In the middle of a list item
902 (markdown-test-string "- foo bar\n"
903 (forward-word)
904 (call-interactively 'markdown-insert-list-item)
905 (should (string-equal (buffer-string) "- foo\n- bar\n")))
906 ;; Before a list marker, but not at beginning of line
907 (markdown-test-string " - foo\n"
908 (forward-char 2)
909 (call-interactively 'markdown-insert-list-item)
910 (should (string-equal (buffer-string) " - \n - foo\n")))
911 ;; Following an ordered list item
912 (markdown-test-string "6. foo"
913 (goto-char (point-max))
914 (call-interactively 'markdown-insert-list-item)
915 (should (string-equal (buffer-string) "6. foo\n7. ")))
916 ;; Following a nested ordered list item
917 (markdown-test-string "6. foo\n 1. bar"
918 (goto-char (point-max))
919 (call-interactively 'markdown-insert-list-item)
920 (should (string-equal (buffer-string) "6. foo\n 1. bar\n 2. ")))
921 ;; Preceding an ordered list item
922 (markdown-test-string "\n1. foo\n2. bar"
923 (goto-char (point-min))
924 (call-interactively 'markdown-insert-list-item)
925 (should (string-equal (buffer-string) "1. \n1. foo\n2. bar")))
926 ;; Preserve previous spacing in ordered list
927 (markdown-test-string "1. foo"
928 (goto-char (point-max))
929 (call-interactively 'markdown-insert-list-item)
930 (should (string-equal (buffer-string) "1. foo\n2. ")))
931 ;; Adjust spacing for number width changes (e.g., 9 -> 10)
932 (markdown-test-string "9. foo"
933 (goto-char (point-max))
934 (call-interactively 'markdown-insert-list-item)
935 (should (string-equal (buffer-string) "9. foo\n10. ")))
936 ;; Don't adjust spacing for number width changes if no extra whitespace
937 (markdown-test-string "99. foo"
938 (goto-char (point-max))
939 (call-interactively 'markdown-insert-list-item)
940 (should (string-equal (buffer-string) "99. foo\n100. ")))
941 ;; Don't adjust spacing if tabs are used as whitespace
942 (markdown-test-string "9.\tfoo"
943 (goto-char (point-max))
944 (call-interactively 'markdown-insert-list-item)
945 (should (string-equal (buffer-string) "9.\tfoo\n10.\t"))))
947 (ert-deftest test-markdown-insertion/nested-list-marker ()
948 "Test marker detection for `markdown-insert-list-item'."
949 (markdown-test-string
950 "1. A\n * AA\n 1. AAA"
951 (goto-char (point-max))
952 (let ((current-prefix-arg '(4)))
953 (call-interactively 'markdown-insert-list-item))
954 (should (eq (point) 36))
955 (should (looking-back "\* "))
956 (should (string-equal
957 (buffer-string)
958 "1. A\n * AA\n 1. AAA\n * "))
959 (let ((current-prefix-arg '(4)))
960 (call-interactively 'markdown-insert-list-item))
961 (should (eq (point) 40))
962 (should (looking-back "2\. "))
963 (should (string-equal
964 (buffer-string)
965 "1. A\n * AA\n 1. AAA\n * \n2. "))
966 (let ((current-prefix-arg '(4)))
967 (call-interactively 'markdown-insert-list-item))
968 (should (eq (point) 44))
969 (should (looking-back "3\. "))
970 (should (string-equal
971 (buffer-string)
972 "1. A\n * AA\n 1. AAA\n * \n2. \n3. "))))
974 (ert-deftest test-markdown-insertion/reference-link ()
975 "Basic tests for `markdown-insert-reference-link'."
976 ;; Test optional parameters (leave point after link)
977 (markdown-test-string ""
978 (markdown-insert-reference-link "abc" "1")
979 (should (string-equal (buffer-string) "[abc][1]"))
980 (should (= (point) 9)))
981 ;; Full link without title (leave point after link)
982 (markdown-test-string ""
983 (markdown-insert-reference-link "link" "label" "http://jblevins.org/")
984 (should (string-equal (buffer-string) "[link][label]\n\n[label]: http://jblevins.org/\n"))
985 (should (= (point) 14)))
986 ;; Full link without label or title (leave point after link)
987 (markdown-test-string ""
988 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
989 (should (string-equal (buffer-string) "[link][]\n\n[link]: http://jblevins.org/\n"))
990 (should (= (point) 9)))
991 ;; Link only with no label, URL, or title (leave point after link)
992 (markdown-test-string ""
993 (markdown-insert-reference-link "link" "")
994 (should (string-equal (buffer-string) "[link][]"))
995 (should (= (point) 9))))
997 (ert-deftest test-markdown-insertion/reference-link-end ()
998 "Basic reference link insertion test for 'end location."
999 (let ((markdown-reference-location 'end))
1000 (markdown-test-string "first para\n\nsecond para\n"
1001 (end-of-line)
1002 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
1003 (should (= (point) 19))
1004 (goto-char (point-min))
1005 (forward-line 4)
1006 (should (looking-at "\\[link\\]: http://jblevins.org/")))))
1008 (ert-deftest test-markdown-insertion/reference-link-immediately ()
1009 "Basic reference link insertion test for 'immediately location."
1010 (let ((markdown-reference-location 'immediately))
1011 (markdown-test-string "first para\n\nsecond para\n"
1012 (end-of-line)
1013 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
1014 (should (= (point) 19))
1015 (goto-char (point-min))
1016 (forward-line 2)
1017 (should (looking-at "\\[link\\]: http://jblevins.org/")))))
1019 (ert-deftest test-markdown-insertion/reference-link-header ()
1020 "Basic reference link insertion test for 'header location."
1021 (let ((markdown-reference-location 'header))
1022 (markdown-test-string "par one\n\npar two\n\n### header\n"
1023 (end-of-line)
1024 (markdown-insert-reference-link "link" "" "")
1025 (should (= (point) 35))
1026 (should (looking-back "\\[link\\]: " nil)))))
1028 (ert-deftest test-markdown-insertion/inline-to-reference-link ()
1029 "Inline link to reference link conversion."
1030 (markdown-test-string "[text](http://jblevins.org/ \"title\")"
1031 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-reference-link-dwim RET 1 RET"))
1032 (should (string-equal (buffer-string) "[text][1]\n\n[1]: http://jblevins.org/ \"title\"\n")))
1033 (markdown-test-string "[text](http://jblevins.org/)"
1034 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-reference-link-dwim RET 1 RET"))
1035 (should (string-equal (buffer-string) "[text][1]\n\n[1]: http://jblevins.org/\n"))))
1037 (ert-deftest test-markdown-insertion/inline-link ()
1038 "Basic tests for `markdown-insert-link'."
1039 ;; Test empty markup insertion (leave point in square brackets)
1040 (markdown-test-string "abc "
1041 (end-of-line)
1042 (call-interactively 'markdown-insert-link)
1043 (should (string-equal (buffer-string) "abc []()"))
1044 (should (= (point) 6)))
1045 ;; Test with word at point (leave point in parentheses)
1046 (markdown-test-string "abc def ghi"
1047 (forward-word 2)
1048 (call-interactively 'markdown-insert-link)
1049 (should (string-equal (buffer-string) "abc [def]() ghi"))
1050 (should (= (point) 11)))
1051 ;; Test with region (leave point in parentheses)
1052 (markdown-test-string "abc def ghi"
1053 (transient-mark-mode)
1054 (push-mark (point) t t)
1055 (forward-word 2)
1056 (call-interactively 'markdown-insert-link)
1057 (should (string-equal (buffer-string) "[abc def]() ghi"))
1058 (should (= (point) 11))))
1060 ;;; Footnote tests:
1062 (ert-deftest test-markdown-footnote/basic-end ()
1063 "Basic footnote insertion and deletion tests for 'end location."
1064 (let ((markdown-footnote-location 'end))
1065 (markdown-test-string "first line\nsecond line\n"
1066 ;; new buffer with no footnotes
1067 (should (= markdown-footnote-counter 0))
1068 ;; footnote insertion
1069 (end-of-line)
1070 (markdown-insert-footnote)
1071 (should (= (point) 35))
1072 (should (= markdown-footnote-counter 1))
1073 (should (looking-back "\\[^1\\]: " nil))
1074 ;; kill with point in footnote definition
1075 (insert "footnote text")
1076 (let (kill-ring)
1077 (markdown-footnote-kill))
1078 (should (= (point) 24))
1079 (should (bolp))
1080 (should (string-equal (buffer-string) "first line\nsecond line\n"))
1081 ;; insertion, counter should increment
1082 (goto-char (point-min))
1083 (end-of-line)
1084 (markdown-insert-footnote)
1085 (should (= (point) 35))
1086 (should (= markdown-footnote-counter 2))
1087 (should (looking-back "\\[^2\\]: " nil))
1088 (insert "footnote text")
1089 ;; return to marker
1090 (markdown-footnote-return)
1091 (should (= (point) 15))
1092 (should (looking-back "\\[^2\\]" nil))
1093 ;; kill with point at marker
1094 (let (kill-ring)
1095 (markdown-footnote-kill))
1096 (should (= (point) 11))
1097 (should (eolp))
1098 (should (string-equal (buffer-string) "first line\nsecond line\n")))))
1100 (ert-deftest test-markdown-footnote/basic-immediately ()
1101 "Basic footnote insertion and deletion tests for 'immediately location."
1102 (let ((markdown-footnote-location 'immediately))
1103 (markdown-test-string "first paragraph\n\nsecond paragraph\n"
1104 ;; new buffer with no footnotes
1105 (should (= markdown-footnote-counter 0))
1106 ;; footnote insertion
1107 (end-of-line)
1108 (markdown-insert-footnote)
1109 (should (= (point) 28))
1110 (should (= markdown-footnote-counter 1))
1111 (should (looking-back "\\[^1\\]: " nil))
1112 ;; kill with point in footnote definition
1113 (insert "footnote text")
1114 (let (kill-ring)
1115 (markdown-footnote-kill))
1116 (should (= (point) 18))
1117 (should (bolp))
1118 (should (string-equal (buffer-string)
1119 "first paragraph\n\nsecond paragraph\n")))))
1121 (ert-deftest test-markdown-footnote/basic-header ()
1122 "Basic footnote insertion and deletion tests for 'header location."
1123 (let ((markdown-footnote-location 'header))
1124 (markdown-test-string "par one\n\npar two\n\n### header\n"
1125 ;; new buffer with no footnotes
1126 (should (= markdown-footnote-counter 0))
1127 ;; footnote insertion
1128 (end-of-line)
1129 (markdown-insert-footnote)
1130 (should (= (point) 29))
1131 (should (= markdown-footnote-counter 1))
1132 (should (looking-back "\\[^1\\]: " nil))
1133 ;; kill with point in footnote definition
1134 (insert "footnote text")
1135 (let (kill-ring)
1136 (markdown-footnote-kill))
1137 (should (= (point) 19))
1138 (should (bolp))
1139 (should (string-equal (buffer-string)
1140 "par one\n\npar two\n\n### header\n"))
1141 ;; insertion, counter should increment
1142 (goto-char (point-min))
1143 (end-of-line)
1144 (markdown-insert-footnote)
1145 (should (= (point) 29))
1146 (should (= markdown-footnote-counter 2))
1147 (should (looking-back "\\[^2\\]: " nil))
1148 (insert "footnote text")
1149 ;; return to marker
1150 (markdown-footnote-return)
1151 (should (= (point) 12))
1152 (should (looking-back "\\[^2\\]" nil))
1153 ;; kill with point at marker
1154 (let (kill-ring)
1155 (markdown-footnote-kill))
1156 (should (= (point) 8))
1157 (should (eolp))
1158 (should (string-equal (buffer-string)
1159 "par one\n\npar two\n\n### header\n")))))
1161 (ert-deftest test-markdown-footnote/kill-empty-text ()
1162 "Test killing a footnote with marker but no text."
1163 (markdown-test-string "no text[^1]\n\n[^1]: \n"
1164 (end-of-line)
1165 (markdown-footnote-goto-text)
1166 (should (looking-back "\\[^1\\]: " nil))
1167 (let (kill-ring)
1168 (markdown-footnote-kill))
1169 (should (string-equal (buffer-string) "no text\n"))))
1171 (ert-deftest test-markdown-footnote/kill-empty-after ()
1172 "Test killing an empty footnote after one with text (previously killed the
1173 footnote with text above)."
1174 (markdown-test-string "[^with-text][^no-text]\n\n[^with-text]: Text\n[^no-text]:"
1175 (let (kill-ring)
1176 (forward-line 3)
1177 (should (looking-at "\\[\\^no-text\\]:$"))
1178 (markdown-footnote-kill)
1179 (should (string-equal (current-kill 0) "")))))
1181 (ert-deftest test-markdown-footnote/kill-hanging-paras ()
1182 "Test killing a footnote where block text starts after the label (previously
1183 killed the footnote above)."
1184 (markdown-test-string "[^1][^2]\n\n[^1]: Foo\n\n[^2]:\n Text\n\n More text\n\n\nNot indented"
1185 (let (kill-ring)
1186 (forward-line 4)
1187 (should (looking-at "\\[\\^2\\]:$"))
1188 (markdown-footnote-kill)
1189 ;; We want to include the leading space on hanging footnote paragraphs,
1190 ;; even if a hanging paragraph is the first item in the footnote.
1191 (should (string-equal (current-kill 0) "Text\n\n More text\n")))))
1193 (ert-deftest test-markdown-footnote/text-positions-buffer-top ()
1194 "Test markdown-footnote-text-positions on footnote adjacent to buffer top
1195 (was infinite loop)."
1196 (markdown-test-string "[^label]: text\n more text"
1197 (should (equal (markdown-footnote-text-positions) (list "^label" 1 29)))))
1199 (ert-deftest test-markdown-footnote/text-positions-buffer-top-one-line ()
1200 "Test markdown-footnote-text-positions on one-line footnote adjacent to
1201 buffer top (failed to find positions)."
1202 (markdown-test-string "[^label]: text\n"
1203 (should (equal (markdown-footnote-text-positions) (list "^label" 1 16)))))
1205 (ert-deftest test-markdown-footnote/text-positions-buffer-top-not-footnote ()
1206 "Test markdown-footnote-text-positions on plain paragraph adjacent to buffer
1207 top (was infinite loop)."
1208 (markdown-test-string "text\n more text\n"
1209 (should (eq (markdown-footnote-text-positions) nil))))
1211 (ert-deftest test-markdown-footnote/text-positions-buffer-bottom ()
1212 "Test markdown-footnote-text-positions on footnote adjacent to buffer bottom
1213 (was infinite loop)."
1214 (markdown-test-string "\n[^label]: text\n more text"
1215 (forward-line 1)
1216 (should (equal (markdown-footnote-text-positions) (list "^label" 2 30)))))
1218 (ert-deftest test-markdown-footnote/kill-adjacent-footnote ()
1219 "Test killing a footnote adjacent to other one-line footnotes (previously
1220 killed the wrong one)."
1221 (markdown-test-string "Text[^1] with[^2] footnotes[^3]\n\n[^1]: foo\n[^2]: bar\n[^3]: baz"
1222 (let (kill-ring)
1223 (forward-line 3)
1224 (should (looking-at "\\[\\^2\\]: bar"))
1225 (markdown-footnote-kill)
1226 (should (string-equal (current-kill 0) "bar\n")))))
1228 (ert-deftest test-markdown-footnote/kill-adjacent-markers ()
1229 "Test killing a footnote where the labels are adjacent (previously, the wrong
1230 footnote would be killed because the attempt to jump to the marker would jump to
1231 the opening bracket of [^2], and then subsequent functions would kill [^2])."
1232 (markdown-test-string "Text with footnotes[^1][^2]\n\n[^1]: foo\n\n[^2]: bar\n"
1233 (let (kill-ring)
1234 (forward-line 2)
1235 (should (looking-at "\\[\\^1\\]: foo"))
1236 (markdown-footnote-kill)
1237 (should (string-equal (current-kill 0) "foo\n")))))
1239 (ert-deftest test-markdown-footnote-reference/jump ()
1240 "Test `markdown-jump' for footnotes and reference links."
1241 (markdown-test-string "body[^1], [link 1][ref],
1242 [link 2][ref]
1244 [^1]: footnote
1246 [ref]: https://duckduckgo.com/"
1247 (goto-char 5) ; start of [^1]
1248 (markdown-jump) ; markdown-footnote-goto-text
1249 (should (looking-at "footnote"))
1250 (markdown-jump) ; markdown-footnote-return
1251 (should (= (point) 9)) ; just after [^1]
1252 (markdown-next-link) ; beginning of [link 1][]
1253 (markdown-jump)
1254 (should (looking-at "https://duckduckgo.com/"))
1255 (should (equal (markdown-reference-find-links "ref")
1256 (list (list "link 2" 26 2) (list "link 1" 11 1))))
1257 (markdown-jump) ; opens a reference link buffer
1258 (should (string= (buffer-string) "Links using reference ref:\n\nlink 1 (line 1)\nlink 2 (line 2)\n"))
1259 (should (looking-at "link 1")) ; in reference link popop buffer
1260 (execute-kbd-macro (read-kbd-macro "RET")) ; jump to "link 1"
1261 (should (looking-at "\\[link 1\\]")) ; back in main buffer
1262 (should (= (point) 11))))
1264 ;;; Element removal tests:
1266 (ert-deftest test-markdown-kill/simple ()
1267 "Simple tests for `markdown-kill-thing-at-point'."
1268 (let ((kill-ring nil)
1269 (tests (list '("`foo`" . "foo")
1270 '("## foo ##" . "foo")
1271 '("## foo" . "foo")
1272 '("foo\n---" . "foo")
1273 '("foo\n===" . "foo")
1274 '("* * * * *" . "* * * * *")
1275 '("[foo](http://bar.com/)" . "foo")
1276 '("![foo](http://bar.com/)" . "foo")
1277 '("[foo][bar]" . "foo")
1278 '("![foo][bar]" . "foo")
1279 '("<http://foo.com/>" . "http://foo.com/")
1280 '("<foo@bar.com>" . "foo@bar.com")
1281 '("[[foo]]" . "foo")
1282 '("[[foo|bar]]" . "foo")
1283 '("**foo**" . "foo")
1284 '("__foo__" . "foo")
1285 '("*foo*" . "foo")
1286 '("_foo_" . "foo")
1287 '(" [foo]: http://bar.com/" . "http://bar.com/")
1288 '(" [foo]: http://bar.com/ \"title\"" . "http://bar.com/")
1289 '("foo[^bar]\n\n[^bar]: baz" . "baz")
1290 '("[^bar]: baz" . "baz")
1291 '(" * foo\n bar" . " * foo\n bar"))))
1292 (dolist (test tests)
1293 ;; Load test string (the car), move to end of first line, kill
1294 ;; thing at point, and then verify that the kill ring contains cdr.
1295 (markdown-test-string (car test)
1296 (end-of-line)
1297 (call-interactively 'markdown-kill-thing-at-point)
1298 (should (string-equal (current-kill 0) (cdr test)))))))
1300 (ert-deftest test-markdown-kill/footnote-text ()
1301 "Test killing a footnote with point at footnote text."
1302 (markdown-test-string "some text[^1]\n\n[^1]: footnote\n"
1303 (end-of-line)
1304 (markdown-footnote-goto-text)
1305 (let (kill-ring)
1306 (markdown-footnote-kill))
1307 (should (string-equal (buffer-string) "some text\n"))))
1309 (ert-deftest test-markdown-kill/code ()
1310 "Test killing with code regex.."
1311 (let ((kill-ring nil))
1312 (markdown-test-string "Lorem `ipsum` dolor `sit` `amet`."
1313 (goto-char 22) ; position point at s in `sit`
1314 (call-interactively 'markdown-kill-thing-at-point)
1315 (should (string-equal (current-kill 0) "sit")))))
1317 ;;; Completion:
1319 (ert-deftest test-markdown-complete/atx-header-incomplete ()
1320 "Test `markdown-incomplete-atx-p'."
1321 (markdown-test-string "### ###"
1322 (should (looking-at markdown-regex-header-atx))
1323 (should-not (markdown-incomplete-atx-p)))
1324 (markdown-test-string "###abc###"
1325 (should (looking-at markdown-regex-header-atx))
1326 (should (markdown-incomplete-atx-p)))
1327 (markdown-test-string "### ###"
1328 (should (looking-at markdown-regex-header-atx))
1329 (should (markdown-incomplete-atx-p))))
1331 (ert-deftest test-markdown-complete/atx-header ()
1332 "Test `markdown-complete' for atx headers."
1333 (markdown-test-string "##### test"
1334 (call-interactively 'markdown-complete)
1335 (should (string-equal (buffer-string) "##### test #####"))))
1337 (ert-deftest test-markdown-complete/setext-header-incomplete ()
1338 "Test `markdown-incomplete-setext-p'."
1339 (markdown-test-string "abc\n===\n"
1340 (should (looking-at markdown-regex-header-setext))
1341 (should-not (markdown-incomplete-setext-p)))
1342 (markdown-test-string "abc\n==\n"
1343 (should (looking-at markdown-regex-header-setext))
1344 (should (markdown-incomplete-setext-p)))
1345 (markdown-test-string "abc\n====\n"
1346 (should (looking-at markdown-regex-header-setext))
1347 (should (markdown-incomplete-setext-p))))
1349 (ert-deftest test-markdown-complete/setext-header ()
1350 "Test `markdown-complete' for setext headers."
1351 (markdown-test-string " test \n=="
1352 (call-interactively 'markdown-complete)
1353 (should (string-equal (buffer-string) "test\n===="))))
1355 (ert-deftest test-markdown-complete/hr-incomplete ()
1356 "Test `markdown-incomplete-hr-p'."
1357 (dolist (i (number-sequence 0 (1- (length markdown-hr-strings))))
1358 (markdown-test-string (nth i markdown-hr-strings)
1359 (should (looking-at markdown-regex-hr))
1360 (should-not (markdown-incomplete-hr-p))
1361 (should-error (call-interactively 'markdown-complete)))))
1363 (ert-deftest test-markdown-complete/hr ()
1364 "Test completion via `markdown-complete' for horizontal rules."
1365 (markdown-test-string "- - - - -"
1366 (call-interactively 'markdown-complete)
1367 (should (string-equal (buffer-string) (car markdown-hr-strings)))))
1369 (ert-deftest test-markdown-complete/buffer-setext-2 ()
1370 "Test `markdown-complete-buffer' for level two setext heading."
1371 ;; Ensure markdown-complete-buffer doesn't mistake this for a horizontal rule
1372 (markdown-test-string "Subheading\n--\n"
1373 (call-interactively 'markdown-complete-buffer)
1374 (should (string-equal (buffer-string) "Subheading\n----------\n\n")))
1375 (markdown-test-string "Abc\n--\n\nDef\n--\n"
1376 (call-interactively 'markdown-complete-buffer)
1377 (should (string-equal (buffer-string) "Abc\n---\n\nDef\n---\n\n"))))
1379 ;;; Promotion and demotion tests:
1381 (ert-deftest test-markdown-promote/atx-header ()
1382 "Test `markdown-promote' for atx headers."
1383 (markdown-test-string "###### test ######"
1384 (markdown-promote)
1385 (should (string-equal (buffer-string) "##### test #####"))
1386 (markdown-promote)
1387 (should (string-equal (buffer-string) "#### test ####"))
1388 (markdown-promote)
1389 (should (string-equal (buffer-string) "### test ###"))
1390 (markdown-promote)
1391 (should (string-equal (buffer-string) "## test ##"))
1392 (markdown-promote)
1393 (should (string-equal (buffer-string) "# test #"))))
1395 (ert-deftest test-markdown-demote/atx-header ()
1396 "Test `markdown-demote' for atx headers."
1397 (markdown-test-string "# test #"
1398 (markdown-demote)
1399 (should (string-equal (buffer-string) "## test ##"))
1400 (markdown-demote)
1401 (should (string-equal (buffer-string) "### test ###"))
1402 (markdown-demote)
1403 (should (string-equal (buffer-string) "#### test ####"))
1404 (markdown-demote)
1405 (should (string-equal (buffer-string) "##### test #####"))
1406 (markdown-demote)
1407 (should (string-equal (buffer-string) "###### test ######"))))
1409 (ert-deftest test-markdown-promote/setext-header ()
1410 "Test `markdown-promote' for setext headers."
1411 (markdown-test-string "test\n----"
1412 (markdown-promote)
1413 (should (string-equal (buffer-string) "test\n===="))))
1415 (ert-deftest test-markdown-demote/setext-header ()
1416 "Test `markdown-demote' for setext headers."
1417 (markdown-test-string "test\n===="
1418 (markdown-demote)
1419 (should (string-equal (buffer-string) "test\n----"))
1420 (markdown-demote)
1421 (should (string-equal (buffer-string) "### test ###"))
1422 (markdown-demote)
1423 (should (string-equal (buffer-string) "#### test ####"))
1424 (markdown-demote)
1425 (should (string-equal (buffer-string) "##### test #####"))
1426 (markdown-demote)
1427 (should (string-equal (buffer-string) "###### test ######"))))
1429 (ert-deftest test-markdown-promote/hr ()
1430 "Test `markdown-promote' for horizontal rules."
1431 (markdown-test-string (car (reverse markdown-hr-strings))
1432 (dolist (n (number-sequence 4 0 -1))
1433 (markdown-promote)
1434 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))
1436 (ert-deftest test-markdown-demote/hr ()
1437 "Test `markdown-demote' for horizontal rules."
1438 (markdown-test-string (car markdown-hr-strings)
1439 (dolist (n (number-sequence 1 5))
1440 (markdown-demote)
1441 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))
1443 (ert-deftest test-markdown-promote/bold ()
1444 "Test `markdown-promote' for bold markup."
1445 (markdown-test-string "__bold__"
1446 (call-interactively 'markdown-promote)
1447 (should (string-equal (buffer-string) "**bold**"))))
1449 (ert-deftest test-markdown-demote/bold ()
1450 "Test `markdown-demote' for bold markup."
1451 (markdown-test-string "**bold**"
1452 (call-interactively 'markdown-promote)
1453 (should (string-equal (buffer-string) "__bold__"))))
1455 (ert-deftest test-markdown-promote/italic ()
1456 "Test `markdown-promote' for italic markup."
1457 (markdown-test-string "_italic_"
1458 (call-interactively 'markdown-promote)
1459 (should (string-equal (buffer-string) "*italic*"))))
1461 (ert-deftest test-markdown-demote/italic ()
1462 "Test `markdown-demote' for italic markup."
1463 (markdown-test-string "*italic*"
1464 (call-interactively 'markdown-promote)
1465 (should (string-equal (buffer-string) "_italic_"))))
1467 ;;; Subtree editing tests:
1469 (ert-deftest test-markdown-subtree/promote ()
1470 "Test `markdown-promote-subtree'."
1471 (markdown-test-string "# h1 #\n\n## h2 ##\n\n### h3 ###\n\n## h2 ##\n\n# h1 #\n"
1472 ;; The first h1 should get promoted away.
1473 ;; The second h1 should not be promoted.
1474 (markdown-promote-subtree)
1475 (should (string-equal (buffer-string) "h1\n\n# h2 #\n\n## h3 ##\n\n# h2 #\n\n# h1 #\n"))
1476 ;; Second call should do nothing since point is no longer at a heading.
1477 (markdown-promote-subtree)
1478 (should (string-equal (buffer-string) "h1\n\n# h2 #\n\n## h3 ##\n\n# h2 #\n\n# h1 #\n"))
1479 ;; Move to h2 and promote again.
1480 (forward-line 2)
1481 (markdown-promote-subtree)
1482 (should (string-equal (buffer-string) "h1\n\nh2\n\n# h3 #\n\n# h2 #\n\n# h1 #\n"))))
1484 (ert-deftest test-markdown-subtree/demote ()
1485 "Test `markdown-demote-subtree'."
1486 (markdown-test-string "# h1 #\n\n## h2 ##\n\n### h3 ###\n\n## h2 ##\n\n# h1 #\n"
1487 ;; The second h1 should not be demoted
1488 (markdown-demote-subtree)
1489 (should (string-equal (buffer-string) "## h1 ##\n\n### h2 ###\n\n#### h3 ####\n\n### h2 ###\n\n# h1 #\n"))
1490 (markdown-demote-subtree)
1491 (should (string-equal (buffer-string) "### h1 ###\n\n#### h2 ####\n\n##### h3 #####\n\n#### h2 ####\n\n# h1 #\n"))
1492 (markdown-demote-subtree)
1493 (should (string-equal (buffer-string) "#### h1 ####\n\n##### h2 #####\n\n###### h3 ######\n\n##### h2 #####\n\n# h1 #\n"))
1494 ;; Stop demoting at level six
1495 (markdown-demote-subtree)
1496 (should (string-equal (buffer-string) "##### h1 #####\n\n###### h2 ######\n\n###### h3 ######\n\n###### h2 ######\n\n# h1 #\n"))
1497 (markdown-demote-subtree)
1498 (should (string-equal (buffer-string) "###### h1 ######\n\n###### h2 ######\n\n###### h3 ######\n\n###### h2 ######\n\n# h1 #\n"))))
1500 (ert-deftest test-markdown-subtree/move-up ()
1501 "Test `markdown-move-subtree-up'."
1502 ;; Note that prior to Emacs 24.5, this does not work for the last subtree in
1503 ;; the buffer due to Emacs bug #19102:
1504 ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19102
1505 ;; https://github.com/emacs-mirror/emacs/commit/b3910f
1506 ;; That also corrects the type of the "Cannot move pase superior level" error
1507 ;; from 'error to 'user-error.
1508 (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"
1509 (re-search-forward "^# 2")
1510 (markdown-move-subtree-up)
1511 (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"))
1512 ;; Second attempt should fail, leaving buffer unchanged.
1513 ;; (This way of asserting the contents of the error
1514 ;; message is a bit convoluted and more fragile than
1515 ;; ideal. But prior to Emacs 24.5, the type of this
1516 ;; error is just 'error, and a bare "should-error" is
1517 ;; really overly broad.)
1518 (should (string-equal
1519 "Cannot move past superior level"
1520 (second (should-error (markdown-move-subtree-up)))))))
1522 (ert-deftest test-markdown-subtree/move-down ()
1523 "Test `markdown-move-subtree-down'."
1524 (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"
1525 (re-search-forward "^## 1\.1")
1526 (markdown-move-subtree-down)
1527 (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"))))
1529 ;(ert-deftest test-markdown-subtree/move-down ()
1531 ;;; Cycling:
1533 (ert-deftest test-markdown-cycle/atx-header ()
1534 "Test `markdown-demote' cycling for atx headers."
1535 (markdown-test-string "##### test"
1536 (call-interactively 'markdown-demote)
1537 (should (string-equal (buffer-string) "###### test ######"))
1538 (call-interactively 'markdown-demote)
1539 (should (string-equal (buffer-string) "# test #"))
1540 (call-interactively 'markdown-demote)
1541 (should (string-equal (buffer-string) "## test ##"))))
1543 (ert-deftest test-markdown-cycle/setext-header ()
1544 "Test `markdown-demote' cycling for setext headers."
1545 (markdown-test-string "test\n===="
1546 (call-interactively 'markdown-demote)
1547 (should (string-equal (buffer-string) "test\n----"))
1548 (call-interactively 'markdown-demote)
1549 (should (string-equal (buffer-string) "### test ###"))
1550 (call-interactively 'markdown-demote)
1551 (should (string-equal (buffer-string) "#### test ####"))
1552 (call-interactively 'markdown-demote)
1553 (should (string-equal (buffer-string) "##### test #####"))
1554 (call-interactively 'markdown-demote)
1555 (should (string-equal (buffer-string) "###### test ######"))
1556 (call-interactively 'markdown-demote)
1557 (should (string-equal (buffer-string) "# test #"))))
1559 (ert-deftest test-markdown-cycle/hr ()
1560 "Test cycling of horizontal rules."
1561 ;; Cycle using markdown-demote
1562 (markdown-test-string (car markdown-hr-strings)
1563 (dolist (n (number-sequence 1 5))
1564 (call-interactively 'markdown-demote)
1565 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
1566 (call-interactively 'markdown-demote)
1567 (should (string-equal (buffer-string) (car markdown-hr-strings))))
1568 ;; Cycle using markdown-promote
1569 (markdown-test-string (car (reverse markdown-hr-strings))
1570 (dolist (n (number-sequence 4 0 -1))
1571 (call-interactively 'markdown-promote)
1572 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
1573 (call-interactively 'markdown-promote)
1574 (should (string-equal (buffer-string) (car (reverse markdown-hr-strings))))))
1576 (ert-deftest test-markdown-cycle/bold ()
1577 "Test cycling of bold markup."
1578 (markdown-test-string "**bold**"
1579 (call-interactively 'markdown-demote)
1580 (should (string-equal (buffer-string) "__bold__"))
1581 (call-interactively 'markdown-demote)
1582 (should (string-equal (buffer-string) "**bold**"))))
1584 (ert-deftest test-markdown-cycle/italic ()
1585 "Test cycling of italic markup."
1586 (markdown-test-string "*italic*"
1587 (call-interactively 'markdown-demote)
1588 (should (string-equal (buffer-string) "_italic_"))
1589 (call-interactively 'markdown-demote)
1590 (should (string-equal (buffer-string) "*italic*"))))
1592 ;;; Indentation tests:
1594 (ert-deftest test-markdown-indentation/calc-indents ()
1595 "Test `markdown-calc-indents' a nested list context."
1596 (markdown-test-file "nested-list.text"
1597 (goto-char (point-max))
1598 (let ((indents (markdown-calc-indents)))
1599 (should (= (car indents) 17)) ; indentation of previous line first
1600 (should (equal (sort indents '<)
1601 (list
1602 0 ; beginning of line
1603 3 ; first-level list marker
1604 7 ; second-level list marker
1605 11 ; third-level list marker
1606 13 ; previous list item text
1607 16 ; pre-block indentation
1608 17 ; indentation of previous line
1609 21 ; previous line plus tab-width
1610 ))))))
1612 (ert-deftest test-markdown-indentation/indent-region ()
1613 "Test `markdown-indent-region'."
1614 ;; Basic test with multiple lines
1615 (markdown-test-string "abc\ndef\nghi\n"
1616 (markdown-indent-region (point-min) (point-max) nil)
1617 (should (string-equal (buffer-string) " abc\n def\n ghi\n")))
1618 ;; Following a list item
1619 (markdown-test-string " * abc\ndef\n"
1620 (forward-line)
1621 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
1622 (should (string-equal (buffer-string) " * abc\n def\n"))
1623 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
1624 (should (string-equal (buffer-string) " * abc\n def\n"))))
1626 (ert-deftest test-markdown-indentation/indent-list-hanging ()
1627 "Test `markdown-indent-line' with hanging list item."
1628 (markdown-test-string
1629 "- list
1630 - nested list with long lines which need to be
1631 hard wrapped"
1632 (goto-char (point-max))
1633 (markdown-enter-key)
1634 (should (eq (point) 78))))
1636 (ert-deftest test-markdown-indentation/indent-list-single ()
1637 "Test `markdown-indent-line' with single list item."
1638 (markdown-test-string
1639 " * item 1"
1640 (end-of-line)
1641 (markdown-enter-key)
1642 (should (string-equal (buffer-string) " * item 1\n "))
1643 (should (eq (point) 14))))
1645 (ert-deftest test-markdown-indentation/indent-pre ()
1646 "Test `markdown-indent-line' with a pre block."
1647 (markdown-test-string
1648 "I'm gonna write a code block:
1650 my first line of code"
1651 (goto-char (point-max))
1652 (markdown-enter-key)
1653 (should (eq (point) 62))
1654 (should (looking-back "^ "))))
1656 ;;; Font lock tests:
1658 (ert-deftest test-markdown-font-lock/italics-1 ()
1659 "A simple italics test."
1660 (markdown-test-file "inline.text"
1661 (goto-char 9)
1662 (should (looking-at "\*"))
1663 ;; Check face of char before leading asterisk
1664 (markdown-test-range-has-face 8 8 nil)
1665 ;; Check face of italic range
1666 (markdown-test-range-has-face 9 9 markdown-markup-face)
1667 (markdown-test-range-has-face 10 16 markdown-italic-face)
1668 (markdown-test-range-has-face 17 17 markdown-markup-face)
1669 ;; Check face of point past leading asterisk
1670 (markdown-test-range-has-face 18 18 nil)))
1672 (ert-deftest test-markdown-font-lock/italics-2 ()
1673 "Test space after leading asterisk or underscore."
1674 (markdown-test-string
1675 "This is * not italic*, nor _ is this_."
1676 (markdown-test-range-has-face (point-min) (point-max) nil)))
1678 (ert-deftest test-markdown-font-lock/italics-3 ()
1679 "Test that slash inside asterisks is not italic."
1680 (markdown-test-string
1681 "not italic *\\*"
1682 (markdown-test-range-has-face (point-min) (point-max) nil)))
1684 (ert-deftest test-markdown-font-lock/italics-4 ()
1685 "Test that escaped asterisk inside italics is not bold."
1686 (markdown-test-string
1687 "italic **\\**"
1688 (markdown-test-range-has-face 1 7 nil)
1689 (markdown-test-range-has-face 8 8 markdown-markup-face)
1690 (markdown-test-range-has-face 9 11 markdown-italic-face)
1691 (markdown-test-range-has-face 12 12 markdown-markup-face)))
1693 (ert-deftest test-markdown-font-lock/italics-5 ()
1694 "Test italic single letter."
1695 (markdown-test-string
1696 "*a*"
1697 (markdown-test-range-has-face 1 1 markdown-markup-face)
1698 (markdown-test-range-has-face 2 2 markdown-italic-face)
1699 (markdown-test-range-has-face 3 3 markdown-markup-face)))
1701 (ert-deftest test-markdown-font-lock/italics-after-hr ()
1702 "Test italics after a horizontal rule with asterisks."
1703 (markdown-test-string "* * *\n\n*italic*\n"
1704 (markdown-test-range-has-face 1 5 markdown-header-delimiter-face)
1705 (markdown-test-range-has-face 8 8 markdown-markup-face)
1706 (markdown-test-range-has-face 9 14 markdown-italic-face)
1707 (markdown-test-range-has-face 15 15 markdown-markup-face)))
1709 (ert-deftest test-markdown-font-lock/italics-in-heading ()
1710 "Test italic overlay in a heading."
1711 (markdown-test-string
1712 "# *Italics* in a Heading"
1713 (markdown-test-range-has-face 3 3 markdown-markup-face)
1714 (markdown-test-range-has-face 4 10 markdown-italic-face)
1715 (markdown-test-range-has-face 11 11 markdown-markup-face)))
1717 (ert-deftest test-markdown-font-lock/italics-link ()
1718 "Test italic overlay in an inline link."
1719 (markdown-test-string
1720 "*[italic link](http://www.link.com/)*"
1721 (markdown-test-range-has-face 1 1 markdown-markup-face)
1722 (markdown-test-range-has-face 2 36 markdown-italic-face)
1723 (markdown-test-range-has-face 37 37 markdown-markup-face))
1724 (markdown-test-string
1725 "[*italic link*](http://www.link.com/)"
1726 (markdown-test-range-has-face 2 2 markdown-markup-face)
1727 (markdown-test-range-has-face 3 13 markdown-italic-face)
1728 (markdown-test-range-has-face 14 14 markdown-markup-face)))
1730 (ert-deftest test-markdown-font-lock/italics-in-blockquote ()
1731 "Test italics overlay in a blockquote."
1732 (markdown-test-string
1733 "> *italics* inside a blockquote"
1734 (markdown-test-range-has-face 3 3 markdown-markup-face)
1735 (markdown-test-range-has-face 4 10 markdown-italic-face)
1736 (markdown-test-range-has-face 11 11 markdown-markup-face)))
1738 (ert-deftest test-markdown-font-lock/italics-in-pre ()
1739 "Test italics overlay in a blockquote."
1740 (markdown-test-string
1741 " *italics* inside a pre block"
1742 (markdown-test-range-has-face (point-min) (1- (point-max))
1743 markdown-pre-face)))
1745 (ert-deftest test-markdown-font-lock/italics-and-code ()
1746 "Test seeming italics mixed with code."
1747 (markdown-test-string
1748 "define `var_1` and `var_2` inline code"
1749 (markdown-test-range-has-face 9 13 markdown-inline-code-face)
1750 (markdown-test-range-has-face 21 25 markdown-inline-code-face))
1751 (markdown-test-string
1752 "`var_1` and var_2"
1753 (markdown-test-range-has-face 2 6 markdown-inline-code-face)
1754 (markdown-test-range-has-face 8 17 nil))
1755 (markdown-test-string
1756 "var_1 and `var_2`"
1757 (markdown-test-range-has-face 1 10 nil)
1758 (markdown-test-range-has-face 12 16 markdown-inline-code-face)))
1760 (ert-deftest test-markdown-font-lock/bold-1 ()
1761 "A simple bold test."
1762 (markdown-test-file "inline.text"
1763 (goto-char 27)
1764 (should (looking-at "\*\*"))
1765 ;; Check face of char before leading asterisk
1766 (markdown-test-range-has-face 26 26 nil)
1767 ;; Check face of opening asterisks
1768 (markdown-test-range-has-face 27 28 markdown-markup-face)
1769 ;; Check face of bold range
1770 (markdown-test-range-has-face 29 33 markdown-bold-face)
1771 ;; Check face of closing asterisks
1772 (markdown-test-range-has-face 34 35 markdown-markup-face)
1773 ;; Check face of point past leading asterisk
1774 (markdown-test-range-has-face 36 36 nil)))
1776 (ert-deftest test-markdown-font-lock/bold-2 ()
1777 "Test space after leading asterisks or underscores."
1778 (markdown-test-string
1779 "This is ** not bold**, nor __ is this__ (but they match italics)."
1780 (markdown-test-range-has-face 1 8 nil)
1781 (markdown-test-range-has-face 9 9 markdown-markup-face)
1782 (markdown-test-range-has-face 10 19 markdown-italic-face)
1783 (markdown-test-range-has-face 20 20 markdown-markup-face)
1784 (markdown-test-range-has-face 21 27 nil)
1785 (markdown-test-range-has-face 28 28 markdown-markup-face)
1786 (markdown-test-range-has-face 29 37 markdown-italic-face)
1787 (markdown-test-range-has-face 38 38 markdown-markup-face)
1788 (markdown-test-range-has-face 39 (point-max) nil)))
1790 (ert-deftest test-markdown-font-lock/bold-3 ()
1791 "Test escaped asterisk inside bold."
1792 (markdown-test-string
1793 "bold **\\***"
1794 (markdown-test-range-has-face 1 5 nil)
1795 (markdown-test-range-has-face 6 7 markdown-markup-face)
1796 (markdown-test-range-has-face 8 9 markdown-bold-face)
1797 (markdown-test-range-has-face 10 11 markdown-markup-face)))
1799 (ert-deftest test-markdown-font-lock/bold-4 ()
1800 "Test bold single letter."
1801 (markdown-test-string
1802 "**a**"
1803 (markdown-test-range-has-face 1 2 markdown-markup-face)
1804 (markdown-test-range-has-face 3 3 markdown-bold-face)
1805 (markdown-test-range-has-face 4 5 markdown-markup-face)))
1807 (ert-deftest test-markdown-font-lock/bold-after-hr ()
1808 "Test bold after a horizontal rule with asterisks."
1809 (markdown-test-string "* * *\n\n**bold**\n"
1810 (markdown-test-range-has-face 1 5 markdown-header-delimiter-face)
1811 (markdown-test-range-has-face 8 9 markdown-markup-face)
1812 (markdown-test-range-has-face 10 13 markdown-bold-face)
1813 (markdown-test-range-has-face 14 15 markdown-markup-face)))
1815 (ert-deftest test-markdown-font-lock/bold-link ()
1816 "Test bold overlay in an inline link."
1817 (markdown-test-string
1818 "**[bold link](http://www.link.com/)**"
1819 (markdown-test-range-has-face 1 2 markdown-markup-face)
1820 (markdown-test-range-has-face 3 35 markdown-bold-face)
1821 (markdown-test-range-has-face 36 37 markdown-markup-face))
1822 (markdown-test-string
1823 "[**bold link**](http://www.link.com/)"
1824 (markdown-test-range-has-face 2 3 markdown-markup-face)
1825 (markdown-test-range-has-face 4 12 markdown-bold-face)
1826 (markdown-test-range-has-face 13 14 markdown-markup-face)))
1828 (ert-deftest test-markdown-font-lock/bold-in-blockquote ()
1829 "Test bold overlay in a blockquote."
1830 (markdown-test-string
1831 "> **bold** inside a blockquote"
1832 (markdown-test-range-has-face 3 4 markdown-markup-face)
1833 (markdown-test-range-has-face 5 8 markdown-bold-face)
1834 (markdown-test-range-has-face 9 10 markdown-markup-face)))
1836 (ert-deftest test-markdown-font-lock/bold-in-pre ()
1837 "Test bold overlay in a blockquote."
1838 (markdown-test-string
1839 " **bold** inside a pre block"
1840 (markdown-test-range-has-face (point-min) (1- (point-max))
1841 markdown-pre-face)))
1843 (ert-deftest test-markdown-font-lock/code-1 ()
1844 "A simple inline code test."
1845 (markdown-test-file "inline.text"
1846 (goto-char 45)
1847 (should (looking-at "`"))
1848 ;; Regular code span
1849 (markdown-test-range-has-face 45 45 markdown-markup-face)
1850 (markdown-test-range-has-face 46 49 markdown-inline-code-face)
1851 (markdown-test-range-has-face 50 50 markdown-markup-face)
1852 ;; Code containing backticks
1853 (markdown-test-range-has-face 61 62 markdown-markup-face)
1854 (markdown-test-range-has-face 63 87 markdown-inline-code-face)
1855 (markdown-test-range-has-face 88 89 markdown-markup-face)
1856 ;; Seven backquotes in a row
1857 (markdown-test-range-has-face 119 125 nil)
1858 ;; Backquotes at beginning or end
1859 (markdown-test-range-has-face 228 229 markdown-markup-face)
1860 (markdown-test-range-has-face 230 237 markdown-inline-code-face)
1861 (markdown-test-range-has-face 238 239 markdown-markup-face)
1862 (markdown-test-range-has-face 341 342 markdown-markup-face)
1863 (markdown-test-range-has-face 343 349 markdown-inline-code-face)
1864 (markdown-test-range-has-face 350 351 markdown-markup-face)
1865 ;; Backslash as final character
1866 (markdown-test-range-has-face 460 460 markdown-markup-face)
1867 (markdown-test-range-has-face 461 467 markdown-inline-code-face)
1868 (markdown-test-range-has-face 468 468 markdown-markup-face)
1869 ;; Escaping of leading backquotes
1870 (markdown-test-range-has-face 586 592 nil)
1871 (markdown-test-range-has-face 597 603 nil)
1872 ;; A code span crossing lines
1873 (markdown-test-range-has-face 652 656 nil)
1874 (markdown-test-range-has-face 657 657 markdown-markup-face)
1875 (markdown-test-range-has-face 658 665 markdown-inline-code-face)
1876 (markdown-test-range-has-face 666 666 markdown-markup-face)
1877 ;; Three backquotes: same line, across lines, not across blocks
1878 (markdown-test-range-has-face 695 748 nil)
1879 (markdown-test-range-has-face 749 750 markdown-markup-face)
1880 (markdown-test-range-has-face 751 755 markdown-inline-code-face)
1881 (markdown-test-range-has-face 756 757 markdown-markup-face)
1882 (markdown-test-range-has-face 758 805 nil)
1883 (markdown-test-range-has-face 806 807 markdown-markup-face)
1884 (markdown-test-range-has-face 808 812 markdown-inline-code-face)
1885 (markdown-test-range-has-face 813 814 markdown-markup-face)
1886 (markdown-test-range-has-face 815 891 nil)
1889 (ert-deftest test-markdown-font-lock/code-2 ()
1890 "Multiple code spans in a row and on different lines."
1891 (markdown-test-string "`foo` `bar` `baz`"
1892 (markdown-test-range-has-face 1 1 markdown-markup-face)
1893 (markdown-test-range-has-face 2 4 markdown-inline-code-face)
1894 (markdown-test-range-has-face 5 5 markdown-markup-face)
1895 (markdown-test-range-has-face 6 6 nil)
1896 (markdown-test-range-has-face 7 7 markdown-markup-face)
1897 (markdown-test-range-has-face 8 10 markdown-inline-code-face)
1898 (markdown-test-range-has-face 11 11 markdown-markup-face)
1899 (markdown-test-range-has-face 12 12 nil)
1900 (markdown-test-range-has-face 13 13 markdown-markup-face)
1901 (markdown-test-range-has-face 14 16 markdown-inline-code-face)
1902 (markdown-test-range-has-face 17 17 markdown-markup-face))
1903 (markdown-test-string "`a`\n`b`\n`c`\n"
1904 (markdown-test-range-has-face 1 1 markdown-markup-face)
1905 (markdown-test-range-has-face 2 2 markdown-inline-code-face)
1906 (markdown-test-range-has-face 3 3 markdown-markup-face)
1907 (markdown-test-range-has-face 4 4 nil)
1908 (markdown-test-range-has-face 5 5 markdown-markup-face)
1909 (markdown-test-range-has-face 6 6 markdown-inline-code-face)
1910 (markdown-test-range-has-face 7 7 markdown-markup-face)
1911 (markdown-test-range-has-face 8 8 nil)
1912 (markdown-test-range-has-face 9 9 markdown-markup-face)
1913 (markdown-test-range-has-face 10 10 markdown-inline-code-face)
1914 (markdown-test-range-has-face 11 11 markdown-markup-face)
1915 (markdown-test-range-has-face 12 12 nil))
1916 (markdown-test-string "a`foo`b`bar`c`baz`d"
1917 (markdown-test-range-has-face 1 1 nil)
1918 (markdown-test-range-has-face 2 2 markdown-markup-face)
1919 (markdown-test-range-has-face 3 5 markdown-inline-code-face)
1920 (markdown-test-range-has-face 6 6 markdown-markup-face)
1921 (markdown-test-range-has-face 7 7 nil)
1922 (markdown-test-range-has-face 8 8 markdown-markup-face)
1923 (markdown-test-range-has-face 9 11 markdown-inline-code-face)
1924 (markdown-test-range-has-face 12 12 markdown-markup-face)
1925 (markdown-test-range-has-face 13 13 nil)
1926 (markdown-test-range-has-face 14 14 markdown-markup-face)
1927 (markdown-test-range-has-face 15 17 markdown-inline-code-face)
1928 (markdown-test-range-has-face 18 18 markdown-markup-face)
1929 (markdown-test-range-has-face 19 19 nil)))
1931 (ert-deftest test-markdown-font-lock/kbd ()
1932 "Test font lock for <kbd> tags."
1933 (markdown-test-string "<kbd>C-c <</kbd>"
1934 (markdown-test-range-has-face 1 5 markdown-markup-face)
1935 (markdown-test-range-has-face 6 10 markdown-inline-code-face)
1936 (markdown-test-range-has-face 11 16 markdown-markup-face))
1937 (markdown-test-string "To quit Emacs, press <kbd>C-x C-c</kbd>."
1938 (markdown-test-range-has-face 1 21 nil)
1939 (markdown-test-range-has-face 22 26 markdown-markup-face)
1940 (markdown-test-range-has-face 27 33 markdown-inline-code-face)
1941 (markdown-test-range-has-face 34 39 markdown-markup-face)
1942 (markdown-test-range-has-face 40 40 nil)))
1944 (ert-deftest test-markdown-font-lock/lists-1 ()
1945 "A simple list marker font lock test."
1946 (markdown-test-file "lists.text"
1947 (dolist (loc (list 1063 1283 1659 1830 1919 2150 2393 2484
1948 2762 2853 3097 3188 3700 3903 4009))
1949 (goto-char loc)
1950 (should (looking-at "[*+-]"))
1951 (markdown-test-range-has-face loc loc markdown-list-face))))
1953 (ert-deftest test-markdown-font-lock/pre-1 ()
1954 "Nested list and pre block font lock test."
1955 (markdown-test-file "nested-list.text"
1956 (dolist (loc (list 4 29 194 224 491 525))
1957 (markdown-test-range-has-face loc loc markdown-list-face))
1958 (markdown-test-range-has-face 6 25 nil)
1959 (markdown-test-range-has-face 31 83 nil)
1960 (markdown-test-range-has-face 85 154 markdown-pre-face)
1961 (markdown-test-range-has-face 157 189 nil)
1962 (markdown-test-range-has-face 196 215 nil)
1963 (markdown-test-range-has-face 226 403 nil)
1964 (markdown-test-range-has-face 405 481 markdown-pre-face)
1965 (markdown-test-range-has-face 493 512 nil)
1966 (markdown-test-range-has-face 527 546 nil)
1967 (markdown-test-range-has-face 548 580 markdown-pre-face)))
1969 (ert-deftest test-markdown-font-lock/pre-2 ()
1970 (markdown-test-string "* item\n\nreset baseline\n\n pre block\n"
1971 (markdown-test-range-has-face 1 1 markdown-list-face)
1972 (markdown-test-range-has-face 2 23 nil)
1973 (markdown-test-range-has-face 29 37 markdown-pre-face)))
1975 (ert-deftest test-markdown-font-lock/pre-3 ()
1976 (markdown-test-string "It is interesting to see what happens when one queries
1977 `social upheaval` and `protopalatial era`.
1979 * `social upheaval`: the follwing queries have been tried:
1981 social upheaval subClassOf"
1982 (markdown-test-range-has-face 160 190 nil)))
1984 (ert-deftest test-markdown-font-lock/pre-4 ()
1985 "Pre blocks must be preceded by a blank line"
1986 (markdown-test-string "Paragraph
1987 for (var i = 0; i < 10; i++) {
1988 console.log(i);
1990 (markdown-test-range-has-face (point-min) (point-max) nil)))
1992 (ert-deftest test-markdown-font-lock/fenced-1 ()
1993 "Test fenced code blocks containing four-space indents."
1994 (markdown-test-string "Fenced code block
1997 if (x)
1998 foo();
2000 if (y)
2001 bar();
2004 (markdown-test-range-has-face 1 19 nil)
2005 (markdown-test-range-has-face 20 63 markdown-pre-face)))
2007 (ert-deftest test-markdown-font-lock/gfm-fenced-1 ()
2008 "Test GFM-style fenced code blocks (1)."
2009 (markdown-test-string "```ruby
2010 require 'redcarpet'
2011 markdown = Redcarpet.new('Hello World!')
2012 puts markdown.to_html
2013 ```"
2014 (markdown-test-range-has-face 1 3 markdown-markup-face) ; ```
2015 (markdown-test-range-has-face 4 7 markdown-language-keyword-face) ; ruby
2016 (markdown-test-range-has-face 9 90 markdown-pre-face) ; code
2017 (markdown-test-range-has-face 92 94 markdown-markup-face))) ; ```
2019 (ert-deftest test-markdown-font-lock/gfm-fenced-2 ()
2020 "Test GFM-style fenced code blocks (2)."
2021 (markdown-test-string "```{r sum}\n2+2\n```"
2022 (markdown-test-range-has-face 1 3 markdown-markup-face) ; ```
2023 (markdown-test-range-has-face 4 10 markdown-language-keyword-face) ; {r sum}
2024 (markdown-test-range-has-face 12 14 markdown-pre-face) ; 2+2
2025 (markdown-test-range-has-face 16 18 markdown-markup-face))) ; ```
2027 (ert-deftest test-markdown-font-lock/gfm-fenced-3 ()
2028 "GFM-style code blocks need not be preceded by a blank line."
2029 (markdown-test-string "Paragraph
2030 ```js
2031 for (var i = 0; i < 10; i++) {
2032 console.log(i);
2034 ```"
2035 (markdown-test-range-has-face 1 10 nil) ; Paragraph
2036 (markdown-test-range-has-face 11 13 markdown-markup-face) ; ```
2037 (markdown-test-range-has-face 14 15 markdown-language-keyword-face) ; js
2038 (markdown-test-range-has-face 17 68 markdown-pre-face)
2039 (markdown-test-range-has-face 70 72 markdown-markup-face)))
2041 (ert-deftest test-markdown-font-lock/atx-no-spaces ()
2042 "Test font-lock for atx headers with no spaces."
2043 (markdown-test-string "##abc##"
2044 (markdown-test-range-has-face 1 2 markdown-header-delimiter-face)
2045 (markdown-test-range-has-face 3 5 markdown-header-face-2)
2046 (markdown-test-range-has-face 6 7 markdown-header-delimiter-face))
2047 (markdown-test-string "##"
2048 (markdown-test-range-has-face 1 1 markdown-header-delimiter-face)
2049 (markdown-test-range-has-face 2 2 markdown-header-face-1))
2050 (markdown-test-string "###"
2051 (markdown-test-range-has-face 1 2 markdown-header-delimiter-face)
2052 (markdown-test-range-has-face 3 3 markdown-header-face-2)))
2054 (ert-deftest test-markdown-font-lock/setext-1-letter ()
2055 "An edge case for level-one setext headers."
2056 (markdown-test-string "a\n=\n"
2057 (markdown-test-range-has-face 1 1 markdown-header-face-1)
2058 (markdown-test-range-has-face 3 3 markdown-header-rule-face)))
2060 (ert-deftest test-markdown-font-lock/setext-2-letter ()
2061 "An edge case for level-two setext headers."
2062 (markdown-test-string "b\n-\n"
2063 (markdown-test-range-has-face 1 1 markdown-header-face-2)
2064 (markdown-test-range-has-face 3 3 markdown-header-rule-face)))
2066 (ert-deftest test-markdown-font-lock/inline-links ()
2067 "Test font lock for inline links."
2068 (markdown-test-file "inline.text"
2069 (markdown-test-range-has-face 925 925 markdown-markup-face)
2070 (markdown-test-range-has-face 926 929 markdown-link-face)
2071 (markdown-test-range-has-face 930 931 markdown-markup-face)
2072 (markdown-test-range-has-face 932 949 markdown-url-face)
2073 (markdown-test-range-has-face 951 957 markdown-link-title-face)
2074 (markdown-test-range-has-face 958 958 markdown-markup-face)))
2076 (ert-deftest test-markdown-font-lock/pre-comment ()
2077 "Test comments inside of a pre block."
2078 (markdown-test-string " <!-- pre, not comment -->"
2079 (markdown-test-range-has-face (point-min) (1- (point-max)) markdown-pre-face)))
2081 (ert-deftest test-markdown-font-lock/inline-code-comment ()
2082 "Test comments inside of a pre block."
2083 (markdown-test-string "`<h1> <!-- HTML comment inside inline code -->`"
2084 (markdown-test-range-has-face (1+ (point-min)) (- (point-max) 2) markdown-inline-code-face)))
2086 (ert-deftest test-markdown-font-lock/comment-hanging-indent ()
2087 "Test comments with hanging indentation."
2088 (markdown-test-string "<!-- This comment has\n hanging indentation -->"
2089 (markdown-test-range-has-face (point-min) (1- (point-max)) markdown-comment-face)))
2091 (ert-deftest test-markdown-font-lock/comment-multiple ()
2092 "Test multiple single-line comments in arow."
2093 (markdown-test-string "<!-- This is a comment -->\n<!-- And so is this -->"
2094 (markdown-test-range-has-face
2095 (point-at-bol) (1- (point-at-eol)) markdown-comment-face)
2096 (forward-line)
2097 (markdown-test-range-has-face
2098 (point-at-bol) (1- (point-at-eol)) markdown-comment-face)))
2100 (ert-deftest test-markdown-font-lock/footnote-markers-links ()
2101 "Test an edge case involving footnote markers and inline reference links."
2102 (markdown-test-string "Harvard[^1] [tuition][]"
2103 (markdown-test-range-has-face 1 7 nil)
2104 (markdown-test-range-has-face 8 8 markdown-markup-face)
2105 (markdown-test-range-has-face 10 10 markdown-footnote-face)
2106 (markdown-test-range-has-face 11 11 markdown-markup-face)
2107 (markdown-test-range-has-face 12 12 nil)
2108 (markdown-test-range-has-face 13 13 markdown-markup-face)
2109 (markdown-test-range-has-face 14 20 markdown-link-face)
2110 (markdown-test-range-has-face 21 21 markdown-markup-face)
2111 (markdown-test-range-has-face 22 23 markdown-markup-face)))
2113 (ert-deftest test-markdown-font-lock/mmd-metadata ()
2114 "Basic MultMarkdown metadata tests."
2115 (markdown-test-string "Title: peg-multimarkdown User's Guide
2116 Author: Fletcher T. Penney
2117 Base Header Level: 2 "
2118 (markdown-test-range-has-face 1 5 markdown-metadata-key-face)
2119 (markdown-test-range-has-face 6 6 markdown-markup-face)
2120 (markdown-test-range-has-face 8 37 markdown-metadata-value-face)
2121 (markdown-test-range-has-face 41 46 markdown-metadata-key-face)
2122 (markdown-test-range-has-face 47 47 markdown-markup-face)
2123 (markdown-test-range-has-face 49 66 markdown-metadata-value-face)
2124 (markdown-test-range-has-face 70 86 markdown-metadata-key-face)
2125 (markdown-test-range-has-face 87 87 markdown-markup-face)
2126 (markdown-test-range-has-face 89 89 markdown-metadata-value-face))
2127 ;; Avoid triggering when a title contains a colon (e.g., Markdown: Syntax)
2128 (markdown-test-file "syntax.text"
2129 (markdown-test-range-has-face 1 16 markdown-header-face-1)))
2131 (ert-deftest test-markdown-font-lock/mmd-metadata-after-header ()
2132 "Ensure that similar lines are not matched after the header."
2133 (markdown-test-string "Title: peg-multimarkdown User's Guide
2135 Author: Fletcher T. Penney
2136 Base Header Level: 2 "
2137 (markdown-test-range-has-face 1 5 markdown-metadata-key-face)
2138 (markdown-test-range-has-face 6 6 markdown-markup-face)
2139 (markdown-test-range-has-face 8 37 markdown-metadata-value-face)
2140 (markdown-test-range-has-face 40 67 nil)
2141 (markdown-test-range-has-face 71 90 nil)))
2143 (ert-deftest test-markdown-font-lock/pandoc-metadata ()
2144 "Basic Pandoc metadata tests."
2145 (markdown-test-string "% title
2146 two-line title
2147 % first author;
2148 second author
2149 % date
2151 body"
2152 (markdown-test-range-has-face 1 1 markdown-markup-face)
2153 (markdown-test-range-has-face 3 24 markdown-metadata-value-face)
2154 (markdown-test-range-has-face 26 26 markdown-markup-face)
2155 (markdown-test-range-has-face 28 56 markdown-metadata-value-face)
2156 (markdown-test-range-has-face 58 58 markdown-markup-face)
2157 (markdown-test-range-has-face 60 63 markdown-metadata-value-face)
2158 (markdown-test-range-has-face 64 69 nil)))
2160 (ert-deftest test-markdown-font-lock/line-break ()
2161 "Basic line break tests."
2162 (markdown-test-string " \nasdf \n"
2163 (markdown-test-range-has-face 1 9 nil)
2164 (markdown-test-range-has-face 10 11 markdown-line-break-face)))
2166 (ert-deftest test-markdown-font-lock/blockquote-bold ()
2167 "Test font lock for bold inside of a blockquote."
2168 (markdown-test-string
2169 "> **bold**"
2170 (markdown-test-range-has-face 2 10 markdown-blockquote-face)
2171 (markdown-test-range-has-face 5 8 markdown-bold-face)))
2173 (ert-deftest test-markdown-font-lock/blockquote-italic ()
2174 "Test font lock for italic inside of a blockquote."
2175 (markdown-test-string
2176 "> *italic*"
2177 (markdown-test-range-has-face 2 10 markdown-blockquote-face)
2178 (markdown-test-range-has-face 4 9 markdown-italic-face)))
2180 (ert-deftest test-markdown-font-lock/blockquote-link ()
2181 "Test font lock for links inside of a blockquote."
2182 :expected-result :failed
2183 (markdown-test-string
2184 "> [link](url)"
2185 (markdown-test-range-has-face 1 13 markdown-blockquote-face)
2186 (markdown-test-range-has-face 3 8 markdown-link-face)
2187 (markdown-test-range-has-face 9 13 markdown-url-face)))
2189 (ert-deftest test-markdown-font-lock/blockquote-comment ()
2190 "Test font lock for comments inside of a blockquote."
2191 :expected-result :failed
2192 (markdown-test-string
2193 "> <!-- comment -->"
2194 (markdown-test-range-has-face 1 18 markdown-blockquote-face)
2195 (markdown-test-range-has-face 3 18 markdown-comment-face)))
2197 (ert-deftest test-markdown-font-lock/pre-override ()
2198 "Test that font lock for pre blocks overrides everything else."
2199 (markdown-test-string
2200 " **bold**
2201 _italic_
2202 <!-- comment -->
2203 [link](url)
2204 * list"
2205 (markdown-test-range-has-face 1 73 markdown-pre-face)))
2207 (ert-deftest test-markdown-font-lock/gfm-code-block-font-lock ()
2208 "GFM code block font lock test. Now in base markdown-mode as well!"
2209 (markdown-test-file "gfm.text"
2210 (markdown-test-range-has-face 2639 2641 markdown-markup-face) ; ```
2211 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face) ; lang
2212 (markdown-test-range-has-face 2647 2728 markdown-pre-face) ; code
2213 (markdown-test-range-has-face 2730 2732 markdown-markup-face))) ; ```
2215 (ert-deftest test-markdown-font-lock/reference-definition ()
2216 "Reference definitions should not include ]."
2217 (markdown-test-string "[1]: http://daringfireball.net/ \"title\""
2218 (markdown-test-range-has-face 2 2 markdown-reference-face) ; 1
2219 (markdown-test-range-has-face 6 31 markdown-url-face) ; URL
2220 (markdown-test-range-has-face 34 38 markdown-link-title-face)) ; title
2221 (markdown-test-string "[foo][1] and [bar][2]: not a reference definition"
2222 (markdown-test-range-has-face 2 4 markdown-link-face) ; foo
2223 (markdown-test-range-has-face 7 7 markdown-reference-face) ; 1
2224 (markdown-test-range-has-face 9 13 nil) ; [ ]and[ ]
2225 (markdown-test-range-has-face 15 17 markdown-link-face) ; bar
2226 (markdown-test-range-has-face 20 20 markdown-reference-face) ; 2
2227 (markdown-test-range-has-face 22 49 nil))) ; [ ]and[ ]
2229 ;;; Markdown Parsing Functions:
2231 (ert-deftest test-markdown-parsing/reference-definition-basic ()
2232 "Test reference definition function."
2233 (markdown-test-file "syntax.text"
2234 ;; Test accuracy of returned text and bounds
2235 (should (equal (markdown-reference-definition "1")
2236 (list "http://docutils.sourceforge.net/mirror/setext.html" 1942 1992)))
2237 (should (equal (markdown-reference-definition "2")
2238 (list "http://www.aaronsw.com/2002/atx/" 2000 2032)))
2239 ;; Test that match data remains intact
2240 (should (string-equal (match-string 5) "http://www.aaronsw.com/2002/atx/"))
2241 ;; Test anchor-only relative URL
2242 (should (equal (markdown-reference-definition "bq")
2243 (list "#blockquote" 7536 7547)))
2244 ;; Example references that appear in pre blocks in the text
2245 (should (not (markdown-reference-definition "")))
2246 (should (not (markdown-reference-definition "id")))
2247 (should (not (markdown-reference-definition "foo")))
2248 (should (not (markdown-reference-definition "A")))
2249 (should (not (markdown-reference-definition "Google")))
2250 ;; Test that we don't pick up other text in square brackets
2251 (should (not (markdown-reference-definition "blockquoting")))
2252 (should (not (markdown-reference-definition "square brackets")))
2253 ;; Test case insensitivity
2254 (should (equal (markdown-reference-definition "SRC")
2255 (list "/projects/markdown/syntax.text" 1245 1275)))))
2257 (ert-deftest test-markdown-parsing/get-defined-references ()
2258 "Test `markdown-get-defined-references'."
2259 (markdown-test-file "syntax.text"
2260 (should (equal (markdown-get-defined-references)
2261 '("src" "1" "2" "3" "4" "5" "6" "bq" "l"))))
2262 (markdown-test-file "outline.text"
2263 (should (equal (markdown-get-defined-references) nil)))
2264 (markdown-test-file "wiki-links.text"
2265 (should (equal (markdown-get-defined-references) nil))))
2267 (ert-deftest test-markdown-parsing/code-at-point-inline ()
2268 "Test `markdown-code-at-point-p'."
2269 (cl-flet ((test-region (beg end)
2270 (goto-char (1- beg))
2271 (should-not (markdown-code-at-point-p))
2272 (goto-char (1+ end))
2273 (should-not (markdown-code-at-point-p))
2274 (dolist (loc (number-sequence beg end))
2275 (goto-char loc)
2276 (should (markdown-code-at-point-p))
2277 (should (equal (match-beginning 0) beg))
2278 (should (equal (match-end 0) end)))))
2279 (markdown-test-file "inline.text"
2280 (test-region 45 51) ; Regular code span
2281 (test-region 61 90) ; Code containing backticks
2282 (test-region 228 240) ; Backquotes at beginning
2283 (test-region 341 352) ; Backquotes at end
2284 (test-region 460 469) ; Backslash as final character
2285 (test-region 657 667) ; A code span crossing lines
2286 (test-region 749 758) ; Three backquotes on same line
2287 (test-region 806 815) ; Three backquotes across lines
2290 (ert-deftest test-markdown-parsing/code-at-point-one-space ()
2291 "Test `markdown-code-at-point-p' with multiple code spans in a row."
2292 (markdown-test-string "`foo` `bar` `baz`"
2293 (dolist (loc (number-sequence 1 6))
2294 (goto-char loc)
2295 (should (markdown-code-at-point-p))
2296 (should (equal (match-data) (list 1 6 1 2 2 5 5 6))))
2297 (dolist (loc (number-sequence 7 12))
2298 (goto-char loc)
2299 (should (markdown-code-at-point-p))
2300 (should (equal (match-data) (list 7 12 7 8 8 11 11 12))))
2301 (dolist (loc (number-sequence 13 18))
2302 (goto-char loc)
2303 (should (markdown-code-at-point-p))
2304 (should (equal (match-data) (list 13 18 13 14 14 17 17 18))))))
2306 (ert-deftest test-markdown-parsing/code-at-point-no-space ()
2307 "Test `markdown-code-at-point-p' with multiple code spans in a row."
2308 (markdown-test-string "a`foo`b`bar`c`baz`d"
2309 (goto-char 1) ; "a"
2310 (should-not (markdown-code-at-point-p))
2311 (dolist (loc (number-sequence 2 7)) ; "`foo`b"
2312 (goto-char loc)
2313 (should (markdown-code-at-point-p))
2314 (should (equal (match-data) (list 2 7 2 3 3 6 6 7))))
2315 (dolist (loc (number-sequence 8 13)) ; "`bar`c"
2316 (goto-char loc)
2317 (should (markdown-code-at-point-p))
2318 (should (equal (match-data) (list 8 13 8 9 9 12 12 13))))
2319 (dolist (loc (number-sequence 14 19)) ; "`baz`d"
2320 (goto-char loc)
2321 (should (markdown-code-at-point-p))
2322 (should (equal (match-data) (list 14 19 14 15 15 18 18 19))))))
2324 (ert-deftest test-markdown-parsing/code-at-point-blank-line ()
2325 "Test `markdown-code-at-point-p' at beginning of block."
2326 (markdown-test-string "----------\n\n## foo\n"
2327 (should-not (markdown-code-at-point-p))
2328 (forward-line)
2329 (should-not (markdown-code-at-point-p))
2330 (forward-line)
2331 (should-not (markdown-code-at-point-p))))
2333 (ert-deftest test-markdown-parsing/match-comments ()
2334 "Test `markdown-match-comments'."
2335 (markdown-test-string
2336 "HTML <!-- foo --> comment"
2337 (should (markdown-match-comments (point-max)))
2338 (should (eq (point) 18))
2339 (should (equal (match-data) (list 6 18)))
2340 (should-not (markdown-match-comments (point-max)))))
2342 (ert-deftest test-markdown-parsing/range-property-any ()
2343 "Test behavior of `markdown-range-property-any'."
2344 (markdown-test-file
2345 "inline.text"
2346 (should (markdown-range-property-any
2347 (point-min) (point-at-eol)
2348 'face (list markdown-markup-face
2349 markdown-italic-face)))
2350 (should-not (markdown-range-property-any
2351 (point-min) (point-at-eol)
2352 'face (list markdown-bold-face)))))
2354 ;;; Reference Checking:
2356 (ert-deftest test-markdown-references/goto-line-button ()
2357 "Create and test a goto line button."
2358 (markdown-test-string "line 1\nline 2\n"
2359 ;; Store the temporary buffer with the text
2360 (let ((target (current-buffer)))
2361 ;; Create a new buffer for inserting
2362 (with-temp-buffer
2363 ;; Verify that point is in a different buffer
2364 (should (not (equal (current-buffer) target)))
2365 ;; Insert and press the button
2366 (insert-button "goto line 2"
2367 :type 'markdown-goto-line-button
2368 'target-buffer target
2369 'target-line 2)
2370 (should (string-equal (buffer-string) "goto line 2"))
2371 (backward-button 1)
2372 (call-interactively 'push-button)
2373 ;; Verify that point is on line 2 of target buffer
2374 (should (= (line-number-at-pos) 2))
2375 (should (looking-at "line 2"))
2376 (should (equal (current-buffer) target))))))
2378 (ert-deftest test-markdown-references/button-map ()
2379 "Verify that button-buffer-map is used for check references buffer."
2380 (markdown-test-string "[undefined][ref]\n"
2381 (let* ((target (buffer-name))
2382 (check (format "*Undefined references for %s*" target)))
2383 (markdown-check-refs)
2384 (with-current-buffer (get-buffer check)
2385 (should (equal (local-key-binding (kbd "TAB")) 'forward-button))
2386 (should (equal (local-key-binding (kbd "<backtab>")) 'backward-button))))))
2388 ;;; Lists:
2390 (ert-deftest test-markdown-lists/levels-1 ()
2391 "Test list levels function `markdown-calculate-list-levels'."
2392 (markdown-test-file "nested-list.text"
2393 (let ((values '(((1 . 1) . nil) ((2 . 13) . (3)) ((14 . 23) . (7 3))
2394 ((24 . 26) . (11 7 3)))))
2395 (loop for (range . value) in values
2396 do (goto-char (point-min))
2397 (forward-line (1- (car range)))
2398 (dotimes (n (- (cdr range) (car range)))
2399 (should (equal (markdown-calculate-list-levels) value))
2400 (forward-line))))))
2402 (ert-deftest test-markdown-lists/levels-2 ()
2403 "Test list levels function `markdown-calculate-list-levels'."
2404 (markdown-test-file "syntax.text"
2405 (let ((values '(((1 . 13) . nil) ((14 . 14) . (0)) ((15 . 17) . (4 0))
2406 ((18 . 18) . (0)) ((19 . 24) . (4 0)) ((25 . 25) . (0))
2407 ((26 . 29) . (4 0)) ((30 . 30) . (0)) ((31 . 33) . (4 0))
2408 ((34 . 588) . nil) ((589 . 595) . (0)) ((596 . 814) . nil)
2409 ((815 . 820) . (0)) ((821 . 898) . nil))))
2410 (loop for (range . value) in values
2411 do (goto-char (point-min))
2412 (forward-line (1- (car range)))
2413 (dotimes (n (- (cdr range) (car range)))
2414 (should (equal (markdown-calculate-list-levels) value))
2415 (forward-line))))))
2417 (ert-deftest test-markdown-lists/levels-interior ()
2418 "Test `markdown-calculate-list-levels' from inside a list item."
2419 (markdown-test-file "nested-list.text"
2420 (goto-char 36)
2421 (should (equal (markdown-calculate-list-levels) (list 3)))
2422 (goto-char 267)
2423 (should (equal (markdown-calculate-list-levels) (list 7 3)))
2424 (goto-char 540)
2425 (should (equal (markdown-calculate-list-levels) (list 11 7 3)))))
2427 (ert-deftest test-markdown-lists/bounds-1 ()
2428 "Test list item bounds function `markdown-cur-list-item-bounds'."
2429 (markdown-test-file "lists.text"
2430 (markdown-test-goto-heading "Case 9")
2431 (forward-line)
2432 (should (eq (point) 3699))
2433 (markdown-next-list-item 4)
2434 (should (eq (point) 3700))
2435 (should (equal (markdown-cur-list-item-bounds)
2436 (list 3700 3901 0 4 "- ")))
2437 (markdown-next-list-item 4)
2438 (should (eq (point) 3903))
2439 (should (equal (markdown-cur-list-item-bounds)
2440 (list 3903 3937 0 4 "* ")))))
2442 (ert-deftest test-markdown-lists/bounds-2 ()
2443 "Function `markdown-cur-list-item-bounds' should return nil outside of list items."
2444 (markdown-test-string "line one\n\n* item\n"
2445 (should (null (markdown-cur-list-item-bounds)))
2446 (forward-line)
2447 (should (null (markdown-cur-list-item-bounds)))
2448 (forward-line)
2449 (should (markdown-cur-list-item-bounds))))
2451 (ert-deftest test-markdown-lists/promotion-and-demotion ()
2452 "Test function `markdown-promote-list-item'."
2453 (markdown-test-file "nested-list.text"
2454 (forward-line)
2455 (should (looking-at " - List level 1 item 2
2457 Second paragraph of item 2
2459 Nested pre block in item 2
2460 Four spaces past the marker
2462 Another paragraph of item 2"))
2463 (markdown-demote-list-item)
2464 (should (looking-at " - List level 1 item 2
2466 Second paragraph of item 2
2468 Nested pre block in item 2
2469 Four spaces past the marker
2471 Another paragraph of item 2"))
2472 (markdown-promote-list-item)
2473 (should (looking-at " - List level 1 item 2
2475 Second paragraph of item 2
2477 Nested pre block in item 2
2478 Four spaces past the marker
2480 Another paragraph of item 2"))
2481 (goto-char (point-min))
2482 (forward-line 22)
2483 (should (looking-at " - List level 3 item 1
2485 Nested pre block"))
2486 (markdown-demote-list-item)
2487 (should (looking-at " - List level 3 item 1
2489 Nested pre block"))
2490 (markdown-promote-list-item)
2491 (should (looking-at " - List level 3 item 1
2493 Nested pre block"))))
2495 ;;; Outline minor mode tests:
2497 (ert-deftest test-markdown-outline/navigation ()
2498 "Test outline navigation functions."
2499 (markdown-test-file "outline.text"
2500 ;; Navigate to the first visible heading
2501 (markdown-next-visible-heading 1)
2502 (should (eq (point) 19))
2503 (should (looking-at "^# A top-level header"))
2504 ;; Navigate forward at the same level
2505 (markdown-forward-same-level 1)
2506 (should (eq (point) 377))
2507 (should (looking-at "^=+$"))
2508 ;; Navigate backward by four visible headings
2509 (markdown-previous-visible-heading 4)
2510 (should (eq (point) 69))
2511 (should (looking-at "^## A second-level header$"))))
2513 (ert-deftest test-markdown-outline/navigation-with-code ()
2514 "Test outline navigation functions with code blocks."
2515 (markdown-test-file "outline-code.text"
2516 ;; Navigate forward at the same level
2517 (markdown-forward-same-level 1)
2518 (should (eq (point) 159))
2519 (should (looking-at "^# Level one again"))))
2521 (ert-deftest test-markdown-outline/visibility-atx ()
2522 "Test outline visibility cycling for ATX-style headers."
2523 (markdown-test-file "outline.text"
2524 (let (last-command this-command)
2525 ;; Navigate to the second visible heading
2526 (markdown-next-visible-heading 2)
2527 (should (eq (point) 69))
2528 (should (looking-at "^## A second-level header$"))
2529 ;; Cycle visibility of this subtree
2530 (setq this-command 'markdown-cycle)
2531 (markdown-cycle)
2532 (setq last-command 'markdown-cycle)
2533 (should (eq (point) 69))
2534 (should (looking-at "^## A second-level header$"))
2535 ;; Test that the entire subtree is invisible
2536 (markdown-test-range-has-property 93 349 'invisible 'outline)
2537 ;; Cycle visibility of this subtree again
2538 (markdown-cycle)
2539 (should (eq (point) 69))
2540 (should (looking-at "^## A second-level header$"))
2541 ;; Test that text is visible
2542 (markdown-test-range-has-property 95 121 'invisible nil)
2543 ;; Test that subheadings are visible
2544 (markdown-test-range-has-property 123 141 'invisible nil)
2545 ;; Cycle visibility of this subtree again
2546 (markdown-cycle)
2547 (should (eq (point) 69))
2548 (should (looking-at "^## A second-level header$"))
2549 ;; Verify that entire subtree is visible
2550 (markdown-test-range-has-property 93 349 'invisible nil))))
2552 (ert-deftest test-markdown-outline/visibility-setext ()
2553 "Test outline visibility cycling for setext-style headers."
2554 (markdown-test-file "outline.text"
2555 ;; Navigate to the sixth visible heading
2556 (markdown-next-visible-heading 7)
2557 (markdown-previous-visible-heading 1)
2558 (should (looking-at markdown-regex-header))
2559 (should (string-equal (match-string-no-properties 1) "An underline-style header"))
2560 (should (string-equal (match-string-no-properties 2) "========================="))
2561 ;; Cycle visibility subtree, test that it's invisible
2562 (markdown-cycle)
2563 (markdown-test-range-has-property 404 515 'invisible 'outline)
2564 ;; Cycle visibility subtree, test that text and headers are visible
2565 (markdown-cycle)
2566 (markdown-test-range-has-property 404 417 'invisible nil)
2567 (markdown-test-range-has-property 420 451 'invisible nil)))
2569 (ert-deftest test-markdown-outline/visibility-with-code ()
2570 "Test outline visibility cycling with code blocks."
2571 (markdown-test-file "outline-code.text"
2572 (let (last-command this-command)
2573 ;; Cycle global visibility to "overview" mode
2574 (setq this-command 'markdown-cycle)
2575 (markdown-cycle t)
2576 (setq last-command 'markdown-cycle)
2577 (should (eq (point) (point-min)))
2578 (should (looking-at "^# Level one"))
2579 ;; Test that the code block is invisible
2580 (markdown-test-range-has-property 83 157 'invisible 'outline)
2581 ;; Check subsequent headings
2582 (outline-next-visible-heading 1)
2583 (should (eq (point) 69))
2584 (should (looking-at "^## Level two"))
2585 (outline-next-visible-heading 1)
2586 (should (eq (point) 159))
2587 (should (looking-at "^# Level one again")))))
2589 ;;; Movement tests:
2591 (ert-deftest test-markdown-movement/defun ()
2592 "Test defun navigation."
2593 (markdown-test-file "outline.text"
2594 ;; end-of-defun should go to point-max
2595 (end-of-defun 10)
2596 (should (= (point) (point-max)))
2597 ;; end-of-defun should stop just before the next header
2598 (goto-char (point-min))
2599 (end-of-defun)
2600 (should (looking-at "\n# A top-level header"))
2601 (end-of-defun)
2602 (should (looking-at "\n## A second-level header"))
2603 (end-of-defun)
2604 (should (looking-at "\n### Third level ###"))
2605 (end-of-defun)
2606 (should (looking-at "\n### Third level number two ###"))
2607 ;; beginning-of-defun should move to the start of the previous header
2608 (beginning-of-defun)
2609 (should (looking-at "### Third level ###"))
2610 (beginning-of-defun)
2611 (should (looking-at "## A second-level header"))
2612 (beginning-of-defun)
2613 (should (looking-at "# A top-level header"))
2614 (beginning-of-defun)
2615 ;; beginning-of-defun should move up to point-min
2616 (should (= (point) (point-min)))
2617 ;; (beginning-of-defun -1) should move to the start of the next header
2618 (forward-line 2)
2619 (beginning-of-defun -1)
2620 (should (looking-at "## A second-level header"))
2621 (beginning-of-defun -1)
2622 (should (looking-at "### Third level ###"))
2623 (beginning-of-defun -1)
2624 (should (looking-at "### Third level number two ###"))))
2626 (ert-deftest test-markdown-movement/block ()
2627 "Test block movement."
2628 (markdown-test-file "outline.text"
2629 (markdown-end-of-block)
2630 (should (looking-at "\n# A top-level header"))
2631 (markdown-end-of-block)
2632 (should (looking-at "\nfollowed by some body text"))
2633 (markdown-end-of-block)
2634 (should (looking-at "\n## A second-level header"))
2635 (markdown-end-of-block)
2636 (should (looking-at "\nfollowed by some body text"))
2637 (markdown-end-of-block)
2638 (should (looking-at "\n### Third level ###"))
2639 (markdown-end-of-block)
2640 (should (looking-at "\n\\* A list item"))
2641 (markdown-end-of-block)
2642 (should (looking-at "\n### Third level number two ###"))
2643 (markdown-end-of-block)
2644 (should (looking-at "\n### Level two again"))
2645 (markdown-end-of-block)
2646 (should (looking-at "\nfollowed by some body text"))
2648 (markdown-test-goto-heading "Level two")
2649 (markdown-end-of-block)
2650 (should (looking-at "\nbar"))
2651 (markdown-end-of-block)
2652 (should (= (point) (point-max)))
2653 (markdown-beginning-of-block)
2654 (should (looking-at "bar"))
2655 (markdown-beginning-of-block)
2656 (should (looking-at "## Level two"))
2657 (markdown-beginning-of-block)
2658 (should (looking-at "foo"))
2659 (markdown-beginning-of-block)
2660 (should (looking-at "# Level one"))
2661 (markdown-beginning-of-block)
2662 (should (looking-at "* With"))
2663 (markdown-beginning-of-block)
2664 (should (looking-at "And a level two underline header"))
2666 (goto-char (point-min))
2667 (markdown-test-goto-heading "A top-level header")
2668 (beginning-of-line)
2669 (markdown-beginning-of-block)
2670 (should (= (point) (point-min)))))
2672 (ert-deftest test-markdown-movement/reference-definition ()
2673 "Test jumping to reference definitions."
2674 ;; Jumping to explicit reference definition
2675 (markdown-test-string "[a][ref]\n\n[ref]: gopher://localhost/\n"
2676 (markdown-reference-goto-definition)
2677 (should (= (point) 18)))
2678 ;; Jumping to implicit reference definition
2679 (markdown-test-string "[a][]\n\n[a]: ftp://localhost/\n"
2680 (markdown-reference-goto-definition)
2681 (should (= (point) 13)))
2682 ;; Creating non-existent reference definition
2683 (markdown-test-string "[a][]\n"
2684 (markdown-reference-goto-definition)
2685 (should (= (point) 13))
2686 (should (string-equal (buffer-string) "[a][]\n\n[a]: "))))
2688 ;;; Wiki link tests:
2690 (ert-deftest test-markdown-wiki-link/aliasing ()
2691 "Test filename extraction for aliased wiki links."
2692 (markdown-test-file "wiki-links.text"
2693 ;; Confirm location of first wiki link
2694 (should (eq (markdown-next-link) 8))
2695 ;; Confirm location of second wiki link
2696 (should (eq (markdown-next-link) 73))
2697 ;; Test predicate function
2698 (should (markdown-wiki-link-p))
2699 ;; Test alias-first filename extraction
2700 (setq markdown-wiki-link-alias-first t)
2701 (should (string-equal (markdown-wiki-link-link) "second"))
2702 ;; Test alias-second filename extraction
2703 (setq markdown-wiki-link-alias-first nil)
2704 (should (string-equal (markdown-wiki-link-link) "first"))))
2706 (ert-deftest test-markdown-wiki-link/navigation ()
2707 "Test wiki link navigation."
2708 (markdown-test-file "wiki-links.text"
2709 ;; Advance to first link
2710 (should (eq (markdown-next-link) 8))
2711 ;; Advance to second link
2712 (should (eq (markdown-next-link) 73))
2713 ;; Avance to final link
2714 (should (eq (markdown-next-link) 155))
2715 ;; Return nil and don't advance point
2716 (should (eq (markdown-next-link) nil))
2717 (should (eq (point) 155))
2718 ;; Move back to second link
2719 (should (eq (markdown-previous-link) 73))
2720 ;; Move back to first link
2721 (should (eq (markdown-previous-link) 8))
2722 ;; Return nil and don't move point
2723 (should (eq (markdown-previous-link) nil))
2724 (should (eq (point) 8))))
2726 (ert-deftest test-markdown-wiki-link/font-lock ()
2727 "Test font lock faces for wiki links."
2728 (markdown-test-temp-file "wiki-links.text"
2729 (let* ((fn (concat (file-name-directory buffer-file-name)
2730 "inline.text")))
2731 ;; Create inline.text in the same temp directory, refontify
2732 (write-region "" nil fn nil 1)
2733 (markdown-fontify-buffer-wiki-links)
2734 ;; Confirm location of first wiki link
2735 (should (eq (markdown-next-link) 8))
2736 ;; First wiki link doesn't have a corresponding file
2737 (markdown-test-range-has-property 8 20 'font-lock-face markdown-missing-link-face)
2738 ;; Second wiki link doesn't have a corresponding file
2739 (should (eq (markdown-next-link) 73))
2740 (markdown-test-range-has-property 73 88 'font-lock-face markdown-missing-link-face)
2741 ;; Move to third wiki link, and create the missing file
2742 (should (eq (markdown-next-link) 155))
2743 (should (string-equal (markdown-wiki-link-link) "inline"))
2744 (markdown-test-range-has-property 155 164 'font-lock-face markdown-link-face)
2745 ;; Check wiki links in code blocks
2746 (markdown-test-range-has-face 360 395 markdown-pre-face)
2747 ;; Remove temporary files
2748 (delete-file fn)
2751 ;;; Filling tests:
2753 (ert-deftest test-markdown-filling/blockquote ()
2754 "Test filling of blockquotes.
2755 See `adaptive-fill-first-line-regexp'."
2756 (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
2757 (fill-paragraph)
2758 (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."))))
2760 (ert-deftest test-markdown-filling/space-after-list-marker ()
2761 "`fill-paragraph' should preserve more than one space after a list marker,
2762 since users may wish to indent their lists more than one space more than the
2763 width of the marker. The examples on the Markdown Syntax page have three
2764 spaces after the list marker for a total indentation of four."
2765 (let ((str "\n\n* List item indented four spaces.\n* Also four spaces."))
2766 (markdown-test-string str
2767 (forward-line 2)
2768 (fill-paragraph)
2769 (should (string-equal (buffer-string) str)))))
2771 (ert-deftest test-markdown-filling/multi-line-list-with-more-space ()
2772 "`fill-paragraph' should preserve more than one space after a list marker
2773 (see `test-preserve-space-after-list-marker')."
2774 (let ((str "* This list item is continued on\n the next line"))
2775 (markdown-test-string str
2776 ;; The first line is exactly 35 columns
2777 (let ((fill-column 35))
2778 (fill-paragraph)
2779 (should (string-equal (buffer-string) str))))))
2781 (ert-deftest test-markdown-filling/list-item-plus ()
2782 "Test filling of list items with plus sign markers.
2783 See `adaptive-fill-regexp'."
2784 (markdown-test-string " + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
2785 (fill-paragraph)
2786 (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."))))
2788 (ert-deftest test-markdown-filling/list-item-plus-in-blockquote ()
2789 "Test filling of list items with plus sign markers inside blockquote.
2790 See `adaptive-fill-regexp'."
2791 (markdown-test-string "> + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
2792 (fill-paragraph)
2793 (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."))))
2795 (ert-deftest test-markdown-filling/line-break ()
2796 "Test filling of paragraphs with hard line breaks.
2797 See `paragraph-separate'."
2798 (markdown-test-string "Lorem ipsum dolor sit amet, \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
2799 (let ((fill-column 70))
2800 (fill-paragraph)
2801 (should (string-equal (buffer-string) "Lorem ipsum dolor sit amet, \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut\nlabore et dolore magna aliqua.")))))
2803 (ert-deftest test-markdown-filling/decimal-number-at-beginning ()
2804 "Test filling when a number with a decimal appears at the beginning of a line."
2805 (markdown-test-string "The circumference of a circle divided by it's radius is around\n3.14."
2806 (fill-paragraph)
2807 (should (string-equal (buffer-string) "The circumference of a circle divided by it's radius is around 3.14."))))
2809 (ert-deftest test-markdown-filling/avoid-unintended-list-item ()
2810 "Avoid breaking lines where it would result in an unintended list item."
2811 (markdown-test-string "Lorem ipsum dolor sit 4. amet"
2812 (let ((fill-column 22))
2813 (fill-paragraph)
2814 (should (string-equal (buffer-string) "Lorem ipsum dolor\nsit 4. amet")))))
2816 (ert-deftest test-markdown-filling/no-break-link-reference ()
2817 "Shouldn't break line between label and url, or combine two link references."
2818 (let ((str "[label1]: http://long-url.example.com\n[label2]: http://another-long-url.example.com/"))
2819 (markdown-test-string str
2820 (let ((fill-column 15)) ; after end of label, before end of URL
2821 (fill-paragraph)
2822 (should (string-equal (buffer-string) str))))))
2824 (ert-deftest test-markdown-filling/no-break-before-list-item ()
2825 "There's no point in putting the first item of a list on the next line,
2826 indented the same amount."
2827 :expected-result :failed
2828 (let ((str "* [Link](http://way-too-long.example.com)\n"))
2829 (markdown-test-string str
2830 (auto-fill-mode 1)
2831 (let ((fill-column 10))
2832 (end-of-line)
2833 (funcall auto-fill-function)
2834 (should (string-equal (buffer-string) str))))))
2836 (ert-deftest test-markdown-filling/break-within-list-item ()
2837 "This doesn't suppress auto-fill within a multi-word list item."
2838 :expected-result :failed
2839 (markdown-test-string "* [Link](http://example.com/) more text"
2840 (auto-fill-mode 1)
2841 (let ((fill-column 10))
2842 (end-of-line)
2843 (funcall auto-fill-function)
2844 (should (string-equal
2845 (buffer-string)
2846 "* [Link](http://example.com/)\n more text")))))
2848 (ert-deftest test-markdown-filling/preserve-next-line-footnote ()
2849 "Footnote block can be after label"
2850 (let ((str "[^label1]:\n Footnote block\n more footnote")) ; six spaces
2851 (markdown-test-string str
2852 (let ((fill-column 20)) ; could fit "footnote" after label, but shouldn't
2853 (fill-paragraph)
2854 (should (string-equal (buffer-string) str))))))
2856 (ert-deftest test-markdown-filling/wrap-same-line-footnote ()
2857 "Additional lines must be indented one level (four spaces) when wrapped."
2858 (markdown-test-string "[^label]: Long line should be wrapped"
2859 (let ((fill-column 25)) ; wrap before end of "should"
2860 (fill-paragraph)
2861 (should (string-equal (buffer-string) "[^label]: Long line\n should be wrapped")))))
2863 (ert-deftest test-markdown-filling/wrap-extra-hanging-indentation ()
2864 "Additional lines must be indented one level (four spaces) when wrapped."
2865 (markdown-test-string "[^label]: Long line\n should be wrapped"
2866 (let ((fill-column 25)) ; wrap before end of "should"
2867 (fill-paragraph)
2868 (should (string-equal (buffer-string) "[^label]: Long line\n should be wrapped")))))
2870 (ert-deftest test-markdown-filling/full-justification ()
2871 "Test paragraph detection with lines with lots of whitespace."
2872 (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"
2873 (setq default-justification 'full)
2874 (fill-paragraph)
2875 (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"))
2876 (backward-paragraph)
2877 (forward-paragraph)
2878 (should (= (point) 198))))
2880 ;;; Export tests:
2882 (ert-deftest test-markdown-hook/xhtml-standalone ()
2883 "Test `markdown-xhtml-standalone-regexp' and `markdown-output-standalone-p'."
2884 (should (string-match markdown-xhtml-standalone-regexp
2885 "<?xml version='1.0' encoding='UTF-8'?>"))
2886 (should (string-match markdown-xhtml-standalone-regexp
2887 "<!DOCTYPE html>"))
2888 (should (string-match markdown-xhtml-standalone-regexp
2889 "<html>"))
2890 (should-not (string-match markdown-xhtml-standalone-regexp
2891 "<h1>title</h1>"))
2892 (should-not (string-match markdown-xhtml-standalone-regexp
2893 "<div id=\"name\">")))
2895 ;;; Hook tests:
2897 (ert-deftest test-markdown-hook/before-export ()
2898 "Test hook run before export XHTML."
2899 (markdown-test-temp-file "lists.text"
2900 (let* ((before-hook-run nil)
2901 (orig-point (point))
2902 (func (lambda ()
2903 ;; Change value of a variable
2904 (setq before-hook-run t)
2905 ;; Insert some text
2906 (goto-char (point-min))
2907 (insert "#")
2908 ;; Deliberately move the point
2909 (end-of-line)
2910 ;; Verify changes
2911 (should (looking-back "^## List Cases" nil))
2912 (should-not (= (point) orig-point))))
2913 (ofile (progn
2914 ;; Register hook
2915 (add-hook 'markdown-before-export-hook func)
2916 ;; Export XHTML and return filename
2917 (markdown-export)))
2918 (obuffer (get-file-buffer ofile)))
2919 ;; Test side effects of hook
2920 (should (eq before-hook-run t))
2921 ;; Test position of point
2922 (should (= (point) orig-point))
2923 ;; Test that buffer was restored to original state
2924 (goto-char (point-min))
2925 (should (looking-at "^# List Cases"))
2926 ;; Clean
2927 (remove-hook 'markdown-before-export-hook func)
2928 (kill-buffer obuffer)
2929 (delete-file ofile))))
2931 (ert-deftest test-markdown-hook/after-export ()
2932 "Test hook run after export XHTML."
2933 (markdown-test-temp-file "lists.text"
2934 (let* ((after-hook-run nil)
2935 (func (lambda ()
2936 ;; Change variable value
2937 (setq after-hook-run t)
2938 ;; Add comment to output buffer
2939 (goto-char (point-min))
2940 (insert "<!-- after-export-hook -->\n")))
2941 (ofile (progn
2942 ;; Register hook
2943 (add-hook 'markdown-after-export-hook func)
2944 ;; Export XHTML and return filename
2945 (markdown-export)))
2946 (obuffer (get-file-buffer ofile)))
2947 (message "obuffer = %S" obuffer)
2948 ;; Test that variable was changed
2949 (should (eq after-hook-run t))
2950 ;; Test that output buffer remains open
2951 (should (get-buffer obuffer))
2952 ;; Test that output buffer modification remains
2953 (with-current-buffer obuffer
2954 (goto-char (point-min))
2955 (should (looking-at "<!-- after-export-hook -->\n")))
2956 ;; Test that buffer modification was saved
2957 (should-not (buffer-modified-p obuffer))
2958 ;; Clean up
2959 (remove-hook 'markdown-after-export-hook func)
2960 (kill-buffer obuffer)
2961 (delete-file ofile))))
2963 ;;; Extension: math support
2965 (ert-deftest test-markdown-math/file-local-variable ()
2966 "Test enabling math mode via `hack-local-variables-hook'."
2967 (markdown-test-file "math.text"
2968 (should-not markdown-enable-math)
2969 (hack-local-variables)
2970 (should markdown-enable-math)))
2972 (ert-deftest test-markdown-math/reload ()
2973 "Test enabling math mode via function `markdown-enable-math'."
2974 (markdown-test-file "math.text"
2975 (markdown-enable-math t)
2976 ;; Flag should be set to t
2977 (should markdown-enable-math)
2978 ;; Font-lock keywords should be updated
2979 (should (member (cons markdown-regex-math-display '((1 markdown-markup-face prepend)
2980 (2 markdown-math-face append)
2981 (3 markdown-markup-face prepend)))
2982 markdown-mode-font-lock-keywords))))
2984 (ert-deftest test-markdown-math/font-lock ()
2985 "Test markdown math mode."
2986 (markdown-test-file "math.text"
2987 (markdown-enable-math t)
2988 (funcall markdown-test-font-lock-function)
2989 (markdown-test-range-has-face 1 32 nil)
2990 (markdown-test-range-has-face 33 33 markdown-markup-face)
2991 (markdown-test-range-has-face 34 45 markdown-math-face)
2992 (markdown-test-range-has-face 46 46 markdown-markup-face)
2993 (markdown-test-range-has-face 47 49 nil)
2994 (markdown-test-range-has-face 50 51 markdown-markup-face)
2995 (markdown-test-range-has-face 52 63 markdown-math-face)
2996 (markdown-test-range-has-face 64 65 markdown-markup-face)
2997 (markdown-test-range-has-face 66 98 nil)
2998 (markdown-test-range-has-face 99 100 markdown-markup-face)
2999 (markdown-test-range-has-face 101 112 markdown-math-face)
3000 (markdown-test-range-has-face 113 114 markdown-markup-face)
3001 (markdown-test-range-has-face 113 114 markdown-markup-face)
3002 (markdown-test-range-has-face 117 117 markdown-header-delimiter-face)
3003 (markdown-test-range-has-face 119 152 markdown-header-face-1)
3004 (markdown-test-range-has-face 129 129 markdown-markup-face)
3005 (markdown-test-range-has-face 136 136 markdown-markup-face)
3006 (markdown-test-range-has-face 174 214 markdown-pre-face)
3007 (markdown-test-range-has-face 218 218 markdown-markup-face)
3008 (markdown-test-range-has-face 219 223 markdown-math-face)
3009 (markdown-test-range-has-face 224 224 markdown-markup-face)))
3011 (ert-deftest test-markdown-math/font-lock-italics ()
3012 "Test markdown math mode with underscores."
3013 (markdown-test-file "math.text"
3014 (markdown-enable-math t)
3015 (funcall markdown-test-font-lock-function)
3016 (markdown-test-range-has-face 227 227 markdown-markup-face)
3017 (markdown-test-range-has-face 228 233 markdown-math-face)
3018 (markdown-test-range-has-face 234 234 markdown-markup-face)
3019 (markdown-test-range-has-face 235 270 nil)
3020 (markdown-test-range-has-face 271 271 markdown-markup-face)
3021 (markdown-test-range-has-face 272 274 markdown-math-face)
3022 (markdown-test-range-has-face 275 275 markdown-markup-face)))
3024 ;;; gfm-mode tests:
3026 (ert-deftest test-markdown-gfm/pre-1 ()
3027 "GFM pre block font lock test."
3028 (markdown-test-file-gfm "gfm.text"
3029 (markdown-test-range-has-face 2626 2637 nil)
3030 (markdown-test-range-has-face 2639 2641 markdown-markup-face)
3031 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face)
3032 (markdown-test-range-has-face 2647 2728 markdown-pre-face)
3033 (markdown-test-range-has-face 2730 2732 markdown-markup-face)))
3035 (ert-deftest test-markdown-gfm/italic-1 ()
3036 "GFM italic font lock test."
3037 (markdown-test-file-gfm "gfm.text"
3038 (markdown-test-range-has-face 1483 1483 markdown-markup-face)
3039 (markdown-test-range-has-face 1484 1487 markdown-italic-face)
3040 (markdown-test-range-has-face 1488 1488 markdown-markup-face)
3041 (markdown-test-range-has-face 1729 1790 nil)))
3043 (ert-deftest test-markdown-gfm/strike-through-1 ()
3044 "GFM strike through font lock test."
3045 (markdown-test-string-gfm "one ~~two~~ three"
3046 (markdown-test-range-has-face 1 4 nil)
3047 (markdown-test-range-has-face 5 6 markdown-markup-face)
3048 (markdown-test-range-has-face 7 9 markdown-strike-through-face)
3049 (markdown-test-range-has-face 10 11 markdown-markup-face)
3050 (markdown-test-range-has-face 12 17 nil)))
3052 (ert-deftest test-markdown-gfm/toggle-strike-through ()
3053 "Test toggling functionality of `markdown-insert-strike-through'."
3054 (markdown-test-string-gfm "one ~~two~~ three"
3055 (forward-word 2)
3056 (markdown-insert-strike-through)
3057 (should (string-equal (buffer-string) "one two three"))
3058 (should (= (point) 8))
3059 (forward-word)
3060 (markdown-insert-strike-through)
3061 (should (= (point) 16))
3062 (should (string-equal (buffer-string) "one two ~~three~~"))))
3064 (ert-deftest test-markdown-gfm/insert-code-block ()
3065 "GFM code block insertion test."
3066 ;; Test empty markup insertion
3067 (markdown-test-string-gfm "line 1\nline 2\n"
3068 (end-of-line)
3069 (markdown-insert-gfm-code-block "elisp")
3070 (should (string-equal (buffer-string)
3071 "line 1\n\n``` elisp\n\n```\n\nline 2\n")))
3072 ;; Test with active region
3073 (markdown-test-string-gfm "line 1\nline 2\nline 3\n"
3074 (forward-line)
3075 (transient-mark-mode)
3076 (push-mark (point) t t)
3077 (end-of-line)
3078 (should (markdown-use-region-p))
3079 (markdown-insert-gfm-code-block "elisp")
3080 (should (string-equal (buffer-string)
3081 "line 1\n\n``` elisp\nline 2\n```\n\nline 3\n"))))
3083 (ert-deftest test-markdown-gfm/code-block-font-lock ()
3084 "GFM code block font lock test."
3085 (markdown-test-file-gfm "gfm.text"
3086 (markdown-test-range-has-face 2639 2641 markdown-markup-face) ; ```
3087 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face) ; lang
3088 (markdown-test-range-has-face 2647 2728 markdown-pre-face) ; code
3089 (markdown-test-range-has-face 2730 2732 markdown-markup-face))) ; ```
3091 (ert-deftest test-markdown-gfm/code-block-font-lock-2 ()
3092 "GFM code block font lock test without language identifier."
3093 (markdown-test-string-gfm "Plain code block:\n\n```\nfoo\n```\n"
3094 (markdown-test-range-has-face 20 22 markdown-markup-face)
3095 (markdown-test-range-has-face 24 26 markdown-pre-face)
3096 (markdown-test-range-has-face 28 30 markdown-markup-face)))
3098 ;;; Tests for other extensions:
3100 (ert-deftest test-markdown-ext/pandoc-fancy-lists ()
3101 "Test basic support for font lock and filling of Pandoc 'fancy lists'."
3102 (markdown-test-string " #. abc\ndef\n"
3103 ;; font lock
3104 (markdown-test-range-has-face 1 1 nil)
3105 (markdown-test-range-has-face 2 3 markdown-list-face)
3106 (markdown-test-range-has-face 4 11 nil)
3107 ;; filling
3108 (forward-line)
3109 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
3110 (should (string-equal (buffer-string) " #. abc\n def\n"))
3111 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
3112 (should (string-equal (buffer-string) " #. abc\n def\n"))))
3114 (ert-deftest test-markdown-ext/ikiwiki ()
3115 (let ((markdown-wiki-link-search-parent-directories t))
3116 (progn
3117 (find-file "ikiwiki/root")
3118 (unwind-protect
3119 (progn
3120 (markdown-mode)
3121 ;; font lock
3122 (markdown-test-range-has-property 1 11 'font-lock-face markdown-link-face)
3123 (markdown-test-range-has-property 14 33 'font-lock-face markdown-missing-link-face))
3124 (kill-buffer)))
3125 (progn
3126 (find-file "ikiwiki/sub/foo")
3127 (unwind-protect
3128 (progn
3129 (markdown-mode)
3130 ;; font lock
3131 (markdown-test-range-has-property 1 16 'font-lock-face markdown-missing-link-face)
3132 (markdown-test-range-has-property 19 26 'font-lock-face markdown-link-face))
3133 (kill-buffer)))))
3135 (provide 'markdown-test)
3137 ;;; markdown-test.el ends here