test-org-capture.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. ;;; test-org-capture.el --- Tests for org-capture.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015, 2017 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  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. ;; Unit tests for Org Capture library.
  16. ;;; Code:
  17. (require 'org-capture)
  18. (ert-deftest test-org-capture/fill-template ()
  19. "Test `org-capture-fill-template' specifications."
  20. ;; When working on these tests consider to also change
  21. ;; `test-org-feed/fill-template'.
  22. ;; %(sexp) placeholder.
  23. (should
  24. (equal "success!\n"
  25. (org-capture-fill-template "%(concat \"success\" \"!\")")))
  26. ;; It is possible to include other place holders in %(sexp). In
  27. ;; that case properly escape \ and " characters.
  28. (should
  29. (equal "Nested string \"\\\"\\\"\"\n"
  30. (let ((org-store-link-plist nil))
  31. (org-capture-fill-template "%(concat \"%i\")"
  32. "Nested string \"\\\"\\\"\""))))
  33. ;; %<...> placeholder.
  34. (should
  35. (equal (concat (format-time-string "%Y") "\n")
  36. (org-capture-fill-template "%<%Y>")))
  37. ;; %t and %T placeholders.
  38. (should
  39. (equal (concat (format-time-string (org-time-stamp-format nil nil)) "\n")
  40. (org-capture-fill-template "%t")))
  41. (should
  42. (equal (concat (format-time-string (org-time-stamp-format t nil)) "\n")
  43. (org-capture-fill-template "%T")))
  44. ;; %u and %U placeholders.
  45. (should
  46. (equal
  47. (concat (format-time-string (org-time-stamp-format nil t)) "\n")
  48. (org-capture-fill-template "%u")))
  49. (should
  50. (equal
  51. (concat (format-time-string (org-time-stamp-format t t)) "\n")
  52. (org-capture-fill-template "%U")))
  53. ;; %i placeholder. Make sure sexp placeholders are not expanded
  54. ;; when they are inserted through this one.
  55. (should
  56. (equal "success!\n"
  57. (let ((org-store-link-plist nil))
  58. (org-capture-fill-template "%i" "success!"))))
  59. (should
  60. (equal "%(concat \"no \" \"evaluation\")\n"
  61. (let ((org-store-link-plist nil))
  62. (org-capture-fill-template
  63. "%i" "%(concat \"no \" \"evaluation\")"))))
  64. ;; When %i contents span over multiple line, repeat initial leading
  65. ;; characters over each line.
  66. (should
  67. (equal "> line 1\n> line 2\n"
  68. (let ((org-store-link-plist nil))
  69. (org-capture-fill-template "> %i" "line 1\nline 2"))))
  70. ;; Test %-escaping with \ character.
  71. (should
  72. (equal "%i\n"
  73. (let ((org-store-link-plist nil))
  74. (org-capture-fill-template "\\%i" "success!"))))
  75. (should
  76. (equal "\\success!\n"
  77. (let ((org-store-link-plist nil))
  78. (org-capture-fill-template "\\\\%i" "success!"))))
  79. (should
  80. (equal "\\%i\n"
  81. (let ((org-store-link-plist nil))
  82. (org-capture-fill-template "\\\\\\%i" "success!"))))
  83. ;; More than one placeholder in the same template.
  84. (should
  85. (equal "success! success! success! success!\n"
  86. (let ((org-store-link-plist nil))
  87. (org-capture-fill-template "%i %i %i %i" "success!"))))
  88. ;; %(sexp) placeholder with an input containing the traps %, " and )
  89. ;; all at once which is complicated to parse.
  90. (should
  91. (equal "5 % Less (See Item \"3)\" Somewhere)\n"
  92. (let ((org-store-link-plist nil))
  93. (org-capture-fill-template
  94. "%(capitalize \"%i\")"
  95. "5 % less (see item \"3)\" somewhere)")))))
  96. (ert-deftest test-org-capture/refile ()
  97. "Test `org-capture-refile' specifications."
  98. ;; When refiling, make sure the headline being refiled is the one
  99. ;; being captured. In particular, empty lines after the entry may
  100. ;; be removed, and we don't want to shift onto the next heading.
  101. (should
  102. (string-prefix-p
  103. "** H1"
  104. (org-test-with-temp-text-in-file "* A\n* B\n"
  105. (let* ((file (buffer-file-name))
  106. (org-capture-templates
  107. `(("t" "Todo" entry (file+headline ,file "A") "** H1 %?"))))
  108. (org-capture nil "t")
  109. (insert "\n")
  110. (cl-letf (((symbol-function 'org-refile)
  111. (lambda ()
  112. (interactive)
  113. (throw :return
  114. (buffer-substring-no-properties
  115. (line-beginning-position)
  116. (line-end-position))))))
  117. (catch :return (org-capture-refile)))))))
  118. ;; When the entry is refiled, `:jump-to-captured' moves point to the
  119. ;; refile location, not the initial capture target.
  120. (should
  121. (org-test-with-temp-text-in-file "* Refile target"
  122. (let ((file1 (buffer-file-name)))
  123. (org-test-with-temp-text-in-file "* A"
  124. (let* ((file2 (buffer-file-name))
  125. (org-capture-templates
  126. `(("t" "Todo" entry (file+headline ,file2 "A")
  127. "** H1 %?" :jump-to-captured t))))
  128. (org-capture nil "t")
  129. (cl-letf (((symbol-function 'org-refile-get-location)
  130. (lambda (&rest args)
  131. (list (file-name-nondirectory file1) file1 nil nil))))
  132. (org-capture-refile)
  133. (list file1 file2 (buffer-file-name)))))))))
  134. (ert-deftest test-org-capture/abort ()
  135. "Test aborting a capture process."
  136. ;; Test that capture can be aborted after inserting at end of
  137. ;; capture buffer.
  138. (should
  139. (equal
  140. "* A\n* B\n"
  141. (org-test-with-temp-text-in-file "* A\n* B\n"
  142. (let* ((file (buffer-file-name))
  143. (org-capture-templates
  144. `(("t" "Todo" entry (file+headline ,file "A") "** H1 %?"))))
  145. (org-capture nil "t")
  146. (goto-char (point-max))
  147. (insert "Capture text")
  148. (org-capture-kill))
  149. (buffer-string))))
  150. (should
  151. (equal "- A\n - B"
  152. (org-test-with-temp-text-in-file "- A\n - B"
  153. (let* ((file (buffer-file-name))
  154. (org-capture-templates
  155. `(("t" "Item" item (file ,file) "- X"))))
  156. (org-capture nil "t")
  157. (org-capture-kill))
  158. (buffer-string))))
  159. (should
  160. (equal "| a |\n| b |"
  161. (org-test-with-temp-text-in-file "| a |\n| b |"
  162. (let* ((file (buffer-file-name))
  163. (org-capture-templates
  164. `(("t" "Table" table-line (file ,file) "| x |"))))
  165. (org-capture nil "t")
  166. (org-capture-kill))
  167. (buffer-string))))
  168. ;; Test aborting a capture that split the line.
  169. (should
  170. (equal
  171. "* AB\n"
  172. (org-test-with-temp-text-in-file "* AB\n"
  173. (let* ((file (buffer-file-name))
  174. (org-capture-templates
  175. `(("t" "Todo" entry
  176. (file+function ,file (lambda () (goto-char 4))) "** H1 %?"))))
  177. (org-capture nil "t")
  178. (org-capture-kill))
  179. (buffer-string)))))
  180. (ert-deftest test-org-caputre/entry ()
  181. "Test `entry' type in capture template."
  182. ;; Do not break next headline.
  183. (should
  184. (equal
  185. "* A\n** H1 Capture text\n* B\n"
  186. (org-test-with-temp-text-in-file "* A\n* B\n"
  187. (let* ((file (buffer-file-name))
  188. (org-capture-templates
  189. `(("t" "Todo" entry (file+headline ,file "A") "** H1 %?"))))
  190. (org-capture nil "t")
  191. (goto-char (point-max))
  192. (insert "Capture text")
  193. (org-capture-finalize))
  194. (buffer-string)))))
  195. (ert-deftest test-org-capture/item ()
  196. "Test `item' type in capture template."
  197. ;; Insert item in the first plain list found at the target location.
  198. (should
  199. (equal
  200. "* A\n- list 1\n- X\n\n\n1. list 2"
  201. (org-test-with-temp-text-in-file "* A\n- list 1\n\n\n1. list 2"
  202. (let* ((file (buffer-file-name))
  203. (org-capture-templates
  204. `(("t" "Item" item (file+headline ,file "A") "- X"))))
  205. (org-capture nil "t")
  206. (org-capture-finalize))
  207. (buffer-string))))
  208. (should
  209. (equal
  210. "Text\n- list 1\n- X\n\n\n1. list 2"
  211. (org-test-with-temp-text-in-file "Text\n- list 1\n\n\n1. list 2"
  212. (let* ((file (buffer-file-name))
  213. (org-capture-templates
  214. `(("t" "Item" item (file ,file) "- X"))))
  215. (org-capture nil "t")
  216. (org-capture-finalize))
  217. (buffer-string))))
  218. ;; When targeting a specific location, start looking for plain lists
  219. ;; from there.
  220. (should
  221. (equal
  222. "* A\n- skip\n\n\n1. here\n2. X\n"
  223. (org-test-with-temp-text-in-file "* A\n- skip\n\n\n1. here"
  224. (let* ((file (buffer-file-name))
  225. (org-capture-templates
  226. `(("t" "Item" item (file+regexp ,file "here") "1. X"))))
  227. (org-capture nil "t")
  228. (org-capture-finalize))
  229. (buffer-string))))
  230. ;; If there is no such list, create it.
  231. (should
  232. (equal
  233. "* A\n- X\n"
  234. (org-test-with-temp-text-in-file "* A"
  235. (let* ((file (buffer-file-name))
  236. (org-capture-templates
  237. `(("t" "Item" item (file+headline ,file "A") "- X"))))
  238. (org-capture nil "t")
  239. (org-capture-finalize))
  240. (buffer-string))))
  241. ;; When `:prepend' is non-nil, insert new item as the first item.
  242. (should
  243. (equal
  244. "* A\n- X\n- 1\n- 2"
  245. (org-test-with-temp-text-in-file "* A\n- 1\n- 2"
  246. (let* ((file (buffer-file-name))
  247. (org-capture-templates
  248. `(("t" "Item" item (file+headline ,file "A") "- X"
  249. :prepend t))))
  250. (org-capture nil "t")
  251. (org-capture-finalize))
  252. (buffer-string))))
  253. ;; When `:prepend' is nil, insert new item as the last top-level
  254. ;; item.
  255. (should
  256. (equal
  257. "* A\n- 1\n - 2\n- X\n"
  258. (org-test-with-temp-text-in-file "* A\n- 1\n - 2"
  259. (let* ((file (buffer-file-name))
  260. (org-capture-templates
  261. `(("t" "Item" item (file+headline ,file "A") "- X"))))
  262. (org-capture nil "t")
  263. (org-capture-finalize))
  264. (buffer-string))))
  265. ;; When targeting a specific location, one can insert in a sub-list.
  266. (should
  267. (equal
  268. "* A\n- skip\n - here\n - X\n- skip"
  269. (org-test-with-temp-text-in-file "* A\n- skip\n - here\n- skip"
  270. (let* ((file (buffer-file-name))
  271. (org-capture-templates
  272. `(("t" "Item" item (file+regexp ,file "here") "- X"))))
  273. (org-capture nil "t")
  274. (org-capture-finalize))
  275. (buffer-string))))
  276. ;; Obey `:empty-lines' when creating a new list.
  277. (should
  278. (equal
  279. "\n- X\n\n\n* H"
  280. (org-test-with-temp-text-in-file "\n* H"
  281. (let* ((file (buffer-file-name))
  282. (org-capture-templates
  283. `(("t" "Item" item (file ,file) "- X"
  284. :empty-lines-before 1 :empty-lines-after 2 :prepend t))))
  285. (org-capture nil "t")
  286. (org-capture-finalize))
  287. (buffer-string))))
  288. ;; Obey `:empty-lines' in an existing list only between items, and
  289. ;; only if the value doesn't break the list.
  290. (should
  291. (equal
  292. "- A\n\n- X\nText"
  293. (org-test-with-temp-text-in-file "- A\nText"
  294. (let* ((file (buffer-file-name))
  295. (org-capture-templates
  296. `(("t" "Item" item (file ,file) "- X" :empty-lines 1))))
  297. (org-capture nil "t")
  298. (org-capture-finalize))
  299. (buffer-string))))
  300. (should
  301. (equal
  302. "Text\n- X\n\n- A"
  303. (org-test-with-temp-text-in-file "Text\n- A"
  304. (let* ((file (buffer-file-name))
  305. (org-capture-templates
  306. `(("t" "Item" item (file ,file) "- X"
  307. :prepend t :empty-lines 1))))
  308. (org-capture nil "t")
  309. (org-capture-finalize))
  310. (buffer-string))))
  311. (should-not
  312. (equal
  313. "- A\n\n\n- X"
  314. (org-test-with-temp-text-in-file "- A"
  315. (let* ((file (buffer-file-name))
  316. (org-capture-templates
  317. `(("t" "Item" item (file ,file) "- X" :empty-lines 2))))
  318. (org-capture nil "t")
  319. (org-capture-finalize))
  320. (buffer-string))))
  321. ;; Preserve list type when pre-pending.
  322. (should
  323. (equal
  324. "1. X\n2. A"
  325. (org-test-with-temp-text-in-file "1. A"
  326. (let* ((file (buffer-file-name))
  327. (org-capture-templates
  328. `(("t" "Item" item (file ,file) "- X" :prepend t))))
  329. (org-capture nil "t")
  330. (org-capture-finalize))
  331. (buffer-string))))
  332. ;; Handle indentation. Handle multi-lines templates.
  333. (should
  334. (equal
  335. " - A\n - X\n"
  336. (org-test-with-temp-text-in-file " - A"
  337. (let* ((file (buffer-file-name))
  338. (org-capture-templates
  339. `(("t" "Item" item (file ,file) "- X"))))
  340. (org-capture nil "t")
  341. (org-capture-finalize))
  342. (buffer-string))))
  343. (should
  344. (equal
  345. " - A\n - X\n Line 2\n"
  346. (org-test-with-temp-text-in-file " - A"
  347. (let* ((file (buffer-file-name))
  348. (org-capture-templates
  349. `(("t" "Item" item (file ,file) "- X\n Line 2"))))
  350. (org-capture nil "t")
  351. (org-capture-finalize))
  352. (buffer-string))))
  353. ;; Handle incomplete templates.
  354. (should
  355. (equal
  356. "- A\n- X\n"
  357. (org-test-with-temp-text-in-file "- A"
  358. (let* ((file (buffer-file-name))
  359. (org-capture-templates
  360. `(("t" "Item" item (file ,file) "X"))))
  361. (org-capture nil "t")
  362. (org-capture-finalize))
  363. (buffer-string))))
  364. ;; Do not break next headline.
  365. (should-not
  366. (equal
  367. "- A\n- X\nFoo* H"
  368. (org-test-with-temp-text-in-file "- A\n* H"
  369. (let* ((file (buffer-file-name))
  370. (org-capture-templates
  371. `(("t" "Item" item (file ,file) "- X"))))
  372. (org-capture nil "t")
  373. (goto-char (point-max))
  374. (insert "Foo")
  375. (org-capture-finalize))
  376. (buffer-string)))))
  377. (ert-deftest test-org-capture/table-line ()
  378. "Test `table-line' type in capture template."
  379. ;; When a only file is specified, use the first table available.
  380. (should
  381. (equal "Text
  382. | a |
  383. | x |
  384. | b |"
  385. (org-test-with-temp-text-in-file "Text\n\n| a |\n\n| b |"
  386. (let* ((file (buffer-file-name))
  387. (org-capture-templates
  388. `(("t" "Table" table-line (file ,file) "| x |"
  389. :immediate-finish t))))
  390. (org-capture nil "t"))
  391. (buffer-string))))
  392. ;; When an entry is specified, find the first table in the
  393. ;; corresponding section.
  394. (should
  395. (equal "* Foo
  396. | a |
  397. * Inbox
  398. | b |
  399. | x |
  400. "
  401. (org-test-with-temp-text-in-file "* Foo\n| a |\n* Inbox\n| b |\n"
  402. (let* ((file (buffer-file-name))
  403. (org-capture-templates
  404. `(("t" "Table" table-line (file+headline ,file "Inbox")
  405. "| x |" :immediate-finish t))))
  406. (org-capture nil "t"))
  407. (buffer-string))))
  408. (should
  409. (equal "* Inbox
  410. | a |
  411. | x |
  412. | b |
  413. "
  414. (org-test-with-temp-text-in-file "* Inbox\n| a |\n\n| b |\n"
  415. (let* ((file (buffer-file-name))
  416. (org-capture-templates
  417. `(("t" "Table" table-line (file+headline ,file "Inbox")
  418. "| x |" :immediate-finish t))))
  419. (org-capture nil "t"))
  420. (buffer-string))))
  421. ;; When a precise location is specified, find the first table after
  422. ;; point, down to the end of the section.
  423. (should
  424. (equal "| a |
  425. | b |
  426. | x |
  427. "
  428. (org-test-with-temp-text-in-file "| a |\n\n\n| b |\n"
  429. (let* ((file (buffer-file-name))
  430. (org-capture-templates
  431. `(("t" "Table" table-line (file+function ,file forward-line)
  432. "| x |" :immediate-finish t))))
  433. (org-capture nil "t"))
  434. (buffer-string))))
  435. ;; Create a new table with an empty header when none can be found.
  436. (should
  437. (equal "| | |\n|---+---|\n| a | b |\n"
  438. (org-test-with-temp-text-in-file ""
  439. (let* ((file (buffer-file-name))
  440. (org-capture-templates
  441. `(("t" "Table" table-line (file ,file) "| a | b |"
  442. :immediate-finish t))))
  443. (org-capture nil "t"))
  444. (buffer-string))))
  445. ;; Properly insert row with formulas.
  446. (should
  447. (equal "| 1 |\n| 2 |\n#+TBLFM: "
  448. (org-test-with-temp-text-in-file "| 1 |\n#+TBLFM: "
  449. (let* ((file (buffer-file-name))
  450. (org-capture-templates
  451. `(("t" "Table" table-line (file ,file)
  452. "| 2 |" :immediate-finish t))))
  453. (org-capture nil "t"))
  454. (buffer-string))))
  455. ;; When `:prepend' is nil, add the row at the end of the table.
  456. (should
  457. (equal "| a |\n| x |\n"
  458. (org-test-with-temp-text-in-file "| a |"
  459. (let* ((file (buffer-file-name))
  460. (org-capture-templates
  461. `(("t" "Table" table-line (file ,file)
  462. "| x |" :immediate-finish t))))
  463. (org-capture nil "t"))
  464. (buffer-string))))
  465. ;; When `:prepend' is non-nil, add it as the first row after the
  466. ;; header, if there is one, or the first row otherwise.
  467. (should
  468. (equal "| a |\n|---|\n| x |\n| b |"
  469. (org-test-with-temp-text-in-file "| a |\n|---|\n| b |"
  470. (let* ((file (buffer-file-name))
  471. (org-capture-templates
  472. `(("t" "Table" table-line (file ,file)
  473. "| x |" :immediate-finish t :prepend t))))
  474. (org-capture nil "t"))
  475. (buffer-string))))
  476. (should
  477. (equal "| x |\n| a |"
  478. (org-test-with-temp-text-in-file "| a |"
  479. (let* ((file (buffer-file-name))
  480. (org-capture-templates
  481. `(("t" "Table" table-line (file ,file)
  482. "| x |" :immediate-finish t :prepend t))))
  483. (org-capture nil "t"))
  484. (buffer-string))))
  485. ;; When `:table-line-pos' is set and is meaningful, obey it.
  486. (should
  487. (equal "| a |\n|---|\n| b |\n| x |\n|---|\n| c |"
  488. (org-test-with-temp-text-in-file "| a |\n|---|\n| b |\n|---|\n| c |"
  489. (let* ((file (buffer-file-name))
  490. (org-capture-templates
  491. `(("t" "Table" table-line (file ,file)
  492. "| x |" :immediate-finish t :table-line-pos "II-1"))))
  493. (org-capture nil "t"))
  494. (buffer-string))))
  495. (should
  496. (equal "| a |\n|---|\n| x |\n| b |\n|---|\n| c |"
  497. (org-test-with-temp-text-in-file "| a |\n|---|\n| b |\n|---|\n| c |"
  498. (let* ((file (buffer-file-name))
  499. (org-capture-templates
  500. `(("t" "Table" table-line (file ,file)
  501. "| x |" :immediate-finish t :table-line-pos "I+1"))))
  502. (org-capture nil "t"))
  503. (buffer-string))))
  504. ;; Throw an error on invalid `:table-line-pos' specifications.
  505. (should-error
  506. (org-test-with-temp-text-in-file "| a |"
  507. (let* ((file (buffer-file-name))
  508. (org-capture-templates
  509. `(("t" "Table" table-line (file ,file)
  510. "| x |" :immediate-finish t :table-line-pos "II+99"))))
  511. (org-capture nil "t")
  512. t)))
  513. ;; Update formula when capturing one or more rows.
  514. (should
  515. (equal
  516. '(("@3$1" . "9"))
  517. (org-test-with-temp-text-in-file "| 1 |\n|---|\n| 9 |\n#+tblfm: @2$1=9"
  518. (let* ((file (buffer-file-name))
  519. (org-capture-templates
  520. `(("t" "Table" table-line (file ,file)
  521. "| 2 |" :immediate-finish t :table-line-pos "I-1"))))
  522. (org-capture nil "t")
  523. (org-table-get-stored-formulas)))))
  524. (should
  525. (equal
  526. '(("@4$1" . "9"))
  527. (org-test-with-temp-text-in-file "| 1 |\n|---|\n| 9 |\n#+tblfm: @2$1=9"
  528. (let* ((file (buffer-file-name))
  529. (org-capture-templates
  530. `(("t" "Table" table-line (file ,file)
  531. "| 2 |\n| 3 |" :immediate-finish t :table-line-pos "I-1"))))
  532. (org-capture nil "t")
  533. (org-table-get-stored-formulas)))))
  534. ;; Do not update formula when cell in inserted below affected row.
  535. (should-not
  536. (equal
  537. '(("@3$1" . "9"))
  538. (org-test-with-temp-text-in-file "| 1 |\n|---|\n| 9 |\n#+tblfm: @2$1=9"
  539. (let* ((file (buffer-file-name))
  540. (org-capture-templates
  541. `(("t" "Table" table-line (file ,file)
  542. "| 2 |" :immediate-finish t))))
  543. (org-capture nil "t")
  544. (org-table-get-stored-formulas))))))
  545. (ert-deftest test-org-capture/plain ()
  546. "Test `plain' type in capture template."
  547. ;; Insert at end of the file, unless `:prepend' is non-nil.
  548. (should
  549. (equal "Some text.\nFoo\n"
  550. (org-test-with-temp-text-in-file "Some text."
  551. (let* ((file (buffer-file-name))
  552. (org-capture-templates
  553. `(("t" "Text" plain (file ,file) "Foo"
  554. :immediate-finish t))))
  555. (org-capture nil "t")
  556. (buffer-string)))))
  557. (should
  558. (equal "Foo\nSome text."
  559. (org-test-with-temp-text-in-file "Some text."
  560. (let* ((file (buffer-file-name))
  561. (org-capture-templates
  562. `(("t" "Text" plain (file ,file) "Foo"
  563. :immediate-finish t :prepend t))))
  564. (org-capture nil "t")
  565. (buffer-string)))))
  566. ;; When a headline is specified, add it at the beginning of the
  567. ;; entry, past any meta-data, or at its end, depending on
  568. ;; `:prepend'.
  569. (should
  570. (equal "* A\nSCHEDULED: <2012-03-29 Thu>\nSome text.\nFoo\n* B"
  571. (org-test-with-temp-text-in-file
  572. "* A\nSCHEDULED: <2012-03-29 Thu>\nSome text.\n* B"
  573. (let* ((file (buffer-file-name))
  574. (org-capture-templates
  575. `(("t" "Text" plain (file+headline ,file "A") "Foo"
  576. :immediate-finish t))))
  577. (org-capture nil "t")
  578. (buffer-string)))))
  579. (should
  580. (equal "* A\nSCHEDULED: <2012-03-29 Thu>\nFoo\nSome text.\n* B"
  581. (org-test-with-temp-text-in-file
  582. "* A\nSCHEDULED: <2012-03-29 Thu>\nSome text.\n* B"
  583. (let* ((file (buffer-file-name))
  584. (org-capture-templates
  585. `(("t" "Text" plain (file+headline ,file "A") "Foo"
  586. :immediate-finish t :prepend t))))
  587. (org-capture nil "t")
  588. (buffer-string)))))
  589. ;; At an exact position, in the middle of a line, make sure to
  590. ;; insert text on a line on its own.
  591. (should
  592. (equal "A\nX\nB"
  593. (org-test-with-temp-text-in-file "AB"
  594. (let* ((file (buffer-file-name))
  595. (org-capture-templates
  596. `(("t" "Text" plain (file+function ,file forward-char) "X"
  597. :immediate-finish t))))
  598. (org-capture nil "t")
  599. (buffer-string)))))
  600. ;; Pathological case: insert an empty template in an empty file.
  601. (should
  602. (equal ""
  603. (org-test-with-temp-text-in-file ""
  604. (let* ((file (buffer-file-name))
  605. (org-capture-templates
  606. `(("t" "Text" plain (file ,file) ""
  607. :immediate-finish t))))
  608. (org-capture nil "t")
  609. (buffer-string))))))
  610. (provide 'test-org-capture)
  611. ;;; test-org-capture.el ends here