test-org-element.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. ;;; test-org-element.el --- Tests for org-element.el
  2. ;; Copyright (C) 2012 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;; Code:
  16. (unless (featurep 'org-element)
  17. (signal 'missing-test-dependency "org-element"))
  18. ;;; Tests:
  19. ;;;; Headlines
  20. (ert-deftest test-org-element/headline-quote-keyword ()
  21. "Test QUOTE keyword recognition."
  22. ;; Reference test.
  23. (org-test-with-temp-text "* Headline"
  24. (let ((org-quote-string "QUOTE"))
  25. (should-not (org-element-property :quotedp (org-element-at-point)))))
  26. ;; Standard position.
  27. (org-test-with-temp-text "* QUOTE Headline"
  28. (let ((org-quote-string "QUOTE"))
  29. (let ((headline (org-element-at-point)))
  30. (should (org-element-property :quotedp headline))
  31. ;; Test removal from raw value.
  32. (should (equal (org-element-property :raw-value headline) "Headline"))))
  33. ;; Case sensitivity.
  34. (let ((org-quote-string "Quote"))
  35. (should-not (org-element-property :quotedp (org-element-at-point)))))
  36. ;; With another keyword.
  37. (org-test-with-temp-text "* TODO QUOTE Headline"
  38. (let ((org-quote-string "QUOTE")
  39. (org-todo-keywords '((sequence "TODO" "DONE"))))
  40. (should (org-element-property :quotedp (org-element-at-point))))))
  41. (ert-deftest test-org-element/headline-comment-keyword ()
  42. "Test COMMENT keyword recognition."
  43. ;; Reference test.
  44. (org-test-with-temp-text "* Headline"
  45. (let ((org-comment-string "COMMENT"))
  46. (should-not (org-element-property :commentedp (org-element-at-point)))))
  47. ;; Standard position.
  48. (org-test-with-temp-text "* COMMENT Headline"
  49. (let ((org-comment-string "COMMENT"))
  50. (let ((headline (org-element-at-point)))
  51. (should (org-element-property :commentedp headline))
  52. ;; Test removal from raw value.
  53. (should (equal (org-element-property :raw-value headline) "Headline"))))
  54. ;; Case sensitivity.
  55. (let ((org-comment-string "Comment"))
  56. (should-not (org-element-property :commentedp (org-element-at-point)))))
  57. ;; With another keyword.
  58. (org-test-with-temp-text "* TODO COMMENT Headline"
  59. (let ((org-comment-string "COMMENT")
  60. (org-todo-keywords '((sequence "TODO" "DONE"))))
  61. (should (org-element-property :commentedp (org-element-at-point))))))
  62. (ert-deftest test-org-element/headline-archive-tag ()
  63. "Test ARCHIVE tag recognition."
  64. ;; Reference test.
  65. (org-test-with-temp-text "* Headline"
  66. (let ((org-archive-tag "ARCHIVE"))
  67. (should-not (org-element-property :archivedp (org-element-at-point)))))
  68. ;; Single tag.
  69. (org-test-with-temp-text "* Headline :ARCHIVE:"
  70. (let ((org-archive-tag "ARCHIVE"))
  71. (let ((headline (org-element-at-point)))
  72. (should (org-element-property :archivedp headline))
  73. ;; Test tag removal.
  74. (should-not (org-element-property :tags headline))))
  75. (let ((org-archive-tag "Archive"))
  76. (should-not (org-element-property :archivedp (org-element-at-point)))))
  77. ;; Multiple tags.
  78. (org-test-with-temp-text "* Headline :test:ARCHIVE:"
  79. (let ((org-archive-tag "ARCHIVE"))
  80. (let ((headline (org-element-at-point)))
  81. (should (org-element-property :archivedp headline))
  82. ;; Test tag removal.
  83. (should (equal (org-element-property :tags headline) ":test:"))))))
  84. ;;;; Example-blocks and Src-blocks
  85. (ert-deftest test-org-element/block-switches ()
  86. "Test `example-block' and `src-block' switches parsing."
  87. (let ((org-coderef-label-format "(ref:%s)"))
  88. ;; 1. Test "-i" switch.
  89. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  90. (let ((element (org-element-current-element)))
  91. (should-not (org-element-property :preserve-indent element))))
  92. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  93. (let ((element (org-element-current-element)))
  94. (should (org-element-property :preserve-indent element))))
  95. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  96. (let ((element (org-element-current-element)))
  97. (should-not (org-element-property :preserve-indent element))))
  98. (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
  99. (let ((element (org-element-current-element)))
  100. (should (org-element-property :preserve-indent element))))
  101. ;; 2. "-n -r -k" combination should number lines, retain labels but
  102. ;; not use them in coderefs.
  103. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\N#+END_EXAMPLE"
  104. (let ((element (org-element-current-element)))
  105. (should (and (org-element-property :number-lines element)
  106. (org-element-property :retain-labels element)
  107. (not (org-element-property :use-labels element))))))
  108. (org-test-with-temp-text
  109. "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
  110. (let ((element (org-element-current-element)))
  111. (should (and (org-element-property :number-lines element)
  112. (org-element-property :retain-labels element)
  113. (not (org-element-property :use-labels element))))))
  114. ;; 3. "-n -r" combination should number-lines remove labels and not
  115. ;; use them in coderefs.
  116. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
  117. (let ((element (org-element-current-element)))
  118. (should (and (org-element-property :number-lines element)
  119. (not (org-element-property :retain-labels element))
  120. (not (org-element-property :use-labels element))))))
  121. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
  122. (let ((element (org-element-current-element)))
  123. (should (and (org-element-property :number-lines element)
  124. (not (org-element-property :retain-labels element))
  125. (not (org-element-property :use-labels element))))))
  126. ;; 4. "-n" or "+n" should number lines, retain labels and use them
  127. ;; in coderefs.
  128. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
  129. (let ((element (org-element-current-element)))
  130. (should (and (org-element-property :number-lines element)
  131. (org-element-property :retain-labels element)
  132. (org-element-property :use-labels element)))))
  133. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
  134. (let ((element (org-element-current-element)))
  135. (should (and (org-element-property :number-lines element)
  136. (org-element-property :retain-labels element)
  137. (org-element-property :use-labels element)))))
  138. (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
  139. (let ((element (org-element-current-element)))
  140. (should (and (org-element-property :number-lines element)
  141. (org-element-property :retain-labels element)
  142. (org-element-property :use-labels element)))))
  143. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
  144. (let ((element (org-element-current-element)))
  145. (should (and (org-element-property :number-lines element)
  146. (org-element-property :retain-labels element)
  147. (org-element-property :use-labels element)))))
  148. ;; 5. No switch should not number lines, but retain labels and use
  149. ;; them in coderefs.
  150. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  151. (let ((element (org-element-current-element)))
  152. (should (and (not (org-element-property :number-lines element))
  153. (org-element-property :retain-labels element)
  154. (org-element-property :use-labels element)))))
  155. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  156. (let ((element (org-element-current-element)))
  157. (should (and (not (org-element-property :number-lines element))
  158. (org-element-property :retain-labels element)
  159. (org-element-property :use-labels element)))))
  160. ;; 6. "-r" switch only: do not number lines, remove labels, and
  161. ;; don't use labels in coderefs.
  162. (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
  163. (let ((element (org-element-current-element)))
  164. (should (and (not (org-element-property :number-lines element))
  165. (not (org-element-property :retain-labels element))
  166. (not (org-element-property :use-labels element))))))
  167. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
  168. (let ((element (org-element-current-element)))
  169. (should (and (not (org-element-property :number-lines element))
  170. (not (org-element-property :retain-labels element))
  171. (not (org-element-property :use-labels element))))))
  172. ;; 7. Recognize coderefs with user-defined syntax.
  173. (org-test-with-temp-text
  174. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
  175. (let ((element (org-element-current-element)))
  176. (should
  177. (equal (org-element-property :coderef-fmt element) "[ref:%s]"))))
  178. (org-test-with-temp-text
  179. "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
  180. (let ((element (org-element-current-element)))
  181. (should
  182. (equal (org-element-property :coderef-fmt element) "[ref:%s]"))))))
  183. ;;; Navigation tools.
  184. (ert-deftest test-org-element/forward-element ()
  185. "Test `org-element-forward' specifications."
  186. ;; 1. At EOB: should error.
  187. (org-test-with-temp-text "Some text\n"
  188. (goto-char (point-max))
  189. (should-error (org-element-forward)))
  190. ;; 2. Standard move: expected to ignore blank lines.
  191. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  192. (org-element-forward)
  193. (should (looking-at "Second paragraph.")))
  194. ;; 3. Headline tests.
  195. (org-test-with-temp-text "
  196. * Head 1
  197. ** Head 1.1
  198. *** Head 1.1.1
  199. ** Head 1.2"
  200. ;; 3.1. At an headline beginning: move to next headline at the
  201. ;; same level.
  202. (goto-line 3)
  203. (org-element-forward)
  204. (should (looking-at "** Head 1.2"))
  205. ;; 3.2. At an headline beginning: move to parent headline if no
  206. ;; headline at the same level.
  207. (goto-line 3)
  208. (org-element-forward)
  209. (should (looking-at "** Head 1.2")))
  210. ;; 4. Greater element tests.
  211. (org-test-with-temp-text
  212. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  213. ;; 4.1. At a greater element: expected to skip contents.
  214. (org-element-forward)
  215. (should (looking-at "Outside."))
  216. ;; 4.2. At the end of greater element contents: expected to skip
  217. ;; to the end of the greater element.
  218. (goto-line 2)
  219. (org-element-forward)
  220. (should (looking-at "Outside.")))
  221. ;; 5. List tests.
  222. (org-test-with-temp-text "
  223. - item1
  224. - sub1
  225. - sub2
  226. - sub3
  227. Inner paragraph.
  228. - item2
  229. Outside."
  230. ;; 5.1. At list top point: expected to move to the element after
  231. ;; the list.
  232. (goto-line 2)
  233. (org-element-forward)
  234. (should (looking-at "Outside."))
  235. ;; 5.2. Special case: at the first line of a sub-list, but not at
  236. ;; beginning of line, move to next item.
  237. (goto-line 2)
  238. (forward-char)
  239. (org-element-forward)
  240. (should (looking-at "- item2"))
  241. (goto-line 4)
  242. (forward-char)
  243. (org-element-forward)
  244. (should (looking-at " - sub2"))
  245. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  246. (goto-line 4)
  247. (org-element-forward)
  248. (should (looking-at " Inner paragraph."))
  249. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  250. (goto-line 8)
  251. (org-element-forward)
  252. (should (looking-at " Inner paragraph."))
  253. ;; 5.5. At an item: expected to move to next item, if any.
  254. (goto-line 6)
  255. (org-element-forward)
  256. (should (looking-at " - sub3"))))
  257. (ert-deftest test-org-element/backward-element ()
  258. "Test `org-element-backward' specifications."
  259. ;; 1. At BOB (modulo some white spaces): should error.
  260. (org-test-with-temp-text " \nParagraph."
  261. (org-skip-whitespace)
  262. (should-error (org-element-backward)))
  263. ;; 2. Not at the beginning of an element: move at its beginning.
  264. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  265. (goto-line 3)
  266. (end-of-line)
  267. (org-element-backward)
  268. (should (looking-at "Paragraph2.")))
  269. ;; 3. Headline tests.
  270. (org-test-with-temp-text "
  271. * Head 1
  272. ** Head 1.1
  273. *** Head 1.1.1
  274. ** Head 1.2"
  275. ;; 3.1. At an headline beginning: move to previous headline at the
  276. ;; same level.
  277. (goto-line 5)
  278. (org-element-backward)
  279. (should (looking-at "** Head 1.1"))
  280. ;; 3.2. At an headline beginning: move to parent headline if no
  281. ;; headline at the same level.
  282. (goto-line 3)
  283. (org-element-backward)
  284. (should (looking-at "* Head 1"))
  285. ;; 3.3. At the first top-level headline: should error.
  286. (goto-line 2)
  287. (should-error (org-element-backward)))
  288. ;; 4. At beginning of first element inside a greater element:
  289. ;; expected to move to greater element's beginning.
  290. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER."
  291. (goto-line 3)
  292. (org-element-backward)
  293. (should (looking-at "#\\+BEGIN_CENTER")))
  294. ;; 5. List tests.
  295. (org-test-with-temp-text "
  296. - item1
  297. - sub1
  298. - sub2
  299. - sub3
  300. Inner paragraph.
  301. - item2
  302. Outside."
  303. ;; 5.1. At beginning of sub-list: expected to move to the
  304. ;; paragraph before it.
  305. (goto-line 4)
  306. (org-element-backward)
  307. (should (looking-at "item1"))
  308. ;; 5.2. At an item in a list: expected to move at previous item.
  309. (goto-line 8)
  310. (org-element-backward)
  311. (should (looking-at " - sub2"))
  312. (goto-line 12)
  313. (org-element-backward)
  314. (should (looking-at "- item1"))
  315. ;; 5.3. At end of list/sub-list: expected to move to list/sub-list
  316. ;; beginning.
  317. (goto-line 10)
  318. (org-element-backward)
  319. (should (looking-at " - sub1"))
  320. (goto-line 15)
  321. (org-element-backward)
  322. (should (looking-at "- item1"))
  323. ;; 5.4. At blank-lines before list end: expected to move to top
  324. ;; item.
  325. (goto-line 14)
  326. (org-element-backward)
  327. (should (looking-at "- item1"))))
  328. (ert-deftest test-org-element/up-element ()
  329. "Test `org-element-up' specifications."
  330. ;; 1. At BOB or with no surrounding element: should error.
  331. (org-test-with-temp-text "Paragraph."
  332. (should-error (org-element-up)))
  333. (org-test-with-temp-text "* Head1\n* Head2"
  334. (goto-line 2)
  335. (should-error (org-element-up)))
  336. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  337. (goto-line 3)
  338. (should-error (org-element-up)))
  339. ;; 2. At an headline: move to parent headline.
  340. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  341. (goto-line 3)
  342. (org-element-up)
  343. (should (looking-at "\\* Head1")))
  344. ;; 3. Inside a greater element: move to greater element beginning.
  345. (org-test-with-temp-text
  346. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  347. (goto-line 3)
  348. (org-element-up)
  349. (should (looking-at "#\\+BEGIN_CENTER")))
  350. ;; 4. List tests.
  351. (org-test-with-temp-text "* Top
  352. - item1
  353. - sub1
  354. - sub2
  355. Paragraph within sub2.
  356. - item2"
  357. ;; 4.1. Within an item: move to the item beginning.
  358. (goto-line 8)
  359. (org-element-up)
  360. (should (looking-at " - sub2"))
  361. ;; 4.2. At an item in a sub-list: move to parent item.
  362. (goto-line 4)
  363. (org-element-up)
  364. (should (looking-at "- item1"))
  365. ;; 4.3. At an item in top list: move to beginning of whole list.
  366. (goto-line 10)
  367. (org-element-up)
  368. (should (looking-at "- item1"))
  369. ;; 4.4. Special case. At very top point: should move to parent of
  370. ;; list.
  371. (goto-line 2)
  372. (org-element-up)
  373. (should (looking-at "\\* Top"))))
  374. (ert-deftest test-org-element/down-element ()
  375. "Test `org-element-down' specifications."
  376. ;; 1. Error when the element hasn't got a recursive type.
  377. (org-test-with-temp-text "Paragraph."
  378. (should-error (org-element-down)))
  379. ;; 2. When at a plain-list, move to first item.
  380. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  381. (goto-line 2)
  382. (org-element-down)
  383. (should (looking-at " - Item 1.1")))
  384. ;; 3. Otherwise, move inside the greater element.
  385. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  386. (org-element-down)
  387. (should (looking-at "Paragraph"))))
  388. (provide 'test-org-element)
  389. ;;; test-org-element.el ends here