test-org-capture.el 21 KB

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