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