test-oc.el 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. ;;; test-oc.el --- Tests for Org Cite library -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (require 'oc)
  16. (require 'ox)
  17. ;; We need `org-test-with-parsed-data' macro.
  18. (require 'test-ox "../testing/lisp/test-ox.el")
  19. (ert-deftest test-org-cite/register-processor ()
  20. "Test `org-cite-register-processor'."
  21. ;; Default test.
  22. (should
  23. (let ((org-cite--processors nil))
  24. (org-cite-register-processor 'name)))
  25. ;; Handle duplicate processor.
  26. (should
  27. (let ((org-cite--processors nil))
  28. (org-cite-register-processor 'name)
  29. (org-cite-register-processor 'name)))
  30. ;; Invalid name type.
  31. (should-error (org-cite-register-processor "name"))
  32. ;; Unknown property.
  33. (should-error
  34. (let ((org-cite--processors nil))
  35. (org-cite-register-processor :foo 'bar))))
  36. (ert-deftest test-org-cite/unregister-processor ()
  37. "Test `org-cite-unregister-processor'."
  38. ;; Default test.
  39. (should-not
  40. (let ((org-cite--processors nil))
  41. (org-cite-register-processor 'name)
  42. (org-cite-unregister-processor 'name)
  43. org-cite--processors))
  44. ;; Error out with an unknown processor.
  45. (should-error
  46. (let ((org-cite--processors nil))
  47. (org-cite-unregister-processor 'name))))
  48. (ert-deftest test-org-cite/inside-footnote-p ()
  49. "Test `org-cite-inside-footnote-p'."
  50. ;; Regular tests.
  51. (should
  52. (org-test-with-parsed-data "[fn:1] <point>[cite:@key]"
  53. (org-cite-inside-footnote-p
  54. (org-element-map tree 'citation #'identity info t))))
  55. (should
  56. (org-test-with-parsed-data "[fn::<point>[cite:@key]]"
  57. (org-cite-inside-footnote-p
  58. (org-element-map tree 'citation #'identity info t))))
  59. (should-not
  60. (org-test-with-parsed-data "[cite:@key]"
  61. (org-cite-inside-footnote-p
  62. (org-element-map tree 'citation #'identity info t))))
  63. (should
  64. (org-test-with-parsed-data "[fn:1] Text.<point>[cite:@key]"
  65. (org-cite-inside-footnote-p
  66. (org-element-map tree 'citation #'identity info t))))
  67. (should
  68. (org-test-with-parsed-data "[fn:1] <point>[cite:@key]\n: fixed width"
  69. (org-cite-inside-footnote-p
  70. (org-element-map tree 'citation #'identity info t))))
  71. (should
  72. (org-test-with-parsed-data "[fn:1] <point>[cite:@key] "
  73. (org-cite-inside-footnote-p
  74. (org-element-map tree 'citation #'identity info t))))
  75. ;; Test optional argument.
  76. (should
  77. (org-test-with-parsed-data "[fn:1] <point>[cite:@key]"
  78. (org-cite-inside-footnote-p
  79. (org-element-map tree 'citation #'identity info t)
  80. t)))
  81. (should-not
  82. (org-test-with-parsed-data "[fn:1] <point>See [cite:@key]."
  83. (org-cite-inside-footnote-p
  84. (org-element-map tree 'citation #'identity info t)
  85. t)))
  86. (should
  87. (org-test-with-parsed-data "[fn::<point>[cite:@key]]"
  88. (org-cite-inside-footnote-p
  89. (org-element-map tree 'citation #'identity info t)
  90. t)))
  91. (should-not
  92. (org-test-with-parsed-data "[fn::<point>See [cite:@key].]"
  93. (org-cite-inside-footnote-p
  94. (org-element-map tree 'citation #'identity info t)
  95. t))))
  96. (ert-deftest test-org-cite/processor-has-capability-p ()
  97. "Test `org-cite-processor-has-capability-p'."
  98. ;; Unknown capability error.
  99. (should-error
  100. (let ((org-cite--processors nil))
  101. (org-cite-register-processor 'name :activate #'ignore)
  102. (org-cite-processor-has-capability-p 'name 'unknown)))
  103. ;; Test `activate' capability.
  104. (should
  105. (let ((org-cite--processors nil))
  106. (org-cite-register-processor 'name :activate #'ignore)
  107. (org-cite-processor-has-capability-p 'name 'activate)))
  108. (should-not
  109. (let ((org-cite--processors nil))
  110. (org-cite-register-processor 'name :follow #'ignore)
  111. (org-cite-processor-has-capability-p 'name 'activate)))
  112. ;; Test `export' capability.
  113. (should
  114. (let ((org-cite--processors nil))
  115. (org-cite-register-processor 'name
  116. :export-bibliography #'ignore
  117. :export-citation #'ignore)
  118. (org-cite-processor-has-capability-p 'name 'export)))
  119. (should
  120. (let ((org-cite--processors nil))
  121. (org-cite-register-processor 'name :export-citation #'ignore)
  122. (org-cite-processor-has-capability-p 'name 'export)))
  123. (should-not
  124. (let ((org-cite--processors nil))
  125. (org-cite-register-processor 'name :export-bibliography #'ignore)
  126. (org-cite-processor-has-capability-p 'name 'export)))
  127. ;; Test `follow' capability.
  128. (should
  129. (let ((org-cite--processors nil))
  130. (org-cite-register-processor 'name :follow #'ignore)
  131. (org-cite-processor-has-capability-p 'name 'follow)))
  132. (should-not
  133. (let ((org-cite--processors nil))
  134. (org-cite-register-processor 'name :activate #'ignore)
  135. (org-cite-processor-has-capability-p 'name 'follow)))
  136. ;; Unknown processors have no capabilities.
  137. (should-not (org-cite-processor-has-capability-p 'foo 'activate))
  138. (should-not (org-cite-processor-has-capability-p 'foo 'export))
  139. (should-not (org-cite-processor-has-capability-p 'foo 'follow)))
  140. (ert-deftest test-org-cite/get-references ()
  141. "Test `org-cite-get-references'."
  142. ;; Return a list of citation reference objects.
  143. (should
  144. (equal '(citation-reference)
  145. (org-test-with-temp-text "[cite:@a]"
  146. (mapcar #'org-element-type
  147. (org-cite-get-references (org-element-context))))))
  148. (should
  149. (equal '("a")
  150. (org-test-with-temp-text "[cite:@a]"
  151. (mapcar (lambda (r) (org-element-property :key r))
  152. (org-cite-get-references (org-element-context))))))
  153. ;; Preserve order of references.
  154. (should
  155. (equal '("a" "b")
  156. (org-test-with-temp-text "[cite:@a;@b]"
  157. (mapcar (lambda (r) (org-element-property :key r))
  158. (org-cite-get-references (org-element-context))))))
  159. ;; Parse prefix and suffix.
  160. (should
  161. (equal '("a" "b")
  162. (org-test-with-temp-text "[cite:prefix @a suffix;@b]"
  163. (mapcar (lambda (r) (org-element-property :key r))
  164. (org-cite-get-references (org-element-context))))))
  165. (should
  166. (equal '(("prefix ") nil)
  167. (org-test-with-temp-text "[cite:prefix @a suffix;@b]"
  168. (mapcar (lambda (r) (org-element-property :prefix r))
  169. (org-cite-get-references (org-element-context))))))
  170. (should
  171. (equal '((" suffix") nil)
  172. (org-test-with-temp-text "[cite:prefix @a suffix;@b]"
  173. (mapcar (lambda (r) (org-element-property :suffix r))
  174. (org-cite-get-references (org-element-context))))))
  175. ;; Ignore common prefix and suffix.
  176. (should
  177. (equal '("a")
  178. (org-test-with-temp-text "[cite:common prefix; @a ; common suffix]"
  179. (mapcar (lambda (r) (org-element-property :key r))
  180. (org-cite-get-references (org-element-context))))))
  181. ;; Preserve buffer positions.
  182. (should
  183. (org-test-with-temp-text "[cite:@a] [cite<point>:@b]"
  184. (= (1+ (point))
  185. (org-element-property :begin
  186. (car (org-cite-get-references (org-element-context)))))))
  187. ;; Handle citation from a full parse tree.
  188. (should
  189. (equal '(1 2)
  190. (org-test-with-temp-text "[cite:@a] [cite:@a;@b]"
  191. (org-element-map (org-element-parse-buffer) 'citation
  192. (lambda (c) (length (org-cite-get-references c)))))))
  193. ;; Test optional argument.
  194. (should
  195. (equal '("a" "b")
  196. (org-test-with-temp-text "[cite:@a;@b]"
  197. (org-cite-get-references (org-element-context) t))))
  198. (should
  199. (equal '("a" "b")
  200. (org-test-with-temp-text "[cite:@a;@b]"
  201. (org-element-map (org-element-parse-buffer) 'citation
  202. (lambda (c) (org-cite-get-references c t)) nil t)))))
  203. (ert-deftest test-org-cite/key-boundaries ()
  204. "Test `org-cite-key-boundaries'."
  205. (should
  206. (equal "@key"
  207. (org-test-with-temp-text "[cite:<point>@key]"
  208. (let ((boundaries (org-cite-key-boundaries (org-element-context))))
  209. (buffer-substring-no-properties
  210. (car boundaries)
  211. (cdr boundaries))))))
  212. (should
  213. (equal "@key"
  214. (org-test-with-temp-text "[cite:<point>prefix @key]"
  215. (let ((boundaries (org-cite-key-boundaries (org-element-context))))
  216. (buffer-substring-no-properties
  217. (car boundaries)
  218. (cdr boundaries))))))
  219. (should
  220. (equal "@key"
  221. (org-test-with-temp-text "[cite:<point>@key suffix]"
  222. (let ((boundaries (org-cite-key-boundaries (org-element-context))))
  223. (buffer-substring-no-properties
  224. (car boundaries)
  225. (cdr boundaries))))))
  226. (should
  227. (equal "@key"
  228. (org-test-with-temp-text "[cite:global ;<point>@key]"
  229. (let ((boundaries (org-cite-key-boundaries (org-element-context))))
  230. (buffer-substring-no-properties
  231. (car boundaries)
  232. (cdr boundaries))))))
  233. (should
  234. (equal "@key"
  235. (org-test-with-temp-text "[cite:<point>@key; global]"
  236. (let ((boundaries (org-cite-key-boundaries (org-element-context))))
  237. (buffer-substring-no-properties
  238. (car boundaries)
  239. (cdr boundaries)))))))
  240. (ert-deftest test-org-cite/supported-styles ()
  241. "Test `org-cite-supported-styles'."
  242. ;; Default behavior is to use export processors.
  243. (should
  244. (equal '((("")))
  245. (let ((org-cite--processors nil)
  246. (org-cite-export-processors '((t test))))
  247. (org-cite-register-processor 'test :cite-styles '(((""))))
  248. (org-cite-supported-styles))))
  249. (should
  250. (equal '((("foo" "f")) (("")))
  251. (let ((org-cite--processors nil)
  252. (org-cite-export-processors '((t test))))
  253. (org-cite-register-processor 'test
  254. :cite-styles '((("foo" "f")) ((""))))
  255. (org-cite-supported-styles))))
  256. ;; Explicitly provide a processor.
  257. (should
  258. (equal '((("")))
  259. (let ((org-cite--processors nil))
  260. (org-cite-register-processor 'test :cite-styles '(((""))))
  261. (org-cite-supported-styles '(test)))))
  262. ;; Merge style shortcuts.
  263. (should
  264. (equal '((("foo" "f" "g")))
  265. (let ((org-cite--processors nil))
  266. (org-cite-register-processor 'test :cite-styles '((("foo" "f"))))
  267. (org-cite-register-processor 'test2 :cite-styles '((("foo" "g"))))
  268. (org-cite-supported-styles '(test test2)))))
  269. ;; Merge style variants.
  270. (should
  271. (equal '((("foo") ("bar") ("baz")))
  272. (let ((org-cite--processors nil))
  273. (org-cite-register-processor 'test
  274. :cite-styles '((("foo") ("bar"))))
  275. (org-cite-register-processor 'test2
  276. :cite-styles '((("foo") ("baz"))))
  277. (org-cite-supported-styles '(test test2)))))
  278. ;; Merge variant shortcuts.
  279. (should
  280. (equal '((("foo") ("bar" "b" "c")))
  281. (let ((org-cite--processors nil))
  282. (org-cite-register-processor 'test
  283. :cite-styles '((("foo") ("bar" "b"))))
  284. (org-cite-register-processor 'test2
  285. :cite-styles '((("foo") ("bar" "c"))))
  286. (org-cite-supported-styles '(test test2)))))
  287. ;; Ignore duplicates.
  288. (should
  289. (equal '((("foo") ("bar")))
  290. (let ((org-cite--processors nil))
  291. (org-cite-register-processor 'test
  292. :cite-styles '((("foo") ("bar"))))
  293. (org-cite-register-processor 'test2
  294. :cite-styles '((("foo") ("bar"))))
  295. (org-cite-supported-styles '(test test2)))))
  296. (should
  297. (equal '((("foo") ("bar" "b")))
  298. (let ((org-cite--processors nil))
  299. (org-cite-register-processor 'test
  300. :cite-styles '((("foo") ("bar" "b"))))
  301. (org-cite-register-processor 'test2
  302. :cite-styles '((("foo") ("bar" "b"))))
  303. (org-cite-supported-styles '(test test2))))))
  304. (ert-deftest test-org-cite/delete-citation ()
  305. "Test `org-cite-delete-citation'."
  306. ;; Error when not on a citation or citation reference.
  307. (should-error
  308. (org-test-with-temp-text "Text"
  309. (org-cite-delete-citation (org-element-context))))
  310. ;; When argument is a citation, delete it completely. Manage
  311. ;; properly blanks around it.
  312. (should
  313. (equal ""
  314. (org-test-with-temp-text "[cite:@key]"
  315. (org-cite-delete-citation (org-element-context))
  316. (buffer-string))))
  317. (should
  318. (equal "Before After"
  319. (org-test-with-temp-text "Before [<point>cite:@key] After"
  320. (org-cite-delete-citation (org-element-context))
  321. (buffer-string))))
  322. (should
  323. (equal "Before After"
  324. (org-test-with-temp-text "Before [<point>cite:@key]After"
  325. (org-cite-delete-citation (org-element-context))
  326. (buffer-string))))
  327. (should
  328. (equal "Before After"
  329. (org-test-with-temp-text "Before[<point>cite:@key] After"
  330. (org-cite-delete-citation (org-element-context))
  331. (buffer-string))))
  332. ;; Ensure there is at least a blank to separate consecutive objects.
  333. (should
  334. (equal "Before After"
  335. (org-test-with-temp-text "Before[<point>cite:@key]After"
  336. (org-cite-delete-citation (org-element-context))
  337. (buffer-string))))
  338. ;; Remove trailing blanks when removing the citation.
  339. (should
  340. (equal "Before"
  341. (org-test-with-temp-text "Before[<point>cite:@key] "
  342. (org-cite-delete-citation (org-element-context))
  343. (buffer-string))))
  344. ;; Preserve indentation if citation is at the beginning of the line.
  345. (should
  346. (equal " After"
  347. (org-test-with-temp-text " [<point>cite:@key] After"
  348. (org-cite-delete-citation (org-element-context))
  349. (buffer-string))))
  350. ;; When the citation is alone on a line, remove the whole line.
  351. (should
  352. (equal "Line 1\nLine 3"
  353. (org-test-with-temp-text "Line 1\n[<point>cite:@key]\nLine 3"
  354. (org-cite-delete-citation (org-element-context))
  355. (buffer-string))))
  356. ;; When there is only one citation reference object, remove the full
  357. ;; citation.
  358. (should
  359. (equal ""
  360. (org-test-with-temp-text "[cite:@<point>key]"
  361. (org-cite-delete-citation (org-element-context))
  362. (buffer-string))))
  363. (should
  364. (equal ""
  365. (org-test-with-temp-text "[cite:pre @<point>key post]"
  366. (org-cite-delete-citation (org-element-context))
  367. (buffer-string))))
  368. (should
  369. (equal ""
  370. (org-test-with-temp-text "[cite:pre; @<point>key ;post]"
  371. (org-cite-delete-citation (org-element-context))
  372. (buffer-string))))
  373. ;; Otherwise, remove the reference, including any affix.
  374. (should
  375. (equal "[cite:@before;@after]"
  376. (org-test-with-temp-text "[cite:@before;@<point>key;@after]"
  377. (org-cite-delete-citation (org-element-context))
  378. (buffer-string))))
  379. (should
  380. (equal "[cite:@before;@after]"
  381. (org-test-with-temp-text "[cite:@before;pre @<point>key post;@after]"
  382. (org-cite-delete-citation (org-element-context))
  383. (buffer-string))))
  384. (should
  385. (equal "[cite:@before]"
  386. (org-test-with-temp-text "[cite:@before;@<point>key]"
  387. (org-cite-delete-citation (org-element-context))
  388. (buffer-string))))
  389. (should
  390. (equal "[cite:@before;post]"
  391. (org-test-with-temp-text "[cite:@before;@<point>key;post]"
  392. (org-cite-delete-citation (org-element-context))
  393. (buffer-string))))
  394. (should
  395. (equal "[cite:@after]"
  396. (org-test-with-temp-text "[cite:@<point>key;@after]"
  397. (org-cite-delete-citation (org-element-context))
  398. (buffer-string))))
  399. (should
  400. (equal "[cite:pre;@after]"
  401. (org-test-with-temp-text "[cite:pre;@<point>key;@after]"
  402. (org-cite-delete-citation (org-element-context))
  403. (buffer-string)))))
  404. (ert-deftest test-org-cite/list-bibliography-files ()
  405. "Test `org-cite-list-bibliography-files'."
  406. (should
  407. (equal '("/bibliography")
  408. (org-test-with-temp-text "#+bibliography: /bibliography"
  409. (let ((org-cite-global-bibliography nil))
  410. (org-cite-list-bibliography-files)))))
  411. (should
  412. (equal '("/bibliography")
  413. (org-test-with-temp-text "#+bibliography: \"/bibliography\""
  414. (let ((org-cite-global-bibliography nil))
  415. (org-cite-list-bibliography-files)))))
  416. (should
  417. (equal '("/bibliography" "/other-bibliography")
  418. (org-test-with-temp-text "#+bibliography: /bibliography"
  419. (let ((org-cite-global-bibliography '("/other-bibliography")))
  420. (org-cite-list-bibliography-files)))))
  421. (should
  422. (equal '(t)
  423. (org-test-with-temp-text "#+bibliography: ./bibliography"
  424. (let ((org-cite-global-bibliography nil))
  425. (mapcar #'file-name-absolute-p (org-cite-list-bibliography-files))))))
  426. (should
  427. (equal '("/bibliographyA" "/bibliographyB")
  428. (org-test-with-temp-text
  429. "#+bibliography: /bibliographyA\n#+bibliography: /bibliographyB"
  430. (let ((org-cite-global-bibliography nil))
  431. (org-cite-list-bibliography-files)))))
  432. (should
  433. (equal '("/bibliographyA")
  434. (org-test-with-temp-text
  435. "#+bibliography: /bibliographyA\n#+bibliography: /bibliographyA"
  436. (let ((org-cite-global-bibliography nil))
  437. (org-cite-list-bibliography-files))))))
  438. (ert-deftest test-org-cite/bibliography-style ()
  439. "Test `org-cite-bibliography-style'."
  440. ;; Extract style from global processor definition.
  441. (should
  442. (equal "a"
  443. (catch :exit
  444. (org-test-with-temp-text "#+print_bibliography:"
  445. (let ((org-cite-export-processors '((t . (foo "a" "b"))))
  446. (org-cite--processors nil))
  447. (org-cite-register-processor 'foo
  448. :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
  449. :export-citation #'ignore)
  450. (org-export-as (org-export-create-backend)))))))
  451. ;; Extract style from local processor definition.
  452. (should
  453. (equal "a"
  454. (catch :exit
  455. (org-test-with-temp-text
  456. "#+cite_export: foo a b\n#+print_bibliography:"
  457. (let ((org-cite-export-processors nil)
  458. (org-cite--processors nil))
  459. (org-cite-register-processor 'foo
  460. :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
  461. :export-citation #'ignore)
  462. (org-export-as (org-export-create-backend)))))))
  463. (should
  464. (equal "a b"
  465. (catch :exit
  466. (org-test-with-temp-text
  467. "#+cite_export: foo \"a b\" c\n#+print_bibliography:"
  468. (let ((org-cite-export-processors nil)
  469. (org-cite--processors nil))
  470. (org-cite-register-processor 'foo
  471. :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
  472. :export-citation #'ignore)
  473. (org-export-as (org-export-create-backend)))))))
  474. ;; Test priority: first keyword, then local.
  475. (should
  476. (equal "local"
  477. (catch :exit
  478. (org-test-with-temp-text
  479. "#+print_bibliography:\n#+cite_export: foo local a\n[cite:@a]"
  480. (let ((org-cite-export-processors '((t . (foo "global" "b"))))
  481. (org-cite--processors nil))
  482. (org-cite-register-processor 'foo
  483. :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
  484. :export-citation #'ignore)
  485. (org-export-as (org-export-create-backend)))))))
  486. ;; Explicit "nil" styles forces default style.
  487. (should-not
  488. (catch :exit
  489. (org-test-with-temp-text
  490. "#+print_bibliography:\n#+cite_export: foo nil a\n[cite:@a]"
  491. (let ((org-cite-export-processors '((t . (foo "global" "b"))))
  492. (org-cite--processors nil))
  493. (org-cite-register-processor 'foo
  494. :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
  495. :export-citation #'ignore)
  496. (org-export-as (org-export-create-backend)))))))
  497. (ert-deftest test-org-cite/bibliography-properties ()
  498. "Test `org-cite-bibliography-properties'."
  499. ;; Return nil without properties.
  500. (should-not
  501. (org-test-with-parsed-data "#+print_bibliography:"
  502. (org-element-map tree 'keyword
  503. #'org-cite-bibliography-properties info t)))
  504. ;; Regular tests.
  505. (should
  506. (equal
  507. '(:key "value")
  508. (org-test-with-parsed-data "#+print_bibliography: :key value"
  509. (org-element-map tree 'keyword
  510. #'org-cite-bibliography-properties info t))))
  511. (should
  512. (equal
  513. '(:key "value" :key2 "value2")
  514. (org-test-with-parsed-data "#+print_bibliography: :key value :key2 value2"
  515. (org-element-map tree 'keyword
  516. #'org-cite-bibliography-properties info t))))
  517. ;; Allow empty values.
  518. (should
  519. (equal
  520. '(:key)
  521. (org-test-with-parsed-data "#+print_bibliography: :key"
  522. (org-element-map tree 'keyword
  523. #'org-cite-bibliography-properties info t))))
  524. (should
  525. (equal
  526. '(:key "")
  527. (org-test-with-parsed-data "#+print_bibliography: :key \"\""
  528. (org-element-map tree 'keyword
  529. #'org-cite-bibliography-properties info t))))
  530. ;; Allow space with double quotes.
  531. (should
  532. (equal
  533. '(:key "space space")
  534. (org-test-with-parsed-data "#+print_bibliography: :key \"space space\""
  535. (org-element-map tree 'keyword
  536. #'org-cite-bibliography-properties info t))))
  537. ;; Ignore spurious values.
  538. (should
  539. (equal
  540. '(:key "space")
  541. (org-test-with-parsed-data "#+print_bibliography: :key space space"
  542. (org-element-map tree 'keyword
  543. #'org-cite-bibliography-properties info t))))
  544. ;; Gracefully handle incomplete quotations.
  545. (should
  546. (equal
  547. '(:key "\"space" :key2 "value2")
  548. (org-test-with-parsed-data "#+print_bibliography: :key \"space :key2 value2"
  549. (org-element-map tree 'keyword
  550. #'org-cite-bibliography-properties info t)))))
  551. (ert-deftest test-org-cite/citation-style ()
  552. "Test `org-cite-citation-style'."
  553. ;; Extract style from global processor definition.
  554. (should
  555. (equal '("b")
  556. (catch :exit
  557. (org-test-with-temp-text "[cite:@a]"
  558. (let ((org-cite-export-processors '((t . (foo "a" "b"))))
  559. (org-cite--processors nil))
  560. (org-cite-register-processor 'foo
  561. :export-citation (lambda (_ s _ _) (throw :exit s)))
  562. (org-export-as (org-export-create-backend)))))))
  563. (should
  564. (equal '("b" . "variant")
  565. (catch :exit
  566. (org-test-with-temp-text "[cite:@a]"
  567. (let ((org-cite-export-processors '((t . (foo "a" "b/variant"))))
  568. (org-cite--processors nil))
  569. (org-cite-register-processor 'foo
  570. :export-citation (lambda (_ s _ _) (throw :exit s)))
  571. (org-export-as (org-export-create-backend)))))))
  572. ;; Extract style from local processor definition.
  573. (should
  574. (equal '("b")
  575. (catch :exit
  576. (org-test-with-temp-text "#+cite_export: foo a b\n[cite:@a]"
  577. (let ((org-cite-export-processors nil)
  578. (org-cite--processors nil))
  579. (org-cite-register-processor 'foo
  580. :export-citation (lambda (_ s _ _) (throw :exit s)))
  581. (org-export-as (org-export-create-backend)))))))
  582. (should
  583. (equal '("b c")
  584. (catch :exit
  585. (org-test-with-temp-text "#+cite_export: foo a \"b c\"\n[cite:@a]"
  586. (let ((org-cite-export-processors nil)
  587. (org-cite--processors nil))
  588. (org-cite-register-processor 'foo
  589. :export-citation (lambda (_ s _ _) (throw :exit s)))
  590. (org-export-as (org-export-create-backend)))))))
  591. (should
  592. (equal '("b" . "variant")
  593. (catch :exit
  594. (org-test-with-temp-text "#+cite_export: foo a b/variant\n[cite:@a]"
  595. (let ((org-cite-export-processors nil)
  596. (org-cite--processors nil))
  597. (org-cite-register-processor 'foo
  598. :export-citation (lambda (_ s _ _) (throw :exit s)))
  599. (org-export-as (org-export-create-backend)))))))
  600. (should
  601. (equal '("b c" . "variant")
  602. (catch :exit
  603. (org-test-with-temp-text
  604. "#+cite_export: foo a \"b c/variant\"\n[cite:@a]"
  605. (let ((org-cite-export-processors nil)
  606. (org-cite--processors nil))
  607. (org-cite-register-processor 'foo
  608. :export-citation (lambda (_ s _ _) (throw :exit s)))
  609. (org-export-as (org-export-create-backend)))))))
  610. ;; Extract style from citation itself.
  611. (should
  612. (equal '("b")
  613. (catch :exit
  614. (org-test-with-temp-text "[cite/b:@a]"
  615. (let ((org-cite-export-processors '((t . (foo nil nil))))
  616. (org-cite--processors nil))
  617. (org-cite-register-processor 'foo
  618. :export-citation (lambda (_ s _ _) (throw :exit s)))
  619. (org-export-as (org-export-create-backend)))))))
  620. (should
  621. (equal '("b" . "variant")
  622. (catch :exit
  623. (org-test-with-temp-text "[cite/b/variant:@a]"
  624. (let ((org-cite-export-processors '((t . (foo nil nil))))
  625. (org-cite--processors nil))
  626. (org-cite-register-processor 'foo
  627. :export-citation (lambda (_ s _ _) (throw :exit s)))
  628. (org-export-as (org-export-create-backend)))))))
  629. ;; Test priority: first object, then local.
  630. (should
  631. (equal '("object")
  632. (catch :exit
  633. (org-test-with-temp-text
  634. "#+cite_export: foo nil local\n[cite/object:@a]"
  635. (let ((org-cite-export-processors '((t . (foo nil "global"))))
  636. (org-cite--processors nil))
  637. (org-cite-register-processor 'foo
  638. :export-citation (lambda (_ s _ _) (throw :exit s)))
  639. (org-export-as (org-export-create-backend)))))))
  640. (should
  641. (equal '("local")
  642. (catch :exit
  643. (org-test-with-temp-text
  644. "#+cite_export: foo nil local\n[cite:@a]"
  645. (let ((org-cite-export-processors '((t . (foo nil "global"))))
  646. (org-cite--processors nil))
  647. (org-cite-register-processor 'foo
  648. :export-citation (lambda (_ s _ _) (throw :exit s)))
  649. (org-export-as (org-export-create-backend)))))))
  650. ;; Force default style with "nil".
  651. (should
  652. (equal '(nil)
  653. (catch :exit
  654. (org-test-with-temp-text
  655. "#+cite_export: foo nil nil\n[cite:@a]"
  656. (let ((org-cite-export-processors '((t . (foo nil "global"))))
  657. (org-cite--processors nil))
  658. (org-cite-register-processor 'foo
  659. :export-citation (lambda (_ s _ _) (throw :exit s)))
  660. (org-export-as (org-export-create-backend)))))))
  661. (should
  662. (equal '(nil)
  663. (catch :exit
  664. (org-test-with-temp-text "[cite/nil:@a]"
  665. (let ((org-cite-export-processors '((t . (foo nil "global"))))
  666. (org-cite--processors nil))
  667. (org-cite-register-processor 'foo
  668. :export-citation (lambda (_ s _ _) (throw :exit s)))
  669. (org-export-as (org-export-create-backend)))))))
  670. ;; Test variant inheritance.
  671. (should
  672. (equal '("local" . "v2")
  673. (catch :exit
  674. (org-test-with-temp-text "[cite/local/v2:@a]"
  675. (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
  676. (org-cite--processors nil))
  677. (org-cite-register-processor 'foo
  678. :export-citation (lambda (_ s _ _) (throw :exit s)))
  679. (org-export-as (org-export-create-backend)))))))
  680. (should
  681. (equal '("global" . "v2")
  682. (catch :exit
  683. (org-test-with-temp-text "[cite//v2:@a]"
  684. (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
  685. (org-cite--processors nil))
  686. (org-cite-register-processor 'foo
  687. :export-citation (lambda (_ s _ _) (throw :exit s)))
  688. (org-export-as (org-export-create-backend)))))))
  689. (should
  690. (equal '(nil . "v2")
  691. (catch :exit
  692. (org-test-with-temp-text "[cite/nil/v2:@a]"
  693. (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
  694. (org-cite--processors nil))
  695. (org-cite-register-processor 'foo
  696. :export-citation (lambda (_ s _ _) (throw :exit s)))
  697. (org-export-as (org-export-create-backend)))))))
  698. (should
  699. (equal '("local" . nil)
  700. (catch :exit
  701. (org-test-with-temp-text "[cite/local:@a]"
  702. (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
  703. (org-cite--processors nil))
  704. (org-cite-register-processor 'foo
  705. :export-citation (lambda (_ s _ _) (throw :exit s)))
  706. (org-export-as (org-export-create-backend))))))))
  707. (ert-deftest test-org-cite/list-citations ()
  708. "Test `org-cite-list-citations'."
  709. (should
  710. (equal '("a")
  711. (org-test-with-parsed-data "Test [cite:@a]"
  712. (cl-mapcan (lambda (c)
  713. (mapcar (lambda (ref)
  714. (org-element-property :key ref))
  715. (org-element-contents c)))
  716. (org-cite-list-citations info)))))
  717. (should
  718. (equal '("a" "b")
  719. (org-test-with-parsed-data "Test [cite:@a] [cite:@b]"
  720. (cl-mapcan (lambda (c)
  721. (mapcar (lambda (ref)
  722. (org-element-property :key ref))
  723. (org-element-contents c)))
  724. (org-cite-list-citations info)))))
  725. (should
  726. (equal '("a")
  727. (org-test-with-parsed-data "Test[fn:1]\n[fn:1] [cite:@a]"
  728. (cl-mapcan (lambda (c)
  729. (mapcar (lambda (ref)
  730. (org-element-property :key ref))
  731. (org-element-contents c)))
  732. (org-cite-list-citations info)))))
  733. (should
  734. (equal '("a" "b")
  735. (org-test-with-parsed-data "First[cite:@a] Second[fn:1]\n[fn:1] [cite:@b]"
  736. (cl-mapcan (lambda (c)
  737. (mapcar (lambda (ref)
  738. (org-element-property :key ref))
  739. (org-element-contents c)))
  740. (org-cite-list-citations info)))))
  741. (should
  742. (equal '("b" "a")
  743. (org-test-with-parsed-data "First[fn:1] Second[cite:@a]\n[fn:1] [cite:@b]"
  744. (cl-mapcan (lambda (c)
  745. (mapcar (lambda (ref)
  746. (org-element-property :key ref))
  747. (org-element-contents c)))
  748. (org-cite-list-citations info)))))
  749. (should
  750. (equal '("a" "b")
  751. (org-test-with-parsed-data
  752. "Text[fn:1][fn:2]\n[fn:1] [cite:@a]\n\n[fn:2] [cite:@b]"
  753. (cl-mapcan (lambda (c)
  754. (mapcar (lambda (ref)
  755. (org-element-property :key ref))
  756. (org-element-contents c)))
  757. (org-cite-list-citations info)))))
  758. (should
  759. (equal '("b" "a")
  760. (org-test-with-parsed-data
  761. "Text[fn:1]\n[fn:1] [fn:2][cite:@a]\n\n[fn:2] [cite:@b]"
  762. (cl-mapcan (lambda (c)
  763. (mapcar (lambda (ref)
  764. (org-element-property :key ref))
  765. (org-element-contents c)))
  766. (org-cite-list-citations info)))))
  767. (should
  768. (equal '("a" "b")
  769. (org-test-with-parsed-data
  770. "Text[fn:1]\n[fn:1] [cite:@a][fn:2]\n\n[fn:2] [cite:@b]"
  771. (cl-mapcan (lambda (c)
  772. (mapcar (lambda (ref)
  773. (org-element-property :key ref))
  774. (org-element-contents c)))
  775. (org-cite-list-citations info)))))
  776. (should
  777. (equal '("a")
  778. (org-test-with-parsed-data "Text[fn::[cite:@a]]"
  779. (cl-mapcan (lambda (c)
  780. (mapcar (lambda (ref)
  781. (org-element-property :key ref))
  782. (org-element-contents c)))
  783. (org-cite-list-citations info))))))
  784. (ert-deftest test-org-cite/list-keys ()
  785. "Test `org-cite-list-keys'."
  786. (should
  787. (equal '("a")
  788. (org-test-with-parsed-data "Test [cite:@a]"
  789. (org-cite-list-keys info))))
  790. (should
  791. (equal '("a" "b")
  792. (org-test-with-parsed-data "Test [cite:@a] [cite:@b]"
  793. (org-cite-list-keys info))))
  794. ;; Remove duplicates.
  795. (should
  796. (equal '("a")
  797. (org-test-with-parsed-data "Test [cite:@a] [cite:@a]"
  798. (org-cite-list-keys info))))
  799. ;; Keys are ordered by first appearance in the document.
  800. (should
  801. (equal '("a" "b")
  802. (org-test-with-parsed-data "Test [cite:@a] [cite:@b] [cite:@a]"
  803. (org-cite-list-keys info))))
  804. (should
  805. (equal '("a" "b" "c")
  806. (org-test-with-parsed-data
  807. "Test [cite:@a][fn:1] [cite:@c] [cite:@a]\n[fn:1] [cite:@b]"
  808. (org-cite-list-keys info)))))
  809. (ert-deftest test-org-cite/key-number ()
  810. "Test `org-cite-key-number'."
  811. (should
  812. (= 1 (org-test-with-parsed-data "[cite:@key]"
  813. (org-cite-key-number "key" info))))
  814. (should
  815. (equal '(1 2)
  816. (org-test-with-parsed-data "[cite:@key] [cite:@key2] [cite:@key]"
  817. (list (org-cite-key-number "key" info)
  818. (org-cite-key-number "key2" info)))))
  819. ;; When "predicate" is nil, keys are sorted by appearance order in
  820. ;; the buffer.
  821. (should
  822. (equal '((1 . "a") (2 . "c") (3 . "b"))
  823. (org-test-with-parsed-data
  824. "[cite:@a][fn:1] [cite:@b]\n[fn:1] [cite:@c]"
  825. (sort (mapcar (lambda (key)
  826. (cons (org-cite-key-number key info) key))
  827. '("a" "b" "c"))
  828. #'car-less-than-car))))
  829. (should
  830. (equal '((1 . "a") (2 . "b") (3 . "c"))
  831. (org-test-with-parsed-data
  832. "[cite:@a][fn:1] [cite:@b]\n[fn:1] [cite:@c]"
  833. (sort (mapcar (lambda (key)
  834. (cons (org-cite-key-number key info #'string<) key))
  835. '("a" "b" "c"))
  836. #'car-less-than-car)))))
  837. (ert-deftest test-org-cite/wrap-citation ()
  838. "Test `org-cite-wrap-citation'."
  839. ;; Reference test.
  840. (should
  841. (org-test-with-parsed-data "[cite:@key]"
  842. (org-element-map tree 'citation
  843. (lambda (c)
  844. (org-cite-wrap-citation c info)
  845. (org-cite-inside-footnote-p c))
  846. info)))
  847. ;; Created footnote is anonymous.
  848. (should-not
  849. (org-test-with-parsed-data "[cite:@key] "
  850. (org-element-map tree 'citation
  851. (lambda (c)
  852. (org-cite-wrap-citation c info)
  853. (org-element-property :label (org-cite-inside-footnote-p c)))
  854. info)))
  855. ;; Created footnote is inline.
  856. (should
  857. (equal '(inline)
  858. (org-test-with-parsed-data "[cite:@key]"
  859. (org-element-map tree 'citation
  860. (lambda (c)
  861. (org-cite-wrap-citation c info)
  862. (org-element-property :type
  863. (org-cite-inside-footnote-p c)))
  864. info))))
  865. ;; Preserve `:post-blank' property.
  866. (should
  867. (equal '(2)
  868. (org-test-with-parsed-data "[cite:@key] "
  869. (org-element-map tree 'citation
  870. (lambda (c)
  871. (org-cite-wrap-citation c info)
  872. (org-element-property :post-blank
  873. (org-cite-inside-footnote-p c)))
  874. info))))
  875. ;; Set `:post-blank' to 0 in the element before new footnote.
  876. (should-not
  877. (org-test-with-parsed-data "Text [cite:@key]"
  878. (org-element-map tree 'citation
  879. (lambda (c)
  880. (org-cite-wrap-citation c info)
  881. (let ((previous
  882. (org-export-get-previous-element
  883. (org-cite-inside-footnote-p c) info)))
  884. (string-match (rx blank string-end) previous)))
  885. info)))
  886. (should
  887. (equal '(0)
  888. (org-test-with-parsed-data "*Text* [cite:@key]"
  889. (org-element-map tree 'citation
  890. (lambda (c)
  891. (org-cite-wrap-citation c info)
  892. (let ((previous
  893. (org-export-get-previous-element
  894. (org-cite-inside-footnote-p c) info)))
  895. (org-element-property :post-blank previous)))
  896. info))))
  897. (should
  898. (equal '("Text")
  899. (org-test-with-parsed-data "Text [cite:@key]"
  900. (org-element-map tree 'citation
  901. (lambda (c)
  902. (org-cite-wrap-citation c info)
  903. (org-export-get-previous-element
  904. (org-cite-inside-footnote-p c) info))
  905. info)))))
  906. (defun test-org-cite--export-with-rule (text &optional rule punct)
  907. "Export TEXT string using RULE for punctuation positioning.
  908. Call `org-cite-adjust-note' on each citation object with RULE and, PUNCT
  909. arguments. Replace citation with \"@\" character in the output."
  910. (org-test-with-temp-text text
  911. (let ((org-cite--processors nil))
  912. (org-cite-register-processor 'test
  913. :export-citation
  914. (lambda (citation _s _b info)
  915. (org-cite-adjust-note citation info rule punct)
  916. "@"))
  917. (let ((org-cite-export-processors '((t . (test nil nil)))))
  918. (org-trim
  919. (org-export-as
  920. (org-export-create-backend
  921. :transcoders
  922. '((section . (lambda (_s c _i) (replace-regexp-in-string " @" "@" c)))
  923. (paragraph . (lambda (_s c _i) c))))))))))
  924. (ert-deftest test-org-cite/adjust-note ()
  925. "Test `org-cite-adjust-note' function."
  926. ;; Basic tests for all rules. In the output, @ replaces citation.
  927. (let ((cases '("\"[cite:@k]!"
  928. ".\"[cite:@k]!"
  929. "\"[cite:@k]"
  930. ".\"[cite:@k]"
  931. ".[cite:@k]"
  932. "[cite:@k]!")))
  933. (should ;test (inside inside after)
  934. (equal
  935. '(iia "!@\"" ".@\"!" "@\"" ".@\"" ".@" "!@")
  936. (cons 'iia
  937. (mapcar (lambda (c)
  938. (test-org-cite--export-with-rule
  939. c '(inside inside after)))
  940. cases))))
  941. (should ;test (inside inside before)
  942. (equal
  943. '(iib "@!\"" "@.\"!" "@\"" "@.\"" "@." "@!")
  944. (cons 'iib
  945. (mapcar (lambda (c)
  946. (test-org-cite--export-with-rule
  947. c '(inside inside before)))
  948. cases))))
  949. (should ;test (inside outside after)
  950. (equal
  951. '(ioa "!\"@" ".\"!@" "\"@" ".\"@" ".@" "!@")
  952. (cons 'ioa
  953. (mapcar (lambda (c)
  954. (test-org-cite--export-with-rule
  955. c '(inside outside after)))
  956. cases))))
  957. (should ;test (inside outside before)
  958. (equal
  959. '(iob "!\"@" ".\"@!" "\"@" ".\"@" "@." "@!")
  960. (cons 'iob
  961. (mapcar (lambda (c)
  962. (test-org-cite--export-with-rule
  963. c '(inside outside before)))
  964. cases))))
  965. (should ;test (inside same after)
  966. (equal
  967. '(isa "!@\"" ".\"!@" "\"@" ".@\"" ".@" "!@")
  968. (cons 'isa
  969. (mapcar (lambda (c)
  970. (test-org-cite--export-with-rule
  971. c '(inside same after)))
  972. cases))))
  973. (should ;test (inside same before)
  974. (equal
  975. '(isb "@!\"" ".\"@!" "\"@" "@.\"" "@." "@!")
  976. (cons 'isb
  977. (mapcar (lambda (c)
  978. (test-org-cite--export-with-rule
  979. c '(inside same before)))
  980. cases))))
  981. (should ;test (outside inside after)
  982. (equal
  983. '(oia "@\"!" ".@\"!" "@\"" "@\"." ".@" "!@")
  984. (cons 'oia
  985. (mapcar (lambda (c)
  986. (test-org-cite--export-with-rule
  987. c '(outside inside after)))
  988. cases))))
  989. (should ;test (outside inside before)
  990. (equal
  991. '(oib "@\"!" "@.\"!" "@\"" "@\"." "@." "@!")
  992. (cons 'oib
  993. (mapcar (lambda (c)
  994. (test-org-cite--export-with-rule
  995. c '(outside inside before)))
  996. cases))))
  997. (should ;test (outside outside after)
  998. (equal
  999. '(ooa "\"!@" ".\"!@" "\"@" "\".@" ".@" "!@")
  1000. (cons 'ooa
  1001. (mapcar (lambda (c)
  1002. (test-org-cite--export-with-rule
  1003. c '(outside outside after)))
  1004. cases))))
  1005. (should ;test (outside outside before)
  1006. (equal
  1007. '(oob "\"@!" ".\"@!" "\"@" "\"@." "@." "@!")
  1008. (cons 'oob
  1009. (mapcar (lambda (c)
  1010. (test-org-cite--export-with-rule
  1011. c '(outside outside before)))
  1012. cases))))
  1013. (should ;test (outside same after)
  1014. (equal
  1015. '(osa "\"!@" ".\"!@" "\"@" "\".@" ".@" "!@")
  1016. (cons 'osa
  1017. (mapcar (lambda (c)
  1018. (test-org-cite--export-with-rule
  1019. c '(outside same after)))
  1020. cases))))
  1021. (should ;test (outside same before)
  1022. (equal
  1023. '(osb "\"@!" ".\"@!" "\"@" "\"@." "@." "@!")
  1024. (cons 'osb
  1025. (mapcar (lambda (c)
  1026. (test-org-cite--export-with-rule
  1027. c '(outside same before)))
  1028. cases)))))
  1029. ;; Test `adaptive' behaviour.
  1030. (should
  1031. (equal "@\"."
  1032. (test-org-cite--export-with-rule ".\" [cite:@k]"
  1033. '(adaptive inside after))))
  1034. (should
  1035. (equal "@\"!"
  1036. (test-org-cite--export-with-rule "\" [cite:@k]!"
  1037. '(adaptive inside after))))
  1038. (should
  1039. (equal ".@\""
  1040. (test-org-cite--export-with-rule ".\"[cite:@k]"
  1041. '(adaptive inside after))))
  1042. (should
  1043. (equal "!@\""
  1044. (test-org-cite--export-with-rule "\"[cite:@k]!"
  1045. '(adaptive inside after))))
  1046. ;; Handle white space when inserting citation before quotation mark
  1047. ;; or punctuation.
  1048. (should
  1049. (equal ",@\" next"
  1050. (test-org-cite--export-with-rule ",\" [cite:@k] next"
  1051. '(inside inside after))))
  1052. (should
  1053. (equal "@,\" next"
  1054. (test-org-cite--export-with-rule ",\" [cite:@k] next"
  1055. '(inside inside before))))
  1056. (should
  1057. (equal "@\"."
  1058. (test-org-cite--export-with-rule "\" [cite:@k]."
  1059. '(outside inside before))))
  1060. (should
  1061. (equal "@\" !"
  1062. (test-org-cite--export-with-rule "\" [cite:@k] !"
  1063. '(outside inside before))))
  1064. (should
  1065. (equal "text@ !"
  1066. (test-org-cite--export-with-rule "text ![cite:@k]"
  1067. '(inside outside before))))
  1068. ;; Preserve white space between citation and final punctuation when
  1069. ;; moving citation past final punctuation.
  1070. (should
  1071. (equal "text !@"
  1072. (test-org-cite--export-with-rule "text [cite:@k] !"
  1073. '(inside inside after))))
  1074. (should
  1075. (equal "text\n !@"
  1076. (test-org-cite--export-with-rule "text [cite:@k]\n !"
  1077. '(inside inside after))))
  1078. ;; Choose punctuation with optional argument.
  1079. (should-not
  1080. (equal "!@"
  1081. (test-org-cite--export-with-rule "[cite:@k]!"
  1082. '(inside outside after)
  1083. '("."))))
  1084. (should
  1085. (equal ".@"
  1086. (test-org-cite--export-with-rule "[cite:@k]."
  1087. '(inside outside after)
  1088. '(".")))))
  1089. (ert-deftest test-org-cite/parse-elements ()
  1090. "Test `org-cite-parse-elements' function."
  1091. (should-error (org-cite-parse-elements "* H"))
  1092. (should-error (org-cite-parse-elements "Paragraph\n* H"))
  1093. (should
  1094. (equal '(paragraph)
  1095. (mapcar #'org-element-type (org-cite-parse-elements "s"))))
  1096. (should
  1097. (equal '(paragraph paragraph)
  1098. (mapcar #'org-element-type (org-cite-parse-elements "Text\n\nText")))))
  1099. (ert-deftest test-org-cite/parse-objects ()
  1100. "Test `org-cite-parse-objects' function."
  1101. (should
  1102. (equal '(plain-text)
  1103. (mapcar #'org-element-type (org-cite-parse-objects "s"))))
  1104. (should
  1105. (equal '(plain-text bold)
  1106. (mapcar #'org-element-type (org-cite-parse-objects "s *b*"))))
  1107. (should
  1108. (equal '(link)
  1109. (mapcar #'org-element-type (org-cite-parse-objects "[[link]]"))))
  1110. ;; When optional argument is non-nil, only recognize types allowed
  1111. ;; in as a citation reference affix.
  1112. (should-not
  1113. (equal '(link)
  1114. (mapcar #'org-element-type (org-cite-parse-objects "[[link]]" t))))
  1115. (should
  1116. (equal '(bold)
  1117. (mapcar #'org-element-type (org-cite-parse-objects "*b*" t)))))
  1118. (ert-deftest test-org-cite/make-paragraph ()
  1119. "Test `org-cite-make-paragraph' function."
  1120. ;; Check string as argument.
  1121. (should
  1122. (eq 'paragraph
  1123. (org-element-type (org-cite-make-paragraph "a"))))
  1124. (should
  1125. (equal '("a")
  1126. (org-element-contents (org-cite-make-paragraph "a"))))
  1127. ;; Check object as argument.
  1128. (should
  1129. (eq 'paragraph
  1130. (org-element-type
  1131. (org-cite-make-paragraph (org-element-create 'bold nil "b")))))
  1132. (should
  1133. (equal '(bold)
  1134. (mapcar #'org-element-type
  1135. (org-element-contents
  1136. (org-cite-make-paragraph (org-element-create 'bold nil "b"))))))
  1137. ;; Check secondary string as argument.
  1138. (should
  1139. (eq 'paragraph
  1140. (org-element-type (org-cite-make-paragraph '("a")))))
  1141. (should
  1142. (equal '("a")
  1143. (org-element-contents (org-cite-make-paragraph '("a")))))
  1144. ;; Mix all types of arguments.
  1145. (should
  1146. (equal '(plain-text bold plain-text)
  1147. (mapcar #'org-element-type
  1148. (org-element-contents
  1149. (org-cite-make-paragraph
  1150. "a" (org-element-create 'bold nil "b") '("c"))))))
  1151. ;; Check `:parent' property.
  1152. (should
  1153. (eq 'paragraph
  1154. (org-element-type
  1155. (org-element-property
  1156. :parent
  1157. (car (org-element-contents (org-cite-make-paragraph "a"))))))))
  1158. (ert-deftest test-org-cite/emphasize ()
  1159. "Test `org-cite-emphasize' function."
  1160. ;; Raise an error if first argument has wrong type.
  1161. (should-error (org-cite-emphasize 'code "a"))
  1162. ;; Check string argument.
  1163. (should (eq 'bold (org-element-type (org-cite-emphasize 'bold "a"))))
  1164. (should (equal '("a") (org-element-contents (org-cite-emphasize 'bold "a"))))
  1165. ;; Check object argument.
  1166. (should
  1167. (eq 'bold
  1168. (org-element-type
  1169. (org-cite-emphasize 'bold (org-element-create 'bold nil "a")))))
  1170. (should
  1171. (equal '(italic)
  1172. (mapcar #'org-element-type
  1173. (org-element-contents
  1174. (org-cite-emphasize 'bold
  1175. (org-element-create 'italic nil "a"))))))
  1176. ;; Check secondary string argument.
  1177. (should (eq 'bold (org-element-type (org-cite-emphasize 'bold '("a")))))
  1178. (should (equal '("a") (org-element-contents (org-cite-emphasize 'bold '("a")))))
  1179. ;; Mix all types of arguments.
  1180. (should
  1181. (equal '(plain-text italic plain-text)
  1182. (mapcar #'org-element-type
  1183. (org-element-contents
  1184. (org-cite-emphasize 'bold
  1185. "a" (org-element-create 'italic nil "b") '("c"))))))
  1186. ;; Check `:parent' property.
  1187. (should
  1188. (eq 'bold
  1189. (org-element-type
  1190. (org-element-property
  1191. :parent
  1192. (car (org-element-contents (org-cite-emphasize 'bold "a"))))))))
  1193. (ert-deftest test-org-cite/concat ()
  1194. "Test `org-cite-concat' function."
  1195. ;; Return nil when there is no data.
  1196. (should
  1197. (equal "" (org-element-interpret-data (org-cite-concat))))
  1198. ;; Concatenate strings, objects and secondary strings.
  1199. (should
  1200. (equal "ab"
  1201. (org-element-interpret-data (org-cite-concat "a" "b"))))
  1202. (should
  1203. (equal "*a* b"
  1204. (org-element-interpret-data
  1205. (org-cite-concat (org-element-create 'bold nil "a") " b"))))
  1206. (should
  1207. (equal "*a* b"
  1208. (org-element-interpret-data
  1209. (org-cite-concat
  1210. (list (org-element-create 'bold nil "a")) " b"))))
  1211. ;; Return an error for any other object type.
  1212. (should-error (org-cite-concat 2)))
  1213. (ert-deftest test-org-cite/mapconcat ()
  1214. "Test `org-cite-mapconcat' function."
  1215. (should
  1216. (equal ""
  1217. (org-element-interpret-data
  1218. (org-cite-mapconcat #'identity nil ""))))
  1219. (should
  1220. (equal "ab"
  1221. (org-element-interpret-data
  1222. (org-cite-mapconcat #'identity '("a" "b") ""))))
  1223. (should
  1224. (equal "*a* b *c*"
  1225. (org-element-interpret-data
  1226. (org-cite-mapconcat
  1227. #'identity
  1228. (list (org-element-create 'bold nil "a")
  1229. (list " b " (org-element-create 'bold nil "c"))) ""))))
  1230. (should
  1231. (equal "*a* *b*"
  1232. (org-element-interpret-data
  1233. (org-cite-mapconcat
  1234. (lambda (s) (org-element-create 'bold nil s))
  1235. '("a" "b") " "))))
  1236. (should
  1237. (equal "*a* b*c*"
  1238. (org-element-interpret-data
  1239. (org-cite-mapconcat
  1240. #'identity
  1241. (list (org-element-create 'bold nil "a")
  1242. (list "b" (org-element-create 'bold nil "c"))) " ")))))
  1243. ;;; TEST capabilities.
  1244. (ert-deftest test-org-cite/activate-capability ()
  1245. "Test \"activate\" capability."
  1246. ;; Standard test.
  1247. (should
  1248. (eq 'success
  1249. (catch :exit
  1250. (org-test-with-temp-text "[cite:@key]"
  1251. (let ((org-cite--processors nil)
  1252. (org-cite-activate-processor 'foo))
  1253. (org-cite-register-processor 'foo
  1254. :activate (lambda (_) (throw :exit 'success)))
  1255. (font-lock-ensure))))))
  1256. ;; If there is no "follow" processor, or if processor does not
  1257. ;; handle this capability, fall back to fontifying whole citation
  1258. ;; with `org-cite' face and each key with `org-cite-key' face.
  1259. (should
  1260. (eq 'org-cite
  1261. (org-test-with-temp-text "[cite:@key]"
  1262. (let ((org-cite-activate-processor nil))
  1263. (font-lock-ensure)
  1264. (face-at-point)))))
  1265. (should
  1266. (eq 'org-cite-key
  1267. (org-test-with-temp-text "[cite:@<point>key]"
  1268. (let ((org-cite-activate-processor nil))
  1269. (font-lock-ensure)
  1270. (face-at-point)))))
  1271. (should
  1272. (eq 'org-cite
  1273. (org-test-with-temp-text "[cite:@key]"
  1274. (let ((org-cite--processors nil)
  1275. (org-cite-activate-processor 'foo))
  1276. (org-cite-register-processor 'foo)
  1277. (font-lock-ensure)
  1278. (face-at-point))))))
  1279. (ert-deftest test-org-cite/export-capability ()
  1280. "Test \"export\" capability."
  1281. ;; Regular citations export.
  1282. (should
  1283. (eq 'success
  1284. (catch :exit
  1285. (org-test-with-temp-text "[cite:@key]"
  1286. (let ((org-cite--processors nil)
  1287. (org-cite-export-processors '((t . (foo nil nil)))))
  1288. (org-cite-register-processor 'foo
  1289. :export-citation (lambda (&rest _) (throw :exit 'success)))
  1290. (org-export-as (org-export-create-backend)))))))
  1291. ;; Export citation as string.
  1292. (should
  1293. (equal "citation\n"
  1294. (org-test-with-temp-text "[cite:@key]"
  1295. (let ((org-cite--processors nil)
  1296. (org-cite-export-processors '((t . (foo nil nil)))))
  1297. (org-cite-register-processor 'foo
  1298. :export-citation (lambda (&rest _) "citation"))
  1299. (org-export-as (org-export-create-backend
  1300. :transcoders
  1301. '((section . (lambda (_ c _) c))
  1302. (paragraph . (lambda (_ c _) c)))))))))
  1303. ;; Export citation as parsed object.
  1304. (should
  1305. (equal "success\n"
  1306. (org-test-with-temp-text "[cite:@key]"
  1307. (let ((org-cite--processors nil)
  1308. (org-cite-export-processors '((t . (foo nil nil)))))
  1309. (org-cite-register-processor 'foo
  1310. :export-citation (lambda (&rest _)
  1311. (org-element-create 'bold nil "cite")))
  1312. (org-export-as (org-export-create-backend
  1313. :transcoders
  1314. '((section . (lambda (_ c _) c))
  1315. (paragraph . (lambda (_ c _) c))
  1316. (bold . (lambda (&rest _) "success")))))))))
  1317. ;; Export citation as a secondary string.
  1318. (should
  1319. (equal "boldtwo\n"
  1320. (org-test-with-temp-text "[cite:@key]"
  1321. (let ((org-cite--processors nil)
  1322. (org-cite-export-processors '((t . (foo nil nil)))))
  1323. (org-cite-register-processor 'foo
  1324. :export-citation (lambda (&rest _)
  1325. (list (org-element-create 'bold nil "one")
  1326. "two")))
  1327. (org-export-as (org-export-create-backend
  1328. :transcoders
  1329. '((section . (lambda (_ c _) c))
  1330. (paragraph . (lambda (_ c _) c))
  1331. (bold . (lambda (&rest _) "bold")))))))))
  1332. ;; When exporting citation as a secondary string, last object
  1333. ;; inherits post-blank from initial citation.
  1334. (should
  1335. (equal "twobold one-space\n"
  1336. (org-test-with-temp-text "[cite:@key] one-space"
  1337. (let ((org-cite--processors nil)
  1338. (org-cite-export-processors '((t . (foo nil nil)))))
  1339. (org-cite-register-processor 'foo
  1340. :export-citation (lambda (&rest _)
  1341. (list "two"
  1342. (org-element-create 'bold nil "one"))))
  1343. (org-export-as (org-export-create-backend
  1344. :transcoders
  1345. '((section . (lambda (_ c _) c))
  1346. (paragraph . (lambda (_ c _) c))
  1347. (bold . (lambda (&rest _) "bold")))))))))
  1348. (should
  1349. (equal "boldtwo one-space\n"
  1350. (org-test-with-temp-text "[cite:@key] one-space"
  1351. (let ((org-cite--processors nil)
  1352. (org-cite-export-processors '((t . (foo nil nil)))))
  1353. (org-cite-register-processor 'foo
  1354. :export-citation (lambda (&rest _)
  1355. (list (org-element-create 'bold nil "one")
  1356. "two")))
  1357. (org-export-as (org-export-create-backend
  1358. :transcoders
  1359. '((section . (lambda (_ c _) c))
  1360. (paragraph . (lambda (_ c _) c))
  1361. (bold . (lambda (&rest _) "bold")))))))))
  1362. ;; Regular bibliography export.
  1363. (should
  1364. (eq 'success
  1365. (catch :exit
  1366. (org-test-with-temp-text "#+print_bibliography:"
  1367. (let ((org-cite--processors nil)
  1368. (org-cite-export-processors '((t . (foo nil nil)))))
  1369. (org-cite-register-processor 'foo
  1370. :export-bibliography (lambda (&rest _) (throw :exit 'success))
  1371. :export-citation #'ignore)
  1372. (org-export-as (org-export-create-backend)))))))
  1373. (should
  1374. (equal ""
  1375. (org-test-with-temp-text "#+print_bibliography:"
  1376. (let ((org-cite--processors nil)
  1377. (org-cite-export-processors '((t . (foo nil nil)))))
  1378. (org-cite-register-processor 'foo
  1379. :export-citation #'ignore)
  1380. (org-export-as (org-export-create-backend
  1381. :transcoders
  1382. '((section . (lambda (_ c _) c))
  1383. (paragraph . (lambda (_ c _) c)))))))))
  1384. ;; Export bibliography as string.
  1385. (should
  1386. (equal "bibliography\n"
  1387. (org-test-with-temp-text "#+print_bibliography:"
  1388. (let ((org-cite--processors nil)
  1389. (org-cite-export-processors '((t . (foo nil nil)))))
  1390. (org-cite-register-processor 'foo
  1391. :export-bibliography (lambda (&rest _) "bibliography")
  1392. :export-citation #'ignore)
  1393. (org-export-as (org-export-create-backend
  1394. :transcoders
  1395. '((section . (lambda (_ c _) c))
  1396. (paragraph . (lambda (_ c _) c)))))))))
  1397. ;; Export bibliography as a parsed element.
  1398. (should
  1399. (equal "success\n"
  1400. (org-test-with-temp-text "#+print_bibliography:"
  1401. (let ((org-cite--processors nil)
  1402. (org-cite-export-processors '((t . (foo nil nil)))))
  1403. (org-cite-register-processor 'foo
  1404. :export-bibliography
  1405. (lambda (&rest _)
  1406. (org-element-create 'example-block '(:value "foo")))
  1407. :export-citation #'ignore)
  1408. (org-export-as
  1409. (org-export-create-backend
  1410. :transcoders
  1411. '((section . (lambda (_ c _) c))
  1412. (example-block . (lambda (&rest _) "success")))))))))
  1413. ;; Export bibliography as a list of parsed elements.
  1414. (should
  1415. (equal "success\nsuccess\n"
  1416. (org-test-with-temp-text "#+print_bibliography:"
  1417. (let ((org-cite--processors nil)
  1418. (org-cite-export-processors '((t . (foo nil nil)))))
  1419. (org-cite-register-processor 'foo
  1420. :export-bibliography
  1421. (lambda (&rest _)
  1422. (list (org-element-create 'example-block '(:value "foo"))
  1423. (org-element-create 'example-block '(:value "bar"))))
  1424. :export-citation #'ignore)
  1425. (org-export-as
  1426. (org-export-create-backend
  1427. :transcoders
  1428. '((section . (lambda (_ c _) c))
  1429. (example-block . (lambda (&rest _) "success")))))))))
  1430. ;; When exporting bibliography as a list of parsed elements, the
  1431. ;; last element inherits post-blank from initial keyword.
  1432. (should
  1433. (equal "success\nsuccess\n\nText\n"
  1434. (org-test-with-temp-text "#+print_bibliography:\n\nText"
  1435. (let ((org-cite--processors nil)
  1436. (org-cite-export-processors '((t . (foo nil nil)))))
  1437. (org-cite-register-processor 'foo
  1438. :export-bibliography
  1439. (lambda (&rest _)
  1440. (list (org-element-create 'example-block '(:value "foo"))
  1441. (org-element-create 'example-block '(:value "bar"))))
  1442. :export-citation #'ignore)
  1443. (org-export-as
  1444. (org-export-create-backend
  1445. :transcoders
  1446. '((section . (lambda (_ c _) c))
  1447. (example-block . (lambda (&rest _) "success"))
  1448. (paragraph . (lambda (_ c _) c)))))))))
  1449. ;; Use more appropriate citation processor.
  1450. (should
  1451. (equal
  1452. '(p1 p1 p1 p3)
  1453. (org-test-with-temp-text "[cite:@a]"
  1454. (let ((org-export-registered-backends nil)
  1455. (org-cite--procesors nil)
  1456. (org-cite-export-processors
  1457. '((b1 . (p1))
  1458. (t . (p3)))))
  1459. (org-cite-register-processor 'p1
  1460. :export-citation (lambda (&rest _) (throw :exit 'p1)))
  1461. (org-cite-register-processor 'p2
  1462. :export-citation (lambda (&rest _) (throw :exit 'p2)))
  1463. (org-cite-register-processor 'p3
  1464. :export-citation (lambda (&rest _) (throw :exit 'p3)))
  1465. (org-export-define-backend 'b1 nil)
  1466. (org-export-define-derived-backend 'b2 'b1)
  1467. (org-export-define-derived-backend 'b3 'b2)
  1468. (list (catch :exit (org-export-as 'b1))
  1469. (catch :exit (org-export-as 'b2))
  1470. (catch :exit (org-export-as 'b3))
  1471. (catch :exit (org-export-as (org-export-create-backend))))))))
  1472. (should
  1473. (eq 'p2
  1474. (org-test-with-temp-text "#+cite_export: p2\n[cite:@a]"
  1475. (let ((org-export-registered-backends nil)
  1476. (org-cite--procesors nil)
  1477. (org-cite-export-processors '((t . (p1)))))
  1478. (org-cite-register-processor 'p1
  1479. :export-citation (lambda (&rest _) (throw :exit 'p1)))
  1480. (org-cite-register-processor 'p2
  1481. :export-citation (lambda (&rest _) (throw :exit 'p2)))
  1482. (catch :exit (org-export-as (org-export-create-backend)))))))
  1483. ;; Test finalizer.
  1484. (should
  1485. (eq 'success
  1486. (catch :exit
  1487. (org-test-with-temp-text "[cite:@key]"
  1488. (let ((org-cite--processors nil)
  1489. (org-cite-export-processors '((t . (foo nil nil)))))
  1490. (org-cite-register-processor 'foo
  1491. :export-citation (lambda (&rest _) "")
  1492. :export-finalizer (lambda (&rest _) (throw :exit 'success)))
  1493. (org-export-as (org-export-create-backend)))))))
  1494. (should
  1495. (equal "finalized!"
  1496. (org-test-with-temp-text "[cite:@key]"
  1497. (let ((org-cite--processors nil)
  1498. (org-cite-export-processors '((t . (foo nil nil)))))
  1499. (org-cite-register-processor 'foo
  1500. :export-citation #'ignore
  1501. :export-finalizer (lambda (&rest _) "finalized!"))
  1502. (org-export-as (org-export-create-backend))))))
  1503. ;; Ignore citations when there is no selected "export" processor.
  1504. ;; In that case, white space is removed before the citation, not
  1505. ;; after.
  1506. (should
  1507. (equal ""
  1508. (org-test-with-temp-text "[cite:@key]"
  1509. (let ((org-cite-export-processors nil))
  1510. (org-export-as (org-export-create-backend
  1511. :transcoders
  1512. '((section . (lambda (_ c _) c))
  1513. (paragraph . (lambda (_ c _) c)))))))))
  1514. (should
  1515. (equal "Text.\n"
  1516. (org-test-with-temp-text "Text [cite:@key]."
  1517. (let ((org-cite-export-processors nil))
  1518. (org-export-as (org-export-create-backend
  1519. :transcoders
  1520. '((section . (lambda (_ c _) c))
  1521. (paragraph . (lambda (_ c _) c)))))))))
  1522. ;; Throw an error if selected processor does not handle "export"
  1523. ;; capability.
  1524. (should-error
  1525. (org-test-with-temp-text "[cite:@key]"
  1526. (let ((org-cite--processors nil)
  1527. (org-cite-export-processors '((t . (foo nil nil)))))
  1528. (org-cite-register-processor 'foo)
  1529. (org-export-as (org-export-create-backend))))))
  1530. (ert-deftest test-org-cite/follow-capability ()
  1531. "Test \"follow\" capability."
  1532. ;; Standard test.
  1533. (should
  1534. (eq 'success
  1535. (catch :exit
  1536. (org-test-with-temp-text "[cite:@key]"
  1537. (let ((org-cite--processors nil)
  1538. (org-cite-follow-processor 'foo))
  1539. (org-cite-register-processor 'foo
  1540. :follow (lambda (_ _) (throw :exit 'success)))
  1541. (org-open-at-point))))))
  1542. ;; Throw an error if there is no "follow" processor, or if it is
  1543. ;; unable to follow a citation.
  1544. (should-error
  1545. (org-test-with-temp-text "[cite:@key]"
  1546. (let ((org-cite-follow-processor nil))
  1547. (org-open-at-point))))
  1548. (should-error
  1549. (org-test-with-temp-text "[cite:@key]"
  1550. (let ((org-cite--processors nil)
  1551. (org-cite-follow-processor 'foo))
  1552. (org-cite-register-processor 'foo)
  1553. (org-open-at-point)))))
  1554. (ert-deftest test-org-cite/make-insert-processor ()
  1555. "Test `org-cite-make-insert-processor'."
  1556. (should-error (org-cite-make-insert-processor 1 2))
  1557. (should-error
  1558. (org-test-with-temp-text "[cite:@<point>a]"
  1559. (let ((org-cite--processors nil)
  1560. (org-cite-insert-processor 'foo))
  1561. (org-cite-register-processor 'foo
  1562. :insert (org-cite-make-insert-processor
  1563. #'ignore (lambda (&rest _) "s")))
  1564. (org-cite-insert nil))))
  1565. (should
  1566. (equal "[cite:@k]"
  1567. (org-test-with-temp-text "[cite:@<point>a]"
  1568. (let ((org-cite--processors nil)
  1569. (org-cite-insert-processor 'foo))
  1570. (org-cite-register-processor 'foo
  1571. :insert (org-cite-make-insert-processor
  1572. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1573. (org-cite-insert nil)
  1574. (buffer-string)))))
  1575. (should
  1576. (equal "[cite:@k;@a]"
  1577. (org-test-with-temp-text "[cite:<point>@a]"
  1578. (let ((org-cite--processors nil)
  1579. (org-cite-insert-processor 'foo))
  1580. (org-cite-register-processor 'foo
  1581. :insert (org-cite-make-insert-processor
  1582. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1583. (org-cite-insert nil)
  1584. (buffer-string)))))
  1585. (should
  1586. (equal "[cite:@k;pre @a]"
  1587. (org-test-with-temp-text "[cite:<point>pre @a]"
  1588. (let ((org-cite--processors nil)
  1589. (org-cite-insert-processor 'foo))
  1590. (org-cite-register-processor 'foo
  1591. :insert (org-cite-make-insert-processor
  1592. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1593. (org-cite-insert nil)
  1594. (buffer-string)))))
  1595. (should
  1596. (equal "[cite:pre;@k;@a]"
  1597. (org-test-with-temp-text "[cite:<point>pre;@a]"
  1598. (let ((org-cite--processors nil)
  1599. (org-cite-insert-processor 'foo))
  1600. (org-cite-register-processor 'foo
  1601. :insert (org-cite-make-insert-processor
  1602. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1603. (org-cite-insert nil)
  1604. (buffer-string)))))
  1605. (should
  1606. (equal "[cite:@a;@k]"
  1607. (org-test-with-temp-text "[cite:@a<point>]"
  1608. (let ((org-cite--processors nil)
  1609. (org-cite-insert-processor 'foo))
  1610. (org-cite-register-processor 'foo
  1611. :insert (org-cite-make-insert-processor
  1612. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1613. (org-cite-insert nil)
  1614. (buffer-string)))))
  1615. (should
  1616. (equal "[cite:@a post;@k]"
  1617. (org-test-with-temp-text "[cite:@a post<point>]"
  1618. (let ((org-cite--processors nil)
  1619. (org-cite-insert-processor 'foo))
  1620. (org-cite-register-processor 'foo
  1621. :insert (org-cite-make-insert-processor
  1622. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1623. (org-cite-insert nil)
  1624. (buffer-string)))))
  1625. (should
  1626. (equal "[cite:@a;@k;post]"
  1627. (org-test-with-temp-text "[cite:@a;post<point>]"
  1628. (let ((org-cite--processors nil)
  1629. (org-cite-insert-processor 'foo))
  1630. (org-cite-register-processor 'foo
  1631. :insert (org-cite-make-insert-processor
  1632. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1633. (org-cite-insert nil)
  1634. (buffer-string)))))
  1635. (should
  1636. (equal ""
  1637. (org-test-with-temp-text "[cite:@<point>a]"
  1638. (let ((org-cite--processors nil)
  1639. (org-cite-insert-processor 'foo))
  1640. (org-cite-register-processor 'foo
  1641. :insert (org-cite-make-insert-processor
  1642. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1643. (org-cite-insert t)
  1644. (buffer-string)))))
  1645. (should
  1646. (equal "[cite/s:@a]"
  1647. (org-test-with-temp-text "[cite<point>:@a]"
  1648. (let ((org-cite--processors nil)
  1649. (org-cite-insert-processor 'foo))
  1650. (org-cite-register-processor 'foo
  1651. :insert (org-cite-make-insert-processor
  1652. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1653. (org-cite-insert nil)
  1654. (buffer-string)))))
  1655. (should
  1656. (equal "[cite:@a]"
  1657. (org-test-with-temp-text "[cite<point>/style:@a]"
  1658. (let ((org-cite--processors nil)
  1659. (org-cite-insert-processor 'foo))
  1660. (org-cite-register-processor 'foo
  1661. :insert (org-cite-make-insert-processor
  1662. (lambda (&rest _) "k") (lambda (&rest _) "")))
  1663. (org-cite-insert nil)
  1664. (buffer-string)))))
  1665. (should-error
  1666. (org-test-with-temp-text "[cite<point>/style:@a]"
  1667. (let ((org-cite--processors nil)
  1668. (org-cite-insert-processor 'foo))
  1669. (org-cite-register-processor 'foo
  1670. :insert (org-cite-make-insert-processor
  1671. (lambda (&rest _) "k") #'ignore))
  1672. (org-cite-insert nil))))
  1673. (should
  1674. (equal "[cite:@a][cite:@k]"
  1675. (org-test-with-temp-text "[cite:@a]<point>"
  1676. (let ((org-cite--processors nil)
  1677. (org-cite-insert-processor 'foo))
  1678. (org-cite-register-processor 'foo
  1679. :insert (org-cite-make-insert-processor
  1680. (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
  1681. (org-cite-insert nil)
  1682. (buffer-string)))))
  1683. (should
  1684. (equal "[cite:@k][cite:@a]"
  1685. (org-test-with-temp-text "<point>[cite:@a]"
  1686. (let ((org-cite--processors nil)
  1687. (org-cite-insert-processor 'foo))
  1688. (org-cite-register-processor 'foo
  1689. :insert (org-cite-make-insert-processor
  1690. (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
  1691. (org-cite-insert nil)
  1692. (buffer-string)))))
  1693. (should
  1694. (equal "[cite/s:@k][cite:@a]"
  1695. (org-test-with-temp-text "<point>[cite:@a]"
  1696. (let ((org-cite--processors nil)
  1697. (org-cite-insert-processor 'foo))
  1698. (org-cite-register-processor 'foo
  1699. :insert (org-cite-make-insert-processor
  1700. (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
  1701. (org-cite-insert t)
  1702. (buffer-string))))))
  1703. (ert-deftest test-org-cite/insert-capability ()
  1704. "Test \"insert\" capability."
  1705. ;; Standard test.
  1706. (should
  1707. (eq 'success
  1708. (catch :exit
  1709. (org-test-with-temp-text ""
  1710. (let ((org-cite--processors nil)
  1711. (org-cite-insert-processor 'foo))
  1712. (org-cite-register-processor 'foo
  1713. :insert (lambda (_ _) (throw :exit 'success)))
  1714. (call-interactively #'org-cite-insert))))))
  1715. ;; Throw an error if there is no "insert" processor, or if it is
  1716. ;; unable to insert a citation.
  1717. (should-error
  1718. (org-test-with-temp-text ""
  1719. (let ((org-cite-insert-processor nil))
  1720. (call-interactively #'org-cite-insert))))
  1721. (should-error
  1722. (org-test-with-temp-text ""
  1723. (let ((org-cite--processors nil)
  1724. (org-cite-insert-processor 'foo))
  1725. (org-cite-register-processor 'foo)
  1726. (call-interactively #'org-cite-insert))))
  1727. ;; Throw an error if the location is inappropriate for a citation.
  1728. (should-error
  1729. (org-test-with-temp-text "=verbatim<point> text="
  1730. (let ((org-cite--processors nil)
  1731. (org-cite-insert-processor 'foo))
  1732. (org-cite-register-processor 'foo
  1733. :insert (lambda (_ _) (throw :exit 'success)))
  1734. (call-interactively #'org-cite-insert)))))
  1735. (provide 'test-oc)
  1736. ;;; test-oc.el ends here