Make number of spaces after code fence customizable
[markdown-mode.git] / tests / markdown-test.el
bloba7827fbe37c1cb24eb2fa152684048a8d15332a9
1 ;;;; markdown-test.el --- Tests for markdown-mode
3 ;; Copyright (C) 2013-2017 Jason R. Blevins <jblevins@xbeta.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 3 of the License, or
12 ;; (at your option) 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, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; This file contains the `markdown-mode' test suite. To run the tests:
26 ;; M-x load-file RET markdown-test.el RET
27 ;; M-x markdown-test RET
29 ;;; Code:
31 (require 'markdown-mode)
32 (require 'ert)
33 (require 'cl-lib)
35 (defconst markdown-test-dir
36 (expand-file-name (file-name-directory
37 (or load-file-name buffer-file-name))))
39 (defconst markdown-test-font-lock-function
40 (if (and noninteractive (fboundp 'font-lock-ensure))
41 #'font-lock-ensure #'font-lock-fontify-buffer))
43 (defmacro markdown-test-string-mode (mode string &rest body)
44 "Run BODY in a temporary buffer containing STRING in MODE."
45 (declare (indent 2))
46 `(let ((win (selected-window)))
47 (unwind-protect
48 (with-temp-buffer
49 (set-window-buffer win (current-buffer) t)
50 (erase-buffer)
51 (funcall ,mode)
52 (setq-default indent-tabs-mode nil)
53 (insert ,string)
54 (goto-char (point-min))
55 (funcall markdown-test-font-lock-function)
56 (prog1 ,@body (kill-buffer))))))
58 (defmacro markdown-test-file-mode (mode file &rest body)
59 "Open FILE from `markdown-test-dir' in MODE and execute BODY."
60 (declare (indent 2))
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 (declare (indent 1))
73 `(markdown-test-string-mode 'markdown-mode ,string ,@body))
74 (def-edebug-spec markdown-test-string (form body))
76 (defmacro markdown-test-file (file &rest body)
77 "Open FILE from `markdown-test-dir' in `markdown-mode' and execute BODY."
78 (declare (indent 1))
79 `(markdown-test-file-mode 'markdown-mode ,file ,@body))
80 (def-edebug-spec markdown-test-file (form body))
82 (defmacro markdown-test-string-gfm (string &rest body)
83 "Run BODY in a temporary buffer containing STRING in `gfm-mode'."
84 (declare (indent 1))
85 `(markdown-test-string-mode 'gfm-mode ,string ,@body))
86 (def-edebug-spec markdown-test-string-gfm (form body))
88 (defmacro markdown-test-file-gfm (file &rest body)
89 "Open FILE from `markdown-test-dir' in `gfm-mode' and execute BODY."
90 (declare (indent 1))
91 `(markdown-test-file-mode 'gfm-mode ,file ,@body))
92 (def-edebug-spec markdown-test-file-gfm (form body))
94 (defmacro markdown-test-temp-file (file &rest body)
95 "Open FILE from `markdown-test-dir' visiting temp file and execute BODY.
96 This file is not saved."
97 (declare (indent 1))
98 `(let ((fn (concat markdown-test-dir ,file))
99 (tmp (make-temp-file "markdown-test" nil ".text"))
100 buf)
101 (save-window-excursion
102 (unwind-protect
103 (progn
104 (setq buf (find-file tmp))
105 (insert-file-contents fn)
106 (markdown-mode)
107 (goto-char (point-min))
108 (funcall markdown-test-font-lock-function)
109 ,@body
110 (set-buffer-modified-p nil))
111 (when (buffer-live-p buf) (kill-buffer buf))
112 (delete-file tmp)))))
113 (def-edebug-spec markdown-test-temp-file (form body))
115 (defun markdown-test-report-property-range (begin end prop)
116 "Report buffer substring and property PROP from BEGIN to END."
117 (message "Buffer substring: %s" (buffer-substring begin (1+ end)))
118 (message "Properties in range are as follows:")
119 (dolist (loc (number-sequence begin end))
120 (message "%d: %s" loc (get-char-property loc prop))))
122 (defun markdown-test-range-has-property (begin end prop value)
123 "Verify that range BEGIN to END has PROP equal to or containing VALUE."
124 (let (vals fail-loc)
125 (setq fail-loc
126 (catch 'fail
127 (dolist (loc (number-sequence begin end))
128 (setq vals (get-char-property loc prop))
129 (if (and vals (listp vals))
130 (unless (memq value vals)
131 (throw 'fail loc))
132 (unless (eq vals value)
133 (throw 'fail loc))))))
134 (when fail-loc
135 (message "Testing range (%d,%d) for property %s equal to %s."
136 begin end prop value)
137 (message "Expected value (%s) not found in property (%s) at location %d" value prop fail-loc)
138 (markdown-test-report-property-range begin end prop))
139 (should-not fail-loc)))
141 (defun markdown-test-range-property-equals (begin end prop value)
142 "Verify that range BEGIN to END has property PROP equal to VALUE."
143 (let ((fail-loc
144 (catch 'fail
145 (dolist (loc (number-sequence begin end))
146 (unless (eq (get-char-property loc prop) value)
147 (throw 'fail loc))))))
148 (when fail-loc
149 (message "Testing range (%d,%d) for property %s equal to %s."
150 begin end prop value)
151 (message "Expected value (%s) not found in property (%s) at location %d" value prop fail-loc)
152 (markdown-test-report-property-range begin end prop))
153 (should-not fail-loc)))
155 (defun markdown-test-range-has-face (begin end face)
156 "Verify that the range from BEGIN to END has face FACE."
157 (markdown-test-range-has-property begin end 'face face))
159 (defun markdown-test-range-face-equals (begin end face)
160 "Verify that the range from BEGIN to END has face equal to FACE."
161 (markdown-test-range-property-equals begin end 'face face))
163 (defun markdown-test-goto-heading (title)
164 "Move the point to section with TITLE."
165 (let ((regexp (format "\\(^#+ %s\\( #+\\)?\\|^%s\n[=-]+\n\\)" title title)))
166 (if (re-search-forward regexp nil t)
167 (goto-char (match-end 0)))))
169 (defun markdown-test ()
170 "Run all defined test cases for `markdown-mode'."
171 (interactive)
172 (ert "markdown"))
174 ;;; Example tests:
176 (ert-deftest test-markdown-example/string ()
177 "An example string test using the `ert' framework."
178 (markdown-test-string "foo *bar* baz"
179 (goto-char 5)
180 (delete-char 1)
181 (should (looking-at "bar"))))
183 (ert-deftest test-markdown-example/file ()
184 "An example file test using the `ert' framework."
185 (markdown-test-file "inline.text"
186 (goto-char 9)
187 (should (looking-at "\*"))))
189 ;;; Basic mode tests:
191 (ert-deftest test-markdown-mode/variables ()
192 "Test `markdown-mode' variables."
193 (markdown-test-file "inline.text"
194 (should (= tab-width 4))
195 (should (eq font-lock-multiline t))
196 (should (eq major-mode 'markdown-mode))))
198 ;;; Element insertion tests:
200 (ert-deftest test-markdown-insertion/blank-line-before-1 ()
201 "Test function `markdown-ensure-blank-line-before' at beginning of line."
202 (markdown-test-file "syntax.text"
203 (search-forward "as plain text")
204 (should (= (point) 1556))
205 (beginning-of-line)
206 (should (= (point) 1505))
207 (should (looking-back "A Markdown-formatted\n" nil))
208 (should (not (markdown-prev-line-blank-p)))
209 (markdown-ensure-blank-line-before)
210 (should (looking-back "A Markdown-formatted\n\n" nil))
211 (should (markdown-prev-line-blank-p))))
213 (ert-deftest test-markdown-insertion/blank-line-before-2 ()
214 "Test function `markdown-ensure-blank-line-before' in middle of line."
215 (markdown-test-file "syntax.text"
216 (search-forward "as plain text")
217 (should (= (point) 1556))
218 (should (looking-back "as plain text" nil))
219 (should (not (markdown-prev-line-blank-p)))
220 (markdown-ensure-blank-line-before)
221 (should (looking-back "as plain text\n\n" nil))
222 (should (markdown-prev-line-blank-p))))
224 (ert-deftest test-markdown-insertion/blank-line-before-3 ()
225 "Test function `markdown-ensure-blank-line-before' with blank line before."
226 (markdown-test-file "syntax.text"
227 (search-forward "web.\n\nMarkdown is not a replacement for HTML")
228 (beginning-of-line)
229 (should (= (point) 2704))
230 (should (looking-back "web.\n\n" nil))
231 (should (markdown-prev-line-blank-p))
232 (markdown-ensure-blank-line-before)
233 (should (= (point) 2704))
234 (should (looking-back "web.\n\n" nil))
235 (should (markdown-prev-line-blank-p))))
237 (ert-deftest test-markdown-insertion/blank-line-before-4 ()
238 "Test function `markdown-ensure-blank-line-before' at beginning of buffer."
239 (markdown-test-string "first line"
240 (beginning-of-line)
241 (should (bobp))
242 (should (= (point-max) 11))
243 (markdown-ensure-blank-line-before)
244 (should (= (point-max) 11))
245 (should (string-equal (buffer-substring (point-min) (point-max))
246 "first line"))
247 (forward-word)
248 (markdown-ensure-blank-line-before)
249 (should (string-equal (buffer-substring (point-min) (point-max))
250 "first\n\n line"))))
252 (ert-deftest test-markdown-insertion/blank-line-after-1 ()
253 "Test function `markdown-ensure-blank-line-after' at end of line."
254 (markdown-test-file "syntax.text"
255 (search-forward "as plain text")
256 (should (= (point) 1556))
257 (end-of-line)
258 (should (= (point) 1573))
259 (should (looking-at "\nlike it's been"))
260 (should (not (markdown-next-line-blank-p)))
261 (markdown-ensure-blank-line-after)
262 (should (looking-at "\n\nlike it's been"))
263 (should (markdown-next-line-blank-p))))
265 (ert-deftest test-markdown-insertion/blank-line-after-2 ()
266 "Test function `markdown-ensure-blank-line-after' in middle of line."
267 (markdown-test-file "syntax.text"
268 (search-forward "as plain text")
269 (should (= (point) 1556))
270 (should (looking-at ", without looking"))
271 (should (not (markdown-next-line-blank-p)))
272 (markdown-ensure-blank-line-after)
273 (should (looking-at "\n\n, without looking"))
274 (should (markdown-next-line-blank-p))))
276 (ert-deftest test-markdown-insertion/blank-line-after-3 ()
277 "Test function `markdown-ensure-blank-line-after' with blank line after."
278 (markdown-test-file "syntax.text"
279 (search-forward "*writing* for the web.")
280 (should (= (point) 2702))
281 (should (looking-at "\n\nMarkdown is not a replacement for HTML"))
282 (should (markdown-next-line-blank-p))
283 (markdown-ensure-blank-line-after)
284 (should (= (point) 2702))
285 (should (looking-at "\n\nMarkdown is not a replacement for HTML"))
286 (should (markdown-next-line-blank-p))))
288 (ert-deftest test-markdown-insertion/blank-line-after-4 ()
289 "Test function `markdown-ensure-blank-line-after' at end of buffer."
290 (markdown-test-string "last line"
291 (end-of-line)
292 (should (eobp))
293 (should (= (point-max) 10))
294 (markdown-ensure-blank-line-after)
295 (should (= (point-max) 10))
296 (should (string-equal (buffer-substring (point-min) (point-max))
297 "last line"))
298 (backward-word)
299 (markdown-ensure-blank-line-after)
300 (should (string-equal (buffer-substring (point-min) (point-max))
301 "last \n\nline"))))
303 (ert-deftest test-markdown-insertion/point-after-unwrap ()
304 "Test new point position calculations after unwrap operations."
305 (markdown-test-string "line **one**\n"
306 (let ((prefix (cons 6 8)) (suffix (cons 11 13)))
307 ;; Prefix
308 (should (eq (markdown-point-after-unwrap 6 prefix suffix) 6))
309 (should (eq (markdown-point-after-unwrap 7 prefix suffix) 6))
310 ;; Word
311 (should (eq (markdown-point-after-unwrap 8 prefix suffix) 6))
312 (should (eq (markdown-point-after-unwrap 9 prefix suffix) 7))
313 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 8))
314 ;; Suffix
315 (should (eq (markdown-point-after-unwrap 11 prefix suffix) 9))
316 (should (eq (markdown-point-after-unwrap 12 prefix suffix) 9))
317 ;; Immediately after
318 (should (eq (markdown-point-after-unwrap 13 prefix suffix) 9))))
319 (markdown-test-string "line _one_\n"
320 (let ((prefix (cons 6 7)) (suffix (cons 10 11)))
321 ;; Prefix
322 (should (eq (markdown-point-after-unwrap 6 prefix suffix) 6))
323 ;; Word
324 (should (eq (markdown-point-after-unwrap 7 prefix suffix) 6))
325 (should (eq (markdown-point-after-unwrap 8 prefix suffix) 7))
326 (should (eq (markdown-point-after-unwrap 9 prefix suffix) 8))
327 ;; Suffix
328 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 9))
329 ;; Immediately after
330 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 9)))))
332 (ert-deftest test-markdown-insertion/unwrap-thing-at-point-italic ()
333 "Test function `markdown-unwrap-thing-at-point' on italics."
334 (markdown-test-file "syntax.text"
335 ;; Unwrap *not*
336 (goto-char 2859)
337 (should (thing-at-point-looking-at markdown-regex-italic))
338 (should (equal (markdown-unwrap-thing-at-point
339 markdown-regex-italic 1 3)
340 (cons 2859 2862)))
341 (should (= (point) 2859))
342 ;; Unwrap *publishing*
343 (goto-char 3064)
344 (should (thing-at-point-looking-at markdown-regex-italic))
345 (should (equal (markdown-unwrap-thing-at-point
346 markdown-regex-italic 1 3)
347 (cons 3060 3070)))
348 (should (= (point) 3063))
349 ;; Unwrap *writing*
350 (goto-char 3101)
351 (should (thing-at-point-looking-at markdown-regex-italic))
352 (should (equal (markdown-unwrap-thing-at-point
353 markdown-regex-italic 1 3)
354 (cons 3093 3100)))
355 (should (= (point) 3100))))
357 (ert-deftest test-markdown-insertion/unwrap-things-in-region-italic ()
358 "Test function `markdown-unwrap-things-in-region' on italics."
359 (markdown-test-file "syntax.text"
360 (should (equal (markdown-unwrap-things-in-region
361 2704 3207 markdown-regex-italic 1 3)
362 (cons 2704 3201)))))
364 (ert-deftest test-markdown-insertion/unwrap-things-in-region-bound ()
365 "Ensure that `markdown-unwrap-things-in-region' respects end bound"
366 (markdown-test-string "**a** **b** **c** **d** **e** **f**"
367 ;; Set region to unrwap a, b, c, and d only. If endpoint is not
368 ;; respected (i.e, not adjusted for character removal), the
369 ;; function will unwrap e and f also.
370 (should (equal (markdown-unwrap-things-in-region
371 1 24 markdown-regex-bold 2 4)
372 (cons 1 8)))
373 (should (string-equal (buffer-string) "a b c d **e** **f**"))))
375 (ert-deftest test-markdown-insertion/unwrap-things-in-region-links ()
376 "Test function `markdown-unwrap-things-in-region' on inline links."
377 (markdown-test-string "a [link](http://jblevins.org/) or [two](/).\n"
378 (should (equal (markdown-unwrap-things-in-region
379 (point-min) (point-max) markdown-regex-link-inline 0 3)
380 (cons 1 16)))
381 (should (string-equal (buffer-string) "a link or two.\n"))))
383 (ert-deftest test-markdown-insertion/toggle-bold ()
384 "Test toggling functionality of `markdown-insert-bold'."
385 (markdown-test-string "one **two** three"
386 (forward-word 2)
387 (markdown-insert-bold)
388 (should (string-equal (buffer-string) "one two three"))
389 (should (= (point) 8))
390 (forward-word)
391 (markdown-insert-bold)
392 (should (= (point) 16))
393 (should (string-equal (buffer-string) "one two **three**"))))
395 (ert-deftest test-markdown-insertion/toggle-italic ()
396 "Test toggling functionality of `markdown-insert-italic'."
397 (markdown-test-string "one *two* three"
398 (forward-word 2)
399 (markdown-insert-italic)
400 (should (string-equal (buffer-string) "one two three"))
401 (should (= (point) 8))
402 (forward-word)
403 (markdown-insert-italic)
404 (should (string-equal (buffer-string) "one two *three*"))
405 (should (= (point) 15))))
407 (ert-deftest test-markdown-insertion/toggle-code ()
408 "Test toggling functionality of `markdown-insert-code'."
409 (markdown-test-string "one `two` three"
410 (forward-word 2)
411 (markdown-insert-code)
412 (should (string-equal (buffer-string) "one two three"))
413 (should (= (point) 8))
414 (forward-word)
415 (markdown-insert-code)
416 (should (string-equal (buffer-string) "one two `three`"))
417 (should (= (point) 15))))
419 (ert-deftest test-markdown-insertion/toggle-kbd ()
420 "Test toggling functionality of `markdown-insert-code'."
421 (markdown-test-string "test <kbd>C-c C-s k</kbd> toggle"
422 (forward-word 2)
423 (markdown-insert-kbd)
424 (should (string-equal (buffer-string) "test C-c C-s k toggle"))
425 (should (= (point) 6))
426 (backward-word)
427 (markdown-insert-kbd)
428 (should (string-equal (buffer-string) "<kbd>test</kbd> C-c C-s k toggle"))
429 (should (= (point) 6))))
431 (ert-deftest test-markdown-insertion/toggle-wiki-link-alias-first ()
432 "Test toggling of `markdown-insert-wiki-link' with alias first.
433 Test point position upon removal and insertion."
434 (let ((markdown-wiki-link-alias-first t))
435 (markdown-test-string "[[text|page]]"
436 (goto-char 5) ; point in interior of alias text, at 'x'
437 (call-interactively 'markdown-insert-wiki-link)
438 (should (= (point) 3)) ; leave point at, at 'x'
439 (should (string-equal (buffer-string) "text"))
440 (call-interactively 'markdown-insert-wiki-link)
441 (should (= (point) 5)) ; leave point at, at 'x'
442 (should (string-equal (buffer-string) "[[text]]")))
443 (markdown-test-string "[[text|page]]"
444 (goto-char 10) ; point in interior of link text, at 'g'
445 (call-interactively 'markdown-insert-wiki-link)
446 (should (= (point) 5)) ; leave point at end of alias text
447 (should (string-equal (buffer-string) "text"))
448 (call-interactively 'markdown-insert-wiki-link)
449 (should (= (point) 7)) ; leave point at end of alias text
450 (should (string-equal (buffer-string) "[[text]]")))))
452 (ert-deftest test-markdown-insertion/toggle-wiki-link-alias-last ()
453 "Test toggling of `markdown-insert-wiki-link' with alias last.
454 Test point position upon removal and insertion."
455 (let ((markdown-wiki-link-alias-first nil))
456 (markdown-test-string "[[page|text]]"
457 (goto-char 10) ; point in interior of alias text, at 'x'
458 (call-interactively 'markdown-insert-wiki-link)
459 (goto-char 3) ; leave point at, at 'x'
460 (should (string-equal (buffer-string) "text"))
461 (call-interactively 'markdown-insert-wiki-link)
462 (should (= (point) 5)) ; leave point at, at 'x'
463 (should (string-equal (buffer-string) "[[text]]")))
464 (markdown-test-string "[[page|text]]"
465 (goto-char 3) ; point in interior of link text, at 'g'
466 (call-interactively 'markdown-insert-wiki-link)
467 (should (= (point) 1)) ; leave point at beginning of alias text
468 (should (string-equal (buffer-string) "text"))
469 (call-interactively 'markdown-insert-wiki-link)
470 (should (= (point) 3)) ; leave point at beginning of alias text
471 (should (string-equal (buffer-string) "[[text]]")))))
473 (ert-deftest test-markdown-insertion/bold-region ()
474 "Test region functionality of `markdown-insert-bold'."
475 (markdown-test-string "one two three"
476 (push-mark (point) t t)
477 (forward-word 2)
478 (markdown-insert-bold)
479 (should (string-equal (buffer-string) "**one two** three"))
480 (should (= (point) 10))))
482 (ert-deftest test-markdown-insertion/italic-region ()
483 "Test region functionality of `markdown-insert-italic'."
484 (markdown-test-string "one two three"
485 (transient-mark-mode)
486 (push-mark (point) t t)
487 (forward-word 2)
488 (markdown-insert-italic)
489 (should (string-equal (buffer-string) "*one two* three"))
490 (should (= (point) 9))))
492 (ert-deftest test-markdown-insertion/code-region ()
493 "Test region functionality of `markdown-insert-code'."
494 (markdown-test-string "one two three"
495 (transient-mark-mode)
496 (push-mark (point) t t)
497 (forward-word 2)
498 (markdown-insert-code)
499 (should (string-equal (buffer-string) "`one two` three"))
500 (should (= (point) 9))))
502 (ert-deftest test-markdown-insertion/kbd-region ()
503 "Test region functionality of `markdown-insert-kbd'."
504 (markdown-test-string "one two three"
505 (transient-mark-mode)
506 (push-mark (point) t t)
507 (forward-word 2)
508 (markdown-insert-kbd)
509 (should (string-equal (buffer-string) "<kbd>one two</kbd> three"))
510 (should (= (point) 13))))
512 (ert-deftest test-markdown-insertion/atx-line ()
513 "Test ATX header insertion without region."
514 (markdown-test-string "line one\nline two\n"
515 (forward-word)
516 (markdown-insert-header-atx-1)
517 (should (= (point) 11))
518 (should (string-equal (buffer-substring (point-min) (point-max))
519 "# line one #\n\nline two\n"))
520 (forward-line 2)
521 (markdown-insert-header-atx-2)
522 (should (= (point) 26))
523 (should (string-equal (buffer-substring (point-min) (point-max))
524 "# line one #\n\n## line two ##\n\n"))))
526 (ert-deftest test-markdown-insertion/atx-region ()
527 "Test ATX header insertion with region."
528 (markdown-test-string "line one\nline two\n"
529 (transient-mark-mode)
530 (forward-char 5)
531 (push-mark (point) t t)
532 (forward-word)
533 (should (string-equal (buffer-substring (region-beginning) (region-end))
534 "one"))
535 (markdown-insert-header-atx-4)
536 (should (= (point) 16))
537 (should (string-equal (buffer-substring (point-min) (point-max))
538 "line \n\n#### one ####\n\nline two\n"))))
540 (ert-deftest test-markdown-insertion/atx-blank ()
541 "Test ATX header insertion on blank line."
542 (markdown-test-string "line one\n\nline two\n"
543 (forward-line)
544 (markdown-insert-header-atx-3)
545 (should (string-equal (buffer-substring (point-min) (point-max))
546 "line one\n\n### ###\n\nline two\n"))
547 (should (= (point) 15))
548 (should (looking-at " ###\n"))))
550 (ert-deftest test-markdown-insertion/atx-region-whitespace ()
551 "Test ATX header insertion using a region with whitespace."
552 (markdown-test-string " line one\n\nline two\n \n"
553 (transient-mark-mode)
554 (push-mark (point) t t)
555 (goto-char (point-max))
556 (markdown-insert-header-atx-2)
557 (should (string-equal (buffer-substring (point-min) (point-max))
558 "## line one line two ##"))
559 (should (= (point) 21))
560 (should (looking-at " ##"))))
562 (ert-deftest test-markdown-insertion/atx-line-whitespace ()
563 "Test ATX header insertion using current line with whitespace."
564 (markdown-test-string " line one \n\nline two\n"
565 (goto-char (line-end-position))
566 (markdown-insert-header-atx-3)
567 (should (string-equal (buffer-substring (point-min) (point-max))
568 "### line one ###\n\nline two\n"))
569 (should (= (point) 13))
570 (should (looking-at " ###\n"))))
572 (ert-deftest test-markdown-insertion/atx-replace-atx ()
573 "Test ATX header insertion when replacing an existing ATX header."
574 (markdown-test-string "## replace ##\n"
575 (markdown-insert-header-atx-4)
576 (should (string-equal (buffer-string) "#### replace ####\n\n"))
577 (should (looking-at " ####\n"))))
579 (ert-deftest test-markdown-insertion/atx-replace-setext-1 ()
580 "Test ATX header insertion when replacing an existing setext header."
581 (markdown-test-string "replace\n=======\n"
582 (markdown-insert-header-atx-2)
583 (should (string-equal (buffer-string) "## replace ##\n\n"))
584 (should (looking-at " ##\n"))))
586 (ert-deftest test-markdown-insertion/atx-replace-setext-2 ()
587 "Test ATX header insertion when replacing an existing setext header."
588 (markdown-test-string "replace\n-------\n"
589 (markdown-insert-header-atx-5)
590 (should (string-equal (buffer-string) "##### replace #####\n\n"))
591 (should (looking-at " #####\n"))))
593 (ert-deftest test-markdown-insertion/atx-asymmetric-point ()
594 "Test point after ATX header insertion with `markdown-asymmetric-header'."
595 (markdown-test-string
596 "Test"
597 (let ((markdown-asymmetric-header t))
598 (markdown-insert-header-atx-5)
599 (should (= (point) 11))
600 (should (string-equal (buffer-string) "##### Test")))))
602 (ert-deftest test-markdown-insertion/setext-line ()
603 "Test setext header insertion without region."
604 (markdown-test-string "line one\nline two\n"
605 (forward-word)
606 (markdown-insert-header-setext-1)
607 (should (string-equal (buffer-substring (point-min) (point-max))
608 "line one\n========\n\nline two\n"))
609 (forward-line 3)
610 (markdown-insert-header-setext-2)
611 (should (string-equal (buffer-substring (point-min) (point-max))
612 "line one\n========\n\nline two\n--------\n\n"))))
614 (ert-deftest test-markdown-insertion/setext-region ()
615 "Test setext header insertion with region."
616 (markdown-test-string "line one\nline two\n"
617 (transient-mark-mode)
618 (forward-char 5)
619 (push-mark (point) t t)
620 (forward-word)
621 (should (string-equal (buffer-substring (region-beginning) (region-end))
622 "one"))
623 (markdown-insert-header-setext-1)
624 (should (string-equal (buffer-substring (point-min) (point-max))
625 "line \n\none\n===\n\nline two\n"))))
627 (ert-deftest test-markdown-insertion/setext-blank ()
628 "Test setext header insertion on blank line."
629 (markdown-test-string "line one\n\nline two\n"
630 (forward-line)
631 (markdown-insert-header 2 "foo" t)
632 (should (string-equal (buffer-substring (point-min) (point-max))
633 "line one\n\nfoo\n---\n\nline two\n"))
634 (should (= (point) 14))
635 (should (looking-at "\n---"))))
637 (ert-deftest test-markdown-insertion/setext-region-whitespace ()
638 "Test setext header insertion using a region with whitespace."
639 (markdown-test-string " line one\n\nline two\n \n"
640 (transient-mark-mode)
641 (push-mark (point) t t)
642 (goto-char (point-max))
643 (markdown-insert-header-setext-1)
644 (should (string-equal (buffer-substring (point-min) (point-max))
645 "line one line two\n================="))
646 (should (= (point) 18))
647 (should (looking-at "\n===="))))
649 (ert-deftest test-markdown-insertion/setext-line-whitespace ()
650 "Test setext header insertion using current line with whitespace."
651 (markdown-test-string " line one \n\nline two\n"
652 (goto-char (line-end-position))
653 (markdown-insert-header-setext-2)
654 (should (string-equal (buffer-substring (point-min) (point-max))
655 "line one\n--------\n\nline two\n"))
656 (should (= (point) 9))
657 (should (looking-at "\n---"))))
659 (ert-deftest test-markdown-insertion/setext-replace-atx ()
660 "Test setext header insertion when replacing an existing ATX header."
661 (markdown-test-string "## replace ##\n"
662 (markdown-insert-header-setext-1)
663 (should (string-equal (buffer-string) "replace\n=======\n\n"))
664 (should (looking-at "\n==="))))
666 (ert-deftest test-markdown-insertion/setext-replace-setext-1 ()
667 "Test setext header insertion when replacing an existing setext title."
668 (markdown-test-string "replace\n=======\n"
669 (markdown-insert-header-setext-2)
670 (should (string-equal (buffer-string) "replace\n-------\n\n"))
671 (should (looking-at "\n---"))))
673 (ert-deftest test-markdown-insertion/setext-replace-setext-2 ()
674 "Test setext header insertion when replacing an existing setext section."
675 (markdown-test-string "replace\n-------\n"
676 (markdown-insert-header-setext-1)
677 (should (string-equal (buffer-string) "replace\n=======\n\n"))
678 (should (looking-at "\n==="))))
680 (ert-deftest test-markdown-insertion/header-dwim ()
681 "Test 'do what I mean' header insertion."
682 (markdown-test-file "outline.text"
683 (call-interactively 'markdown-insert-header-dwim)
684 (should (looking-at " #$"))
685 (end-of-defun 2)
686 (call-interactively 'markdown-insert-header-dwim)
687 (beginning-of-line)
688 (should (looking-at "^# #$"))
689 (end-of-defun 3)
690 (call-interactively 'markdown-insert-header-dwim)
691 (beginning-of-line)
692 (should (looking-at "^### ###$"))))
694 (ert-deftest test-markdown-insertion/header-dwim-prefix ()
695 "Test 'do what I mean' header insertion with prefix arguments."
696 (let ((tests (list '(nil . "## abc ##")
697 '(1 . "# abc #")
698 '(2 . "## abc ##")
699 '(3 . "### abc ###")
700 '(4 . "#### abc ####")
701 '(5 . "##### abc #####")
702 '(6 . "###### abc ######")
703 '((4) . "# abc #")
704 '((16) . "### abc ###"))))
705 (dolist (test tests)
706 (markdown-test-string "## atx\n\nabc"
707 (goto-char (point-max))
708 (let ((current-prefix-arg (car test)))
709 (call-interactively 'markdown-insert-header-dwim)
710 (should (string-equal
711 (buffer-substring (line-beginning-position) (line-end-position))
712 (cdr test))))))))
714 (ert-deftest test-markdown-insertion/header-setext-dwim-prefix ()
715 "Test 'do what I mean' header insertion with prefix arguments."
716 (let ((tests (list '(nil . "abc\n---")
717 '(1 . "abc\n===")
718 '(2 . "abc\n---")
719 '(3 . "### abc ###")
720 '(4 . "#### abc ####")
721 '(5 . "##### abc #####")
722 '(6 . "###### abc ######")
723 '((4) . "abc\n===")
724 '((16) . "### abc ###"))))
725 (dolist (test tests)
726 (markdown-test-string "atx\n---\n\nabc"
727 (goto-char (point-max))
728 (let ((current-prefix-arg (car test)))
729 (call-interactively 'markdown-insert-header-setext-dwim)
730 (should (string-equal
731 (buffer-substring (line-beginning-position) (line-end-position 2))
732 (cdr test))))))))
734 (ert-deftest test-markdown-insertion/header-setext-dwim ()
735 "Test 'do what I mean' header insertion with setext headers."
736 (markdown-test-string
737 "asdfasfasfdsadfasdfasdf\n======="
738 (goto-char 12)
739 (call-interactively 'markdown-insert-header-dwim)
740 (should (string-equal
741 (buffer-string)
742 "asdfasfasfdsadfasdfasdf\n======================="))))
744 (ert-deftest test-markdown-insertion/remove-header ()
745 "Test ATX and setext header."
746 (markdown-test-string
747 "# atx1\n\n## atx2 ##\n\nsetext1\n=======\n\nsetext2\n-------\n"
748 (should (equal (markdown-remove-header) (cons 1 5)))
749 (forward-line)
750 (should (not (markdown-remove-header)))
751 (forward-line)
752 (should (equal (markdown-remove-header) (cons 7 11)))
753 (forward-line)
754 (should (not (markdown-remove-header)))
755 (forward-line)
756 (should (equal (markdown-remove-header) (cons 13 20)))
757 (forward-line)
758 (should (not (markdown-remove-header)))
759 (forward-line)
760 (should (equal (markdown-remove-header) (cons 22 29)))
761 (should (string-equal (buffer-string)
762 "atx1\n\natx2\n\nsetext1\n\nsetext2\n"))))
764 (ert-deftest test-markdown-insertion/italic-unwrap-region ()
765 "A test of inserting italics with italic text in the region."
766 (markdown-test-string "*foo* bar *baz*"
767 (transient-mark-mode)
768 (push-mark (point) t t)
769 (end-of-line)
770 (markdown-insert-italic)
771 (should (string-equal (buffer-string) "*foo bar baz*"))))
773 (ert-deftest test-markdown-insertion/bold-unwrap-region ()
774 "A test of inserting bold with italic text in the region."
775 (markdown-test-string "*foo* **bar** *baz*"
776 (transient-mark-mode)
777 (push-mark (point) t t)
778 (end-of-line)
779 (markdown-insert-bold)
780 (should (string-equal (buffer-string) "***foo* bar *baz***"))))
782 (ert-deftest test-markdown-insertion/code-unwrap-region ()
783 "A test of inserting code with code already in the region."
784 (markdown-test-string "`foo` *bar* `baz`"
785 (transient-mark-mode)
786 (push-mark (point) t t)
787 (end-of-line)
788 (markdown-insert-code)
789 (should (string-equal (buffer-string) "`foo *bar* baz`"))))
791 (ert-deftest test-markdown-insertion/hr-order ()
792 "Test inserting horizontal rules."
793 (dotimes (n (length markdown-hr-strings))
794 (markdown-test-string ""
795 (let ((current-prefix-arg n))
796 (call-interactively 'markdown-insert-hr))
797 (should (string-equal (buffer-string) (nth (1- n) markdown-hr-strings))))))
799 (ert-deftest test-markdown-insertion/hr-prefix ()
800 "Test inserting horizontal rule with C-u prefix."
801 (markdown-test-string ""
802 (let ((current-prefix-arg '(4)))
803 (call-interactively 'markdown-insert-hr))
804 (should (string-equal (buffer-string) (car (last markdown-hr-strings))))))
806 (ert-deftest test-markdown-insertion/hr-bob ()
807 "Test inserting horizontal rule at beginning of buffer."
808 (markdown-test-string "one line\n"
809 (call-interactively 'markdown-insert-hr)
810 (should (string-equal (buffer-string)
811 (concat (car markdown-hr-strings)
812 "\n\none line\n")))))
814 (ert-deftest test-markdown-insertion/hr-eob ()
815 "Test inserting horizontal rule at end of buffer."
816 (markdown-test-string "one line\n"
817 (forward-line)
818 (call-interactively 'markdown-insert-hr)
819 (should (string-equal (buffer-string)
820 (concat "one line\n\n" (car markdown-hr-strings))))))
822 (ert-deftest test-markdown-insertion/hr-mob ()
823 "Test inserting horizontal rule in middle of buffer."
824 (markdown-test-string "one line\n"
825 (forward-word)
826 (let ((markdown-hr-strings '("----------")))
827 (call-interactively 'markdown-insert-hr)
828 (should (string-equal (buffer-string)
829 (concat "one\n\n" (car markdown-hr-strings)
830 "\n\n line\n"))))))
832 (ert-deftest test-markdown-insertion/pre-region-1 ()
833 "Test `markdown-pre-region'."
834 ;; Simple test as non-interactive command
835 (markdown-test-string "line one\nline two\n"
836 (markdown-pre-region (line-beginning-position) (line-end-position))
837 (should (string-equal (buffer-string) " line one\n\nline two\n")))
838 ;; Test removal of whitespace before and after region
839 (markdown-test-string "line one abc\nline two\n"
840 (markdown-pre-region 6 9)
841 (should (string-equal (buffer-string) "line\n\n one\n\nabc\nline two\n")))
842 ;; Simple test as interactive command
843 (markdown-test-string "line one\nline two\n"
844 (push-mark (point) t t)
845 (forward-line 2)
846 (call-interactively 'markdown-pre-region)
847 (should (string-equal (buffer-string) " line one\n line two\n\n"))))
849 (ert-deftest test-markdown-insertion/blockquote-region-1 ()
850 "Test `markdown-blockquote-region'."
851 ;; Simple test as non-interactive command
852 (markdown-test-string "line one\nline two\n"
853 (markdown-blockquote-region (line-beginning-position) (line-end-position))
854 (should (string-equal (buffer-string) "> line one\n\nline two\n")))
855 ;; Test removal of whitespace before and after region
856 (markdown-test-string "line one abc\nline two\n"
857 (markdown-blockquote-region 6 9)
858 (should (string-equal (buffer-string) "line\n\n> one\n\nabc\nline two\n")))
859 ;; Simple test as interactive command
860 (markdown-test-string "line one\nline two\n"
861 (push-mark (point) t t)
862 (forward-line 2)
863 (call-interactively 'markdown-blockquote-region)
864 (should (string-equal (buffer-string) "> line one\n> line two\n\n"))))
866 (ert-deftest test-markdown-insertion/pre-nested-lists ()
867 "Test `markdown-pre-indentation' and `markdown-insert-pre' with nested list."
868 (markdown-test-string "* item\n * item\n"
869 ;; before the first item
870 (should (string-equal (markdown-pre-indentation (point)) " "))
871 (markdown-insert-pre)
872 (beginning-of-line)
873 (should (markdown-prev-line-blank-p))
874 (should (looking-at "^ $"))
875 (should (markdown-next-line-blank-p))
876 ;; before the second item
877 (forward-line 3)
878 (should (string-equal (markdown-pre-indentation (point)) " "))
879 (markdown-insert-pre)
880 (beginning-of-line)
881 (should (markdown-prev-line-blank-p))
882 (should (looking-at "^ $"))
883 (should (markdown-next-line-blank-p))
884 ;; after the second item
885 (forward-line 3)
886 (should (string-equal (markdown-pre-indentation (point)) " "))
887 (markdown-insert-pre)
888 (beginning-of-line)
889 (should (markdown-prev-line-blank-p))
890 (should (looking-at "^ $"))
891 (should (markdown-next-line-blank-p))))
893 (ert-deftest test-markdown-insertion/pre-faux-list ()
894 "Test `markdown-pre-indentation' following a list-marker in a pre block."
895 (markdown-test-string " * pre block, not a list item\n"
896 (should (string-equal (markdown-pre-indentation (point-max)) " "))))
898 (ert-deftest test-markdown-insertion/blockquote-nested-lists ()
899 "Test blockquote insertion in a nested list context."
900 (markdown-test-string "* item\n * item\n"
901 ;; before the first item
902 (should (string-equal (markdown-blockquote-indentation (point)) ""))
903 (markdown-insert-blockquote)
904 (beginning-of-line)
905 (should (markdown-prev-line-blank-p))
906 (should (looking-at "^> $"))
907 (should (markdown-next-line-blank-p))
908 ;; before the second item
909 (forward-line 3)
910 (should (string-equal (markdown-blockquote-indentation (point)) " "))
911 (markdown-insert-blockquote)
912 (beginning-of-line)
913 (should (markdown-prev-line-blank-p))
914 (should (looking-at "^ > $"))
915 (should (markdown-next-line-blank-p))
916 ;; after the second item
917 (forward-line 3)
918 (should (string-equal (markdown-blockquote-indentation (point)) " "))
919 (markdown-insert-blockquote)
920 (beginning-of-line)
921 (should (markdown-prev-line-blank-p))
922 (should (looking-at "^ > $"))
923 (should (markdown-next-line-blank-p))))
925 (ert-deftest test-markdown-insertion/empty-italic ()
926 "Test `markdown-insert-italic' with no word at point and no region."
927 (markdown-test-string ""
928 (call-interactively 'markdown-insert-italic)
929 (should (string-equal (buffer-string) "**"))
930 (should (= (point) 2))))
932 (ert-deftest test-markdown-insertion/empty-bold ()
933 "Test `markdown-insert-bold' with no word at point and no region."
934 (markdown-test-string ""
935 (call-interactively 'markdown-insert-bold)
936 (should (string-equal (buffer-string) "****"))
937 (should (= (point) 3))))
939 (ert-deftest test-markdown-insertion/uri ()
940 "Test `markdown-insert-uri'."
941 (markdown-test-string "http://jblevins.org/projects/markdown-mode/"
942 (call-interactively 'markdown-insert-uri)
943 (should (string-equal (buffer-string) "<http://jblevins.org/projects/markdown-mode/>"))
944 (should (= (point) 2))
945 (call-interactively 'markdown-insert-uri)
946 (should (string-equal (buffer-string) "http://jblevins.org/projects/markdown-mode/"))
947 (should (= (point) 1))
948 (erase-buffer)
949 (call-interactively 'markdown-insert-uri)
950 (should (string-equal (buffer-string) "<>"))
951 (should (= (point) 2))))
953 (ert-deftest test-markdown-insertion/list-item ()
954 "Test `markdown-insert-list-item' on several lists."
955 ;; No existing list
956 (markdown-test-string "abc"
957 (goto-char (point-max))
958 (call-interactively 'markdown-insert-list-item)
959 (should (string-equal (buffer-string) "abc\n * "))
960 (should (= (point) 9)))
961 ;; Following a list item, on the same line
962 (markdown-test-string " * foo"
963 (goto-char (point-max))
964 (call-interactively 'markdown-insert-list-item)
965 (should (string-equal (buffer-string) " * foo\n * ")))
966 ;; Following a list item, on the next line
967 (markdown-test-string "- foo\n"
968 (goto-char (point-max))
969 (call-interactively 'markdown-insert-list-item)
970 (should (string-equal (buffer-string) "- foo\n- ")))
971 ;; Following a list item, after a blank line
972 (markdown-test-string "- foo\n\n"
973 (goto-char (point-max))
974 (call-interactively 'markdown-insert-list-item)
975 (should (string-equal (buffer-string) "- foo\n\n- ")))
976 ;; Preceding a list item
977 (markdown-test-string "- foo\n"
978 (goto-char (point-min))
979 (call-interactively 'markdown-insert-list-item)
980 (should (string-equal (buffer-string) "- \n- foo\n")))
981 ;; Preceding a list item and a blank line
982 (markdown-test-string "\n\n- foo\n"
983 (goto-char (point-min))
984 (call-interactively 'markdown-insert-list-item)
985 (should (string-equal (buffer-string) "- \n\n- foo\n")))
986 ;; In the middle of a list item
987 (markdown-test-string "- foo bar\n"
988 (forward-word)
989 (call-interactively 'markdown-insert-list-item)
990 (should (string-equal (buffer-string) "- foo\n- bar\n")))
991 ;; Before a list marker, but not at beginning of line
992 (markdown-test-string " - foo\n"
993 (forward-char 2)
994 (call-interactively 'markdown-insert-list-item)
995 (should (string-equal (buffer-string) " - \n - foo\n")))
996 ;; Following an ordered list item
997 (markdown-test-string "6. foo"
998 (goto-char (point-max))
999 (call-interactively 'markdown-insert-list-item)
1000 (should (string-equal (buffer-string) "6. foo\n7. ")))
1001 ;; Following a fancy list item, on the next line
1002 (markdown-test-string "#. foo"
1003 (goto-char (point-max))
1004 (call-interactively 'markdown-insert-list-item)
1005 (should (string-equal (buffer-string) "#. foo\n#. ")))
1006 ;; Following a nested ordered list item
1007 (markdown-test-string "6. foo\n 1. bar"
1008 (goto-char (point-max))
1009 (call-interactively 'markdown-insert-list-item)
1010 (should (string-equal (buffer-string) "6. foo\n 1. bar\n 2. ")))
1011 ;; Preceding an ordered list item
1012 (markdown-test-string "\n1. foo\n2. bar"
1013 (goto-char (point-min))
1014 (call-interactively 'markdown-insert-list-item)
1015 (should (string-equal (buffer-string) "1. \n1. foo\n2. bar")))
1016 ;; Preserve previous spacing in ordered list
1017 (markdown-test-string "1. foo"
1018 (goto-char (point-max))
1019 (call-interactively 'markdown-insert-list-item)
1020 (should (string-equal (buffer-string) "1. foo\n2. ")))
1021 ;; Adjust spacing for number width changes (e.g., 9 -> 10)
1022 (markdown-test-string "9. foo"
1023 (goto-char (point-max))
1024 (call-interactively 'markdown-insert-list-item)
1025 (should (string-equal (buffer-string) "9. foo\n10. ")))
1026 ;; Don't adjust spacing for number width changes if no extra whitespace
1027 (markdown-test-string "99. foo"
1028 (goto-char (point-max))
1029 (call-interactively 'markdown-insert-list-item)
1030 (should (string-equal (buffer-string) "99. foo\n100. ")))
1031 ;; Don't adjust spacing if tabs are used as whitespace
1032 (markdown-test-string "9.\tfoo"
1033 (goto-char (point-max))
1034 (call-interactively 'markdown-insert-list-item)
1035 (should (string-equal (buffer-string) "9.\tfoo\n10.\t"))))
1037 (ert-deftest test-markdown-insertion/nested-list-marker ()
1038 "Test marker detection for `markdown-insert-list-item'."
1039 (markdown-test-string
1040 "1. A\n * AA\n 1. AAA"
1041 (goto-char (point-max))
1042 (let ((current-prefix-arg '(4)))
1043 (call-interactively 'markdown-insert-list-item))
1044 (should (eq (point) 36))
1045 (should (looking-back "\* "))
1046 (should (string-equal
1047 (buffer-string)
1048 "1. A\n * AA\n 1. AAA\n * "))
1049 (let ((current-prefix-arg '(4)))
1050 (call-interactively 'markdown-insert-list-item))
1051 (should (eq (point) 40))
1052 (should (looking-back "2\. "))
1053 (should (string-equal
1054 (buffer-string)
1055 "1. A\n * AA\n 1. AAA\n * \n2. "))
1056 (let ((current-prefix-arg '(4)))
1057 (call-interactively 'markdown-insert-list-item))
1058 (should (eq (point) 44))
1059 (should (looking-back "3\. "))
1060 (should (string-equal
1061 (buffer-string)
1062 "1. A\n * AA\n 1. AAA\n * \n2. \n3. "))))
1064 (ert-deftest test-markdown-insertion/reference-link ()
1065 "Basic tests for `markdown-insert-reference-link'."
1066 ;; Test optional parameters (leave point after link)
1067 (markdown-test-string ""
1068 (markdown-insert-reference-link "abc" "1")
1069 (should (string-equal (buffer-string) "[abc][1]"))
1070 (should (= (point) 9)))
1071 ;; Full link without title (leave point after link)
1072 (markdown-test-string ""
1073 (markdown-insert-reference-link "link" "label" "http://jblevins.org/")
1074 (should (string-equal (buffer-string) "[link][label]\n\n[label]: http://jblevins.org/\n"))
1075 (should (= (point) 14)))
1076 ;; Full link without label or title (leave point after link)
1077 (markdown-test-string ""
1078 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
1079 (should (string-equal (buffer-string) "[link][]\n\n[link]: http://jblevins.org/\n"))
1080 (should (= (point) 9)))
1081 ;; Link only with no label, URL, or title (leave point after link)
1082 (markdown-test-string ""
1083 (markdown-insert-reference-link "link" "")
1084 (should (string-equal (buffer-string) "[link][]"))
1085 (should (= (point) 9))))
1087 (ert-deftest test-markdown-insertion/reference-link-end ()
1088 "Basic reference link insertion test for 'end location."
1089 (let ((markdown-reference-location 'end))
1090 (markdown-test-string "first para\n\nsecond para\n"
1091 (end-of-line)
1092 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
1093 (should (= (point) 19))
1094 (goto-char (point-min))
1095 (forward-line 4)
1096 (should (looking-at "\\[link\\]: http://jblevins.org/")))))
1098 (ert-deftest test-markdown-insertion/reference-link-immediately ()
1099 "Basic reference link insertion test for 'immediately location."
1100 (let ((markdown-reference-location 'immediately))
1101 (markdown-test-string "first para\n\nsecond para\n"
1102 (end-of-line)
1103 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
1104 (should (= (point) 19))
1105 (goto-char (point-min))
1106 (forward-line 2)
1107 (should (looking-at "\\[link\\]: http://jblevins.org/")))))
1109 (ert-deftest test-markdown-insertion/reference-link-header ()
1110 "Basic reference link and definition insertion test for 'header location."
1111 (let ((markdown-reference-location 'header))
1112 (markdown-test-string "par one\n\npar two\n\n### header\n"
1113 (end-of-line)
1114 (markdown-insert-reference-link "link" "")
1115 (markdown-insert-reference-definition "link")
1116 (should (= (point) 35))
1117 (should (looking-back "\\[link\\]: " nil)))))
1119 (ert-deftest test-markdown-insertion/reference-definition-block ()
1120 "Test whitespace when inserting a reference definition among others"
1121 (let ((markdown-reference-location 'header))
1122 (markdown-test-string "text
1124 [1]: https://www.gnu.org/
1126 ### header
1128 (markdown-insert-reference-definition "2")
1129 (should (= (point) 38))
1130 (should (looking-back "https://www.gnu.org/\n\\[2\\]: " nil)))))
1132 (ert-deftest test-markdown-insertion/inline-to-reference-link ()
1133 "Inline link to reference link conversion with tab completion."
1134 (markdown-test-string "[text](http://jblevins.org/ \"title\")"
1135 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET M-DEL M-DEL M-DEL [1] RET RET h TAB RET RET"))
1136 (should (string-equal (buffer-string) "[text][1]\n\n[1]: http://jblevins.org/ \"title\"\n"))))
1138 (ert-deftest test-markdown-insertion/inline-to-reference-link-2 ()
1139 "Inline link to reference link conversion with existing reference links."
1140 (markdown-test-string "[text](http://jblevins.org/ \"title\")\n\n[1]: https://www.gnu.org"
1141 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET M-DEL M-DEL M-DEL [1] RET RET"))
1142 (should (string-equal (buffer-string) "[text][1]\n\n[1]: https://www.gnu.org"))))
1144 (ert-deftest test-markdown-insertion/inline-link-angle-url-at-point ()
1145 "Test `markdown-insert-link' with angle URL at point."
1146 (markdown-test-string "<https://www.gnu.org/>"
1147 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET RET GNU RET RET"))
1148 (should (string-equal (buffer-string) "[GNU](https://www.gnu.org/)"))))
1150 (ert-deftest test-markdown-insertion/inline-link-plain-url-at-point ()
1151 "Test `markdown-insert-link' with plain URL at point."
1152 (markdown-test-string "https://www.gnu.org/"
1153 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET RET GNU RET RET"))
1154 (should (string-equal (buffer-string) "[GNU](https://www.gnu.org/)"))))
1156 (ert-deftest test-markdown-insertion/inline-link-reference-link-at-point ()
1157 "Test `markdown-insert-link' with reference link at point."
1158 (markdown-test-string ""
1159 (markdown-insert-reference-link "link" "label" "http://jblevins.org/")
1160 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET DEL DEL DEL DEL DEL DEL DEL http://example.com/ RET RET RET"))
1161 (should (string-equal (buffer-substring 1 28) "[link](http://example.com/)"))
1162 (should (= (point) 28))))
1164 (ert-deftest test-markdown-insertion/inline-link-active-region ()
1165 "Test `markdown-insert-link' with active region."
1166 (markdown-test-string "abc def ghi"
1167 (let ((tmm-orig transient-mark-mode))
1168 (transient-mark-mode 1)
1169 (push-mark (point) t t)
1170 (forward-word 2)
1171 (execute-kbd-macro (read-kbd-macro "M-x markdown-insert-link RET http://example.com/ RET RET RET"))
1172 (should (string-equal (buffer-string) "[abc def](http://example.com/) ghi"))
1173 (should (= (point) 31))
1174 (transient-mark-mode tmm-orig))))
1176 ;;; Footnote tests:
1178 (ert-deftest test-markdown-footnote/basic-end ()
1179 "Basic footnote insertion and deletion tests for 'end location."
1180 (let ((markdown-footnote-location 'end))
1181 (markdown-test-string "first line\nsecond line\n"
1182 ;; new buffer with no footnotes
1183 (should (= markdown-footnote-counter 0))
1184 ;; footnote insertion
1185 (end-of-line)
1186 (markdown-insert-footnote)
1187 (should (= (point) 35))
1188 (should (= markdown-footnote-counter 1))
1189 (should (looking-back "\\[^1\\]: " nil))
1190 ;; kill with point in footnote definition
1191 (insert "footnote text")
1192 (let (kill-ring)
1193 (markdown-footnote-kill))
1194 (should (= (point) 24))
1195 (should (bolp))
1196 (should (string-equal (buffer-string) "first line\nsecond line\n"))
1197 ;; insertion, counter should increment
1198 (goto-char (point-min))
1199 (end-of-line)
1200 (markdown-insert-footnote)
1201 (should (= (point) 35))
1202 (should (= markdown-footnote-counter 2))
1203 (should (looking-back "\\[^2\\]: " nil))
1204 (insert "footnote text")
1205 ;; return to marker
1206 (markdown-footnote-return)
1207 (should (= (point) 15))
1208 (should (looking-back "\\[^2\\]" nil))
1209 ;; kill with point at marker
1210 (let (kill-ring)
1211 (markdown-footnote-kill))
1212 (should (= (point) 11))
1213 (should (eolp))
1214 (should (string-equal (buffer-string) "first line\nsecond line\n")))))
1216 (ert-deftest test-markdown-footnote/basic-immediately ()
1217 "Basic footnote insertion and deletion tests for 'immediately location."
1218 (let ((markdown-footnote-location 'immediately))
1219 (markdown-test-string "first paragraph\n\nsecond paragraph\n"
1220 ;; new buffer with no footnotes
1221 (should (= markdown-footnote-counter 0))
1222 ;; footnote insertion
1223 (end-of-line)
1224 (markdown-insert-footnote)
1225 (should (= (point) 28))
1226 (should (= markdown-footnote-counter 1))
1227 (should (looking-back "\\[^1\\]: " nil))
1228 ;; kill with point in footnote definition
1229 (insert "footnote text")
1230 (let (kill-ring)
1231 (markdown-footnote-kill))
1232 (should (= (point) 18))
1233 (should (bolp))
1234 (should (string-equal (buffer-string)
1235 "first paragraph\n\nsecond paragraph\n")))))
1237 (ert-deftest test-markdown-footnote/basic-header ()
1238 "Basic footnote insertion and deletion tests for 'header location."
1239 (let ((markdown-footnote-location 'header))
1240 (markdown-test-string "par one\n\npar two\n\n### header\n"
1241 ;; new buffer with no footnotes
1242 (should (= markdown-footnote-counter 0))
1243 ;; footnote insertion
1244 (end-of-line)
1245 (markdown-insert-footnote)
1246 (should (= (point) 29))
1247 (should (= markdown-footnote-counter 1))
1248 (should (looking-back "\\[^1\\]: " nil))
1249 ;; kill with point in footnote definition
1250 (insert "footnote text")
1251 (let (kill-ring)
1252 (markdown-footnote-kill))
1253 (should (= (point) 19))
1254 (should (bolp))
1255 (should (string-equal (buffer-string)
1256 "par one\n\npar two\n\n### header\n"))
1257 ;; insertion, counter should increment
1258 (goto-char (point-min))
1259 (end-of-line)
1260 (markdown-insert-footnote)
1261 (should (= (point) 29))
1262 (should (= markdown-footnote-counter 2))
1263 (should (looking-back "\\[^2\\]: " nil))
1264 (insert "footnote text")
1265 ;; return to marker
1266 (markdown-footnote-return)
1267 (should (= (point) 12))
1268 (should (looking-back "\\[^2\\]" nil))
1269 ;; kill with point at marker
1270 (let (kill-ring)
1271 (markdown-footnote-kill))
1272 (should (= (point) 8))
1273 (should (eolp))
1274 (should (string-equal (buffer-string)
1275 "par one\n\npar two\n\n### header\n")))))
1277 (ert-deftest test-markdown-footnote/basic-subtree ()
1278 "Basic footnote insertion and deletion tests for 'subtree location."
1279 (let ((markdown-footnote-location 'subtree))
1280 (markdown-test-string "# h1\n\nfoo\n\n## h2\n\nbar\n"
1281 ;; new buffer with no footnotes
1282 (should (= markdown-footnote-counter 0))
1283 ;; footnote insertion
1284 (forward-line 2)
1285 (end-of-line)
1286 (markdown-insert-footnote)
1287 (should (= (point) 34))
1288 (should (= markdown-footnote-counter 1))
1289 (should (looking-back "\\[^1\\]: " nil)))))
1291 (ert-deftest test-markdown-footnote/kill-empty-text ()
1292 "Test killing a footnote with marker but no text."
1293 (markdown-test-string "no text[^1]\n\n[^1]: \n"
1294 (end-of-line)
1295 (markdown-footnote-goto-text)
1296 (should (looking-back "\\[^1\\]: " nil))
1297 (let (kill-ring)
1298 (markdown-footnote-kill))
1299 (should (string-equal (buffer-string) "no text\n"))))
1301 (ert-deftest test-markdown-footnote/kill-empty-after ()
1302 "Test killing an empty footnote after one with text (previously killed the
1303 footnote with text above)."
1304 (markdown-test-string "[^with-text][^no-text]\n\n[^with-text]: Text\n[^no-text]:"
1305 (let (kill-ring)
1306 (forward-line 3)
1307 (should (looking-at "\\[\\^no-text\\]:$"))
1308 (markdown-footnote-kill)
1309 (should (string-equal (current-kill 0) "")))))
1311 (ert-deftest test-markdown-footnote/kill-hanging-paras ()
1312 "Test killing a footnote where block text starts after the label (previously
1313 killed the footnote above)."
1314 (markdown-test-string "[^1][^2]\n\n[^1]: Foo\n\n[^2]:\n Text\n\n More text\n\n\nNot indented"
1315 (let (kill-ring)
1316 (forward-line 4)
1317 (should (looking-at "\\[\\^2\\]:$"))
1318 (markdown-footnote-kill)
1319 ;; We want to include the leading space on hanging footnote paragraphs,
1320 ;; even if a hanging paragraph is the first item in the footnote.
1321 (should (string-equal (current-kill 0) "Text\n\n More text\n")))))
1323 (ert-deftest test-markdown-footnote/text-positions-buffer-top ()
1324 "Test markdown-footnote-text-positions on footnote adjacent to buffer top
1325 (was infinite loop)."
1326 (markdown-test-string "[^label]: text\n more text"
1327 (should (equal (markdown-footnote-text-positions) (list "^label" 1 29)))))
1329 (ert-deftest test-markdown-footnote/text-positions-buffer-top-one-line ()
1330 "Test markdown-footnote-text-positions on one-line footnote adjacent to
1331 buffer top (failed to find positions)."
1332 (markdown-test-string "[^label]: text\n"
1333 (should (equal (markdown-footnote-text-positions) (list "^label" 1 16)))))
1335 (ert-deftest test-markdown-footnote/text-positions-buffer-top-not-footnote ()
1336 "Test markdown-footnote-text-positions on plain paragraph adjacent to buffer
1337 top (was infinite loop)."
1338 (markdown-test-string "text\n more text\n"
1339 (should (eq (markdown-footnote-text-positions) nil))))
1341 (ert-deftest test-markdown-footnote/text-positions-buffer-bottom ()
1342 "Test markdown-footnote-text-positions on footnote adjacent to buffer bottom
1343 (was infinite loop)."
1344 (markdown-test-string "\n[^label]: text\n more text"
1345 (forward-line 1)
1346 (should (equal (markdown-footnote-text-positions) (list "^label" 2 30)))))
1348 (ert-deftest test-markdown-footnote/kill-adjacent-footnote ()
1349 "Test killing a footnote adjacent to other one-line footnotes (previously
1350 killed the wrong one)."
1351 (markdown-test-string "Text[^1] with[^2] footnotes[^3]\n\n[^1]: foo\n[^2]: bar\n[^3]: baz"
1352 (let (kill-ring)
1353 (forward-line 3)
1354 (should (looking-at "\\[\\^2\\]: bar"))
1355 (markdown-footnote-kill)
1356 (should (string-equal (current-kill 0) "bar\n")))))
1358 (ert-deftest test-markdown-footnote/kill-adjacent-markers ()
1359 "Test killing a footnote where the labels are adjacent (previously, the wrong
1360 footnote would be killed because the attempt to jump to the marker would jump to
1361 the opening bracket of [^2], and then subsequent functions would kill [^2])."
1362 (markdown-test-string "Text with footnotes[^1][^2]\n\n[^1]: foo\n\n[^2]: bar\n"
1363 (let (kill-ring)
1364 (forward-line 2)
1365 (should (looking-at "\\[\\^1\\]: foo"))
1366 (markdown-footnote-kill)
1367 (should (string-equal (current-kill 0) "foo\n")))))
1369 (when (version< emacs-version "24.2")
1370 ;; fix segfault on 24.1 with the normal implementation of this function. isn't
1371 ;; exactly correct, but should make tests work the same
1372 (defadvice kill-buffer-and-window (around markdown-test-fix-segfault activate)
1373 (kill-buffer)
1374 (select-window (previous-window))))
1376 (ert-deftest test-markdown-footnote-reference/jump ()
1377 "Test `markdown-do' for footnotes and reference links."
1378 (markdown-test-string
1379 "body[^1], [link 1][ref],
1380 [link 2][ref]
1382 [^1]: footnote
1384 [ref]: https://duckduckgo.com/"
1385 (goto-char 5) ; start of [^1]
1386 (markdown-do) ; markdown-footnote-goto-text
1387 (should (looking-at "footnote"))
1388 (markdown-do) ; markdown-footnote-return
1389 (should (= (point) 9)) ; just after [^1]
1390 (markdown-next-link) ; beginning of [link 1][]
1391 (markdown-do)
1392 (should (looking-at "https://duckduckgo.com/"))
1393 (should (equal (markdown-reference-find-links "ref")
1394 (list (list "link 2" 26 2) (list "link 1" 11 1))))
1395 (markdown-do) ; opens a reference link buffer
1396 (should (string= (buffer-string) "Links using reference ref:\n\nlink 1 (line 1)\nlink 2 (line 2)\n"))
1397 (should (looking-at "link 1")) ; in reference link popop buffer
1398 (execute-kbd-macro (read-kbd-macro "RET")) ; jump to "link 1"
1399 (should (looking-at "\\[link 1\\]")) ; back in main buffer
1400 (should (= (point) 11))))
1402 ;;; Element removal tests:
1404 (ert-deftest test-markdown-kill/simple ()
1405 "Simple tests for `markdown-kill-thing-at-point'."
1406 (let ((kill-ring nil)
1407 (tests (list '("`foo`" . "foo")
1408 '("## foo ##" . "foo")
1409 '("## foo" . "foo")
1410 '("foo\n---" . "foo")
1411 '("foo\n===" . "foo")
1412 '("* * * * *" . "* * * * *")
1413 '("[foo](http://bar.com/)" . "foo")
1414 '("![foo](http://bar.com/)" . "foo")
1415 '("[foo][bar]" . "foo")
1416 '("![foo][bar]" . "foo")
1417 '("<http://foo.com/>" . "http://foo.com/")
1418 '("<foo@bar.com>" . "foo@bar.com")
1419 '("**foo**" . "foo")
1420 '("__foo__" . "foo")
1421 '("*foo*" . "foo")
1422 '("_foo_" . "foo")
1423 '(" [foo]: http://bar.com/" . "http://bar.com/")
1424 '(" [foo]: http://bar.com/ \"title\"" . "http://bar.com/")
1425 '("foo[^bar]\n\n[^bar]: baz" . "baz")
1426 '("[^bar]: baz" . "baz")
1427 '(" * foo\n bar" . " * foo\n bar"))))
1428 (dolist (test tests)
1429 ;; Load test string (the car), move to end of first line, kill
1430 ;; thing at point, and then verify that the kill ring contains cdr.
1431 (markdown-test-string (car test)
1432 (end-of-line)
1433 (call-interactively 'markdown-kill-thing-at-point)
1434 (should (string-equal (current-kill 0) (cdr test)))))))
1436 (ert-deftest test-markdown-kill/footnote-text ()
1437 "Test killing a footnote with point at footnote text."
1438 (markdown-test-string "some text[^1]\n\n[^1]: footnote\n"
1439 (end-of-line)
1440 (markdown-footnote-goto-text)
1441 (let (kill-ring)
1442 (markdown-footnote-kill))
1443 (should (string-equal (buffer-string) "some text\n"))))
1445 (ert-deftest test-markdown-kill/code ()
1446 "Test killing with code regex.."
1447 (let ((kill-ring nil))
1448 (markdown-test-string "Lorem `ipsum` dolor `sit` `amet`."
1449 (goto-char 22) ; position point at s in `sit`
1450 (call-interactively 'markdown-kill-thing-at-point)
1451 (should (string-equal (current-kill 0) "sit")))))
1453 ;;; Completion:
1455 (ert-deftest test-markdown-complete/atx-header-incomplete ()
1456 "Test `markdown-incomplete-atx-p'."
1457 (markdown-test-string "### ###"
1458 (should (looking-at markdown-regex-header-atx))
1459 (should-not (markdown-incomplete-atx-p)))
1460 (markdown-test-string "###abc###"
1461 (should-not (looking-at markdown-regex-header-atx)))
1462 (markdown-test-string "### ###"
1463 (should (looking-at markdown-regex-header-atx))
1464 (should (markdown-incomplete-atx-p))))
1466 (ert-deftest test-markdown-complete/atx-header ()
1467 "Test `markdown-complete' for atx headers."
1468 (markdown-test-string "##### test"
1469 (call-interactively 'markdown-complete)
1470 (should (string-equal (buffer-string) "##### test #####"))))
1472 (ert-deftest test-markdown-complete/setext-header-incomplete ()
1473 "Test `markdown-incomplete-setext-p'."
1474 (markdown-test-string "abc\n===\n"
1475 (should (looking-at markdown-regex-header-setext))
1476 (should-not (markdown-incomplete-setext-p)))
1477 (markdown-test-string "abc\n==\n"
1478 (should (looking-at markdown-regex-header-setext))
1479 (should (markdown-incomplete-setext-p)))
1480 (markdown-test-string "abc\n====\n"
1481 (should (looking-at markdown-regex-header-setext))
1482 (should (markdown-incomplete-setext-p))))
1484 (ert-deftest test-markdown-complete/setext-header ()
1485 "Test `markdown-complete' for setext headers."
1486 (markdown-test-string "test \n=="
1487 (call-interactively 'markdown-complete)
1488 (should (string-equal (buffer-string) "test\n===="))))
1490 (ert-deftest test-markdown-complete/hr-incomplete ()
1491 "Test `markdown-incomplete-hr-p'."
1492 (dolist (i (number-sequence 0 (1- (length markdown-hr-strings))))
1493 (markdown-test-string (nth i markdown-hr-strings)
1494 (should (looking-at markdown-regex-hr))
1495 (should-not (markdown-incomplete-hr-p))
1496 (should-error (call-interactively 'markdown-complete)))))
1498 (ert-deftest test-markdown-complete/hr ()
1499 "Test completion via `markdown-complete' for horizontal rules."
1500 (markdown-test-string "- - - - -"
1501 (call-interactively 'markdown-complete)
1502 (should (string-equal (buffer-string) (car markdown-hr-strings)))))
1504 (ert-deftest test-markdown-complete/buffer-setext-2 ()
1505 "Test `markdown-complete-buffer' for level two setext heading."
1506 ;; Ensure markdown-complete-buffer doesn't mistake this for a horizontal rule
1507 (markdown-test-string "Subheading\n--\n"
1508 (call-interactively 'markdown-complete-buffer)
1509 (should (string-equal (buffer-string) "Subheading\n----------\n\n")))
1510 (markdown-test-string "Abc\n--\n\nDef\n--\n"
1511 (call-interactively 'markdown-complete-buffer)
1512 (should (string-equal (buffer-string) "Abc\n---\n\nDef\n---\n\n"))))
1514 ;;; Promotion and demotion tests:
1516 (ert-deftest test-markdown-promote/atx-header ()
1517 "Test `markdown-promote' for atx headers."
1518 (markdown-test-string "###### test ######"
1519 (markdown-promote)
1520 (should (string-equal (buffer-string) "##### test #####"))
1521 (markdown-promote)
1522 (should (string-equal (buffer-string) "#### test ####"))
1523 (markdown-promote)
1524 (should (string-equal (buffer-string) "### test ###"))
1525 (markdown-promote)
1526 (should (string-equal (buffer-string) "## test ##"))
1527 (markdown-promote)
1528 (should (string-equal (buffer-string) "# test #"))))
1530 (ert-deftest test-markdown-demote/atx-header ()
1531 "Test `markdown-demote' for atx headers."
1532 (markdown-test-string "# test #"
1533 (markdown-demote)
1534 (should (string-equal (buffer-string) "## test ##"))
1535 (markdown-demote)
1536 (should (string-equal (buffer-string) "### test ###"))
1537 (markdown-demote)
1538 (should (string-equal (buffer-string) "#### test ####"))
1539 (markdown-demote)
1540 (should (string-equal (buffer-string) "##### test #####"))
1541 (markdown-demote)
1542 (should (string-equal (buffer-string) "###### test ######"))))
1544 (ert-deftest test-markdown-promote/setext-header ()
1545 "Test `markdown-promote' for setext headers."
1546 (markdown-test-string "test\n----"
1547 (markdown-promote)
1548 (should (string-equal (buffer-string) "test\n===="))))
1550 (ert-deftest test-markdown-demote/setext-header ()
1551 "Test `markdown-demote' for setext headers."
1552 (markdown-test-string "test\n===="
1553 (markdown-demote)
1554 (should (string-equal (buffer-string) "test\n----"))
1555 (markdown-demote)
1556 (should (string-equal (buffer-string) "### test ###"))
1557 (markdown-demote)
1558 (should (string-equal (buffer-string) "#### test ####"))
1559 (markdown-demote)
1560 (should (string-equal (buffer-string) "##### test #####"))
1561 (markdown-demote)
1562 (should (string-equal (buffer-string) "###### test ######"))))
1564 (ert-deftest test-markdown-promote/hr ()
1565 "Test `markdown-promote' for horizontal rules."
1566 (markdown-test-string (car (reverse markdown-hr-strings))
1567 (dolist (n (number-sequence 4 0 -1))
1568 (markdown-promote)
1569 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))
1571 (ert-deftest test-markdown-demote/hr ()
1572 "Test `markdown-demote' for horizontal rules."
1573 (markdown-test-string (car markdown-hr-strings)
1574 (dolist (n (number-sequence 1 5))
1575 (markdown-demote)
1576 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))
1578 (ert-deftest test-markdown-promote/bold ()
1579 "Test `markdown-promote' for bold markup."
1580 (markdown-test-string "__bold__"
1581 (call-interactively 'markdown-promote)
1582 (should (string-equal (buffer-string) "**bold**"))))
1584 (ert-deftest test-markdown-demote/bold ()
1585 "Test `markdown-demote' for bold markup."
1586 (markdown-test-string "**bold**"
1587 (call-interactively 'markdown-promote)
1588 (should (string-equal (buffer-string) "__bold__"))))
1590 (ert-deftest test-markdown-promote/italic ()
1591 "Test `markdown-promote' for italic markup."
1592 (markdown-test-string "_italic_"
1593 (call-interactively 'markdown-promote)
1594 (should (string-equal (buffer-string) "*italic*"))))
1596 (ert-deftest test-markdown-demote/italic ()
1597 "Test `markdown-demote' for italic markup."
1598 (markdown-test-string "*italic*"
1599 (call-interactively 'markdown-promote)
1600 (should (string-equal (buffer-string) "_italic_"))))
1602 ;;; Subtree editing tests:
1604 (ert-deftest test-markdown-subtree/promote ()
1605 "Test `markdown-promote-subtree'."
1606 (markdown-test-string "# h1 #\n\n## h2 ##\n\n### h3 ###\n\n## h2 ##\n\n# h1 #\n"
1607 ;; The first h1 should get promoted away.
1608 ;; The second h1 should not be promoted.
1609 (markdown-promote-subtree)
1610 (should (string-equal (buffer-string) "h1\n\n# h2 #\n\n## h3 ##\n\n# h2 #\n\n# h1 #\n"))
1611 ;; Second call should do nothing since point is no longer at a heading.
1612 (markdown-promote-subtree)
1613 (should (string-equal (buffer-string) "h1\n\n# h2 #\n\n## h3 ##\n\n# h2 #\n\n# h1 #\n"))
1614 ;; Move to h2 and promote again.
1615 (forward-line 2)
1616 (markdown-promote-subtree)
1617 (should (string-equal (buffer-string) "h1\n\nh2\n\n# h3 #\n\n# h2 #\n\n# h1 #\n"))))
1619 (ert-deftest test-markdown-subtree/promote-single-section ()
1620 "Test `markdown-promote-subtree' on a single or last section.
1621 Should not cause an infinite loop."
1622 (markdown-test-string "foo\n\n## h2 ##\n\nbar\n"
1623 ;; The h2 should get promoted to h1 away.
1624 (markdown-test-goto-heading "h2")
1625 (markdown-promote-subtree)
1626 (should (string-equal (buffer-string) "foo\n\n# h2 #\n\nbar\n"))))
1628 (ert-deftest test-markdown-subtree/demote ()
1629 "Test `markdown-demote-subtree'."
1630 (markdown-test-string "# h1 #\n\n## h2 ##\n\n### h3 ###\n\n## h2 ##\n\n# h1 #\n"
1631 ;; The second h1 should not be demoted
1632 (markdown-demote-subtree)
1633 (should (string-equal (buffer-string) "## h1 ##\n\n### h2 ###\n\n#### h3 ####\n\n### h2 ###\n\n# h1 #\n"))
1634 (markdown-demote-subtree)
1635 (should (string-equal (buffer-string) "### h1 ###\n\n#### h2 ####\n\n##### h3 #####\n\n#### h2 ####\n\n# h1 #\n"))
1636 (markdown-demote-subtree)
1637 (should (string-equal (buffer-string) "#### h1 ####\n\n##### h2 #####\n\n###### h3 ######\n\n##### h2 #####\n\n# h1 #\n"))
1638 ;; Stop demoting at level six
1639 (markdown-demote-subtree)
1640 (should (string-equal (buffer-string) "##### h1 #####\n\n###### h2 ######\n\n###### h3 ######\n\n###### h2 ######\n\n# h1 #\n"))
1641 (markdown-demote-subtree)
1642 (should (string-equal (buffer-string) "###### h1 ######\n\n###### h2 ######\n\n###### h3 ######\n\n###### h2 ######\n\n# h1 #\n"))))
1644 (ert-deftest test-markdown-subtree/move-up ()
1645 "Test `markdown-move-subtree-up'."
1646 ;; Note that prior to Emacs 24.5, this does not work for the last subtree in
1647 ;; the buffer due to Emacs bug #19102:
1648 ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19102
1649 ;; https://github.com/emacs-mirror/emacs/commit/b3910f
1650 ;; That also corrects the type of the "Cannot move pase superior level" error
1651 ;; from 'error to 'user-error.
1652 (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"
1653 (re-search-forward "^# 2")
1654 (markdown-move-subtree-up)
1655 (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"))
1656 ;; Second attempt should fail, leaving buffer unchanged.
1657 ;; (This way of asserting the contents of the error
1658 ;; message is a bit convoluted and more fragile than
1659 ;; ideal. But prior to Emacs 24.5, the type of this
1660 ;; error is just 'error, and a bare "should-error" is
1661 ;; really overly broad.)
1662 (should (string-equal
1663 "Cannot move past superior level"
1664 (cl-second (should-error (markdown-move-subtree-up)))))))
1666 (ert-deftest test-markdown-subtree/move-down ()
1667 "Test `markdown-move-subtree-down'."
1668 (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"
1669 (re-search-forward "^## 1\.1")
1670 (markdown-move-subtree-down)
1671 (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"))))
1673 (ert-deftest test-markdown-subtree/mark ()
1674 "Test `markdown-mark-subtree'."
1675 (markdown-test-file "outline.text"
1676 (markdown-next-visible-heading 1)
1677 (should-not mark-active)
1678 (markdown-mark-subtree)
1679 (should (= (point) 19))
1680 (should (= (mark) 349))
1681 (should mark-active)
1682 (deactivate-mark)
1683 (should-not mark-active)
1684 (markdown-forward-same-level 1)
1685 (markdown-mark-subtree)
1686 (should (= (point) 351))
1687 (should (= (mark) 515))
1688 (should mark-active)))
1690 (ert-deftest test-markdown-subtree/narrow ()
1691 "Test `markdown-narrow-to-subtree'."
1692 (markdown-test-file "outline.text"
1693 (markdown-next-visible-heading 1)
1694 (markdown-forward-same-level 1)
1695 (widen)
1696 (should (= (point-min) 1))
1697 (should (= (point-max) 553))
1698 (markdown-narrow-to-subtree)
1699 (should (= (point-min) 351))
1700 (should (= (point-max) 515))))
1702 ;;; Cycling:
1704 (ert-deftest test-markdown-cycle/atx-header ()
1705 "Test `markdown-demote' cycling for atx headers."
1706 (markdown-test-string "# test"
1707 (call-interactively 'markdown-demote)
1708 (should (string-equal (buffer-string) "## test ##"))
1709 (call-interactively 'markdown-demote)
1710 (should (string-equal (buffer-string) "### test ###"))
1711 (call-interactively 'markdown-demote)
1712 (should (string-equal (buffer-string) "#### test ####"))
1713 (call-interactively 'markdown-demote)
1714 (should (string-equal (buffer-string) "##### test #####"))
1715 (call-interactively 'markdown-demote)
1716 (should (string-equal (buffer-string) "###### test ######"))
1717 (call-interactively 'markdown-demote)
1718 (should (string-equal (buffer-string) "###### test ######"))))
1720 (ert-deftest test-markdown-cycle/setext-header ()
1721 "Test `markdown-demote' cycling for setext headers."
1722 (markdown-test-string "test\n===="
1723 (call-interactively 'markdown-demote)
1724 (should (string-equal (buffer-string) "test\n----"))
1725 (call-interactively 'markdown-demote)
1726 (should (string-equal (buffer-string) "### test ###"))
1727 (call-interactively 'markdown-demote)
1728 (should (string-equal (buffer-string) "#### test ####"))
1729 (call-interactively 'markdown-demote)
1730 (should (string-equal (buffer-string) "##### test #####"))
1731 (call-interactively 'markdown-demote)
1732 (should (string-equal (buffer-string) "###### test ######"))
1733 (call-interactively 'markdown-demote)
1734 (should (string-equal (buffer-string) "###### test ######"))))
1736 (ert-deftest test-markdown-cycle/hr ()
1737 "Test cycling of horizontal rules."
1738 ;; Cycle using markdown-demote
1739 (markdown-test-string (car markdown-hr-strings)
1740 (dolist (n (number-sequence 1 5))
1741 (call-interactively 'markdown-demote)
1742 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
1743 (call-interactively 'markdown-demote)
1744 (should (string-equal (buffer-string) (car markdown-hr-strings))))
1745 ;; Cycle using markdown-promote
1746 (markdown-test-string (car (reverse markdown-hr-strings))
1747 (dolist (n (number-sequence 4 0 -1))
1748 (call-interactively 'markdown-promote)
1749 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
1750 (call-interactively 'markdown-promote)
1751 (should (string-equal (buffer-string) (car (reverse markdown-hr-strings))))))
1753 (ert-deftest test-markdown-cycle/bold ()
1754 "Test cycling of bold markup."
1755 (markdown-test-string "**bold**"
1756 (call-interactively 'markdown-demote)
1757 (should (string-equal (buffer-string) "__bold__"))
1758 (call-interactively 'markdown-demote)
1759 (should (string-equal (buffer-string) "**bold**"))))
1761 (ert-deftest test-markdown-cycle/italic ()
1762 "Test cycling of italic markup."
1763 (markdown-test-string "*italic*"
1764 (call-interactively 'markdown-demote)
1765 (should (string-equal (buffer-string) "_italic_"))
1766 (call-interactively 'markdown-demote)
1767 (should (string-equal (buffer-string) "*italic*"))))
1769 ;;; Indentation tests:
1771 (ert-deftest test-markdown-indentation/calc-indents ()
1772 "Test `markdown-calc-indents' a nested list context."
1773 (markdown-test-file "nested-list.text"
1774 (goto-char (point-max))
1775 (let ((indents (markdown-calc-indents)))
1776 (should (= (car indents) 17)) ; indentation of previous line first
1777 (should (equal (sort indents '<)
1778 (list
1779 0 ; beginning of line
1780 3 ; first-level list marker
1781 7 ; second-level list marker
1782 11 ; third-level list marker
1783 13 ; previous list item text
1784 16 ; pre-block indentation
1785 17 ; indentation of previous line
1786 21 ; previous line plus tab-width
1787 ))))))
1789 (ert-deftest test-markdown-indentation/indent-region ()
1790 "Test `markdown-indent-region'."
1791 ;; Basic test with multiple lines
1792 (markdown-test-string "abc\ndef\nghi\n"
1793 (markdown-indent-region (point-min) (point-max) nil)
1794 (should (string-equal (buffer-string) " abc\n def\n ghi\n")))
1795 ;; Following a list item
1796 (markdown-test-string " * abc\ndef\n"
1797 (forward-line)
1798 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
1799 (should (string-equal (buffer-string) " * abc\n def\n"))
1800 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
1801 (should (string-equal (buffer-string) " * abc\n def\n"))))
1803 (ert-deftest test-markdown-indentation/indent-list-hanging ()
1804 "Test `markdown-indent-line' with hanging list item."
1805 (markdown-test-string
1806 "- list
1807 - nested list with long lines which need to be
1808 hard wrapped"
1809 (goto-char (point-max))
1810 (markdown-enter-key)
1811 (should (eq (point) 78))))
1813 (ert-deftest test-markdown-indentation/indent-list-single ()
1814 "Test `markdown-indent-line' with single list item."
1815 (let ((markdown-indent-on-enter 'indent-and-new-item))
1816 (markdown-test-string " * item 1"
1817 (end-of-line)
1818 (call-interactively #'markdown-enter-key)
1819 (should (string-equal (buffer-string) " * item 1\n * "))
1820 (should (eq (point) 16))
1821 (call-interactively #'markdown-enter-key)
1822 (should (string-equal (buffer-string) " * item 1\n\n"))
1823 (should (eq (point) 13)))))
1825 (ert-deftest test-markdown-indentation/indent-nested-list ()
1826 "Test `markdown-enter-key' with a nested list item."
1827 (let ((markdown-indent-on-enter 'indent-and-new-item))
1828 (markdown-test-string "* foo\n* bar\n * baz"
1829 (goto-char (point-max))
1830 (call-interactively #'markdown-enter-key)
1831 (should (string-equal (buffer-string) "* foo\n* bar\n * baz\n * "))
1832 (should (eq (point) 25))
1833 (call-interactively #'markdown-enter-key)
1834 (should (string-equal (buffer-string) "* foo\n* bar\n * baz\n\n"))
1835 (should (eq (point) 22)))))
1837 (ert-deftest test-markdown-indentation/indent-pre ()
1838 "Test `markdown-indent-line' with a pre block."
1839 (markdown-test-string
1840 "I'm gonna write a code block:
1842 my first line of code"
1843 (goto-char (point-max))
1844 (markdown-enter-key)
1845 (should (eq (point) 62))
1846 (should (looking-back "^ "))))
1848 (ert-deftest test-markdown-indentation/continue-gfm-task-lists ()
1849 (markdown-test-string " - [X] item"
1850 (end-of-line)
1851 (let ((markdown-indent-on-enter 'indent-and-new-item))
1852 (call-interactively #'markdown-enter-key))
1853 (should (string-equal (buffer-string) " - [X] item\n - [ ] "))
1854 (should (= (point) 28))))
1856 ;;; Markup hiding tests:
1858 (ert-deftest test-markdown-markup-hiding/italics-1 ()
1859 "Test hiding markup for italics."
1860 (markdown-test-file "inline.text"
1861 (goto-char 9)
1862 (should (looking-at "\*italic\*"))
1863 (markdown-test-range-has-property (point) (point) 'invisible 'markdown-markup)
1864 (should-not (invisible-p (point)))
1865 (should-not (invisible-p (+ 1 (point))))
1866 (markdown-toggle-markup-hiding t)
1867 (should (invisible-p (point)))
1868 (should-not (invisible-p (+ 1 (point))))))
1870 (ert-deftest test-markdown-markup-hiding/bold-1 ()
1871 "Test hiding markup for bold."
1872 (markdown-test-file "inline.text"
1873 (goto-char 27)
1874 (should (looking-at "\*\*bold\*\*"))
1875 (markdown-test-range-has-property (point) (1+ (point)) 'invisible 'markdown-markup)
1876 (should-not (invisible-p (point)))
1877 (should-not (invisible-p (+ 1 (point))))
1878 (should-not (invisible-p (+ 2 (point))))
1879 (markdown-toggle-markup-hiding t)
1880 (should (invisible-p (point)))
1881 (should (invisible-p (+ 1 (point))))
1882 (should-not (invisible-p (+ 2 (point))))))
1884 (ert-deftest test-markdown-markup-hiding/code-1 ()
1885 "Test hiding markup for inline code."
1886 (markdown-test-file "inline.text"
1887 (goto-char 45)
1888 (should (looking-at "`code`"))
1889 (markdown-test-range-has-property (point) (point) 'invisible 'markdown-markup)
1890 (should-not (invisible-p (point)))
1891 (should-not (invisible-p (1+ (point))))
1892 (markdown-toggle-markup-hiding t)
1893 (should (invisible-p (point)))
1894 (should-not (invisible-p (1+ (point))))))
1896 (ert-deftest test-markdown-markup-hiding/kbd-1 ()
1897 "Test hiding markup for <kbd> tags."
1898 (markdown-test-string "<kbd>C-c C-x C-m</kbd>"
1899 (markdown-test-range-has-property (point) (+ 4 (point)) 'invisible 'markdown-markup)
1900 (should-not (invisible-p (point))) ;; part of <kbd>
1901 (should-not (invisible-p (+ 4 (point)))) ;; part of <kbd>
1902 (should-not (invisible-p (+ 5 (point)))) ;; inside <kbd>
1903 (markdown-toggle-markup-hiding t)
1904 (should (invisible-p (point))) ;; part of <kbd>
1905 (should (invisible-p (+ 4 (point)))) ;; part of <kbd>
1906 (should-not (invisible-p (+ 5 (point)))))) ;; inside <kbd>
1908 (ert-deftest test-markdown-markup-hiding/inline-links ()
1909 "Test hiding markup for inline links."
1910 (markdown-test-file "inline.text"
1911 (goto-char 925)
1912 (should (looking-at "\\[text\\](http://www.w3.org/ \"title\")"))
1913 (markdown-test-range-has-property 925 925 'invisible 'markdown-markup) ; [
1914 (markdown-test-range-has-property 930 958 'invisible 'markdown-markup) ; ](...)
1915 (should-not (invisible-p 925))
1916 (should-not (invisible-p 958))
1917 (markdown-toggle-markup-hiding t)
1918 (should (invisible-p 925))
1919 (should-not (invisible-p 926))
1920 (should (invisible-p 958))))
1922 (ert-deftest test-markdown-markup-hiding/reference-links ()
1923 "Test hiding markup for reference links."
1924 (markdown-test-string "[text][ref]"
1925 (markdown-test-range-has-property 1 1 'invisible 'markdown-markup) ; [
1926 (markdown-test-range-has-property 6 11 'invisible 'markdown-markup) ; ][ref]
1927 (should-not (invisible-p 1))
1928 (should-not (invisible-p 6))
1929 (markdown-toggle-markup-hiding t)
1930 (should (invisible-p 1))
1931 (should-not (invisible-p 2))
1932 (should (invisible-p 6))))
1934 (ert-deftest test-markdown-markup-hiding/angle-urls ()
1935 "Test hiding markup for angle urls."
1936 (markdown-test-string "<http://jblevins.org/projects/markdown-mode/>"
1937 (markdown-test-range-has-property 1 1 'invisible 'markdown-markup) ; <
1938 (markdown-test-range-has-property 45 45 'invisible 'markdown-markup) ; >
1939 (should-not (invisible-p 1))
1940 (should-not (invisible-p 2))
1941 (should-not (invisible-p 45))
1942 (markdown-toggle-markup-hiding t)
1943 (should (invisible-p 1))
1944 (should-not (invisible-p 2))
1945 (should (invisible-p 45))))
1947 (ert-deftest test-markdown-markup-hiding/list-items ()
1948 "Test hiding markup for list items."
1949 (let ((markdown-hide-markup t))
1950 (markdown-test-file "nested-list.text"
1951 (markdown-test-range-has-property 4 4 'display (nth 0 markdown-list-item-bullets))
1952 (markdown-test-range-has-property 194 194 'display (nth 0 markdown-list-item-bullets))
1953 (markdown-test-range-has-property 224 224 'display (nth 1 markdown-list-item-bullets))
1954 (markdown-test-range-has-property 525 525 'display (nth 2 markdown-list-item-bullets)))))
1956 (ert-deftest test-markdown-markup-hiding/gfm-code-blocks ()
1957 "Test hiding markup for GFM code blocks."
1958 (let ((markdown-hide-markup t))
1959 (markdown-test-file "GFM.md"
1960 (markdown-test-range-has-property 1548 1552 'invisible 'markdown-markup)
1961 (should (invisible-p 1548))
1962 (should (invisible-p 1552))
1963 (markdown-test-range-has-property 1607 1609 'invisible 'markdown-markup)
1964 (should (invisible-p 1607))
1965 (should (invisible-p 1609)))))
1967 (ert-deftest test-markdown-markup-hiding/fenced-code-blocks ()
1968 "Test hiding markup for tilde fenced code blocks."
1969 (let ((markdown-hide-markup t))
1970 (markdown-test-file "outline-code.text"
1971 (markdown-test-range-has-property 83 93 'invisible 'markdown-markup)
1972 (should (invisible-p 83))
1973 (should (invisible-p 93))
1974 (markdown-test-range-has-property 154 156 'invisible 'markdown-markup)
1975 (should (invisible-p 154))
1976 (should (invisible-p 156)))))
1978 ;;; Font lock tests:
1980 (ert-deftest test-markdown-font-lock/italics-1 ()
1981 "A simple italics test."
1982 (markdown-test-file "inline.text"
1983 (goto-char 9)
1984 (should (looking-at "\*"))
1985 ;; Check face of char before leading asterisk
1986 (markdown-test-range-has-face 8 8 nil)
1987 ;; Check face of italic range
1988 (markdown-test-range-has-face 9 9 markdown-markup-face)
1989 (markdown-test-range-has-face 10 16 markdown-italic-face)
1990 (markdown-test-range-has-face 17 17 markdown-markup-face)
1991 ;; Check face of point past leading asterisk
1992 (markdown-test-range-has-face 18 18 nil)))
1994 (ert-deftest test-markdown-font-lock/italics-2 ()
1995 "Test space after leading asterisk or underscore."
1996 (markdown-test-string
1997 "This is * not italic*, nor _ is this_."
1998 (markdown-test-range-has-face (point-min) (point-max) nil)))
2000 (ert-deftest test-markdown-font-lock/italics-3 ()
2001 "Test that slash inside asterisks is not italic."
2002 (markdown-test-string
2003 "not italic *\\*"
2004 (markdown-test-range-has-face (point-min) (point-max) nil)))
2006 (ert-deftest test-markdown-font-lock/italics-4 ()
2007 "Test that escaped asterisk inside italics is not bold."
2008 (markdown-test-string
2009 "italic **\\**"
2010 (markdown-test-range-has-face 1 7 nil)
2011 (markdown-test-range-has-face 8 8 markdown-markup-face)
2012 (markdown-test-range-has-face 9 11 markdown-italic-face)
2013 (markdown-test-range-has-face 12 12 markdown-markup-face)))
2015 (ert-deftest test-markdown-font-lock/italics-5 ()
2016 "Test italic single letter."
2017 (markdown-test-string
2018 "*a*"
2019 (markdown-test-range-has-face 1 1 markdown-markup-face)
2020 (markdown-test-range-has-face 2 2 markdown-italic-face)
2021 (markdown-test-range-has-face 3 3 markdown-markup-face)))
2023 (ert-deftest test-markdown-font-lock/italics-6 ()
2024 "Test multiline italics across list items."
2025 (markdown-test-string
2026 "* something about function foo_bar
2027 * something else about foo_bar"
2028 (markdown-test-range-has-face 31 34 nil)
2029 (markdown-test-range-has-face 38 62 nil)))
2031 (ert-deftest test-markdown-font-lock/italics-8 ()
2032 "Test multiline italics across list items."
2033 (markdown-test-string
2034 "* something about function
2035 foo_bar
2036 * something else about
2037 foo_bar"
2038 (markdown-test-range-has-face 30 36 nil)
2039 (markdown-test-range-has-face 63 69 nil)))
2041 (ert-deftest test-markdown-font-lock/italics-9 ()
2042 "Test multiline italics across list items."
2043 (markdown-test-string
2044 "foo_bar
2045 * foo_bar"
2046 (markdown-test-range-has-face 4 7 nil)
2047 (markdown-test-range-has-face 11 14 nil)))
2049 (ert-deftest test-markdown-font-lock/italics-10 ()
2050 "Underscores in URLs should not trigger italics."
2051 (markdown-test-string
2052 "<http://jblevins.org/research/centroid/cd_z_path.m>"
2053 (markdown-test-range-face-equals 2 50 'markdown-plain-url-face)
2054 (should-not (markdown-range-property-any 43 43 'face '(markdown-italic-face)))))
2056 (ert-deftest test-markdown-font-lock/italics-11 ()
2057 "Underscores in URLs should not trigger italics."
2058 (markdown-test-string
2059 "[1]: http://jblevins.org/research/centroid/cd_z_path.m"
2060 (markdown-test-range-face-equals 6 54 markdown-url-face)))
2062 (ert-deftest test-markdown-font-lock/italics-12 ()
2063 "Underscores in URLs should not trigger italics."
2064 (markdown-test-string
2065 "[cd\\_z\\_path.m](http://jblevins.org/research/centroid/cd_z_path.m)"
2066 (markdown-test-range-face-equals 17 65 markdown-url-face)))
2068 (ert-deftest test-markdown-font-lock/italics-after-hr ()
2069 "Test italics after a horizontal rule with asterisks."
2070 (markdown-test-string "* * *\n\n*italic*\n"
2071 (markdown-test-range-has-face 1 5 'markdown-hr-face)
2072 (markdown-test-range-has-face 8 8 markdown-markup-face)
2073 (markdown-test-range-has-face 9 14 markdown-italic-face)
2074 (markdown-test-range-has-face 15 15 markdown-markup-face)))
2076 (ert-deftest test-markdown-font-lock/italics-in-heading ()
2077 "Test italic overlay in a heading."
2078 (markdown-test-string
2079 "# *Italics* in a Heading"
2080 (markdown-test-range-has-face 3 3 markdown-markup-face)
2081 (markdown-test-range-has-face 4 10 markdown-italic-face)
2082 (markdown-test-range-has-face 11 11 markdown-markup-face)))
2084 (ert-deftest test-markdown-font-lock/italics-link ()
2085 "Test italic overlay in an inline link."
2086 (markdown-test-string
2087 "*[italic link](http://www.link.com/)*"
2088 (markdown-test-range-has-face 1 1 markdown-markup-face)
2089 (markdown-test-range-has-face 2 36 markdown-italic-face)
2090 (markdown-test-range-has-face 37 37 markdown-markup-face))
2091 (markdown-test-string
2092 "[*italic link*](http://www.link.com/)"
2093 (markdown-test-range-has-face 2 2 markdown-markup-face)
2094 (markdown-test-range-has-face 3 13 markdown-italic-face)
2095 (markdown-test-range-has-face 14 14 markdown-markup-face)))
2097 (ert-deftest test-markdown-font-lock/italics-in-blockquote ()
2098 "Test italics overlay in a blockquote."
2099 (markdown-test-string
2100 "> *italics* inside a blockquote"
2101 (markdown-test-range-has-face 3 3 markdown-markup-face)
2102 (markdown-test-range-has-face 4 10 markdown-italic-face)
2103 (markdown-test-range-has-face 11 11 markdown-markup-face)))
2105 (ert-deftest test-markdown-font-lock/italics-in-pre ()
2106 "Test italics overlay in a blockquote."
2107 (markdown-test-string
2108 " *italics* inside a pre block"
2109 (markdown-test-range-has-face (point-min) (1- (point-max))
2110 markdown-pre-face)))
2112 (ert-deftest test-markdown-font-lock/italics-and-code ()
2113 "Test seeming italics mixed with code."
2114 (markdown-test-string
2115 "define `var_1` and `var_2` inline code"
2116 (markdown-test-range-has-face 9 13 markdown-inline-code-face)
2117 (markdown-test-range-has-face 21 25 markdown-inline-code-face))
2118 (markdown-test-string
2119 "`var_1` and var_2"
2120 (markdown-test-range-has-face 2 6 markdown-inline-code-face)
2121 (markdown-test-range-has-face 8 17 nil))
2122 (markdown-test-string
2123 "var_1 and `var_2`"
2124 (markdown-test-range-has-face 1 10 nil)
2125 (markdown-test-range-has-face 12 16 markdown-inline-code-face)))
2127 (ert-deftest test-markdown-font-lock/italics-in-reference-definitions ()
2128 "Test not matching italics in reference definitions across lines."
2129 (markdown-test-string
2130 "[lg]: twilight_sm.png\n[sm]: twilight_lg.png"
2131 (markdown-test-range-has-face 7 21 markdown-url-face)
2132 (markdown-test-range-has-face 22 22 nil)
2133 (markdown-test-range-has-face 29 43 markdown-url-face)
2134 (markdown-test-range-has-face 28 28 nil)))
2136 (ert-deftest test-markdown-font-lock/italics-in-comment ()
2137 "Test not matching italics in comments."
2138 (markdown-test-string
2139 "<!-- -*- coding: utf-8 -*- -->"
2140 (markdown-test-range-has-face 1 30 'markdown-comment-face)
2141 (should-not (markdown-range-property-any 1 30 'face '(markdown-italic-face)))))
2143 (ert-deftest test-markdown-font-lock/bold-1 ()
2144 "A simple bold test."
2145 (markdown-test-file "inline.text"
2146 (goto-char 27)
2147 (should (looking-at "\*\*"))
2148 ;; Check face of char before leading asterisk
2149 (markdown-test-range-has-face 26 26 nil)
2150 ;; Check face of opening asterisks
2151 (markdown-test-range-has-face 27 28 markdown-markup-face)
2152 ;; Check face of bold range
2153 (markdown-test-range-has-face 29 33 markdown-bold-face)
2154 ;; Check face of closing asterisks
2155 (markdown-test-range-has-face 34 35 markdown-markup-face)
2156 ;; Check face of point past leading asterisk
2157 (markdown-test-range-has-face 36 36 nil)))
2159 (ert-deftest test-markdown-font-lock/bold-2 ()
2160 "Test space after leading asterisks or underscores."
2161 (markdown-test-string
2162 "This is ** not bold**, nor __ is this__ (but they match italics)."
2163 (markdown-test-range-has-face 1 8 nil)
2164 (markdown-test-range-has-face 9 9 markdown-markup-face)
2165 (markdown-test-range-has-face 10 19 markdown-italic-face)
2166 (markdown-test-range-has-face 20 20 markdown-markup-face)
2167 (markdown-test-range-has-face 21 27 nil)
2168 (markdown-test-range-has-face 28 28 markdown-markup-face)
2169 (markdown-test-range-has-face 29 37 markdown-italic-face)
2170 (markdown-test-range-has-face 38 38 markdown-markup-face)
2171 (markdown-test-range-has-face 39 (point-max) nil)))
2173 (ert-deftest test-markdown-font-lock/bold-3 ()
2174 "Test escaped asterisk inside bold."
2175 (markdown-test-string
2176 "bold **\\***"
2177 (markdown-test-range-has-face 1 5 nil)
2178 (markdown-test-range-has-face 6 7 markdown-markup-face)
2179 (markdown-test-range-has-face 8 9 markdown-bold-face)
2180 (markdown-test-range-has-face 10 11 markdown-markup-face)))
2182 (ert-deftest test-markdown-font-lock/bold-4 ()
2183 "Test bold single letter."
2184 (markdown-test-string
2185 "**a**"
2186 (markdown-test-range-has-face 1 2 markdown-markup-face)
2187 (markdown-test-range-has-face 3 3 markdown-bold-face)
2188 (markdown-test-range-has-face 4 5 markdown-markup-face)))
2190 (ert-deftest test-markdown-font-lock/bold-after-hr ()
2191 "Test bold after a horizontal rule with asterisks."
2192 (markdown-test-string "* * *\n\n**bold**\n"
2193 (markdown-test-range-has-face 1 5 'markdown-hr-face)
2194 (markdown-test-range-has-face 8 9 markdown-markup-face)
2195 (markdown-test-range-has-face 10 13 markdown-bold-face)
2196 (markdown-test-range-has-face 14 15 markdown-markup-face)))
2198 (ert-deftest test-markdown-font-lock/bold-link ()
2199 "Test bold overlay in an inline link."
2200 (markdown-test-string
2201 "**[bold link](http://www.link.com/)**"
2202 (markdown-test-range-has-face 1 2 markdown-markup-face)
2203 (markdown-test-range-has-face 3 35 markdown-bold-face)
2204 (markdown-test-range-has-face 36 37 markdown-markup-face))
2205 (markdown-test-string
2206 "[**bold link**](http://www.link.com/)"
2207 (markdown-test-range-has-face 2 3 markdown-markup-face)
2208 (markdown-test-range-has-face 4 12 markdown-bold-face)
2209 (markdown-test-range-has-face 13 14 markdown-markup-face)))
2211 (ert-deftest test-markdown-font-lock/bold-in-blockquote ()
2212 "Test bold overlay in a blockquote."
2213 (markdown-test-string
2214 "> **bold** inside a blockquote"
2215 (markdown-test-range-has-face 3 4 markdown-markup-face)
2216 (markdown-test-range-has-face 5 8 markdown-bold-face)
2217 (markdown-test-range-has-face 9 10 markdown-markup-face)))
2219 (ert-deftest test-markdown-font-lock/bold-in-pre ()
2220 "Test bold overlay in a blockquote."
2221 (markdown-test-string
2222 " **bold** inside a pre block"
2223 (markdown-test-range-has-face (point-min) (1- (point-max))
2224 markdown-pre-face)))
2226 (ert-deftest test-markdown-font-lock/no-bold-in-code ()
2227 "Bold markers in inline code should not trigger bold."
2228 (markdown-test-string
2229 "`def __init__(self):`"
2230 (markdown-test-range-has-face 8 11 'markdown-inline-code-face)
2231 (should-not (markdown-range-property-any
2232 (point-min) (point-max) 'face '(markdown-bold-face))))
2233 (markdown-test-string
2234 "`**foo` bar `baz**`"
2235 (markdown-test-range-has-face 2 6 'markdown-inline-code-face)
2236 (markdown-test-range-face-equals 9 11 nil)
2237 (markdown-test-range-has-face 14 18 'markdown-inline-code-face)
2238 (should-not (markdown-range-property-any
2239 (point-min) (point-max) 'face '(markdown-bold-face)))))
2241 (ert-deftest test-markdown-font-lock/bold-in-comment ()
2242 "Test not matching bold in comments."
2243 (markdown-test-string
2244 "<!-- **not bold** -->"
2245 (markdown-test-range-has-face 1 21 'markdown-comment-face)
2246 (should-not
2247 (markdown-range-property-any 1 21 'face '(markdown-bold-face)))))
2249 (ert-deftest test-markdown-font-lock/no-bold-in-url ()
2250 "Test not matching bold in plain URL links."
2251 (markdown-test-string
2252 "<https://example.com/__not-bold__>"
2253 (should-not (markdown-range-property-any 23 30 'face '(markdown-bold-face)))))
2255 (ert-deftest test-markdown-font-lock/code-1 ()
2256 "A simple inline code test."
2257 (markdown-test-file "inline.text"
2258 (goto-char 45)
2259 (should (looking-at "`"))
2260 ;; Regular code span
2261 (markdown-test-range-has-face 45 45 markdown-markup-face)
2262 (markdown-test-range-has-face 46 49 markdown-inline-code-face)
2263 (markdown-test-range-has-face 50 50 markdown-markup-face)
2264 ;; Code containing backticks
2265 (markdown-test-range-has-face 61 62 markdown-markup-face)
2266 (markdown-test-range-has-face 63 87 markdown-inline-code-face)
2267 (markdown-test-range-has-face 88 89 markdown-markup-face)
2268 ;; Seven backquotes in a row
2269 (markdown-test-range-has-face 119 125 nil)
2270 ;; Backquotes at beginning or end
2271 (markdown-test-range-has-face 228 229 markdown-markup-face)
2272 (markdown-test-range-has-face 230 237 markdown-inline-code-face)
2273 (markdown-test-range-has-face 238 239 markdown-markup-face)
2274 (markdown-test-range-has-face 341 342 markdown-markup-face)
2275 (markdown-test-range-has-face 343 349 markdown-inline-code-face)
2276 (markdown-test-range-has-face 350 351 markdown-markup-face)
2277 ;; Backslash as final character
2278 (markdown-test-range-has-face 460 460 markdown-markup-face)
2279 (markdown-test-range-has-face 461 467 markdown-inline-code-face)
2280 (markdown-test-range-has-face 468 468 markdown-markup-face)
2281 ;; Escaping of leading backquotes
2282 (markdown-test-range-has-face 586 592 nil)
2283 (markdown-test-range-has-face 597 603 nil)
2284 ;; A code span crossing lines
2285 (markdown-test-range-has-face 652 656 nil)
2286 (markdown-test-range-has-face 657 657 markdown-markup-face)
2287 (markdown-test-range-has-face 658 665 markdown-inline-code-face)
2288 (markdown-test-range-has-face 666 666 markdown-markup-face)
2289 ;; Three backquotes: same line, across lines, not across blocks
2290 (markdown-test-range-has-face 695 748 nil)
2291 (markdown-test-range-has-face 749 750 markdown-markup-face)
2292 (markdown-test-range-has-face 751 755 markdown-inline-code-face)
2293 (markdown-test-range-has-face 756 757 markdown-markup-face)
2294 (markdown-test-range-has-face 758 805 nil)
2295 (markdown-test-range-has-face 806 807 markdown-markup-face)
2296 (markdown-test-range-has-face 808 812 markdown-inline-code-face)
2297 (markdown-test-range-has-face 813 814 markdown-markup-face)
2298 (markdown-test-range-has-face 815 891 nil)
2301 (ert-deftest test-markdown-font-lock/code-2 ()
2302 "Multiple code spans in a row and on different lines."
2303 (markdown-test-string "`foo` `bar` `baz`"
2304 (markdown-test-range-has-face 1 1 markdown-markup-face)
2305 (markdown-test-range-has-face 2 4 markdown-inline-code-face)
2306 (markdown-test-range-has-face 5 5 markdown-markup-face)
2307 (markdown-test-range-has-face 6 6 nil)
2308 (markdown-test-range-has-face 7 7 markdown-markup-face)
2309 (markdown-test-range-has-face 8 10 markdown-inline-code-face)
2310 (markdown-test-range-has-face 11 11 markdown-markup-face)
2311 (markdown-test-range-has-face 12 12 nil)
2312 (markdown-test-range-has-face 13 13 markdown-markup-face)
2313 (markdown-test-range-has-face 14 16 markdown-inline-code-face)
2314 (markdown-test-range-has-face 17 17 markdown-markup-face))
2315 (markdown-test-string "`a`\n`b`\n`c`\n"
2316 (markdown-test-range-has-face 1 1 markdown-markup-face)
2317 (markdown-test-range-has-face 2 2 markdown-inline-code-face)
2318 (markdown-test-range-has-face 3 3 markdown-markup-face)
2319 (markdown-test-range-has-face 4 4 nil)
2320 (markdown-test-range-has-face 5 5 markdown-markup-face)
2321 (markdown-test-range-has-face 6 6 markdown-inline-code-face)
2322 (markdown-test-range-has-face 7 7 markdown-markup-face)
2323 (markdown-test-range-has-face 8 8 nil)
2324 (markdown-test-range-has-face 9 9 markdown-markup-face)
2325 (markdown-test-range-has-face 10 10 markdown-inline-code-face)
2326 (markdown-test-range-has-face 11 11 markdown-markup-face)
2327 (markdown-test-range-has-face 12 12 nil))
2328 (markdown-test-string "a`foo`b`bar`c`baz`d"
2329 (markdown-test-range-has-face 1 1 nil)
2330 (markdown-test-range-has-face 2 2 markdown-markup-face)
2331 (markdown-test-range-has-face 3 5 markdown-inline-code-face)
2332 (markdown-test-range-has-face 6 6 markdown-markup-face)
2333 (markdown-test-range-has-face 7 7 nil)
2334 (markdown-test-range-has-face 8 8 markdown-markup-face)
2335 (markdown-test-range-has-face 9 11 markdown-inline-code-face)
2336 (markdown-test-range-has-face 12 12 markdown-markup-face)
2337 (markdown-test-range-has-face 13 13 nil)
2338 (markdown-test-range-has-face 14 14 markdown-markup-face)
2339 (markdown-test-range-has-face 15 17 markdown-inline-code-face)
2340 (markdown-test-range-has-face 18 18 markdown-markup-face)
2341 (markdown-test-range-has-face 19 19 nil)))
2343 (ert-deftest test-markdown-font-lock/code-3 ()
2344 "Backslashes don't escape backticks inside of inline code strings."
2345 (markdown-test-string
2346 "`foo\\`bar`"
2347 (markdown-test-range-has-face 1 1 markdown-markup-face)
2348 (markdown-test-range-has-face 2 5 markdown-inline-code-face)
2349 (markdown-test-range-has-face 6 6 markdown-markup-face)
2350 (markdown-test-range-has-face 7 10 nil)))
2352 (ert-deftest test-markdown-font-lock/code-link-precedence ()
2353 "Test that inline code takes precedence over inline links.
2354 Test currently fails because this case isn't handled properly."
2355 :expected-result :failed
2356 (markdown-test-string
2357 "[not a `link](/foo`)"
2358 (markdown-test-range-has-face 1 7 nil)
2359 (markdown-test-range-has-face 8 8 markdown-markup-face)
2360 (markdown-test-range-has-face 9 18 markdown-inline-code-face)
2361 (markdown-test-range-has-face 19 19 markdown-markup-face)
2362 (markdown-test-range-has-face 20 20 nil)))
2364 (ert-deftest test-markdown-font-lock/code-in-comment ()
2365 "Test that inline code is not matched inside a comment."
2366 (markdown-test-string
2367 "<!-- `not code` -->"
2368 (markdown-test-range-has-face 1 19 'markdown-comment-face)
2369 (should-not (markdown-range-property-any 1 19 'face '(markdown-inline-code-face)))))
2371 (ert-deftest test-markdown-font-lock/kbd ()
2372 "Test font lock for <kbd> tags."
2373 (markdown-test-string "<kbd>C-c <</kbd>"
2374 (markdown-test-range-has-face 1 5 markdown-markup-face)
2375 (markdown-test-range-has-face 6 10 markdown-inline-code-face)
2376 (markdown-test-range-has-face 11 16 markdown-markup-face))
2377 (markdown-test-string "To quit Emacs, press <kbd>C-x C-c</kbd>."
2378 (markdown-test-range-has-face 1 21 nil)
2379 (markdown-test-range-has-face 22 26 markdown-markup-face)
2380 (markdown-test-range-has-face 27 33 markdown-inline-code-face)
2381 (markdown-test-range-has-face 34 39 markdown-markup-face)
2382 (markdown-test-range-has-face 40 40 nil)))
2384 (ert-deftest test-markdown-font-lock/lists-1 ()
2385 "A simple list marker font lock test."
2386 (markdown-test-file "lists.text"
2387 (dolist (loc (list 1063 1283 1659 1830 1919 2150 2393 2484
2388 2762 2853 3097 3188 3700 3903 4009))
2389 (goto-char loc)
2390 (should (looking-at "[*+-]"))
2391 (markdown-test-range-has-face loc loc markdown-list-face))))
2393 (ert-deftest test-markdown-font-lock/definition-list ()
2394 "A simple definition list marker font lock test."
2395 (markdown-test-file "definition-list.text"
2396 (markdown-test-range-has-face 7 7 'markdown-list-face)
2397 (markdown-test-range-has-face 29 52 'markdown-pre-face)
2398 (markdown-test-range-has-face 55 55 'markdown-list-face)))
2400 (ert-deftest test-markdown-font-lock/pre-1 ()
2401 "Nested list and pre block font lock test."
2402 (markdown-test-file "nested-list.text"
2403 (dolist (loc (list 4 29 194 224 491 525))
2404 (markdown-test-range-has-face loc loc markdown-list-face))
2405 (markdown-test-range-has-face 6 25 nil)
2406 (markdown-test-range-has-face 31 83 nil)
2407 (markdown-test-range-has-face 85 154 markdown-pre-face)
2408 (markdown-test-range-has-face 157 189 nil)
2409 (markdown-test-range-has-face 196 215 nil)
2410 (markdown-test-range-has-face 226 403 nil)
2411 (markdown-test-range-has-face 405 481 markdown-pre-face)
2412 (markdown-test-range-has-face 493 512 nil)
2413 (markdown-test-range-has-face 527 546 nil)
2414 (markdown-test-range-has-face 548 580 markdown-pre-face)))
2416 (ert-deftest test-markdown-font-lock/pre-2 ()
2417 (markdown-test-string "* item\n\nreset baseline\n\n pre block\n"
2418 (markdown-test-range-has-face 1 1 markdown-list-face)
2419 (markdown-test-range-has-face 2 23 nil)
2420 (markdown-test-range-has-face 29 37 markdown-pre-face)))
2422 (ert-deftest test-markdown-font-lock/pre-3 ()
2423 (markdown-test-string "It is interesting to see what happens when one queries
2424 `social upheaval` and `protopalatial era`.
2426 * `social upheaval`: the follwing queries have been tried:
2428 social upheaval subClassOf"
2429 (markdown-test-range-has-face 160 190 nil)))
2431 (ert-deftest test-markdown-font-lock/pre-4 ()
2432 "Pre blocks must be preceded by a blank line"
2433 (markdown-test-string "Paragraph
2434 for (var i = 0; i < 10; i++) {
2435 console.log(i);
2437 (markdown-test-range-has-face (point-min) (point-max) nil)))
2439 (ert-deftest test-markdown-font-lock/fenced-1 ()
2440 "Test fenced code blocks containing four-space indents."
2441 (markdown-test-string "Fenced code block
2444 if (x)
2445 foo();
2447 if (y)
2448 bar();
2451 (markdown-test-range-has-face 1 19 nil)
2452 (markdown-test-range-has-face 20 22 markdown-markup-face)
2453 (markdown-test-range-has-face 24 60 markdown-pre-face)
2454 (markdown-test-range-has-face 61 63 markdown-markup-face)))
2456 (ert-deftest test-markdown-font-lock/gfm-fenced-1 ()
2457 "Test GFM-style fenced code blocks (1)."
2458 (let ((markdown-fontify-code-blocks-natively t))
2459 (markdown-test-string "```ruby
2460 require 'redcarpet'
2461 markdown = Redcarpet.new('Hello World!')
2462 puts markdown.to_html
2463 ```"
2464 (markdown-test-range-has-face 1 3 markdown-markup-face) ; ```
2465 (markdown-test-range-has-face 4 7 markdown-language-keyword-face) ; ruby
2466 (markdown-test-range-has-face 9 90 'markdown-code-face) ; entire code block
2467 (unless (version< emacs-version "24.4")
2468 (markdown-test-range-has-face 9 15 'font-lock-builtin-face)) ; require
2469 (markdown-test-range-has-face 17 27 'font-lock-string-face) ; 'redcarpet'
2470 (markdown-test-range-has-face 40 48 'font-lock-type-face) ; Redcarpet
2471 (unless (version< emacs-version "24.4")
2472 (markdown-test-range-has-face 70 72 'font-lock-builtin-face)) ; puts
2473 (markdown-test-range-has-face 92 94 markdown-markup-face)))) ; ```
2475 (ert-deftest test-markdown-font-lock/gfm-fenced-2 ()
2476 "Test GFM-style fenced code blocks (2)."
2477 (markdown-test-string "```{r sum}\n2+2\n```"
2478 (markdown-test-range-has-face 1 3 markdown-markup-face) ; ```
2479 (markdown-test-range-has-face 4 4 markdown-markup-face) ; {
2480 (markdown-test-range-has-face 5 5 markdown-language-keyword-face) ; r
2481 (markdown-test-range-has-face 7 9 markdown-language-info-face) ; sum
2482 (markdown-test-range-has-face 10 10 markdown-markup-face) ; }
2483 (markdown-test-range-has-face 12 14 markdown-pre-face) ; 2+2
2484 (markdown-test-range-has-face 16 18 markdown-markup-face))) ; ```
2486 (ert-deftest test-markdown-font-lock/gfm-fenced-3 ()
2487 "GFM-style code blocks need not be preceded by a blank line."
2488 (markdown-test-string "Paragraph
2489 ```js
2490 for (var i = 0; i < 10; i++) {
2491 console.log(i);
2493 ```"
2494 (markdown-test-range-has-face 1 10 nil) ; Paragraph
2495 (markdown-test-range-has-face 11 13 markdown-markup-face) ; ```
2496 (markdown-test-range-has-face 14 15 markdown-language-keyword-face) ; js
2497 (markdown-test-range-has-face 17 68 markdown-pre-face)
2498 (markdown-test-range-has-face 70 72 markdown-markup-face)))
2500 (ert-deftest test-markdown-font-lock/gfm-fenced-4 ()
2501 "Test GFM-style fenced code blocks (2)."
2502 (markdown-test-string "```scalaFiddle libraries=\"Java8 Time-0.1.0\"\nimport java.time._\n\nval hour = LocalTime.now().getHour()\n\nprintln(hour)\n```"
2503 (markdown-test-range-has-face 1 3 markdown-markup-face) ; ```
2504 (markdown-test-range-has-face 4 14 markdown-language-keyword-face) ; scalaFiddle
2505 (markdown-test-range-has-face 16 43 markdown-language-info-face) ; libraries="Java8 Time-0.1.0"
2506 (markdown-test-range-has-face 45 115 markdown-pre-face) ; [code]
2507 (markdown-test-range-has-face 117 119 markdown-markup-face))) ; ```
2509 (ert-deftest test-markdown-font-lock/tilde-fenced-1 ()
2510 "Test native fontification of tilde fenced code blocks."
2511 (let ((markdown-fontify-code-blocks-natively t))
2512 (markdown-test-string "~~~ruby
2513 require 'redcarpet'
2514 markdown = Redcarpet.new('Hello World!')
2515 puts markdown.to_html
2516 ~~~"
2517 (markdown-test-range-has-face 1 3 markdown-markup-face) ; ```
2518 (markdown-test-range-has-face 4 7 markdown-language-keyword-face) ; ruby
2519 (markdown-test-range-has-face 9 90 'markdown-code-face) ; entire code block
2520 (unless (version< emacs-version "24.4")
2521 (markdown-test-range-has-face 9 15 'font-lock-builtin-face)) ; require
2522 (markdown-test-range-has-face 17 27 'font-lock-string-face) ; 'redcarpet'
2523 (markdown-test-range-has-face 40 48 'font-lock-type-face) ; Redcarpet
2524 (unless (version< emacs-version "24.4")
2525 (markdown-test-range-has-face 70 72 'font-lock-builtin-face)) ; puts
2526 (markdown-test-range-has-face 92 94 markdown-markup-face)))) ; ```
2528 (ert-deftest test-markdown-font-lock/atx-no-spaces ()
2529 "Test font-lock for atx headers with no spaces."
2530 (markdown-test-string "##abc##"
2531 (markdown-test-range-has-face 1 7 nil))
2532 (markdown-test-string "##"
2533 (markdown-test-range-has-face 1 2 nil))
2534 (markdown-test-string "###"
2535 (markdown-test-range-has-face 1 3 nil)))
2537 (ert-deftest test-markdown-font-lock/setext-1-letter ()
2538 "An edge case for level-one setext headers."
2539 (markdown-test-string "a\n=\n"
2540 (markdown-test-range-has-face 1 1 markdown-header-face-1)
2541 (markdown-test-range-has-face 3 3 markdown-header-rule-face)))
2543 (ert-deftest test-markdown-font-lock/setext-2-letter ()
2544 "An edge case for level-two setext headers."
2545 (markdown-test-string "b\n-\n"
2546 (markdown-test-range-has-face 1 1 markdown-header-face-2)
2547 (markdown-test-range-has-face 3 3 markdown-header-rule-face)))
2549 (ert-deftest test-markdown-font-lock/inline-links ()
2550 "Test font lock for inline links."
2551 (let ((markdown-hide-urls nil))
2552 (markdown-test-file "inline.text"
2553 (markdown-test-range-has-face 925 925 markdown-markup-face)
2554 (markdown-test-range-has-face 926 929 markdown-link-face)
2555 (markdown-test-range-has-face 930 931 markdown-markup-face)
2556 (markdown-test-range-has-face 932 949 markdown-url-face)
2557 (markdown-test-range-has-face 951 957 markdown-link-title-face)
2558 (markdown-test-range-has-face 958 958 markdown-markup-face))))
2560 (ert-deftest test-markdown-font-lock/inline-links-with-parentheses ()
2561 "Test font lock for inline links with nested parentheses.
2562 See <https://github.com/jrblevin/markdown-mode/issues/170>."
2563 (let ((markdown-hide-urls nil))
2564 (markdown-test-string "[foo](bar(baz)qux)"
2565 (markdown-test-range-has-face 1 1 markdown-markup-face)
2566 (markdown-test-range-has-face 2 4 markdown-link-face)
2567 (markdown-test-range-has-face 5 6 markdown-markup-face)
2568 (markdown-test-range-has-face 7 17 markdown-url-face)
2569 (markdown-test-range-has-face 18 18 markdown-markup-face))))
2571 (ert-deftest test-markdown-font-lock/pre-comment ()
2572 "Test comments inside of a pre block."
2573 (markdown-test-string " <!-- pre, not comment -->"
2574 (markdown-test-range-has-face (point-min) (1- (point-max)) markdown-pre-face)))
2576 (ert-deftest test-markdown-font-lock/inline-code-comment ()
2577 "Test comments inside of inline code."
2578 (markdown-test-string "`<h1> <!-- HTML comment inside inline code -->`"
2579 (markdown-test-range-has-face (1+ (point-min)) (- (point-max) 2) markdown-inline-code-face)))
2581 (ert-deftest test-markdown-font-lock/inline-code-link ()
2582 "Test links inside of inline code."
2583 (markdown-test-string "`[text](url)`"
2584 (markdown-test-range-has-face (1+ (point-min)) (- (point-max) 2) 'markdown-inline-code-face)
2585 (should-not (markdown-range-property-any
2586 (1+ (point-min)) (- (point-max) 2) 'face
2587 '(markdown-markup-face markdown-link-face markdown-url-face)))))
2589 (ert-deftest test-markdown-font-lock/comment-hanging-indent ()
2590 "Test comments with hanging indentation."
2591 (markdown-test-string "<!-- This comment has\n hanging indentation -->"
2592 (markdown-test-range-has-face (point-min) (1- (point-max)) markdown-comment-face)))
2594 (ert-deftest test-markdown-font-lock/comment-multiple ()
2595 "Test multiple single-line comments in arow."
2596 (markdown-test-string "<!-- This is a comment -->\n<!-- And so is this -->"
2597 (markdown-test-range-has-face
2598 (point-at-bol) (1- (point-at-eol)) markdown-comment-face)
2599 (forward-line)
2600 (markdown-test-range-has-face
2601 (point-at-bol) (1- (point-at-eol)) markdown-comment-face)))
2603 (ert-deftest test-markdown-font-lock/comment-list-items ()
2604 "Test comment with list inside."
2605 (markdown-test-string
2606 "<!--
2607 - note 1;
2608 - note 2.
2609 -->"
2610 (markdown-test-range-face-equals (point-min) (1- (point-max))
2611 markdown-comment-face)))
2613 (ert-deftest test-markdown-font-lock/comment-angle-bracket ()
2614 "Regression test for GH-117."
2615 (markdown-test-string "<!-- > test -->"
2616 (markdown-test-range-face-equals (point-min) (1- (point-max))
2617 markdown-comment-face)))
2619 (ert-deftest test-markdown-font-lock/comment-link ()
2620 "Test links inside of comments."
2621 (markdown-test-string "<!-- [text](url) -->"
2622 (markdown-test-range-has-face (+ (point-min) 5) (- (point-max) 4) 'markdown-comment-face)
2623 (should-not (markdown-range-property-any
2624 (+ (point-min) 5) (- (point-max) 4) 'face
2625 '(markdown-markup-face markdown-link-face markdown-url-face)))))
2627 (ert-deftest test-markdown-font-lock/footnote-markers-links ()
2628 "Test an edge case involving footnote markers and inline reference links."
2629 (markdown-test-string "Harvard[^1] [tuition][]"
2630 (markdown-test-range-has-face 1 7 nil)
2631 (markdown-test-range-has-face 8 8 markdown-markup-face)
2632 (markdown-test-range-has-face 10 10 markdown-footnote-marker-face)
2633 (markdown-test-range-has-face 11 11 markdown-markup-face)
2634 (markdown-test-range-has-face 12 12 nil)
2635 (markdown-test-range-has-face 13 13 markdown-markup-face)
2636 (markdown-test-range-has-face 14 20 markdown-link-face)
2637 (markdown-test-range-has-face 21 21 markdown-markup-face)
2638 (markdown-test-range-has-face 22 23 markdown-markup-face)))
2640 (ert-deftest test-markdown-font-lock/mmd-metadata ()
2641 "Basic MultMarkdown metadata tests."
2642 (markdown-test-string "Title: peg-multimarkdown User's Guide
2643 Author: Fletcher T. Penney
2644 Base Header Level: 2"
2645 (markdown-test-range-has-face 1 5 markdown-metadata-key-face)
2646 (markdown-test-range-has-face 6 6 markdown-markup-face)
2647 (markdown-test-range-has-face 8 37 markdown-metadata-value-face)
2648 (markdown-test-range-has-face 39 44 markdown-metadata-key-face)
2649 (markdown-test-range-has-face 46 46 markdown-markup-face)
2650 (markdown-test-range-has-face 47 64 markdown-metadata-value-face)
2651 (markdown-test-range-has-face 66 82 markdown-metadata-key-face)
2652 (markdown-test-range-has-face 83 83 markdown-markup-face)
2653 (markdown-test-range-has-face 85 85 markdown-metadata-value-face))
2654 ;; Avoid triggering when a title contains a colon (e.g., Markdown: Syntax)
2655 (markdown-test-file "syntax.text"
2656 (markdown-test-range-has-face 1 16 markdown-header-face-1)))
2658 (ert-deftest test-markdown-font-lock/mmd-metadata-after-header ()
2659 "Ensure that similar lines are not matched after the header."
2660 (markdown-test-string "Title: peg-multimarkdown User's Guide
2662 Author: Fletcher T. Penney
2663 Base Header Level: 2"
2664 (markdown-test-range-has-face 1 5 markdown-metadata-key-face)
2665 (markdown-test-range-has-face 6 6 markdown-markup-face)
2666 (markdown-test-range-has-face 8 37 markdown-metadata-value-face)
2667 (markdown-test-range-has-face 40 65 nil)
2668 (markdown-test-range-has-face 67 86 nil)))
2670 (ert-deftest test-markdown-font-lock/pandoc-metadata ()
2671 "Basic Pandoc metadata tests."
2672 (markdown-test-string "% title
2673 two-line title
2674 % first author;
2675 second author
2676 % date
2678 body"
2679 (markdown-test-range-has-face 1 1 markdown-markup-face)
2680 (markdown-test-range-has-face 3 24 markdown-metadata-value-face)
2681 (markdown-test-range-has-face 26 26 markdown-markup-face)
2682 (markdown-test-range-has-face 28 56 markdown-metadata-value-face)
2683 (markdown-test-range-has-face 58 58 markdown-markup-face)
2684 (markdown-test-range-has-face 60 63 markdown-metadata-value-face)
2685 (markdown-test-range-has-face 64 69 nil)))
2687 (ert-deftest test-markdown-font-lock/yaml-metadata ()
2688 "Basic YAML metadata tests."
2689 (markdown-test-string
2690 "---
2691 layout: post
2692 date: 2015-08-13 11:35:25 EST
2695 (markdown-test-range-has-face 1 3 markdown-markup-face)
2696 (markdown-test-range-has-face 5 10 markdown-metadata-key-face)
2697 (markdown-test-range-has-face 11 11 markdown-markup-face)
2698 (markdown-test-range-has-face 13 16 markdown-metadata-value-face)
2699 (markdown-test-range-has-face 18 21 markdown-metadata-key-face)
2700 (markdown-test-range-has-face 22 22 markdown-markup-face)
2701 (markdown-test-range-has-face 24 46 markdown-metadata-value-face)
2702 (markdown-test-range-has-face 48 50 markdown-markup-face)))
2704 (ert-deftest test-markdown-font-lock/toml-metadata ()
2705 "Basic TOML metadata tests."
2706 (markdown-test-string
2707 "---
2708 layout = post
2709 date = 2015-08-13 11:35:25 EST
2712 (markdown-test-range-has-face 1 3 markdown-markup-face)
2713 (markdown-test-range-has-face 5 10 markdown-metadata-key-face)
2714 (markdown-test-range-has-face 12 12 markdown-markup-face)
2715 (markdown-test-range-has-face 14 17 markdown-metadata-value-face)
2716 (markdown-test-range-has-face 19 22 markdown-metadata-key-face)
2717 (markdown-test-range-has-face 24 24 markdown-markup-face)
2718 (markdown-test-range-has-face 26 48 markdown-metadata-value-face)
2719 (markdown-test-range-has-face 50 52 markdown-markup-face)))
2721 (ert-deftest test-markdown-font-lock/pandoc-yaml-metadata ()
2722 "Basic yaml metadata tests, with pandoc syntax."
2723 (let ((markdown-use-pandoc-style-yaml-metadata t))
2724 (markdown-test-string
2725 "some text
2728 layout: post
2729 date: 2015-08-13 11:35:25 EST
2732 more text
2735 layout: post
2736 date: 2015-08-13 11:35:25 EST
2739 But this is merely a code block
2743 layout: post
2744 date: 2015-08-13 11:35:25 EST
2748 ;; first section
2749 (markdown-test-range-has-face 12 14 markdown-markup-face)
2750 (markdown-test-range-has-face 16 21 markdown-metadata-key-face)
2751 (markdown-test-range-has-face 22 22 markdown-markup-face)
2752 (markdown-test-range-has-face 24 27 markdown-metadata-value-face)
2753 (markdown-test-range-has-face 29 32 markdown-metadata-key-face)
2754 (markdown-test-range-has-face 33 33 markdown-markup-face)
2755 (markdown-test-range-has-face 35 57 markdown-metadata-value-face)
2756 (markdown-test-range-has-face 59 61 markdown-markup-face)
2757 ;; second section
2758 (markdown-test-range-has-face 75 77 markdown-markup-face)
2759 (markdown-test-range-has-face 79 84 markdown-metadata-key-face)
2760 (markdown-test-range-has-face 85 85 markdown-markup-face)
2761 (markdown-test-range-has-face 87 90 markdown-metadata-value-face)
2762 (markdown-test-range-has-face 92 95 markdown-metadata-key-face)
2763 (markdown-test-range-has-face 96 96 markdown-markup-face)
2764 (markdown-test-range-has-face 98 120 markdown-metadata-value-face)
2765 (markdown-test-range-has-face 122 124 markdown-markup-face)
2766 ;; third section
2767 (markdown-test-range-has-face 160 162 markdown-markup-face)
2768 (markdown-test-range-has-face 164 213 markdown-pre-face)
2769 (markdown-test-range-has-face 215 217 markdown-markup-face))))
2771 (ert-deftest test-markdown-font-lock/line-break ()
2772 "Basic line break tests."
2773 (markdown-test-string " \nasdf \n"
2774 (markdown-test-range-has-face 1 9 nil)
2775 (markdown-test-range-has-face 10 11 markdown-line-break-face)))
2777 (ert-deftest test-markdown-font-lock/blockquote-bold ()
2778 "Test font lock for bold inside of a blockquote."
2779 (markdown-test-string
2780 "> **bold**"
2781 (markdown-test-range-has-face 1 10 markdown-blockquote-face)
2782 (markdown-test-range-has-face 5 8 markdown-bold-face)))
2784 (ert-deftest test-markdown-font-lock/blockquote-italic ()
2785 "Test font lock for italic inside of a blockquote."
2786 (markdown-test-string
2787 "> *italic*"
2788 (markdown-test-range-has-face 1 10 markdown-blockquote-face)
2789 (markdown-test-range-has-face 4 9 markdown-italic-face)))
2791 (ert-deftest test-markdown-font-lock/blockquote-code ()
2792 "Test font lock for inline code inside of a blockquote."
2793 (markdown-test-string
2794 "> `code`"
2795 (markdown-test-range-has-face 1 8 'markdown-blockquote-face)
2796 (markdown-test-range-has-face 3 3 'markdown-markup-face)
2797 (markdown-test-range-has-face 4 7 'markdown-inline-code-face)
2798 (markdown-test-range-has-face 8 8 'markdown-markup-face)))
2800 (ert-deftest test-markdown-font-lock/blockquote-link ()
2801 "Test font lock for links inside of a blockquote.
2802 This test will fail until font lock for inline links inside
2803 blockquotes is implemented (at present, the blockquote face
2804 takes precedence)."
2805 (markdown-test-string
2806 "> [link](url)"
2807 (markdown-test-range-has-face 1 1 markdown-markup-face)
2808 (markdown-test-range-has-face 2 13 markdown-blockquote-face)
2809 (markdown-test-range-has-face 3 3 markdown-markup-face)
2810 (markdown-test-range-has-face 4 7 markdown-link-face)
2811 (markdown-test-range-has-face 8 9 markdown-markup-face)
2812 (markdown-test-range-has-face 10 12 markdown-url-face)
2813 (markdown-test-range-has-face 13 13 markdown-markup-face)))
2815 (ert-deftest test-markdown-font-lock/blockquote-comment ()
2816 "Test font lock for comments inside of a blockquote."
2817 (markdown-test-string
2818 "> <!-- comment -->"
2819 (markdown-test-range-has-face 1 1 markdown-markup-face)
2820 (markdown-test-range-has-face 3 18 markdown-comment-face)))
2822 (ert-deftest test-markdown-font-lock/pre-override ()
2823 "Test that font lock for pre blocks overrides everything else."
2824 (markdown-test-string
2825 " **bold**
2826 _italic_
2827 <!-- comment -->
2828 [link](url)
2829 * list"
2830 (markdown-test-range-has-face 1 73 markdown-pre-face)))
2832 (ert-deftest test-markdown-font-lock/gfm-code-block-font-lock ()
2833 "GFM code block font lock test. Now in base markdown-mode as well!"
2834 (markdown-test-file "gfm.text"
2835 (markdown-test-range-has-face 2639 2641 markdown-markup-face) ; ```
2836 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face) ; lang
2837 (markdown-test-range-has-face 2647 2728 markdown-pre-face) ; code
2838 (markdown-test-range-has-face 2730 2732 markdown-markup-face))) ; ```
2840 (ert-deftest test-markdown-font-lock/reference-definition ()
2841 "Reference definitions should not include ]."
2842 (let ((markdown-hide-urls nil))
2843 (markdown-test-string "[1]: http://daringfireball.net/ \"title\""
2844 (markdown-test-range-has-face 2 2 markdown-reference-face) ; 1
2845 (markdown-test-range-has-face 6 31 markdown-url-face) ; URL
2846 (markdown-test-range-has-face 34 38 markdown-link-title-face)) ; title
2847 (markdown-test-string "[foo][1] and [bar][2]: not a reference definition"
2848 (markdown-test-range-has-face 2 4 markdown-link-face) ; foo
2849 (markdown-test-range-has-face 7 7 markdown-reference-face) ; 1
2850 (markdown-test-range-has-face 9 13 nil) ; [ ]and[ ]
2851 (markdown-test-range-has-face 15 17 markdown-link-face) ; bar
2852 (markdown-test-range-has-face 20 20 markdown-reference-face) ; 2
2853 (markdown-test-range-has-face 22 49 nil)))) ; [ ]and[ ]
2855 (ert-deftest test-markdown-font-lock/subscripts ()
2856 "Test font lock for subscripts."
2857 (markdown-test-string "H~2~0"
2858 (markdown-test-range-has-face 2 2 'markdown-markup-face) ; First ~
2859 (markdown-test-range-has-face 3 3 nil) ; 2
2860 (markdown-test-range-has-face 4 4 'markdown-markup-face))) ; Second ~
2862 (ert-deftest test-markdown-font-lock/superscripts ()
2863 "Test font lock for subscripts."
2864 (markdown-test-string "334^10^"
2865 (markdown-test-range-has-face 1 3 nil) ; 334
2866 (markdown-test-range-has-face 4 4 'markdown-markup-face) ; First ^
2867 (markdown-test-range-has-face 5 6 nil) ; 10
2868 (markdown-test-range-has-face 7 7 'markdown-markup-face))) ; Second ^
2870 (ert-deftest test-markdown-font-lock/hidden-urls-inline ()
2871 "Test URL hiding and toggling."
2872 (let ((markdown-hide-urls t))
2873 (markdown-test-file "inline.text"
2874 (markdown-test-range-has-face 925 925 markdown-markup-face)
2875 (markdown-test-range-has-face 926 929 markdown-link-face)
2876 (markdown-test-range-has-face 930 931 markdown-markup-face)
2877 (markdown-test-range-has-face 932 949 markdown-url-face)
2878 (markdown-test-range-has-face 951 957 markdown-link-title-face)
2879 (markdown-test-range-has-face 958 958 markdown-markup-face)
2880 (should (equal '((26 . 8734)) (get-text-property 932 'composition))))))
2882 (ert-deftest test-markdown-font-lock/hidden-urls-reference ()
2883 "Test URL hiding and toggling."
2884 (let ((markdown-hide-urls t))
2885 (markdown-test-string "[link][15]"
2886 ;; Two-character reference labels shouldn't get composed.
2887 (markdown-test-range-has-face 1 1 markdown-markup-face)
2888 (markdown-test-range-has-face 2 5 markdown-link-face)
2889 (markdown-test-range-has-face 6 7 markdown-markup-face)
2890 (markdown-test-range-has-face 8 9 markdown-reference-face)
2891 (markdown-test-range-has-face 10 10 markdown-markup-face)
2892 (should-not (get-text-property 8 'composition)))
2893 (markdown-test-string "[link][long-reference-label]"
2894 ;; Longer reference labels should be composed
2895 (markdown-test-range-has-face 1 1 markdown-markup-face)
2896 (markdown-test-range-has-face 2 5 markdown-link-face)
2897 (markdown-test-range-has-face 6 7 markdown-markup-face)
2898 (markdown-test-range-has-face 8 27 markdown-reference-face)
2899 (markdown-test-range-has-face 28 28 markdown-markup-face)
2900 (should (equal '((20 . 8734)) (get-text-property 8 'composition))))))
2902 (ert-deftest test-markdown-font-lock/snake-case-code-in-heading ()
2903 "Test underscores in inline code in headings."
2904 (markdown-test-string "# Title with `snake_case_code`"
2905 (should-not (markdown-range-property-any 21 24 'face '(markdown-italic-face)))
2906 (markdown-test-range-has-face 15 29 'markdown-inline-code-face)))
2908 (ert-deftest test-markdown-font-lock/stars-in-code-in-heading ()
2909 "Test asterisks in inline code in headings."
2910 (markdown-test-string "# Title with `char** foo, int* bar`"
2911 (should-not (markdown-range-property-any 20 29 'face '(markdown-italic-face)))
2912 (markdown-test-range-has-face 15 34 'markdown-inline-code-face)))
2914 (ert-deftest test-markdown-font-lock/stars-in-code-in-blockquote ()
2915 "Test asterisks in inline code in blockquote."
2916 (markdown-test-string "> Quote with `**stars**`"
2917 (should-not (markdown-range-property-any
2918 17 21 'face '(markdown-italic-face markdown-bold-face)))
2919 (markdown-test-range-has-face 15 23 'markdown-inline-code-face)))
2921 (ert-deftest test-markdown-font-lock/two-bold-words-after-list ()
2922 "Test two bold words after a list marker."
2923 (markdown-test-string "- **foo** **bar**"
2924 (should-not (markdown-range-property-any
2925 (point-min) (point-max) 'face '(markdown-italic-face)))))
2927 (ert-deftest test-markdown-font-lock/heading-with-italics-and-bold ()
2928 "Test two bold words after a list marker."
2929 (markdown-test-string "# Title with *italics* and **bold**"
2930 (markdown-test-range-has-face 15 21 'markdown-italic-face)
2931 (markdown-test-range-has-face 30 33 'markdown-bold-face)
2932 (should-not (markdown-range-property-any 30 33 'face '(markdown-italic-face)))))
2934 (ert-deftest test-markdown-font-lock/heading-with-italics-and-bold ()
2935 "Test that HRs are distinguished from setext H2 markup."
2936 (markdown-test-file "outline.text"
2937 (goto-char 485)
2938 (should (markdown-on-heading-p))
2939 (beginning-of-line)
2940 (should (markdown-on-heading-p))
2941 (should-not (markdown-range-property-any 453 484 'face '(markdown-hr-face)))))
2943 (ert-deftest test-markdown-font-lock/inline-attributes ()
2944 "Test inline attributes before a fenced code block."
2945 (markdown-test-file "Leanpub.md"
2946 ;; Inline attributes for a heading
2947 (markdown-test-range-has-face 38 42 'markdown-markup-face)
2948 ;; Inline attributes inside an aside block
2949 (markdown-test-range-has-face 123 141 'markdown-markup-face)
2950 ;; Inline attributes before a fenced code block
2951 (markdown-test-range-has-face 632 696 'markdown-markup-face)))
2953 (ert-deftest test-markdown-font-lock/leanpub-sections ()
2954 "Test Leanpub section markers."
2955 (markdown-test-file "Leanpub.md"
2956 ;; {frontmatter}
2957 (markdown-test-range-has-face 12 24 'markdown-markup-face)
2958 ;; {mainmatter}
2959 (markdown-test-range-has-face 69 80 'markdown-markup-face)
2960 ;; {pagebreak}
2961 (markdown-test-range-has-face 427 437 'markdown-markup-face)))
2963 (ert-deftest test-markdown-font-lock/leanpub-include ()
2964 "Test Leanpub include syntax."
2965 (markdown-test-file "Leanpub.md"
2966 ;; no title
2967 (markdown-test-range-has-face 561 563 'markdown-markup-face)
2968 (markdown-test-range-has-face 564 577 'markdown-url-face)
2969 (markdown-test-range-has-face 578 578 'markdown-markup-face)
2970 ;; title
2971 (markdown-test-range-has-face 581 583 'markdown-markup-face)
2972 (markdown-test-range-has-face 584 611 'markdown-link-title-face)
2973 (markdown-test-range-has-face 612 613 'markdown-markup-face)
2974 (markdown-test-range-has-face 614 628 'markdown-url-face)
2975 (markdown-test-range-has-face 629 629 'markdown-markup-face)))
2977 (ert-deftest test-markdown-font-lock/curly-brace-include ()
2978 "Test curly brace include syntax."
2979 (markdown-test-string "<<{file}"
2980 (markdown-test-range-has-face 1 3 'markdown-markup-face)
2981 (markdown-test-range-has-face 4 7 'markdown-url-face)
2982 (markdown-test-range-has-face 8 8 'markdown-markup-face)))
2984 (ert-deftest test-markdown-font-lock/square-bracket-include ()
2985 "Test square bracket include syntax."
2986 (markdown-test-string "<<[file]"
2987 (markdown-test-range-has-face 1 3 'markdown-markup-face)
2988 (markdown-test-range-has-face 4 7 'markdown-url-face)
2989 (markdown-test-range-has-face 8 8 'markdown-markup-face)))
2991 (ert-deftest test-markdown-font-lock/pandoc-inline-footnote ()
2992 "Test font lock for Pandoc inline footnotes."
2993 (markdown-test-string "Here is an inline note.^[Inline notes are easier to write, since
2994 you don't have to pick an identifier and move down to type the
2995 note.] And then you can close it and continue writing."
2996 (markdown-test-range-has-face 1 23 nil)
2997 (markdown-test-range-has-face 24 25 'markdown-markup-face)
2998 (markdown-test-range-has-face 26 133 'markdown-footnote-text-face)
2999 (markdown-test-range-has-face 134 134 'markdown-markup-face)))
3001 (ert-deftest test-markdown-font-lock/pandoc-inline-footnote-across-block ()
3002 "Test font lock for Pandoc inline footnotes."
3003 (markdown-test-string "Inline notes should not^[match
3005 across blocks]"
3006 (markdown-test-range-has-face (point-min) (point-max) nil)))
3008 ;;; Markdown Parsing Functions:
3010 (ert-deftest test-markdown-parsing/extend-region-function ()
3011 "Test `markdown-syntax-propertize-extend-region'.
3012 Should return a cons (NEW-START . NEW-END) or nil if no
3013 adjustment should be made. Function is called repeatedly until it
3014 returns nil."
3015 (markdown-test-file
3016 "inline.text"
3017 (should (equal (markdown-syntax-propertize-extend-region 1 17)
3018 (cons 1 91)))
3019 (should (equal (markdown-syntax-propertize-extend-region 2 17)
3020 (cons 1 91)))
3021 (should (equal (markdown-syntax-propertize-extend-region 1 91)
3022 nil))
3023 (should (equal (markdown-syntax-propertize-extend-region 93 157)
3024 nil))
3025 (should (equal (markdown-syntax-propertize-extend-region 496 502)
3026 (cons 486 510)))
3027 (should (equal (markdown-syntax-propertize-extend-region 486 510)
3028 nil))
3029 ;; Region that begins and ends with \n\n should not be extended
3030 (should (equal (markdown-syntax-propertize-extend-region 157 355)
3031 nil))))
3033 (defun markdown-test-check-match-limits (prop num begin end &optional pos)
3034 (let* ((posn (or pos (point)))
3035 (props (get-text-property posn prop)))
3036 (save-match-data
3037 (set-match-data props)
3038 (and (match-beginning num) (match-end num)
3039 (= (match-beginning num) begin)
3040 (= (match-end num) end)))))
3042 (ert-deftest test-markdown-parsing/syntax-with-adjacent-code-blocks ()
3043 "Test `markdown-syntax-propertize-fenced-code-blocks' with adjacent blocks."
3044 (markdown-test-string
3045 "~~~ shell
3046 #!/bin/sh
3048 echo \"Hello, world!\"
3051 ~~~ shell
3052 #!/bin/sh
3054 echo \"Hello, world v2!\"
3057 (let ((start-top-1 (make-marker)) (end-top-1 (make-marker))
3058 (start-lang-1 (make-marker)) (end-lang-1 (make-marker))
3059 (start-mid-1 (make-marker)) (end-mid-1 (make-marker))
3060 (start-bottom-1 (make-marker)) (end-bottom-1 (make-marker))
3061 (between (make-marker))
3062 (start-top-2 (make-marker)) (end-top-2 (make-marker))
3063 (start-lang-2 (make-marker)) (end-lang-2 (make-marker))
3064 (start-mid-2 (make-marker)) (end-mid-2 (make-marker))
3065 (start-bottom-2 (make-marker)) (end-bottom-2 (make-marker)))
3066 ;; First code block
3067 (set-marker start-top-1 1)
3068 (set-marker end-top-1 4)
3069 (set-marker start-lang-1 5)
3070 (set-marker end-lang-1 10)
3071 (set-marker start-mid-1 11)
3072 (set-marker end-mid-1 43)
3073 (set-marker start-bottom-1 43)
3074 (set-marker end-bottom-1 46)
3075 ;; check top tildes
3076 (should (markdown-test-check-match-limits
3077 'markdown-tilde-fence-begin 1 (marker-position start-top-1)
3078 (marker-position end-top-1) (marker-position start-top-1)))
3079 ;; check top language specifier
3080 (should (markdown-test-check-match-limits
3081 'markdown-tilde-fence-begin 3 (marker-position start-lang-1)
3082 (marker-position end-lang-1) (marker-position start-lang-1)))
3083 ;; check text in between
3084 (should (markdown-test-check-match-limits
3085 'markdown-fenced-code 0 (marker-position start-mid-1)
3086 (marker-position end-mid-1) (marker-position start-mid-1)))
3087 ;; check bottom tildes
3088 (should (markdown-test-check-match-limits
3089 'markdown-tilde-fence-end 1 (marker-position start-bottom-1)
3090 (marker-position end-bottom-1) (marker-position start-bottom-1)))
3091 ;; Point between code blocks
3092 (set-marker between 47)
3093 (should (equal (get-text-property between 'markdown-fenced-code)
3094 nil))
3095 ;; Second code block
3096 (set-marker start-top-2 48)
3097 (set-marker end-top-2 51)
3098 (set-marker start-lang-2 52)
3099 (set-marker end-lang-2 57)
3100 (set-marker start-mid-2 58)
3101 (set-marker end-mid-2 93)
3102 (set-marker start-bottom-2 93)
3103 (set-marker end-bottom-2 96)
3104 (should (markdown-test-check-match-limits
3105 'markdown-tilde-fence-begin 1 (marker-position start-top-2)
3106 (marker-position end-top-2) (marker-position start-top-2)))
3107 (should (markdown-test-check-match-limits
3108 'markdown-tilde-fence-begin 3 (marker-position start-lang-2)
3109 (marker-position end-lang-2) (marker-position start-lang-2)))
3110 (should (markdown-test-check-match-limits
3111 'markdown-fenced-code 0 (marker-position start-mid-2)
3112 (marker-position end-mid-2) (marker-position start-mid-2)))
3113 (should (markdown-test-check-match-limits
3114 'markdown-tilde-fence-end 1 (marker-position start-bottom-2)
3115 (marker-position end-bottom-2) (marker-position start-bottom-2)))
3116 ;; ;; Move point between code blocks and insert a character
3117 (goto-char between)
3118 (insert "x")
3119 ;; Re-propertize region after change
3120 (let ((range (markdown-syntax-propertize-extend-region (1- between) (point-max))))
3121 (markdown-syntax-propertize (car range) (cdr range)))
3122 ;; Re-check first code block
3123 (should (markdown-test-check-match-limits
3124 'markdown-tilde-fence-begin 1 (marker-position start-top-1)
3125 (marker-position end-top-1) (marker-position start-top-1)))
3126 (should (markdown-test-check-match-limits
3127 'markdown-tilde-fence-begin 3 (marker-position start-lang-1)
3128 (marker-position end-lang-1) (marker-position start-lang-1)))
3129 (should (markdown-test-check-match-limits
3130 'markdown-fenced-code 0 (marker-position start-mid-1)
3131 (marker-position end-mid-1) (marker-position start-mid-1)))
3132 (should (markdown-test-check-match-limits
3133 'markdown-tilde-fence-end 1 (marker-position start-bottom-1)
3134 (marker-position end-bottom-1) (marker-position start-bottom-1)))
3135 ;; Re-check point between code blocks
3136 (should (equal (get-text-property between 'markdown-fenced-code)
3137 nil))
3138 ;; Re-check second code block
3139 (should (markdown-test-check-match-limits
3140 'markdown-tilde-fence-begin 1 (marker-position start-top-2)
3141 (marker-position end-top-2) (marker-position start-top-2)))
3142 (should (markdown-test-check-match-limits
3143 'markdown-tilde-fence-begin 3 (marker-position start-lang-2)
3144 (marker-position end-lang-2) (marker-position start-lang-2)))
3145 (should (markdown-test-check-match-limits
3146 'markdown-fenced-code 0 (marker-position start-mid-2)
3147 (marker-position end-mid-2) (marker-position start-mid-2)))
3148 (should (markdown-test-check-match-limits
3149 'markdown-tilde-fence-end 1 (marker-position start-bottom-2)
3150 (marker-position end-bottom-2)
3151 (marker-position start-bottom-2))))))
3153 (ert-deftest test-markdown-parsing/propertize-fenced-in-between ()
3154 "Test whether `markdown-syntax-propertize-fenced-block-constructs' handles the
3155 case when it can't propertize both the start and end of a fenced block within a
3156 single pass (the end of the block is past the END argument)."
3157 (markdown-test-string
3158 "~~~ shell
3159 #!/bin/sh
3161 echo \"Hello, world!\"
3164 (set-text-properties (point-min) (point-max) nil)
3165 ;; syntax-propertize up to right after hashbang
3166 (markdown-syntax-propertize-fenced-block-constructs (point-min) 21)
3167 ;; ~~~ shell should be propertized, but nothing else
3168 ;; check tildes
3169 (should (markdown-test-check-match-limits
3170 'markdown-tilde-fence-begin 1 1 4 1))
3171 ;; check language
3172 (should (markdown-test-check-match-limits
3173 'markdown-tilde-fence-begin 3 5 10 5))
3174 ;; middle should not be propertized
3175 (should-not (get-text-property 11 'markdown-fenced-code))
3176 ;; neither should end
3177 (should-not (get-text-property 43 'markdown-tilde-fence-end))
3178 (markdown-syntax-propertize-fenced-block-constructs 21 (point-max))
3179 ;; everything should be propertized now
3180 ;; re-check top
3181 (should (markdown-test-check-match-limits
3182 'markdown-tilde-fence-begin 1 1 4 1))
3183 (should (markdown-test-check-match-limits
3184 'markdown-tilde-fence-begin 3 5 10 5))
3185 ;; check middle
3186 (should (markdown-test-check-match-limits 'markdown-fenced-code 0 10 43 10))
3187 ;; check ending tildes
3188 (should (markdown-test-check-match-limits
3189 'markdown-tilde-fence-end 1 43 46 43))))
3191 (ert-deftest test-markdown-parsing/get-code-block-at-pos ()
3192 "Test whether `markdown-code-block-at-pos' works in all situations. All
3193 situations are:
3194 1. pre block
3195 2. tilde block
3196 3. gfm block
3197 4. yaml metadata block"
3198 (let ((markdown-use-pandoc-style-yaml-metadata t))
3199 (markdown-test-string
3201 ~~~ ruby
3202 some_ruby_fun()
3206 a: b
3209 ``` {.bash}
3210 #!/bin/sh
3211 echo hey
3214 pre code
3215 random stuff
3216 more preformatted code
3219 data: pandoc
3222 ;; start/mid/end at tilde block
3223 (should (equal (markdown-code-block-at-pos 2) (list 2 30)))
3224 (should (equal (markdown-code-block-at-pos 11) (list 2 30)))
3225 (should (equal (markdown-code-block-at-pos 27) (list 2 30)))
3226 ;; yaml metadata block
3227 (should (equal (markdown-code-block-at-pos 32) (list 32 44)))
3228 (should (equal (markdown-code-block-at-pos 36) (list 32 44)))
3229 (should (equal (markdown-code-block-at-pos 41) (list 32 44)))
3230 ;; gfm block
3231 (should (equal (markdown-code-block-at-pos 46) (list 46 80)))
3232 (should (equal (markdown-code-block-at-pos 58) (list 46 80)))
3233 (should (equal (markdown-code-block-at-pos 77) (list 46 80)))
3234 ;; pre block
3235 (should (equal (markdown-code-block-at-pos 82) (list 82 138)))
3236 (should (equal (markdown-code-block-at-pos 99) (list 82 138)))
3237 (should (equal (markdown-code-block-at-pos 137) (list 82 138)))
3238 ;; pandoc yaml metadata block (should work if yaml above works)
3239 (should (equal (markdown-code-block-at-pos 140) (list 140 160)))
3240 (should (equal (markdown-code-block-at-pos 142) (list 140 160)))
3241 (should (equal (markdown-code-block-at-pos 144) (list 140 160)))
3242 (should (equal (markdown-code-block-at-pos 157) (list 140 160)))
3243 (should (equal (markdown-code-block-at-pos 159) (list 140 160))))))
3245 (ert-deftest test-markdown-parsing/syntax-get-fenced-blocks ()
3246 "Test whether *-get-fenced-block-* functions work in the case where a block is
3247 only partially propertized."
3248 (save-match-data
3249 (markdown-test-string
3250 "~~~
3252 (should (equal (markdown-syntax-propertize-extend-region
3253 (point-min) (point-max))
3254 nil))
3255 (goto-char 1)
3256 (set-match-data (markdown-text-property-at-point
3257 'markdown-tilde-fence-begin))
3258 (should (equal (markdown-get-fenced-block-from-start
3259 'markdown-tilde-fence-begin)
3260 nil)))
3261 (markdown-test-string
3262 "~~~
3263 ~~~"
3264 (goto-char 1)
3265 (set-match-data (markdown-text-property-at-point
3266 'markdown-tilde-fence-begin))
3267 (should (equal (markdown-get-fenced-block-from-start
3268 'markdown-tilde-fence-begin)
3269 (list 1 8)))
3270 (should (equal (markdown-code-block-at-pos (point)) (list 1 8)))
3272 ;; markdown-code-block-at-point-p should not modify match data
3273 (set-match-data (list 1 2 3 4))
3274 (should (markdown-code-block-at-point-p))
3275 (should (equal (match-data) (list 1 2 3 4)))
3277 (goto-char 5)
3278 (set-match-data (markdown-text-property-at-point
3279 'markdown-tilde-fence-end))
3280 (should (equal (markdown-get-fenced-block-from-end
3281 'markdown-tilde-fence-end)
3282 (list 1 8)))
3283 (should (equal (markdown-code-block-at-pos (point)) (list 1 8))))
3284 (markdown-test-string
3285 "~~~
3287 ~~~"
3288 (goto-char 1)
3289 (set-match-data (markdown-text-property-at-point
3290 'markdown-tilde-fence-begin))
3291 (should (equal (markdown-get-fenced-block-from-start
3292 'markdown-tilde-fence-begin)
3293 (list 1 9)))
3294 (should (equal (markdown-code-block-at-pos (point)) (list 1 9)))
3295 (goto-char 5)
3296 (set-match-data (markdown-text-property-at-point 'markdown-fenced-code))
3297 (should (equal (markdown-get-fenced-block-from-middle
3298 'markdown-fenced-code)
3299 (list 1 9)))
3300 (should (equal (markdown-code-block-at-pos (point)) (list 1 9)))
3301 (goto-char 6)
3302 (set-match-data (markdown-text-property-at-point
3303 'markdown-tilde-fence-end))
3304 (should (equal (markdown-get-fenced-block-from-end
3305 'markdown-tilde-fence-end)
3306 (list 1 9)))
3307 (should (equal (markdown-code-block-at-pos (point)) (list 1 9))))))
3309 (ert-deftest test-markdown-parsing/reference-definition-basic ()
3310 "Test reference definition function."
3311 (markdown-test-file "syntax.text"
3312 ;; Test accuracy of returned text and bounds
3313 (should (equal (markdown-reference-definition "1")
3314 (list "http://docutils.sourceforge.net/mirror/setext.html" 1942 1992)))
3315 (should (equal (markdown-reference-definition "2")
3316 (list "http://www.aaronsw.com/2002/atx/" 2000 2032)))
3317 ;; Test that match data remains intact
3318 (should (string-equal (match-string 5) "http://www.aaronsw.com/2002/atx/"))
3319 ;; Test anchor-only relative URL
3320 (should (equal (markdown-reference-definition "bq")
3321 (list "#blockquote" 7536 7547)))
3322 ;; Example references that appear in pre blocks in the text
3323 (should (not (markdown-reference-definition "")))
3324 (should (not (markdown-reference-definition "id")))
3325 (should (not (markdown-reference-definition "foo")))
3326 (should (not (markdown-reference-definition "A")))
3327 (should (not (markdown-reference-definition "Google")))
3328 ;; Test that we don't pick up other text in square brackets
3329 (should (not (markdown-reference-definition "blockquoting")))
3330 (should (not (markdown-reference-definition "square brackets")))
3331 ;; Test case insensitivity
3332 (should (equal (markdown-reference-definition "SRC")
3333 (list "/projects/markdown/syntax.text" 1245 1275)))))
3335 (ert-deftest test-markdown-parsing/get-defined-references ()
3336 "Test `markdown-get-defined-references'."
3337 (markdown-test-file "syntax.text"
3338 (should (equal (markdown-get-defined-references)
3339 '("src" "1" "2" "3" "4" "5" "6" "bq" "l"))))
3340 (markdown-test-file "outline.text"
3341 (should (equal (markdown-get-defined-references) nil)))
3342 (markdown-test-file "wiki-links.text"
3343 (should (equal (markdown-get-defined-references) nil))))
3345 (ert-deftest test-markdown-parsing/get-used-uris ()
3346 "Test `markdown-get-used-uris'."
3347 (markdown-test-file "syntax.text"
3348 (let ((uris (markdown-get-used-uris)))
3349 (should (equal (nth 0 uris) "#overview"))
3350 (should (equal (nth 20 uris) "http://www.aaronsw.com/2002/atx/"))
3351 (should-not (member "http://example.com/" uris))
3352 (should-not (member "address@example.com" uris)))))
3354 (defun markdown-test-test-region (beg end)
3355 (goto-char (1- beg))
3356 (should-not (markdown-inline-code-at-point-p))
3357 (goto-char (1+ end))
3358 (should-not (markdown-inline-code-at-point-p))
3359 (dolist (loc (number-sequence beg end))
3360 (goto-char loc)
3361 (should (markdown-inline-code-at-point))
3362 (should (equal (match-beginning 0) beg))
3363 (should (equal (match-end 0) end))))
3365 (ert-deftest test-markdown-parsing/inline-code-at-point ()
3366 "Test `markdown-inline-code-at-point'."
3367 (markdown-test-file "inline.text"
3368 (markdown-test-test-region 45 51) ; Regular code span
3369 (markdown-test-test-region 61 90) ; Code containing backticks
3370 (markdown-test-test-region 228 240) ; Backquotes at beginning
3371 (markdown-test-test-region 341 352) ; Backquotes at end
3372 (markdown-test-test-region 460 469) ; Backslash as final character
3373 (markdown-test-test-region 657 667) ; A code span crossing lines
3374 (markdown-test-test-region 749 758) ; Three backquotes on same line
3375 (markdown-test-test-region 806 815) ; Three backquotes across lines
3378 (ert-deftest test-markdown-parsing/inline-code-at-point-one-space ()
3379 "Test `markdown-inline-code-at-point' with multiple code spans in a row."
3380 (markdown-test-string "`foo` `bar` `baz`"
3381 (dolist (loc (number-sequence 1 6))
3382 (goto-char loc)
3383 ;; markdown-inline-code-at-point should set match data
3384 (should (markdown-inline-code-at-point))
3385 (should (equal (match-data) (list 1 6 1 2 2 5 5 6)))
3386 ;; markdown-inline-code-at-point-p should not modify match data
3387 (set-match-data (list 1 2 3 4))
3388 (should (markdown-inline-code-at-point-p))
3389 (should (equal (match-data) (list 1 2 3 4))))
3390 (dolist (loc (number-sequence 7 12))
3391 (goto-char loc)
3392 (should (markdown-inline-code-at-point))
3393 (should (equal (match-data) (list 7 12 7 8 8 11 11 12))))
3394 (dolist (loc (number-sequence 13 18))
3395 (goto-char loc)
3396 (should (markdown-inline-code-at-point))
3397 (should (equal (match-data) (list 13 18 13 14 14 17 17 18))))))
3399 (ert-deftest test-markdown-parsing/inline-code-at-point-no-space ()
3400 "Test `markdown-inline-code-at-point' with multiple code spans in a row.."
3401 (markdown-test-string "a`foo`b`bar`c`baz`d"
3402 (goto-char 1) ; "a"
3403 (should-not (markdown-inline-code-at-point-p))
3404 (dolist (loc (number-sequence 2 7)) ; "`foo`b"
3405 (goto-char loc)
3406 (should (markdown-inline-code-at-point))
3407 (should (equal (match-data) (list 2 7 2 3 3 6 6 7))))
3408 (dolist (loc (number-sequence 8 13)) ; "`bar`c"
3409 (goto-char loc)
3410 (should (markdown-inline-code-at-point))
3411 (should (equal (match-data) (list 8 13 8 9 9 12 12 13))))
3412 (dolist (loc (number-sequence 14 19)) ; "`baz`d"
3413 (goto-char loc)
3414 (should (markdown-inline-code-at-point))
3415 (should (equal (match-data) (list 14 19 14 15 15 18 18 19))))))
3417 (ert-deftest test-markdown-parsing/code-at-point-blank-line ()
3418 "Test `markdown-inline-code-at-point-p' at beginning of block."
3419 (markdown-test-string "----------\n\n## foo\n"
3420 (should-not (markdown-inline-code-at-point-p))
3421 (forward-line)
3422 (should-not (markdown-inline-code-at-point-p))
3423 (forward-line)
3424 (should-not (markdown-inline-code-at-point-p))))
3426 (ert-deftest test-markdown-parsing/match-comments ()
3427 "Test `markdown-match-comments'."
3428 (markdown-test-string
3429 "HTML <!-- foo --> comment"
3430 (should (markdown-match-comments (point-max)))
3431 (should (eq (point) 18))
3432 (should (equal (match-data) (list 6 18)))
3433 (should-not (markdown-match-comments (point-max)))))
3435 (ert-deftest test-markdown-parsing/range-property-any ()
3436 "Test behavior of `markdown-range-property-any'."
3437 (markdown-test-file
3438 "inline.text"
3439 (should (markdown-range-property-any
3440 (point-min) (point-at-eol)
3441 'face (list markdown-markup-face
3442 markdown-italic-face)))
3443 (should-not (markdown-range-property-any
3444 (point-min) (point-at-eol)
3445 'face (list markdown-bold-face)))))
3447 (ert-deftest test-markdown-parsing/inline-code ()
3448 "Don't cause infinite loop for inline code just after metadata block
3449 Detail: https://github.com/jrblevin/markdown-mode/issues/115"
3450 (markdown-test-string "---
3451 x: x
3455 (should (markdown-match-code (point-max)))
3456 (should (= (point) 17))
3457 (should (equal (match-data t) '(14 17 14 15 15 16 16 17)))
3458 (should-not (markdown-match-code (point-max)))))
3460 (ert-deftest test-markdown-parsing/list-item-at-point ()
3461 "Test `markdown-list-item-at-point-p'."
3462 (markdown-test-file "lists.text"
3463 (let ((orig-match-data '(1 2 3 4))
3464 (not-list-points '(273 399 512 3615))
3465 (list-points '(1063 1063 1176 1283 1659 1830 1919 2150
3466 2393 2484 2762 2853 3097 3188 3700
3467 3903 4009)))
3468 ;; markdown-inline-code-at-point-p should not modify match data
3469 (set-match-data orig-match-data)
3470 ;; Not list items
3471 (dolist (pos not-list-points)
3472 (goto-char pos)
3473 (should-not (markdown-list-item-at-point-p))
3474 (should (equal (match-data) orig-match-data)))
3475 ;; List items
3476 (dolist (pos list-points)
3477 (goto-char pos)
3478 (should (markdown-list-item-at-point-p))
3479 (should (equal (match-data) orig-match-data))))))
3481 (ert-deftest test-markdown-parsing/heading-at-point ()
3482 "Test `markdown-heading-at-point'."
3483 (save-match-data
3484 (markdown-test-file "outline.text"
3485 (should-not (markdown-heading-at-point))
3486 (markdown-test-goto-heading "An underline-style header")
3487 (forward-line -1)
3488 (should (markdown-heading-at-point))
3489 (should (equal (match-data t) (get-text-property (point) 'markdown-heading)))
3490 (should (equal (match-data t) (get-text-property (point) 'markdown-heading-1-setext))))))
3492 (ert-deftest test-markdown-parsing/inline-link-in-code-block ()
3493 "Test `markdown-match-generic-links'."
3494 (markdown-test-string " **bold**
3495 _italic_
3496 <!-- comment -->
3497 [link](url)
3498 * list"
3499 (goto-char (point-min))
3500 ;; The link inside the pre block should not match.
3501 (should-not (markdown-match-generic-links (point-max) nil))
3502 ;; Point should be left at limit.
3503 (should (= (point) (point-max)))))
3505 (ert-deftest test-markdown-parsing/broken-inline-link ()
3506 "Test `markdown-match-generic-links' with an invalid link."
3507 (markdown-test-string "[site1](http://site1.com
3508 [site2](http://site2.com)
3509 [site3](http://site3.com)"
3510 (goto-char (point-min))
3511 (let ((limit (point-at-eol)))
3512 ;; The first link is broken and shouldn't match.
3513 (should-not (markdown-match-generic-links limit nil))
3514 ;; Subsequent search shouldn't match, so point should move to limit.
3515 (should (= (point) limit)))
3516 ;; The second link should still match, starting from (point-min).
3517 (let ((limit (point-at-eol 2)))
3518 (should (markdown-match-generic-links limit nil))
3519 (should (= (point) (match-end 0))))
3520 ;; The third link should match when starting past the second one.
3521 (goto-char (match-end 0))
3522 (should (markdown-match-generic-links (point-max) nil))
3523 (should (= (point) (match-end 0)))))
3525 (ert-deftest test-markdown-parsing/code-block-lang ()
3526 "Test `markdown-code-block-lang'."
3527 ;; Test with GFM code blocks.
3528 (markdown-test-file "GFM.md"
3529 ;; Test a call with the optional argument.
3530 (should (string-equal
3531 (markdown-code-block-lang
3532 '(1455 . markdown-gfm-block-begin)) "js"))
3533 ;; Test a call without the optional argument.
3534 (goto-char 1504) ;; middle of a GFM code block
3535 (should (string-equal (markdown-code-block-lang) "js")))
3536 ;; Test with tilde-fenced cdoe blocks.
3537 (markdown-test-file "outline-code.text"
3538 (goto-char 107) ;; middle of a tilde fenced code block
3539 (should (string-equal (markdown-code-block-lang
3540 '(83 . markdown-tilde-fence-begin)) "bash"))))
3542 (ert-deftest test-markdown-parsing/code-block-lang-period ()
3543 "Test `markdown-code-block-lang' when language name begins with a period."
3544 (markdown-test-string "~~~ { .ruby }
3545 puts 'hello, world'
3548 (should (string-equal (markdown-code-block-lang) "ruby"))))
3550 ;;; Reference Checking:
3552 (ert-deftest test-markdown-references/goto-line-button ()
3553 "Create and test a goto line button."
3554 (markdown-test-string "line 1\nline 2\n"
3555 ;; Store the temporary buffer with the text
3556 (let ((target (current-buffer)))
3557 ;; Create a new buffer for inserting
3558 (with-temp-buffer
3559 ;; Verify that point is in a different buffer
3560 (should (not (equal (current-buffer) target)))
3561 ;; Insert and press the button
3562 (insert-button "goto line 2"
3563 :type 'markdown-goto-line-button
3564 'target-buffer target
3565 'target-line 2)
3566 (should (string-equal (buffer-string) "goto line 2"))
3567 (backward-button 1)
3568 (call-interactively 'push-button)
3569 ;; Verify that point is on line 2 of target buffer
3570 (should (= (line-number-at-pos) 2))
3571 (should (looking-at "line 2"))
3572 (should (equal (current-buffer) target))))))
3574 (ert-deftest test-markdown-references/button-map ()
3575 "Verify that button-buffer-map is used for check references buffer."
3576 (markdown-test-string "[undefined][ref]\n"
3577 (let* ((target (buffer-name))
3578 (check (format "*Undefined references for %s*" target)))
3579 (markdown-check-refs)
3580 (with-current-buffer (get-buffer check)
3581 (should (equal (local-key-binding (kbd "TAB")) 'forward-button))
3582 (should (equal (local-key-binding (kbd "<backtab>")) 'backward-button))))))
3584 ;;; Lists:
3586 (ert-deftest test-markdown-lists/levels-1 ()
3587 "Test list levels function `markdown-calculate-list-levels'."
3588 (markdown-test-file "nested-list.text"
3589 (let ((values '(((1 . 1) . nil) ((2 . 13) . (3)) ((14 . 23) . (7 3))
3590 ((24 . 26) . (11 7 3)))))
3591 (cl-loop for (range . value) in values
3592 do (goto-char (point-min))
3593 (forward-line (1- (car range)))
3594 (dotimes (n (- (cdr range) (car range)))
3595 (should (equal (markdown-calculate-list-levels) value))
3596 (forward-line))))))
3598 (ert-deftest test-markdown-lists/levels-2 ()
3599 "Test list levels function `markdown-calculate-list-levels'."
3600 (markdown-test-file "syntax.text"
3601 (let ((values '(((1 . 13) . nil) ((14 . 14) . (0)) ((15 . 17) . (4 0))
3602 ((18 . 18) . (0)) ((19 . 24) . (4 0)) ((25 . 25) . (0))
3603 ((26 . 29) . (4 0)) ((30 . 30) . (0)) ((31 . 33) . (4 0))
3604 ((34 . 588) . nil) ((589 . 595) . (0)) ((596 . 814) . nil)
3605 ((815 . 820) . (0)) ((821 . 898) . nil))))
3606 (cl-loop for (range . value) in values
3607 do (goto-char (point-min))
3608 (forward-line (1- (car range)))
3609 (dotimes (n (- (cdr range) (car range)))
3610 (should (equal (markdown-calculate-list-levels) value))
3611 (forward-line))))))
3613 (ert-deftest test-markdown-lists/levels-interior ()
3614 "Test `markdown-calculate-list-levels' from inside a list item."
3615 (markdown-test-file "nested-list.text"
3616 (goto-char 36)
3617 (should (equal (markdown-calculate-list-levels) (list 3)))
3618 (goto-char 267)
3619 (should (equal (markdown-calculate-list-levels) (list 7 3)))
3620 (goto-char 540)
3621 (should (equal (markdown-calculate-list-levels) (list 11 7 3)))))
3623 (ert-deftest test-markdown-lists/bounds-1 ()
3624 "Test list item bounds function `markdown-cur-list-item-bounds'."
3625 (markdown-test-file "lists.text"
3626 (markdown-test-goto-heading "Case 9")
3627 (forward-line)
3628 (should (eq (point) 3699))
3629 (markdown-next-list-item 4)
3630 (should (eq (point) 3700))
3631 (should (equal (markdown-cur-list-item-bounds)
3632 (list 3700 3901 0 4 "- " nil)))
3633 (markdown-next-list-item 4)
3634 (should (eq (point) 3903))
3635 (should (equal (markdown-cur-list-item-bounds)
3636 (list 3903 3937 0 4 "* " nil)))))
3638 (ert-deftest test-markdown-lists/bounds-2 ()
3639 "Function `markdown-cur-list-item-bounds' should return nil outside of list items."
3640 (markdown-test-string "line one\n\n* item\n"
3641 (should (null (markdown-cur-list-item-bounds)))
3642 (forward-line)
3643 (should (null (markdown-cur-list-item-bounds)))
3644 (forward-line)
3645 (should (markdown-cur-list-item-bounds))))
3647 (ert-deftest test-markdown-lists/bounds-prev ()
3648 "Test list item bounds function `markdown-prev-list-item-bounds'."
3649 (markdown-test-file "lists.text"
3650 (markdown-test-goto-heading "Case 9")
3651 (markdown-next-list-item 4)
3652 (markdown-next-list-item 4)
3653 (should (eq (point) 3903))
3654 (should (equal (markdown-prev-list-item-bounds)
3655 (list 3700 3901 0 4 "- " nil)))))
3657 (ert-deftest test-markdown-lists/bounds-next ()
3658 "Test list item bounds function `markdown-next-list-item-bounds'."
3659 (markdown-test-file "lists.text"
3660 (markdown-test-goto-heading "Case 2")
3661 (goto-char 1283)
3662 (should-not (markdown-next-list-item-bounds))
3663 (markdown-test-goto-heading "Case 9")
3664 (markdown-next-list-item 4)
3665 (should (eq (point) 3700))
3666 (should (equal (markdown-next-list-item-bounds)
3667 (list 3903 3937 0 4 "* " nil)))))
3669 (ert-deftest test-markdown-lists/bounds-gfm-task-list-item ()
3670 "Test `markdown-cur-list-item-bounds' with a GFM task list item."
3671 (markdown-test-string " - [ ] task name"
3672 (should (equal (markdown-cur-list-item-bounds)
3673 '(1 18 2 4 "- " "[ ] ")))))
3675 (ert-deftest test-markdown-lists/gfm-task-list-item-at-point-1 ()
3676 "Test `markdown-gfm-task-list-item-at-point' with regular list items."
3677 (markdown-test-file "nested-list.text"
3678 (dolist (pos '(1 26 36 267 514 540))
3679 (goto-char pos)
3680 (should-not (markdown-gfm-task-list-item-at-point)))))
3682 (ert-deftest test-markdown-lists/gfm-task-list-item-at-point-2 ()
3683 "Test `markdown-gfm-task-list-item-at-point' with a task list item."
3684 (markdown-test-string " - [ ] task"
3685 (should (markdown-gfm-task-list-item-at-point))))
3687 (ert-deftest test-markdown-insertion/insert-gfm-task-list-item ()
3688 "Test `markdown-insert-list-item' in a GFM task list."
3689 (markdown-test-string " - [ ] task"
3690 (goto-char (point-max))
3691 (call-interactively 'markdown-insert-list-item)
3692 (should (string-equal (buffer-string) " - [ ] task\n - [ ] "))))
3694 (ert-deftest test-markdown-lists/promotion-and-demotion ()
3695 "Test function `markdown-promote-list-item'."
3696 (markdown-test-file "nested-list.text"
3697 (forward-line)
3698 (should (looking-at " - List level 1 item 2
3700 Second paragraph of item 2
3702 Nested pre block in item 2
3703 Four spaces past the marker
3705 Another paragraph of item 2"))
3706 (markdown-demote-list-item)
3707 (should (looking-at " - List level 1 item 2
3709 Second paragraph of item 2
3711 Nested pre block in item 2
3712 Four spaces past the marker
3714 Another paragraph of item 2"))
3715 (markdown-promote-list-item)
3716 (should (looking-at " - List level 1 item 2
3718 Second paragraph of item 2
3720 Nested pre block in item 2
3721 Four spaces past the marker
3723 Another paragraph of item 2"))
3724 (goto-char (point-min))
3725 (forward-line 22)
3726 (should (looking-at " - List level 3 item 1
3728 Nested pre block"))
3729 (markdown-demote-list-item)
3730 (should (looking-at " - List level 3 item 1
3732 Nested pre block"))
3733 (markdown-promote-list-item)
3734 (should (looking-at " - List level 3 item 1
3736 Nested pre block"))))
3738 (ert-deftest test-markdown-lists/promotion-and-demotion-custom ()
3739 "Test custom variable `markdown-list-indent-width'."
3740 (markdown-test-file "nested-list.text"
3741 (forward-line)
3742 (should (looking-at " - List level 1 item 2
3744 Second paragraph of item 2
3746 Nested pre block in item 2
3747 Four spaces past the marker
3749 Another paragraph of item 2"))
3750 (let ((markdown-list-indent-width 2))
3751 (markdown-demote-list-item))
3752 (should (looking-at " - List level 1 item 2
3754 Second paragraph of item 2
3756 Nested pre block in item 2
3757 Four spaces past the marker
3759 Another paragraph of item 2"))))
3761 (ert-deftest test-markdown-lists/toggle-gfm-checkbox ()
3762 (markdown-test-string " - [X] GFM task list item"
3763 (should (string-equal (markdown-toggle-gfm-checkbox) "[ ]"))
3764 (should (string-equal (buffer-string) " - [ ] GFM task list item"))
3765 (should (string-equal (markdown-toggle-gfm-checkbox) "[x]"))
3766 (should (string-equal (buffer-string) " - [x] GFM task list item"))))
3768 (ert-deftest test-markdown-lists/beginning-of-list ()
3769 "Test `markdown-beginning-of-list'."
3770 (markdown-test-file "lists.text"
3771 ;; Case 1: not in a list
3772 (goto-char 399)
3773 (should-not (markdown-beginning-of-list))
3774 (should (= (point) 399))
3775 ;; Case 2
3776 (goto-char 1281)
3777 (should (= (markdown-beginning-of-list) 1063))
3778 (should (= (point) 1063))
3779 (goto-char 1395)
3780 (should (= (markdown-beginning-of-list) 1063))
3781 (should (= (point) 1063))
3782 ;; Case 3
3783 (goto-char 1848)
3784 (should (= (markdown-beginning-of-list) 1659))
3785 (should (= (point) 1659))
3786 ;; Case 4
3787 (goto-char 2041)
3788 (should (= (markdown-beginning-of-list) 1919))
3789 (should (= (point) 1919))
3790 ;; Case 8
3791 (goto-char 3553)
3792 (should (= (markdown-beginning-of-list) 3096))
3793 (should (= (point) 3096))))
3795 (ert-deftest test-markdown-lists/end-of-list ()
3796 "Test `markdown-end-of-list'."
3797 (markdown-test-file "lists.text"
3798 ;; Case 1: not in a list
3799 (goto-char 399)
3800 (should-not (markdown-end-of-list))
3801 (should (= (point) 399))
3802 ;; Case 2
3803 (goto-char 1281)
3804 (should (= (markdown-end-of-list) 1396))
3805 (should (= (point) 1396))
3806 (goto-char 1395)
3807 (should (= (markdown-end-of-list) 1396))
3808 (should (= (point) 1396))
3809 ;; Case 3
3810 (goto-char 1659)
3811 (should (= (markdown-end-of-list) 1849))
3812 (should (= (point) 1849))
3813 ;; Case 4
3814 (goto-char 2041)
3815 (should (= (markdown-end-of-list) 2092))
3816 (should (= (point) 2092))
3817 ;; Case 8
3818 (goto-char 3553)
3819 (should (= (markdown-end-of-list) 3614))
3820 (should (= (point) 3614))))
3822 (ert-deftest test-markdown-lists/up-list ()
3823 "Test `markdown-up-list'."
3824 (markdown-test-file "nested-list.text"
3825 (goto-char 581)
3826 (should (= (markdown-up-list) 484))
3827 (should (= (point) 484))
3828 (should (= (markdown-up-list) 191))
3829 (should (= (point) 191))
3830 ;; Return nil upon failure, but move out of list.
3831 (should-not (markdown-up-list))
3832 (should (= (point) (point-min)))))
3834 ;;; Outline minor mode tests:
3836 (ert-deftest test-markdown-outline/navigation ()
3837 "Test outline navigation functions."
3838 (markdown-test-file "outline.text"
3839 ;; Navigate to the first visible heading
3840 (markdown-next-visible-heading 1)
3841 (should (eq (point) 19))
3842 (should (looking-at "^# A top-level header"))
3843 ;; Navigate forward at the same level
3844 (markdown-forward-same-level 1)
3845 (should (eq (point) 351))
3846 (should (looking-at "^An underline-style header$"))
3847 ;; Navigate backward by four visible headings
3848 (markdown-previous-visible-heading 4)
3849 (should (eq (point) 69))
3850 (should (looking-at "^## A second-level header$"))
3851 ;; Navigate up the hierarchy (atx)
3852 (call-interactively #'markdown-up-heading)
3853 (should (looking-at "^# A top-level header"))
3854 (should (eq (mark) 69))
3855 ;; Navigate up the hierarchy (setext)
3856 (goto-char 516)
3857 (call-interactively #'markdown-up-heading)
3858 (should (looking-at "^An underline-style header$"))
3859 (should (eq (mark) 516))
3860 ;; Navigate back in the outline (setext to atx)
3861 (forward-line) ;; move to setext underline
3862 (markdown-backward-same-level 1)
3863 (should (looking-at "^# A top-level header"))))
3865 (ert-deftest test-markdown-outline/navigation-with-code ()
3866 "Test outline navigation functions with code blocks."
3867 (markdown-test-file "outline-code.text"
3868 ;; Navigate forward at the same level
3869 (markdown-forward-same-level 1)
3870 (should (eq (point) 159))
3871 (should (looking-at "^# Level one again"))))
3873 (ert-deftest test-markdown-outline/back-to-heading-over-code-block ()
3874 "Test `markdown-back-to-heading-over-code-block' over."
3875 (markdown-test-file "outline-code.text"
3876 ;; Initialize match data to known quantity.
3877 (set-match-data '(1 2 3 4))
3878 (should (equal (match-data t) '(1 2 3 4)))
3879 ;; Function should navigate back over code blocks.
3880 (re-search-forward "^# In a code block")
3881 (should (= (markdown-back-to-heading-over-code-block) 69))
3882 ;; Match data should be set for markdown-regex-header.
3883 (should (equal (match-data t) (get-text-property (point) 'markdown-heading)))
3884 ;; Function should return t when at a heading.
3885 (should (equal (markdown-back-to-heading-over-code-block) t))
3886 ;; Insert some text before the first heading.
3887 (goto-char (point-min))
3888 (save-excursion (insert "foo\n\n"))
3889 ;; Function should throw an error if no previous heading.
3890 (should-error (markdown-back-to-heading-over-code-block))
3891 ;; Function should return nil without error if NO-ERROR is non-nil.
3892 (should-not (markdown-back-to-heading-over-code-block t t))))
3894 (ert-deftest test-markdown-outline/visibility-atx ()
3895 "Test outline visibility cycling for ATX-style headers."
3896 (markdown-test-file "outline.text"
3897 (let (last-command this-command)
3898 ;; Navigate to the second visible heading
3899 (markdown-next-visible-heading 2)
3900 (should (eq (point) 69))
3901 (should (looking-at "^## A second-level header$"))
3902 ;; Cycle visibility of this subtree
3903 (setq this-command 'markdown-cycle)
3904 (markdown-cycle)
3905 (setq last-command 'markdown-cycle)
3906 (should (eq (point) 69))
3907 (should (looking-at "^## A second-level header$"))
3908 ;; Test that the entire subtree is invisible
3909 (markdown-test-range-has-property 93 349 'invisible 'outline)
3910 ;; Cycle visibility of this subtree again
3911 (markdown-cycle)
3912 (should (eq (point) 69))
3913 (should (looking-at "^## A second-level header$"))
3914 ;; Test that text is visible
3915 (markdown-test-range-has-property 95 121 'invisible nil)
3916 ;; Test that subheadings are visible
3917 (markdown-test-range-has-property 123 141 'invisible nil)
3918 ;; Cycle visibility of this subtree again
3919 (markdown-cycle)
3920 (should (eq (point) 69))
3921 (should (looking-at "^## A second-level header$"))
3922 ;; Verify that entire subtree is visible
3923 (markdown-test-range-has-property 93 349 'invisible nil))))
3925 (ert-deftest test-markdown-outline/visibility-setext ()
3926 "Test outline visibility cycling for setext-style headers."
3927 (markdown-test-file "outline.text"
3928 ;; Navigate to the sixth visible heading
3929 (markdown-next-visible-heading 7)
3930 (markdown-previous-visible-heading 1)
3931 (should (looking-at markdown-regex-header))
3932 (should (string-equal (match-string-no-properties 1) "An underline-style header"))
3933 (should (string-equal (match-string-no-properties 2) "========================="))
3934 ;; Cycle visibility subtree, test that it's invisible
3935 (markdown-cycle)
3936 (markdown-test-range-has-property 404 515 'invisible 'outline)
3937 ;; Cycle visibility subtree, test that text and headers are visible
3938 (markdown-cycle)
3939 (markdown-test-range-has-property 404 417 'invisible nil)
3940 (markdown-test-range-has-property 420 451 'invisible nil)))
3942 (ert-deftest test-markdown-outline/visibility-with-code ()
3943 "Test outline visibility cycling with code blocks."
3944 (markdown-test-file "outline-code.text"
3945 (let (last-command this-command)
3946 ;; Cycle global visibility to "overview" mode
3947 (setq this-command 'markdown-cycle)
3948 (markdown-cycle t)
3949 (setq last-command 'markdown-cycle)
3950 (should (eq (point) (point-min)))
3951 (should (looking-at "^# Level one"))
3952 ;; Test that the code block is invisible
3953 (markdown-test-range-has-property 83 157 'invisible 'outline)
3954 ;; Check subsequent headings
3955 (outline-next-visible-heading 1)
3956 (should (eq (point) 69))
3957 (should (looking-at "^## Level two"))
3958 (outline-next-visible-heading 1)
3959 (should (eq (point) 159))
3960 (should (looking-at "^# Level one again")))))
3962 (ert-deftest test-markdown-outline/visibility-with-metadata ()
3963 "Test outline visibility cycling with metadata blocks."
3964 (markdown-test-string
3965 "---
3966 layout = post
3967 date = 2015-08-13 11:35:25 EST
3970 (let (last-command this-command)
3971 ;; Cycle global visibility to "overview" mode
3972 (setq this-command 'markdown-cycle)
3973 (markdown-cycle t)
3974 ;; Check that text is visible
3975 (markdown-test-range-has-property (point-min) (point-max) 'invisible nil))))
3977 (ert-deftest test-markdown-outline/level ()
3978 "Test `markdown-outline-level'."
3979 (markdown-test-file "outline.text"
3980 (markdown-next-heading)
3981 (should (= (markdown-outline-level) 1))
3982 (markdown-forward-same-level 1)
3983 (should (= (markdown-outline-level) 1))
3984 (markdown-next-heading)
3985 (should (= (markdown-outline-level) 2))
3986 (markdown-next-heading)
3987 (should (= (markdown-outline-level) 1))
3988 (markdown-next-heading)
3989 (should (= (markdown-outline-level) 2))))
3991 ;;; Movement tests:
3993 (ert-deftest test-markdown-movement/defun ()
3994 "Test defun navigation."
3995 (markdown-test-file "outline.text"
3996 ;; end-of-defun should go to point-max
3997 (end-of-defun 10)
3998 (should (= (point) (point-max)))
3999 ;; end-of-defun should stop just before the next header
4000 (goto-char (point-min))
4001 (end-of-defun)
4002 (should (looking-at "\n# A top-level header"))
4003 (end-of-defun)
4004 (should (looking-at "\n## A second-level header"))
4005 (end-of-defun)
4006 (should (looking-at "\n### Third level ###"))
4007 (end-of-defun)
4008 (should (looking-at "\n### Third level number two ###"))
4009 ;; beginning-of-defun should move to the start of the previous header
4010 (beginning-of-defun)
4011 (should (looking-at "### Third level ###"))
4012 (beginning-of-defun)
4013 (should (looking-at "## A second-level header"))
4014 (beginning-of-defun)
4015 (should (looking-at "# A top-level header"))
4016 (beginning-of-defun)
4017 ;; beginning-of-defun should move up to point-min
4018 (should (= (point) (point-min)))
4019 ;; (beginning-of-defun -1) should move to the start of the next header
4020 (forward-line 2)
4021 (beginning-of-defun -1)
4022 (should (looking-at "## A second-level header"))
4023 (beginning-of-defun -1)
4024 (should (looking-at "### Third level ###"))
4025 (beginning-of-defun -1)
4026 (should (looking-at "### Third level number two ###"))))
4028 (ert-deftest test-markdown-movement/beginning-of-defun-at-point-max ()
4029 "Test beginning of defun navigation at point-max."
4030 (markdown-test-file "outline.text"
4031 (goto-char (point-max))
4032 (beginning-of-defun)))
4034 (ert-deftest test-markdown-movement/text-block ()
4035 "Test plain text block movement."
4036 (markdown-test-file "outline.text"
4037 (markdown-end-of-text-block)
4038 (should (looking-at "\n# A top-level header"))
4039 (markdown-end-of-text-block)
4040 (should (looking-at "\nfollowed by some body text"))
4041 (markdown-end-of-text-block)
4042 (should (looking-at "\n## A second-level header"))
4043 (markdown-end-of-text-block)
4044 (should (looking-at "\nfollowed by some body text"))
4045 (markdown-end-of-text-block)
4046 (should (looking-at "\n### Third level ###"))
4047 (markdown-end-of-text-block)
4048 (should (looking-at "\n\\* A list item"))
4049 (markdown-end-of-text-block)
4050 (should (looking-at "\n### Third level number two ###"))
4051 (markdown-end-of-text-block)
4052 (should (looking-at "\n### Level two again"))
4053 (markdown-end-of-text-block)
4054 (should (looking-at "\nfollowed by some body text"))
4056 (markdown-test-goto-heading "Level two")
4057 (markdown-end-of-text-block)
4058 (should (looking-at "\nbar"))
4059 (markdown-end-of-text-block)
4060 (should (= (point) (point-max)))
4061 (markdown-beginning-of-text-block)
4062 (should (looking-at "bar"))
4063 (markdown-beginning-of-text-block)
4064 (should (looking-at "## Level two"))
4065 (markdown-beginning-of-text-block)
4066 (should (looking-at "foo"))
4067 (markdown-beginning-of-text-block)
4068 (should (looking-at "# Level one"))
4069 (markdown-beginning-of-text-block)
4070 (should (looking-at "* With"))
4071 (markdown-beginning-of-text-block)
4072 (should (looking-at "And a level two underline header"))
4074 (goto-char (point-min))
4075 (markdown-test-goto-heading "A top-level header")
4076 (beginning-of-line)
4077 (markdown-beginning-of-text-block)
4078 (should (= (point) (point-min)))))
4080 (ert-deftest test-markdown-movement/mark-text-block ()
4081 "Test `markdown-mark-text-block'."
4082 (markdown-test-file "outline.text"
4083 ;; Start in middle of nested list with no separating whitespace.
4084 (goto-char 193)
4085 (markdown-mark-text-block)
4086 (should (= (point) 143))
4087 (should (= (mark) 269))))
4089 (ert-deftest test-markdown-movement/paragraph ()
4090 "Test Markdown paragraph movement."
4091 (markdown-test-file "outline.text"
4092 (markdown-forward-paragraph)
4093 (should (looking-at "\n# A top-level header"))
4094 (markdown-forward-paragraph)
4095 (should (looking-at "\nfollowed by some body text"))
4096 (markdown-forward-paragraph)
4097 (should (looking-at "\n## A second-level header"))
4098 (markdown-forward-paragraph)
4099 (should (looking-at "\nfollowed by some body text"))
4100 (markdown-forward-paragraph)
4101 (should (looking-at "\n### Third level ###"))
4102 (markdown-forward-paragraph)
4103 (should (looking-at "\n\\* A list item"))
4104 (markdown-forward-paragraph)
4105 (should (looking-at "\\* and another"))
4106 (markdown-forward-paragraph)
4107 (should (looking-at " \\+ and a sublist"))
4108 (markdown-forward-paragraph)
4109 (should (looking-at "- And a third"))
4110 (markdown-forward-paragraph)
4111 (should (looking-at "\n### Third level number two ###"))
4112 (markdown-forward-paragraph)
4113 (should (looking-at "\n### Level two again"))
4114 (markdown-forward-paragraph)
4115 (should (looking-at "\nfollowed by some body text"))
4117 (markdown-test-goto-heading "Level two")
4118 (markdown-forward-paragraph)
4119 (should (looking-at "\nbar"))
4120 (markdown-forward-paragraph)
4121 (should (= (point) (point-max)))
4122 (markdown-backward-paragraph)
4123 (should (looking-at "bar"))
4124 (markdown-backward-paragraph)
4125 (should (looking-at "## Level two"))
4126 (markdown-backward-paragraph)
4127 (should (looking-at "foo"))
4128 (markdown-backward-paragraph)
4129 (should (looking-at "# Level one"))
4130 (markdown-backward-paragraph)
4131 (should (looking-at "\\* List"))
4132 (markdown-backward-paragraph)
4133 (should (looking-at "\\* an unordered"))
4134 (markdown-backward-paragraph)
4135 (should (looking-at "\\* With"))
4136 (markdown-backward-paragraph)
4137 (should (looking-at "And a level two underline header"))
4139 (goto-char (point-min))
4140 (markdown-test-goto-heading "A top-level header")
4141 (beginning-of-line)
4142 (markdown-backward-paragraph)
4143 (should (= (point) (point-min)))))
4145 (ert-deftest test-markdown-movement/forward-paragraph-with-whitespace ()
4146 "Test Markdown paragraph movement."
4147 (markdown-test-file "blocks.md"
4148 (markdown-test-goto-heading "With Whitespace")
4149 (dolist (pos '(58 67 78 94 109 114 123 131 135 147 157 170 184 199))
4150 (markdown-forward-paragraph)
4151 (should (= (point) pos)))))
4153 (ert-deftest test-markdown-movement/backward-paragraph-with-whitespace ()
4154 "Test Markdown paragraph movement."
4155 (markdown-test-file "blocks.md"
4156 (markdown-test-goto-heading "With Whitespace")
4157 (markdown-next-heading)
4158 (should (= (point) 200))
4159 (dolist (pos '(185 172 158 148 136 132 124 115 110 94 78 67 59))
4160 (markdown-backward-paragraph)
4161 (should (= (point) pos)))))
4163 (ert-deftest test-markdown-movement/forward-paragraph-without-whitespace ()
4164 "Test Markdown paragraph movement."
4165 (markdown-test-file "blocks.md"
4166 (markdown-test-goto-heading "Without Whitespace")
4167 (dolist (pos '(222 230 240 255 270 275 283 291 294 305 314 326 340 354))
4168 (markdown-forward-paragraph)
4169 (should (= (point) pos)))))
4171 (ert-deftest test-markdown-movement/backward-paragraph-without-whitespace ()
4172 "Test Markdown paragraph movement."
4173 (markdown-test-file "blocks.md"
4174 (goto-char (point-max))
4175 (dolist (pos '(340 328 314 305 294 291 284 275 271 255 240 230 223 200))
4176 (markdown-backward-paragraph)
4177 (should (= (point) pos)))))
4179 (ert-deftest test-markdown-movement/block ()
4180 "Test Markdown block movement."
4181 (markdown-test-file "outline.text"
4182 (markdown-forward-block)
4183 (should (looking-at "\n# A top-level header"))
4184 (markdown-forward-block)
4185 (should (looking-at "\nfollowed by some body text"))
4186 (markdown-forward-block)
4187 (should (looking-at "\n## A second-level header"))
4188 (markdown-forward-block)
4189 (should (looking-at "\nfollowed by some body text"))
4190 (markdown-forward-block)
4191 (should (looking-at "\n### Third level ###"))
4192 (markdown-forward-block)
4193 (should (looking-at "\n\\* A list item"))
4194 (markdown-forward-block)
4195 (should (looking-at "\n### Third level number two ###"))
4196 (markdown-forward-block)
4197 (should (looking-at "\n### Level two again"))
4198 (markdown-forward-block)
4199 (should (looking-at "\nfollowed by some body text"))
4201 (markdown-test-goto-heading "Level two")
4202 (markdown-forward-block)
4203 (should (looking-at "\nbar"))
4204 (markdown-forward-block)
4205 (should (= (point) (point-max)))
4206 (markdown-backward-block)
4207 (should (looking-at "bar"))
4208 (markdown-backward-block)
4209 (should (looking-at "## Level two"))
4210 (markdown-backward-block)
4211 (should (looking-at "foo"))
4212 (markdown-backward-block)
4213 (should (looking-at "# Level one"))
4214 (markdown-backward-block)
4215 (should (looking-at "\\* With"))
4216 (markdown-backward-block)
4217 (should (looking-at "And a level two underline header"))
4219 (goto-char (point-min))
4220 (markdown-test-goto-heading "A top-level header")
4221 (beginning-of-line)
4222 (markdown-backward-block)
4223 (should (= (point) (point-min)))))
4225 (ert-deftest test-markdown-movement/forward-block-with-whitespace ()
4226 "Test Markdown block movement."
4227 (markdown-test-file "blocks.md"
4228 (markdown-test-goto-heading "With Whitespace")
4229 (dolist (pos '(58 109 114 131 135 147 157 184 199))
4230 (markdown-forward-block)
4231 (should (= (point) pos)))))
4233 (ert-deftest test-markdown-movement/backward-block-with-whitespace ()
4234 "Test Markdown block movement."
4235 (markdown-test-file "blocks.md"
4236 (markdown-test-goto-heading "With Whitespace")
4237 (markdown-next-heading)
4238 (dolist (pos '(185 158 148 136 132 115 110 59))
4239 (markdown-backward-block)
4240 (should (= (point) pos)))))
4242 (ert-deftest test-markdown-movement/forward-block-without-whitespace ()
4243 "Test Markdown block movement."
4244 (markdown-test-file "blocks.md"
4245 (markdown-test-goto-heading "Without Whitespace")
4246 (dolist (pos '(222 270 275 291 294 305 314 340 354))
4247 (markdown-forward-block)
4248 (should (= (point) pos)))))
4250 (ert-deftest test-markdown-movement/backward-block-without-whitespace ()
4251 "Test Markdown block movement."
4252 (markdown-test-file "blocks.md"
4253 (goto-char (point-max))
4254 (dolist (pos '(340 314 305 294 291 275 271 223 200))
4255 (markdown-backward-block)
4256 (should (= (point) pos)))))
4258 (ert-deftest test-markdown-movement/page ()
4259 "Test Markdown page movement."
4260 (markdown-test-file "outline.text"
4261 (markdown-forward-page)
4262 (should (looking-at "# A top-level header"))
4263 (markdown-forward-page)
4264 (should (looking-at "An underline-style header"))
4265 (markdown-forward-page)
4266 (should (looking-at "# Level one"))
4267 (markdown-forward-page)
4268 (should (eobp))
4269 (markdown-backward-page)
4270 (should (looking-at "# Level one"))
4271 (markdown-backward-page)
4272 (should (looking-at "An underline-style header"))
4273 (markdown-backward-page)
4274 (should (looking-at "# A top-level header"))
4275 (markdown-backward-page)
4276 (should (bobp))))
4278 (ert-deftest test-markdown-movement/blockquote-paragraphs ()
4279 "Test filling of blockquotes containing multiple paragraphs."
4280 (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n>\n> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n"
4281 (forward-paragraph)
4282 (should (looking-at "^>$"))
4283 (should (= (point) 128))
4284 (forward-paragraph)
4285 (should (= (point) (point-max)))))
4287 (ert-deftest test-markdown-movement/reference-definition ()
4288 "Test jumping to reference definitions."
4289 ;; Jumping to explicit reference definition
4290 (markdown-test-string "[a][ref]\n\n[ref]: gopher://localhost/\n"
4291 (markdown-reference-goto-definition)
4292 (should (= (point) 18)))
4293 ;; Jumping to implicit reference definition
4294 (markdown-test-string "[a][]\n\n[a]: ftp://localhost/\n"
4295 (markdown-reference-goto-definition)
4296 (should (= (point) 13)))
4297 ;; Creating non-existent reference definition
4298 (markdown-test-string "[a][]\n"
4299 (markdown-reference-goto-definition)
4300 (should (= (point) 13))
4301 (should (string-equal (buffer-string) "[a][]\n\n[a]: \n"))))
4303 (ert-deftest test-markdown-movement/back-to-same-level-over-code-block ()
4304 "`markdown-backward-same-level' over code block which contains header
4305 like statement. Detail: https://github.com/jrblevin/markdown-mode/issues/75"
4306 (markdown-test-string "
4307 ## Header 2-1
4309 ## Header 2-2
4311 ```R
4312 # Header Like Statement
4315 ## Header 2-3
4317 (search-forward "## Header 2-3")
4318 (let ((last-header-pos (point)))
4319 (forward-line -1)
4320 (call-interactively #'markdown-backward-same-level)
4321 (should (looking-at-p "## Header 2-1"))
4323 (goto-char last-header-pos)
4324 (call-interactively #'markdown-backward-same-level)
4325 (should (looking-at-p "## Header 2-2"))
4327 (goto-char last-header-pos)
4328 (markdown-backward-same-level 2)
4329 (should (looking-at-p "## Header 2-1"))
4331 (search-forward "# Header Like Statement")
4332 (call-interactively #'markdown-backward-same-level)
4333 (should (looking-at-p "## Header 2-1")))))
4335 ;;; Link tests:
4337 (ert-deftest test-markdown-link/follow ()
4338 "Test link following in a browser and in Emacs."
4339 (markdown-test-string "[text](http://path?query=foo#id)"
4340 (let* ((opened-url nil)
4341 (browse-url-browser-function
4342 (lambda (url &rest args) (setq opened-url url))))
4343 (markdown-follow-thing-at-point nil)
4344 (should (equal opened-url "http://path?query=foo#id"))))
4345 (when (featurep 'url-parse)
4346 (markdown-test-string "[text](path?query=foo#id)"
4347 (markdown-follow-thing-at-point nil)
4348 (should (equal (file-name-nondirectory (buffer-file-name)) "path"))
4349 (kill-buffer))))
4351 (ert-deftest test-markdown-link/inline-link-at-pos ()
4352 "Test `markdown-link-at-pos' return values with an inline link."
4353 (markdown-test-string "[text](url \"title\")"
4354 (should (equal (markdown-link-at-pos (point)) '(1 20 "text" "url" nil "title" nil)))))
4356 (ert-deftest test-markdown-link/inline-image-at-pos ()
4357 "Test `markdown-link-at-pos' return values with an inline image."
4358 (markdown-test-string "![text](url \"title\")"
4359 (should (equal (markdown-link-at-pos (point)) '(1 21 "text" "url" nil "title" "!")))))
4361 (ert-deftest test-markdown-link/reference-link-at-pos ()
4362 "Test `markdown-link-at-pos' return values with a reference link."
4363 (markdown-test-string "[text][ref]"
4364 (should (equal (markdown-link-at-pos (point)) '(1 12 "text" nil "ref" nil nil)))))
4366 (ert-deftest test-markdown-link/reference-image-at-pos ()
4367 "Test `markdown-link-at-pos' return values with a reference image."
4368 (markdown-test-string "![text][ref]"
4369 (should (equal (markdown-link-at-pos (point)) '(1 13 "text" nil "ref" nil "!")))))
4371 (ert-deftest test-markdown-link/angle-uri-at-pos ()
4372 "Test `markdown-link-at-pos' return values with an angle bracket inline link."
4373 (markdown-test-string "<http://jblevins.org/projects/markdown-mode/>"
4374 (should (equal (markdown-link-at-pos (point)) '(1 46 nil "http://jblevins.org/projects/markdown-mode/" nil nil nil)))))
4376 (ert-deftest test-markdown-link/plain-uri-at-pos ()
4377 "Test `markdown-link-at-pos' return values with a plain URI."
4378 (markdown-test-string "http://jblevins.org/projects/markdown-mode/"
4379 (should (equal (markdown-link-at-pos (point)) '(1 44 nil "http://jblevins.org/projects/markdown-mode/" nil nil nil)))))
4381 ;;; Wiki link tests:
4383 (ert-deftest test-markdown-wiki-link/file-local-variables ()
4384 "Test enabling wiki links via file-local variables."
4385 (markdown-test-file "wiki-links.text"
4386 (should-not markdown-enable-wiki-links)
4387 (hack-local-variables)
4388 (should markdown-enable-wiki-links)))
4390 (ert-deftest test-markdown-wiki-link/aliasing ()
4391 "Test filename extraction for aliased wiki links."
4392 (let ((markdown-enable-wiki-links t))
4393 (markdown-test-file "wiki-links.text"
4394 ;; Confirm location of first wiki link
4395 (should (eq (markdown-next-link) 8))
4396 ;; Confirm location of second wiki link
4397 (should (eq (markdown-next-link) 73))
4398 ;; Test predicate function
4399 (should (markdown-wiki-link-p))
4400 ;; Test alias-first filename extraction
4401 (setq markdown-wiki-link-alias-first t)
4402 (should (string-equal (markdown-wiki-link-link) "second"))
4403 ;; Test alias-second filename extraction
4404 (setq markdown-wiki-link-alias-first nil)
4405 (should (string-equal (markdown-wiki-link-link) "first")))))
4407 (ert-deftest test-markdown-wiki-link/navigation ()
4408 "Test wiki link navigation."
4409 (let ((markdown-enable-wiki-links t))
4410 (markdown-test-file "wiki-links.text"
4411 ;; Advance to first link
4412 (should (eq (markdown-next-link) 8))
4413 ;; Advance to second link
4414 (should (eq (markdown-next-link) 73))
4415 ;; Avance to final link
4416 (should (eq (markdown-next-link) 155))
4417 ;; Return nil and don't advance point
4418 (should (eq (markdown-next-link) nil))
4419 (should (eq (point) 155))
4420 ;; Move back to second link
4421 (should (eq (markdown-previous-link) 73))
4422 ;; Move back to first link
4423 (should (eq (markdown-previous-link) 8))
4424 ;; Return nil and don't move point
4425 (should (eq (markdown-previous-link) nil))
4426 (should (eq (point) 8)))))
4428 (ert-deftest test-markdown-wiki-link/font-lock ()
4429 "Test font lock faces for wiki links."
4430 (markdown-test-temp-file "wiki-links.text"
4431 (let* ((fn (concat (file-name-directory buffer-file-name)
4432 "inline.text"))
4433 (markdown-enable-wiki-links t))
4434 ;; Create inline.text in the same temp directory, refontify
4435 (write-region "" nil fn nil 1)
4436 (markdown-fontify-buffer-wiki-links)
4437 ;; Confirm location of first wiki link
4438 (should (eq (markdown-next-link) 8))
4439 ;; First wiki link doesn't have a corresponding file
4440 (markdown-test-range-has-property 8 20 'font-lock-face markdown-missing-link-face)
4441 ;; Second wiki link doesn't have a corresponding file
4442 (should (eq (markdown-next-link) 73))
4443 (markdown-test-range-has-property 73 88 'font-lock-face markdown-missing-link-face)
4444 ;; Move to third wiki link, and create the missing file
4445 (should (eq (markdown-next-link) 155))
4446 (should (string-equal (markdown-wiki-link-link) "inline"))
4447 (markdown-test-range-has-property 155 164 'font-lock-face markdown-link-face)
4448 ;; Check wiki links in code blocks
4449 (markdown-test-range-has-face 360 395 markdown-pre-face)
4450 ;; Remove temporary files
4451 (delete-file fn)
4454 (ert-deftest test-markdown-wiki-link/kill ()
4455 "Simple tests for `markdown-kill-thing-at-point' for wiki links."
4456 (let ((kill-ring nil)
4457 (markdown-enable-wiki-links t)
4458 (tests (list '("[[foo]]" . "foo")
4459 '("[[foo|bar]]" . "bar"))))
4460 (dolist (test tests)
4461 ;; Load test string (the car), move to end of first line, kill
4462 ;; thing at point, and then verify that the kill ring contains cdr.
4463 (markdown-test-string (car test)
4464 (end-of-line)
4465 (call-interactively 'markdown-kill-thing-at-point)
4466 (should (string-equal (current-kill 0) (cdr test)))))))
4468 ;;; Filling tests:
4470 (ert-deftest test-markdown-filling/blockquote ()
4471 "Test filling of blockquotes.
4472 See `adaptive-fill-first-line-regexp'."
4473 (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
4474 (fill-paragraph)
4475 (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."))))
4477 (ert-deftest test-markdown-filling/blockquote-paragraphs ()
4478 "Test filling of blockquotes containing multiple paragraphs."
4479 (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n>\n> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n"
4480 (forward-paragraph)
4481 (fill-paragraph)
4482 (should (string-equal (buffer-string) "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n>\n> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\n> nisi ut aliquip ex ea commodo consequat.\n"))))
4484 (ert-deftest test-markdown-filling/leanpub-block ()
4485 "Test adaptive filling of Leanpub blocks."
4486 (markdown-test-string "A> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
4487 (fill-paragraph)
4488 (should (string-equal (buffer-string) "A> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\nA> eiusmod tempor incididunt ut labore et dolore magna aliqua."))))
4490 (ert-deftest test-markdown-filling/space-after-list-marker ()
4491 "`fill-paragraph' should preserve more than one space after a list marker,
4492 since users may wish to indent their lists more than one space more than the
4493 width of the marker. The examples on the Markdown Syntax page have three
4494 spaces after the list marker for a total indentation of four."
4495 (let ((str "\n\n* List item indented four spaces.\n* Also four spaces."))
4496 (markdown-test-string str
4497 (forward-line 2)
4498 (fill-paragraph)
4499 (should (string-equal (buffer-string) str)))))
4501 (ert-deftest test-markdown-filling/multi-line-list-with-more-space ()
4502 "`fill-paragraph' should preserve more than one space after a list marker
4503 (see `test-preserve-space-after-list-marker')."
4504 (let ((str "* This list item is continued on\n the next line"))
4505 (markdown-test-string str
4506 ;; The first line is exactly 35 columns
4507 (let ((fill-column 35))
4508 (fill-paragraph)
4509 (should (string-equal (buffer-string) str))))))
4511 (ert-deftest test-markdown-filling/definition-list-add-leading-spaces ()
4512 "`fill-paragraph' should adapt to spaces after list marker."
4513 (markdown-test-string
4514 ": This list item is continued on the next line"
4515 (let ((fill-column 35))
4516 (fill-paragraph)
4517 (should (string-equal
4518 (buffer-string)
4519 ": This list item is continued on\n the next line")))))
4521 (ert-deftest test-markdown-filling/definition-list-preserve-leading-spaces ()
4522 "`fill-paragraph' should preserve spaces after list marker."
4523 (let ((str ": This list item is continued on\n the next line")
4524 (fill-column 35))
4525 (markdown-test-string
4526 str (fill-paragraph)
4527 (should (string-equal (buffer-string) str)))))
4529 (ert-deftest test-markdown-filling/list-item-plus ()
4530 "Test filling of list items with plus sign markers.
4531 See `adaptive-fill-regexp'."
4532 (markdown-test-string " + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
4533 (fill-paragraph)
4534 (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."))))
4536 (ert-deftest test-markdown-filling/list-item-plus-in-blockquote ()
4537 "Test filling of list items with plus sign markers inside blockquote.
4538 See `adaptive-fill-regexp'."
4539 (markdown-test-string "> + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
4540 (fill-paragraph)
4541 (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."))))
4543 (ert-deftest test-markdown-filling/line-break ()
4544 "Test filling of paragraphs with hard line breaks.
4545 See `paragraph-separate'."
4546 (markdown-test-string "Lorem ipsum dolor sit amet, \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
4547 (let ((fill-column 70))
4548 (fill-paragraph)
4549 (should (string-equal (buffer-string) "Lorem ipsum dolor sit amet, \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut\nlabore et dolore magna aliqua.")))))
4551 (ert-deftest test-markdown-filling/decimal-number-at-beginning ()
4552 "Test filling when a number with a decimal appears at the beginning of a line."
4553 (markdown-test-string "The circumference of a circle divided by it's radius is around\n3.14."
4554 (fill-paragraph)
4555 (should (string-equal (buffer-string) "The circumference of a circle divided by it's radius is around 3.14."))))
4557 (ert-deftest test-markdown-filling/avoid-unintended-list-item ()
4558 "Avoid breaking lines where it would result in an unintended list item."
4559 (markdown-test-string "Lorem ipsum dolor sit 4. amet"
4560 (let ((fill-column 22))
4561 (fill-paragraph)
4562 (should (string-equal (buffer-string) "Lorem ipsum dolor\nsit 4. amet")))))
4564 (ert-deftest test-markdown-filling/no-break-link-reference ()
4565 "Shouldn't break line between label and url, or combine two link references."
4566 (let ((str "[label1]: http://long-url.example.com\n[label2]: http://another-long-url.example.com/"))
4567 (markdown-test-string str
4568 (let ((fill-column 15)) ; after end of label, before end of URL
4569 (fill-paragraph)
4570 (should (string-equal (buffer-string) str))))))
4572 (ert-deftest test-markdown-filling/no-break-before-list-item ()
4573 "There's no point in putting the first item of a list on the next line,
4574 indented the same amount."
4575 :expected-result :failed
4576 (let ((str "* [Link](http://way-too-long.example.com)\n"))
4577 (markdown-test-string str
4578 (auto-fill-mode 1)
4579 (let ((fill-column 10))
4580 (end-of-line)
4581 (funcall auto-fill-function)
4582 (should (string-equal (buffer-string) str))))))
4584 (ert-deftest test-markdown-filling/break-within-list-item ()
4585 "This doesn't suppress auto-fill within a multi-word list item."
4586 :expected-result :failed
4587 (markdown-test-string "* [Link](http://example.com/) more text"
4588 (auto-fill-mode 1)
4589 (let ((fill-column 10))
4590 (end-of-line)
4591 (funcall auto-fill-function)
4592 (should (string-equal
4593 (buffer-string)
4594 "* [Link](http://example.com/)\n more text")))))
4596 (ert-deftest test-markdown-filling/preserve-next-line-footnote ()
4597 "Footnote block can be after label"
4598 (let ((str "[^label1]:\n Footnote block\n more footnote")) ; six spaces
4599 (markdown-test-string str
4600 (let ((fill-column 20)) ; could fit "footnote" after label, but shouldn't
4601 (fill-paragraph)
4602 (should (string-equal (buffer-string) str))))))
4604 (ert-deftest test-markdown-filling/wrap-same-line-footnote ()
4605 "Additional lines must be indented one level (four spaces) when wrapped."
4606 (markdown-test-string "[^label]: Long line should be wrapped"
4607 (let ((fill-column 25)) ; wrap before end of "should"
4608 (fill-paragraph)
4609 (should (string-equal (buffer-string) "[^label]: Long line\n should be wrapped")))))
4611 (ert-deftest test-markdown-filling/wrap-extra-hanging-indentation ()
4612 "Additional lines must be indented one level (four spaces) when wrapped."
4613 (markdown-test-string "[^label]: Long line\n should be wrapped"
4614 (let ((fill-column 25)) ; wrap before end of "should"
4615 (fill-paragraph)
4616 (should (string-equal (buffer-string) "[^label]: Long line\n should be wrapped")))))
4618 (ert-deftest test-markdown-filling/full-justification ()
4619 "Test paragraph detection with lines with lots of whitespace."
4620 (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"
4621 (setq default-justification 'full)
4622 (fill-paragraph)
4623 (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"))
4624 (backward-paragraph)
4625 (forward-paragraph)
4626 (should (= (point) 198))))
4628 (ert-deftest test-markdown-filling/list-line ()
4629 "Test fill-paragraph for list line. Don't insert bullet automatically.
4630 Detail: https://github.com/jrblevin/markdown-mode/issues/79"
4631 (markdown-test-string "* foo foo *foo* foo foo foo foo foo foo"
4632 (let ((fill-column 10))
4633 (fill-paragraph)
4634 (fill-paragraph)
4635 (forward-line 2)
4636 (back-to-indentation)
4637 (should-not (looking-at-p "\\*foo"))
4638 (forward-line 1)
4639 (back-to-indentation)
4640 (should-not (looking-at-p "\\*foo")))))
4642 (ert-deftest test-markdown-filling/ignore-header ()
4643 "# Test fill-paragraph for containing header line paragraph.
4644 https://github.com/jrblevin/markdown-mode/issues/159"
4645 (markdown-test-string "# this is header line
4646 this is not header line
4648 (let ((fill-column 10))
4649 (fill-paragraph)
4650 (should (string= (buffer-substring (point) (line-end-position)) "# this is header line")))))
4652 (ert-deftest test-markdown-filling/unclosed-square-bracket ()
4653 "Test fill-paragraph following an unclosed square bracket."
4654 (markdown-test-string "```\n[3\n```\n\naaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbb"
4655 (let ((fill-column 20))
4656 (forward-line 4)
4657 (fill-paragraph)
4658 (should (looking-at "aaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbb")))))
4660 (ert-deftest test-markdown-filling/skip-code-blocks ()
4661 "Test `markdown-fill-paragraph' on code blocks."
4662 (let ((text "test\n\n```\nhello\nworld\n```"))
4663 (markdown-test-string text
4664 (dotimes (n 5)
4665 ;; Fill at each line; buffer should not change.
4666 (fill-paragraph)
4667 (should (string-equal (buffer-string) text))))))
4669 (ert-deftest test-markdown-filling/fill-region-skip-code-blocks ()
4670 "Test `fill-region' on code blocks."
4671 (let ((text "testing\n\n```\nhello\nworld\n```\n\n123"))
4672 (markdown-test-string text
4673 ;; Fill entire buffer; buffer should not change.
4674 (fill-region (point-min) (point-max))
4675 (should (string-equal (buffer-string) text)))))
4677 (ert-deftest test-markdown-filling/fill-region-skip-code-blocks-2 ()
4678 "Test `fill-region' on a buffer with a code block with long paragraphs."
4679 (markdown-test-string "long unwrapped paragraph 1
4682 code
4683 block
4689 long unwrapped paragraph 2"
4690 ;; Test markdown-fill-forward-paragraph movement.
4691 (should (= (markdown-fill-forward-paragraph 1) 0))
4692 (should (= (point) 28)) ;; Point just after par. 1.
4693 (should (= (markdown-fill-forward-paragraph 1) 0))
4694 (should (= (point) 84)) ;; Point at end of par. 2.
4695 ;; Test filling the entire buffer with `fill-region'.
4696 (let ((fill-column 12))
4697 (fill-region (point-min) (point-max))
4698 (should (string-equal (buffer-string)
4699 "long
4700 unwrapped
4701 paragraph 1
4704 code
4705 block
4711 long
4712 unwrapped
4713 paragraph 2")))))
4715 (ert-deftest test-markdown-filling/fill-region-skip-code-blocks-3 ()
4716 "Test `fill-region' on a lone code block with no surrounding text."
4717 (let ((text "```\ncode\nblock\n```\n"))
4718 (markdown-test-string text
4719 ;; Fill entire buffer; buffer should not change.
4720 (fill-region (point-min) (point-max))
4721 (should (string-equal (buffer-string) text)))))
4723 (ert-deftest test-markdown-filling/long-paragraph-with-link ()
4724 "Test `fill-paragraph' on a long paragraph with a long link."
4725 (markdown-test-string
4726 "aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa [aaa aaa aaa aaa](aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) aaa aaa aaa aaa aaa."
4727 (fill-paragraph)
4728 (should (string-equal (buffer-string) "aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa\naaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa\naaa aaa [aaa aaa aaa aaa](aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) aaa\naaa aaa aaa aaa."))))
4730 (ert-deftest test-markdown-filling/pandoc-line-blocks ()
4731 "Filling should leave Pandoc line blocks undisturbed.
4732 This includes preserving whitespace after the pipe."
4733 (let ((text "| The limerick packs laughs anatomical
4734 | In space that is quite economical.
4735 | But the good ones I've seen
4736 | So seldom are clean
4737 | And the clean ones so seldom are comical
4739 | 200 Main St.
4740 | Berkeley, CA 94718"))
4741 (markdown-test-string text
4742 (fill-region (point-min) (point-max))
4743 (should (string-equal (buffer-string) text)))))
4745 ;;; Export tests:
4747 (ert-deftest test-markdown-hook/xhtml-standalone ()
4748 "Test `markdown-xhtml-standalone-regexp' and `markdown-output-standalone-p'."
4749 (should (string-match markdown-xhtml-standalone-regexp
4750 "<?xml version='1.0' encoding='UTF-8'?>"))
4751 (should (string-match markdown-xhtml-standalone-regexp
4752 "<!DOCTYPE html>"))
4753 (should (string-match markdown-xhtml-standalone-regexp
4754 "<html>"))
4755 (should-not (string-match markdown-xhtml-standalone-regexp
4756 "<h1>title</h1>"))
4757 (should-not (string-match markdown-xhtml-standalone-regexp
4758 "<div id=\"name\">")))
4760 ;;; Hook tests:
4762 (ert-deftest test-markdown-hook/before-export ()
4763 "Test hook run before export XHTML."
4764 (markdown-test-temp-file "lists.text"
4765 (let* ((before-hook-run nil)
4766 (orig-point (point))
4767 (func (lambda ()
4768 ;; Change value of a variable
4769 (setq before-hook-run t)
4770 ;; Insert some text
4771 (goto-char (point-min))
4772 (insert "#")
4773 ;; Deliberately move the point
4774 (end-of-line)
4775 ;; Verify changes
4776 (should (looking-back "^## List Cases" nil))
4777 (should-not (= (point) orig-point))))
4778 (ofile (progn
4779 ;; Register hook
4780 (add-hook 'markdown-before-export-hook func)
4781 ;; Export XHTML and return filename
4782 (markdown-export)))
4783 (obuffer (get-file-buffer ofile)))
4784 ;; Test side effects of hook
4785 (should (eq before-hook-run t))
4786 ;; Test position of point
4787 (should (= (point) orig-point))
4788 ;; Test that buffer was restored to original state
4789 (goto-char (point-min))
4790 (should (looking-at "^# List Cases"))
4791 ;; Clean
4792 (remove-hook 'markdown-before-export-hook func)
4793 (kill-buffer obuffer)
4794 (delete-file ofile))))
4796 (ert-deftest test-markdown-hook/after-export ()
4797 "Test hook run after export XHTML."
4798 (markdown-test-temp-file "lists.text"
4799 (let* ((after-hook-run nil)
4800 (func (lambda ()
4801 ;; Change variable value
4802 (setq after-hook-run t)
4803 ;; Add comment to output buffer
4804 (goto-char (point-min))
4805 (insert "<!-- after-export-hook -->\n")))
4806 (ofile (progn
4807 ;; Register hook
4808 (add-hook 'markdown-after-export-hook func)
4809 ;; Export XHTML and return filename
4810 (markdown-export)))
4811 (obuffer (get-file-buffer ofile)))
4812 (message "obuffer = %S" obuffer)
4813 ;; Test that variable was changed
4814 (should (eq after-hook-run t))
4815 ;; Test that output buffer remains open
4816 (should (get-buffer obuffer))
4817 ;; Test that output buffer modification remains
4818 (with-current-buffer obuffer
4819 (goto-char (point-min))
4820 (should (looking-at "<!-- after-export-hook -->\n")))
4821 ;; Test that buffer modification was saved
4822 (should-not (buffer-modified-p obuffer))
4823 ;; Clean up
4824 (remove-hook 'markdown-after-export-hook func)
4825 (kill-buffer obuffer)
4826 (delete-file ofile))))
4828 ;;; Extension: math support
4830 (ert-deftest test-markdown-math/file-local-variable ()
4831 "Test enabling math mode via `hack-local-variables-hook'."
4832 (markdown-test-file "math.text"
4833 (should-not markdown-enable-math)
4834 (hack-local-variables)
4835 (should markdown-enable-math)))
4837 (ert-deftest test-markdown-math/reload ()
4838 "Test enabling math mode via function `markdown-enable-math'."
4839 (let ((markdown-enable-math t))
4840 (markdown-test-file "math.text"
4841 ;; Flag should be set to t
4842 (should markdown-enable-math)
4843 ;; Font-lock keywords should be updated
4844 (should (member (cons markdown-regex-math-display '((1 markdown-markup-face prepend)
4845 (2 markdown-math-face append)
4846 (3 markdown-markup-face prepend)))
4847 markdown-mode-font-lock-keywords)))))
4849 (ert-deftest test-markdown-math/font-lock ()
4850 "Test markdown math mode."
4851 (let ((markdown-enable-math t))
4852 (markdown-test-file "math.text"
4853 (markdown-test-range-has-face 1 32 nil)
4854 (markdown-test-range-has-face 33 33 markdown-markup-face)
4855 (markdown-test-range-has-face 34 45 markdown-math-face)
4856 (markdown-test-range-has-face 46 46 markdown-markup-face)
4857 (markdown-test-range-has-face 47 49 nil)
4858 (markdown-test-range-has-face 50 51 markdown-markup-face)
4859 (markdown-test-range-has-face 52 63 markdown-math-face)
4860 (markdown-test-range-has-face 64 65 markdown-markup-face)
4861 (markdown-test-range-has-face 66 98 nil)
4862 (markdown-test-range-has-face 99 100 markdown-markup-face)
4863 (markdown-test-range-has-face 101 112 markdown-math-face)
4864 (markdown-test-range-has-face 113 114 markdown-markup-face)
4865 (markdown-test-range-has-face 113 114 markdown-markup-face)
4866 (markdown-test-range-has-face 117 117 markdown-header-delimiter-face)
4867 (markdown-test-range-has-face 119 152 markdown-header-face-1)
4868 (markdown-test-range-has-face 129 129 markdown-markup-face)
4869 (markdown-test-range-has-face 136 136 markdown-markup-face)
4871 (markdown-test-range-has-face 174 177 markdown-markup-face)
4872 (markdown-test-range-has-face 179 179 markdown-markup-face)
4873 (markdown-test-range-has-face 180 187 markdown-language-keyword-face)
4874 (markdown-test-range-has-face 188 188 markdown-markup-face)
4875 (markdown-test-range-has-face 190 211 markdown-pre-face)
4876 (markdown-test-range-has-face 212 215 markdown-markup-face)
4878 (markdown-test-range-has-face 218 218 markdown-markup-face)
4879 (markdown-test-range-has-face 219 223 markdown-math-face)
4880 (markdown-test-range-has-face 224 224 markdown-markup-face)
4881 (markdown-test-range-has-face 350 351 markdown-markup-face)
4882 (markdown-test-range-has-face 352 356 markdown-math-face)
4883 (markdown-test-range-has-face 357 358 markdown-markup-face)
4884 (markdown-test-range-has-face 359 391 nil)
4885 (markdown-test-range-has-face 392 393 markdown-markup-face)
4886 (markdown-test-range-has-face 394 398 markdown-math-face)
4887 (markdown-test-range-has-face 399 400 markdown-markup-face))))
4889 (ert-deftest test-markdown-math/font-lock-italics ()
4890 "Test markdown math mode with underscores."
4891 (let ((markdown-enable-math t))
4892 (markdown-test-file "math.text"
4893 (markdown-test-range-has-face 227 227 markdown-markup-face)
4894 (markdown-test-range-has-face 228 233 markdown-math-face)
4895 (markdown-test-range-has-face 234 234 markdown-markup-face)
4896 (markdown-test-range-has-face 235 270 nil)
4897 (markdown-test-range-has-face 271 271 markdown-markup-face)
4898 (markdown-test-range-has-face 272 274 markdown-math-face)
4899 (markdown-test-range-has-face 275 275 markdown-markup-face))))
4901 (ert-deftest test-markdown-math/font-lock-no-bold ()
4902 "Bold markers in math should not trigger bold."
4903 (let ((markdown-enable-math t))
4904 (markdown-test-file "math.text"
4905 (markdown-test-range-has-face 279 299 markdown-math-face)
4906 (markdown-test-range-has-face 301 308 nil)
4907 (markdown-test-range-has-face 310 312 markdown-math-face))))
4909 ;;; gfm-mode tests:
4911 (ert-deftest test-markdown-gfm/pre-1 ()
4912 "GFM pre block font lock test."
4913 (markdown-test-file-gfm "gfm.text"
4914 (markdown-test-range-has-face 2626 2637 nil)
4915 (markdown-test-range-has-face 2639 2641 markdown-markup-face)
4916 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face)
4917 (markdown-test-range-has-face 2647 2728 markdown-pre-face)
4918 (markdown-test-range-has-face 2730 2732 markdown-markup-face)))
4920 (ert-deftest test-markdown-gfm/italic-1 ()
4921 "GFM italic font lock test."
4922 (markdown-test-file-gfm "gfm.text"
4923 (markdown-test-range-has-face 1483 1483 markdown-markup-face)
4924 (markdown-test-range-has-face 1484 1487 markdown-italic-face)
4925 (markdown-test-range-has-face 1488 1488 markdown-markup-face)
4926 (markdown-test-range-has-face 1729 1790 nil)))
4928 (ert-deftest test-markdown-gfm/strike-through-1 ()
4929 "GFM strike through font lock test."
4930 (markdown-test-string-gfm "one ~~two~~ three"
4931 (markdown-test-range-has-face 1 4 nil)
4932 (markdown-test-range-has-face 5 6 markdown-markup-face)
4933 (markdown-test-range-has-face 7 9 markdown-strike-through-face)
4934 (markdown-test-range-has-face 10 11 markdown-markup-face)
4935 (markdown-test-range-has-face 12 17 nil)))
4937 (ert-deftest test-markdown-gfm/toggle-strike-through ()
4938 "Test toggling functionality of `markdown-insert-strike-through'."
4939 (markdown-test-string-gfm "one ~~two~~ three"
4940 (forward-word 2)
4941 (markdown-insert-strike-through)
4942 (should (string-equal (buffer-string) "one two three"))
4943 (should (= (point) 8))
4944 (forward-word)
4945 (markdown-insert-strike-through)
4946 (should (= (point) 16))
4947 (should (string-equal (buffer-string) "one two ~~three~~"))))
4949 (ert-deftest test-markdown-gfm/insert-code-block ()
4950 "GFM code block insertion test."
4951 ;; Test empty markup insertion
4952 (markdown-test-string-gfm "line 1\nline 2\n"
4953 (end-of-line)
4954 (markdown-insert-gfm-code-block "elisp")
4955 (should (equal (car markdown-gfm-used-languages) "elisp"))
4956 (should (equal (car (markdown-gfm-get-corpus)) "elisp"))
4957 (should (string-equal (buffer-string)
4958 "line 1\n\n``` elisp\n\n```\n\nline 2\n")))
4959 ;; Test ‘markdown-spaces-after-code-fence’.
4960 (markdown-test-string-gfm ""
4961 (let ((markdown-spaces-after-code-fence 0))
4962 (markdown-insert-gfm-code-block "elisp")
4963 (should (equal (buffer-string) "```elisp\n\n```"))))
4964 ;; Test with active region
4965 (markdown-test-string-gfm "line 1\nline 2\nline 3\n"
4966 (forward-line)
4967 (transient-mark-mode)
4968 (push-mark (point) t t)
4969 (end-of-line)
4970 (should (markdown-use-region-p))
4971 (markdown-insert-gfm-code-block "elisp")
4972 (should (string-equal (buffer-string)
4973 "line 1\n\n``` elisp\nline 2\n```\n\nline 3\n"))))
4975 (ert-deftest test-markdown-gfm/gfm-parse-buffer-for-languages ()
4976 "Parse buffer for existing languages for `markdown-gfm-used-languages' test."
4977 (markdown-test-string-gfm "``` MADEUP\n\n```\n``` LANGUAGES\n\n```\n```MaDeUp\n\n```\n```\n\n```\n``` \n\n```\n"
4978 (markdown-gfm-parse-buffer-for-languages)
4979 (should (equal markdown-gfm-used-languages
4980 (list "MaDeUp" "LANGUAGES" "MADEUP")))
4981 (should (equal (car markdown-gfm-used-languages) "MaDeUp"))
4982 (should (equal (car (markdown-gfm-get-corpus)) "MaDeUp"))
4983 (goto-char (point-max))
4984 (markdown-insert-gfm-code-block "newlang")
4985 (should (equal markdown-gfm-used-languages
4986 (list "newlang" "MaDeUp" "LANGUAGES" "MADEUP")))
4987 (should (equal (car markdown-gfm-used-languages) "newlang"))
4988 (should (equal (car (markdown-gfm-get-corpus)) "newlang"))
4989 (let ((markdown-gfm-downcase-languages nil))
4990 (should
4991 (equal (markdown-gfm-get-corpus)
4992 (append markdown-gfm-used-languages
4993 markdown-gfm-additional-languages
4994 markdown-gfm-recognized-languages))))
4995 (let ((markdown-gfm-downcase-languages t))
4996 (should
4997 (equal
4998 (markdown-gfm-get-corpus)
4999 (append markdown-gfm-used-languages
5000 (cl-mapcar #'downcase
5001 (append markdown-gfm-additional-languages
5002 markdown-gfm-recognized-languages))))))))
5004 (ert-deftest test-markdown-gfm/code-block-font-lock ()
5005 "GFM code block font lock test."
5006 (markdown-test-file-gfm "gfm.text"
5007 (markdown-test-range-has-face 2639 2641 markdown-markup-face) ; ```
5008 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face) ; lang
5009 (markdown-test-range-has-face 2647 2728 markdown-pre-face) ; code
5010 (markdown-test-range-has-face 2730 2732 markdown-markup-face))) ; ```
5012 (ert-deftest test-markdown-gfm/code-block-font-lock-2 ()
5013 "GFM code block font lock test without language identifier."
5014 (markdown-test-string-gfm "Plain code block:\n\n```\nfoo\n```\n"
5015 (markdown-test-range-has-face 20 22 markdown-markup-face)
5016 (markdown-test-range-has-face 24 26 markdown-pre-face)
5017 (markdown-test-range-has-face 28 30 markdown-markup-face)))
5019 ;;; Tests for other extensions:
5021 (ert-deftest test-markdown-ext/pandoc-fancy-lists ()
5022 "Test basic support for font lock and filling of Pandoc 'fancy lists'."
5023 (markdown-test-string " #. abc\ndef\n"
5024 ;; font lock
5025 (markdown-test-range-has-face 1 1 nil)
5026 (markdown-test-range-has-face 2 3 markdown-list-face)
5027 (markdown-test-range-has-face 4 11 nil)
5028 ;; filling
5029 (forward-line)
5030 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
5031 (should (string-equal (buffer-string) " #. abc\n def\n"))
5032 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
5033 (should (string-equal (buffer-string) " #. abc\n def\n"))))
5035 (ert-deftest test-markdown-ext/wiki-link-rules ()
5036 "Test wiki link search rules and font lock for missing pages."
5037 (let ((markdown-enable-wiki-links t)
5038 (markdown-wiki-link-fontify-missing t)
5039 (markdown-wiki-link-search-subdirectories t)
5040 (markdown-wiki-link-search-parent-directories t))
5041 (progn
5042 (find-file "wiki/root")
5043 (unwind-protect
5044 (progn
5045 (markdown-mode)
5046 ;; search rules
5047 (should (string-match-p
5048 "/sub/foo$"
5049 (markdown-convert-wiki-link-to-filename "foo")))
5050 (should (string-equal
5051 (markdown-convert-wiki-link-to-filename "doesnotexist")
5052 "doesnotexist"))
5053 ;; font lock
5054 (markdown-test-range-has-property 1 11 'font-lock-face markdown-link-face)
5055 (markdown-test-range-has-property 14 33 'font-lock-face markdown-missing-link-face)
5056 (markdown-test-range-has-property 36 42 'font-lock-face markdown-link-face)
5057 (markdown-test-range-has-property 45 60 'font-lock-face markdown-missing-link-face))
5058 (kill-buffer)))
5059 (progn
5060 (find-file "wiki/sub/foo")
5061 (unwind-protect
5062 (progn
5063 (markdown-mode)
5064 ;; search rules
5065 (should (string-match-p
5066 "/wiki/root$"
5067 (markdown-convert-wiki-link-to-filename "root")))
5068 (should (string-equal
5069 (markdown-convert-wiki-link-to-filename "doesnotexist")
5070 "doesnotexist"))
5071 ;; font lock
5072 (markdown-test-range-has-property 1 16 'font-lock-face markdown-missing-link-face)
5073 (markdown-test-range-has-property 19 26 'font-lock-face markdown-link-face))
5074 (kill-buffer)))))
5076 (defadvice markdown-live-preview-window-eww
5077 (around markdown-test-create-fake-eww disable)
5078 (setq ad-return-value (get-buffer-create "*eww*")))
5080 (defmacro markdown-test-fake-eww (&rest body)
5081 `(progn
5082 ,@(if (and (fboundp 'libxml-parse-html-region) (require 'eww nil t)) body
5083 `((ad-enable-advice #'markdown-live-preview-window-eww
5084 'around 'markdown-test-create-fake-eww)
5085 (ad-activate #'markdown-live-preview-window-eww)
5086 ,@body
5087 (ad-disable-advice #'markdown-live-preview-window-eww
5088 'around 'markdown-test-create-fake-eww)
5089 (ad-activate #'markdown-live-preview-window-eww)))))
5091 (defmacro markdown-test-eww-or-nothing (test &rest body)
5092 (if (and (fboundp 'libxml-parse-html-region) (require 'eww nil t)
5093 (executable-find markdown-command))
5094 `(progn ,@body)
5095 (message "no eww, no libxml2, or no %s found: skipping %s" markdown-command test)
5096 nil))
5098 (ert-deftest test-markdown-ext/live-preview-no-file ()
5099 "Live-preview a `markdown-mode' buffer without a file."
5100 (with-temp-buffer
5101 (markdown-mode)
5103 ;; Activating `markdown-live-preview-mode' signals error
5104 (should-error (markdown-live-preview-mode))
5106 ;; After trying to activate live preview mode, mode is not activated
5107 (should-not markdown-live-preview-mode)
5109 ;; `markdown-live-preview-export' does nothing
5110 (should-not (markdown-live-preview-export))
5112 ;; `markdown-live-preview-remove' does nothing
5113 (should-not (markdown-live-preview-remove))))
5115 (ert-deftest test-markdown-ext/live-preview-exports ()
5116 (markdown-test-temp-file "inline.text"
5117 (unless (and (fboundp 'libxml-parse-html-region) (require 'eww nil t))
5118 (should-error (markdown-live-preview-mode)))
5119 (markdown-test-fake-eww
5120 (markdown-live-preview-mode)
5121 (should (buffer-live-p markdown-live-preview-buffer))
5122 (should (eq (current-buffer)
5123 (with-current-buffer markdown-live-preview-buffer
5124 markdown-live-preview-source-buffer)))
5125 (kill-buffer markdown-live-preview-buffer)
5126 (should (null markdown-live-preview-buffer))
5127 (set-buffer-modified-p t)
5128 (save-buffer) ; should create new export
5129 (should (buffer-live-p markdown-live-preview-buffer)))))
5131 (ert-deftest test-markdown-ext/live-preview-delete-exports ()
5132 (markdown-test-fake-eww
5133 (let ((markdown-live-preview-delete-export 'delete-on-destroy)
5134 file-output)
5135 (markdown-test-temp-file "inline.text"
5136 (markdown-live-preview-mode)
5137 (setq file-output (markdown-export-file-name)))
5138 (should-not (file-exists-p file-output)))
5139 (let ((markdown-live-preview-delete-export 'delete-on-export)
5140 file-output)
5141 (markdown-test-temp-file "inline.text"
5142 (markdown-live-preview-mode)
5143 (setq file-output (markdown-export-file-name))
5144 (should-not (file-exists-p file-output))))
5145 (let ((markdown-live-preview-delete-export nil)
5146 file-output)
5147 (unwind-protect
5148 (markdown-test-temp-file "inline.text"
5149 (markdown-live-preview-mode)
5150 (setq file-output (markdown-export-file-name))
5151 (should (file-exists-p file-output)))
5152 (delete-file file-output)))))
5154 (ert-deftest test-markdown-ext/live-preview-follow-min-max ()
5155 (markdown-test-eww-or-nothing "live-preview-follow-min-max"
5156 (markdown-test-temp-file "inline.text"
5157 (markdown-live-preview-mode)
5158 (should (buffer-live-p markdown-live-preview-buffer))
5159 (should (window-live-p (get-buffer-window markdown-live-preview-buffer)))
5160 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
5161 (goto-char (point-min)))
5162 (goto-char (point-min))
5163 (insert "a test ")
5164 (markdown-live-preview-export)
5165 (let (final-pt final-win-st-diff)
5166 ;; test that still starts at point-min
5167 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
5168 (should (= (window-point) 1))
5169 (should (= (markdown-visual-lines-between-points
5170 (window-start) (window-point))
5172 (set-window-point (selected-window) (point-max))
5173 (setq final-pt (window-point)
5174 final-win-st-diff (markdown-visual-lines-between-points
5175 (window-start) (window-point))))
5176 (goto-char (point-min))
5177 (insert "this is ")
5178 (markdown-live-preview-export)
5179 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
5180 (should (= (window-point) (+ final-pt (length "this is "))))
5181 (should (= (markdown-visual-lines-between-points
5182 (window-start) (window-point))
5183 final-win-st-diff))
5184 ;; test that still starts at point-max, with correct line difference
5185 (goto-char (floor (/ (float (- (point-max) (point-min))) 2)))
5186 (setq final-pt (window-point)
5187 final-win-st-diff (markdown-visual-lines-between-points
5188 (window-start) final-pt)))
5189 (markdown-live-preview-export)
5190 ;; test that still starts at same point, with correct line difference
5191 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
5192 (should (= (window-point) final-pt))
5193 (should (= (markdown-visual-lines-between-points
5194 (window-start) (window-point))
5195 final-win-st-diff)))))))
5197 ;; Tests for imenu
5199 (ert-deftest test-markdown-imenu/metadata ()
5200 "Don't correct header like statement in metadata.
5201 https://github.com/jrblevin/markdown-mode/issues/145"
5202 (markdown-test-string "---
5203 title = \"Blah\"
5204 comments = false
5207 # Header1
5209 ## Header2
5211 (let ((headers (mapcar #'car (markdown-imenu-create-flat-index))))
5212 (should (member "Header1" headers))
5213 (should (member "Header2" headers))
5214 (should-not (member "comments = false" headers)))))
5216 (provide 'markdown-test)
5218 ;;; markdown-test.el ends here