Dynamic loading and unloading for math support
[markdown-mode.git] / tests / markdown-test.el
blobf032dbd6534c64cb7c08641d6a828d585d559501
1 ;;;; markdown-test.el --- Tests for markdown-mode
3 ;; Copyright (C) 2013 Jason R. Blevins <jrblevin@sdf.org>
4 ;; Copyright (C) 2013 Makoto Motohashi <mkt.motohashi@gmail.com>
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; if not, write to the Free Software
20 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
23 ;;; Commentary:
25 ;; This file contains the `markdown-mode' test suite. To run the tests:
27 ;; M-x load-file RET markdown-test.el RET
28 ;; M-x markdown-test RET
30 ;;; Code:
32 (unless (featurep 'markdown-mode)
33 (require 'markdown-mode))
35 (defconst markdown-test-dir
36 (expand-file-name (file-name-directory
37 (or load-file-name buffer-file-name))))
39 (defmacro markdown-test-string (string &rest body)
40 "Run body in a temporary buffer containing STRING."
41 `(with-temp-buffer
42 (markdown-mode)
43 (setq-default indent-tabs-mode nil)
44 (insert ,string)
45 (goto-char (point-min))
46 (font-lock-fontify-buffer)
47 (prog1 ,@body (kill-buffer))))
48 (def-edebug-spec markdown-test-string (form body))
50 (defmacro markdown-test-file (file &rest body)
51 "Open FILE from `markdown-test-dir' and execute body."
52 `(let ((fn (concat markdown-test-dir ,file)))
53 (save-window-excursion
54 (with-temp-buffer
55 (insert-file-contents fn)
56 (markdown-mode)
57 (goto-char (point-min))
58 (font-lock-fontify-buffer)
59 ,@body))))
60 (def-edebug-spec markdown-test-file (form body))
62 (defmacro markdown-test-file-gfm (file &rest body)
63 "Open FILE from `markdown-test-dir' and execute body."
64 `(let ((fn (concat markdown-test-dir ,file)))
65 (save-window-excursion
66 (with-temp-buffer
67 (insert-file-contents fn)
68 (gfm-mode)
69 (goto-char (point-min))
70 (font-lock-fontify-buffer)
71 ,@body))))
72 (def-edebug-spec markdown-test-file-gfm (form body))
74 (defmacro markdown-test-temp-file (file &rest body)
75 "Open FILE from `markdown-test-dir' visiting temp file and execute body.
76 This file is not saved."
77 `(let ((fn (concat markdown-test-dir ,file))
78 (tmp (make-temp-file "markdown-test" nil ".text"))
79 buf)
80 (save-window-excursion
81 (setq buf (find-file tmp))
82 (insert-file-contents fn)
83 (markdown-mode)
84 (goto-char (point-min))
85 (font-lock-fontify-buffer)
86 ,@body
87 (set-buffer-modified-p nil)
88 (kill-buffer buf)
89 (delete-file tmp))))
90 (def-edebug-spec markdown-test-temp-file (form body))
92 (defun markdown-test-range-has-property (begin end prop value)
93 "Verify that the range from BEGIN to END has property PROP equal to VALUE."
94 (let (loc props)
95 (dolist (loc (number-sequence begin end))
96 (setq props (get-char-property loc prop))
97 (cond ((and props (listp props))
98 (should (memq value props)))
100 (should (eq props value)))))))
102 (defun markdown-test-range-has-face (begin end face)
103 "Verify that the range from BEGIN to END has face equal to FACE."
104 (markdown-test-range-has-property begin end 'face face))
106 (defun markdown-test-goto-heading (title)
107 "Move the point to section with TITLE."
108 (let ((regexp (format "\\(^#+ %s\\( #+\\)?\\|^%s\n[=-]+\n\\)" title title)))
109 (if (re-search-forward regexp nil t)
110 (goto-char (match-end 0)))))
112 (defun markdown-test ()
113 "Run all defined tests for `markdown-mode'."
114 (interactive)
115 (ert "markdown"))
117 ;;; Example tests:
119 (ert-deftest test-markdown-example/string ()
120 "An example string test using the `ert' framework."
121 (markdown-test-string "foo *bar* baz"
122 (goto-char 5)
123 (delete-char 1)
124 (should (looking-at "bar"))))
126 (ert-deftest test-markdown-example/file ()
127 "An example file test using the `ert' framework."
128 (markdown-test-file "inline.text"
129 (goto-char 9)
130 (should (looking-at "\*"))))
132 ;;; Basic mode tests:
134 (ert-deftest test-markdown-mode/variables ()
135 "Test `markdown-mode' variables."
136 (markdown-test-file "inline.text"
137 (should (= tab-width 4))
138 (should (eq font-lock-multiline t))
139 (should (eq major-mode 'markdown-mode))))
141 ;;; Element insertion tests:
143 (ert-deftest test-markdown-insertion/blank-line-before-1 ()
144 "Test function `markdown-ensure-blank-line-before' at beginning of line."
145 (markdown-test-file "syntax.text"
146 (search-forward "as plain text")
147 (should (= (point) 1556))
148 (beginning-of-line)
149 (should (= (point) 1505))
150 (should (looking-back "A Markdown-formatted\n"))
151 (should (not (markdown-prev-line-blank-p)))
152 (markdown-ensure-blank-line-before)
153 (should (looking-back "A Markdown-formatted\n\n"))
154 (should (markdown-prev-line-blank-p))))
156 (ert-deftest test-markdown-insertion/blank-line-before-2 ()
157 "Test function `markdown-ensure-blank-line-before' in middle of line."
158 (markdown-test-file "syntax.text"
159 (search-forward "as plain text")
160 (should (= (point) 1556))
161 (should (looking-back "as plain text"))
162 (should (not (markdown-prev-line-blank-p)))
163 (markdown-ensure-blank-line-before)
164 (should (looking-back "as plain text\n\n"))
165 (should (markdown-prev-line-blank-p))))
167 (ert-deftest test-markdown-insertion/blank-line-before-3 ()
168 "Test function `markdown-ensure-blank-line-before' with blank line before."
169 (markdown-test-file "syntax.text"
170 (search-forward "web.\n\nMarkdown is not a replacement for HTML")
171 (beginning-of-line)
172 (should (= (point) 2704))
173 (should (looking-back "web.\n\n"))
174 (should (markdown-prev-line-blank-p))
175 (markdown-ensure-blank-line-before)
176 (should (= (point) 2704))
177 (should (looking-back "web.\n\n"))
178 (should (markdown-prev-line-blank-p))))
180 (ert-deftest test-markdown-insertion/blank-line-before-4 ()
181 "Test function `markdown-ensure-blank-line-before' at beginning of buffer."
182 (markdown-test-string "first line"
183 (beginning-of-line)
184 (should (bobp))
185 (should (= (point-max) 11))
186 (markdown-ensure-blank-line-before)
187 (should (= (point-max) 11))
188 (should (string-equal (buffer-substring (point-min) (point-max))
189 "first line"))
190 (forward-word)
191 (markdown-ensure-blank-line-before)
192 (should (string-equal (buffer-substring (point-min) (point-max))
193 "first\n\n line"))))
195 (ert-deftest test-markdown-insertion/blank-line-after-1 ()
196 "Test function `markdown-ensure-blank-line-after' at end of line."
197 (markdown-test-file "syntax.text"
198 (search-forward "as plain text")
199 (should (= (point) 1556))
200 (end-of-line)
201 (should (= (point) 1573))
202 (should (looking-at "\nlike it's been"))
203 (should (not (markdown-next-line-blank-p)))
204 (markdown-ensure-blank-line-after)
205 (should (looking-at "\n\nlike it's been"))
206 (should (markdown-next-line-blank-p))))
208 (ert-deftest test-markdown-insertion/blank-line-after-2 ()
209 "Test function `markdown-ensure-blank-line-after' in middle of line."
210 (markdown-test-file "syntax.text"
211 (search-forward "as plain text")
212 (should (= (point) 1556))
213 (should (looking-at ", without looking"))
214 (should (not (markdown-next-line-blank-p)))
215 (markdown-ensure-blank-line-after)
216 (should (looking-at "\n\n, without looking"))
217 (should (markdown-next-line-blank-p))))
219 (ert-deftest test-markdown-insertion/blank-line-after-3 ()
220 "Test function `markdown-ensure-blank-line-after' with blank line after."
221 (markdown-test-file "syntax.text"
222 (search-forward "*writing* for the web.")
223 (should (= (point) 2702))
224 (should (looking-at "\n\nMarkdown is not a replacement for HTML"))
225 (should (markdown-next-line-blank-p))
226 (markdown-ensure-blank-line-after)
227 (should (= (point) 2702))
228 (should (looking-at "\n\nMarkdown is not a replacement for HTML"))
229 (should (markdown-next-line-blank-p))))
231 (ert-deftest test-markdown-insertion/blank-line-after-4 ()
232 "Test function `markdown-ensure-blank-line-after' at end of buffer."
233 (markdown-test-string "last line"
234 (end-of-line)
235 (should (eobp))
236 (should (= (point-max) 10))
237 (markdown-ensure-blank-line-after)
238 (should (= (point-max) 10))
239 (should (string-equal (buffer-substring (point-min) (point-max))
240 "last line"))
241 (backward-word)
242 (markdown-ensure-blank-line-after)
243 (should (string-equal (buffer-substring (point-min) (point-max))
244 "last \n\nline"))))
246 (ert-deftest test-markdown-insertion/point-after-unwrap ()
247 "Test new point position calculations after unwrap operations."
248 (markdown-test-string "line **one**\n"
249 (let ((prefix (cons 6 8)) (suffix (cons 11 13)))
250 ;; Prefix
251 (should (eq (markdown-point-after-unwrap 6 prefix suffix) 6))
252 (should (eq (markdown-point-after-unwrap 7 prefix suffix) 6))
253 ;; Word
254 (should (eq (markdown-point-after-unwrap 8 prefix suffix) 6))
255 (should (eq (markdown-point-after-unwrap 9 prefix suffix) 7))
256 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 8))
257 ;; Suffix
258 (should (eq (markdown-point-after-unwrap 11 prefix suffix) 9))
259 (should (eq (markdown-point-after-unwrap 12 prefix suffix) 9))
260 ;; Immediately after
261 (should (eq (markdown-point-after-unwrap 13 prefix suffix) 9))))
262 (markdown-test-string "line _one_\n"
263 (let ((prefix (cons 6 7)) (suffix (cons 10 11)))
264 ;; Prefix
265 (should (eq (markdown-point-after-unwrap 6 prefix suffix) 6))
266 ;; Word
267 (should (eq (markdown-point-after-unwrap 7 prefix suffix) 6))
268 (should (eq (markdown-point-after-unwrap 8 prefix suffix) 7))
269 (should (eq (markdown-point-after-unwrap 9 prefix suffix) 8))
270 ;; Suffix
271 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 9))
272 ;; Immediately after
273 (should (eq (markdown-point-after-unwrap 10 prefix suffix) 9)))))
275 (ert-deftest test-markdown-insertion/unwrap-thing-at-point-italic ()
276 "Test function `markdown-unwrap-thing-at-point' on italics."
277 (markdown-test-file "syntax.text"
278 ;; Unwrap *not*
279 (goto-char 2859)
280 (should (thing-at-point-looking-at markdown-regex-italic))
281 (should (equal (markdown-unwrap-thing-at-point
282 markdown-regex-italic 2 4)
283 (cons 2859 2862)))
284 (should (= (point) 2859))
285 ;; Unwrap *publishing*
286 (goto-char 3064)
287 (should (thing-at-point-looking-at markdown-regex-italic))
288 (should (equal (markdown-unwrap-thing-at-point
289 markdown-regex-italic 2 4)
290 (cons 3060 3070)))
291 (should (= (point) 3063))
292 ;; Unwrap *writing*
293 (goto-char 3101)
294 (should (thing-at-point-looking-at markdown-regex-italic))
295 (should (equal (markdown-unwrap-thing-at-point
296 markdown-regex-italic 2 4)
297 (cons 3093 3100)))
298 (should (= (point) 3100))))
300 (ert-deftest test-markdown-insertion/unwrap-things-in-region-italic ()
301 "Test function `markdown-unwrap-things-in-region' on italics."
302 (markdown-test-file "syntax.text"
303 (should (equal (markdown-unwrap-things-in-region
304 2704 3207 markdown-regex-italic 2 4)
305 (cons 2704 3201)))))
307 (ert-deftest test-markdown-insertion/unwrap-things-in-region-bound ()
308 "Ensure that `markdown-unwrap-things-in-region' respects end bound"
309 (markdown-test-string "**a** **b** **c** **d** **e** **f**"
310 ;; Set region to unrwap a, b, c, and d only. If endpoint is not
311 ;; respected (i.e, not adjusted for character removal), the
312 ;; function will unwrap e and f also.
313 (should (equal (markdown-unwrap-things-in-region
314 1 24 markdown-regex-bold 2 4)
315 (cons 1 8)))
316 (should (string-equal (buffer-string) "a b c d **e** **f**"))))
318 (ert-deftest test-markdown-insertion/unwrap-things-in-region-links ()
319 "Test function `markdown-unwrap-things-in-region' on inline links."
320 (markdown-test-string "a [link](http://jblevins.org/) or [two](/).\n"
321 (should (equal (markdown-unwrap-things-in-region
322 (point-min) (point-max) markdown-regex-link-inline 0 3)
323 (cons 1 16)))
324 (should (string-equal (buffer-string) "a link or two.\n"))))
326 (ert-deftest test-markdown-insertion/toggle-bold ()
327 "Test toggling functionality of `markdown-insert-bold'."
328 (markdown-test-string "one **two** three"
329 (forward-word 2)
330 (markdown-insert-bold)
331 (should (string-equal (buffer-string) "one two three"))
332 (should (= (point) 8))
333 (forward-word)
334 (markdown-insert-bold)
335 (should (= (point) 16))
336 (should (string-equal (buffer-string) "one two **three**"))))
338 (ert-deftest test-markdown-insertion/toggle-italic ()
339 "Test toggling functionality of `markdown-insert-italic'."
340 (markdown-test-string "one *two* three"
341 (forward-word 2)
342 (markdown-insert-italic)
343 (should (string-equal (buffer-string) "one two three"))
344 (should (= (point) 8))
345 (forward-word)
346 (markdown-insert-italic)
347 (should (string-equal (buffer-string) "one two *three*"))
348 (should (= (point) 15))))
350 (ert-deftest test-markdown-insertion/toggle-code ()
351 "Test toggling functionality of `markdown-insert-code'."
352 (markdown-test-string "one `two` three"
353 (forward-word 2)
354 (markdown-insert-code)
355 (should (string-equal (buffer-string) "one two three"))
356 (should (= (point) 8))
357 (forward-word)
358 (markdown-insert-code)
359 (should (string-equal (buffer-string) "one two `three`"))
360 (should (= (point) 15))))
362 (ert-deftest test-markdown-insertion/bold-region ()
363 "Test region functionality of `markdown-insert-bold'."
364 (markdown-test-string "one two three"
365 (push-mark (point) t t)
366 (forward-word 2)
367 (markdown-insert-bold)
368 (should (string-equal (buffer-string) "**one two** three"))
369 (should (= (point) 10))))
371 (ert-deftest test-markdown-insertion/italic-region ()
372 "Test region functionality of `markdown-insert-italic'."
373 (markdown-test-string "one two three"
374 (push-mark (point) t t)
375 (forward-word 2)
376 (markdown-insert-italic)
377 (should (string-equal (buffer-string) "*one two* three"))
378 (should (= (point) 9))))
380 (ert-deftest test-markdown-insertion/code-region ()
381 "Test region functionality of `markdown-insert-code'."
382 (markdown-test-string "one two three"
383 (push-mark (point) t t)
384 (forward-word 2)
385 (markdown-insert-code)
386 (should (string-equal (buffer-string) "`one two` three"))
387 (should (= (point) 9))))
389 (ert-deftest test-markdown-insertion/atx-line ()
390 "Test ATX header insertion without region."
391 (markdown-test-string "line one\nline two\n"
392 (forward-word)
393 (markdown-insert-header-atx-1)
394 (should (string-equal (buffer-substring (point-min) (point-max))
395 "# line one #\n\nline two\n"))
396 (forward-line 2)
397 (markdown-insert-header-atx-2)
398 (should (string-equal (buffer-substring (point-min) (point-max))
399 "# line one #\n\n## line two ##\n\n"))))
401 (ert-deftest test-markdown-insertion/atx-region ()
402 "Test ATX header insertion with region."
403 (markdown-test-string "line one\nline two\n"
404 (transient-mark-mode)
405 (forward-char 5)
406 (push-mark (point) t t)
407 (forward-word)
408 (should (string-equal (buffer-substring (region-beginning) (region-end))
409 "one"))
410 (markdown-insert-header-atx-4)
411 (should (string-equal (buffer-substring (point-min) (point-max))
412 "line \n\n#### one ####\n\nline two\n"))))
414 (ert-deftest test-markdown-insertion/atx-blank ()
415 "Test ATX header insertion on blank line."
416 (markdown-test-string "line one\n\nline two\n"
417 (forward-line)
418 (markdown-insert-header-atx-3)
419 (should (string-equal (buffer-substring (point-min) (point-max))
420 "line one\n\n### ###\n\nline two\n"))
421 (should (= (point) 15))
422 (should (looking-at " ###\n"))))
424 (ert-deftest test-markdown-insertion/atx-region-whitespace ()
425 "Test ATX header insertion using a region with whitespace."
426 (markdown-test-string " line one\n\nline two\n \n"
427 (transient-mark-mode)
428 (push-mark (point) t t)
429 (goto-char (point-max))
430 (markdown-insert-header-atx-2)
431 (should (string-equal (buffer-substring (point-min) (point-max))
432 "## line one line two ##"))
433 (should (= (point) 21))
434 (should (looking-at " ##"))))
436 (ert-deftest test-markdown-insertion/atx-line-whitespace ()
437 "Test ATX header insertion using current line with whitespace."
438 (markdown-test-string " line one \n\nline two\n"
439 (goto-char (line-end-position))
440 (markdown-insert-header-atx-3)
441 (should (string-equal (buffer-substring (point-min) (point-max))
442 "### line one ###\n\nline two\n"))
443 (should (= (point) 13))
444 (should (looking-at " ###\n"))))
446 (ert-deftest test-markdown-insertion/atx-replace-atx ()
447 "Test ATX header insertion when replacing an existing ATX header."
448 (markdown-test-string "## replace ##\n"
449 (markdown-insert-header-atx-4)
450 (should (string-equal (buffer-string) "#### replace ####\n\n"))
451 (should (looking-at " ####\n"))))
453 (ert-deftest test-markdown-insertion/atx-replace-setext-1 ()
454 "Test ATX header insertion when replacing an existing setext header."
455 (markdown-test-string "replace\n=======\n"
456 (markdown-insert-header-atx-2)
457 (should (string-equal (buffer-string) "## replace ##\n\n"))
458 (should (looking-at " ##\n"))))
460 (ert-deftest test-markdown-insertion/atx-replace-setext-2 ()
461 "Test ATX header insertion when replacing an existing setext header."
462 (markdown-test-string "replace\n-------\n"
463 (markdown-insert-header-atx-5)
464 (should (string-equal (buffer-string) "##### replace #####\n\n"))
465 (should (looking-at " #####\n"))))
467 (ert-deftest test-markdown-insertion/setext-line ()
468 "Test setext header insertion without region."
469 (markdown-test-string "line one\nline two\n"
470 (forward-word)
471 (markdown-insert-header-setext-1)
472 (should (string-equal (buffer-substring (point-min) (point-max))
473 "line one\n========\n\nline two\n"))
474 (forward-line 3)
475 (markdown-insert-header-setext-2)
476 (should (string-equal (buffer-substring (point-min) (point-max))
477 "line one\n========\n\nline two\n--------\n\n"))))
479 (ert-deftest test-markdown-insertion/setext-region ()
480 "Test setext header insertion with region."
481 (markdown-test-string "line one\nline two\n"
482 (transient-mark-mode)
483 (forward-char 5)
484 (push-mark (point) t t)
485 (forward-word)
486 (should (string-equal (buffer-substring (region-beginning) (region-end))
487 "one"))
488 (markdown-insert-header-setext-1)
489 (should (string-equal (buffer-substring (point-min) (point-max))
490 "line \n\none\n===\n\nline two\n"))))
492 (ert-deftest test-markdown-insertion/setext-blank ()
493 "Test setext header insertion on blank line."
494 (markdown-test-string "line one\n\nline two\n"
495 (forward-line)
496 (markdown-insert-header 2 "foo" t)
497 (should (string-equal (buffer-substring (point-min) (point-max))
498 "line one\n\nfoo\n---\n\nline two\n"))
499 (should (= (point) 14))
500 (should (looking-at "\n---"))))
502 (ert-deftest test-markdown-insertion/setext-region-whitespace ()
503 "Test setext header insertion using a region with whitespace."
504 (markdown-test-string " line one\n\nline two\n \n"
505 (transient-mark-mode)
506 (push-mark (point) t t)
507 (goto-char (point-max))
508 (markdown-insert-header-setext-1)
509 (should (string-equal (buffer-substring (point-min) (point-max))
510 "line one line two\n================="))
511 (should (= (point) 18))
512 (should (looking-at "\n===="))))
514 (ert-deftest test-markdown-insertion/setext-line-whitespace ()
515 "Test setext header insertion using current line with whitespace."
516 (markdown-test-string " line one \n\nline two\n"
517 (goto-char (line-end-position))
518 (markdown-insert-header-setext-2)
519 (should (string-equal (buffer-substring (point-min) (point-max))
520 "line one\n--------\n\nline two\n"))
521 (should (= (point) 9))
522 (should (looking-at "\n---"))))
524 (ert-deftest test-markdown-insertion/setext-replace-atx ()
525 "Test setext header insertion when replacing an existing ATX header."
526 (markdown-test-string "## replace ##\n"
527 (markdown-insert-header-setext-1)
528 (should (string-equal (buffer-string) "replace\n=======\n\n"))
529 (should (looking-at "\n==="))))
531 (ert-deftest test-markdown-insertion/setext-replace-setext-1 ()
532 "Test setext header insertion when replacing an existing setext title."
533 (markdown-test-string "replace\n=======\n"
534 (markdown-insert-header-setext-2)
535 (should (string-equal (buffer-string) "replace\n-------\n\n"))
536 (should (looking-at "\n---"))))
538 (ert-deftest test-markdown-insertion/setext-replace-setext-2 ()
539 "Test setext header insertion when replacing an existing setext section."
540 (markdown-test-string "replace\n-------\n"
541 (markdown-insert-header-setext-1)
542 (should (string-equal (buffer-string) "replace\n=======\n\n"))
543 (should (looking-at "\n==="))))
545 (ert-deftest test-markdown-insertion/header-dwim ()
546 "Test 'do what I mean' header insertion."
547 (markdown-test-file "outline.text"
548 (call-interactively 'markdown-insert-header-dwim)
549 (should (looking-at " #$"))
550 (end-of-defun 2)
551 (call-interactively 'markdown-insert-header-dwim)
552 (beginning-of-line)
553 (should (looking-at "^# #$"))
554 (end-of-defun 3)
555 (call-interactively 'markdown-insert-header-dwim)
556 (beginning-of-line)
557 (should (looking-at "^### ###$"))))
559 (ert-deftest test-markdown-insertion/header-dwim-prefix ()
560 "Test 'do what I mean' header insertion with prefix arguments."
561 (let ((tests (list '(nil . "# abc #")
562 '(1 . "# abc #")
563 '(2 . "## abc ##")
564 '(3 . "### abc ###")
565 '(4 . "#### abc ####")
566 '(5 . "##### abc #####")
567 '(6 . "###### abc ######")
568 '((4) . "abc\n===")
569 '((16) . "abc\n---"))))
570 (dolist (test tests)
571 (markdown-test-string "abc"
572 (let ((current-prefix-arg (car test)))
573 (call-interactively 'markdown-insert-header-dwim)
574 (should (string-equal (buffer-string) (cdr test))))))))
576 (ert-deftest test-markdown-insertion/header-setext-dwim-prefix ()
577 "Test 'do what I mean' header insertion with prefix arguments."
578 (let ((tests (list '(nil . "abc\n===")
579 '(1 . "abc\n===")
580 '(2 . "abc\n---")
581 '(3 . "### abc ###")
582 '(4 . "#### abc ####")
583 '(5 . "##### abc #####")
584 '(6 . "###### abc ######")
585 '((4) . "abc\n===")
586 '((16) . "abc\n---"))))
587 (dolist (test tests)
588 (markdown-test-string "abc"
589 (let ((current-prefix-arg (car test)))
590 (call-interactively 'markdown-insert-header-setext-dwim)
591 (should (string-equal (buffer-string) (cdr test))))))))
593 (ert-deftest test-markdown-insertion/remove-header ()
594 "Test ATX and setext header."
595 (markdown-test-string
596 "# atx1\n\n## atx2 ##\n\nsetext1\n=======\n\nsetext2\n-------\n"
597 (should (equal (markdown-remove-header) (cons 1 5)))
598 (forward-line)
599 (should (not (markdown-remove-header)))
600 (forward-line)
601 (should (equal (markdown-remove-header) (cons 7 11)))
602 (forward-line)
603 (should (not (markdown-remove-header)))
604 (forward-line)
605 (should (equal (markdown-remove-header) (cons 13 20)))
606 (forward-line)
607 (should (not (markdown-remove-header)))
608 (forward-line)
609 (should (equal (markdown-remove-header) (cons 22 29)))
610 (should (string-equal (buffer-string)
611 "atx1\n\natx2\n\nsetext1\n\nsetext2\n"))))
613 (ert-deftest test-markdown-insertion/italic-unwrap-region ()
614 "A test of inserting italics with italic text in the region."
615 (markdown-test-string "*foo* bar *baz*"
616 (transient-mark-mode)
617 (push-mark (point) t t)
618 (end-of-line)
619 (markdown-insert-italic)
620 (should (string-equal (buffer-string) "*foo bar baz*"))))
622 (ert-deftest test-markdown-insertion/bold-unwrap-region ()
623 "A test of inserting bold with italic text in the region."
624 (markdown-test-string "*foo* **bar** *baz*"
625 (transient-mark-mode)
626 (push-mark (point) t t)
627 (end-of-line)
628 (markdown-insert-bold)
629 (should (string-equal (buffer-string) "***foo* bar *baz***"))))
631 (ert-deftest test-markdown-insertion/code-unwrap-region ()
632 "A test of inserting code with code already in the region."
633 (markdown-test-string "`foo` *bar* `baz`"
634 (transient-mark-mode)
635 (push-mark (point) t t)
636 (end-of-line)
637 (markdown-insert-code)
638 (should (string-equal (buffer-string) "`foo *bar* baz`"))))
640 (ert-deftest test-markdown-insertion/hr-order ()
641 "Test inserting horizontal rules."
642 (dotimes (n (length markdown-hr-strings))
643 (markdown-test-string ""
644 (let ((current-prefix-arg n))
645 (call-interactively 'markdown-insert-hr))
646 (should (string-equal (buffer-string) (nth (1- n) markdown-hr-strings))))))
648 (ert-deftest test-markdown-insertion/hr-prefix ()
649 "Test inserting horizontal rule with C-u prefix."
650 (markdown-test-string ""
651 (let ((current-prefix-arg '(4)))
652 (call-interactively 'markdown-insert-hr))
653 (should (string-equal (buffer-string) (car (last markdown-hr-strings))))))
655 (ert-deftest test-markdown-insertion/hr-bob ()
656 "Test inserting horizontal rule at beginning of buffer."
657 (markdown-test-string "one line\n"
658 (call-interactively 'markdown-insert-hr)
659 (should (string-equal (buffer-string)
660 (concat (car markdown-hr-strings)
661 "\n\none line\n")))))
663 (ert-deftest test-markdown-insertion/hr-eob ()
664 "Test inserting horizontal rule at end of buffer."
665 (markdown-test-string "one line\n"
666 (forward-line)
667 (call-interactively 'markdown-insert-hr)
668 (should (string-equal (buffer-string)
669 (concat "one line\n\n" (car markdown-hr-strings))))))
671 (ert-deftest test-markdown-insertion/hr-mob ()
672 "Test inserting horizontal rule in middle of buffer."
673 (markdown-test-string "one line\n"
674 (forward-word)
675 (let ((markdown-hr-strings '("----------")))
676 (call-interactively 'markdown-insert-hr)
677 (should (string-equal (buffer-string)
678 (concat "one\n\n" (car markdown-hr-strings)
679 "\n\n line\n"))))))
681 (ert-deftest test-markdown-insertion/pre-region-1 ()
682 "Test `markdown-pre-region'."
683 ;; Simple test as non-interactive command
684 (markdown-test-string "line one\nline two\n"
685 (markdown-pre-region (line-beginning-position) (line-end-position))
686 (should (string-equal (buffer-string) " line one\n\nline two\n")))
687 ;; Test removal of whitespace before and after region
688 (markdown-test-string "line one abc\nline two\n"
689 (markdown-pre-region 6 9)
690 (should (string-equal (buffer-string) "line\n\n one\n\nabc\nline two\n")))
691 ;; Simple test as interactive command
692 (markdown-test-string "line one\nline two\n"
693 (push-mark (point) t t)
694 (forward-line 2)
695 (call-interactively 'markdown-pre-region)
696 (should (string-equal (buffer-string) " line one\n line two\n\n"))))
698 (ert-deftest test-markdown-insertion/blockquote-region-1 ()
699 "Test `markdown-blockquote-region'."
700 ;; Simple test as non-interactive command
701 (markdown-test-string "line one\nline two\n"
702 (markdown-blockquote-region (line-beginning-position) (line-end-position))
703 (should (string-equal (buffer-string) "> line one\n\nline two\n")))
704 ;; Test removal of whitespace before and after region
705 (markdown-test-string "line one abc\nline two\n"
706 (markdown-blockquote-region 6 9)
707 (should (string-equal (buffer-string) "line\n\n> one\n\nabc\nline two\n")))
708 ;; Simple test as interactive command
709 (markdown-test-string "line one\nline two\n"
710 (push-mark (point) t t)
711 (forward-line 2)
712 (call-interactively 'markdown-blockquote-region)
713 (should (string-equal (buffer-string) "> line one\n> line two\n\n"))))
715 (ert-deftest test-markdown-insertion/pre-nested-lists ()
716 "Test `markdown-pre-indentation' and `markdown-insert-pre' with nested list."
717 (markdown-test-string "* item\n * item\n"
718 ;; before the first item
719 (should (string-equal (markdown-pre-indentation (point)) " "))
720 (markdown-insert-pre)
721 (beginning-of-line)
722 (should (markdown-prev-line-blank-p))
723 (should (looking-at "^ $"))
724 (should (markdown-next-line-blank-p))
725 ;; before the second item
726 (forward-line 3)
727 (should (string-equal (markdown-pre-indentation (point)) " "))
728 (markdown-insert-pre)
729 (beginning-of-line)
730 (should (markdown-prev-line-blank-p))
731 (should (looking-at "^ $"))
732 (should (markdown-next-line-blank-p))
733 ;; after the second item
734 (forward-line 3)
735 (should (string-equal (markdown-pre-indentation (point)) " "))
736 (markdown-insert-pre)
737 (beginning-of-line)
738 (should (markdown-prev-line-blank-p))
739 (should (looking-at "^ $"))
740 (should (markdown-next-line-blank-p))))
742 (ert-deftest test-markdown-insertion/pre-faux-list ()
743 "Test `markdown-pre-indentation' following a list-marker in a pre block."
744 (markdown-test-string " * pre block, not a list item\n"
745 (should (string-equal (markdown-pre-indentation (point-max)) " "))))
747 (ert-deftest test-markdown-insertion/blockquote-nested-lists ()
748 "Test blockquote insertion in a nested list context."
749 (markdown-test-string "* item\n * item\n"
750 ;; before the first item
751 (should (string-equal (markdown-blockquote-indentation (point)) ""))
752 (markdown-insert-blockquote)
753 (beginning-of-line)
754 (should (markdown-prev-line-blank-p))
755 (should (looking-at "^> $"))
756 (should (markdown-next-line-blank-p))
757 ;; before the second item
758 (forward-line 3)
759 (should (string-equal (markdown-blockquote-indentation (point)) " "))
760 (markdown-insert-blockquote)
761 (beginning-of-line)
762 (should (markdown-prev-line-blank-p))
763 (should (looking-at "^ > $"))
764 (should (markdown-next-line-blank-p))
765 ;; after the second item
766 (forward-line 3)
767 (should (string-equal (markdown-blockquote-indentation (point)) " "))
768 (markdown-insert-blockquote)
769 (beginning-of-line)
770 (should (markdown-prev-line-blank-p))
771 (should (looking-at "^ > $"))
772 (should (markdown-next-line-blank-p))))
774 (ert-deftest test-markdown-insertion/empty-italic ()
775 "Test `markdown-insert-italic' with no word at point and no region."
776 (markdown-test-string ""
777 (call-interactively 'markdown-insert-italic)
778 (should (string-equal (buffer-string) "**"))
779 (should (= (point) 2))))
781 (ert-deftest test-markdown-insertion/empty-bold ()
782 "Test `markdown-insert-bold' with no word at point and no region."
783 (markdown-test-string ""
784 (call-interactively 'markdown-insert-bold)
785 (should (string-equal (buffer-string) "****"))
786 (should (= (point) 3))))
788 (ert-deftest test-markdown-insertion/uri ()
789 "Test `markdown-insert-uri'."
790 (markdown-test-string "http://jblevins.org/projects/markdown-mode/"
791 (call-interactively 'markdown-insert-uri)
792 (should (string-equal (buffer-string) "<http://jblevins.org/projects/markdown-mode/>"))
793 (should (= (point) 2))
794 (call-interactively 'markdown-insert-uri)
795 (should (string-equal (buffer-string) "http://jblevins.org/projects/markdown-mode/"))
796 (should (= (point) 1))
797 (erase-buffer)
798 (call-interactively 'markdown-insert-uri)
799 (should (string-equal (buffer-string) "<>"))
800 (should (= (point) 2))))
802 (ert-deftest test-markdown-insertion/list-item ()
803 "Test `markdown-insert-list-item' on several lists."
804 ;; No existing list
805 (markdown-test-string "abc"
806 (goto-char (point-max))
807 (call-interactively 'markdown-insert-list-item)
808 (should (string-equal (buffer-string) "abc\n* "))
809 (should (= (point) 7)))
810 ;; Following a list item, on the same line
811 (markdown-test-string " * foo"
812 (goto-char (point-max))
813 (call-interactively 'markdown-insert-list-item)
814 (should (string-equal (buffer-string) " * foo\n * ")))
815 ;; Following a list item, on the next line
816 (markdown-test-string "- foo\n"
817 (goto-char (point-max))
818 (call-interactively 'markdown-insert-list-item)
819 (should (string-equal (buffer-string) "- foo\n- ")))
820 ;; Following a list item, after a blank line
821 (markdown-test-string "- foo\n\n"
822 (goto-char (point-max))
823 (call-interactively 'markdown-insert-list-item)
824 (should (string-equal (buffer-string) "- foo\n\n- ")))
825 ;; Preceding a list item
826 (markdown-test-string "- foo\n"
827 (goto-char (point-min))
828 (call-interactively 'markdown-insert-list-item)
829 (should (string-equal (buffer-string) "- \n- foo\n")))
830 ;; Preceding a list item and a blank line
831 (markdown-test-string "\n\n- foo\n"
832 (goto-char (point-min))
833 (call-interactively 'markdown-insert-list-item)
834 (should (string-equal (buffer-string) "- \n\n- foo\n")))
835 ;; In the middle of a list item
836 (markdown-test-string "- foo bar\n"
837 (forward-word)
838 (call-interactively 'markdown-insert-list-item)
839 (should (string-equal (buffer-string) "- foo\n- bar\n")))
840 ;; Before a list marker, but not at beginning of line
841 (markdown-test-string " - foo\n"
842 (forward-char 2)
843 (call-interactively 'markdown-insert-list-item)
844 (should (string-equal (buffer-string) " - \n - foo\n")))
845 ;; Following an ordered list item
846 (markdown-test-string "6. foo"
847 (goto-char (point-max))
848 (call-interactively 'markdown-insert-list-item)
849 (should (string-equal (buffer-string) "6. foo\n7. ")))
850 ;; Following a nested ordered list item
851 (markdown-test-string "6. foo\n 1. bar"
852 (goto-char (point-max))
853 (call-interactively 'markdown-insert-list-item)
854 (should (string-equal (buffer-string) "6. foo\n 1. bar\n 2. "))))
856 (ert-deftest test-markdown-insertion/reference-link ()
857 "Basic tests for `markdown-insert-reference-link'."
858 ;; Test optional parameters (leave point after link)
859 (markdown-test-string ""
860 (markdown-insert-reference-link "abc" "1")
861 (should (string-equal (buffer-string) "[abc][1]"))
862 (should (= (point) 9)))
863 ;; Full link without title (leave point after link)
864 (markdown-test-string ""
865 (markdown-insert-reference-link "link" "label" "http://jblevins.org/")
866 (should (string-equal (buffer-string) "[link][label]\n\n[label]: http://jblevins.org/\n"))
867 (should (= (point) 14)))
868 ;; Full link without label or title (leave point after link)
869 (markdown-test-string ""
870 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
871 (should (string-equal (buffer-string) "[link][]\n\n[link]: http://jblevins.org/\n"))
872 (should (= (point) 9)))
873 ;; Link only with no label, URL, or title (leave point after link)
874 (markdown-test-string ""
875 (markdown-insert-reference-link "link" "")
876 (should (string-equal (buffer-string) "[link][]"))
877 (should (= (point) 9))))
879 (ert-deftest test-markdown-insertion/reference-link-end ()
880 "Basic reference link insertion test for 'end location."
881 (let ((markdown-reference-location 'end))
882 (markdown-test-string "first para\n\nsecond para\n"
883 (end-of-line)
884 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
885 (should (= (point) 19))
886 (goto-line 5)
887 (should (looking-at "\\[link\\]: http://jblevins.org/")))))
889 (ert-deftest test-markdown-insertion/reference-link-immediately ()
890 "Basic reference link insertion test for 'immediately location."
891 (let ((markdown-reference-location 'immediately))
892 (markdown-test-string "first para\n\nsecond para\n"
893 (end-of-line)
894 (markdown-insert-reference-link "link" "" "http://jblevins.org/")
895 (should (= (point) 19))
896 (goto-line 3)
897 (should (looking-at "\\[link\\]: http://jblevins.org/")))))
899 (ert-deftest test-markdown-insertion/reference-link-header ()
900 "Basic reference link insertion test for 'header location."
901 (let ((markdown-reference-location 'header))
902 (markdown-test-string "par one\n\npar two\n\n### header\n"
903 (end-of-line)
904 (markdown-insert-reference-link "link" "" "")
905 (should (= (point) 35))
906 (should (looking-back "\\[link\\]: ")))))
908 (ert-deftest test-markdown-insertion/inline-link ()
909 "Basic tests for `markdown-insert-link'."
910 ;; Test empty markup insertion (leave point in square brackets)
911 (markdown-test-string "abc "
912 (end-of-line)
913 (call-interactively 'markdown-insert-link)
914 (should (string-equal (buffer-string) "abc []()"))
915 (should (= (point) 6)))
916 ;; Test with word at point (leave point in parentheses)
917 (markdown-test-string "abc def ghi"
918 (forward-word 2)
919 (call-interactively 'markdown-insert-link)
920 (should (string-equal (buffer-string) "abc [def]() ghi"))
921 (should (= (point) 11)))
922 ;; Test with region (leave point in parentheses)
923 (markdown-test-string "abc def ghi"
924 (push-mark (point) t t)
925 (forward-word 2)
926 (call-interactively 'markdown-insert-link)
927 (should (string-equal (buffer-string) "[abc def]() ghi"))
928 (should (= (point) 11))))
930 ;;; Footnote tests:
932 (ert-deftest test-markdown-footnote/basic-end ()
933 "Basic footnote insertion and deletion tests for 'end location."
934 (let ((markdown-footnote-location 'end))
935 (markdown-test-string "first line\nsecond line\n"
936 ;; new buffer with no footnotes
937 (should (= markdown-footnote-counter 0))
938 ;; footnote insertion
939 (end-of-line)
940 (markdown-footnote-new)
941 (should (= (point) 35))
942 (should (= markdown-footnote-counter 1))
943 (should (looking-back "\\[^1\\]: "))
944 ;; kill with point in footnote definition
945 (insert "footnote text")
946 (let (kill-ring)
947 (markdown-footnote-kill))
948 (should (= (point) 24))
949 (should (bolp))
950 (should (string-equal (buffer-string) "first line\nsecond line\n"))
951 ;; insertion, counter should increment
952 (goto-char (point-min))
953 (end-of-line)
954 (markdown-footnote-new)
955 (should (= (point) 35))
956 (should (= markdown-footnote-counter 2))
957 (should (looking-back "\\[^2\\]: "))
958 (insert "footnote text")
959 ;; return to marker
960 (markdown-footnote-return)
961 (should (= (point) 15))
962 (should (looking-back "\\[^2\\]"))
963 ;; kill with point at marker
964 (let (kill-ring)
965 (markdown-footnote-kill))
966 (should (= (point) 11))
967 (should (eolp))
968 (should (string-equal (buffer-string) "first line\nsecond line\n")))))
970 (ert-deftest test-markdown-footnote/basic-immediately ()
971 "Basic footnote insertion and deletion tests for 'immediately location."
972 (let ((markdown-footnote-location 'immediately))
973 (markdown-test-string "first paragraph\n\nsecond paragraph\n"
974 ;; new buffer with no footnotes
975 (should (= markdown-footnote-counter 0))
976 ;; footnote insertion
977 (end-of-line)
978 (markdown-footnote-new)
979 (should (= (point) 28))
980 (should (= markdown-footnote-counter 1))
981 (should (looking-back "\\[^1\\]: "))
982 ;; kill with point in footnote definition
983 (insert "footnote text")
984 (let (kill-ring)
985 (markdown-footnote-kill))
986 (should (= (point) 18))
987 (should (bolp))
988 (should (string-equal (buffer-string)
989 "first paragraph\n\nsecond paragraph\n")))))
991 (ert-deftest test-markdown-footnote/basic-header ()
992 "Basic footnote insertion and deletion tests for 'header location."
993 (let ((markdown-footnote-location 'header))
994 (markdown-test-string "par one\n\npar two\n\n### header\n"
995 ;; new buffer with no footnotes
996 (should (= markdown-footnote-counter 0))
997 ;; footnote insertion
998 (end-of-line)
999 (markdown-footnote-new)
1000 (should (= (point) 29))
1001 (should (= markdown-footnote-counter 1))
1002 (should (looking-back "\\[^1\\]: "))
1003 ;; kill with point in footnote definition
1004 (insert "footnote text")
1005 (let (kill-ring)
1006 (markdown-footnote-kill))
1007 (should (= (point) 19))
1008 (should (bolp))
1009 (should (string-equal (buffer-string)
1010 "par one\n\npar two\n\n### header\n"))
1011 ;; insertion, counter should increment
1012 (goto-char (point-min))
1013 (end-of-line)
1014 (markdown-footnote-new)
1015 (should (= (point) 29))
1016 (should (= markdown-footnote-counter 2))
1017 (should (looking-back "\\[^2\\]: "))
1018 (insert "footnote text")
1019 ;; return to marker
1020 (markdown-footnote-return)
1021 (should (= (point) 12))
1022 (should (looking-back "\\[^2\\]"))
1023 ;; kill with point at marker
1024 (let (kill-ring)
1025 (markdown-footnote-kill))
1026 (should (= (point) 8))
1027 (should (eolp))
1028 (should (string-equal (buffer-string)
1029 "par one\n\npar two\n\n### header\n")))))
1031 (ert-deftest test-markdown-footnote/kill-empty-text ()
1032 "Test killing a footnote with marker but no text."
1033 (markdown-test-string "no text[^1]\n\n[^1]: \n"
1034 (end-of-line)
1035 (markdown-footnote-goto-text)
1036 (should (looking-back "\\[^1\\]: "))
1037 (let (kill-ring)
1038 (markdown-footnote-kill))
1039 (should (string-equal (buffer-string) "no text\n"))))
1041 ;;; Element removal tests:
1043 (ert-deftest test-markdown-kill/simple ()
1044 "Simple tests for `markdown-kill-thing-at-point'."
1045 (let ((kill-ring nil)
1046 (tests (list '("`foo`" . "foo")
1047 '("## foo ##" . "foo")
1048 '("## foo" . "foo")
1049 '("foo\n---" . "foo")
1050 '("foo\n===" . "foo")
1051 '("* * * * *" . "* * * * *")
1052 '("[foo](http://bar.com/)" . "foo")
1053 '("![foo](http://bar.com/)" . "foo")
1054 '("[foo][bar]" . "foo")
1055 '("![foo][bar]" . "foo")
1056 '("<http://foo.com/>" . "http://foo.com/")
1057 '("<foo@bar.com>" . "foo@bar.com")
1058 '("[[foo]]" . "foo")
1059 '("[[foo|bar]]" . "foo")
1060 '("**foo**" . "foo")
1061 '("__foo__" . "foo")
1062 '("*foo*" . "foo")
1063 '("_foo_" . "foo")
1064 '(" [foo]: http://bar.com/" . "http://bar.com/")
1065 '(" [foo]: http://bar.com/ \"title\"" . "http://bar.com/")
1066 '("foo[^bar]\n\n[^bar]: baz" . "baz")
1067 '("[^bar]: baz" . "baz")
1068 '(" * foo\n bar" . " * foo\n bar"))))
1069 (dolist (test tests)
1070 ;; Load test string (the car), move to end of first line, kill
1071 ;; thing at point, and then verify that the kill ring contains cdr.
1072 (markdown-test-string (car test)
1073 (end-of-line)
1074 (call-interactively 'markdown-kill-thing-at-point)
1075 (should (string-equal (current-kill 0) (cdr test)))))))
1077 (ert-deftest test-markdown-kill/footnote-text ()
1078 "Test killing a footnote with point at footnote text."
1079 (markdown-test-string "some text[^1]\n\n[^1]: footnote\n"
1080 (end-of-line)
1081 (markdown-footnote-goto-text)
1082 (let (kill-ring)
1083 (markdown-footnote-kill))
1084 (should (string-equal (buffer-string) "some text\n"))))
1086 ;;; Promotion and demotion tests:
1088 (ert-deftest test-markdown-promote/atx-header ()
1089 "Test `markdown-promote' for atx headers."
1090 (markdown-test-string "###### test ######"
1091 (markdown-promote)
1092 (should (string-equal (buffer-string) "##### test #####"))
1093 (markdown-promote)
1094 (should (string-equal (buffer-string) "#### test ####"))
1095 (markdown-promote)
1096 (should (string-equal (buffer-string) "### test ###"))
1097 (markdown-promote)
1098 (should (string-equal (buffer-string) "## test ##"))
1099 (markdown-promote)
1100 (should (string-equal (buffer-string) "# test #"))
1101 (markdown-promote)
1102 (should (string-equal (buffer-string) "test"))
1103 (markdown-promote)
1104 (should (string-equal (buffer-string) "test"))))
1106 (ert-deftest test-markdown-demote/atx-header ()
1107 "Test `markdown-demote' for atx headers."
1108 (markdown-test-string "test"
1109 (markdown-demote)
1110 (should (string-equal (buffer-string) "# test #"))
1111 (markdown-demote)
1112 (should (string-equal (buffer-string) "## test ##"))
1113 (markdown-demote)
1114 (should (string-equal (buffer-string) "### test ###"))
1115 (markdown-demote)
1116 (should (string-equal (buffer-string) "#### test ####"))
1117 (markdown-demote)
1118 (should (string-equal (buffer-string) "##### test #####"))
1119 (markdown-demote)
1120 (should (string-equal (buffer-string) "###### test ######"))
1121 (markdown-demote)
1122 (should (string-equal (buffer-string) "###### test ######"))))
1124 (ert-deftest test-markdown-promote/setext-header ()
1125 "Test `markdown-promote' for setext headers."
1126 (markdown-test-string "test\n----"
1127 (markdown-promote)
1128 (should (string-equal (buffer-string) "test\n===="))
1129 (markdown-promote)
1130 (should (string-equal (buffer-string) "test"))
1131 (markdown-promote)
1132 (should (string-equal (buffer-string) "test"))))
1134 (ert-deftest test-markdown-demote/setext-header ()
1135 "Test `markdown-demote' for setext headers."
1136 (markdown-test-string "test\n===="
1137 (markdown-demote)
1138 (should (string-equal (buffer-string) "test\n----"))
1139 (markdown-demote)
1140 (should (string-equal (buffer-string) "### test ###"))
1141 (markdown-demote)
1142 (should (string-equal (buffer-string) "#### test ####"))
1143 (markdown-demote)
1144 (should (string-equal (buffer-string) "##### test #####"))
1145 (markdown-demote)
1146 (should (string-equal (buffer-string) "###### test ######"))
1147 (markdown-demote)
1148 (should (string-equal (buffer-string) "###### test ######"))))
1150 (ert-deftest test-markdown-promote/hr ()
1151 "Test `markdown-promote' for horizontal rules."
1152 (markdown-test-string (car (reverse markdown-hr-strings))
1153 (dolist (n (number-sequence 4 0 -1))
1154 (markdown-promote)
1155 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
1156 (markdown-promote)
1157 (should (string-equal (buffer-string) (nth 0 markdown-hr-strings)))))
1159 (ert-deftest test-markdown-demote/hr ()
1160 "Test `markdown-demote' for horizontal rules."
1161 (markdown-test-string (car markdown-hr-strings)
1162 (dolist (n (number-sequence 1 5))
1163 (markdown-demote)
1164 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))
1165 (markdown-demote)
1166 (should (string-equal (buffer-string) ""))))
1168 ;;; Completion and cycling:
1170 (ert-deftest test-markdown-complete-or-cycle/atx-header-incomplete ()
1171 "Test `markdown-incomplete-atx-p' for headers with no text."
1172 (markdown-test-string "### ###"
1173 (should (looking-at markdown-regex-header-atx))
1174 (should-not (markdown-incomplete-atx-p)))
1175 (markdown-test-string "###abc###"
1176 (should (looking-at markdown-regex-header-atx))
1177 (should (markdown-incomplete-atx-p)))
1178 (markdown-test-string "### ###"
1179 (should (looking-at markdown-regex-header-atx))
1180 (should (markdown-incomplete-atx-p))))
1182 (ert-deftest test-markdown-complete-or-cycle/atx-header ()
1183 "Test `markdown-complete-or-cycle' for atx headers."
1184 (markdown-test-string "##### test"
1185 (call-interactively 'markdown-complete-or-cycle)
1186 (should (string-equal (buffer-string) "##### test #####"))
1187 (call-interactively 'markdown-complete-or-cycle)
1188 (should (string-equal (buffer-string) "###### test ######"))
1189 (call-interactively 'markdown-complete-or-cycle)
1190 (should (string-equal (buffer-string) "# test #"))
1191 (call-interactively 'markdown-complete-or-cycle)
1192 (should (string-equal (buffer-string) "## test ##"))))
1194 (ert-deftest test-markdown-complete-or-cycle/setext-header ()
1195 "Test `markdown-complete-or-cycle' for setext headers."
1196 (markdown-test-string " test \n=="
1197 (call-interactively 'markdown-complete-or-cycle)
1198 (should (string-equal (buffer-string) "test\n===="))
1199 (call-interactively 'markdown-complete-or-cycle)
1200 (should (string-equal (buffer-string) "test\n----"))
1201 (call-interactively 'markdown-complete-or-cycle)
1202 (should (string-equal (buffer-string) "### test ###"))))
1204 (ert-deftest test-markdown-complete/hr ()
1205 "Test completion via `markdown-complete-or-cycle' for horizontal rules."
1206 (markdown-test-string "- - - - -"
1207 (call-interactively 'markdown-complete-or-cycle)
1208 (should (string-equal (buffer-string) (car markdown-hr-strings)))))
1210 (ert-deftest test-markdown-cycle/hr ()
1211 "Test cycling via `markdown-complete-or-cycle' for horizontal rules."
1212 (markdown-test-string (car markdown-hr-strings)
1213 (dolist (n (number-sequence 1 5))
1214 (call-interactively 'markdown-complete-or-cycle)
1215 (should (string-equal (buffer-string) (nth n markdown-hr-strings))))))
1217 (ert-deftest test-markdown-cycle/bold ()
1218 "Test cycling via `markdown-complete-or-cycle' for bold markup."
1219 (markdown-test-string "**bold**"
1220 (call-interactively 'markdown-complete-or-cycle)
1221 (should (string-equal (buffer-string) "__bold__"))
1222 (call-interactively 'markdown-complete-or-cycle)
1223 (should (string-equal (buffer-string) "**bold**"))))
1225 (ert-deftest test-markdown-cycle/italic ()
1226 "Test cycling via `markdown-complete-or-cycle' for italic markup."
1227 (markdown-test-string "*italic*"
1228 (call-interactively 'markdown-complete-or-cycle)
1229 (should (string-equal (buffer-string) "_italic_"))
1230 (call-interactively 'markdown-complete-or-cycle)
1231 (should (string-equal (buffer-string) "*italic*"))))
1233 ;;; Indentation tests:
1235 (ert-deftest test-markdown-indentation/calc-indents ()
1236 "Test `markdown-calc-indents' a nested list context."
1237 (markdown-test-file "nested-list.text"
1238 (goto-char (point-max))
1239 (let ((indents (markdown-calc-indents)))
1240 (should (= (car indents) 17)) ; indentation of previous line first
1241 (should (equal (sort indents '<)
1242 (list
1243 0 ; beginning of line
1244 3 ; first-level list marker
1245 7 ; second-level list marker
1246 11 ; third-level list marker
1247 13 ; previous list item text
1248 16 ; pre-block indentation
1249 17 ; indentation of previous line
1250 21 ; previous line plus tab-width
1251 ))))))
1253 (ert-deftest test-markdown-indentation/indent-region ()
1254 "Test `markdown-indent-region'."
1255 ;; Basic test with multiple lines
1256 (markdown-test-string "abc\ndef\nghi\n"
1257 (markdown-indent-region (point-min) (point-max) nil)
1258 (should (string-equal (buffer-string) " abc\n def\n ghi\n")))
1259 ;; Following a list item
1260 (markdown-test-string " * abc\ndef\n"
1261 (forward-line)
1262 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
1263 (should (string-equal (buffer-string) " * abc\n def\n"))
1264 (markdown-indent-region (line-beginning-position) (line-end-position) nil)
1265 (should (string-equal (buffer-string) " * abc\n def\n"))))
1267 ;;; Font lock tests:
1269 (ert-deftest test-markdown-font-lock/italics-1 ()
1270 "A simple italics test."
1271 (markdown-test-file "inline.text"
1272 (goto-char 9)
1273 (should (looking-at "\*"))
1274 ;; Check face of char before leading asterisk
1275 (markdown-test-range-has-face 8 8 nil)
1276 ;; Check face of italic range
1277 (markdown-test-range-has-face 9 17 markdown-italic-face)
1278 ;; Check face of point past leading asterisk
1279 (markdown-test-range-has-face 18 18 nil)))
1281 (ert-deftest test-markdown-font-lock/italics-2 ()
1282 "Test space after leading asterisk or underscore."
1283 (markdown-test-string
1284 "This is * not italic*, nor _ is this_."
1285 (markdown-test-range-has-face (point-min) (point-max) nil)))
1287 (ert-deftest test-markdown-font-lock/italics-3 ()
1288 "Test that slash inside asterisks is not italic."
1289 (markdown-test-string
1290 "not italic *\\*"
1291 (markdown-test-range-has-face (point-min) (point-max) nil)))
1293 (ert-deftest test-markdown-font-lock/italics-4 ()
1294 "Test that escaped asterisk inside italics is not bold."
1295 (markdown-test-string
1296 "italic **\\**"
1297 (markdown-test-range-has-face 1 7 nil)
1298 (markdown-test-range-has-face 8 12 markdown-italic-face)))
1300 (ert-deftest test-markdown-font-lock/italics-5 ()
1301 "Test italic single letter."
1302 (markdown-test-string
1303 "*a*"
1304 (markdown-test-range-has-face 1 3 markdown-italic-face)))
1306 (ert-deftest test-markdown-font-lock/italics-after-hr ()
1307 "Test italics after a horizontal rule with asterisks."
1308 (markdown-test-string "* * *\n\n*italic*\n"
1309 (markdown-test-range-has-face 1 5 markdown-header-face)
1310 (markdown-test-range-has-face 8 15 markdown-italic-face)))
1312 (ert-deftest test-markdown-font-lock/bold-1 ()
1313 "A simple bold test."
1314 (markdown-test-file "inline.text"
1315 (goto-char 27)
1316 (should (looking-at "\*\*"))
1317 ;; Check face of char before leading asterisk
1318 (markdown-test-range-has-face 26 26 nil)
1319 ;; Check face of bold range
1320 (markdown-test-range-has-face 27 35 markdown-bold-face)
1321 ;; Check face of point past leading asterisk
1322 (markdown-test-range-has-face 36 36 nil)))
1324 (ert-deftest test-markdown-font-lock/bold-2 ()
1325 "Test space after leading asterisks or underscores."
1326 (markdown-test-string
1327 "This is ** not bold**, nor __ is this__ (but they match italics)."
1328 (markdown-test-range-has-face 1 8 nil)
1329 (markdown-test-range-has-face 9 20 markdown-italic-face)
1330 (markdown-test-range-has-face 21 27 nil)
1331 (markdown-test-range-has-face 28 38 markdown-italic-face)
1332 (markdown-test-range-has-face 39 (point-max) nil)))
1334 (ert-deftest test-markdown-font-lock/bold-3 ()
1335 "Test escaped asterisk inside bold."
1336 (markdown-test-string
1337 "bold **\\***"
1338 (markdown-test-range-has-face 6 11 markdown-bold-face)))
1340 (ert-deftest test-markdown-font-lock/bold-4 ()
1341 "Test bold single letter."
1342 (markdown-test-string
1343 "**a**"
1344 (markdown-test-range-has-face 1 5 markdown-bold-face)))
1346 (ert-deftest test-markdown-font-lock/bold-after-hr ()
1347 "Test bold after a horizontal rule with asterisks."
1348 (markdown-test-string "* * *\n\n**bold**\n"
1349 (markdown-test-range-has-face 1 5 markdown-header-face)
1350 (markdown-test-range-has-face 8 15 markdown-bold-face)))
1352 (ert-deftest test-markdown-font-lock/code-1 ()
1353 "A simple inline code test."
1354 (markdown-test-file "inline.text"
1355 (goto-char 45)
1356 (should (looking-at "`"))
1357 ;; Regular code span
1358 (markdown-test-range-has-face 45 50 markdown-inline-code-face)
1359 ;; Code containing backticks
1360 (markdown-test-range-has-face 61 89 markdown-inline-code-face)
1361 ;; Seven backquotes in a row
1362 (markdown-test-range-has-face 119 125 nil)
1363 ;; Backquotes at beginning or end
1364 (markdown-test-range-has-face 228 239 markdown-inline-code-face)
1365 (markdown-test-range-has-face 341 351 markdown-inline-code-face)
1366 ;; Backslash as final character
1367 (markdown-test-range-has-face 460 468 markdown-inline-code-face)
1368 ;; Escaping of leading backquotes
1369 (markdown-test-range-has-face 586 592 nil)
1370 (markdown-test-range-has-face 597 603 nil)
1371 ;; A code span crossing lines
1372 (markdown-test-range-has-face 652 656 nil)
1373 (markdown-test-range-has-face 657 666 markdown-inline-code-face)
1374 ;; Three backquotes: same line, across lines, not across blocks
1375 (markdown-test-range-has-face 695 748 nil)
1376 (markdown-test-range-has-face 749 757 markdown-inline-code-face)
1377 (markdown-test-range-has-face 758 805 nil)
1378 (markdown-test-range-has-face 806 814 markdown-inline-code-face)
1379 (markdown-test-range-has-face 815 891 nil)
1382 (ert-deftest test-markdown-font-lock/code-2 ()
1383 "Multiple code spans in a row and on different lines."
1384 (markdown-test-string "`foo` `bar` `baz`"
1385 (markdown-test-range-has-face 1 5 markdown-inline-code-face)
1386 (markdown-test-range-has-face 6 6 nil)
1387 (markdown-test-range-has-face 7 11 markdown-inline-code-face)
1388 (markdown-test-range-has-face 12 12 nil)
1389 (markdown-test-range-has-face 13 17 markdown-inline-code-face))
1390 (markdown-test-string "`a`\n`b`\n`c`\n"
1391 (markdown-test-range-has-face 1 3 markdown-inline-code-face)
1392 (markdown-test-range-has-face 4 4 nil)
1393 (markdown-test-range-has-face 5 7 markdown-inline-code-face)
1394 (markdown-test-range-has-face 8 8 nil)
1395 (markdown-test-range-has-face 9 11 markdown-inline-code-face)
1396 (markdown-test-range-has-face 12 12 nil))
1397 (markdown-test-string "a`foo`b`bar`c`baz`d"
1398 (markdown-test-range-has-face 1 1 nil)
1399 (markdown-test-range-has-face 2 6 markdown-inline-code-face)
1400 (markdown-test-range-has-face 7 7 nil)
1401 (markdown-test-range-has-face 8 12 markdown-inline-code-face)
1402 (markdown-test-range-has-face 13 13 nil)
1403 (markdown-test-range-has-face 14 18 markdown-inline-code-face)
1404 (markdown-test-range-has-face 19 19 nil)))
1406 (ert-deftest test-markdown-font-lock/lists-1 ()
1407 "A simple list marker font lock test."
1408 (markdown-test-file "lists.text"
1409 (dolist (loc (list 1063 1283 1659 1830 1919 2150 2393 2484
1410 2762 2853 3097 3188 3700 3903 4009))
1411 (goto-char loc)
1412 (should (looking-at "[*+-]"))
1413 (markdown-test-range-has-face loc loc markdown-list-face))))
1415 (ert-deftest test-markdown-font-lock/pre-1 ()
1416 "Nested list and pre block font lock test."
1417 (markdown-test-file "nested-list.text"
1418 (dolist (loc (list 4 29 194 224 491 525))
1419 (markdown-test-range-has-face loc loc markdown-list-face))
1420 (markdown-test-range-has-face 6 25 nil)
1421 (markdown-test-range-has-face 31 83 nil)
1422 (markdown-test-range-has-face 85 155 markdown-pre-face)
1423 (markdown-test-range-has-face 157 189 nil)
1424 (markdown-test-range-has-face 196 215 nil)
1425 (markdown-test-range-has-face 226 403 nil)
1426 (markdown-test-range-has-face 405 482 markdown-pre-face)
1427 (markdown-test-range-has-face 493 512 nil)
1428 (markdown-test-range-has-face 527 546 nil)
1429 (markdown-test-range-has-face 548 581 markdown-pre-face)))
1431 (ert-deftest test-markdown-font-lock/pre-2 ()
1432 (markdown-test-string "* item\n\nreset baseline\n\n pre block\n"
1433 (markdown-test-range-has-face 2 24 nil)
1434 (markdown-test-range-has-face 29 37 markdown-pre-face)))
1436 (ert-deftest test-markdown-font-lock/pre-3 ()
1437 (markdown-test-string "It is interesting to see what happens when one queries
1438 `social upheaval` and `protopalatial era`.
1440 * `social upheaval`: the follwing queries have been tried:
1442 social upheaval subClassOf"
1443 (markdown-test-range-has-face 160 190 nil)))
1445 (ert-deftest test-markdown-font-lock/atx-no-spaces ()
1446 "Test font-lock for atx headers with no spaces."
1447 (markdown-test-string "##abc##"
1448 (markdown-test-range-has-face 1 2 markdown-header-delimiter-face)
1449 (markdown-test-range-has-face 3 5 markdown-header-face-2)
1450 (markdown-test-range-has-face 6 7 markdown-header-delimiter-face))
1451 (markdown-test-string "##"
1452 (markdown-test-range-has-face 1 1 markdown-header-delimiter-face)
1453 (markdown-test-range-has-face 2 2 markdown-header-face-1))
1454 (markdown-test-string "###"
1455 (markdown-test-range-has-face 1 2 markdown-header-delimiter-face)
1456 (markdown-test-range-has-face 3 3 markdown-header-face-2)))
1458 (ert-deftest test-markdown-font-lock/setext-1-letter ()
1459 "An edge case for level-one setext headers."
1460 (markdown-test-string "a\n=\n"
1461 (markdown-test-range-has-face 1 1 markdown-header-face-1)
1462 (markdown-test-range-has-face 3 3 markdown-header-rule-face)))
1464 (ert-deftest test-markdown-font-lock/setext-2-letter ()
1465 "An edge case for level-two setext headers."
1466 (markdown-test-string "b\n-\n"
1467 (markdown-test-range-has-face 1 1 markdown-header-face-2)
1468 (markdown-test-range-has-face 3 3 markdown-header-rule-face)))
1470 (ert-deftest test-markdown-font-lock/inline-links ()
1471 "Test font lock for inline links."
1472 (markdown-test-file "inline.text"
1473 (markdown-test-range-has-face 925 930 markdown-link-face)
1474 (markdown-test-range-has-face 931 950 markdown-url-face)
1475 (markdown-test-range-has-face 951 957 markdown-link-title-face)
1476 (markdown-test-range-has-face 958 958 markdown-url-face)))
1478 (ert-deftest test-markdown-font-lock/pre-comment ()
1479 "Test comments inside of a pre block."
1480 (markdown-test-string " <!-- pre, not comment -->"
1481 (markdown-test-range-has-face (point-min) (1- (point-max)) markdown-pre-face)))
1483 (ert-deftest test-markdown-font-lock/footnote-markers-links ()
1484 "Test an edge case involving footnote markers and inline reference links."
1485 (markdown-test-string "Harvard[^1] [tuition][]"
1486 (markdown-test-range-has-face 8 11 markdown-footnote-face)
1487 (markdown-test-range-has-face 13 21 markdown-link-face)
1488 (markdown-test-range-has-face 22 23 markdown-reference-face)))
1490 (ert-deftest test-markdown-font-lock/mmd-metadata ()
1491 "Basic MultMarkdown metadata tests."
1492 (markdown-test-string "Title: peg-multimarkdown User's Guide
1493 Author: Fletcher T. Penney
1494 Base Header Level: 2 "
1495 (markdown-test-range-has-face 1 5 markdown-metadata-key-face)
1496 (markdown-test-range-has-face 6 6 nil)
1497 (markdown-test-range-has-face 8 37 markdown-metadata-value-face)
1498 (markdown-test-range-has-face 41 46 markdown-metadata-key-face)
1499 (markdown-test-range-has-face 47 47 nil)
1500 (markdown-test-range-has-face 49 66 markdown-metadata-value-face)
1501 (markdown-test-range-has-face 70 86 markdown-metadata-key-face)
1502 (markdown-test-range-has-face 87 87 nil)
1503 (markdown-test-range-has-face 89 89 markdown-metadata-value-face)))
1505 (ert-deftest test-markdown-font-lock/mmd-metadata-after-header ()
1506 "Ensure that similar lines are not matched after the header."
1507 (markdown-test-string "Title: peg-multimarkdown User's Guide
1509 Author: Fletcher T. Penney
1510 Base Header Level: 2 "
1511 (markdown-test-range-has-face 1 5 markdown-metadata-key-face)
1512 (markdown-test-range-has-face 6 6 nil)
1513 (markdown-test-range-has-face 8 37 markdown-metadata-value-face)
1514 (markdown-test-range-has-face 40 67 nil)
1515 (markdown-test-range-has-face 71 90 nil)))
1517 (ert-deftest test-markdown-font-lock/pandoc-metadata ()
1518 "Basic Pandoc metadata tests."
1519 (markdown-test-string "% title
1520 two-line title
1521 % first author;
1522 second author
1523 % date
1525 body"
1526 (markdown-test-range-has-face 1 1 markdown-comment-face)
1527 (markdown-test-range-has-face 3 24 markdown-metadata-value-face)
1528 (markdown-test-range-has-face 26 26 markdown-comment-face)
1529 (markdown-test-range-has-face 28 56 markdown-metadata-value-face)
1530 (markdown-test-range-has-face 58 58 markdown-comment-face)
1531 (markdown-test-range-has-face 60 63 markdown-metadata-value-face)
1532 (markdown-test-range-has-face 64 69 nil)))
1534 (ert-deftest test-markdown-font-lock/line-break ()
1535 "Basic line break tests."
1536 (markdown-test-string " \nasdf \n"
1537 (markdown-test-range-has-face 1 9 nil)
1538 (markdown-test-range-has-face 10 11 markdown-line-break-face)))
1540 ;;; Markdown Parsing Functions:
1542 (ert-deftest test-markdown-parsing/reference-definition-basic ()
1543 "Test reference definition function."
1544 (markdown-test-file "syntax.text"
1545 ;; Test accuracy of returned text and bounds
1546 (should (equal (markdown-reference-definition "[1]")
1547 (list "http://docutils.sourceforge.net/mirror/setext.html" 1942 1992)))
1548 (should (equal (markdown-reference-definition "[2]")
1549 (list "http://www.aaronsw.com/2002/atx/" 2000 2032)))
1550 ;; Test that match data remains intact
1551 (should (string-equal (match-string 2) "http://www.aaronsw.com/2002/atx/"))
1552 ;; Test anchor-only relative URL
1553 (should (equal (markdown-reference-definition "[bq]")
1554 (list "#blockquote" 7536 7547)))
1555 ;; Example references that appear in pre blocks in the text
1556 (should (not (markdown-reference-definition "[]")))
1557 (should (not (markdown-reference-definition "[id]")))
1558 (should (not (markdown-reference-definition "[foo]")))
1559 (should (not (markdown-reference-definition "[A]")))
1560 (should (not (markdown-reference-definition "[Google]")))
1561 ;; Test that we don't pick up other text in square brackets
1562 (should (not (markdown-reference-definition "[blockquoting]")))
1563 (should (not (markdown-reference-definition "[square brackets]")))
1564 ;; Test case insensitivity
1565 (should (equal (markdown-reference-definition "[SRC]")
1566 (list "/projects/markdown/syntax.text" 1245 1275)))))
1568 (ert-deftest test-markdown-parsing/get-defined-references ()
1569 "Test `markdown-get-defined-references'."
1570 (markdown-test-file "syntax.text"
1571 (should (equal (markdown-get-defined-references)
1572 '("[src]" "[1]" "[2]" "[3]" "[4]" "[5]" "[6]" "[bq]" "[l]"))))
1573 (markdown-test-file "outline.text"
1574 (should (equal (markdown-get-defined-references) nil)))
1575 (markdown-test-file "wiki-links.text"
1576 (should (equal (markdown-get-defined-references) nil))))
1578 ;;; Reference Checking:
1580 (ert-deftest test-markdown-references/goto-line-button ()
1581 "Create and test a goto line button."
1582 (markdown-test-string "line 1\nline 2\n"
1583 ;; Store the temporary buffer with the text
1584 (let ((target (current-buffer)))
1585 ;; Create a new buffer for inserting
1586 (with-temp-buffer
1587 ;; Verify that point is in a different buffer
1588 (should (not (equal (current-buffer) target)))
1589 ;; Insert and press the button
1590 (insert-button "goto line 2"
1591 :type 'markdown-goto-line-button
1592 'target-buffer target
1593 'target-line 2)
1594 (should (string-equal (buffer-string) "goto line 2"))
1595 (backward-button 1)
1596 (call-interactively 'push-button)
1597 ;; Verify that point is on line 2 of target buffer
1598 (should (= (line-number-at-pos) 2))
1599 (should (looking-at "line 2"))
1600 (should (equal (current-buffer) target))))))
1602 (ert-deftest test-markdown-references/button-map ()
1603 "Verify that button-buffer-map is used for check references buffer."
1604 (markdown-test-string "[undefined][ref]\n"
1605 (let* ((target (buffer-name))
1606 (check (format "*Undefined references for %s*" target)))
1607 (markdown-check-refs)
1608 (with-current-buffer (get-buffer check)
1609 (should (equal (local-key-binding (kbd "TAB")) 'forward-button))
1610 (should (equal (local-key-binding (kbd "<backtab>")) 'backward-button))))))
1612 ;;; Lists:
1614 (ert-deftest test-markdown-lists/levels-1 ()
1615 "Test list levels function `markdown-calculate-list-levels'."
1616 (markdown-test-file "nested-list.text"
1617 (let ((values '(((1 . 1) . nil) ((2 . 13) . (3)) ((14 . 23) . (7 3))
1618 ((24 . 26) . (11 7 3)))))
1619 (loop for (range . value) in values
1620 do (goto-char (point-min))
1621 (forward-line (1- (car range)))
1622 (dotimes (n (- (cdr range) (car range)))
1623 (should (equal (markdown-calculate-list-levels) value))
1624 (forward-line))))))
1626 (ert-deftest test-markdown-lists/levels-2 ()
1627 "Test list levels function `markdown-calculate-list-levels'."
1628 (markdown-test-file "syntax.text"
1629 (let ((values '(((1 . 13) . nil) ((14 . 14) . (0)) ((15 . 17) . (4 0))
1630 ((18 . 18) . (0)) ((19 . 24) . (4 0)) ((25 . 25) . (0))
1631 ((26 . 29) . (4 0)) ((30 . 30) . (0)) ((31 . 33) . (4 0))
1632 ((34 . 588) . nil) ((589 . 595) . (0)) ((596 . 814) . nil)
1633 ((815 . 820) . (0)) ((821 . 898) . nil))))
1634 (loop for (range . value) in values
1635 do (goto-char (point-min))
1636 (forward-line (1- (car range)))
1637 (dotimes (n (- (cdr range) (car range)))
1638 (should (equal (markdown-calculate-list-levels) value))
1639 (forward-line))))))
1641 (ert-deftest test-markdown-lists/levels-interior ()
1642 "Test `markdown-calculate-list-levels' from inside a list item."
1643 (markdown-test-file "nested-list.text"
1644 (goto-char 36)
1645 (should (equal (markdown-calculate-list-levels) (list 3)))
1646 (goto-char 267)
1647 (should (equal (markdown-calculate-list-levels) (list 7 3)))
1648 (goto-char 540)
1649 (should (equal (markdown-calculate-list-levels) (list 11 7 3)))))
1651 (ert-deftest test-markdown-lists/bounds-1 ()
1652 "Test list item bounds function `markdown-cur-list-item-bounds'."
1653 (markdown-test-file "lists.text"
1654 (markdown-test-goto-heading "Case 9")
1655 (forward-line)
1656 (should (eq (point) 3699))
1657 (markdown-next-list-item 4)
1658 (should (eq (point) 3700))
1659 (should (equal (markdown-cur-list-item-bounds)
1660 (list 3700 3901 0 4 "- ")))
1661 (markdown-next-list-item 4)
1662 (should (eq (point) 3903))
1663 (should (equal (markdown-cur-list-item-bounds)
1664 (list 3903 3937 0 4 "* ")))))
1666 (ert-deftest test-markdown-lists/bounds-2 ()
1667 "Function `markdown-cur-list-item-bounds' should return nil outside of list items."
1668 (markdown-test-string "line one\n\n* item\n"
1669 (should (null (markdown-cur-list-item-bounds)))
1670 (forward-line)
1671 (should (null (markdown-cur-list-item-bounds)))
1672 (forward-line)
1673 (should (markdown-cur-list-item-bounds))))
1675 (ert-deftest test-markdown-lists/promotion-and-demotion ()
1676 "Test function `markdown-promote-list-item'."
1677 (markdown-test-file "nested-list.text"
1678 (forward-line)
1679 (should (looking-at " - List level 1 item 2
1681 Second paragraph of item 2
1683 Nested pre block in item 2
1684 Four spaces past the marker
1686 Another paragraph of item 2"))
1687 (markdown-demote-list-item)
1688 (should (looking-at " - List level 1 item 2
1690 Second paragraph of item 2
1692 Nested pre block in item 2
1693 Four spaces past the marker
1695 Another paragraph of item 2"))
1696 (markdown-promote-list-item)
1697 (should (looking-at " - List level 1 item 2
1699 Second paragraph of item 2
1701 Nested pre block in item 2
1702 Four spaces past the marker
1704 Another paragraph of item 2"))
1705 (goto-line 23)
1706 (should (looking-at " - List level 3 item 1
1708 Nested pre block"))
1709 (markdown-demote-list-item)
1710 (should (looking-at " - List level 3 item 1
1712 Nested pre block"))
1713 (markdown-promote-list-item)
1714 (should (looking-at " - List level 3 item 1
1716 Nested pre block"))))
1718 ;;; Outline minor mode tests:
1720 (ert-deftest test-markdown-outline/navigation ()
1721 "Test outline navigation functions."
1722 (markdown-test-file "outline.text"
1723 ;; Navigate to the first visible heading
1724 (outline-next-visible-heading 1)
1725 (should (eq (point) 19))
1726 (should (looking-at "^# A top-level header"))
1727 ;; Navigate forward at the same level
1728 (outline-forward-same-level 1)
1729 (should (eq (point) 377))
1730 (should (looking-at "^=+$"))
1731 ;; Navigate backward by four visible headings
1732 (outline-previous-visible-heading 4)
1733 (should (eq (point) 69))
1734 (should (looking-at "^## A second-level header$"))))
1736 (ert-deftest test-markdown-outline/visibility-atx ()
1737 "Test outline visibility cycling for ATX-style headers."
1738 (markdown-test-file "outline.text"
1739 (let (last-command this-command)
1740 ;; Navigate to the second visible heading
1741 (outline-next-visible-heading 2)
1742 (should (eq (point) 69))
1743 (should (looking-at "^## A second-level header$"))
1744 ;; Cycle visibility of this subtree
1745 (setq this-command 'markdown-cycle)
1746 (markdown-cycle)
1747 (setq last-command 'markdown-cycle)
1748 (should (eq (point) 69))
1749 (should (looking-at "^## A second-level header$"))
1750 ;; Test that the entire subtree is invisible
1751 (markdown-test-range-has-property 93 349 'invisible 'outline)
1752 ;; Cycle visibility of this subtree again
1753 (markdown-cycle)
1754 (should (eq (point) 69))
1755 (should (looking-at "^## A second-level header$"))
1756 ;; Test that text is visible
1757 (markdown-test-range-has-property 95 121 'invisible nil)
1758 ;; Test that subheadings are visible
1759 (markdown-test-range-has-property 123 141 'invisible nil)
1760 ;; Cycle visibility of this subtree again
1761 (markdown-cycle)
1762 (should (eq (point) 69))
1763 (should (looking-at "^## A second-level header$"))
1764 ;; Verify that entire subtree is visible
1765 (markdown-test-range-has-property 93 349 'invisible nil))))
1767 (ert-deftest test-markdown-outline/visibility-setext ()
1768 "Test outline visibility cycling for setext-style headers."
1769 (markdown-test-file "outline.text"
1770 ;; Navigate to the sixth visible heading
1771 (outline-next-visible-heading 7)
1772 (outline-previous-visible-heading 1)
1773 (should (looking-at markdown-regex-header))
1774 (should (string-equal (match-string-no-properties 1) "An underline-style header"))
1775 (should (string-equal (match-string-no-properties 2) "========================="))
1776 ;; Cycle visibility subtree, test that it's invisible
1777 (markdown-cycle)
1778 (markdown-test-range-has-property 404 515 'invisible 'outline)
1779 ;; Cycle visibility subtree, test that text and headers are visible
1780 (markdown-cycle)
1781 (markdown-test-range-has-property 404 417 'invisible nil)
1782 (markdown-test-range-has-property 420 451 'invisible nil)))
1784 ;;; Movement tests:
1786 (ert-deftest test-markdown-movement/defun ()
1787 "Test defun navigation."
1788 (markdown-test-file "outline.text"
1789 ;; end-of-defun should go to point-max
1790 (end-of-defun 10)
1791 (should (= (point) (point-max)))
1792 ;; end-of-defun should stop just before the next header
1793 (goto-char (point-min))
1794 (end-of-defun)
1795 (should (looking-at "\n# A top-level header"))
1796 (end-of-defun)
1797 (should (looking-at "\n## A second-level header"))
1798 (end-of-defun)
1799 (should (looking-at "\n### Third level ###"))
1800 (end-of-defun)
1801 (should (looking-at "\n### Third level number two ###"))
1802 ;; beginning-of-defun should move to the start of the previous header
1803 (beginning-of-defun)
1804 (should (looking-at "### Third level ###"))
1805 (beginning-of-defun)
1806 (should (looking-at "## A second-level header"))
1807 (beginning-of-defun)
1808 (should (looking-at "# A top-level header"))
1809 (beginning-of-defun)
1810 ;; beginning-of-defun should move up to point-min
1811 (should (= (point) (point-min)))))
1813 (ert-deftest test-markdown-movement/block ()
1814 "Test block movement."
1815 (markdown-test-file "outline.text"
1816 (markdown-end-of-block)
1817 (should (looking-at "\n# A top-level header"))
1818 (markdown-end-of-block)
1819 (should (looking-at "\nfollowed by some body text"))
1820 (markdown-end-of-block)
1821 (should (looking-at "\n## A second-level header"))
1822 (markdown-end-of-block)
1823 (should (looking-at "\nfollowed by some body text"))
1824 (markdown-end-of-block)
1825 (should (looking-at "\n### Third level ###"))
1826 (markdown-end-of-block)
1827 (should (looking-at "\n\\* A list item"))
1828 (markdown-end-of-block)
1829 (should (looking-at "\n### Third level number two ###"))
1830 (markdown-end-of-block)
1831 (should (looking-at "\n### Level two again"))
1832 (markdown-end-of-block)
1833 (should (looking-at "\nfollowed by some body text"))
1835 (markdown-test-goto-heading "Level two")
1836 (markdown-end-of-block)
1837 (should (looking-at "\nbar"))
1838 (markdown-end-of-block)
1839 (should (= (point) (point-max)))
1840 (markdown-beginning-of-block)
1841 (should (looking-at "bar"))
1842 (markdown-beginning-of-block)
1843 (should (looking-at "## Level two"))
1844 (markdown-beginning-of-block)
1845 (should (looking-at "foo"))
1846 (markdown-beginning-of-block)
1847 (should (looking-at "# Level one"))
1848 (markdown-beginning-of-block)
1849 (should (looking-at "* With"))
1850 (markdown-beginning-of-block)
1851 (should (looking-at "And a level two underline header"))
1853 (goto-char (point-min))
1854 (markdown-test-goto-heading "A top-level header")
1855 (beginning-of-line)
1856 (markdown-beginning-of-block)
1857 (should (= (point) (point-min)))))
1859 (ert-deftest test-markdown-movement/reference-definition ()
1860 "Test jumping to reference definitions."
1861 ;; Jumping to explicit reference definition
1862 (markdown-test-string "[a][ref]\n\n[ref]: gopher://localhost/\n"
1863 (markdown-reference-goto-definition)
1864 (should (= (point) 18)))
1865 ;; Jumping to implicit reference definition
1866 (markdown-test-string "[a][]\n\n[a]: ftp://localhost/\n"
1867 (markdown-reference-goto-definition)
1868 (should (= (point) 13)))
1869 ;; Creating non-existent reference definition
1870 (markdown-test-string "[a][]\n"
1871 (markdown-reference-goto-definition)
1872 (should (= (point) 13))
1873 (should (string-equal (buffer-string) "[a][]\n\n[a]: "))))
1875 ;;; Wiki link tests:
1877 (ert-deftest test-markdown-wiki-link/aliasing ()
1878 "Test filename extraction for aliased wiki links."
1879 (markdown-test-file "wiki-links.text"
1880 ;; Confirm location of first wiki link
1881 (should (eq (markdown-next-link) 8))
1882 ;; Confirm location of second wiki link
1883 (should (eq (markdown-next-link) 73))
1884 ;; Test predicate function
1885 (should (markdown-wiki-link-p))
1886 ;; Test alias-first filename extraction
1887 (setq markdown-wiki-link-alias-first t)
1888 (should (string-equal (markdown-wiki-link-link) "second"))
1889 ;; Test alias-second filename extraction
1890 (setq markdown-wiki-link-alias-first nil)
1891 (should (string-equal (markdown-wiki-link-link) "first"))))
1893 (ert-deftest test-markdown-wiki-link/navigation ()
1894 "Test wiki link navigation."
1895 (markdown-test-file "wiki-links.text"
1896 ;; Advance to first link
1897 (should (eq (markdown-next-link) 8))
1898 ;; Advance to second link
1899 (should (eq (markdown-next-link) 73))
1900 ;; Avance to final link
1901 (should (eq (markdown-next-link) 155))
1902 ;; Return nil and don't advance point
1903 (should (eq (markdown-next-link) nil))
1904 (should (eq (point) 155))
1905 ;; Move back to second link
1906 (should (eq (markdown-previous-link) 73))
1907 ;; Move back to first link
1908 (should (eq (markdown-previous-link) 8))
1909 ;; Return nil and don't move point
1910 (should (eq (markdown-previous-link) nil))
1911 (should (eq (point) 8))))
1913 (ert-deftest test-markdown-wiki-link/font-lock ()
1914 "Test font lock faces for wiki links."
1915 (markdown-test-temp-file "wiki-links.text"
1916 (let* ((fn (concat (file-name-directory buffer-file-name)
1917 "inline.text")))
1918 ;; Create inline.text in the same temp directory, refontify
1919 (write-region "" nil fn nil 1)
1920 (markdown-fontify-buffer-wiki-links)
1921 ;; Confirm location of first wiki link
1922 (should (eq (markdown-next-link) 8))
1923 ;; First wiki link doesn't have a corresponding file
1924 (markdown-test-range-has-property 8 20 'font-lock-face markdown-missing-link-face)
1925 ;; Second wiki link doesn't have a corresponding file
1926 (should (eq (markdown-next-link) 73))
1927 (markdown-test-range-has-property 73 88 'font-lock-face markdown-missing-link-face)
1928 ;; Move to third wiki link, and create the missing file
1929 (should (eq (markdown-next-link) 155))
1930 (should (string-equal (markdown-wiki-link-link) "inline"))
1931 (markdown-test-range-has-property 155 164 'font-lock-face markdown-link-face)
1932 ;; Remove temporary files
1933 (delete-file fn)
1936 ;;; Filling tests:
1938 (ert-deftest test-markdown-filling/blockquote ()
1939 "Test filling of blockquotes.
1940 See `adaptive-fill-first-line-regexp'."
1941 (markdown-test-string "> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
1942 (fill-paragraph)
1943 (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."))))
1945 (ert-deftest test-markdown-filling/list-item-plus ()
1946 "Test filling of list items with plus sign markers.
1947 See `adaptive-fill-regexp'."
1948 (markdown-test-string " + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
1949 (fill-paragraph)
1950 (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."))))
1952 (ert-deftest test-markdown-filling/list-item-plus-in-blockquote ()
1953 "Test filling of list items with plus sign markers inside blockquote.
1954 See `adaptive-fill-regexp'."
1955 (markdown-test-string "> + Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
1956 (fill-paragraph)
1957 (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."))))
1959 (ert-deftest test-markdown-filling/line-break ()
1960 "Test filling of paragraphs with hard line breaks.
1961 See `paragraph-separate'."
1962 (markdown-test-string "Lorem ipsum dolor sit amet, \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
1963 (let ((fill-column 70))
1964 (fill-paragraph)
1965 (should (string-equal (buffer-string) "Lorem ipsum dolor sit amet, \nconsectetur adipisicing elit, sed do eiusmod tempor incididunt ut\nlabore et dolore magna aliqua.")))))
1967 ;;; Export tests:
1969 (ert-deftest test-markdown-hook/xhtml-standalone ()
1970 "Test `markdown-xhtml-standalone-regexp' and `markdown-output-standalone-p'."
1971 (should (string-match markdown-xhtml-standalone-regexp
1972 "<?xml version='1.0' encoding='UTF-8'?>"))
1973 (should (string-match markdown-xhtml-standalone-regexp
1974 "<!DOCTYPE html>"))
1975 (should (string-match markdown-xhtml-standalone-regexp
1976 "<html>"))
1977 (should-not (string-match markdown-xhtml-standalone-regexp
1978 "<h1>title</h1>"))
1979 (should-not (string-match markdown-xhtml-standalone-regexp
1980 "<div id=\"name\">")))
1982 ;;; Hook tests:
1984 (ert-deftest test-markdown-hook/before-export ()
1985 "Test hook run before export XHTML."
1986 (markdown-test-temp-file "lists.text"
1987 (let* ((before-hook-run nil)
1988 (ofile (concat buffer-file-name ".html"))
1989 (func (lambda (fname)
1990 (setq before-hook-run t)
1991 (should (string-equal fname ofile)))))
1992 ;; Register function
1993 (add-hook 'markdown-before-export-hooks func)
1994 ;; Export XHTML
1995 (markdown-export ofile)
1996 (should (eq before-hook-run t))
1997 ;; Clean
1998 (remove-hook 'markdown-before-export-hooks func)
1999 (kill-buffer (get-file-buffer ofile))
2000 (delete-file ofile))))
2002 (ert-deftest test-markdown-hook/after-export ()
2003 "Test hook run after export XHTML."
2004 (markdown-test-temp-file "lists.text"
2005 (let* ((after-hook-run nil)
2006 (ofile (concat buffer-file-name ".html"))
2007 (func (lambda (fname)
2008 (setq after-hook-run t)
2009 (should (string-equal fname ofile)))))
2010 ;; Register function
2011 (add-hook 'markdown-after-export-hooks func)
2012 ;; Export XHTML
2013 (markdown-export ofile)
2014 (should (eq after-hook-run t))
2015 ;; Clean
2016 (remove-hook 'markdown-after-export-hooks func)
2017 (kill-buffer (get-file-buffer ofile))
2018 (delete-file ofile))))
2020 ;;; Extension: math support
2022 (ert-deftest test-markdown-math/file-local-variable ()
2023 "Test enabling math mode via `hack-local-variables-hook'."
2024 (markdown-test-file "math.text"
2025 (should-not markdown-enable-math)
2026 (hack-local-variables)
2027 (should markdown-enable-math)))
2029 (ert-deftest test-markdown-math/reload ()
2030 "Test enabling math mode via function `markdown-enable-math'."
2031 (markdown-test-file "math.text"
2032 (markdown-enable-math t)
2033 ;; Flag should be set to t
2034 (should markdown-enable-math)
2035 ;; Font-lock keywords should be updated
2036 (should (member (cons markdown-regex-math-display 'markdown-math-face)
2037 markdown-mode-font-lock-keywords))))
2039 (ert-deftest test-markdown-math/font-lock ()
2040 "Test markdown math mode."
2041 (markdown-test-file "math.text"
2042 (markdown-enable-math t)
2043 (font-lock-fontify-buffer)
2044 (markdown-test-range-has-face 1 32 nil)
2045 (markdown-test-range-has-face 33 46 markdown-math-face)
2046 (markdown-test-range-has-face 47 49 nil)
2047 (markdown-test-range-has-face 50 65 markdown-math-face)
2048 (markdown-test-range-has-face 66 98 nil)
2049 (markdown-test-range-has-face 99 114 markdown-math-face)))
2051 ;;; gfm-mode tests:
2053 (ert-deftest test-markdown-gfm/pre-1 ()
2054 "GFM pre block font lock test."
2055 (markdown-test-file-gfm "gfm.text"
2056 (markdown-test-range-has-face 2626 2637 nil)
2057 (markdown-test-range-has-face 2639 2641 markdown-pre-face)
2058 (markdown-test-range-has-face 2642 2645 markdown-language-keyword-face)
2059 (markdown-test-range-has-face 2647 2728 markdown-pre-face)
2060 (markdown-test-range-has-face 2730 2732 markdown-pre-face)))
2062 (ert-deftest test-markdown-gfm/italic-1 ()
2063 "GFM italic font lock test."
2064 (markdown-test-file-gfm "gfm.text"
2065 (markdown-test-range-has-face 1483 1488 markdown-italic-face)
2066 (markdown-test-range-has-face 1729 1790 nil)))
2068 (provide 'markdown-test)
2070 ;;; markdown-test.el ends here