tests.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. ;;;_ org-html/tests.el --- Tests for org-html
  2. ;;;_. Headers
  3. ;;;_ , License
  4. ;; Copyright (C) 2010 Tom Breton (Tehom)
  5. ;; Author: Tom Breton (Tehom) <tehom@panix.com>
  6. ;; Keywords: lisp, maint, internal
  7. ;; This file 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.
  11. ;; This file is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs; see the file COPYING. If not, write to
  17. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. ;; Boston, MA 02111-1307, USA.
  19. ;;;_ , Commentary:
  20. ;;
  21. ;;;_ , Requires
  22. (require 'org-html)
  23. ;;;_. Body
  24. ;;;_ , org-id testhelp
  25. ;;This would go into org-id/testhelp.el if there were such a file
  26. (defconst org-id:thd:usual-id-locations
  27. (org-id-alist-to-hash
  28. '(
  29. ("file1" "id-1-in-file1")
  30. ("file1" "id-2-in-file1")
  31. ("file2" "id-1-in-file2")))
  32. "A stable id-locations table for testing purposes" )
  33. ;;;_ . Validation
  34. (emt:deftest-3 org-id:thd:usual-id-locations
  35. (nil
  36. (progn
  37. (emt:doc "Operation: Look up one of the keys we inserted.")
  38. (emt:doc "Response: It has the value we gave it.")
  39. (assert
  40. (equal
  41. (gethash "id-1-in-file1" org-id:thd:usual-id-locations)
  42. "file1")))))
  43. ;;;_ , Config
  44. ;;;_ . org-html:thd:isolation
  45. (defconst org-html:thd:isolation
  46. ;;Can't hope to capture all the org configuration any time soon,
  47. ;;but let's set it up to some degree.
  48. ;;It is generally better for this to remain constant than to try to
  49. ;;sync this with new versions. To change it is effectively to
  50. ;;write new tests.
  51. '(let
  52. (
  53. (org-export-html-inline-image-extensions
  54. '("png" "jpeg" "jpg" "gif"))
  55. (org-html-cvt-link-fn nil)
  56. (org-export-first-hook nil)
  57. (org-par-open t)
  58. (org-url-encoding-use-url-hexify nil)
  59. ;;To control the org-id lookups
  60. (org-id-locations
  61. org-id:thd:usual-id-locations)
  62. ;;To control `org-default-export-plist'
  63. (org-export-inbuffer-options-extra nil)
  64. ;;To control `org-infile-export-plist'. Set up for minimal
  65. ;;export so we can more easily handle examples. When
  66. ;;specific behavior is to be tested, locally bind the
  67. ;;controlling variable(s), don't change them here.
  68. (org-export-html-link-up "")
  69. (org-export-html-link-home "")
  70. (org-export-default-language "en")
  71. (org-export-page-keywords "")
  72. (org-export-page-description "")
  73. (org-display-custom-times nil)
  74. (org-export-headline-levels 100)
  75. (org-export-with-section-numbers nil)
  76. (org-export-section-number-format '((("1" ".")) . ""))
  77. (org-export-with-toc nil)
  78. (org-export-preserve-breaks nil)
  79. (org-export-with-archived-trees nil)
  80. (org-export-with-emphasize nil)
  81. (org-export-with-sub-superscripts nil)
  82. (org-export-with-special-strings nil)
  83. (org-export-with-footnotes nil)
  84. (org-export-with-drawers nil)
  85. (org-export-with-tags nil)
  86. (org-export-with-todo-keywords nil)
  87. (org-export-with-priority nil)
  88. (org-export-with-TeX-macros nil)
  89. (org-export-with-LaTeX-fragments nil)
  90. (org-export-latex-listings nil)
  91. (org-export-skip-text-before-1st-heading nil)
  92. (org-export-with-fixed-width nil)
  93. (org-export-with-timestamps nil)
  94. (org-export-author-info nil)
  95. (org-export-email-info nil)
  96. (org-export-creator-info nil)
  97. (org-export-time-stamp-file nil)
  98. (org-export-with-tables nil)
  99. (org-export-highlight-first-table-line nil)
  100. (org-export-html-style-include-default nil)
  101. (org-export-html-style-include-scripts nil)
  102. (org-export-html-style "")
  103. (org-export-html-style-extra "")
  104. (org-agenda-export-html-style "")
  105. (org-export-html-link-org-files-as-html nil)
  106. (org-export-html-inline-images nil)
  107. (org-export-html-extension "html")
  108. (org-export-html-xml-declaration
  109. '(("html" . "<?xml version=\"1.0\" encoding=\"%s\"?>")
  110. ("php" . "<?php echo \"<?xml version=\\\"1.0\\\" encoding=\\\"%s\\\" ?>\"; ?>")))
  111. (org-export-html-table-tag
  112. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">")
  113. (org-export-html-expand nil)
  114. (org-export-html-with-timestamp nil)
  115. (org-export-publishing-directory nil)
  116. (org-export-html-preamble nil)
  117. (org-export-html-postamble nil)
  118. (org-export-html-auto-preamble nil)
  119. (org-export-html-auto-postamble nil)
  120. (user-full-name "Emtest user")
  121. (user-mail-address "emtest-user@localhost.localdomain")
  122. (org-export-select-tags '("export"))
  123. (org-export-exclude-tags '("noexport"))
  124. (org-export-latex-image-default-option nil)
  125. (org-export-plist-vars
  126. '(
  127. (:link-up nil org-export-html-link-up)
  128. (:link-home nil org-export-html-link-home)
  129. (:language nil org-export-default-language)
  130. (:keywords nil org-export-page-keywords)
  131. (:description nil org-export-page-description)
  132. (:customtime nil org-display-custom-times)
  133. (:headline-levels "H" org-export-headline-levels)
  134. (:section-numbers "num" org-export-with-section-numbers)
  135. (:section-number-format nil org-export-section-number-format)
  136. (:table-of-contents "toc" org-export-with-toc)
  137. (:preserve-breaks "\\n" org-export-preserve-breaks)
  138. (:archived-trees nil org-export-with-archived-trees)
  139. (:emphasize "*" org-export-with-emphasize)
  140. (:sub-superscript "^" org-export-with-sub-superscripts)
  141. (:special-strings "-" org-export-with-special-strings)
  142. (:footnotes "f" org-export-with-footnotes)
  143. (:drawers "d" org-export-with-drawers)
  144. (:tags "tags" org-export-with-tags)
  145. (:todo-keywords "todo" org-export-with-todo-keywords)
  146. (:priority "pri" org-export-with-priority)
  147. (:TeX-macros "TeX" org-export-with-TeX-macros)
  148. (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments)
  149. (:latex-listings nil org-export-latex-listings)
  150. (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading)
  151. (:fixed-width ":" org-export-with-fixed-width)
  152. (:timestamps "<" org-export-with-timestamps)
  153. (:author-info "author" org-export-author-info)
  154. (:email-info "email" org-export-email-info)
  155. (:creator-info "creator" org-export-creator-info)
  156. (:time-stamp-file "timestamp" org-export-time-stamp-file)
  157. (:tables "|" org-export-with-tables)
  158. (:table-auto-headline nil org-export-highlight-first-table-line)
  159. (:style-include-default nil org-export-html-style-include-default)
  160. (:style-include-scripts nil org-export-html-style-include-scripts)
  161. (:style nil org-export-html-style)
  162. (:style-extra nil org-export-html-style-extra)
  163. (:agenda-style nil org-agenda-export-html-style)
  164. (:convert-org-links nil org-export-html-link-org-files-as-html)
  165. (:inline-images nil org-export-html-inline-images)
  166. (:html-extension nil org-export-html-extension)
  167. (:xml-declaration nil org-export-html-xml-declaration)
  168. (:html-table-tag nil org-export-html-table-tag)
  169. (:expand-quoted-html "@" org-export-html-expand)
  170. (:timestamp nil org-export-html-with-timestamp)
  171. (:publishing-directory nil org-export-publishing-directory)
  172. (:preamble nil org-export-html-preamble)
  173. (:postamble nil org-export-html-postamble)
  174. (:auto-preamble nil org-export-html-auto-preamble)
  175. (:auto-postamble nil org-export-html-auto-postamble)
  176. (:author nil user-full-name)
  177. (:email nil user-mail-address)
  178. (:select-tags nil org-export-select-tags)
  179. (:exclude-tags nil org-export-exclude-tags)
  180. (:latex-image-options nil org-export-latex-image-default-option)))
  181. ))
  182. "Isolation let-form for org-html tests.
  183. Isolation let-forms are intended to be included by
  184. `:surrounders'. They provide a known configuration and keep
  185. tests from altering the outside state." )
  186. ;;;_ , Examples
  187. (defconst org-html:thd:examples
  188. (emt:eg:define+ ;;xmp:tqu804919ze0
  189. ((project org)
  190. (library html)
  191. (subsection link-examples))
  192. (group
  193. ((name only-path))
  194. ;;No src-text because this arglist wouldn't be generated by
  195. ;;org-export-as-html, though it might be created by custom link
  196. ;;types.
  197. (item ((type arglist))
  198. '("" "foo" nil "desc" nil nil))
  199. (item ((type link-text))
  200. "<a href=\"foo\">desc</a>"))
  201. (group
  202. ((name only-fragment))
  203. ;;No src-text, same reason
  204. (item ((type arglist))
  205. '("" "" "bar" "desc" nil nil))
  206. (item ((type link-text))
  207. "<a href=\"#bar\">desc</a>"))
  208. (group
  209. ((name all-3-parts))
  210. (item ((type src-text))
  211. "[[http:foo#bar][desc]]")
  212. (item ((type arglist))
  213. '("http" "foo" "bar" "desc" nil nil))
  214. (item ((type link-text))
  215. "<a href=\"http:foo#bar\">desc</a>"))
  216. ;;Filename has to be absolute to trigger substitution.
  217. (group
  218. ((name subst-in-filename))
  219. (item ((type src-text))
  220. "[[file:/foo/unfoo/.././baz][desc]]")
  221. (item ((type arglist))
  222. '("file" "/foo/unfoo/.././baz" "" "desc" nil nil))
  223. (item ((type link-text))
  224. "<a href=\"file:/foo/baz\">desc</a>"))
  225. (group
  226. ((name type=file))
  227. (item ((type src-text))
  228. "[[file:foo.txt][desc]]")
  229. (item ((type arglist))
  230. '("file" "foo.txt" "" "desc" nil nil))
  231. (item ((type link-text))
  232. "<a href=\"file:foo.txt\">desc</a>"))
  233. ;;We control what location id finds by controlling
  234. ;;`org-id-locations' in `org-html:thd:isolation'
  235. (group
  236. ((name type=id))
  237. (item ((type src-text))
  238. "[[id:id-1-in-file1][desc]]")
  239. (item ((type arglist))
  240. '("" "file1" "id-1-in-file1" "desc" nil nil))
  241. (item ((type link-text))
  242. "<a href=\"file1#id-1-in-file1\">desc</a>"))
  243. (group
  244. ((name type=ftp))
  245. (item ((type src-text))
  246. "[[ftp:foo.com][desc]]")
  247. (item ((type arglist))
  248. '("ftp" "foo.com" "" "desc" nil nil))
  249. (item ((type link-text))
  250. "<a href=\"ftp:foo.com\">desc</a>"))
  251. ;;Punt coderef, internal logic is too hairy, would have to control
  252. ;;`org-export-get-coderef-format'.
  253. ;;Punt custom links, would have to make a controlled
  254. ;;`org-link-protocols', which means identifying and binding every
  255. ;;variable that `org-add-link-type' alters, then binding
  256. ;;`org-link-protocols' to empty list, then calling
  257. ;;`org-add-link-type' (possibly for id as well)
  258. (group
  259. ((name convertable))
  260. (item ((type src-text))
  261. "[[file:foo.org][desc]]")
  262. (item ((type arglist))
  263. '("file" "foo.org" nil "desc" nil nil))
  264. (group
  265. ((type link-text))
  266. (item ((subname old-conversion))
  267. "<a href=\"http:foo.html\">desc</a>")
  268. (item ((subname new-conversion))
  269. "<a href=\"xform:transformed-foo.org\">desc</a>")
  270. (item ((subname no-conversion))
  271. "<a href=\"file:foo.org\">desc</a>")))
  272. ))
  273. ;;;_ , Helpers
  274. (defun org-html:th:cvt-fn (opt-plist type path)
  275. "Trivial URL transformer"
  276. (declare (ignored opt-plist))
  277. (list
  278. "xform"
  279. (concat "transformed-" path)))
  280. (defun org-html:th:check-link-matches (expected)
  281. "Build a link text and check it against expected text.
  282. Sensitive to emt:eg narrowing."
  283. (assert
  284. (equal
  285. (apply #'org-html-make-link
  286. '(:html-extension "html")
  287. (emt:eg (type arglist)))
  288. expected)
  289. t))
  290. ;;;_ , org-html-make-link
  291. (emt:deftest-3
  292. ((of 'org-html-make-link)
  293. (:surrounders
  294. (list
  295. org-html:thd:isolation
  296. '(emt:eg:with org-html:thd:examples
  297. ((project org)
  298. (library html)
  299. (subsection link-examples))))))
  300. (nil
  301. (emt:eg:map name name
  302. (unless
  303. (eq name 'convertable)
  304. (emt:doc "Proves: Example arglist gives the expected result.")
  305. (org-html:th:check-link-matches
  306. (emt:eg (type link-text))))))
  307. (nil
  308. (emt:eg:narrow ((name convertable))
  309. (let
  310. (
  311. (org-export-html-link-org-files-as-html t)
  312. (org-html-cvt-link-fn nil))
  313. (emt:doc "Proves: Old org->html conversion works.")
  314. (org-html:th:check-link-matches
  315. (emt:eg (type link-text) (subname old-conversion))))))
  316. (nil
  317. (emt:eg:narrow ((name convertable))
  318. (let
  319. (
  320. (org-export-html-link-org-files-as-html nil)
  321. (org-html-cvt-link-fn #'org-html:th:cvt-fn))
  322. (emt:doc "Proves: New file->url conversion works.")
  323. (org-html:th:check-link-matches
  324. (emt:eg (type link-text) (subname new-conversion))))))
  325. (nil
  326. (emt:eg:narrow ((name convertable))
  327. (let
  328. (
  329. (org-export-html-link-org-files-as-html t)
  330. (org-html-cvt-link-fn #'org-html:th:cvt-fn))
  331. (emt:doc "Proves: New conversion has precedence over old.")
  332. (org-html:th:check-link-matches
  333. (emt:eg (type link-text) (subname new-conversion))))))
  334. ;;Add tests for making images - but it's nearly direct.
  335. )
  336. ;;;_ , Helpers
  337. (defun org-html:th:build-source (type path fragment &optional desc
  338. descp attr may-inline-p)
  339. ""
  340. (declare (ignored descp attr may-inline-p))
  341. (concat
  342. "[["type":"
  343. (org-link-escape path)
  344. (if fragment
  345. (cond
  346. ((string= type "file")(concat "::" fragment))
  347. ((string= type "http")(concat "#" fragment))))
  348. "]["desc"]]"))
  349. ;;;_ . org-html:th:strip-whitepadding
  350. (defun org-html:th:strip-whitepadding (str)
  351. ""
  352. (with-temp-buffer
  353. (insert str)
  354. (goto-char (point-min))
  355. (while (search-forward "\n" nil t)
  356. (replace-match ""))
  357. (goto-char (point-min))
  358. (while (search-forward "<p>" nil t)
  359. (replace-match ""))
  360. (goto-char (point-min))
  361. (while (search-forward "</p>" nil t)
  362. (replace-match ""))
  363. (buffer-string)))
  364. ;;;_ , Examples
  365. (defconst org-html:stripwhite:thd:examples
  366. (emt:eg:define+ ;;xmp:khpjmfi0aze0
  367. ((project org)(library html)
  368. (subsection org-html:th:strip-whitepadding)
  369. (type string)
  370. (role before))
  371. (item ((name 0))
  372. "\na\nb")
  373. (item ((name 1))
  374. "\n<p>ab")
  375. (item ((name 2))
  376. "\n</p>a\nb")))
  377. ;;;_ , Tests
  378. (emt:deftest-3 org-html:th:strip-whitepadding
  379. (nil
  380. (emt:eg:with org-html:stripwhite:thd:examples
  381. ((project org)(library html)
  382. (subsection org-html:th:strip-whitepadding))
  383. (emt:eg:map name name
  384. (emt:doc
  385. "Check: The stripped string matches what's expected.")
  386. (assert
  387. (string=
  388. (org-html:th:strip-whitepadding (emt:eg))
  389. "ab"))))))
  390. ;;;_ , org-export-as-html
  391. (emt:deftest-3
  392. ((of 'org-export-as-html)
  393. (:surrounders
  394. (list
  395. org-html:thd:isolation
  396. ;;Re-use the link examples.
  397. '(emt:eg:with org-html:thd:examples
  398. ((project org)(library html))))))
  399. (nil
  400. (emt:eg:narrow ((subsection link-examples))
  401. (emt:eg:map name name
  402. (when
  403. (and
  404. (not (eq name 'convertable))
  405. ;;Dormant for id because it wants to find filename
  406. ;;relative to `org-current-export-file', but for
  407. ;;buffer export there is none.
  408. (not (eq name 'type=id))
  409. (emt:eg:boundp '(type src-text)))
  410. (emt:doc
  411. "Situation: the only thing in the buffer is that link")
  412. (with-buffer-containing-object
  413. (:string
  414. (emt:eg (type src-text)))
  415. (org-mode)
  416. ;;This calculation has to be done outside the assert
  417. ;;or it will be done twice.
  418. (emt:doc "Operation: export the buffer as HTML.")
  419. (let
  420. ((result
  421. (org-html:th:strip-whitepadding
  422. (org-export-region-as-html
  423. (point-min)
  424. (point-max)
  425. t
  426. 'string))))
  427. (emt:doc
  428. "Proves: Example arglist gives the expected result.")
  429. (assert
  430. (string=
  431. result
  432. (emt:eg (type link-text)))
  433. t)))))))
  434. ;;Could also use testpoints to test that we feed the link-builder
  435. ;;functions as expected.
  436. ;;Could also make example files and convert them.
  437. )
  438. ;;;_. Footers
  439. ;;;_ , Provides
  440. (provide 'org-html/tests)
  441. ;;;_ * Local emacs vars.
  442. ;;;_ + Local variables:
  443. ;;;_ + mode: allout
  444. ;;;_ + End:
  445. ;;;_ , End
  446. ;;; org-html/tests.el ends here