Add Makefile with test target
[markdown-mode.git] / markdown-test.el
blob4472302ed3c8ea800e8b96b05b9f05403d6d7571
1 ;;;; markdown-test.el --- Tests for markdown-mode
3 ;; Copyright (C) 2013 Jason R. Blevins <jrblevin@sdf.org>
5 ;; This file is not part of GNU Emacs.
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program; if not, write to the Free Software
19 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;; This file contains the `markdown-mode' test suite. To run the tests:
26 ;; M-x load-file markdown-test.el
27 ;; M-x markdown-test
29 ;;; Code:
31 (unless (featurep 'markdown-mode)
32 (require 'markdown-mode))
34 (defconst markdown-test-dir
35 (concat
36 (expand-file-name (file-name-directory
37 (or load-file-name buffer-file-name)))
38 "tests/"))
40 (defmacro markdown-test-string (string &rest body)
41 "Run body in a temporary buffer containing STRING."
42 `(with-temp-buffer
43 (markdown-mode)
44 (insert ,string)
45 (goto-char (point-min))
46 (prog1 ,@body (kill-buffer))))
47 (def-edebug-spec markdown-test-string (form body))
49 (defmacro markdown-test-file (file &rest body)
50 "Open FILE from `markdown-test-dir' and execute body."
51 `(let ((fn (concat markdown-test-dir ,file)))
52 (save-window-excursion
53 (with-temp-buffer
54 (insert-file-contents fn)
55 (markdown-mode)
56 (goto-char (point-min))
57 (font-lock-fontify-buffer)
58 ,@body))))
59 (def-edebug-spec markdown-test-file (form body))
61 (defmacro markdown-test-temp-file (file &rest body)
62 "Open FILE from `markdown-test-dir' visiting temp file and execute body.
63 This file is not saved."
64 `(let ((fn (concat markdown-test-dir ,file))
65 (tmp (concat (make-temp-file "markdown-test") ".text"))
66 buf)
67 (save-window-excursion
68 (setq buf (find-file tmp))
69 (insert-file-contents fn)
70 (markdown-mode)
71 (goto-char (point-min))
72 (font-lock-fontify-buffer)
73 ,@body
74 (kill-buffer buf))))
75 (def-edebug-spec markdown-test-temp-file (form body))
77 (defun markdown-test-range-has-face (begin end face)
78 (let (loc)
79 (dolist (loc (number-sequence begin end))
80 (should (eq (get-text-property loc 'face) face)))))
82 (defun markdown-test ()
83 "Run all defined tests for `markdown-mode'."
84 (interactive)
85 (ert "markdown"))
87 ;;; Example tests:
89 (ert-deftest test-markdown-example/string ()
90 "An example string test using the `ert' framework."
91 (markdown-test-string "foo *bar* baz"
92 (goto-char 5)
93 (delete-char 1)
94 (should (looking-at "bar"))))
96 (ert-deftest test-markdown-example/file ()
97 "An example file test using the `ert' framework."
98 (markdown-test-file "inline.text"
99 (goto-char 9)
100 (should (looking-at "\*"))))
102 ;;; Basic mode tests:
104 (ert-deftest test-markdown-mode/variables ()
105 "Test `markdown-mode' variables."
106 (markdown-test-file "inline.text"
107 (should (= tab-width 4))
108 (should (eq font-lock-multiline t))
109 (should (eq major-mode 'markdown-mode))))
111 ;;; Font lock tests:
113 (ert-deftest test-markdown-font-lock/italics-1 ()
114 "A simple italics test."
115 (markdown-test-file "inline.text"
116 (goto-char 9)
117 (should (looking-at "\*"))
118 ;; Check face of char before leading asterisk
119 (should (eq (get-text-property 8 'face) nil))
120 ;; Check face of leading asterisk
121 (should (eq (get-text-property 9 'face) markdown-italic-face))
122 ;; Check face of trailing asterisk
123 (should (eq (get-text-property 17 'face) markdown-italic-face))
124 ;; Check face of point past leading asterisk
125 (should (eq (get-text-property 18 'face) nil))))
127 (ert-deftest test-markdown-font-lock/bold-1 ()
128 "A simple bold test."
129 (markdown-test-file "inline.text"
130 (goto-char 27)
131 (should (looking-at "\*\*"))
132 ;; Check face of char before leading asterisk
133 (should (eq (get-text-property 26 'face) nil))
134 ;; Check face of leading asterisk
135 (should (eq (get-text-property 27 'face) markdown-bold-face))
136 ;; Check face of trailing asterisk
137 (should (eq (get-text-property 35 'face) markdown-bold-face))
138 ;; Check face of point past leading asterisk
139 (should (eq (get-text-property 36 'face) nil))))
141 (ert-deftest test-markdown-font-lock/code-1 ()
142 "A simple inline code test."
143 (markdown-test-file "inline.text"
144 (goto-char 45)
145 (should (looking-at "`"))
146 (markdown-test-range-has-face 45 50 markdown-inline-code-face)
147 (markdown-test-range-has-face 61 89 markdown-inline-code-face)
148 ;; These tests are known to fail:
149 ;; (markdown-test-range-has-face 122 128 markdown-inline-code-face)
150 ;; (markdown-test-range-has-face 288 296 markdown-inline-code-face)
153 ;;; Wiki link tests:
155 (ert-deftest test-markdown-wiki-link/aliasing ()
156 "Test filename extraction for aliased wiki links."
157 (markdown-test-file "wiki-links.text"
158 ;; Confirm location of first wiki link
159 (should (eq (markdown-next-wiki-link) 8))
160 ;; Confirm location of second wiki link
161 (should (eq (markdown-next-wiki-link) 73))
162 ;; Test predicate function
163 (should (markdown-wiki-link-p))
164 ;; Test alias-first filename extraction
165 (setq markdown-wiki-link-alias-first t)
166 (should (string-equal (markdown-wiki-link-link) "second"))
167 ;; Test alias-second filename extraction
168 (setq markdown-wiki-link-alias-first nil)
169 (should (string-equal (markdown-wiki-link-link) "first"))))
171 (ert-deftest test-markdown-wiki-link/navigation ()
172 "Test wiki link navigation."
173 (markdown-test-file "wiki-links.text"
174 ;; Advance to first link
175 (should (eq (markdown-next-wiki-link) 8))
176 ;; Advance to second link
177 (should (eq (markdown-next-wiki-link) 73))
178 ;; Avance to final link
179 (should (eq (markdown-next-wiki-link) 155))
180 ;; Return nil and don't advance point
181 (should (eq (markdown-next-wiki-link) nil))
182 (should (eq (point) 155))
183 ;; Move back to second link
184 (should (eq (markdown-previous-wiki-link) 73))
185 ;; Move back to first link
186 (should (eq (markdown-previous-wiki-link) 8))
187 ;; Return nil and don't move point
188 (should (eq (markdown-previous-wiki-link) nil))
189 (should (eq (point) 8))))
191 (ert-deftest test-markdown-wiki-link/font-lock ()
192 "Test font lock faces for wiki links."
193 (markdown-test-temp-file "wiki-links.text"
194 (let* ((fn (concat (file-name-directory buffer-file-name)
195 "inline.text")))
196 ;; Create inline.text in the same temp directory, refontify
197 (write-region "" nil fn nil 1)
198 (markdown-fontify-buffer-wiki-links)
199 ;; Confirm location of first wiki link
200 (should (eq (markdown-next-wiki-link) 8))
201 ;; First wiki link doesn't have a corresponding file
202 (should (eq (get-char-property 8 'font-lock-face) markdown-missing-link-face))
203 ;; Second wiki link doesn't have a corresponding file
204 (should (eq (markdown-next-wiki-link) 73))
205 (should (eq (get-char-property 8 'font-lock-face) markdown-missing-link-face))
206 ;; Move to third wiki link, and create the missing file
207 (should (eq (markdown-next-wiki-link) 155))
208 (should (string-equal (markdown-wiki-link-link) "inline"))
209 (should (eq (get-char-property 155 'font-lock-face) markdown-link-face))
210 ;; Remove temporary files
211 (delete-file fn)