test-org-capture.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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\n"
  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 |\n"
  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\n"
  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\n"
  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\n"
  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\n"
  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\n"
  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\n"
  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\n"
  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\n"
  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\n"
  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. "
  403. (org-test-with-temp-text-in-file "Text\n\n| a |\n\n| b |"
  404. (let* ((file (buffer-file-name))
  405. (org-capture-templates
  406. `(("t" "Table" table-line (file ,file) "| x |"
  407. :immediate-finish t))))
  408. (org-capture nil "t"))
  409. (buffer-string))))
  410. ;; When an entry is specified, find the first table in the
  411. ;; corresponding section.
  412. (should
  413. (equal "* Foo
  414. | a |
  415. * Inbox
  416. | b |
  417. | x |
  418. "
  419. (org-test-with-temp-text-in-file "* Foo\n| a |\n* Inbox\n| b |\n"
  420. (let* ((file (buffer-file-name))
  421. (org-capture-templates
  422. `(("t" "Table" table-line (file+headline ,file "Inbox")
  423. "| x |" :immediate-finish t))))
  424. (org-capture nil "t"))
  425. (buffer-string))))
  426. (should
  427. (equal "* Inbox
  428. | a |
  429. | x |
  430. | b |
  431. "
  432. (org-test-with-temp-text-in-file "* Inbox\n| a |\n\n| b |\n"
  433. (let* ((file (buffer-file-name))
  434. (org-capture-templates
  435. `(("t" "Table" table-line (file+headline ,file "Inbox")
  436. "| x |" :immediate-finish t))))
  437. (org-capture nil "t"))
  438. (buffer-string))))
  439. ;; When a precise location is specified, find the first table after
  440. ;; point, down to the end of the section.
  441. (should
  442. (equal "| a |
  443. | b |
  444. | x |
  445. "
  446. (org-test-with-temp-text-in-file "| a |\n\n\n| b |\n"
  447. (let* ((file (buffer-file-name))
  448. (org-capture-templates
  449. `(("t" "Table" table-line (file+function ,file forward-line)
  450. "| x |" :immediate-finish t))))
  451. (org-capture nil "t"))
  452. (buffer-string))))
  453. ;; Create a new table with an empty header when none can be found.
  454. (should
  455. (equal "| | |\n|---+---|\n| a | b |\n"
  456. (org-test-with-temp-text-in-file ""
  457. (let* ((file (buffer-file-name))
  458. (org-capture-templates
  459. `(("t" "Table" table-line (file ,file) "| a | b |"
  460. :immediate-finish t))))
  461. (org-capture nil "t"))
  462. (buffer-string))))
  463. ;; Properly insert row with formulas.
  464. (should
  465. (equal "| 1 |\n| 2 |\n#+TBLFM: \n"
  466. (org-test-with-temp-text-in-file "| 1 |\n#+TBLFM: "
  467. (let* ((file (buffer-file-name))
  468. (org-capture-templates
  469. `(("t" "Table" table-line (file ,file)
  470. "| 2 |" :immediate-finish t))))
  471. (org-capture nil "t"))
  472. (buffer-string))))
  473. ;; When `:prepend' is nil, add the row at the end of the table.
  474. (should
  475. (equal "| a |\n| x |\n"
  476. (org-test-with-temp-text-in-file "| a |"
  477. (let* ((file (buffer-file-name))
  478. (org-capture-templates
  479. `(("t" "Table" table-line (file ,file)
  480. "| x |" :immediate-finish t))))
  481. (org-capture nil "t"))
  482. (buffer-string))))
  483. ;; When `:prepend' is non-nil, add it as the first row after the
  484. ;; header, if there is one, or the first row otherwise.
  485. (should
  486. (equal "| a |\n|---|\n| x |\n| b |\n"
  487. (org-test-with-temp-text-in-file "| a |\n|---|\n| b |"
  488. (let* ((file (buffer-file-name))
  489. (org-capture-templates
  490. `(("t" "Table" table-line (file ,file)
  491. "| x |" :immediate-finish t :prepend t))))
  492. (org-capture nil "t"))
  493. (buffer-string))))
  494. (should
  495. (equal "| x |\n| a |\n"
  496. (org-test-with-temp-text-in-file "| a |"
  497. (let* ((file (buffer-file-name))
  498. (org-capture-templates
  499. `(("t" "Table" table-line (file ,file)
  500. "| x |" :immediate-finish t :prepend t))))
  501. (org-capture nil "t"))
  502. (buffer-string))))
  503. ;; When `:table-line-pos' is set and is meaningful, obey it.
  504. (should
  505. (equal "| a |\n|---|\n| b |\n| x |\n|---|\n| c |\n"
  506. (org-test-with-temp-text-in-file "| a |\n|---|\n| b |\n|---|\n| c |"
  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-1"))))
  511. (org-capture nil "t"))
  512. (buffer-string))))
  513. (should
  514. (equal "| a |\n|---|\n| x |\n| b |\n|---|\n| c |\n"
  515. (org-test-with-temp-text-in-file "| a |\n|---|\n| b |\n|---|\n| c |"
  516. (let* ((file (buffer-file-name))
  517. (org-capture-templates
  518. `(("t" "Table" table-line (file ,file)
  519. "| x |" :immediate-finish t :table-line-pos "I+1"))))
  520. (org-capture nil "t"))
  521. (buffer-string))))
  522. ;; Throw an error on invalid `:table-line-pos' specifications.
  523. (should-error
  524. (org-test-with-temp-text-in-file "| a |"
  525. (let* ((file (buffer-file-name))
  526. (org-capture-templates
  527. `(("t" "Table" table-line (file ,file)
  528. "| x |" :immediate-finish t :table-line-pos "II+99"))))
  529. (org-capture nil "t")
  530. t)))
  531. ;; Update formula when capturing one or more rows.
  532. (should
  533. (equal
  534. '(("@3$1" . "9"))
  535. (org-test-with-temp-text-in-file "| 1 |\n|---|\n| 9 |\n#+tblfm: @2$1=9"
  536. (let* ((file (buffer-file-name))
  537. (org-capture-templates
  538. `(("t" "Table" table-line (file ,file)
  539. "| 2 |" :immediate-finish t :table-line-pos "I-1"))))
  540. (org-capture nil "t")
  541. (org-table-get-stored-formulas)))))
  542. (should
  543. (equal
  544. '(("@4$1" . "9"))
  545. (org-test-with-temp-text-in-file "| 1 |\n|---|\n| 9 |\n#+tblfm: @2$1=9"
  546. (let* ((file (buffer-file-name))
  547. (org-capture-templates
  548. `(("t" "Table" table-line (file ,file)
  549. "| 2 |\n| 3 |" :immediate-finish t :table-line-pos "I-1"))))
  550. (org-capture nil "t")
  551. (org-table-get-stored-formulas)))))
  552. ;; Do not update formula when cell in inserted below affected row.
  553. (should-not
  554. (equal
  555. '(("@3$1" . "9"))
  556. (org-test-with-temp-text-in-file "| 1 |\n|---|\n| 9 |\n#+tblfm: @2$1=9"
  557. (let* ((file (buffer-file-name))
  558. (org-capture-templates
  559. `(("t" "Table" table-line (file ,file)
  560. "| 2 |" :immediate-finish t))))
  561. (org-capture nil "t")
  562. (org-table-get-stored-formulas))))))
  563. (ert-deftest test-org-capture/plain ()
  564. "Test `plain' type in capture template."
  565. ;; Insert at end of the file, unless `:prepend' is non-nil.
  566. (should
  567. (equal "Some text.\nFoo\n"
  568. (org-test-with-temp-text-in-file "Some text."
  569. (let* ((file (buffer-file-name))
  570. (org-capture-templates
  571. `(("t" "Text" plain (file ,file) "Foo"
  572. :immediate-finish t))))
  573. (org-capture nil "t")
  574. (buffer-string)))))
  575. (should
  576. (equal "Foo\nSome text.\n"
  577. (org-test-with-temp-text-in-file "Some text."
  578. (let* ((file (buffer-file-name))
  579. (org-capture-templates
  580. `(("t" "Text" plain (file ,file) "Foo"
  581. :immediate-finish t :prepend t))))
  582. (org-capture nil "t")
  583. (buffer-string)))))
  584. ;; When a headline is specified, add it at the beginning of the
  585. ;; entry, past any meta-data, or at its end, depending on
  586. ;; `:prepend'.
  587. (should
  588. (equal "* A\nSCHEDULED: <2012-03-29 Thu>\nSome text.\nFoo\n* B\n"
  589. (org-test-with-temp-text-in-file
  590. "* A\nSCHEDULED: <2012-03-29 Thu>\nSome text.\n* B"
  591. (let* ((file (buffer-file-name))
  592. (org-capture-templates
  593. `(("t" "Text" plain (file+headline ,file "A") "Foo"
  594. :immediate-finish t))))
  595. (org-capture nil "t")
  596. (buffer-string)))))
  597. (should
  598. (equal "* A\nSCHEDULED: <2012-03-29 Thu>\nFoo\nSome text.\n* B\n"
  599. (org-test-with-temp-text-in-file
  600. "* A\nSCHEDULED: <2012-03-29 Thu>\nSome text.\n* B"
  601. (let* ((file (buffer-file-name))
  602. (org-capture-templates
  603. `(("t" "Text" plain (file+headline ,file "A") "Foo"
  604. :immediate-finish t :prepend t))))
  605. (org-capture nil "t")
  606. (buffer-string)))))
  607. ;; At an exact position, in the middle of a line, make sure to
  608. ;; insert text on a line on its own.
  609. (should
  610. (equal "A\nX\nB\n"
  611. (org-test-with-temp-text-in-file "AB"
  612. (let* ((file (buffer-file-name))
  613. (org-capture-templates
  614. `(("t" "Text" plain (file+function ,file forward-char) "X"
  615. :immediate-finish t))))
  616. (org-capture nil "t")
  617. (buffer-string)))))
  618. ;; Pathological case: insert an empty template in an empty file.
  619. (should
  620. (equal ""
  621. (org-test-with-temp-text-in-file ""
  622. (let* ((file (buffer-file-name))
  623. (org-capture-templates
  624. `(("t" "Text" plain (file ,file) ""
  625. :immediate-finish t))))
  626. (org-capture nil "t")
  627. (buffer-string))))))
  628. (provide 'test-org-capture)
  629. ;;; test-org-capture.el ends here