test-org-capture.el 22 KB

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