test-org-capture.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. ;; Correctly save position of inserted entry.
  196. (should
  197. (equal
  198. "** H"
  199. (org-test-with-temp-text-in-file "* A"
  200. (let* ((file (buffer-file-name))
  201. (org-capture-templates
  202. `(("t" "Test" entry (file+headline ,file "A") "** H\nFoo"
  203. :immediate-finish t))))
  204. (org-capture nil "t")
  205. (org-capture '(16))
  206. (buffer-substring (point) (line-end-position)))))))
  207. (ert-deftest test-org-capture/item ()
  208. "Test `item' type in capture template."
  209. ;; Insert item in the first plain list found at the target location.
  210. (should
  211. (equal
  212. "* A\n- list 1\n- X\n\n\n1. list 2"
  213. (org-test-with-temp-text-in-file "* A\n- list 1\n\n\n1. list 2"
  214. (let* ((file (buffer-file-name))
  215. (org-capture-templates
  216. `(("t" "Item" item (file+headline ,file "A") "- X"))))
  217. (org-capture nil "t")
  218. (org-capture-finalize))
  219. (buffer-string))))
  220. (should
  221. (equal
  222. "Text\n- list 1\n- X\n\n\n1. list 2"
  223. (org-test-with-temp-text-in-file "Text\n- list 1\n\n\n1. list 2"
  224. (let* ((file (buffer-file-name))
  225. (org-capture-templates
  226. `(("t" "Item" item (file ,file) "- X"))))
  227. (org-capture nil "t")
  228. (org-capture-finalize))
  229. (buffer-string))))
  230. ;; When targeting a specific location, start looking for plain lists
  231. ;; from there.
  232. (should
  233. (equal
  234. "* A\n- skip\n\n\n1. here\n2. X\n"
  235. (org-test-with-temp-text-in-file "* A\n- skip\n\n\n1. here"
  236. (let* ((file (buffer-file-name))
  237. (org-capture-templates
  238. `(("t" "Item" item (file+regexp ,file "here") "1. X"))))
  239. (org-capture nil "t")
  240. (org-capture-finalize))
  241. (buffer-string))))
  242. ;; If there is no such list, create it.
  243. (should
  244. (equal
  245. "* A\n- X\n"
  246. (org-test-with-temp-text-in-file "* A"
  247. (let* ((file (buffer-file-name))
  248. (org-capture-templates
  249. `(("t" "Item" item (file+headline ,file "A") "- X"))))
  250. (org-capture nil "t")
  251. (org-capture-finalize))
  252. (buffer-string))))
  253. ;; When `:prepend' is non-nil, insert new item as the first item.
  254. (should
  255. (equal
  256. "* A\n- X\n- 1\n- 2"
  257. (org-test-with-temp-text-in-file "* A\n- 1\n- 2"
  258. (let* ((file (buffer-file-name))
  259. (org-capture-templates
  260. `(("t" "Item" item (file+headline ,file "A") "- X"
  261. :prepend t))))
  262. (org-capture nil "t")
  263. (org-capture-finalize))
  264. (buffer-string))))
  265. ;; When `:prepend' is nil, insert new item as the last top-level
  266. ;; item.
  267. (should
  268. (equal
  269. "* A\n- 1\n - 2\n- X\n"
  270. (org-test-with-temp-text-in-file "* A\n- 1\n - 2"
  271. (let* ((file (buffer-file-name))
  272. (org-capture-templates
  273. `(("t" "Item" item (file+headline ,file "A") "- X"))))
  274. (org-capture nil "t")
  275. (org-capture-finalize))
  276. (buffer-string))))
  277. ;; When targeting a specific location, one can insert in a sub-list.
  278. (should
  279. (equal
  280. "* A\n- skip\n - here\n - X\n- skip"
  281. (org-test-with-temp-text-in-file "* A\n- skip\n - here\n- skip"
  282. (let* ((file (buffer-file-name))
  283. (org-capture-templates
  284. `(("t" "Item" item (file+regexp ,file "here") "- X"))))
  285. (org-capture nil "t")
  286. (org-capture-finalize))
  287. (buffer-string))))
  288. ;; Obey `:empty-lines' when creating a new list.
  289. (should
  290. (equal
  291. "\n- X\n\n\n* H"
  292. (org-test-with-temp-text-in-file "\n* H"
  293. (let* ((file (buffer-file-name))
  294. (org-capture-templates
  295. `(("t" "Item" item (file ,file) "- X"
  296. :empty-lines-before 1 :empty-lines-after 2 :prepend t))))
  297. (org-capture nil "t")
  298. (org-capture-finalize))
  299. (buffer-string))))
  300. ;; Obey `:empty-lines' in an existing list only between items, and
  301. ;; only if the value doesn't break the list.
  302. (should
  303. (equal
  304. "- A\n\n- X\nText"
  305. (org-test-with-temp-text-in-file "- A\nText"
  306. (let* ((file (buffer-file-name))
  307. (org-capture-templates
  308. `(("t" "Item" item (file ,file) "- X" :empty-lines 1))))
  309. (org-capture nil "t")
  310. (org-capture-finalize))
  311. (buffer-string))))
  312. (should
  313. (equal
  314. "Text\n- X\n\n- A"
  315. (org-test-with-temp-text-in-file "Text\n- A"
  316. (let* ((file (buffer-file-name))
  317. (org-capture-templates
  318. `(("t" "Item" item (file ,file) "- X"
  319. :prepend t :empty-lines 1))))
  320. (org-capture nil "t")
  321. (org-capture-finalize))
  322. (buffer-string))))
  323. (should-not
  324. (equal
  325. "- A\n\n\n- X"
  326. (org-test-with-temp-text-in-file "- A"
  327. (let* ((file (buffer-file-name))
  328. (org-capture-templates
  329. `(("t" "Item" item (file ,file) "- X" :empty-lines 2))))
  330. (org-capture nil "t")
  331. (org-capture-finalize))
  332. (buffer-string))))
  333. ;; Preserve list type when pre-pending.
  334. (should
  335. (equal
  336. "1. X\n2. A"
  337. (org-test-with-temp-text-in-file "1. A"
  338. (let* ((file (buffer-file-name))
  339. (org-capture-templates
  340. `(("t" "Item" item (file ,file) "- X" :prepend t))))
  341. (org-capture nil "t")
  342. (org-capture-finalize))
  343. (buffer-string))))
  344. ;; Handle indentation. Handle multi-lines templates.
  345. (should
  346. (equal
  347. " - A\n - X\n"
  348. (org-test-with-temp-text-in-file " - A"
  349. (let* ((file (buffer-file-name))
  350. (org-capture-templates
  351. `(("t" "Item" item (file ,file) "- X"))))
  352. (org-capture nil "t")
  353. (org-capture-finalize))
  354. (buffer-string))))
  355. (should
  356. (equal
  357. " - A\n - X\n Line 2\n"
  358. (org-test-with-temp-text-in-file " - A"
  359. (let* ((file (buffer-file-name))
  360. (org-capture-templates
  361. `(("t" "Item" item (file ,file) "- X\n Line 2"))))
  362. (org-capture nil "t")
  363. (org-capture-finalize))
  364. (buffer-string))))
  365. ;; Handle incomplete templates.
  366. (should
  367. (equal
  368. "- A\n- X\n"
  369. (org-test-with-temp-text-in-file "- A"
  370. (let* ((file (buffer-file-name))
  371. (org-capture-templates
  372. `(("t" "Item" item (file ,file) "X"))))
  373. (org-capture nil "t")
  374. (org-capture-finalize))
  375. (buffer-string))))
  376. ;; Do not break next headline.
  377. (should-not
  378. (equal
  379. "- A\n- X\nFoo* H"
  380. (org-test-with-temp-text-in-file "- A\n* H"
  381. (let* ((file (buffer-file-name))
  382. (org-capture-templates
  383. `(("t" "Item" item (file ,file) "- X"))))
  384. (org-capture nil "t")
  385. (goto-char (point-max))
  386. (insert "Foo")
  387. (org-capture-finalize))
  388. (buffer-string)))))
  389. (ert-deftest test-org-capture/table-line ()
  390. "Test `table-line' type in capture template."
  391. ;; When a only file is specified, use the first table available.
  392. (should
  393. (equal "Text
  394. | a |
  395. | x |
  396. | b |"
  397. (org-test-with-temp-text-in-file "Text\n\n| a |\n\n| b |"
  398. (let* ((file (buffer-file-name))
  399. (org-capture-templates
  400. `(("t" "Table" table-line (file ,file) "| x |"
  401. :immediate-finish t))))
  402. (org-capture nil "t"))
  403. (buffer-string))))
  404. ;; When an entry is specified, find the first table in the
  405. ;; corresponding section.
  406. (should
  407. (equal "* Foo
  408. | a |
  409. * Inbox
  410. | b |
  411. | x |
  412. "
  413. (org-test-with-temp-text-in-file "* Foo\n| a |\n* Inbox\n| b |\n"
  414. (let* ((file (buffer-file-name))
  415. (org-capture-templates
  416. `(("t" "Table" table-line (file+headline ,file "Inbox")
  417. "| x |" :immediate-finish t))))
  418. (org-capture nil "t"))
  419. (buffer-string))))
  420. (should
  421. (equal "* Inbox
  422. | a |
  423. | x |
  424. | b |
  425. "
  426. (org-test-with-temp-text-in-file "* Inbox\n| a |\n\n| b |\n"
  427. (let* ((file (buffer-file-name))
  428. (org-capture-templates
  429. `(("t" "Table" table-line (file+headline ,file "Inbox")
  430. "| x |" :immediate-finish t))))
  431. (org-capture nil "t"))
  432. (buffer-string))))
  433. ;; When a precise location is specified, find the first table after
  434. ;; point, down to the end of the section.
  435. (should
  436. (equal "| a |
  437. | b |
  438. | x |
  439. "
  440. (org-test-with-temp-text-in-file "| a |\n\n\n| b |\n"
  441. (let* ((file (buffer-file-name))
  442. (org-capture-templates
  443. `(("t" "Table" table-line (file+function ,file forward-line)
  444. "| x |" :immediate-finish t))))
  445. (org-capture nil "t"))
  446. (buffer-string))))
  447. ;; Create a new table with an empty header when none can be found.
  448. (should
  449. (equal "| | |\n|---+---|\n| a | b |\n"
  450. (org-test-with-temp-text-in-file ""
  451. (let* ((file (buffer-file-name))
  452. (org-capture-templates
  453. `(("t" "Table" table-line (file ,file) "| a | b |"
  454. :immediate-finish t))))
  455. (org-capture nil "t"))
  456. (buffer-string))))
  457. ;; Properly insert row with formulas.
  458. (should
  459. (equal "| 1 |\n| 2 |\n#+TBLFM: "
  460. (org-test-with-temp-text-in-file "| 1 |\n#+TBLFM: "
  461. (let* ((file (buffer-file-name))
  462. (org-capture-templates
  463. `(("t" "Table" table-line (file ,file)
  464. "| 2 |" :immediate-finish t))))
  465. (org-capture nil "t"))
  466. (buffer-string))))
  467. ;; When `:prepend' is nil, add the row at the end of the table.
  468. (should
  469. (equal "| a |\n| x |\n"
  470. (org-test-with-temp-text-in-file "| a |"
  471. (let* ((file (buffer-file-name))
  472. (org-capture-templates
  473. `(("t" "Table" table-line (file ,file)
  474. "| x |" :immediate-finish t))))
  475. (org-capture nil "t"))
  476. (buffer-string))))
  477. ;; When `:prepend' is non-nil, add it as the first row after the
  478. ;; header, if there is one, or the first row otherwise.
  479. (should
  480. (equal "| a |\n|---|\n| x |\n| b |"
  481. (org-test-with-temp-text-in-file "| a |\n|---|\n| b |"
  482. (let* ((file (buffer-file-name))
  483. (org-capture-templates
  484. `(("t" "Table" table-line (file ,file)
  485. "| x |" :immediate-finish t :prepend t))))
  486. (org-capture nil "t"))
  487. (buffer-string))))
  488. (should
  489. (equal "| x |\n| a |"
  490. (org-test-with-temp-text-in-file "| a |"
  491. (let* ((file (buffer-file-name))
  492. (org-capture-templates
  493. `(("t" "Table" table-line (file ,file)
  494. "| x |" :immediate-finish t :prepend t))))
  495. (org-capture nil "t"))
  496. (buffer-string))))
  497. ;; When `:table-line-pos' is set and is meaningful, obey it.
  498. (should
  499. (equal "| a |\n|---|\n| b |\n| x |\n|---|\n| c |"
  500. (org-test-with-temp-text-in-file "| a |\n|---|\n| b |\n|---|\n| c |"
  501. (let* ((file (buffer-file-name))
  502. (org-capture-templates
  503. `(("t" "Table" table-line (file ,file)
  504. "| x |" :immediate-finish t :table-line-pos "II-1"))))
  505. (org-capture nil "t"))
  506. (buffer-string))))
  507. (should
  508. (equal "| a |\n|---|\n| x |\n| b |\n|---|\n| c |"
  509. (org-test-with-temp-text-in-file "| a |\n|---|\n| b |\n|---|\n| c |"
  510. (let* ((file (buffer-file-name))
  511. (org-capture-templates
  512. `(("t" "Table" table-line (file ,file)
  513. "| x |" :immediate-finish t :table-line-pos "I+1"))))
  514. (org-capture nil "t"))
  515. (buffer-string))))
  516. ;; Throw an error on invalid `:table-line-pos' specifications.
  517. (should-error
  518. (org-test-with-temp-text-in-file "| a |"
  519. (let* ((file (buffer-file-name))
  520. (org-capture-templates
  521. `(("t" "Table" table-line (file ,file)
  522. "| x |" :immediate-finish t :table-line-pos "II+99"))))
  523. (org-capture nil "t")
  524. t)))
  525. ;; Update formula when capturing one or more rows.
  526. (should
  527. (equal
  528. '(("@3$1" . "9"))
  529. (org-test-with-temp-text-in-file "| 1 |\n|---|\n| 9 |\n#+tblfm: @2$1=9"
  530. (let* ((file (buffer-file-name))
  531. (org-capture-templates
  532. `(("t" "Table" table-line (file ,file)
  533. "| 2 |" :immediate-finish t :table-line-pos "I-1"))))
  534. (org-capture nil "t")
  535. (org-table-get-stored-formulas)))))
  536. (should
  537. (equal
  538. '(("@4$1" . "9"))
  539. (org-test-with-temp-text-in-file "| 1 |\n|---|\n| 9 |\n#+tblfm: @2$1=9"
  540. (let* ((file (buffer-file-name))
  541. (org-capture-templates
  542. `(("t" "Table" table-line (file ,file)
  543. "| 2 |\n| 3 |" :immediate-finish t :table-line-pos "I-1"))))
  544. (org-capture nil "t")
  545. (org-table-get-stored-formulas)))))
  546. ;; Do not update formula when cell in inserted below affected row.
  547. (should-not
  548. (equal
  549. '(("@3$1" . "9"))
  550. (org-test-with-temp-text-in-file "| 1 |\n|---|\n| 9 |\n#+tblfm: @2$1=9"
  551. (let* ((file (buffer-file-name))
  552. (org-capture-templates
  553. `(("t" "Table" table-line (file ,file)
  554. "| 2 |" :immediate-finish t))))
  555. (org-capture nil "t")
  556. (org-table-get-stored-formulas))))))
  557. (ert-deftest test-org-capture/plain ()
  558. "Test `plain' type in capture template."
  559. ;; Insert at end of the file, unless `:prepend' is non-nil.
  560. (should
  561. (equal "Some text.\nFoo\n"
  562. (org-test-with-temp-text-in-file "Some text."
  563. (let* ((file (buffer-file-name))
  564. (org-capture-templates
  565. `(("t" "Text" plain (file ,file) "Foo"
  566. :immediate-finish t))))
  567. (org-capture nil "t")
  568. (buffer-string)))))
  569. (should
  570. (equal "Foo\nSome text."
  571. (org-test-with-temp-text-in-file "Some text."
  572. (let* ((file (buffer-file-name))
  573. (org-capture-templates
  574. `(("t" "Text" plain (file ,file) "Foo"
  575. :immediate-finish t :prepend t))))
  576. (org-capture nil "t")
  577. (buffer-string)))))
  578. ;; When a headline is specified, add it at the beginning of the
  579. ;; entry, past any meta-data, or at its end, depending on
  580. ;; `:prepend'.
  581. (should
  582. (equal "* A\nSCHEDULED: <2012-03-29 Thu>\nSome text.\nFoo\n* B"
  583. (org-test-with-temp-text-in-file
  584. "* A\nSCHEDULED: <2012-03-29 Thu>\nSome text.\n* B"
  585. (let* ((file (buffer-file-name))
  586. (org-capture-templates
  587. `(("t" "Text" plain (file+headline ,file "A") "Foo"
  588. :immediate-finish t))))
  589. (org-capture nil "t")
  590. (buffer-string)))))
  591. (should
  592. (equal "* A\nSCHEDULED: <2012-03-29 Thu>\nFoo\nSome text.\n* B"
  593. (org-test-with-temp-text-in-file
  594. "* A\nSCHEDULED: <2012-03-29 Thu>\nSome text.\n* B"
  595. (let* ((file (buffer-file-name))
  596. (org-capture-templates
  597. `(("t" "Text" plain (file+headline ,file "A") "Foo"
  598. :immediate-finish t :prepend t))))
  599. (org-capture nil "t")
  600. (buffer-string)))))
  601. ;; At an exact position, in the middle of a line, make sure to
  602. ;; insert text on a line on its own.
  603. (should
  604. (equal "A\nX\nB"
  605. (org-test-with-temp-text-in-file "AB"
  606. (let* ((file (buffer-file-name))
  607. (org-capture-templates
  608. `(("t" "Text" plain (file+function ,file forward-char) "X"
  609. :immediate-finish t))))
  610. (org-capture nil "t")
  611. (buffer-string)))))
  612. ;; Pathological case: insert an empty template in an empty file.
  613. (should
  614. (equal ""
  615. (org-test-with-temp-text-in-file ""
  616. (let* ((file (buffer-file-name))
  617. (org-capture-templates
  618. `(("t" "Text" plain (file ,file) ""
  619. :immediate-finish t))))
  620. (org-capture nil "t")
  621. (buffer-string))))))
  622. (provide 'test-org-capture)
  623. ;;; test-org-capture.el ends here