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