Basic font lock and filling for definition lists
[markdown-mode.git] / tests / markdown-test.el
blob349eb9cea4d3bd127fcdf501a34928930e21d72e
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 ()
1140 "Basic tests for `markdown-insert-link'."
1141 ;; Test empty markup insertion (leave point in square brackets)
1142 (markdown-test-string "abc "
1143 (end-of-line)
1144 (call-interactively 'markdown-insert-link)
1145 (should (string-equal (buffer-string) "abc []()"))
1146 (should (= (point) 6)))
1147 ;; Test with word at point (leave point in parentheses)
1148 (markdown-test-string "abc def ghi"
1149 (forward-word 2)
1150 (call-interactively 'markdown-insert-link)
1151 (should (string-equal (buffer-string) "abc [def]() ghi"))
1152 (should (= (point) 11)))
1153 ;; Test with region (leave point in parentheses)
1154 (markdown-test-string "abc def ghi"
1155 (transient-mark-mode)
1156 (push-mark (point) t t)
1157 (forward-word 2)
1158 (call-interactively 'markdown-insert-link)
1159 (should (string-equal (buffer-string) "[abc def]() ghi"))
1160 (should (= (point) 11))))
1162 ;;; Footnote tests:
1164 (ert-deftest test-markdown-footnote/basic-end ()
1165 "Basic footnote insertion and deletion tests for 'end location."
1166 (let ((markdown-footnote-location 'end))
1167 (markdown-test-string "first line\nsecond line\n"
1168 ;; new buffer with no footnotes
1169 (should (= markdown-footnote-counter 0))
1170 ;; footnote insertion
1171 (end-of-line)
1172 (markdown-insert-footnote)
1173 (should (= (point) 35))
1174 (should (= markdown-footnote-counter 1))
1175 (should (looking-back "\\[^1\\]: " nil))
1176 ;; kill with point in footnote definition
1177 (insert "footnote text")
1178 (let (kill-ring)
1179 (markdown-footnote-kill))
1180 (should (= (point) 24))
1181 (should (bolp))
1182 (should (string-equal (buffer-string) "first line\nsecond line\n"))
1183 ;; insertion, counter should increment
1184 (goto-char (point-min))
1185 (end-of-line)
1186 (markdown-insert-footnote)
1187 (should (= (point) 35))
1188 (should (= markdown-footnote-counter 2))
1189 (should (looking-back "\\[^2\\]: " nil))
1190 (insert "footnote text")
1191 ;; return to marker
1192 (markdown-footnote-return)
1193 (should (= (point) 15))
1194 (should (looking-back "\\[^2\\]" nil))
1195 ;; kill with point at marker
1196 (let (kill-ring)
1197 (markdown-footnote-kill))
1198 (should (= (point) 11))
1199 (should (eolp))
1200 (should (string-equal (buffer-string) "first line\nsecond line\n")))))
1202 (ert-deftest test-markdown-footnote/basic-immediately ()
1203 "Basic footnote insertion and deletion tests for 'immediately location."
1204 (let ((markdown-footnote-location 'immediately))
1205 (markdown-test-string "first paragraph\n\nsecond paragraph\n"
1206 ;; new buffer with no footnotes
1207 (should (= markdown-footnote-counter 0))
1208 ;; footnote insertion
1209 (end-of-line)
1210 (markdown-insert-footnote)
1211 (should (= (point) 28))
1212 (should (= markdown-footnote-counter 1))
1213 (should (looking-back "\\[^1\\]: " nil))
1214 ;; kill with point in footnote definition
1215 (insert "footnote text")
1216 (let (kill-ring)
1217 (markdown-footnote-kill))
1218 (should (= (point) 18))
1219 (should (bolp))
1220 (should (string-equal (buffer-string)
1221 "first paragraph\n\nsecond paragraph\n")))))
1223 (ert-deftest test-markdown-footnote/basic-header ()
1224 "Basic footnote insertion and deletion tests for 'header location."
1225 (let ((markdown-footnote-location 'header))
1226 (markdown-test-string "par one\n\npar two\n\n### header\n"
1227 ;; new buffer with no footnotes
1228 (should (= markdown-footnote-counter 0))
1229 ;; footnote insertion
1230 (end-of-line)
1231 (markdown-insert-footnote)
1232 (should (= (point) 29))
1233 (should (= markdown-footnote-counter 1))
1234 (should (looking-back "\\[^1\\]: " nil))
1235 ;; kill with point in footnote definition
1236 (insert "footnote text")
1237 (let (kill-ring)
1238 (markdown-footnote-kill))
1239 (should (= (point) 19))
1240 (should (bolp))
1241 (should (string-equal (buffer-string)
1242 "par one\n\npar two\n\n### header\n"))
1243 ;; insertion, counter should increment
1244 (goto-char (point-min))
1245 (end-of-line)
1246 (markdown-insert-footnote)
1247 (should (= (point) 29))
1248 (should (= markdown-footnote-counter 2))
1249 (should (looking-back "\\[^2\\]: " nil))
1250 (insert "footnote text")
1251 ;; return to marker
1252 (markdown-footnote-return)
1253 (should (= (point) 12))
1254 (should (looking-back "\\[^2\\]" nil))
1255 ;; kill with point at marker
1256 (let (kill-ring)
1257 (markdown-footnote-kill))
1258 (should (= (point) 8))
1259 (should (eolp))
1260 (should (string-equal (buffer-string)
1261 "par one\n\npar two\n\n### header\n")))))
1263 (ert-deftest test-markdown-footnote/kill-empty-text ()
1264 "Test killing a footnote with marker but no text."
1265 (markdown-test-string "no text[^1]\n\n[^1]: \n"
1266 (end-of-line)
1267 (markdown-footnote-goto-text)
1268 (should (looking-back "\\[^1\\]: " nil))
1269 (let (kill-ring)
1270 (markdown-footnote-kill))
1271 (should (string-equal (buffer-string) "no text\n"))))
1273 (ert-deftest test-markdown-footnote/kill-empty-after ()
1274 "Test killing an empty footnote after one with text (previously killed the
1275 footnote with text above)."
1276 (markdown-test-string "[^with-text][^no-text]\n\n[^with-text]: Text\n[^no-text]:"
1277 (let (kill-ring)
1278 (forward-line 3)
1279 (should (looking-at "\\[\\^no-text\\]:$"))
1280 (markdown-footnote-kill)
1281 (should (string-equal (current-kill 0) "")))))
1283 (ert-deftest test-markdown-footnote/kill-hanging-paras ()
1284 "Test killing a footnote where block text starts after the label (previously
1285 killed the footnote above)."
1286 (markdown-test-string "[^1][^2]\n\n[^1]: Foo\n\n[^2]:\n Text\n\n More text\n\n\nNot indented"
1287 (let (kill-ring)
1288 (forward-line 4)
1289 (should (looking-at "\\[\\^2\\]:$"))
1290 (markdown-footnote-kill)
1291 ;; We want to include the leading space on hanging footnote paragraphs,
1292 ;; even if a hanging paragraph is the first item in the footnote.
1293 (should (string-equal (current-kill 0) "Text\n\n More text\n")))))
1295 (ert-deftest test-markdown-footnote/text-positions-buffer-top ()
1296 "Test markdown-footnote-text-positions on footnote adjacent to buffer top
1297 (was infinite loop)."
1298 (markdown-test-string "[^label]: text\n more text"
1299 (should (equal (markdown-footnote-text-positions) (list "^label" 1 29)))))
1301 (ert-deftest test-markdown-footnote/text-positions-buffer-top-one-line ()
1302 "Test markdown-footnote-text-positions on one-line footnote adjacent to
1303 buffer top (failed to find positions)."
1304 (markdown-test-string "[^label]: text\n"
1305 (should (equal (markdown-footnote-text-positions) (list "^label" 1 16)))))
1307 (ert-deftest test-markdown-footnote/text-positions-buffer-top-not-footnote ()
1308 "Test markdown-footnote-text-positions on plain paragraph adjacent to buffer
1309 top (was infinite loop)."
1310 (markdown-test-string "text\n more text\n"
1311 (should (eq (markdown-footnote-text-positions) nil))))
1313 (ert-deftest test-markdown-footnote/text-positions-buffer-bottom ()
1314 "Test markdown-footnote-text-positions on footnote adjacent to buffer bottom
1315 (was infinite loop)."
1316 (markdown-test-string "\n[^label]: text\n more text"
1317 (forward-line 1)
1318 (should (equal (markdown-footnote-text-positions) (list "^label" 2 30)))))
1320 (ert-deftest test-markdown-footnote/kill-adjacent-footnote ()
1321 "Test killing a footnote adjacent to other one-line footnotes (previously
1322 killed the wrong one)."
1323 (markdown-test-string "Text[^1] with[^2] footnotes[^3]\n\n[^1]: foo\n[^2]: bar\n[^3]: baz"
1324 (let (kill-ring)
1325 (forward-line 3)
1326 (should (looking-at "\\[\\^2\\]: bar"))
1327 (markdown-footnote-kill)
1328 (should (string-equal (current-kill 0) "bar\n")))))
1330 (ert-deftest test-markdown-footnote/kill-adjacent-markers ()
1331 "Test killing a footnote where the labels are adjacent (previously, the wrong
1332 footnote would be killed because the attempt to jump to the marker would jump to
1333 the opening bracket of [^2], and then subsequent functions would kill [^2])."
1334 (markdown-test-string "Text with footnotes[^1][^2]\n\n[^1]: foo\n\n[^2]: bar\n"
1335 (let (kill-ring)
1336 (forward-line 2)
1337 (should (looking-at "\\[\\^1\\]: foo"))
1338 (markdown-footnote-kill)
1339 (should (string-equal (current-kill 0) "foo\n")))))
1341 (when (version< emacs-version "24.2")
1342 ;; fix segfault on 24.1 with the normal implementation of this function. isn't
1343 ;; exactly correct, but should make tests work the same
1344 (defadvice kill-buffer-and-window (around markdown-test-fix-segfault activate)
1345 (kill-buffer)
1346 (select-window (previous-window))))
1348 (ert-deftest test-markdown-footnote-reference/jump ()
1349 "Test `markdown-jump' for footnotes and reference links."
1350 (markdown-test-string
1351 "body[^1], [link 1][ref],
1352 [link 2][ref]
1354 [^1]: footnote
1356 [ref]: https://duckduckgo.com/"
1357 (goto-char 5) ; start of [^1]
1358 (markdown-jump) ; markdown-footnote-goto-text
1359 (should (looking-at "footnote"))
1360 (markdown-jump) ; markdown-footnote-return
1361 (should (= (point) 9)) ; just after [^1]
1362 (markdown-next-link) ; beginning of [link 1][]
1363 (markdown-jump)
1364 (should (looking-at "https://duckduckgo.com/"))
1365 (should (equal (markdown-reference-find-links "ref")
1366 (list (list "link 2" 26 2) (list "link 1" 11 1))))
1367 (markdown-jump) ; opens a reference link buffer
1368 (should (string= (buffer-string) "Links using reference ref:\n\nlink 1 (line 1)\nlink 2 (line 2)\n"))
1369 (should (looking-at "link 1")) ; in reference link popop buffer
1370 (execute-kbd-macro (read-kbd-macro "RET")) ; jump to "link 1"
1371 (should (looking-at "\\[link 1\\]")) ; back in main buffer
1372 (should (= (point) 11))))
1374 ;;; Element removal tests:
1376 (ert-deftest test-markdown-kill/simple ()
1377 "Simple tests for `markdown-kill-thing-at-point'."
1378 (let ((kill-ring nil)
1379 (tests (list '("`foo`" . "foo")
1380 '("## foo ##" . "foo")
1381 '("## foo" . "foo")
1382 '("foo\n---" . "foo")
1383 '("foo\n===" . "foo")
1384 '("* * * * *" . "* * * * *")
1385 '("[foo](http://bar.com/)" . "foo")
1386 '("![foo](http://bar.com/)" . "foo")
1387 '("[foo][bar]" . "foo")
1388 '("![foo][bar]" . "foo")
1389 '("<http://foo.com/>" . "http://foo.com/")
1390 '("<foo@bar.com>" . "foo@bar.com")
1391 '("**foo**" . "foo")
1392 '("__foo__" . "foo")
1393 '("*foo*" . "foo")
1394 '("_foo_" . "foo")
1395 '(" [foo]: http://bar.com/" . "http://bar.com/")
1396 '(" [foo]: http://bar.com/ \"title\"" . "http://bar.com/")
1397 '("foo[^bar]\n\n[^bar]: baz" . "baz")
1398 '("[^bar]: baz" . "baz")
1399 '(" * foo\n bar" . " * foo\n bar"))))
1400 (dolist (test tests)
1401 ;; Load test string (the car), move to end of first line, kill
1402 ;; thing at point, and then verify that the kill ring contains cdr.
1403 (markdown-test-string (car test)
1404 (end-of-line)
1405 (call-interactively 'markdown-kill-thing-at-point)
1406 (should (string-equal (current-kill 0) (cdr test)))))))
1408 (ert-deftest test-markdown-kill/footnote-text ()
1409 "Test killing a footnote with point at footnote text."
1410 (markdown-test-string "some text[^1]\n\n[^1]: footnote\n"
1411 (end-of-line)
1412 (markdown-footnote-goto-text)
1413 (let (kill-ring)
1414 (markdown-footnote-kill))
1415 (should (string-equal (buffer-string) "some text\n"))))
1417 (ert-deftest test-markdown-kill/code ()
1418 "Test killing with code regex.."
1419 (let ((kill-ring nil))
1420 (markdown-test-string "Lorem `ipsum` dolor `sit` `amet`."
1421 (goto-char 22) ; position point at s in `sit`
1422 (call-interactively 'markdown-kill-thing-at-point)
1423 (should (string-equal (current-kill 0) "sit")))))
1425 ;;; Completion:
1427 (ert-deftest test-markdown-complete/atx-header-incomplete ()
1428 "Test `markdown-incomplete-atx-p'."
1429 (markdown-test-string "### ###"
1430 (should (looking-at markdown-regex-header-atx))
1431 (should-not (markdown-incomplete-atx-p)))
1432 (markdown-test-string "###abc###"
1433 (should-not (looking-at markdown-regex-header-atx)))
1434 (markdown-test-string "### ###"
1435 (should (looking-at markdown-regex-header-atx))
1436 (should (markdown-incomplete-atx-p))))
1438 (ert-deftest test-markdown-complete/atx-header ()
1439 "Test `markdown-complete' for atx headers."
1440 (markdown-test-string "##### test"
1441 (call-interactively 'markdown-complete)
1442 (should (string-equal (buffer-string) "##### test #####"))))
1444 (ert-deftest test-markdown-complete/setext-header-incomplete ()
1445 "Test `markdown-incomplete-setext-p'."
1446 (markdown-test-string "abc\n===\n"
1447 (should (looking-at markdown-regex-header-setext))
1448 (should-not (markdown-incomplete-setext-p)))
1449 (markdown-test-string "abc\n==\n"
1450 (should (looking-at markdown-regex-header-setext))
1451 (should (markdown-incomplete-setext-p)))
1452 (markdown-test-string "abc\n====\n"
1453 (should (looking-at markdown-regex-header-setext))
1454 (should (markdown-incomplete-setext-p))))
1456 (ert-deftest test-markdown-complete/setext-header ()
1457 "Test `markdown-complete' for setext headers."
1458 (markdown-test-string "test \n=="
1459 (call-interactively 'markdown-complete)
1460 (should (string-equal (buffer-string) "test\n===="))))
1462 (ert-deftest test-markdown-complete/hr-incomplete ()
1463 "Test `markdown-incomplete-hr-p'."
1464 (dolist (i (number-sequence 0 (1- (length markdown-hr-strings))))
1465 (markdown-test-string (nth i markdown-hr-strings)
1466 (should (looking-at markdown-regex-hr))
1467 (should-not (markdown-incomplete-hr-p))
1468 (should-error (call-interactively 'markdown-complete)))))
1470 (ert-deftest test-markdown-complete/hr ()
1471 "Test completion via `markdown-complete' for horizontal rules."
1472 (markdown-test-string "- - - - -"
1473 (call-interactively 'markdown-complete)
1474 (should (string-equal (buffer-string) (car markdown-hr-strings)))))
1476 (ert-deftest test-markdown-complete/buffer-setext-2 ()
1477 "Test `markdown-complete-buffer' for level two setext heading."
1478 ;; Ensure markdown-complete-buffer doesn't mistake this for a horizontal rule
1479 (markdown-test-string "Subheading\n--\n"
1480 (call-interactively 'markdown-complete-buffer)
1481 (should (string-equal (buffer-string) "Subheading\n----------\n\n")))
1482 (markdown-test-string "Abc\n--\n\nDef\n--\n"
1483 (call-interactively 'markdown-complete-buffer)
1484 (should (string-equal (buffer-string) "Abc\n---\n\nDef\n---\n\n"))))
1486 ;;; Promotion and demotion tests:
1488 (ert-deftest test-markdown-promote/atx-header ()
1489 "Test `markdown-promote' for atx headers."
1490 (markdown-test-string "###### test ######"
1491 (markdown-promote)
1492 (should (string-equal (buffer-string) "##### test #####"))
1493 (markdown-promote)
1494 (should (string-equal (buffer-string) "#### test ####"))
1495 (markdown-promote)
1496 (should (string-equal (buffer-string) "### test ###"))
1497 (markdown-promote)
1498 (should (string-equal (buffer-string) "## test ##"))
1499 (markdown-promote)
1500 (should (string-equal (buffer-string) "# test #"))))
1502 (ert-deftest test-markdown-demote/atx-header ()
1503 "Test `markdown-demote' for atx headers."
1504 (markdown-test-string "# test #"
1505 (markdown-demote)
1506 (should (string-equal (buffer-string) "## test ##"))
1507 (markdown-demote)
1508 (should (string-equal (buffer-string) "### test ###"))
1509 (markdown-demote)
1510 (should (string-equal (buffer-string) "#### test ####"))
1511 (markdown-demote)
1512 (should (string-equal (buffer-string) "##### test #####"))
1513 (markdown-demote)
1514 (should (string-equal (buffer-string) "###### test ######"))))
1516 (ert-deftest test-markdown-promote/setext-header ()
1517 "Test `markdown-promote' for setext headers."
1518 (markdown-test-string "test\n----"
1519 (markdown-promote)
1520 (should (string-equal (buffer-string) "test\n===="))))
1522 (ert-deftest test-markdown-demote/setext-header ()
1523 "Test `markdown-demote' for setext headers."
1524 (markdown-test-string "test\n===="
1525 (markdown-demote)
1526 (should (string-equal (buffer-string) "test\n----"))
1527 (markdown-demote)
1528 (should (string-equal (buffer-string) "### test ###"))
1529 (markdown-demote)
1530 (should (string-equal (buffer-string) "#### test ####"))
1531 (markdown-demote)
1532 (should (string-equal (buffer-string) "##### test #####"))
1533 (markdown-demote)
1534 (should (string-equal (buffer-string) "###### test ######"))))
1536 (ert-deftest test-markdown-promote/hr ()
1537 "Test `markdown-promote' for horizontal rules."
1538 (markdown-test-string (car (reverse markdown-hr-strings))
1539 (dolist (n (number-sequence 4 0 -1))
1540 (markdown-promote)
1541 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))
1543 (ert-deftest test-markdown-demote/hr ()
1544 "Test `markdown-demote' for horizontal rules."
1545 (markdown-test-string (car markdown-hr-strings)
1546 (dolist (n (number-sequence 1 5))
1547 (markdown-demote)
1548 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))
1550 (ert-deftest test-markdown-promote/bold ()
1551 "Test `markdown-promote' for bold markup."
1552 (markdown-test-string "__bold__"
1553 (call-interactively 'markdown-promote)
1554 (should (string-equal (buffer-string) "**bold**"))))
1556 (ert-deftest test-markdown-demote/bold ()
1557 "Test `markdown-demote' for bold markup."
1558 (markdown-test-string "**bold**"
1559 (call-interactively 'markdown-promote)
1560 (should (string-equal (buffer-string) "__bold__"))))
1562 (ert-deftest test-markdown-promote/italic ()
1563 "Test `markdown-promote' for italic markup."
1564 (markdown-test-string "_italic_"
1565 (call-interactively 'markdown-promote)
1566 (should (string-equal (buffer-string) "*italic*"))))
1568 (ert-deftest test-markdown-demote/italic ()
1569 "Test `markdown-demote' for italic markup."
1570 (markdown-test-string "*italic*"
1571 (call-interactively 'markdown-promote)
1572 (should (string-equal (buffer-string) "_italic_"))))
1574 ;;; Subtree editing tests:
1576 (ert-deftest test-markdown-subtree/promote ()
1577 "Test `markdown-promote-subtree'."
1578 (markdown-test-string "# h1 #\n\n## h2 ##\n\n### h3 ###\n\n## h2 ##\n\n# h1 #\n"
1579 ;; The first h1 should get promoted away.
1580 ;; The second h1 should not be promoted.
1581 (markdown-promote-subtree)
1582 (should (string-equal (buffer-string) "h1\n\n# h2 #\n\n## h3 ##\n\n# h2 #\n\n# h1 #\n"))
1583 ;; Second call should do nothing since point is no longer at a heading.
1584 (markdown-promote-subtree)
1585 (should (string-equal (buffer-string) "h1\n\n# h2 #\n\n## h3 ##\n\n# h2 #\n\n# h1 #\n"))
1586 ;; Move to h2 and promote again.
1587 (forward-line 2)
1588 (markdown-promote-subtree)
1589 (should (string-equal (buffer-string) "h1\n\nh2\n\n# h3 #\n\n# h2 #\n\n# h1 #\n"))))
1591 (ert-deftest test-markdown-subtree/demote ()
1592 "Test `markdown-demote-subtree'."
1593 (markdown-test-string "# h1 #\n\n## h2 ##\n\n### h3 ###\n\n## h2 ##\n\n# h1 #\n"
1594 ;; The second h1 should not be demoted
1595 (markdown-demote-subtree)
1596 (should (string-equal (buffer-string) "## h1 ##\n\n### h2 ###\n\n#### h3 ####\n\n### h2 ###\n\n# h1 #\n"))
1597 (markdown-demote-subtree)
1598 (should (string-equal (buffer-string) "### h1 ###\n\n#### h2 ####\n\n##### h3 #####\n\n#### h2 ####\n\n# h1 #\n"))
1599 (markdown-demote-subtree)
1600 (should (string-equal (buffer-string) "#### h1 ####\n\n##### h2 #####\n\n###### h3 ######\n\n##### h2 #####\n\n# h1 #\n"))
1601 ;; Stop demoting at level six
1602 (markdown-demote-subtree)
1603 (should (string-equal (buffer-string) "##### h1 #####\n\n###### h2 ######\n\n###### h3 ######\n\n###### h2 ######\n\n# h1 #\n"))
1604 (markdown-demote-subtree)
1605 (should (string-equal (buffer-string) "###### h1 ######\n\n###### h2 ######\n\n###### h3 ######\n\n###### h2 ######\n\n# h1 #\n"))))
1607 (ert-deftest test-markdown-subtree/move-up ()
1608 "Test `markdown-move-subtree-up'."
1609 ;; Note that prior to Emacs 24.5, this does not work for the last subtree in
1610 ;; the buffer due to Emacs bug #19102:
1611 ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19102
1612 ;; https://github.com/emacs-mirror/emacs/commit/b3910f
1613 ;; That also corrects the type of the "Cannot move pase superior level" error
1614 ;; from 'error to 'user-error.
1615 (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"
1616 (re-search-forward "^# 2")
1617 (markdown-move-subtree-up)
1618 (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"))
1619 ;; Second attempt should fail, leaving buffer unchanged.
1620 ;; (This way of asserting the contents of the error
1621 ;; message is a bit convoluted and more fragile than
1622 ;; ideal. But prior to Emacs 24.5, the type of this
1623 ;; error is just 'error, and a bare "should-error" is
1624 ;; really overly broad.)
1625 (should (string-equal
1626 "Cannot move past superior level"
1627 (cl-second (should-error (markdown-move-subtree-up)))))))
1629 (ert-deftest test-markdown-subtree/move-down ()
1630 "Test `markdown-move-subtree-down'."
1631 (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"
1632 (re-search-forward "^## 1\.1")
1633 (markdown-move-subtree-down)
1634 (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"))))
1636 ;(ert-deftest test-markdown-subtree/move-down ()
1638 ;;; Cycling:
1640 (ert-deftest test-markdown-cycle/atx-header ()
1641 "Test `markdown-demote' cycling for atx headers."
1642 (markdown-test-string "##### test"
1643 (call-interactively 'markdown-demote)
1644 (should (string-equal (buffer-string) "###### test ######"))
1645 (call-interactively 'markdown-demote)
1646 (should (string-equal (buffer-string) "# test #"))
1647 (call-interactively 'markdown-demote)
1648 (should (string-equal (buffer-string) "## test ##"))))
1650 (ert-deftest test-markdown-cycle/setext-header ()
1651 "Test `markdown-demote' cycling for setext headers."
1652 (markdown-test-string "test\n===="
1653 (call-interactively 'markdown-demote)
1654 (should (string-equal (buffer-string) "test\n----"))
1655 (call-interactively 'markdown-demote)
1656 (should (string-equal (buffer-string) "### test ###"))
1657 (call-interactively 'markdown-demote)
1658 (should (string-equal (buffer-string) "#### test ####"))
1659 (call-interactively 'markdown-demote)
1660 (should (string-equal (buffer-string) "##### test #####"))
1661 (call-interactively 'markdown-demote)
1662 (should (string-equal (buffer-string) "###### test ######"))
1663 (call-interactively 'markdown-demote)
1664 (should (string-equal (buffer-string) "# test #"))))
1666 (ert-deftest test-markdown-cycle/hr ()
1667 "Test cycling of horizontal rules."
1668 ;; Cycle using markdown-demote
1669 (markdown-test-string (car markdown-hr-strings)
1670 (dolist (n (number-sequence 1 5))
1671 (call-interactively 'markdown-demote)
1672 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
1673 (call-interactively 'markdown-demote)
1674 (should (string-equal (buffer-string) (car markdown-hr-strings))))
1675 ;; Cycle using markdown-promote
1676 (markdown-test-string (car (reverse markdown-hr-strings))
1677 (dolist (n (number-sequence 4 0 -1))
1678 (call-interactively 'markdown-promote)
1679 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
1680 (call-interactively 'markdown-promote)
1681 (should (string-equal (buffer-string) (car (reverse markdown-hr-strings))))))
1683 (ert-deftest test-markdown-cycle/bold ()
1684 "Test cycling of bold markup."
1685 (markdown-test-string "**bold**"
1686 (call-interactively 'markdown-demote)
1687 (should (string-equal (buffer-string) "__bold__"))
1688 (call-interactively 'markdown-demote)
1689 (should (string-equal (buffer-string) "**bold**"))))
1691 (ert-deftest test-markdown-cycle/italic ()
1692 "Test cycling of italic markup."
1693 (markdown-test-string "*italic*"
1694 (call-interactively 'markdown-demote)
1695 (should (string-equal (buffer-string) "_italic_"))
1696 (call-interactively 'markdown-demote)
1697 (should (string-equal (buffer-string) "*italic*"))))
1699 ;;; Indentation tests:
1701 (ert-deftest test-markdown-indentation/calc-indents ()
1702 "Test `markdown-calc-indents' a nested list context."
1703 (markdown-test-file "nested-list.text"
1704 (goto-char (point-max))
1705 (let ((indents (markdown-calc-indents)))
1706 (should (= (car indents) 17)) ; indentation of previous line first
1707 (should (equal (sort indents '<)
1708 (list
1709 0 ; beginning of line
1710 3 ; first-level list marker
1711 7 ; second-level list marker
1712 11 ; third-level list marker
1713 13 ; previous list item text
1714 16 ; pre-block indentation
1715 17 ; indentation of previous line
1716 21 ; previous line plus tab-width
1717 ))))))
1719 (ert-deftest test-markdown-indentation/indent-region ()
1720 "Test `markdown-indent-region'."
1721 ;; Basic test with multiple lines
1722 (markdown-test-string "abc\ndef\nghi\n"
1723 (markdown-indent-region (point-min) (point-max) nil)
1724 (should (string-equal (buffer-string) " abc\n def\n ghi\n")))
1725 ;; Following a list item
1726 (markdown-test-string " * abc\ndef\n"
1727 (forward-line)
1728 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
1729 (should (string-equal (buffer-string) " * abc\n def\n"))
1730 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
1731 (should (string-equal (buffer-string) " * abc\n def\n"))))
1733 (ert-deftest test-markdown-indentation/indent-list-hanging ()
1734 "Test `markdown-indent-line' with hanging list item."
1735 (markdown-test-string
1736 "- list
1737 - nested list with long lines which need to be
1738 hard wrapped"
1739 (goto-char (point-max))
1740 (markdown-enter-key)
1741 (should (eq (point) 78))))
1743 (ert-deftest test-markdown-indentation/indent-list-single ()
1744 "Test `markdown-indent-line' with single list item."
1745 (markdown-test-string
1746 " * item 1"
1747 (end-of-line)
1748 (markdown-enter-key)
1749 (should (string-equal (buffer-string) " * item 1\n "))
1750 (should (eq (point) 14))))
1752 (ert-deftest test-markdown-indentation/indent-pre ()
1753 "Test `markdown-indent-line' with a pre block."
1754 (markdown-test-string
1755 "I'm gonna write a code block:
1757 my first line of code"
1758 (goto-char (point-max))
1759 (markdown-enter-key)
1760 (should (eq (point) 62))
1761 (should (looking-back "^ "))))
1763 ;;; Font lock tests:
1765 (ert-deftest test-markdown-font-lock/italics-1 ()
1766 "A simple italics test."
1767 (markdown-test-file "inline.text"
1768 (goto-char 9)
1769 (should (looking-at "\*"))
1770 ;; Check face of char before leading asterisk
1771 (markdown-test-range-has-face 8 8 nil)
1772 ;; Check face of italic range
1773 (markdown-test-range-has-face 9 9 markdown-markup-face)
1774 (markdown-test-range-has-face 10 16 markdown-italic-face)
1775 (markdown-test-range-has-face 17 17 markdown-markup-face)
1776 ;; Check face of point past leading asterisk
1777 (markdown-test-range-has-face 18 18 nil)))
1779 (ert-deftest test-markdown-font-lock/italics-2 ()
1780 "Test space after leading asterisk or underscore."
1781 (markdown-test-string
1782 "This is * not italic*, nor _ is this_."
1783 (markdown-test-range-has-face (point-min) (point-max) nil)))
1785 (ert-deftest test-markdown-font-lock/italics-3 ()
1786 "Test that slash inside asterisks is not italic."
1787 (markdown-test-string
1788 "not italic *\\*"
1789 (markdown-test-range-has-face (point-min) (point-max) nil)))
1791 (ert-deftest test-markdown-font-lock/italics-4 ()
1792 "Test that escaped asterisk inside italics is not bold."
1793 (markdown-test-string
1794 "italic **\\**"
1795 (markdown-test-range-has-face 1 7 nil)
1796 (markdown-test-range-has-face 8 8 markdown-markup-face)
1797 (markdown-test-range-has-face 9 11 markdown-italic-face)
1798 (markdown-test-range-has-face 12 12 markdown-markup-face)))
1800 (ert-deftest test-markdown-font-lock/italics-5 ()
1801 "Test italic single letter."
1802 (markdown-test-string
1803 "*a*"
1804 (markdown-test-range-has-face 1 1 markdown-markup-face)
1805 (markdown-test-range-has-face 2 2 markdown-italic-face)
1806 (markdown-test-range-has-face 3 3 markdown-markup-face)))
1808 (ert-deftest test-markdown-font-lock/italics-6 ()
1809 "Test multiline italics across list items."
1810 (markdown-test-string
1811 "* something about function foo_bar
1812 * something else about foo_bar"
1813 (markdown-test-range-has-face 31 34 nil)
1814 (markdown-test-range-has-face 38 62 nil))
1815 (markdown-test-string
1816 "* something about function
1817 foo_bar
1818 * something else about
1819 foo_bar"
1820 (markdown-test-range-has-face 30 36 nil)
1821 (markdown-test-range-has-face 63 69 nil))
1822 (markdown-test-string
1823 "foo_bar
1824 * foo_bar"
1825 (markdown-test-range-has-face 4 7 nil)
1826 (markdown-test-range-has-face 11 14 nil)))
1828 (ert-deftest test-markdown-font-lock/italics-7 ()
1829 "Underscores in URLs should not trigger italics."
1830 :expected-result :failed
1831 (markdown-test-string
1832 "<http://jblevins.org/research/centroid/cd_z_path.m>"
1833 (markdown-test-range-face-equals 2 50 markdown-link-face))
1834 (markdown-test-string
1835 "[1]: http://jblevins.org/research/centroid/cd_z_path.m"
1836 (markdown-test-range-face-equals 6 54 markdown-url-face))
1837 (markdown-test-string
1838 "[cd\\_z\\_path.m](http://jblevins.org/research/centroid/cd_z_path.m)"
1839 (markdown-test-range-face-equals 17 65 markdown-url-face)))
1841 (ert-deftest test-markdown-font-lock/italics-after-hr ()
1842 "Test italics after a horizontal rule with asterisks."
1843 (markdown-test-string "* * *\n\n*italic*\n"
1844 (markdown-test-range-has-face 1 5 markdown-header-delimiter-face)
1845 (markdown-test-range-has-face 8 8 markdown-markup-face)
1846 (markdown-test-range-has-face 9 14 markdown-italic-face)
1847 (markdown-test-range-has-face 15 15 markdown-markup-face)))
1849 (ert-deftest test-markdown-font-lock/italics-in-heading ()
1850 "Test italic overlay in a heading."
1851 (markdown-test-string
1852 "# *Italics* in a Heading"
1853 (markdown-test-range-has-face 3 3 markdown-markup-face)
1854 (markdown-test-range-has-face 4 10 markdown-italic-face)
1855 (markdown-test-range-has-face 11 11 markdown-markup-face)))
1857 (ert-deftest test-markdown-font-lock/italics-link ()
1858 "Test italic overlay in an inline link."
1859 (markdown-test-string
1860 "*[italic link](http://www.link.com/)*"
1861 (markdown-test-range-has-face 1 1 markdown-markup-face)
1862 (markdown-test-range-has-face 2 36 markdown-italic-face)
1863 (markdown-test-range-has-face 37 37 markdown-markup-face))
1864 (markdown-test-string
1865 "[*italic link*](http://www.link.com/)"
1866 (markdown-test-range-has-face 2 2 markdown-markup-face)
1867 (markdown-test-range-has-face 3 13 markdown-italic-face)
1868 (markdown-test-range-has-face 14 14 markdown-markup-face)))
1870 (ert-deftest test-markdown-font-lock/italics-in-blockquote ()
1871 "Test italics overlay in a blockquote."
1872 (markdown-test-string
1873 "> *italics* inside a blockquote"
1874 (markdown-test-range-has-face 3 3 markdown-markup-face)
1875 (markdown-test-range-has-face 4 10 markdown-italic-face)
1876 (markdown-test-range-has-face 11 11 markdown-markup-face)))
1878 (ert-deftest test-markdown-font-lock/italics-in-pre ()
1879 "Test italics overlay in a blockquote."
1880 (markdown-test-string
1881 " *italics* inside a pre block"
1882 (markdown-test-range-has-face (point-min) (1- (point-max))
1883 markdown-pre-face)))
1885 (ert-deftest test-markdown-font-lock/italics-and-code ()
1886 "Test seeming italics mixed with code."
1887 (markdown-test-string
1888 "define `var_1` and `var_2` inline code"
1889 (markdown-test-range-has-face 9 13 markdown-inline-code-face)
1890 (markdown-test-range-has-face 21 25 markdown-inline-code-face))
1891 (markdown-test-string
1892 "`var_1` and var_2"
1893 (markdown-test-range-has-face 2 6 markdown-inline-code-face)
1894 (markdown-test-range-has-face 8 17 nil))
1895 (markdown-test-string
1896 "var_1 and `var_2`"
1897 (markdown-test-range-has-face 1 10 nil)
1898 (markdown-test-range-has-face 12 16 markdown-inline-code-face)))
1900 (ert-deftest test-markdown-font-lock/italics-and-code ()
1901 "Test seeming italics mixed with code."
1902 (markdown-test-string
1903 "[lg]: twilight_sm.png\n[sm]: twilight_lg.png"
1904 (markdown-test-range-has-face 7 21 markdown-url-face)
1905 (markdown-test-range-has-face 22 22 nil)
1906 (markdown-test-range-has-face 29 43 markdown-url-face)
1907 (markdown-test-range-has-face 28 28 nil)))
1909 (ert-deftest test-markdown-font-lock/bold-1 ()
1910 "A simple bold test."
1911 (markdown-test-file "inline.text"
1912 (goto-char 27)
1913 (should (looking-at "\*\*"))
1914 ;; Check face of char before leading asterisk
1915 (markdown-test-range-has-face 26 26 nil)
1916 ;; Check face of opening asterisks
1917 (markdown-test-range-has-face 27 28 markdown-markup-face)
1918 ;; Check face of bold range
1919 (markdown-test-range-has-face 29 33 markdown-bold-face)
1920 ;; Check face of closing asterisks
1921 (markdown-test-range-has-face 34 35 markdown-markup-face)
1922 ;; Check face of point past leading asterisk
1923 (markdown-test-range-has-face 36 36 nil)))
1925 (ert-deftest test-markdown-font-lock/bold-2 ()
1926 "Test space after leading asterisks or underscores."
1927 (markdown-test-string
1928 "This is ** not bold**, nor __ is this__ (but they match italics)."
1929 (markdown-test-range-has-face 1 8 nil)
1930 (markdown-test-range-has-face 9 9 markdown-markup-face)
1931 (markdown-test-range-has-face 10 19 markdown-italic-face)
1932 (markdown-test-range-has-face 20 20 markdown-markup-face)
1933 (markdown-test-range-has-face 21 27 nil)
1934 (markdown-test-range-has-face 28 28 markdown-markup-face)
1935 (markdown-test-range-has-face 29 37 markdown-italic-face)
1936 (markdown-test-range-has-face 38 38 markdown-markup-face)
1937 (markdown-test-range-has-face 39 (point-max) nil)))
1939 (ert-deftest test-markdown-font-lock/bold-3 ()
1940 "Test escaped asterisk inside bold."
1941 (markdown-test-string
1942 "bold **\\***"
1943 (markdown-test-range-has-face 1 5 nil)
1944 (markdown-test-range-has-face 6 7 markdown-markup-face)
1945 (markdown-test-range-has-face 8 9 markdown-bold-face)
1946 (markdown-test-range-has-face 10 11 markdown-markup-face)))
1948 (ert-deftest test-markdown-font-lock/bold-4 ()
1949 "Test bold single letter."
1950 (markdown-test-string
1951 "**a**"
1952 (markdown-test-range-has-face 1 2 markdown-markup-face)
1953 (markdown-test-range-has-face 3 3 markdown-bold-face)
1954 (markdown-test-range-has-face 4 5 markdown-markup-face)))
1956 (ert-deftest test-markdown-font-lock/bold-after-hr ()
1957 "Test bold after a horizontal rule with asterisks."
1958 (markdown-test-string "* * *\n\n**bold**\n"
1959 (markdown-test-range-has-face 1 5 markdown-header-delimiter-face)
1960 (markdown-test-range-has-face 8 9 markdown-markup-face)
1961 (markdown-test-range-has-face 10 13 markdown-bold-face)
1962 (markdown-test-range-has-face 14 15 markdown-markup-face)))
1964 (ert-deftest test-markdown-font-lock/bold-link ()
1965 "Test bold overlay in an inline link."
1966 (markdown-test-string
1967 "**[bold link](http://www.link.com/)**"
1968 (markdown-test-range-has-face 1 2 markdown-markup-face)
1969 (markdown-test-range-has-face 3 35 markdown-bold-face)
1970 (markdown-test-range-has-face 36 37 markdown-markup-face))
1971 (markdown-test-string
1972 "[**bold link**](http://www.link.com/)"
1973 (markdown-test-range-has-face 2 3 markdown-markup-face)
1974 (markdown-test-range-has-face 4 12 markdown-bold-face)
1975 (markdown-test-range-has-face 13 14 markdown-markup-face)))
1977 (ert-deftest test-markdown-font-lock/bold-in-blockquote ()
1978 "Test bold overlay in a blockquote."
1979 (markdown-test-string
1980 "> **bold** inside a blockquote"
1981 (markdown-test-range-has-face 3 4 markdown-markup-face)
1982 (markdown-test-range-has-face 5 8 markdown-bold-face)
1983 (markdown-test-range-has-face 9 10 markdown-markup-face)))
1985 (ert-deftest test-markdown-font-lock/bold-in-pre ()
1986 "Test bold overlay in a blockquote."
1987 (markdown-test-string
1988 " **bold** inside a pre block"
1989 (markdown-test-range-has-face (point-min) (1- (point-max))
1990 markdown-pre-face)))
1992 (ert-deftest test-markdown-font-lock/no-bold-in-code ()
1993 "Bold markers in inline code should not trigger bold."
1994 (markdown-test-string
1995 "`def __init__(self):`"
1996 (markdown-test-range-has-face 8 11 markdown-inline-code-face))
1997 (markdown-test-string
1998 "`**foo` bar `baz**`"
1999 (markdown-test-range-face-equals 2 6 markdown-inline-code-face)
2000 (markdown-test-range-face-equals 9 11 nil)
2001 (markdown-test-range-face-equals 14 18 markdown-inline-code-face)))
2003 (ert-deftest test-markdown-font-lock/no-bold-in-math ()
2004 "Bold markers in math should not trigger bold."
2005 (markdown-test-file "math.text"
2006 (markdown-toggle-math t)
2007 (funcall markdown-test-font-lock-function)
2008 (markdown-test-range-has-face 279 299 markdown-math-face)
2009 (markdown-test-range-has-face 301 308 nil)
2010 (markdown-test-range-has-face 310 312 markdown-math-face)))
2012 (ert-deftest test-markdown-font-lock/code-1 ()
2013 "A simple inline code test."
2014 (markdown-test-file "inline.text"
2015 (goto-char 45)
2016 (should (looking-at "`"))
2017 ;; Regular code span
2018 (markdown-test-range-has-face 45 45 markdown-markup-face)
2019 (markdown-test-range-has-face 46 49 markdown-inline-code-face)
2020 (markdown-test-range-has-face 50 50 markdown-markup-face)
2021 ;; Code containing backticks
2022 (markdown-test-range-has-face 61 62 markdown-markup-face)
2023 (markdown-test-range-has-face 63 87 markdown-inline-code-face)
2024 (markdown-test-range-has-face 88 89 markdown-markup-face)
2025 ;; Seven backquotes in a row
2026 (markdown-test-range-has-face 119 125 nil)
2027 ;; Backquotes at beginning or end
2028 (markdown-test-range-has-face 228 229 markdown-markup-face)
2029 (markdown-test-range-has-face 230 237 markdown-inline-code-face)
2030 (markdown-test-range-has-face 238 239 markdown-markup-face)
2031 (markdown-test-range-has-face 341 342 markdown-markup-face)
2032 (markdown-test-range-has-face 343 349 markdown-inline-code-face)
2033 (markdown-test-range-has-face 350 351 markdown-markup-face)
2034 ;; Backslash as final character
2035 (markdown-test-range-has-face 460 460 markdown-markup-face)
2036 (markdown-test-range-has-face 461 467 markdown-inline-code-face)
2037 (markdown-test-range-has-face 468 468 markdown-markup-face)
2038 ;; Escaping of leading backquotes
2039 (markdown-test-range-has-face 586 592 nil)
2040 (markdown-test-range-has-face 597 603 nil)
2041 ;; A code span crossing lines
2042 (markdown-test-range-has-face 652 656 nil)
2043 (markdown-test-range-has-face 657 657 markdown-markup-face)
2044 (markdown-test-range-has-face 658 665 markdown-inline-code-face)
2045 (markdown-test-range-has-face 666 666 markdown-markup-face)
2046 ;; Three backquotes: same line, across lines, not across blocks
2047 (markdown-test-range-has-face 695 748 nil)
2048 (markdown-test-range-has-face 749 750 markdown-markup-face)
2049 (markdown-test-range-has-face 751 755 markdown-inline-code-face)
2050 (markdown-test-range-has-face 756 757 markdown-markup-face)
2051 (markdown-test-range-has-face 758 805 nil)
2052 (markdown-test-range-has-face 806 807 markdown-markup-face)
2053 (markdown-test-range-has-face 808 812 markdown-inline-code-face)
2054 (markdown-test-range-has-face 813 814 markdown-markup-face)
2055 (markdown-test-range-has-face 815 891 nil)
2058 (ert-deftest test-markdown-font-lock/code-2 ()
2059 "Multiple code spans in a row and on different lines."
2060 (markdown-test-string "`foo` `bar` `baz`"
2061 (markdown-test-range-has-face 1 1 markdown-markup-face)
2062 (markdown-test-range-has-face 2 4 markdown-inline-code-face)
2063 (markdown-test-range-has-face 5 5 markdown-markup-face)
2064 (markdown-test-range-has-face 6 6 nil)
2065 (markdown-test-range-has-face 7 7 markdown-markup-face)
2066 (markdown-test-range-has-face 8 10 markdown-inline-code-face)
2067 (markdown-test-range-has-face 11 11 markdown-markup-face)
2068 (markdown-test-range-has-face 12 12 nil)
2069 (markdown-test-range-has-face 13 13 markdown-markup-face)
2070 (markdown-test-range-has-face 14 16 markdown-inline-code-face)
2071 (markdown-test-range-has-face 17 17 markdown-markup-face))
2072 (markdown-test-string "`a`\n`b`\n`c`\n"
2073 (markdown-test-range-has-face 1 1 markdown-markup-face)
2074 (markdown-test-range-has-face 2 2 markdown-inline-code-face)
2075 (markdown-test-range-has-face 3 3 markdown-markup-face)
2076 (markdown-test-range-has-face 4 4 nil)
2077 (markdown-test-range-has-face 5 5 markdown-markup-face)
2078 (markdown-test-range-has-face 6 6 markdown-inline-code-face)
2079 (markdown-test-range-has-face 7 7 markdown-markup-face)
2080 (markdown-test-range-has-face 8 8 nil)
2081 (markdown-test-range-has-face 9 9 markdown-markup-face)
2082 (markdown-test-range-has-face 10 10 markdown-inline-code-face)
2083 (markdown-test-range-has-face 11 11 markdown-markup-face)
2084 (markdown-test-range-has-face 12 12 nil))
2085 (markdown-test-string "a`foo`b`bar`c`baz`d"
2086 (markdown-test-range-has-face 1 1 nil)
2087 (markdown-test-range-has-face 2 2 markdown-markup-face)
2088 (markdown-test-range-has-face 3 5 markdown-inline-code-face)
2089 (markdown-test-range-has-face 6 6 markdown-markup-face)
2090 (markdown-test-range-has-face 7 7 nil)
2091 (markdown-test-range-has-face 8 8 markdown-markup-face)
2092 (markdown-test-range-has-face 9 11 markdown-inline-code-face)
2093 (markdown-test-range-has-face 12 12 markdown-markup-face)
2094 (markdown-test-range-has-face 13 13 nil)
2095 (markdown-test-range-has-face 14 14 markdown-markup-face)
2096 (markdown-test-range-has-face 15 17 markdown-inline-code-face)
2097 (markdown-test-range-has-face 18 18 markdown-markup-face)
2098 (markdown-test-range-has-face 19 19 nil)))
2100 (ert-deftest test-markdown-font-lock/code-3 ()
2101 "Backslashes don't escape backticks inside of inline code strings."
2102 (markdown-test-string
2103 "`foo\\`bar`"
2104 (markdown-test-range-has-face 1 1 markdown-markup-face)
2105 (markdown-test-range-has-face 2 5 markdown-inline-code-face)
2106 (markdown-test-range-has-face 6 6 markdown-markup-face)
2107 (markdown-test-range-has-face 7 10 nil)))
2109 (ert-deftest test-markdown-font-lock/code-link-precedence ()
2110 "Test that inline code takes precedence over inline links.
2111 Test currently fails because this case isn't handled properly."
2112 :expected-result :failed
2113 (markdown-test-string
2114 "[not a `link](/foo`)"
2115 (markdown-test-range-has-face 1 7 nil)
2116 (markdown-test-range-has-face 8 8 markdown-markup-face)
2117 (markdown-test-range-has-face 9 18 markdown-inline-code-face)
2118 (markdown-test-range-has-face 19 19 markdown-markup-face)
2119 (markdown-test-range-has-face 20 20 nil)))
2121 (ert-deftest test-markdown-font-lock/kbd ()
2122 "Test font lock for <kbd> tags."
2123 (markdown-test-string "<kbd>C-c <</kbd>"
2124 (markdown-test-range-has-face 1 5 markdown-markup-face)
2125 (markdown-test-range-has-face 6 10 markdown-inline-code-face)
2126 (markdown-test-range-has-face 11 16 markdown-markup-face))
2127 (markdown-test-string "To quit Emacs, press <kbd>C-x C-c</kbd>."
2128 (markdown-test-range-has-face 1 21 nil)
2129 (markdown-test-range-has-face 22 26 markdown-markup-face)
2130 (markdown-test-range-has-face 27 33 markdown-inline-code-face)
2131 (markdown-test-range-has-face 34 39 markdown-markup-face)
2132 (markdown-test-range-has-face 40 40 nil)))
2134 (ert-deftest test-markdown-font-lock/lists-1 ()
2135 "A simple list marker font lock test."
2136 (markdown-test-file "lists.text"
2137 (dolist (loc (list 1063 1283 1659 1830 1919 2150 2393 2484
2138 2762 2853 3097 3188 3700 3903 4009))
2139 (goto-char loc)
2140 (should (looking-at "[*+-]"))
2141 (markdown-test-range-has-face loc loc markdown-list-face))))
2143 (ert-deftest test-markdown-font-lock/definition-list ()
2144 "A simple definition list marker font lock test."
2145 (markdown-test-file "definition-list.text"
2146 (markdown-test-range-has-face 7 7 'markdown-list-face)
2147 (markdown-test-range-has-face 29 52 'markdown-pre-face)
2148 (markdown-test-range-has-face 55 55 'markdown-list-face)))
2150 (ert-deftest test-markdown-font-lock/pre-1 ()
2151 "Nested list and pre block font lock test."
2152 (markdown-test-file "nested-list.text"
2153 (dolist (loc (list 4 29 194 224 491 525))
2154 (markdown-test-range-has-face loc loc markdown-list-face))
2155 (markdown-test-range-has-face 6 25 nil)
2156 (markdown-test-range-has-face 31 83 nil)
2157 (markdown-test-range-has-face 85 154 markdown-pre-face)
2158 (markdown-test-range-has-face 157 189 nil)
2159 (markdown-test-range-has-face 196 215 nil)
2160 (markdown-test-range-has-face 226 403 nil)
2161 (markdown-test-range-has-face 405 481 markdown-pre-face)
2162 (markdown-test-range-has-face 493 512 nil)
2163 (markdown-test-range-has-face 527 546 nil)
2164 (markdown-test-range-has-face 548 580 markdown-pre-face)))
2166 (ert-deftest test-markdown-font-lock/pre-2 ()
2167 (markdown-test-string "* item\n\nreset baseline\n\n pre block\n"
2168 (markdown-test-range-has-face 1 1 markdown-list-face)
2169 (markdown-test-range-has-face 2 23 nil)
2170 (markdown-test-range-has-face 29 37 markdown-pre-face)))
2172 (ert-deftest test-markdown-font-lock/pre-3 ()
2173 (markdown-test-string "It is interesting to see what happens when one queries
2174 `social upheaval` and `protopalatial era`.
2176 * `social upheaval`: the follwing queries have been tried:
2178 social upheaval subClassOf"
2179 (markdown-test-range-has-face 160 190 nil)))
2181 (ert-deftest test-markdown-font-lock/pre-4 ()
2182 "Pre blocks must be preceded by a blank line"
2183 (markdown-test-string "Paragraph
2184 for (var i = 0; i < 10; i++) {
2185 console.log(i);
2187 (markdown-test-range-has-face (point-min) (point-max) nil)))
2189 (ert-deftest test-markdown-font-lock/fenced-1 ()
2190 "Test fenced code blocks containing four-space indents."
2191 (markdown-test-string "Fenced code block
2194 if (x)
2195 foo();
2197 if (y)
2198 bar();
2201 (markdown-test-range-has-face 1 19 nil)
2202 (markdown-test-range-has-face 20 22 markdown-markup-face)
2203 (markdown-test-range-has-face 24 60 markdown-pre-face)
2204 (markdown-test-range-has-face 61 63 markdown-markup-face)))
2206 (ert-deftest test-markdown-font-lock/gfm-fenced-1 ()
2207 "Test GFM-style fenced code blocks (1)."
2208 (markdown-test-string "```ruby
2209 require 'redcarpet'
2210 markdown = Redcarpet.new('Hello World!')
2211 puts markdown.to_html
2212 ```"
2213 (markdown-test-range-has-face 1 3 markdown-markup-face) ; ```
2214 (markdown-test-range-has-face 4 7 markdown-language-keyword-face) ; ruby
2215 (markdown-test-range-has-face 9 90 markdown-pre-face) ; code
2216 (markdown-test-range-has-face 92 94 markdown-markup-face))) ; ```
2218 (ert-deftest test-markdown-font-lock/gfm-fenced-2 ()
2219 "Test GFM-style fenced code blocks (2)."
2220 (markdown-test-string "```{r sum}\n2+2\n```"
2221 (markdown-test-range-has-face 1 3 markdown-markup-face) ; ```
2222 (markdown-test-range-has-face 4 10 markdown-language-keyword-face) ; {r sum}
2223 (markdown-test-range-has-face 12 14 markdown-pre-face) ; 2+2
2224 (markdown-test-range-has-face 16 18 markdown-markup-face))) ; ```
2226 (ert-deftest test-markdown-font-lock/gfm-fenced-3 ()
2227 "GFM-style code blocks need not be preceded by a blank line."
2228 (markdown-test-string "Paragraph
2229 ```js
2230 for (var i = 0; i < 10; i++) {
2231 console.log(i);
2233 ```"
2234 (markdown-test-range-has-face 1 10 nil) ; Paragraph
2235 (markdown-test-range-has-face 11 13 markdown-markup-face) ; ```
2236 (markdown-test-range-has-face 14 15 markdown-language-keyword-face) ; js
2237 (markdown-test-range-has-face 17 68 markdown-pre-face)
2238 (markdown-test-range-has-face 70 72 markdown-markup-face)))
2240 (ert-deftest test-markdown-font-lock/atx-no-spaces ()
2241 "Test font-lock for atx headers with no spaces."
2242 (markdown-test-string "##abc##"
2243 (markdown-test-range-has-face 1 7 nil))
2244 (markdown-test-string "##"
2245 (markdown-test-range-has-face 1 2 nil))
2246 (markdown-test-string "###"
2247 (markdown-test-range-has-face 1 3 nil)))
2249 (ert-deftest test-markdown-font-lock/setext-1-letter ()
2250 "An edge case for level-one setext headers."
2251 (markdown-test-string "a\n=\n"
2252 (markdown-test-range-has-face 1 1 markdown-header-face-1)
2253 (markdown-test-range-has-face 3 3 markdown-header-rule-face)))
2255 (ert-deftest test-markdown-font-lock/setext-2-letter ()
2256 "An edge case for level-two setext headers."
2257 (markdown-test-string "b\n-\n"
2258 (markdown-test-range-has-face 1 1 markdown-header-face-2)
2259 (markdown-test-range-has-face 3 3 markdown-header-rule-face)))
2261 (ert-deftest test-markdown-font-lock/inline-links ()
2262 "Test font lock for inline links."
2263 (markdown-test-file "inline.text"
2264 (markdown-test-range-has-face 925 925 markdown-markup-face)
2265 (markdown-test-range-has-face 926 929 markdown-link-face)
2266 (markdown-test-range-has-face 930 931 markdown-markup-face)
2267 (markdown-test-range-has-face 932 949 markdown-url-face)
2268 (markdown-test-range-has-face 951 957 markdown-link-title-face)
2269 (markdown-test-range-has-face 958 958 markdown-markup-face)))
2271 (ert-deftest test-markdown-font-lock/pre-comment ()
2272 "Test comments inside of a pre block."
2273 (markdown-test-string " <!-- pre, not comment -->"
2274 (markdown-test-range-has-face (point-min) (1- (point-max)) markdown-pre-face)))
2276 (ert-deftest test-markdown-font-lock/inline-code-comment ()
2277 "Test comments inside of a pre block."
2278 (markdown-test-string "`<h1> <!-- HTML comment inside inline code -->`"
2279 (markdown-test-range-has-face (1+ (point-min)) (- (point-max) 2) markdown-inline-code-face)))
2281 (ert-deftest test-markdown-font-lock/comment-hanging-indent ()
2282 "Test comments with hanging indentation."
2283 (markdown-test-string "<!-- This comment has\n hanging indentation -->"
2284 (markdown-test-range-has-face (point-min) (1- (point-max)) markdown-comment-face)))
2286 (ert-deftest test-markdown-font-lock/comment-multiple ()
2287 "Test multiple single-line comments in arow."
2288 (markdown-test-string "<!-- This is a comment -->\n<!-- And so is this -->"
2289 (markdown-test-range-has-face
2290 (point-at-bol) (1- (point-at-eol)) markdown-comment-face)
2291 (forward-line)
2292 (markdown-test-range-has-face
2293 (point-at-bol) (1- (point-at-eol)) markdown-comment-face)))
2295 (ert-deftest test-markdown-font-lock/comment-list-items ()
2296 "Test comment with list inside."
2297 (markdown-test-string
2298 "<!--
2299 - note 1;
2300 - note 2.
2301 -->"
2302 (markdown-test-range-face-equals (point-min) (1- (point-max))
2303 markdown-comment-face)))
2305 (ert-deftest test-markdown-font-lock/comment-angle-bracket ()
2306 "Regression test for GH-117."
2307 (markdown-test-string "<!-- > test -->"
2308 (markdown-test-range-face-equals (point-min) (1- (point-max))
2309 markdown-comment-face)))
2311 (ert-deftest test-markdown-font-lock/footnote-markers-links ()
2312 "Test an edge case involving footnote markers and inline reference links."
2313 (markdown-test-string "Harvard[^1] [tuition][]"
2314 (markdown-test-range-has-face 1 7 nil)
2315 (markdown-test-range-has-face 8 8 markdown-markup-face)
2316 (markdown-test-range-has-face 10 10 markdown-footnote-face)
2317 (markdown-test-range-has-face 11 11 markdown-markup-face)
2318 (markdown-test-range-has-face 12 12 nil)
2319 (markdown-test-range-has-face 13 13 markdown-markup-face)
2320 (markdown-test-range-has-face 14 20 markdown-link-face)
2321 (markdown-test-range-has-face 21 21 markdown-markup-face)
2322 (markdown-test-range-has-face 22 23 markdown-markup-face)))
2324 (ert-deftest test-markdown-font-lock/mmd-metadata ()
2325 "Basic MultMarkdown metadata tests."
2326 (markdown-test-string "Title: peg-multimarkdown User's Guide
2327 Author: Fletcher T. Penney
2328 Base Header Level: 2"
2329 (markdown-test-range-has-face 1 5 markdown-metadata-key-face)
2330 (markdown-test-range-has-face 6 6 markdown-markup-face)
2331 (markdown-test-range-has-face 8 37 markdown-metadata-value-face)
2332 (markdown-test-range-has-face 39 44 markdown-metadata-key-face)
2333 (markdown-test-range-has-face 46 46 markdown-markup-face)
2334 (markdown-test-range-has-face 47 64 markdown-metadata-value-face)
2335 (markdown-test-range-has-face 66 82 markdown-metadata-key-face)
2336 (markdown-test-range-has-face 83 83 markdown-markup-face)
2337 (markdown-test-range-has-face 85 85 markdown-metadata-value-face))
2338 ;; Avoid triggering when a title contains a colon (e.g., Markdown: Syntax)
2339 (markdown-test-file "syntax.text"
2340 (markdown-test-range-has-face 1 16 markdown-header-face-1)))
2342 (ert-deftest test-markdown-font-lock/mmd-metadata-after-header ()
2343 "Ensure that similar lines are not matched after the header."
2344 (markdown-test-string "Title: peg-multimarkdown User's Guide
2346 Author: Fletcher T. Penney
2347 Base Header Level: 2"
2348 (markdown-test-range-has-face 1 5 markdown-metadata-key-face)
2349 (markdown-test-range-has-face 6 6 markdown-markup-face)
2350 (markdown-test-range-has-face 8 37 markdown-metadata-value-face)
2351 (markdown-test-range-has-face 40 65 nil)
2352 (markdown-test-range-has-face 67 86 nil)))
2354 (ert-deftest test-markdown-font-lock/pandoc-metadata ()
2355 "Basic Pandoc metadata tests."
2356 (markdown-test-string "% title
2357 two-line title
2358 % first author;
2359 second author
2360 % date
2362 body"
2363 (markdown-test-range-has-face 1 1 markdown-markup-face)
2364 (markdown-test-range-has-face 3 24 markdown-metadata-value-face)
2365 (markdown-test-range-has-face 26 26 markdown-markup-face)
2366 (markdown-test-range-has-face 28 56 markdown-metadata-value-face)
2367 (markdown-test-range-has-face 58 58 markdown-markup-face)
2368 (markdown-test-range-has-face 60 63 markdown-metadata-value-face)
2369 (markdown-test-range-has-face 64 69 nil)))
2371 (ert-deftest test-markdown-font-lock/yaml-metadata ()
2372 "Basic YAML metadata tests."
2373 (markdown-test-string
2374 "---
2375 layout: post
2376 date: 2015-08-13 11:35:25 EST
2379 (markdown-test-range-has-face 1 3 markdown-markup-face)
2380 (markdown-test-range-has-face 5 10 markdown-metadata-key-face)
2381 (markdown-test-range-has-face 11 11 markdown-markup-face)
2382 (markdown-test-range-has-face 13 16 markdown-metadata-value-face)
2383 (markdown-test-range-has-face 18 21 markdown-metadata-key-face)
2384 (markdown-test-range-has-face 22 22 markdown-markup-face)
2385 (markdown-test-range-has-face 24 46 markdown-metadata-value-face)
2386 (markdown-test-range-has-face 48 50 markdown-markup-face)))
2388 (ert-deftest test-markdown-font-lock/toml-metadata ()
2389 "Basic TOML metadata tests."
2390 (markdown-test-string
2391 "---
2392 layout = post
2393 date = 2015-08-13 11:35:25 EST
2396 (markdown-test-range-has-face 1 3 markdown-markup-face)
2397 (markdown-test-range-has-face 5 10 markdown-metadata-key-face)
2398 (markdown-test-range-has-face 12 12 markdown-markup-face)
2399 (markdown-test-range-has-face 14 17 markdown-metadata-value-face)
2400 (markdown-test-range-has-face 19 22 markdown-metadata-key-face)
2401 (markdown-test-range-has-face 24 24 markdown-markup-face)
2402 (markdown-test-range-has-face 26 48 markdown-metadata-value-face)
2403 (markdown-test-range-has-face 50 52 markdown-markup-face)))
2405 (ert-deftest test-markdown-font-lock/pandoc-yaml-metadata ()
2406 "Basic yaml metadata tests, with pandoc syntax."
2407 (let ((markdown-use-pandoc-style-yaml-metadata t))
2408 (markdown-test-string
2409 "some text
2412 layout: post
2413 date: 2015-08-13 11:35:25 EST
2416 more text
2419 layout: post
2420 date: 2015-08-13 11:35:25 EST
2423 But this is merely a code block
2427 layout: post
2428 date: 2015-08-13 11:35:25 EST
2432 ;; first section
2433 (markdown-test-range-has-face 12 14 markdown-markup-face)
2434 (markdown-test-range-has-face 16 21 markdown-metadata-key-face)
2435 (markdown-test-range-has-face 22 22 markdown-markup-face)
2436 (markdown-test-range-has-face 24 27 markdown-metadata-value-face)
2437 (markdown-test-range-has-face 29 32 markdown-metadata-key-face)
2438 (markdown-test-range-has-face 33 33 markdown-markup-face)
2439 (markdown-test-range-has-face 35 57 markdown-metadata-value-face)
2440 (markdown-test-range-has-face 59 61 markdown-markup-face)
2441 ;; second section
2442 (markdown-test-range-has-face 75 77 markdown-markup-face)
2443 (markdown-test-range-has-face 79 84 markdown-metadata-key-face)
2444 (markdown-test-range-has-face 85 85 markdown-markup-face)
2445 (markdown-test-range-has-face 87 90 markdown-metadata-value-face)
2446 (markdown-test-range-has-face 92 95 markdown-metadata-key-face)
2447 (markdown-test-range-has-face 96 96 markdown-markup-face)
2448 (markdown-test-range-has-face 98 120 markdown-metadata-value-face)
2449 (markdown-test-range-has-face 122 124 markdown-markup-face)
2450 ;; third section
2451 (markdown-test-range-has-face 160 162 markdown-markup-face)
2452 (markdown-test-range-has-face 164 213 markdown-pre-face)
2453 (markdown-test-range-has-face 215 217 markdown-markup-face))))
2455 (ert-deftest test-markdown-font-lock/line-break ()
2456 "Basic line break tests."
2457 (markdown-test-string " \nasdf \n"
2458 (markdown-test-range-has-face 1 9 nil)
2459 (markdown-test-range-has-face 10 11 markdown-line-break-face)))
2461 (ert-deftest test-markdown-font-lock/blockquote-bold ()
2462 "Test font lock for bold inside of a blockquote."
2463 (markdown-test-string
2464 "> **bold**"
2465 (markdown-test-range-has-face 2 10 markdown-blockquote-face)
2466 (markdown-test-range-has-face 5 8 markdown-bold-face)))
2468 (ert-deftest test-markdown-font-lock/blockquote-italic ()
2469 "Test font lock for italic inside of a blockquote."
2470 (markdown-test-string
2471 "> *italic*"
2472 (markdown-test-range-has-face 2 10 markdown-blockquote-face)
2473 (markdown-test-range-has-face 4 9 markdown-italic-face)))
2475 (ert-deftest test-markdown-font-lock/blockquote-link ()
2476 "Test font lock for links inside of a blockquote.
2477 This test will fail until font lock for inline links inside
2478 blockquotes is implemented (at present, the blockquote face
2479 takes precedence)."
2480 :expected-result :failed
2481 (markdown-test-string
2482 "> [link](url)"
2483 (markdown-test-range-has-face 1 13 markdown-blockquote-face)
2484 (markdown-test-range-has-face 3 8 markdown-link-face)
2485 (markdown-test-range-has-face 9 13 markdown-url-face)))
2487 (ert-deftest test-markdown-font-lock/blockquote-comment ()
2488 "Test font lock for comments inside of a blockquote."
2489 (markdown-test-string
2490 "> <!-- comment -->"
2491 (markdown-test-range-has-face 1 1 markdown-markup-face)
2492 (markdown-test-range-has-face 3 18 markdown-comment-face)))
2494 (ert-deftest test-markdown-font-lock/pre-override ()
2495 "Test that font lock for pre blocks overrides everything else."
2496 (markdown-test-string
2497 " **bold**
2498 _italic_
2499 <!-- comment -->
2500 [link](url)
2501 * list"
2502 (markdown-test-range-has-face 1 73 markdown-pre-face)))
2504 (ert-deftest test-markdown-font-lock/gfm-code-block-font-lock ()
2505 "GFM code block font lock test. Now in base markdown-mode as well!"
2506 (markdown-test-file "gfm.text"
2507 (markdown-test-range-has-face 2639 2641 markdown-markup-face) ; ```
2508 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face) ; lang
2509 (markdown-test-range-has-face 2647 2728 markdown-pre-face) ; code
2510 (markdown-test-range-has-face 2730 2732 markdown-markup-face))) ; ```
2512 (ert-deftest test-markdown-font-lock/reference-definition ()
2513 "Reference definitions should not include ]."
2514 (markdown-test-string "[1]: http://daringfireball.net/ \"title\""
2515 (markdown-test-range-has-face 2 2 markdown-reference-face) ; 1
2516 (markdown-test-range-has-face 6 31 markdown-url-face) ; URL
2517 (markdown-test-range-has-face 34 38 markdown-link-title-face)) ; title
2518 (markdown-test-string "[foo][1] and [bar][2]: not a reference definition"
2519 (markdown-test-range-has-face 2 4 markdown-link-face) ; foo
2520 (markdown-test-range-has-face 7 7 markdown-reference-face) ; 1
2521 (markdown-test-range-has-face 9 13 nil) ; [ ]and[ ]
2522 (markdown-test-range-has-face 15 17 markdown-link-face) ; bar
2523 (markdown-test-range-has-face 20 20 markdown-reference-face) ; 2
2524 (markdown-test-range-has-face 22 49 nil))) ; [ ]and[ ]
2526 ;;; Markdown Parsing Functions:
2528 (ert-deftest test-markdown-parsing/extend-region-function ()
2529 "Test `markdown-syntax-propertize-extend-region'.
2530 Should return a cons (NEW-START . NEW-END) or nil if no
2531 adjustment should be made. Function is called repeatedly until it
2532 returns nil."
2533 (markdown-test-file
2534 "inline.text"
2535 (should (equal (markdown-syntax-propertize-extend-region 1 17)
2536 (cons 1 91)))
2537 (should (equal (markdown-syntax-propertize-extend-region 2 17)
2538 (cons 1 91)))
2539 (should (equal (markdown-syntax-propertize-extend-region 1 91)
2540 nil))
2541 (should (equal (markdown-syntax-propertize-extend-region 93 157)
2542 nil))
2543 (should (equal (markdown-syntax-propertize-extend-region 496 502)
2544 (cons 486 510)))
2545 (should (equal (markdown-syntax-propertize-extend-region 486 510)
2546 nil))
2547 ;; Region that begins and ends with \n\n should not be extended
2548 (should (equal (markdown-syntax-propertize-extend-region 157 355)
2549 nil))))
2551 (defun markdown-test-check-match-limits (prop num begin end &optional pos)
2552 (let* ((posn (or pos (point)))
2553 (props (get-text-property posn prop)))
2554 (save-match-data
2555 (set-match-data props)
2556 (and (match-beginning num) (match-end num)
2557 (= (match-beginning num) begin)
2558 (= (match-end num) end)))))
2560 (ert-deftest test-markdown-parsing/syntax-with-adjacent-code-blocks ()
2561 "Test `markdown-syntax-propertize-fenced-code-blocks' with adjacent blocks."
2562 (markdown-test-string
2563 "~~~ shell
2564 #!/bin/sh
2566 echo \"Hello, world!\"
2569 ~~~ shell
2570 #!/bin/sh
2572 echo \"Hello, world v2!\"
2575 (let ((start-top-1 (make-marker)) (end-top-1 (make-marker))
2576 (start-lang-1 (make-marker)) (end-lang-1 (make-marker))
2577 (start-mid-1 (make-marker)) (end-mid-1 (make-marker))
2578 (start-bottom-1 (make-marker)) (end-bottom-1 (make-marker))
2579 (between (make-marker))
2580 (start-top-2 (make-marker)) (end-top-2 (make-marker))
2581 (start-lang-2 (make-marker)) (end-lang-2 (make-marker))
2582 (start-mid-2 (make-marker)) (end-mid-2 (make-marker))
2583 (start-bottom-2 (make-marker)) (end-bottom-2 (make-marker)))
2584 ;; First code block
2585 (set-marker start-top-1 1)
2586 (set-marker end-top-1 4)
2587 (set-marker start-lang-1 5)
2588 (set-marker end-lang-1 10)
2589 (set-marker start-mid-1 11)
2590 (set-marker end-mid-1 43)
2591 (set-marker start-bottom-1 43)
2592 (set-marker end-bottom-1 46)
2593 ;; check top tildes
2594 (should (markdown-test-check-match-limits
2595 'markdown-tilde-fence-begin 1 (marker-position start-top-1)
2596 (marker-position end-top-1) (marker-position start-top-1)))
2597 ;; check top language specifier
2598 (should (markdown-test-check-match-limits
2599 'markdown-tilde-fence-begin 2 (marker-position start-lang-1)
2600 (marker-position end-lang-1) (marker-position start-lang-1)))
2601 ;; check text in between
2602 (should (markdown-test-check-match-limits
2603 'markdown-fenced-code 0 (marker-position start-mid-1)
2604 (marker-position end-mid-1) (marker-position start-mid-1)))
2605 ;; check bottom tildes
2606 (should (markdown-test-check-match-limits
2607 'markdown-tilde-fence-end 1 (marker-position start-bottom-1)
2608 (marker-position end-bottom-1) (marker-position start-bottom-1)))
2609 ;; Point between code blocks
2610 (set-marker between 47)
2611 (should (equal (get-text-property between 'markdown-fenced-code)
2612 nil))
2613 ;; Second code block
2614 (set-marker start-top-2 48)
2615 (set-marker end-top-2 51)
2616 (set-marker start-lang-2 52)
2617 (set-marker end-lang-2 57)
2618 (set-marker start-mid-2 58)
2619 (set-marker end-mid-2 93)
2620 (set-marker start-bottom-2 93)
2621 (set-marker end-bottom-2 96)
2622 (should (markdown-test-check-match-limits
2623 'markdown-tilde-fence-begin 1 (marker-position start-top-2)
2624 (marker-position end-top-2) (marker-position start-top-2)))
2625 (should (markdown-test-check-match-limits
2626 'markdown-tilde-fence-begin 2 (marker-position start-lang-2)
2627 (marker-position end-lang-2) (marker-position start-lang-2)))
2628 (should (markdown-test-check-match-limits
2629 'markdown-fenced-code 0 (marker-position start-mid-2)
2630 (marker-position end-mid-2) (marker-position start-mid-2)))
2631 (should (markdown-test-check-match-limits
2632 'markdown-tilde-fence-end 1 (marker-position start-bottom-2)
2633 (marker-position end-bottom-2) (marker-position start-bottom-2)))
2634 ;; ;; Move point between code blocks and insert a character
2635 (goto-char between)
2636 (insert "x")
2637 ;; Re-propertize region after change
2638 (let ((range (markdown-syntax-propertize-extend-region (1- between) (point-max))))
2639 (markdown-syntax-propertize (car range) (cdr range)))
2640 ;; Re-check first code block
2641 (should (markdown-test-check-match-limits
2642 'markdown-tilde-fence-begin 1 (marker-position start-top-1)
2643 (marker-position end-top-1) (marker-position start-top-1)))
2644 (should (markdown-test-check-match-limits
2645 'markdown-tilde-fence-begin 2 (marker-position start-lang-1)
2646 (marker-position end-lang-1) (marker-position start-lang-1)))
2647 (should (markdown-test-check-match-limits
2648 'markdown-fenced-code 0 (marker-position start-mid-1)
2649 (marker-position end-mid-1) (marker-position start-mid-1)))
2650 (should (markdown-test-check-match-limits
2651 'markdown-tilde-fence-end 1 (marker-position start-bottom-1)
2652 (marker-position end-bottom-1) (marker-position start-bottom-1)))
2653 ;; Re-check point between code blocks
2654 (should (equal (get-text-property between 'markdown-fenced-code)
2655 nil))
2656 ;; Re-check second code block
2657 (should (markdown-test-check-match-limits
2658 'markdown-tilde-fence-begin 1 (marker-position start-top-2)
2659 (marker-position end-top-2) (marker-position start-top-2)))
2660 (should (markdown-test-check-match-limits
2661 'markdown-tilde-fence-begin 2 (marker-position start-lang-2)
2662 (marker-position end-lang-2) (marker-position start-lang-2)))
2663 (should (markdown-test-check-match-limits
2664 'markdown-fenced-code 0 (marker-position start-mid-2)
2665 (marker-position end-mid-2) (marker-position start-mid-2)))
2666 (should (markdown-test-check-match-limits
2667 'markdown-tilde-fence-end 1 (marker-position start-bottom-2)
2668 (marker-position end-bottom-2)
2669 (marker-position start-bottom-2))))))
2671 (ert-deftest test-markdown-parsing/propertize-fenced-in-between ()
2672 "Test whether `markdown-syntax-propertize-fenced-block-constructs' handles the
2673 case when it can't propertize both the start and end of a fenced block within a
2674 single pass (the end of the block is past the END argument)."
2675 (markdown-test-string
2676 "~~~ shell
2677 #!/bin/sh
2679 echo \"Hello, world!\"
2682 (set-text-properties (point-min) (point-max) nil)
2683 ;; syntax-propertize up to right after hashbang
2684 (markdown-syntax-propertize-fenced-block-constructs (point-min) 21)
2685 ;; ~~~ shell should be propertized, but nothing else
2686 ;; check tildes
2687 (should (markdown-test-check-match-limits
2688 'markdown-tilde-fence-begin 1 1 4 1))
2689 ;; check language
2690 (should (markdown-test-check-match-limits
2691 'markdown-tilde-fence-begin 2 5 10 5))
2692 ;; middle should not be propertized
2693 (should-not (get-text-property 11 'markdown-fenced-code))
2694 ;; neither should end
2695 (should-not (get-text-property 43 'markdown-tilde-fence-end))
2696 (markdown-syntax-propertize-fenced-block-constructs 21 (point-max))
2697 ;; everything should be propertized now
2698 ;; re-check top
2699 (should (markdown-test-check-match-limits
2700 'markdown-tilde-fence-begin 1 1 4 1))
2701 (should (markdown-test-check-match-limits
2702 'markdown-tilde-fence-begin 2 5 10 5))
2703 ;; check middle
2704 (should (markdown-test-check-match-limits 'markdown-fenced-code 0 10 43 10))
2705 ;; check ending tildes
2706 (should (markdown-test-check-match-limits
2707 'markdown-tilde-fence-end 1 43 46 43))))
2709 (ert-deftest test-markdown-parsing/get-code-block-at-pos ()
2710 "Test whether `markdown-code-block-at-pos' works in all situations. All
2711 situations are:
2712 1. pre block
2713 2. tilde block
2714 3. gfm block
2715 4. yaml metadata block"
2716 (let ((markdown-use-pandoc-style-yaml-metadata t))
2717 (markdown-test-string
2719 ~~~ ruby
2720 some_ruby_fun()
2724 a: b
2727 ``` {.bash}
2728 #!/bin/sh
2729 echo hey
2732 pre code
2733 random stuff
2734 more preformatted code
2737 data: pandoc
2740 ;; start/mid/end at tilde block
2741 (should (equal (markdown-code-block-at-pos 2) (list 2 30)))
2742 (should (equal (markdown-code-block-at-pos 11) (list 2 30)))
2743 (should (equal (markdown-code-block-at-pos 27) (list 2 30)))
2744 ;; yaml metadata block
2745 (should (equal (markdown-code-block-at-pos 32) (list 32 44)))
2746 (should (equal (markdown-code-block-at-pos 36) (list 32 44)))
2747 (should (equal (markdown-code-block-at-pos 41) (list 32 44)))
2748 ;; gfm block
2749 (should (equal (markdown-code-block-at-pos 46) (list 46 80)))
2750 (should (equal (markdown-code-block-at-pos 58) (list 46 80)))
2751 (should (equal (markdown-code-block-at-pos 77) (list 46 80)))
2752 ;; pre block
2753 (should (equal (markdown-code-block-at-pos 82) (list 82 138)))
2754 (should (equal (markdown-code-block-at-pos 99) (list 82 138)))
2755 (should (equal (markdown-code-block-at-pos 137) (list 82 138)))
2756 ;; pandoc yaml metadata block (should work if yaml above works)
2757 (should (equal (markdown-code-block-at-pos 140) (list 140 160)))
2758 (should (equal (markdown-code-block-at-pos 142) (list 140 160)))
2759 (should (equal (markdown-code-block-at-pos 144) (list 140 160)))
2760 (should (equal (markdown-code-block-at-pos 157) (list 140 160)))
2761 (should (equal (markdown-code-block-at-pos 159) (list 140 160))))))
2763 (ert-deftest test-markdown-parsing/syntax-get-fenced-blocks ()
2764 "Test whether *-get-fenced-block-* functions work in the case where a block is
2765 only partially propertized."
2766 (save-match-data
2767 (markdown-test-string
2768 "~~~
2770 (should (equal (markdown-syntax-propertize-extend-region
2771 (point-min) (point-max))
2772 nil))
2773 (goto-char 1)
2774 (set-match-data (markdown-text-property-at-point
2775 'markdown-tilde-fence-begin))
2776 (should (equal (markdown-get-fenced-block-from-start
2777 'markdown-tilde-fence-begin)
2778 nil)))
2779 (markdown-test-string
2780 "~~~
2781 ~~~"
2782 (goto-char 1)
2783 (set-match-data (markdown-text-property-at-point
2784 'markdown-tilde-fence-begin))
2785 (should (equal (markdown-get-fenced-block-from-start
2786 'markdown-tilde-fence-begin)
2787 (list 1 8)))
2788 (should (equal (markdown-code-block-at-point) (list 1 8)))
2789 (goto-char 5)
2790 (set-match-data (markdown-text-property-at-point
2791 'markdown-tilde-fence-end))
2792 (should (equal (markdown-get-fenced-block-from-end
2793 'markdown-tilde-fence-end)
2794 (list 1 8)))
2795 (should (equal (markdown-code-block-at-point) (list 1 8))))
2796 (markdown-test-string
2797 "~~~
2799 ~~~"
2800 (goto-char 1)
2801 (set-match-data (markdown-text-property-at-point
2802 'markdown-tilde-fence-begin))
2803 (should (equal (markdown-get-fenced-block-from-start
2804 'markdown-tilde-fence-begin)
2805 (list 1 9)))
2806 (should (equal (markdown-code-block-at-point) (list 1 9)))
2807 (goto-char 5)
2808 (set-match-data (markdown-text-property-at-point 'markdown-fenced-code))
2809 (should (equal (markdown-get-fenced-block-from-middle
2810 'markdown-fenced-code)
2811 (list 1 9)))
2812 (should (equal (markdown-code-block-at-point) (list 1 9)))
2813 (goto-char 6)
2814 (set-match-data (markdown-text-property-at-point
2815 'markdown-tilde-fence-end))
2816 (should (equal (markdown-get-fenced-block-from-end
2817 'markdown-tilde-fence-end)
2818 (list 1 9)))
2819 (should (equal (markdown-code-block-at-point) (list 1 9))))))
2821 (ert-deftest test-markdown-parsing/reference-definition-basic ()
2822 "Test reference definition function."
2823 (markdown-test-file "syntax.text"
2824 ;; Test accuracy of returned text and bounds
2825 (should (equal (markdown-reference-definition "1")
2826 (list "http://docutils.sourceforge.net/mirror/setext.html" 1942 1992)))
2827 (should (equal (markdown-reference-definition "2")
2828 (list "http://www.aaronsw.com/2002/atx/" 2000 2032)))
2829 ;; Test that match data remains intact
2830 (should (string-equal (match-string 5) "http://www.aaronsw.com/2002/atx/"))
2831 ;; Test anchor-only relative URL
2832 (should (equal (markdown-reference-definition "bq")
2833 (list "#blockquote" 7536 7547)))
2834 ;; Example references that appear in pre blocks in the text
2835 (should (not (markdown-reference-definition "")))
2836 (should (not (markdown-reference-definition "id")))
2837 (should (not (markdown-reference-definition "foo")))
2838 (should (not (markdown-reference-definition "A")))
2839 (should (not (markdown-reference-definition "Google")))
2840 ;; Test that we don't pick up other text in square brackets
2841 (should (not (markdown-reference-definition "blockquoting")))
2842 (should (not (markdown-reference-definition "square brackets")))
2843 ;; Test case insensitivity
2844 (should (equal (markdown-reference-definition "SRC")
2845 (list "/projects/markdown/syntax.text" 1245 1275)))))
2847 (ert-deftest test-markdown-parsing/get-defined-references ()
2848 "Test `markdown-get-defined-references'."
2849 (markdown-test-file "syntax.text"
2850 (should (equal (markdown-get-defined-references)
2851 '("src" "1" "2" "3" "4" "5" "6" "bq" "l"))))
2852 (markdown-test-file "outline.text"
2853 (should (equal (markdown-get-defined-references) nil)))
2854 (markdown-test-file "wiki-links.text"
2855 (should (equal (markdown-get-defined-references) nil))))
2857 (defun markdown-test-test-region (beg end)
2858 (goto-char (1- beg))
2859 (should-not (markdown-code-at-point-p))
2860 (goto-char (1+ end))
2861 (should-not (markdown-code-at-point-p))
2862 (dolist (loc (number-sequence beg end))
2863 (goto-char loc)
2864 (should (markdown-code-at-point-p))
2865 (should (equal (match-beginning 0) beg))
2866 (should (equal (match-end 0) end))))
2868 (ert-deftest test-markdown-parsing/code-at-point-inline ()
2869 "Test `markdown-code-at-point-p'."
2870 (markdown-test-file "inline.text"
2871 (markdown-test-test-region 45 51) ; Regular code span
2872 (markdown-test-test-region 61 90) ; Code containing backticks
2873 (markdown-test-test-region 228 240) ; Backquotes at beginning
2874 (markdown-test-test-region 341 352) ; Backquotes at end
2875 (markdown-test-test-region 460 469) ; Backslash as final character
2876 (markdown-test-test-region 657 667) ; A code span crossing lines
2877 (markdown-test-test-region 749 758) ; Three backquotes on same line
2878 (markdown-test-test-region 806 815) ; Three backquotes across lines
2881 (ert-deftest test-markdown-parsing/code-at-point-one-space ()
2882 "Test `markdown-code-at-point-p' with multiple code spans in a row."
2883 (markdown-test-string "`foo` `bar` `baz`"
2884 (dolist (loc (number-sequence 1 6))
2885 (goto-char loc)
2886 (should (markdown-code-at-point-p))
2887 (should (equal (match-data) (list 1 6 1 2 2 5 5 6))))
2888 (dolist (loc (number-sequence 7 12))
2889 (goto-char loc)
2890 (should (markdown-code-at-point-p))
2891 (should (equal (match-data) (list 7 12 7 8 8 11 11 12))))
2892 (dolist (loc (number-sequence 13 18))
2893 (goto-char loc)
2894 (should (markdown-code-at-point-p))
2895 (should (equal (match-data) (list 13 18 13 14 14 17 17 18))))))
2897 (ert-deftest test-markdown-parsing/code-at-point-no-space ()
2898 "Test `markdown-code-at-point-p' with multiple code spans in a row."
2899 (markdown-test-string "a`foo`b`bar`c`baz`d"
2900 (goto-char 1) ; "a"
2901 (should-not (markdown-code-at-point-p))
2902 (dolist (loc (number-sequence 2 7)) ; "`foo`b"
2903 (goto-char loc)
2904 (should (markdown-code-at-point-p))
2905 (should (equal (match-data) (list 2 7 2 3 3 6 6 7))))
2906 (dolist (loc (number-sequence 8 13)) ; "`bar`c"
2907 (goto-char loc)
2908 (should (markdown-code-at-point-p))
2909 (should (equal (match-data) (list 8 13 8 9 9 12 12 13))))
2910 (dolist (loc (number-sequence 14 19)) ; "`baz`d"
2911 (goto-char loc)
2912 (should (markdown-code-at-point-p))
2913 (should (equal (match-data) (list 14 19 14 15 15 18 18 19))))))
2915 (ert-deftest test-markdown-parsing/code-at-point-blank-line ()
2916 "Test `markdown-code-at-point-p' at beginning of block."
2917 (markdown-test-string "----------\n\n## foo\n"
2918 (should-not (markdown-code-at-point-p))
2919 (forward-line)
2920 (should-not (markdown-code-at-point-p))
2921 (forward-line)
2922 (should-not (markdown-code-at-point-p))))
2924 (ert-deftest test-markdown-parsing/match-comments ()
2925 "Test `markdown-match-comments'."
2926 (markdown-test-string
2927 "HTML <!-- foo --> comment"
2928 (should (markdown-match-comments (point-max)))
2929 (should (eq (point) 18))
2930 (should (equal (match-data) (list 6 18)))
2931 (should-not (markdown-match-comments (point-max)))))
2933 (ert-deftest test-markdown-parsing/range-property-any ()
2934 "Test behavior of `markdown-range-property-any'."
2935 (markdown-test-file
2936 "inline.text"
2937 (should (markdown-range-property-any
2938 (point-min) (point-at-eol)
2939 'face (list markdown-markup-face
2940 markdown-italic-face)))
2941 (should-not (markdown-range-property-any
2942 (point-min) (point-at-eol)
2943 'face (list markdown-bold-face)))))
2945 (ert-deftest test-markdown-parsing/inline-code ()
2946 "Don't cause infinite loop for inline code just after metadata block
2947 Detail: https://github.com/jrblevin/markdown-mode/issues/115"
2948 (markdown-test-string "---
2949 x: x
2953 (should (= (markdown-match-code (point-max)) (point-max)))))
2955 ;;; Reference Checking:
2957 (ert-deftest test-markdown-references/goto-line-button ()
2958 "Create and test a goto line button."
2959 (markdown-test-string "line 1\nline 2\n"
2960 ;; Store the temporary buffer with the text
2961 (let ((target (current-buffer)))
2962 ;; Create a new buffer for inserting
2963 (with-temp-buffer
2964 ;; Verify that point is in a different buffer
2965 (should (not (equal (current-buffer) target)))
2966 ;; Insert and press the button
2967 (insert-button "goto line 2"
2968 :type 'markdown-goto-line-button
2969 'target-buffer target
2970 'target-line 2)
2971 (should (string-equal (buffer-string) "goto line 2"))
2972 (backward-button 1)
2973 (call-interactively 'push-button)
2974 ;; Verify that point is on line 2 of target buffer
2975 (should (= (line-number-at-pos) 2))
2976 (should (looking-at "line 2"))
2977 (should (equal (current-buffer) target))))))
2979 (ert-deftest test-markdown-references/button-map ()
2980 "Verify that button-buffer-map is used for check references buffer."
2981 (markdown-test-string "[undefined][ref]\n"
2982 (let* ((target (buffer-name))
2983 (check (format "*Undefined references for %s*" target)))
2984 (markdown-check-refs)
2985 (with-current-buffer (get-buffer check)
2986 (should (equal (local-key-binding (kbd "TAB")) 'forward-button))
2987 (should (equal (local-key-binding (kbd "<backtab>")) 'backward-button))))))
2989 ;;; Lists:
2991 (ert-deftest test-markdown-lists/levels-1 ()
2992 "Test list levels function `markdown-calculate-list-levels'."
2993 (markdown-test-file "nested-list.text"
2994 (let ((values '(((1 . 1) . nil) ((2 . 13) . (3)) ((14 . 23) . (7 3))
2995 ((24 . 26) . (11 7 3)))))
2996 (cl-loop for (range . value) in values
2997 do (goto-char (point-min))
2998 (forward-line (1- (car range)))
2999 (dotimes (n (- (cdr range) (car range)))
3000 (should (equal (markdown-calculate-list-levels) value))
3001 (forward-line))))))
3003 (ert-deftest test-markdown-lists/levels-2 ()
3004 "Test list levels function `markdown-calculate-list-levels'."
3005 (markdown-test-file "syntax.text"
3006 (let ((values '(((1 . 13) . nil) ((14 . 14) . (0)) ((15 . 17) . (4 0))
3007 ((18 . 18) . (0)) ((19 . 24) . (4 0)) ((25 . 25) . (0))
3008 ((26 . 29) . (4 0)) ((30 . 30) . (0)) ((31 . 33) . (4 0))
3009 ((34 . 588) . nil) ((589 . 595) . (0)) ((596 . 814) . nil)
3010 ((815 . 820) . (0)) ((821 . 898) . nil))))
3011 (cl-loop for (range . value) in values
3012 do (goto-char (point-min))
3013 (forward-line (1- (car range)))
3014 (dotimes (n (- (cdr range) (car range)))
3015 (should (equal (markdown-calculate-list-levels) value))
3016 (forward-line))))))
3018 (ert-deftest test-markdown-lists/levels-interior ()
3019 "Test `markdown-calculate-list-levels' from inside a list item."
3020 (markdown-test-file "nested-list.text"
3021 (goto-char 36)
3022 (should (equal (markdown-calculate-list-levels) (list 3)))
3023 (goto-char 267)
3024 (should (equal (markdown-calculate-list-levels) (list 7 3)))
3025 (goto-char 540)
3026 (should (equal (markdown-calculate-list-levels) (list 11 7 3)))))
3028 (ert-deftest test-markdown-lists/bounds-1 ()
3029 "Test list item bounds function `markdown-cur-list-item-bounds'."
3030 (markdown-test-file "lists.text"
3031 (markdown-test-goto-heading "Case 9")
3032 (forward-line)
3033 (should (eq (point) 3699))
3034 (markdown-next-list-item 4)
3035 (should (eq (point) 3700))
3036 (should (equal (markdown-cur-list-item-bounds)
3037 (list 3700 3901 0 4 "- ")))
3038 (markdown-next-list-item 4)
3039 (should (eq (point) 3903))
3040 (should (equal (markdown-cur-list-item-bounds)
3041 (list 3903 3937 0 4 "* ")))))
3043 (ert-deftest test-markdown-lists/bounds-2 ()
3044 "Function `markdown-cur-list-item-bounds' should return nil outside of list items."
3045 (markdown-test-string "line one\n\n* item\n"
3046 (should (null (markdown-cur-list-item-bounds)))
3047 (forward-line)
3048 (should (null (markdown-cur-list-item-bounds)))
3049 (forward-line)
3050 (should (markdown-cur-list-item-bounds))))
3052 (ert-deftest test-markdown-lists/promotion-and-demotion ()
3053 "Test function `markdown-promote-list-item'."
3054 (markdown-test-file "nested-list.text"
3055 (forward-line)
3056 (should (looking-at " - List level 1 item 2
3058 Second paragraph of item 2
3060 Nested pre block in item 2
3061 Four spaces past the marker
3063 Another paragraph of item 2"))
3064 (markdown-demote-list-item)
3065 (should (looking-at " - List level 1 item 2
3067 Second paragraph of item 2
3069 Nested pre block in item 2
3070 Four spaces past the marker
3072 Another paragraph of item 2"))
3073 (markdown-promote-list-item)
3074 (should (looking-at " - List level 1 item 2
3076 Second paragraph of item 2
3078 Nested pre block in item 2
3079 Four spaces past the marker
3081 Another paragraph of item 2"))
3082 (goto-char (point-min))
3083 (forward-line 22)
3084 (should (looking-at " - List level 3 item 1
3086 Nested pre block"))
3087 (markdown-demote-list-item)
3088 (should (looking-at " - List level 3 item 1
3090 Nested pre block"))
3091 (markdown-promote-list-item)
3092 (should (looking-at " - List level 3 item 1
3094 Nested pre block"))))
3096 (ert-deftest test-markdown-lists/promotion-and-demotion-custom ()
3097 "Test custom variable `markdown-list-indent-width'."
3098 (markdown-test-file "nested-list.text"
3099 (forward-line)
3100 (should (looking-at " - List level 1 item 2
3102 Second paragraph of item 2
3104 Nested pre block in item 2
3105 Four spaces past the marker
3107 Another paragraph of item 2"))
3108 (let ((markdown-list-indent-width 2))
3109 (markdown-demote-list-item))
3110 (should (looking-at " - List level 1 item 2
3112 Second paragraph of item 2
3114 Nested pre block in item 2
3115 Four spaces past the marker
3117 Another paragraph of item 2"))))
3119 ;;; Outline minor mode tests:
3121 (ert-deftest test-markdown-outline/navigation ()
3122 "Test outline navigation functions."
3123 (markdown-test-file "outline.text"
3124 ;; Navigate to the first visible heading
3125 (markdown-next-visible-heading 1)
3126 (should (eq (point) 19))
3127 (should (looking-at "^# A top-level header"))
3128 ;; Navigate forward at the same level
3129 (markdown-forward-same-level 1)
3130 (should (eq (point) 377))
3131 (should (looking-at "^=+$"))
3132 ;; Navigate backward by four visible headings
3133 (markdown-previous-visible-heading 4)
3134 (should (eq (point) 69))
3135 (should (looking-at "^## A second-level header$"))))
3137 (ert-deftest test-markdown-outline/navigation-with-code ()
3138 "Test outline navigation functions with code blocks."
3139 (markdown-test-file "outline-code.text"
3140 ;; Navigate forward at the same level
3141 (markdown-forward-same-level 1)
3142 (should (eq (point) 159))
3143 (should (looking-at "^# Level one again"))))
3145 (ert-deftest test-markdown-outline/visibility-atx ()
3146 "Test outline visibility cycling for ATX-style headers."
3147 (markdown-test-file "outline.text"
3148 (let (last-command this-command)
3149 ;; Navigate to the second visible heading
3150 (markdown-next-visible-heading 2)
3151 (should (eq (point) 69))
3152 (should (looking-at "^## A second-level header$"))
3153 ;; Cycle visibility of this subtree
3154 (setq this-command 'markdown-cycle)
3155 (markdown-cycle)
3156 (setq last-command 'markdown-cycle)
3157 (should (eq (point) 69))
3158 (should (looking-at "^## A second-level header$"))
3159 ;; Test that the entire subtree is invisible
3160 (markdown-test-range-has-property 93 349 'invisible 'outline)
3161 ;; Cycle visibility of this subtree again
3162 (markdown-cycle)
3163 (should (eq (point) 69))
3164 (should (looking-at "^## A second-level header$"))
3165 ;; Test that text is visible
3166 (markdown-test-range-has-property 95 121 'invisible nil)
3167 ;; Test that subheadings are visible
3168 (markdown-test-range-has-property 123 141 'invisible nil)
3169 ;; Cycle visibility of this subtree again
3170 (markdown-cycle)
3171 (should (eq (point) 69))
3172 (should (looking-at "^## A second-level header$"))
3173 ;; Verify that entire subtree is visible
3174 (markdown-test-range-has-property 93 349 'invisible nil))))
3176 (ert-deftest test-markdown-outline/visibility-setext ()
3177 "Test outline visibility cycling for setext-style headers."
3178 (markdown-test-file "outline.text"
3179 ;; Navigate to the sixth visible heading
3180 (markdown-next-visible-heading 7)
3181 (markdown-previous-visible-heading 1)
3182 (should (looking-at markdown-regex-header))
3183 (should (string-equal (match-string-no-properties 1) "An underline-style header"))
3184 (should (string-equal (match-string-no-properties 2) "========================="))
3185 ;; Cycle visibility subtree, test that it's invisible
3186 (markdown-cycle)
3187 (markdown-test-range-has-property 404 515 'invisible 'outline)
3188 ;; Cycle visibility subtree, test that text and headers are visible
3189 (markdown-cycle)
3190 (markdown-test-range-has-property 404 417 'invisible nil)
3191 (markdown-test-range-has-property 420 451 'invisible nil)))
3193 (ert-deftest test-markdown-outline/visibility-with-code ()
3194 "Test outline visibility cycling with code blocks."
3195 (markdown-test-file "outline-code.text"
3196 (let (last-command this-command)
3197 ;; Cycle global visibility to "overview" mode
3198 (setq this-command 'markdown-cycle)
3199 (markdown-cycle t)
3200 (setq last-command 'markdown-cycle)
3201 (should (eq (point) (point-min)))
3202 (should (looking-at "^# Level one"))
3203 ;; Test that the code block is invisible
3204 (markdown-test-range-has-property 83 157 'invisible 'outline)
3205 ;; Check subsequent headings
3206 (outline-next-visible-heading 1)
3207 (should (eq (point) 69))
3208 (should (looking-at "^## Level two"))
3209 (outline-next-visible-heading 1)
3210 (should (eq (point) 159))
3211 (should (looking-at "^# Level one again")))))
3213 (ert-deftest test-markdown-outline/visibility-with-metadata ()
3214 "Test outline visibility cycling with metadata blocks."
3215 (markdown-test-string
3216 "---
3217 layout = post
3218 date = 2015-08-13 11:35:25 EST
3221 (let (last-command this-command)
3222 ;; Cycle global visibility to "overview" mode
3223 (setq this-command 'markdown-cycle)
3224 (markdown-cycle t)
3225 ;; Check that text is visible
3226 (markdown-test-range-has-property (point-min) (point-max) 'invisible nil))))
3228 ;;; Movement tests:
3230 (ert-deftest test-markdown-movement/defun ()
3231 "Test defun navigation."
3232 (markdown-test-file "outline.text"
3233 ;; end-of-defun should go to point-max
3234 (end-of-defun 10)
3235 (should (= (point) (point-max)))
3236 ;; end-of-defun should stop just before the next header
3237 (goto-char (point-min))
3238 (end-of-defun)
3239 (should (looking-at "\n# A top-level header"))
3240 (end-of-defun)
3241 (should (looking-at "\n## A second-level header"))
3242 (end-of-defun)
3243 (should (looking-at "\n### Third level ###"))
3244 (end-of-defun)
3245 (should (looking-at "\n### Third level number two ###"))
3246 ;; beginning-of-defun should move to the start of the previous header
3247 (beginning-of-defun)
3248 (should (looking-at "### Third level ###"))
3249 (beginning-of-defun)
3250 (should (looking-at "## A second-level header"))
3251 (beginning-of-defun)
3252 (should (looking-at "# A top-level header"))
3253 (beginning-of-defun)
3254 ;; beginning-of-defun should move up to point-min
3255 (should (= (point) (point-min)))
3256 ;; (beginning-of-defun -1) should move to the start of the next header
3257 (forward-line 2)
3258 (beginning-of-defun -1)
3259 (should (looking-at "## A second-level header"))
3260 (beginning-of-defun -1)
3261 (should (looking-at "### Third level ###"))
3262 (beginning-of-defun -1)
3263 (should (looking-at "### Third level number two ###"))))
3265 (ert-deftest test-markdown-movement/block ()
3266 "Test block movement."
3267 (markdown-test-file "outline.text"
3268 (markdown-end-of-block)
3269 (should (looking-at "\n# A top-level header"))
3270 (markdown-end-of-block)
3271 (should (looking-at "\nfollowed by some body text"))
3272 (markdown-end-of-block)
3273 (should (looking-at "\n## A second-level header"))
3274 (markdown-end-of-block)
3275 (should (looking-at "\nfollowed by some body text"))
3276 (markdown-end-of-block)
3277 (should (looking-at "\n### Third level ###"))
3278 (markdown-end-of-block)
3279 (should (looking-at "\n\\* A list item"))
3280 (markdown-end-of-block)
3281 (should (looking-at "\n### Third level number two ###"))
3282 (markdown-end-of-block)
3283 (should (looking-at "\n### Level two again"))
3284 (markdown-end-of-block)
3285 (should (looking-at "\nfollowed by some body text"))
3287 (markdown-test-goto-heading "Level two")
3288 (markdown-end-of-block)
3289 (should (looking-at "\nbar"))
3290 (markdown-end-of-block)
3291 (should (= (point) (point-max)))
3292 (markdown-beginning-of-block)
3293 (should (looking-at "bar"))
3294 (markdown-beginning-of-block)
3295 (should (looking-at "## Level two"))
3296 (markdown-beginning-of-block)
3297 (should (looking-at "foo"))
3298 (markdown-beginning-of-block)
3299 (should (looking-at "# Level one"))
3300 (markdown-beginning-of-block)
3301 (should (looking-at "* With"))
3302 (markdown-beginning-of-block)
3303 (should (looking-at "And a level two underline header"))
3305 (goto-char (point-min))
3306 (markdown-test-goto-heading "A top-level header")
3307 (beginning-of-line)
3308 (markdown-beginning-of-block)
3309 (should (= (point) (point-min)))))
3311 (ert-deftest test-markdown-movement/blockquote-paragraphs ()
3312 "Test filling of blockquotes containing multiple paragraphs."
3313 (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"
3314 (forward-paragraph)
3315 (should (looking-at "^>$"))
3316 (should (= (point) 128))
3317 (forward-paragraph)
3318 (should (= (point) (point-max)))))
3320 (ert-deftest test-markdown-movement/reference-definition ()
3321 "Test jumping to reference definitions."
3322 ;; Jumping to explicit reference definition
3323 (markdown-test-string "[a][ref]\n\n[ref]: gopher://localhost/\n"
3324 (markdown-reference-goto-definition)
3325 (should (= (point) 18)))
3326 ;; Jumping to implicit reference definition
3327 (markdown-test-string "[a][]\n\n[a]: ftp://localhost/\n"
3328 (markdown-reference-goto-definition)
3329 (should (= (point) 13)))
3330 ;; Creating non-existent reference definition
3331 (markdown-test-string "[a][]\n"
3332 (markdown-reference-goto-definition)
3333 (should (= (point) 13))
3334 (should (string-equal (buffer-string) "[a][]\n\n[a]: \n"))))
3336 (ert-deftest test-markdown-movement/back-to-same-level-over-code-block ()
3337 "`markdown-backward-same-level' over code block which contains header
3338 like statement. Detail: https://github.com/jrblevin/markdown-mode/issues/75"
3339 (markdown-test-string "
3340 ## Header 2-1
3342 ## Header 2-2
3344 ```R
3345 # Header Like Statement
3348 ## Header 2-3
3350 (search-forward "## Header 2-3")
3351 (let ((last-header-pos (point)))
3352 (forward-line -1)
3353 (call-interactively #'markdown-backward-same-level)
3354 (should (looking-at-p "## Header 2-1"))
3356 (goto-char last-header-pos)
3357 (call-interactively #'markdown-backward-same-level)
3358 (should (looking-at-p "## Header 2-2"))
3360 (goto-char last-header-pos)
3361 (markdown-backward-same-level 2)
3362 (should (looking-at-p "## Header 2-1"))
3364 (search-forward "# Header Like Statement")
3365 (call-interactively #'markdown-backward-same-level)
3366 (should (looking-at-p "## Header 2-1")))))
3368 ;;; Link tests:
3370 (ert-deftest test-markdown-link/follow ()
3371 "Test link following in a browser and in Emacs."
3372 (markdown-test-string "[text](http://path?query=foo#id)"
3373 (let* ((opened-url nil)
3374 (browse-url-browser-function
3375 (lambda (url &rest args) (setq opened-url url))))
3376 (markdown-follow-thing-at-point nil)
3377 (should (equal opened-url "http://path?query=foo#id"))))
3378 (when (featurep 'url-parse)
3379 (markdown-test-string "[text](path?query=foo#id)"
3380 (markdown-follow-thing-at-point nil)
3381 (should (equal (file-name-nondirectory (buffer-file-name)) "path"))
3382 (kill-buffer))))
3384 ;;; Wiki link tests:
3386 (ert-deftest test-markdown-wiki-link/file-local-variables ()
3387 "Test enabling wiki links via file-local variables."
3388 (markdown-test-file "wiki-links.text"
3389 (should-not markdown-enable-wiki-links)
3390 (hack-local-variables)
3391 (should markdown-enable-wiki-links)))
3393 (ert-deftest test-markdown-wiki-link/aliasing ()
3394 "Test filename extraction for aliased wiki links."
3395 (let ((markdown-enable-wiki-links t))
3396 (markdown-test-file "wiki-links.text"
3397 ;; Confirm location of first wiki link
3398 (should (eq (markdown-next-link) 8))
3399 ;; Confirm location of second wiki link
3400 (should (eq (markdown-next-link) 73))
3401 ;; Test predicate function
3402 (should (markdown-wiki-link-p))
3403 ;; Test alias-first filename extraction
3404 (setq markdown-wiki-link-alias-first t)
3405 (should (string-equal (markdown-wiki-link-link) "second"))
3406 ;; Test alias-second filename extraction
3407 (setq markdown-wiki-link-alias-first nil)
3408 (should (string-equal (markdown-wiki-link-link) "first")))))
3410 (ert-deftest test-markdown-wiki-link/navigation ()
3411 "Test wiki link navigation."
3412 (let ((markdown-enable-wiki-links t))
3413 (markdown-test-file "wiki-links.text"
3414 ;; Advance to first link
3415 (should (eq (markdown-next-link) 8))
3416 ;; Advance to second link
3417 (should (eq (markdown-next-link) 73))
3418 ;; Avance to final link
3419 (should (eq (markdown-next-link) 155))
3420 ;; Return nil and don't advance point
3421 (should (eq (markdown-next-link) nil))
3422 (should (eq (point) 155))
3423 ;; Move back to second link
3424 (should (eq (markdown-previous-link) 73))
3425 ;; Move back to first link
3426 (should (eq (markdown-previous-link) 8))
3427 ;; Return nil and don't move point
3428 (should (eq (markdown-previous-link) nil))
3429 (should (eq (point) 8)))))
3431 (ert-deftest test-markdown-wiki-link/font-lock ()
3432 "Test font lock faces for wiki links."
3433 (markdown-test-temp-file "wiki-links.text"
3434 (let* ((fn (concat (file-name-directory buffer-file-name)
3435 "inline.text"))
3436 (markdown-enable-wiki-links t))
3437 ;; Create inline.text in the same temp directory, refontify
3438 (write-region "" nil fn nil 1)
3439 (markdown-fontify-buffer-wiki-links)
3440 ;; Confirm location of first wiki link
3441 (should (eq (markdown-next-link) 8))
3442 ;; First wiki link doesn't have a corresponding file
3443 (markdown-test-range-has-property 8 20 'font-lock-face markdown-missing-link-face)
3444 ;; Second wiki link doesn't have a corresponding file
3445 (should (eq (markdown-next-link) 73))
3446 (markdown-test-range-has-property 73 88 'font-lock-face markdown-missing-link-face)
3447 ;; Move to third wiki link, and create the missing file
3448 (should (eq (markdown-next-link) 155))
3449 (should (string-equal (markdown-wiki-link-link) "inline"))
3450 (markdown-test-range-has-property 155 164 'font-lock-face markdown-link-face)
3451 ;; Check wiki links in code blocks
3452 (markdown-test-range-has-face 360 395 markdown-pre-face)
3453 ;; Remove temporary files
3454 (delete-file fn)
3457 (ert-deftest test-markdown-wiki-link/kill ()
3458 "Simple tests for `markdown-kill-thing-at-point' for wiki links."
3459 (let ((kill-ring nil)
3460 (markdown-enable-wiki-links t)
3461 (tests (list '("[[foo]]" . "foo")
3462 '("[[foo|bar]]" . "bar"))))
3463 (dolist (test tests)
3464 ;; Load test string (the car), move to end of first line, kill
3465 ;; thing at point, and then verify that the kill ring contains cdr.
3466 (markdown-test-string (car test)
3467 (end-of-line)
3468 (call-interactively 'markdown-kill-thing-at-point)
3469 (should (string-equal (current-kill 0) (cdr test)))))))
3471 ;;; Filling tests:
3473 (ert-deftest test-markdown-filling/blockquote ()
3474 "Test filling of blockquotes.
3475 See `adaptive-fill-first-line-regexp'."
3476 (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
3477 (fill-paragraph)
3478 (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."))))
3480 (ert-deftest test-markdown-filling/blockquote-paragraphs ()
3481 "Test filling of blockquotes containing multiple paragraphs."
3482 (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"
3483 (forward-paragraph)
3484 (fill-paragraph)
3485 (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"))))
3487 (ert-deftest test-markdown-filling/space-after-list-marker ()
3488 "`fill-paragraph' should preserve more than one space after a list marker,
3489 since users may wish to indent their lists more than one space more than the
3490 width of the marker. The examples on the Markdown Syntax page have three
3491 spaces after the list marker for a total indentation of four."
3492 (let ((str "\n\n* List item indented four spaces.\n* Also four spaces."))
3493 (markdown-test-string str
3494 (forward-line 2)
3495 (fill-paragraph)
3496 (should (string-equal (buffer-string) str)))))
3498 (ert-deftest test-markdown-filling/multi-line-list-with-more-space ()
3499 "`fill-paragraph' should preserve more than one space after a list marker
3500 (see `test-preserve-space-after-list-marker')."
3501 (let ((str "* This list item is continued on\n the next line"))
3502 (markdown-test-string str
3503 ;; The first line is exactly 35 columns
3504 (let ((fill-column 35))
3505 (fill-paragraph)
3506 (should (string-equal (buffer-string) str))))))
3508 (ert-deftest test-markdown-filling/definition-list-add-leading-spaces ()
3509 "`fill-paragraph' should adapt to spaces after list marker."
3510 (markdown-test-string
3511 ": This list item is continued on the next line"
3512 (let ((fill-column 35))
3513 (fill-paragraph)
3514 (should (string-equal
3515 (buffer-string)
3516 ": This list item is continued on\n the next line")))))
3518 (ert-deftest test-markdown-filling/definition-list-preserve-leading-spaces ()
3519 "`fill-paragraph' should preserve spaces after list marker."
3520 (let ((str ": This list item is continued on\n the next line")
3521 (fill-column 35))
3522 (markdown-test-string
3523 str (fill-paragraph)
3524 (should (string-equal (buffer-string) str)))))
3526 (ert-deftest test-markdown-filling/list-item-plus ()
3527 "Test filling of list items with plus sign markers.
3528 See `adaptive-fill-regexp'."
3529 (markdown-test-string " + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
3530 (fill-paragraph)
3531 (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."))))
3533 (ert-deftest test-markdown-filling/list-item-plus-in-blockquote ()
3534 "Test filling of list items with plus sign markers inside blockquote.
3535 See `adaptive-fill-regexp'."
3536 (markdown-test-string "> + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
3537 (fill-paragraph)
3538 (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."))))
3540 (ert-deftest test-markdown-filling/line-break ()
3541 "Test filling of paragraphs with hard line breaks.
3542 See `paragraph-separate'."
3543 (markdown-test-string "Lorem ipsum dolor sit amet, \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
3544 (let ((fill-column 70))
3545 (fill-paragraph)
3546 (should (string-equal (buffer-string) "Lorem ipsum dolor sit amet, \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut\nlabore et dolore magna aliqua.")))))
3548 (ert-deftest test-markdown-filling/decimal-number-at-beginning ()
3549 "Test filling when a number with a decimal appears at the beginning of a line."
3550 (markdown-test-string "The circumference of a circle divided by it's radius is around\n3.14."
3551 (fill-paragraph)
3552 (should (string-equal (buffer-string) "The circumference of a circle divided by it's radius is around 3.14."))))
3554 (ert-deftest test-markdown-filling/avoid-unintended-list-item ()
3555 "Avoid breaking lines where it would result in an unintended list item."
3556 (markdown-test-string "Lorem ipsum dolor sit 4. amet"
3557 (let ((fill-column 22))
3558 (fill-paragraph)
3559 (should (string-equal (buffer-string) "Lorem ipsum dolor\nsit 4. amet")))))
3561 (ert-deftest test-markdown-filling/no-break-link-reference ()
3562 "Shouldn't break line between label and url, or combine two link references."
3563 (let ((str "[label1]: http://long-url.example.com\n[label2]: http://another-long-url.example.com/"))
3564 (markdown-test-string str
3565 (let ((fill-column 15)) ; after end of label, before end of URL
3566 (fill-paragraph)
3567 (should (string-equal (buffer-string) str))))))
3569 (ert-deftest test-markdown-filling/no-break-before-list-item ()
3570 "There's no point in putting the first item of a list on the next line,
3571 indented the same amount."
3572 :expected-result :failed
3573 (let ((str "* [Link](http://way-too-long.example.com)\n"))
3574 (markdown-test-string str
3575 (auto-fill-mode 1)
3576 (let ((fill-column 10))
3577 (end-of-line)
3578 (funcall auto-fill-function)
3579 (should (string-equal (buffer-string) str))))))
3581 (ert-deftest test-markdown-filling/break-within-list-item ()
3582 "This doesn't suppress auto-fill within a multi-word list item."
3583 :expected-result :failed
3584 (markdown-test-string "* [Link](http://example.com/) more text"
3585 (auto-fill-mode 1)
3586 (let ((fill-column 10))
3587 (end-of-line)
3588 (funcall auto-fill-function)
3589 (should (string-equal
3590 (buffer-string)
3591 "* [Link](http://example.com/)\n more text")))))
3593 (ert-deftest test-markdown-filling/preserve-next-line-footnote ()
3594 "Footnote block can be after label"
3595 (let ((str "[^label1]:\n Footnote block\n more footnote")) ; six spaces
3596 (markdown-test-string str
3597 (let ((fill-column 20)) ; could fit "footnote" after label, but shouldn't
3598 (fill-paragraph)
3599 (should (string-equal (buffer-string) str))))))
3601 (ert-deftest test-markdown-filling/wrap-same-line-footnote ()
3602 "Additional lines must be indented one level (four spaces) when wrapped."
3603 (markdown-test-string "[^label]: Long line should be wrapped"
3604 (let ((fill-column 25)) ; wrap before end of "should"
3605 (fill-paragraph)
3606 (should (string-equal (buffer-string) "[^label]: Long line\n should be wrapped")))))
3608 (ert-deftest test-markdown-filling/wrap-extra-hanging-indentation ()
3609 "Additional lines must be indented one level (four spaces) when wrapped."
3610 (markdown-test-string "[^label]: Long line\n should be wrapped"
3611 (let ((fill-column 25)) ; wrap before end of "should"
3612 (fill-paragraph)
3613 (should (string-equal (buffer-string) "[^label]: Long line\n should be wrapped")))))
3615 (ert-deftest test-markdown-filling/full-justification ()
3616 "Test paragraph detection with lines with lots of whitespace."
3617 (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"
3618 (setq default-justification 'full)
3619 (fill-paragraph)
3620 (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"))
3621 (backward-paragraph)
3622 (forward-paragraph)
3623 (should (= (point) 198))))
3625 (ert-deftest test-markdown-filling/list-line ()
3626 "Test fill-paragraph for list line. Don't insert bullet automatically.
3627 Detail: https://github.com/jrblevin/markdown-mode/issues/79"
3628 (markdown-test-string "* foo foo *foo* foo foo foo foo foo foo"
3629 (let ((fill-column 10))
3630 (fill-paragraph)
3631 (fill-paragraph)
3632 (forward-line 2)
3633 (back-to-indentation)
3634 (should-not (looking-at-p "\\*foo"))
3635 (forward-line 1)
3636 (back-to-indentation)
3637 (should-not (looking-at-p "\\*foo")))))
3639 (ert-deftest test-markdown-filling/ignore-header ()
3640 "# Test fill-paragraph for containing header line paragraph.
3641 https://github.com/jrblevin/markdown-mode/issues/159"
3642 (markdown-test-string "# this is header line
3643 this is not header line
3645 (let ((fill-column 10))
3646 (fill-paragraph)
3647 (should (string= (buffer-substring (point) (line-end-position)) "# this is header line")))))
3649 (ert-deftest test-markdown-filling/unclosed-square-bracket ()
3650 "Test fill-paragraph following an unclosed square bracket."
3651 (markdown-test-string "```\n[3\n```\n\naaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbb"
3652 (let ((fill-column 20))
3653 (forward-line 4)
3654 (fill-paragraph)
3655 (should (looking-at "aaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbb")))))
3657 ;;; Export tests:
3659 (ert-deftest test-markdown-hook/xhtml-standalone ()
3660 "Test `markdown-xhtml-standalone-regexp' and `markdown-output-standalone-p'."
3661 (should (string-match markdown-xhtml-standalone-regexp
3662 "<?xml version='1.0' encoding='UTF-8'?>"))
3663 (should (string-match markdown-xhtml-standalone-regexp
3664 "<!DOCTYPE html>"))
3665 (should (string-match markdown-xhtml-standalone-regexp
3666 "<html>"))
3667 (should-not (string-match markdown-xhtml-standalone-regexp
3668 "<h1>title</h1>"))
3669 (should-not (string-match markdown-xhtml-standalone-regexp
3670 "<div id=\"name\">")))
3672 ;;; Hook tests:
3674 (ert-deftest test-markdown-hook/before-export ()
3675 "Test hook run before export XHTML."
3676 (markdown-test-temp-file "lists.text"
3677 (let* ((before-hook-run nil)
3678 (orig-point (point))
3679 (func (lambda ()
3680 ;; Change value of a variable
3681 (setq before-hook-run t)
3682 ;; Insert some text
3683 (goto-char (point-min))
3684 (insert "#")
3685 ;; Deliberately move the point
3686 (end-of-line)
3687 ;; Verify changes
3688 (should (looking-back "^## List Cases" nil))
3689 (should-not (= (point) orig-point))))
3690 (ofile (progn
3691 ;; Register hook
3692 (add-hook 'markdown-before-export-hook func)
3693 ;; Export XHTML and return filename
3694 (markdown-export)))
3695 (obuffer (get-file-buffer ofile)))
3696 ;; Test side effects of hook
3697 (should (eq before-hook-run t))
3698 ;; Test position of point
3699 (should (= (point) orig-point))
3700 ;; Test that buffer was restored to original state
3701 (goto-char (point-min))
3702 (should (looking-at "^# List Cases"))
3703 ;; Clean
3704 (remove-hook 'markdown-before-export-hook func)
3705 (kill-buffer obuffer)
3706 (delete-file ofile))))
3708 (ert-deftest test-markdown-hook/after-export ()
3709 "Test hook run after export XHTML."
3710 (markdown-test-temp-file "lists.text"
3711 (let* ((after-hook-run nil)
3712 (func (lambda ()
3713 ;; Change variable value
3714 (setq after-hook-run t)
3715 ;; Add comment to output buffer
3716 (goto-char (point-min))
3717 (insert "<!-- after-export-hook -->\n")))
3718 (ofile (progn
3719 ;; Register hook
3720 (add-hook 'markdown-after-export-hook func)
3721 ;; Export XHTML and return filename
3722 (markdown-export)))
3723 (obuffer (get-file-buffer ofile)))
3724 (message "obuffer = %S" obuffer)
3725 ;; Test that variable was changed
3726 (should (eq after-hook-run t))
3727 ;; Test that output buffer remains open
3728 (should (get-buffer obuffer))
3729 ;; Test that output buffer modification remains
3730 (with-current-buffer obuffer
3731 (goto-char (point-min))
3732 (should (looking-at "<!-- after-export-hook -->\n")))
3733 ;; Test that buffer modification was saved
3734 (should-not (buffer-modified-p obuffer))
3735 ;; Clean up
3736 (remove-hook 'markdown-after-export-hook func)
3737 (kill-buffer obuffer)
3738 (delete-file ofile))))
3740 ;;; Extension: math support
3742 (ert-deftest test-markdown-math/file-local-variable ()
3743 "Test enabling math mode via `hack-local-variables-hook'."
3744 (markdown-test-file "math.text"
3745 (should-not markdown-enable-math)
3746 (hack-local-variables)
3747 (should markdown-enable-math)))
3749 (ert-deftest test-markdown-math/reload ()
3750 "Test enabling math mode via function `markdown-enable-math'."
3751 (markdown-test-file "math.text"
3752 (markdown-toggle-math t)
3753 ;; Flag should be set to t
3754 (should markdown-enable-math)
3755 ;; Font-lock keywords should be updated
3756 (should (member (cons markdown-regex-math-display '((1 markdown-markup-face prepend)
3757 (2 markdown-math-face append)
3758 (3 markdown-markup-face prepend)))
3759 markdown-mode-font-lock-keywords))))
3761 (ert-deftest test-markdown-math/font-lock ()
3762 "Test markdown math mode."
3763 (markdown-test-file "math.text"
3764 (markdown-toggle-math t)
3765 (funcall markdown-test-font-lock-function)
3766 (markdown-test-range-has-face 1 32 nil)
3767 (markdown-test-range-has-face 33 33 markdown-markup-face)
3768 (markdown-test-range-has-face 34 45 markdown-math-face)
3769 (markdown-test-range-has-face 46 46 markdown-markup-face)
3770 (markdown-test-range-has-face 47 49 nil)
3771 (markdown-test-range-has-face 50 51 markdown-markup-face)
3772 (markdown-test-range-has-face 52 63 markdown-math-face)
3773 (markdown-test-range-has-face 64 65 markdown-markup-face)
3774 (markdown-test-range-has-face 66 98 nil)
3775 (markdown-test-range-has-face 99 100 markdown-markup-face)
3776 (markdown-test-range-has-face 101 112 markdown-math-face)
3777 (markdown-test-range-has-face 113 114 markdown-markup-face)
3778 (markdown-test-range-has-face 113 114 markdown-markup-face)
3779 (markdown-test-range-has-face 117 117 markdown-header-delimiter-face)
3780 (markdown-test-range-has-face 119 152 markdown-header-face-1)
3781 (markdown-test-range-has-face 129 129 markdown-markup-face)
3782 (markdown-test-range-has-face 136 136 markdown-markup-face)
3783 (markdown-test-range-has-face 174 177 markdown-markup-face)
3784 (markdown-test-range-has-face 179 188 markdown-language-keyword-face)
3785 (markdown-test-range-has-face 190 211 markdown-pre-face)
3786 (markdown-test-range-has-face 212 215 markdown-markup-face)
3787 (markdown-test-range-has-face 218 218 markdown-markup-face)
3788 (markdown-test-range-has-face 219 223 markdown-math-face)
3789 (markdown-test-range-has-face 224 224 markdown-markup-face)
3790 (markdown-test-range-has-face 350 351 markdown-markup-face)
3791 (markdown-test-range-has-face 352 356 markdown-math-face)
3792 (markdown-test-range-has-face 357 358 markdown-markup-face)
3793 (markdown-test-range-has-face 359 391 nil)
3794 (markdown-test-range-has-face 392 393 markdown-markup-face)
3795 (markdown-test-range-has-face 394 398 markdown-math-face)
3796 (markdown-test-range-has-face 399 400 markdown-markup-face)))
3798 (ert-deftest test-markdown-math/font-lock-italics ()
3799 "Test markdown math mode with underscores."
3800 (markdown-test-file "math.text"
3801 (markdown-toggle-math t)
3802 (funcall markdown-test-font-lock-function)
3803 (markdown-test-range-has-face 227 227 markdown-markup-face)
3804 (markdown-test-range-has-face 228 233 markdown-math-face)
3805 (markdown-test-range-has-face 234 234 markdown-markup-face)
3806 (markdown-test-range-has-face 235 270 nil)
3807 (markdown-test-range-has-face 271 271 markdown-markup-face)
3808 (markdown-test-range-has-face 272 274 markdown-math-face)
3809 (markdown-test-range-has-face 275 275 markdown-markup-face)))
3811 ;;; gfm-mode tests:
3813 (ert-deftest test-markdown-gfm/pre-1 ()
3814 "GFM pre block font lock test."
3815 (markdown-test-file-gfm "gfm.text"
3816 (markdown-test-range-has-face 2626 2637 nil)
3817 (markdown-test-range-has-face 2639 2641 markdown-markup-face)
3818 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face)
3819 (markdown-test-range-has-face 2647 2728 markdown-pre-face)
3820 (markdown-test-range-has-face 2730 2732 markdown-markup-face)))
3822 (ert-deftest test-markdown-gfm/italic-1 ()
3823 "GFM italic font lock test."
3824 (markdown-test-file-gfm "gfm.text"
3825 (markdown-test-range-has-face 1483 1483 markdown-markup-face)
3826 (markdown-test-range-has-face 1484 1487 markdown-italic-face)
3827 (markdown-test-range-has-face 1488 1488 markdown-markup-face)
3828 (markdown-test-range-has-face 1729 1790 nil)))
3830 (ert-deftest test-markdown-gfm/strike-through-1 ()
3831 "GFM strike through font lock test."
3832 (markdown-test-string-gfm "one ~~two~~ three"
3833 (markdown-test-range-has-face 1 4 nil)
3834 (markdown-test-range-has-face 5 6 markdown-markup-face)
3835 (markdown-test-range-has-face 7 9 markdown-strike-through-face)
3836 (markdown-test-range-has-face 10 11 markdown-markup-face)
3837 (markdown-test-range-has-face 12 17 nil)))
3839 (ert-deftest test-markdown-gfm/toggle-strike-through ()
3840 "Test toggling functionality of `markdown-insert-strike-through'."
3841 (markdown-test-string-gfm "one ~~two~~ three"
3842 (forward-word 2)
3843 (markdown-insert-strike-through)
3844 (should (string-equal (buffer-string) "one two three"))
3845 (should (= (point) 8))
3846 (forward-word)
3847 (markdown-insert-strike-through)
3848 (should (= (point) 16))
3849 (should (string-equal (buffer-string) "one two ~~three~~"))))
3851 (ert-deftest test-markdown-gfm/insert-code-block ()
3852 "GFM code block insertion test."
3853 ;; Test empty markup insertion
3854 (markdown-test-string-gfm "line 1\nline 2\n"
3855 (end-of-line)
3856 (markdown-insert-gfm-code-block "elisp")
3857 (should (equal (car markdown-gfm-used-languages) "elisp"))
3858 (should (equal (car (markdown-gfm-get-corpus)) "elisp"))
3859 (should (string-equal (buffer-string)
3860 "line 1\n\n``` elisp\n\n```\n\nline 2\n")))
3861 ;; Test with active region
3862 (markdown-test-string-gfm "line 1\nline 2\nline 3\n"
3863 (forward-line)
3864 (transient-mark-mode)
3865 (push-mark (point) t t)
3866 (end-of-line)
3867 (should (markdown-use-region-p))
3868 (markdown-insert-gfm-code-block "elisp")
3869 (should (string-equal (buffer-string)
3870 "line 1\n\n``` elisp\nline 2\n```\n\nline 3\n"))))
3872 (ert-deftest test-markdown-gfm/gfm-parse-buffer-for-languages ()
3873 "Parse buffer for existing languages for `markdown-gfm-used-languages' test."
3874 (markdown-test-string-gfm "``` MADEUP\n\n```\n``` LANGUAGES\n\n```\n```MaDeUp\n\n```\n```\n\n```\n``` \n\n```\n"
3875 (markdown-gfm-parse-buffer-for-languages)
3876 (should (equal markdown-gfm-used-languages
3877 (list "MaDeUp" "LANGUAGES" "MADEUP")))
3878 (should (equal (car markdown-gfm-used-languages) "MaDeUp"))
3879 (should (equal (car (markdown-gfm-get-corpus)) "MaDeUp"))
3880 (goto-char (point-max))
3881 (markdown-insert-gfm-code-block "newlang")
3882 (should (equal markdown-gfm-used-languages
3883 (list "newlang" "MaDeUp" "LANGUAGES" "MADEUP")))
3884 (should (equal (car markdown-gfm-used-languages) "newlang"))
3885 (should (equal (car (markdown-gfm-get-corpus)) "newlang"))
3886 (let ((markdown-gfm-downcase-languages nil))
3887 (should
3888 (equal (markdown-gfm-get-corpus)
3889 (append markdown-gfm-used-languages
3890 markdown-gfm-additional-languages
3891 markdown-gfm-recognized-languages))))
3892 (let ((markdown-gfm-downcase-languages t))
3893 (should
3894 (equal
3895 (markdown-gfm-get-corpus)
3896 (append markdown-gfm-used-languages
3897 (cl-mapcar #'downcase
3898 (append markdown-gfm-additional-languages
3899 markdown-gfm-recognized-languages))))))))
3901 (ert-deftest test-markdown-gfm/code-block-font-lock ()
3902 "GFM code block font lock test."
3903 (markdown-test-file-gfm "gfm.text"
3904 (markdown-test-range-has-face 2639 2641 markdown-markup-face) ; ```
3905 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face) ; lang
3906 (markdown-test-range-has-face 2647 2728 markdown-pre-face) ; code
3907 (markdown-test-range-has-face 2730 2732 markdown-markup-face))) ; ```
3909 (ert-deftest test-markdown-gfm/code-block-font-lock-2 ()
3910 "GFM code block font lock test without language identifier."
3911 (markdown-test-string-gfm "Plain code block:\n\n```\nfoo\n```\n"
3912 (markdown-test-range-has-face 20 22 markdown-markup-face)
3913 (markdown-test-range-has-face 24 26 markdown-pre-face)
3914 (markdown-test-range-has-face 28 30 markdown-markup-face)))
3916 ;;; Tests for other extensions:
3918 (ert-deftest test-markdown-ext/pandoc-fancy-lists ()
3919 "Test basic support for font lock and filling of Pandoc 'fancy lists'."
3920 (markdown-test-string " #. abc\ndef\n"
3921 ;; font lock
3922 (markdown-test-range-has-face 1 1 nil)
3923 (markdown-test-range-has-face 2 3 markdown-list-face)
3924 (markdown-test-range-has-face 4 11 nil)
3925 ;; filling
3926 (forward-line)
3927 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
3928 (should (string-equal (buffer-string) " #. abc\n def\n"))
3929 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
3930 (should (string-equal (buffer-string) " #. abc\n def\n"))))
3932 (ert-deftest test-markdown-ext/ikiwiki ()
3933 (let ((markdown-enable-wiki-links t)
3934 (markdown-wiki-link-fontify-missing t)
3935 (markdown-wiki-link-search-parent-directories t))
3936 (progn
3937 (find-file "ikiwiki/root")
3938 (unwind-protect
3939 (progn
3940 (markdown-mode)
3941 ;; font lock
3942 (markdown-test-range-has-property 1 11 'font-lock-face markdown-link-face)
3943 (markdown-test-range-has-property 14 33 'font-lock-face markdown-missing-link-face))
3944 (kill-buffer)))
3945 (progn
3946 (find-file "ikiwiki/sub/foo")
3947 (unwind-protect
3948 (progn
3949 (markdown-mode)
3950 ;; font lock
3951 (markdown-test-range-has-property 1 16 'font-lock-face markdown-missing-link-face)
3952 (markdown-test-range-has-property 19 26 'font-lock-face markdown-link-face))
3953 (kill-buffer)))))
3955 (defadvice markdown-live-preview-window-eww
3956 (around markdown-test-create-fake-eww disable)
3957 (setq ad-return-value (get-buffer-create "*eww*")))
3959 (defmacro markdown-test-fake-eww (&rest body)
3960 `(progn
3961 ,@(if (and (fboundp 'libxml-parse-html-region) (require 'eww nil t)) body
3962 `((ad-enable-advice #'markdown-live-preview-window-eww
3963 'around 'markdown-test-create-fake-eww)
3964 (ad-activate #'markdown-live-preview-window-eww)
3965 ,@body
3966 (ad-disable-advice #'markdown-live-preview-window-eww
3967 'around 'markdown-test-create-fake-eww)
3968 (ad-activate #'markdown-live-preview-window-eww)))))
3970 (defmacro markdown-test-eww-or-nothing (test &rest body)
3971 (if (and (fboundp 'libxml-parse-html-region) (require 'eww nil t)
3972 (executable-find markdown-command))
3973 `(progn ,@body)
3974 (message "no eww, no libxml2, or no %s found: skipping %s" markdown-command test)
3975 nil))
3977 (ert-deftest test-markdown-ext/live-preview-exports ()
3978 (markdown-test-temp-file "inline.text"
3979 (unless (and (fboundp 'libxml-parse-html-region) (require 'eww nil t))
3980 (should-error (markdown-live-preview-mode)))
3981 (markdown-test-fake-eww
3982 (markdown-live-preview-mode)
3983 (should (buffer-live-p markdown-live-preview-buffer))
3984 (should (eq (current-buffer)
3985 (with-current-buffer markdown-live-preview-buffer
3986 markdown-live-preview-source-buffer)))
3987 (kill-buffer markdown-live-preview-buffer)
3988 (should (null markdown-live-preview-buffer))
3989 (set-buffer-modified-p t)
3990 (save-buffer) ; should create new export
3991 (should (buffer-live-p markdown-live-preview-buffer)))))
3993 (ert-deftest test-markdown-ext/live-preview-delete-exports ()
3994 (markdown-test-fake-eww
3995 (let ((markdown-live-preview-delete-export 'delete-on-destroy)
3996 file-output)
3997 (markdown-test-temp-file "inline.text"
3998 (markdown-live-preview-mode)
3999 (setq file-output (markdown-export-file-name)))
4000 (should-not (file-exists-p file-output)))
4001 (let ((markdown-live-preview-delete-export 'delete-on-export)
4002 file-output)
4003 (markdown-test-temp-file "inline.text"
4004 (markdown-live-preview-mode)
4005 (setq file-output (markdown-export-file-name))
4006 (should-not (file-exists-p file-output))))
4007 (let ((markdown-live-preview-delete-export nil)
4008 file-output)
4009 (unwind-protect
4010 (markdown-test-temp-file "inline.text"
4011 (markdown-live-preview-mode)
4012 (setq file-output (markdown-export-file-name))
4013 (should (file-exists-p file-output)))
4014 (delete-file file-output)))))
4016 (ert-deftest test-markdown-ext/live-preview-follow-min-max ()
4017 (markdown-test-eww-or-nothing "live-preview-follow-min-max"
4018 (markdown-test-temp-file "inline.text"
4019 (markdown-live-preview-mode)
4020 (should (buffer-live-p markdown-live-preview-buffer))
4021 (should (window-live-p (get-buffer-window markdown-live-preview-buffer)))
4022 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
4023 (goto-char (point-min)))
4024 (goto-char (point-min))
4025 (insert "a test ")
4026 (markdown-live-preview-export)
4027 (let (final-pt final-win-st-diff)
4028 ;; test that still starts at point-min
4029 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
4030 (should (= (window-point) 1))
4031 (should (= (markdown-visual-lines-between-points
4032 (window-start) (window-point))
4034 (set-window-point (selected-window) (point-max))
4035 (setq final-pt (window-point)
4036 final-win-st-diff (markdown-visual-lines-between-points
4037 (window-start) (window-point))))
4038 (goto-char (point-min))
4039 (insert "this is ")
4040 (markdown-live-preview-export)
4041 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
4042 (should (= (window-point) (+ final-pt (length "this is "))))
4043 (should (= (markdown-visual-lines-between-points
4044 (window-start) (window-point))
4045 final-win-st-diff))
4046 ;; test that still starts at point-max, with correct line difference
4047 (goto-char (floor (/ (float (- (point-max) (point-min))) 2)))
4048 (setq final-pt (window-point)
4049 final-win-st-diff (markdown-visual-lines-between-points
4050 (window-start) final-pt)))
4051 (markdown-live-preview-export)
4052 ;; test that still starts at same point, with correct line difference
4053 (with-selected-window (get-buffer-window markdown-live-preview-buffer)
4054 (should (= (window-point) final-pt))
4055 (should (= (markdown-visual-lines-between-points
4056 (window-start) (window-point))
4057 final-win-st-diff)))))))
4059 ;; Tests for imenu
4061 (ert-deftest test-markdown-imenu/metadata ()
4062 "Don't correct header like statement in metadata.
4063 https://github.com/jrblevin/markdown-mode/issues/145"
4064 (markdown-test-string "---
4065 title = \"Blah\"
4066 comments = false
4069 # Header1
4071 ## Header2
4073 (let ((headers (mapcar #'car (markdown-imenu-create-flat-index))))
4074 (should (member "Header1" headers))
4075 (should (member "Header2" headers))
4076 (should-not (member "comments = false" headers)))))
4078 (provide 'markdown-test)
4080 ;;; markdown-test.el ends here