test-oc.el 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  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/main-affixes ()
  241. "Test `org-cite-main-affixes'."
  242. (should
  243. (equal '(nil . nil)
  244. (org-test-with-temp-text "[cite:@key]"
  245. (org-cite-main-affixes (org-element-context)))))
  246. (should
  247. (equal '(nil . nil)
  248. (org-test-with-temp-text "[cite:@key1;@key2]"
  249. (org-cite-main-affixes (org-element-context)))))
  250. (should
  251. (equal '(("pre ") . nil)
  252. (org-test-with-temp-text "[cite:pre @key]"
  253. (org-cite-main-affixes (org-element-context)))))
  254. (should
  255. (equal '(("pre ") . (" post"))
  256. (org-test-with-temp-text "[cite:pre @key post]"
  257. (org-cite-main-affixes (org-element-context)))))
  258. (should
  259. (equal '(("pre ") . nil)
  260. (org-test-with-temp-text "[cite:global pre;pre @key]"
  261. (org-cite-main-affixes (org-element-context)))))
  262. (should
  263. (equal '(nil . (" post"))
  264. (org-test-with-temp-text "[cite:@key post;global post]"
  265. (org-cite-main-affixes (org-element-context)))))
  266. (should
  267. (equal '(("global pre") . ("global post"))
  268. (org-test-with-temp-text "[cite:global pre;@key1;@key2;global post]"
  269. (org-cite-main-affixes (org-element-context)))))
  270. (should
  271. (equal '(("global pre") . nil)
  272. (org-test-with-temp-text "[cite:global pre;pre1 @key1;pre2 @key2]"
  273. (org-cite-main-affixes (org-element-context)))))
  274. (should
  275. (equal '(nil . (" global post"))
  276. (org-test-with-temp-text "[cite:@key1 post1;@key2 post2; global post]"
  277. (org-cite-main-affixes (org-element-context))))))
  278. (ert-deftest test-org-cite/supported-styles ()
  279. "Test `org-cite-supported-styles'."
  280. ;; Default behavior is to use export processors.
  281. (should
  282. (equal '((("")))
  283. (let ((org-cite--processors nil)
  284. (org-cite-export-processors '((t test))))
  285. (org-cite-register-processor 'test :cite-styles '(((""))))
  286. (org-cite-supported-styles))))
  287. (should
  288. (equal '((("foo" "f")) (("")))
  289. (let ((org-cite--processors nil)
  290. (org-cite-export-processors '((t test))))
  291. (org-cite-register-processor 'test
  292. :cite-styles '((("foo" "f")) ((""))))
  293. (org-cite-supported-styles))))
  294. ;; Also support functions generating the list.
  295. (should
  296. (equal '((("foo" "f")) (("")))
  297. (let ((org-cite--processors nil)
  298. (org-cite-export-processors '((t test))))
  299. (org-cite-register-processor 'test
  300. :cite-styles (lambda () '((("foo" "f")) (("")))))
  301. (org-cite-supported-styles))))
  302. ;; Explicitly provide a processor.
  303. (should
  304. (equal '((("")))
  305. (let ((org-cite--processors nil))
  306. (org-cite-register-processor 'test :cite-styles '(((""))))
  307. (org-cite-supported-styles '(test)))))
  308. ;; Merge style shortcuts.
  309. (should
  310. (equal '((("foo" "f" "g")))
  311. (let ((org-cite--processors nil))
  312. (org-cite-register-processor 'test :cite-styles '((("foo" "f"))))
  313. (org-cite-register-processor 'test2 :cite-styles '((("foo" "g"))))
  314. (org-cite-supported-styles '(test test2)))))
  315. ;; Merge style variants.
  316. (should
  317. (equal '((("foo") ("bar") ("baz")))
  318. (let ((org-cite--processors nil))
  319. (org-cite-register-processor 'test
  320. :cite-styles '((("foo") ("bar"))))
  321. (org-cite-register-processor 'test2
  322. :cite-styles '((("foo") ("baz"))))
  323. (org-cite-supported-styles '(test test2)))))
  324. ;; Merge variant shortcuts.
  325. (should
  326. (equal '((("foo") ("bar" "b" "c")))
  327. (let ((org-cite--processors nil))
  328. (org-cite-register-processor 'test
  329. :cite-styles '((("foo") ("bar" "b"))))
  330. (org-cite-register-processor 'test2
  331. :cite-styles '((("foo") ("bar" "c"))))
  332. (org-cite-supported-styles '(test test2)))))
  333. ;; Ignore duplicates.
  334. (should
  335. (equal '((("foo") ("bar")))
  336. (let ((org-cite--processors nil))
  337. (org-cite-register-processor 'test
  338. :cite-styles '((("foo") ("bar"))))
  339. (org-cite-register-processor 'test2
  340. :cite-styles '((("foo") ("bar"))))
  341. (org-cite-supported-styles '(test test2)))))
  342. (should
  343. (equal '((("foo") ("bar" "b")))
  344. (let ((org-cite--processors nil))
  345. (org-cite-register-processor 'test
  346. :cite-styles '((("foo") ("bar" "b"))))
  347. (org-cite-register-processor 'test2
  348. :cite-styles '((("foo") ("bar" "b"))))
  349. (org-cite-supported-styles '(test test2))))))
  350. (ert-deftest test-org-cite/delete-citation ()
  351. "Test `org-cite-delete-citation'."
  352. ;; Error when not on a citation or citation reference.
  353. (should-error
  354. (org-test-with-temp-text "Text"
  355. (org-cite-delete-citation (org-element-context))))
  356. ;; When argument is a citation, delete it completely. Manage
  357. ;; properly blanks around it.
  358. (should
  359. (equal ""
  360. (org-test-with-temp-text "[cite:@key]"
  361. (org-cite-delete-citation (org-element-context))
  362. (buffer-string))))
  363. (should
  364. (equal "Before After"
  365. (org-test-with-temp-text "Before [<point>cite:@key] After"
  366. (org-cite-delete-citation (org-element-context))
  367. (buffer-string))))
  368. (should
  369. (equal "Before After"
  370. (org-test-with-temp-text "Before [<point>cite:@key]After"
  371. (org-cite-delete-citation (org-element-context))
  372. (buffer-string))))
  373. (should
  374. (equal "Before After"
  375. (org-test-with-temp-text "Before[<point>cite:@key] After"
  376. (org-cite-delete-citation (org-element-context))
  377. (buffer-string))))
  378. ;; Ensure there is at least a blank to separate consecutive objects.
  379. (should
  380. (equal "Before After"
  381. (org-test-with-temp-text "Before[<point>cite:@key]After"
  382. (org-cite-delete-citation (org-element-context))
  383. (buffer-string))))
  384. ;; Remove trailing blanks when removing the citation.
  385. (should
  386. (equal "Before"
  387. (org-test-with-temp-text "Before[<point>cite:@key] "
  388. (org-cite-delete-citation (org-element-context))
  389. (buffer-string))))
  390. ;; Preserve indentation if citation is at the beginning of the line.
  391. (should
  392. (equal " After"
  393. (org-test-with-temp-text " [<point>cite:@key] After"
  394. (org-cite-delete-citation (org-element-context))
  395. (buffer-string))))
  396. ;; When the citation is alone on a line, remove the whole line.
  397. (should
  398. (equal "Line 1\nLine 3"
  399. (org-test-with-temp-text "Line 1\n[<point>cite:@key]\nLine 3"
  400. (org-cite-delete-citation (org-element-context))
  401. (buffer-string))))
  402. ;; When there is only one citation reference object, remove the full
  403. ;; citation.
  404. (should
  405. (equal ""
  406. (org-test-with-temp-text "[cite:@<point>key]"
  407. (org-cite-delete-citation (org-element-context))
  408. (buffer-string))))
  409. (should
  410. (equal ""
  411. (org-test-with-temp-text "[cite:pre @<point>key post]"
  412. (org-cite-delete-citation (org-element-context))
  413. (buffer-string))))
  414. (should
  415. (equal ""
  416. (org-test-with-temp-text "[cite:pre; @<point>key ;post]"
  417. (org-cite-delete-citation (org-element-context))
  418. (buffer-string))))
  419. ;; Otherwise, remove the reference, including any affix.
  420. (should
  421. (equal "[cite:@before;@after]"
  422. (org-test-with-temp-text "[cite:@before;@<point>key;@after]"
  423. (org-cite-delete-citation (org-element-context))
  424. (buffer-string))))
  425. (should
  426. (equal "[cite:@before;@after]"
  427. (org-test-with-temp-text "[cite:@before;pre @<point>key post;@after]"
  428. (org-cite-delete-citation (org-element-context))
  429. (buffer-string))))
  430. (should
  431. (equal "[cite:@before]"
  432. (org-test-with-temp-text "[cite:@before;@<point>key]"
  433. (org-cite-delete-citation (org-element-context))
  434. (buffer-string))))
  435. (should
  436. (equal "[cite:@before;post]"
  437. (org-test-with-temp-text "[cite:@before;@<point>key;post]"
  438. (org-cite-delete-citation (org-element-context))
  439. (buffer-string))))
  440. (should
  441. (equal "[cite:@after]"
  442. (org-test-with-temp-text "[cite:@<point>key;@after]"
  443. (org-cite-delete-citation (org-element-context))
  444. (buffer-string))))
  445. (should
  446. (equal "[cite:pre;@after]"
  447. (org-test-with-temp-text "[cite:pre;@<point>key;@after]"
  448. (org-cite-delete-citation (org-element-context))
  449. (buffer-string)))))
  450. (ert-deftest test-org-cite/list-bibliography-files ()
  451. "Test `org-cite-list-bibliography-files'."
  452. (should
  453. (equal '("/bibliography")
  454. (org-test-with-temp-text "#+bibliography: /bibliography"
  455. (let ((org-cite-global-bibliography nil))
  456. (org-cite-list-bibliography-files)))))
  457. (should
  458. (equal '("/bibliography")
  459. (org-test-with-temp-text "#+bibliography: \"/bibliography\""
  460. (let ((org-cite-global-bibliography nil))
  461. (org-cite-list-bibliography-files)))))
  462. (should
  463. (equal '("/bibliography" "/other-bibliography")
  464. (org-test-with-temp-text "#+bibliography: /bibliography"
  465. (let ((org-cite-global-bibliography '("/other-bibliography")))
  466. (org-cite-list-bibliography-files)))))
  467. (should
  468. (equal '(t)
  469. (org-test-with-temp-text "#+bibliography: ./bibliography"
  470. (let ((org-cite-global-bibliography nil))
  471. (mapcar #'file-name-absolute-p (org-cite-list-bibliography-files))))))
  472. (should
  473. (equal '("/bibliographyA" "/bibliographyB")
  474. (org-test-with-temp-text
  475. "#+bibliography: /bibliographyA\n#+bibliography: /bibliographyB"
  476. (let ((org-cite-global-bibliography nil))
  477. (org-cite-list-bibliography-files)))))
  478. (should
  479. (equal '("/bibliographyA")
  480. (org-test-with-temp-text
  481. "#+bibliography: /bibliographyA\n#+bibliography: /bibliographyA"
  482. (let ((org-cite-global-bibliography nil))
  483. (org-cite-list-bibliography-files))))))
  484. (ert-deftest test-org-cite/bibliography-style ()
  485. "Test `org-cite-bibliography-style'."
  486. ;; Extract style from global processor definition.
  487. (should
  488. (equal "a"
  489. (catch :exit
  490. (org-test-with-temp-text "#+print_bibliography:"
  491. (let ((org-cite-export-processors '((t . (foo "a" "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. ;; Extract style from local processor definition.
  498. (should
  499. (equal "a"
  500. (catch :exit
  501. (org-test-with-temp-text
  502. "#+cite_export: foo a b\n#+print_bibliography:"
  503. (let ((org-cite-export-processors nil)
  504. (org-cite--processors nil))
  505. (org-cite-register-processor 'foo
  506. :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
  507. :export-citation #'ignore)
  508. (org-export-as (org-export-create-backend)))))))
  509. (should
  510. (equal "a b"
  511. (catch :exit
  512. (org-test-with-temp-text
  513. "#+cite_export: foo \"a b\" c\n#+print_bibliography:"
  514. (let ((org-cite-export-processors nil)
  515. (org-cite--processors nil))
  516. (org-cite-register-processor 'foo
  517. :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
  518. :export-citation #'ignore)
  519. (org-export-as (org-export-create-backend)))))))
  520. ;; Test priority: first keyword, then local.
  521. (should
  522. (equal "local"
  523. (catch :exit
  524. (org-test-with-temp-text
  525. "#+print_bibliography:\n#+cite_export: foo local a\n[cite:@a]"
  526. (let ((org-cite-export-processors '((t . (foo "global" "b"))))
  527. (org-cite--processors nil))
  528. (org-cite-register-processor 'foo
  529. :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
  530. :export-citation #'ignore)
  531. (org-export-as (org-export-create-backend)))))))
  532. ;; Explicit "nil" styles forces default style.
  533. (should-not
  534. (catch :exit
  535. (org-test-with-temp-text
  536. "#+print_bibliography:\n#+cite_export: foo nil a\n[cite:@a]"
  537. (let ((org-cite-export-processors '((t . (foo "global" "b"))))
  538. (org-cite--processors nil))
  539. (org-cite-register-processor 'foo
  540. :export-bibliography (lambda (_ _ s _ _ _) (throw :exit s))
  541. :export-citation #'ignore)
  542. (org-export-as (org-export-create-backend)))))))
  543. (ert-deftest test-org-cite/bibliography-properties ()
  544. "Test `org-cite-bibliography-properties'."
  545. ;; Return nil without properties.
  546. (should-not
  547. (org-test-with-parsed-data "#+print_bibliography:"
  548. (org-element-map tree 'keyword
  549. #'org-cite-bibliography-properties info t)))
  550. ;; Regular tests.
  551. (should
  552. (equal
  553. '(:key "value")
  554. (org-test-with-parsed-data "#+print_bibliography: :key value"
  555. (org-element-map tree 'keyword
  556. #'org-cite-bibliography-properties info t))))
  557. (should
  558. (equal
  559. '(:key "value" :key2 "value2")
  560. (org-test-with-parsed-data "#+print_bibliography: :key value :key2 value2"
  561. (org-element-map tree 'keyword
  562. #'org-cite-bibliography-properties info t))))
  563. ;; Allow empty values.
  564. (should
  565. (equal
  566. '(:key)
  567. (org-test-with-parsed-data "#+print_bibliography: :key"
  568. (org-element-map tree 'keyword
  569. #'org-cite-bibliography-properties info t))))
  570. (should
  571. (equal
  572. '(:key "")
  573. (org-test-with-parsed-data "#+print_bibliography: :key \"\""
  574. (org-element-map tree 'keyword
  575. #'org-cite-bibliography-properties info t))))
  576. ;; Allow space with double quotes.
  577. (should
  578. (equal
  579. '(:key "space space")
  580. (org-test-with-parsed-data "#+print_bibliography: :key \"space space\""
  581. (org-element-map tree 'keyword
  582. #'org-cite-bibliography-properties info t))))
  583. ;; Ignore spurious values.
  584. (should
  585. (equal
  586. '(:key "space")
  587. (org-test-with-parsed-data "#+print_bibliography: :key space space"
  588. (org-element-map tree 'keyword
  589. #'org-cite-bibliography-properties info t))))
  590. ;; Gracefully handle incomplete quotations.
  591. (should
  592. (equal
  593. '(:key "\"space" :key2 "value2")
  594. (org-test-with-parsed-data "#+print_bibliography: :key \"space :key2 value2"
  595. (org-element-map tree 'keyword
  596. #'org-cite-bibliography-properties info t)))))
  597. (ert-deftest test-org-cite/citation-style ()
  598. "Test `org-cite-citation-style'."
  599. ;; Extract style from global processor definition.
  600. (should
  601. (equal '("b")
  602. (catch :exit
  603. (org-test-with-temp-text "[cite:@a]"
  604. (let ((org-cite-export-processors '((t . (foo "a" "b"))))
  605. (org-cite--processors nil))
  606. (org-cite-register-processor 'foo
  607. :export-citation (lambda (_ s _ _) (throw :exit s)))
  608. (org-export-as (org-export-create-backend)))))))
  609. (should
  610. (equal '("b" . "variant")
  611. (catch :exit
  612. (org-test-with-temp-text "[cite:@a]"
  613. (let ((org-cite-export-processors '((t . (foo "a" "b/variant"))))
  614. (org-cite--processors nil))
  615. (org-cite-register-processor 'foo
  616. :export-citation (lambda (_ s _ _) (throw :exit s)))
  617. (org-export-as (org-export-create-backend)))))))
  618. ;; Extract style from local processor definition.
  619. (should
  620. (equal '("b")
  621. (catch :exit
  622. (org-test-with-temp-text "#+cite_export: foo a b\n[cite:@a]"
  623. (let ((org-cite-export-processors nil)
  624. (org-cite--processors nil))
  625. (org-cite-register-processor 'foo
  626. :export-citation (lambda (_ s _ _) (throw :exit s)))
  627. (org-export-as (org-export-create-backend)))))))
  628. (should
  629. (equal '("b c")
  630. (catch :exit
  631. (org-test-with-temp-text "#+cite_export: foo a \"b c\"\n[cite:@a]"
  632. (let ((org-cite-export-processors nil)
  633. (org-cite--processors nil))
  634. (org-cite-register-processor 'foo
  635. :export-citation (lambda (_ s _ _) (throw :exit s)))
  636. (org-export-as (org-export-create-backend)))))))
  637. (should
  638. (equal '("b" . "variant")
  639. (catch :exit
  640. (org-test-with-temp-text "#+cite_export: foo a b/variant\n[cite:@a]"
  641. (let ((org-cite-export-processors nil)
  642. (org-cite--processors nil))
  643. (org-cite-register-processor 'foo
  644. :export-citation (lambda (_ s _ _) (throw :exit s)))
  645. (org-export-as (org-export-create-backend)))))))
  646. (should
  647. (equal '("b c" . "variant")
  648. (catch :exit
  649. (org-test-with-temp-text
  650. "#+cite_export: foo a \"b c/variant\"\n[cite:@a]"
  651. (let ((org-cite-export-processors nil)
  652. (org-cite--processors nil))
  653. (org-cite-register-processor 'foo
  654. :export-citation (lambda (_ s _ _) (throw :exit s)))
  655. (org-export-as (org-export-create-backend)))))))
  656. ;; Extract style from citation itself.
  657. (should
  658. (equal '("b")
  659. (catch :exit
  660. (org-test-with-temp-text "[cite/b:@a]"
  661. (let ((org-cite-export-processors '((t . (foo nil nil))))
  662. (org-cite--processors nil))
  663. (org-cite-register-processor 'foo
  664. :export-citation (lambda (_ s _ _) (throw :exit s)))
  665. (org-export-as (org-export-create-backend)))))))
  666. (should
  667. (equal '("b" . "variant")
  668. (catch :exit
  669. (org-test-with-temp-text "[cite/b/variant:@a]"
  670. (let ((org-cite-export-processors '((t . (foo nil nil))))
  671. (org-cite--processors nil))
  672. (org-cite-register-processor 'foo
  673. :export-citation (lambda (_ s _ _) (throw :exit s)))
  674. (org-export-as (org-export-create-backend)))))))
  675. ;; Test priority: first object, then local.
  676. (should
  677. (equal '("object")
  678. (catch :exit
  679. (org-test-with-temp-text
  680. "#+cite_export: foo nil local\n[cite/object:@a]"
  681. (let ((org-cite-export-processors '((t . (foo nil "global"))))
  682. (org-cite--processors nil))
  683. (org-cite-register-processor 'foo
  684. :export-citation (lambda (_ s _ _) (throw :exit s)))
  685. (org-export-as (org-export-create-backend)))))))
  686. (should
  687. (equal '("local")
  688. (catch :exit
  689. (org-test-with-temp-text
  690. "#+cite_export: foo nil local\n[cite:@a]"
  691. (let ((org-cite-export-processors '((t . (foo nil "global"))))
  692. (org-cite--processors nil))
  693. (org-cite-register-processor 'foo
  694. :export-citation (lambda (_ s _ _) (throw :exit s)))
  695. (org-export-as (org-export-create-backend)))))))
  696. ;; Force default style with "nil".
  697. (should
  698. (equal '(nil)
  699. (catch :exit
  700. (org-test-with-temp-text
  701. "#+cite_export: foo nil nil\n[cite:@a]"
  702. (let ((org-cite-export-processors '((t . (foo nil "global"))))
  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. (should
  708. (equal '(nil)
  709. (catch :exit
  710. (org-test-with-temp-text "[cite/nil:@a]"
  711. (let ((org-cite-export-processors '((t . (foo nil "global"))))
  712. (org-cite--processors nil))
  713. (org-cite-register-processor 'foo
  714. :export-citation (lambda (_ s _ _) (throw :exit s)))
  715. (org-export-as (org-export-create-backend)))))))
  716. ;; Test variant inheritance.
  717. (should
  718. (equal '("local" . "v2")
  719. (catch :exit
  720. (org-test-with-temp-text "[cite/local/v2:@a]"
  721. (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
  722. (org-cite--processors nil))
  723. (org-cite-register-processor 'foo
  724. :export-citation (lambda (_ s _ _) (throw :exit s)))
  725. (org-export-as (org-export-create-backend)))))))
  726. (should
  727. (equal '("global" . "v2")
  728. (catch :exit
  729. (org-test-with-temp-text "[cite//v2:@a]"
  730. (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
  731. (org-cite--processors nil))
  732. (org-cite-register-processor 'foo
  733. :export-citation (lambda (_ s _ _) (throw :exit s)))
  734. (org-export-as (org-export-create-backend)))))))
  735. (should
  736. (equal '(nil . "v2")
  737. (catch :exit
  738. (org-test-with-temp-text "[cite/nil/v2:@a]"
  739. (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
  740. (org-cite--processors nil))
  741. (org-cite-register-processor 'foo
  742. :export-citation (lambda (_ s _ _) (throw :exit s)))
  743. (org-export-as (org-export-create-backend)))))))
  744. (should
  745. (equal '("local" . nil)
  746. (catch :exit
  747. (org-test-with-temp-text "[cite/local:@a]"
  748. (let ((org-cite-export-processors '((t . (foo nil "global/v1"))))
  749. (org-cite--processors nil))
  750. (org-cite-register-processor 'foo
  751. :export-citation (lambda (_ s _ _) (throw :exit s)))
  752. (org-export-as (org-export-create-backend))))))))
  753. (ert-deftest test-org-cite/read-processor-declaration ()
  754. "Test `org-cite-read-processor-declaration'."
  755. ;; Argument should contain 1-3 tokens.
  756. (should-error
  757. (org-cite-read-processor-declaration ""))
  758. (should
  759. (equal '(foo nil nil)
  760. (org-cite-read-processor-declaration "foo")))
  761. (should
  762. (equal '(foo "bar" nil)
  763. (org-cite-read-processor-declaration "foo bar")))
  764. (should
  765. (equal '(foo "bar" "baz")
  766. (org-cite-read-processor-declaration "foo bar baz")))
  767. (should-error
  768. (org-cite-read-processor-declaration "foo bar baz qux"))
  769. ;; nil in second and third arguments is read as `nil'.
  770. (should
  771. (equal '(foo nil "baz")
  772. (org-cite-read-processor-declaration "foo nil baz")))
  773. (should
  774. (equal '(foo "bar" nil)
  775. (org-cite-read-processor-declaration "foo bar nil")))
  776. ;; Second and third arguments may contain spaces if they are quoted.
  777. (should
  778. (equal '(foo "bar baz" nil)
  779. (org-cite-read-processor-declaration "foo \"bar baz\"")))
  780. (should
  781. (equal '(foo "bar" "baz qux")
  782. (org-cite-read-processor-declaration "foo bar \"baz qux\"")))
  783. ;; Spurious spaces are ignored.
  784. (should
  785. (equal '(foo "bar" "baz")
  786. (org-cite-read-processor-declaration " foo bar baz "))))
  787. (ert-deftest test-org-cite/list-citations ()
  788. "Test `org-cite-list-citations'."
  789. (should
  790. (equal '("a")
  791. (org-test-with-parsed-data "Test [cite:@a]"
  792. (cl-mapcan (lambda (c)
  793. (mapcar (lambda (ref)
  794. (org-element-property :key ref))
  795. (org-element-contents c)))
  796. (org-cite-list-citations info)))))
  797. (should
  798. (equal '("a" "b")
  799. (org-test-with-parsed-data "Test [cite:@a] [cite:@b]"
  800. (cl-mapcan (lambda (c)
  801. (mapcar (lambda (ref)
  802. (org-element-property :key ref))
  803. (org-element-contents c)))
  804. (org-cite-list-citations info)))))
  805. (should
  806. (equal '("a")
  807. (org-test-with-parsed-data "Test[fn:1]\n[fn:1] [cite:@a]"
  808. (cl-mapcan (lambda (c)
  809. (mapcar (lambda (ref)
  810. (org-element-property :key ref))
  811. (org-element-contents c)))
  812. (org-cite-list-citations info)))))
  813. (should
  814. (equal '("a" "b")
  815. (org-test-with-parsed-data "First[cite:@a] Second[fn:1]\n[fn:1] [cite:@b]"
  816. (cl-mapcan (lambda (c)
  817. (mapcar (lambda (ref)
  818. (org-element-property :key ref))
  819. (org-element-contents c)))
  820. (org-cite-list-citations info)))))
  821. (should
  822. (equal '("b" "a")
  823. (org-test-with-parsed-data "First[fn:1] Second[cite:@a]\n[fn:1] [cite:@b]"
  824. (cl-mapcan (lambda (c)
  825. (mapcar (lambda (ref)
  826. (org-element-property :key ref))
  827. (org-element-contents c)))
  828. (org-cite-list-citations info)))))
  829. (should
  830. (equal '("a" "b")
  831. (org-test-with-parsed-data
  832. "Text[fn:1][fn:2]\n[fn:1] [cite:@a]\n\n[fn:2] [cite:@b]"
  833. (cl-mapcan (lambda (c)
  834. (mapcar (lambda (ref)
  835. (org-element-property :key ref))
  836. (org-element-contents c)))
  837. (org-cite-list-citations info)))))
  838. (should
  839. (equal '("b" "a")
  840. (org-test-with-parsed-data
  841. "Text[fn:1]\n[fn:1] [fn:2][cite:@a]\n\n[fn:2] [cite:@b]"
  842. (cl-mapcan (lambda (c)
  843. (mapcar (lambda (ref)
  844. (org-element-property :key ref))
  845. (org-element-contents c)))
  846. (org-cite-list-citations info)))))
  847. (should
  848. (equal '("a" "b")
  849. (org-test-with-parsed-data
  850. "Text[fn:1]\n[fn:1] [cite:@a][fn:2]\n\n[fn:2] [cite:@b]"
  851. (cl-mapcan (lambda (c)
  852. (mapcar (lambda (ref)
  853. (org-element-property :key ref))
  854. (org-element-contents c)))
  855. (org-cite-list-citations info)))))
  856. (should
  857. (equal '("a")
  858. (org-test-with-parsed-data "Text[fn::[cite:@a]]"
  859. (cl-mapcan (lambda (c)
  860. (mapcar (lambda (ref)
  861. (org-element-property :key ref))
  862. (org-element-contents c)))
  863. (org-cite-list-citations info))))))
  864. (ert-deftest test-org-cite/list-keys ()
  865. "Test `org-cite-list-keys'."
  866. (should
  867. (equal '("a")
  868. (org-test-with-parsed-data "Test [cite:@a]"
  869. (org-cite-list-keys info))))
  870. (should
  871. (equal '("a" "b")
  872. (org-test-with-parsed-data "Test [cite:@a] [cite:@b]"
  873. (org-cite-list-keys info))))
  874. ;; Remove duplicates.
  875. (should
  876. (equal '("a")
  877. (org-test-with-parsed-data "Test [cite:@a] [cite:@a]"
  878. (org-cite-list-keys info))))
  879. ;; Keys are ordered by first appearance in the document.
  880. (should
  881. (equal '("a" "b")
  882. (org-test-with-parsed-data "Test [cite:@a] [cite:@b] [cite:@a]"
  883. (org-cite-list-keys info))))
  884. (should
  885. (equal '("a" "b" "c")
  886. (org-test-with-parsed-data
  887. "Test [cite:@a][fn:1] [cite:@c] [cite:@a]\n[fn:1] [cite:@b]"
  888. (org-cite-list-keys info)))))
  889. (ert-deftest test-org-cite/key-number ()
  890. "Test `org-cite-key-number'."
  891. (should
  892. (= 1 (org-test-with-parsed-data "[cite:@key]"
  893. (org-cite-key-number "key" info))))
  894. (should
  895. (equal '(1 2)
  896. (org-test-with-parsed-data "[cite:@key] [cite:@key2] [cite:@key]"
  897. (list (org-cite-key-number "key" info)
  898. (org-cite-key-number "key2" info)))))
  899. ;; When "predicate" is nil, keys are sorted by appearance order in
  900. ;; the buffer.
  901. (should
  902. (equal '((1 . "a") (2 . "c") (3 . "b"))
  903. (org-test-with-parsed-data
  904. "[cite:@a][fn:1] [cite:@b]\n[fn:1] [cite:@c]"
  905. (sort (mapcar (lambda (key)
  906. (cons (org-cite-key-number key info) key))
  907. '("a" "b" "c"))
  908. #'car-less-than-car))))
  909. (should
  910. (equal '((1 . "a") (2 . "b") (3 . "c"))
  911. (org-test-with-parsed-data
  912. "[cite:@a][fn:1] [cite:@b]\n[fn:1] [cite:@c]"
  913. (sort (mapcar (lambda (key)
  914. (cons (org-cite-key-number key info #'string<) key))
  915. '("a" "b" "c"))
  916. #'car-less-than-car)))))
  917. (ert-deftest test-org-cite/wrap-citation ()
  918. "Test `org-cite-wrap-citation'."
  919. ;; Reference test.
  920. (should
  921. (org-test-with-parsed-data "[cite:@key]"
  922. (org-element-map tree 'citation
  923. (lambda (c)
  924. (org-cite-wrap-citation c info)
  925. (org-cite-inside-footnote-p c))
  926. info)))
  927. ;; Created footnote is anonymous.
  928. (should-not
  929. (org-test-with-parsed-data "[cite:@key] "
  930. (org-element-map tree 'citation
  931. (lambda (c)
  932. (org-cite-wrap-citation c info)
  933. (org-element-property :label (org-cite-inside-footnote-p c)))
  934. info)))
  935. ;; Created footnote is inline.
  936. (should
  937. (equal '(inline)
  938. (org-test-with-parsed-data "[cite:@key]"
  939. (org-element-map tree 'citation
  940. (lambda (c)
  941. (org-cite-wrap-citation c info)
  942. (org-element-property :type
  943. (org-cite-inside-footnote-p c)))
  944. info))))
  945. ;; Preserve `:post-blank' property.
  946. (should
  947. (equal '(2)
  948. (org-test-with-parsed-data "[cite:@key] "
  949. (org-element-map tree 'citation
  950. (lambda (c)
  951. (org-cite-wrap-citation c info)
  952. (org-element-property :post-blank
  953. (org-cite-inside-footnote-p c)))
  954. info))))
  955. ;; Set `:post-blank' to 0 in the element before new footnote.
  956. (should-not
  957. (org-test-with-parsed-data "Text [cite:@key]"
  958. (org-element-map tree 'citation
  959. (lambda (c)
  960. (org-cite-wrap-citation c info)
  961. (let ((previous
  962. (org-export-get-previous-element
  963. (org-cite-inside-footnote-p c) info)))
  964. (string-match (rx blank string-end) previous)))
  965. info)))
  966. (should
  967. (equal '(0)
  968. (org-test-with-parsed-data "*Text* [cite:@key]"
  969. (org-element-map tree 'citation
  970. (lambda (c)
  971. (org-cite-wrap-citation c info)
  972. (let ((previous
  973. (org-export-get-previous-element
  974. (org-cite-inside-footnote-p c) info)))
  975. (org-element-property :post-blank previous)))
  976. info))))
  977. (should
  978. (equal '("Text")
  979. (org-test-with-parsed-data "Text [cite:@key]"
  980. (org-element-map tree 'citation
  981. (lambda (c)
  982. (org-cite-wrap-citation c info)
  983. (org-export-get-previous-element
  984. (org-cite-inside-footnote-p c) info))
  985. info)))))
  986. (defun test-org-cite--export-with-rule (text &optional rule punct)
  987. "Export TEXT string using RULE for punctuation positioning.
  988. Call `org-cite-adjust-note' on each citation object with RULE and, PUNCT
  989. arguments. Replace citation with \"@\" character in the output."
  990. (org-test-with-temp-text text
  991. (let ((org-cite--processors nil))
  992. (org-cite-register-processor 'test
  993. :export-citation
  994. (lambda (citation _s _b info)
  995. (org-cite-adjust-note citation info rule punct)
  996. "@"))
  997. (let ((org-cite-export-processors '((t . (test nil nil)))))
  998. (org-trim
  999. (org-export-as
  1000. (org-export-create-backend
  1001. :transcoders
  1002. '((section . (lambda (_s c _i) (replace-regexp-in-string " @" "@" c)))
  1003. (paragraph . (lambda (_s c _i) c))))))))))
  1004. (ert-deftest test-org-cite/adjust-note ()
  1005. "Test `org-cite-adjust-note' function."
  1006. ;; Basic tests for all rules. In the output, @ replaces citation.
  1007. (let ((cases '("\"[cite:@k]!"
  1008. ".\"[cite:@k]!"
  1009. "\"[cite:@k]"
  1010. ".\"[cite:@k]"
  1011. ".[cite:@k]"
  1012. "[cite:@k]!")))
  1013. (should ;test (inside inside after)
  1014. (equal
  1015. '(iia "!@\"" ".@\"!" "@\"" ".@\"" ".@" "!@")
  1016. (cons 'iia
  1017. (mapcar (lambda (c)
  1018. (test-org-cite--export-with-rule
  1019. c '(inside inside after)))
  1020. cases))))
  1021. (should ;test (inside inside before)
  1022. (equal
  1023. '(iib "@!\"" "@.\"!" "@\"" "@.\"" "@." "@!")
  1024. (cons 'iib
  1025. (mapcar (lambda (c)
  1026. (test-org-cite--export-with-rule
  1027. c '(inside inside before)))
  1028. cases))))
  1029. (should ;test (inside outside after)
  1030. (equal
  1031. '(ioa "!\"@" ".\"!@" "\"@" ".\"@" ".@" "!@")
  1032. (cons 'ioa
  1033. (mapcar (lambda (c)
  1034. (test-org-cite--export-with-rule
  1035. c '(inside outside after)))
  1036. cases))))
  1037. (should ;test (inside outside before)
  1038. (equal
  1039. '(iob "!\"@" ".\"@!" "\"@" ".\"@" "@." "@!")
  1040. (cons 'iob
  1041. (mapcar (lambda (c)
  1042. (test-org-cite--export-with-rule
  1043. c '(inside outside before)))
  1044. cases))))
  1045. (should ;test (inside same after)
  1046. (equal
  1047. '(isa "!@\"" ".\"!@" "\"@" ".@\"" ".@" "!@")
  1048. (cons 'isa
  1049. (mapcar (lambda (c)
  1050. (test-org-cite--export-with-rule
  1051. c '(inside same after)))
  1052. cases))))
  1053. (should ;test (inside same before)
  1054. (equal
  1055. '(isb "@!\"" ".\"@!" "\"@" "@.\"" "@." "@!")
  1056. (cons 'isb
  1057. (mapcar (lambda (c)
  1058. (test-org-cite--export-with-rule
  1059. c '(inside same before)))
  1060. cases))))
  1061. (should ;test (outside inside after)
  1062. (equal
  1063. '(oia "@\"!" ".@\"!" "@\"" "@\"." ".@" "!@")
  1064. (cons 'oia
  1065. (mapcar (lambda (c)
  1066. (test-org-cite--export-with-rule
  1067. c '(outside inside after)))
  1068. cases))))
  1069. (should ;test (outside inside before)
  1070. (equal
  1071. '(oib "@\"!" "@.\"!" "@\"" "@\"." "@." "@!")
  1072. (cons 'oib
  1073. (mapcar (lambda (c)
  1074. (test-org-cite--export-with-rule
  1075. c '(outside inside before)))
  1076. cases))))
  1077. (should ;test (outside outside after)
  1078. (equal
  1079. '(ooa "\"!@" ".\"!@" "\"@" "\".@" ".@" "!@")
  1080. (cons 'ooa
  1081. (mapcar (lambda (c)
  1082. (test-org-cite--export-with-rule
  1083. c '(outside outside after)))
  1084. cases))))
  1085. (should ;test (outside outside before)
  1086. (equal
  1087. '(oob "\"@!" ".\"@!" "\"@" "\"@." "@." "@!")
  1088. (cons 'oob
  1089. (mapcar (lambda (c)
  1090. (test-org-cite--export-with-rule
  1091. c '(outside outside before)))
  1092. cases))))
  1093. (should ;test (outside same after)
  1094. (equal
  1095. '(osa "\"!@" ".\"!@" "\"@" "\".@" ".@" "!@")
  1096. (cons 'osa
  1097. (mapcar (lambda (c)
  1098. (test-org-cite--export-with-rule
  1099. c '(outside same after)))
  1100. cases))))
  1101. (should ;test (outside same before)
  1102. (equal
  1103. '(osb "\"@!" ".\"@!" "\"@" "\"@." "@." "@!")
  1104. (cons 'osb
  1105. (mapcar (lambda (c)
  1106. (test-org-cite--export-with-rule
  1107. c '(outside same before)))
  1108. cases)))))
  1109. ;; Test `adaptive' behaviour.
  1110. (should
  1111. (equal "@\"."
  1112. (test-org-cite--export-with-rule ".\" [cite:@k]"
  1113. '(adaptive inside after))))
  1114. (should
  1115. (equal "@\"!"
  1116. (test-org-cite--export-with-rule "\" [cite:@k]!"
  1117. '(adaptive inside after))))
  1118. (should
  1119. (equal ".@\""
  1120. (test-org-cite--export-with-rule ".\"[cite:@k]"
  1121. '(adaptive inside after))))
  1122. (should
  1123. (equal "!@\""
  1124. (test-org-cite--export-with-rule "\"[cite:@k]!"
  1125. '(adaptive inside after))))
  1126. ;; Handle white space when inserting citation before quotation mark
  1127. ;; or punctuation.
  1128. (should
  1129. (equal ",@\" next"
  1130. (test-org-cite--export-with-rule ",\" [cite:@k] next"
  1131. '(inside inside after))))
  1132. (should
  1133. (equal "@,\" next"
  1134. (test-org-cite--export-with-rule ",\" [cite:@k] next"
  1135. '(inside inside before))))
  1136. (should
  1137. (equal "@\"."
  1138. (test-org-cite--export-with-rule "\" [cite:@k]."
  1139. '(outside inside before))))
  1140. (should
  1141. (equal "@\" !"
  1142. (test-org-cite--export-with-rule "\" [cite:@k] !"
  1143. '(outside inside before))))
  1144. (should
  1145. (equal "text@ !"
  1146. (test-org-cite--export-with-rule "text ![cite:@k]"
  1147. '(inside outside before))))
  1148. ;; Preserve white space between citation and final punctuation when
  1149. ;; moving citation past final punctuation.
  1150. (should
  1151. (equal "text !@"
  1152. (test-org-cite--export-with-rule "text [cite:@k] !"
  1153. '(inside inside after))))
  1154. (should
  1155. (equal "text\n !@"
  1156. (test-org-cite--export-with-rule "text [cite:@k]\n !"
  1157. '(inside inside after))))
  1158. ;; Choose punctuation with optional argument.
  1159. (should-not
  1160. (equal "!@"
  1161. (test-org-cite--export-with-rule "[cite:@k]!"
  1162. '(inside outside after)
  1163. '("."))))
  1164. (should
  1165. (equal ".@"
  1166. (test-org-cite--export-with-rule "[cite:@k]."
  1167. '(inside outside after)
  1168. '(".")))))
  1169. (ert-deftest test-org-cite/parse-elements ()
  1170. "Test `org-cite-parse-elements' function."
  1171. (should-error (org-cite-parse-elements "* H"))
  1172. (should-error (org-cite-parse-elements "Paragraph\n* H"))
  1173. (should
  1174. (equal '(paragraph)
  1175. (mapcar #'org-element-type (org-cite-parse-elements "s"))))
  1176. (should
  1177. (equal '(paragraph paragraph)
  1178. (mapcar #'org-element-type (org-cite-parse-elements "Text\n\nText")))))
  1179. (ert-deftest test-org-cite/parse-objects ()
  1180. "Test `org-cite-parse-objects' function."
  1181. (should
  1182. (equal '(plain-text)
  1183. (mapcar #'org-element-type (org-cite-parse-objects "s"))))
  1184. (should
  1185. (equal '(plain-text bold)
  1186. (mapcar #'org-element-type (org-cite-parse-objects "s *b*"))))
  1187. (should
  1188. (equal '(link)
  1189. (mapcar #'org-element-type (org-cite-parse-objects "[[link]]"))))
  1190. ;; When optional argument is non-nil, only recognize types allowed
  1191. ;; in as a citation reference affix.
  1192. (should-not
  1193. (equal '(link)
  1194. (mapcar #'org-element-type (org-cite-parse-objects "[[link]]" t))))
  1195. (should
  1196. (equal '(bold)
  1197. (mapcar #'org-element-type (org-cite-parse-objects "*b*" t)))))
  1198. (ert-deftest test-org-cite/make-paragraph ()
  1199. "Test `org-cite-make-paragraph' function."
  1200. ;; Check string as argument.
  1201. (should
  1202. (eq 'paragraph
  1203. (org-element-type (org-cite-make-paragraph "a"))))
  1204. (should
  1205. (equal '("a")
  1206. (org-element-contents (org-cite-make-paragraph "a"))))
  1207. ;; Check object as argument.
  1208. (should
  1209. (eq 'paragraph
  1210. (org-element-type
  1211. (org-cite-make-paragraph (org-element-create 'bold nil "b")))))
  1212. (should
  1213. (equal '(bold)
  1214. (mapcar #'org-element-type
  1215. (org-element-contents
  1216. (org-cite-make-paragraph (org-element-create 'bold nil "b"))))))
  1217. ;; Check secondary string as argument.
  1218. (should
  1219. (eq 'paragraph
  1220. (org-element-type (org-cite-make-paragraph '("a")))))
  1221. (should
  1222. (equal '("a")
  1223. (org-element-contents (org-cite-make-paragraph '("a")))))
  1224. ;; Mix all types of arguments.
  1225. (should
  1226. (equal '(plain-text bold plain-text)
  1227. (mapcar #'org-element-type
  1228. (org-element-contents
  1229. (org-cite-make-paragraph
  1230. "a" (org-element-create 'bold nil "b") '("c"))))))
  1231. ;; Check `:parent' property.
  1232. (should
  1233. (eq 'paragraph
  1234. (org-element-type
  1235. (org-element-property
  1236. :parent
  1237. (car (org-element-contents (org-cite-make-paragraph "a"))))))))
  1238. (ert-deftest test-org-cite/emphasize ()
  1239. "Test `org-cite-emphasize' function."
  1240. ;; Raise an error if first argument has wrong type.
  1241. (should-error (org-cite-emphasize 'code "a"))
  1242. ;; Check string argument.
  1243. (should (eq 'bold (org-element-type (org-cite-emphasize 'bold "a"))))
  1244. (should (equal '("a") (org-element-contents (org-cite-emphasize 'bold "a"))))
  1245. ;; Check object argument.
  1246. (should
  1247. (eq 'bold
  1248. (org-element-type
  1249. (org-cite-emphasize 'bold (org-element-create 'bold nil "a")))))
  1250. (should
  1251. (equal '(italic)
  1252. (mapcar #'org-element-type
  1253. (org-element-contents
  1254. (org-cite-emphasize 'bold
  1255. (org-element-create 'italic nil "a"))))))
  1256. ;; Check secondary string argument.
  1257. (should (eq 'bold (org-element-type (org-cite-emphasize 'bold '("a")))))
  1258. (should (equal '("a") (org-element-contents (org-cite-emphasize 'bold '("a")))))
  1259. ;; Mix all types of arguments.
  1260. (should
  1261. (equal '(plain-text italic plain-text)
  1262. (mapcar #'org-element-type
  1263. (org-element-contents
  1264. (org-cite-emphasize 'bold
  1265. "a" (org-element-create 'italic nil "b") '("c"))))))
  1266. ;; Check `:parent' property.
  1267. (should
  1268. (eq 'bold
  1269. (org-element-type
  1270. (org-element-property
  1271. :parent
  1272. (car (org-element-contents (org-cite-emphasize 'bold "a"))))))))
  1273. (ert-deftest test-org-cite/concat ()
  1274. "Test `org-cite-concat' function."
  1275. ;; Return nil when there is no data.
  1276. (should
  1277. (equal "" (org-element-interpret-data (org-cite-concat))))
  1278. ;; Concatenate strings, objects and secondary strings.
  1279. (should
  1280. (equal "ab"
  1281. (org-element-interpret-data (org-cite-concat "a" "b"))))
  1282. (should
  1283. (equal "*a* b"
  1284. (org-element-interpret-data
  1285. (org-cite-concat (org-element-create 'bold nil "a") " b"))))
  1286. (should
  1287. (equal "*a* b"
  1288. (org-element-interpret-data
  1289. (org-cite-concat
  1290. (list (org-element-create 'bold nil "a")) " b"))))
  1291. ;; Return an error for any other object type.
  1292. (should-error (org-cite-concat 2)))
  1293. (ert-deftest test-org-cite/mapconcat ()
  1294. "Test `org-cite-mapconcat' function."
  1295. (should
  1296. (equal ""
  1297. (org-element-interpret-data
  1298. (org-cite-mapconcat #'identity nil ""))))
  1299. (should
  1300. (equal "ab"
  1301. (org-element-interpret-data
  1302. (org-cite-mapconcat #'identity '("a" "b") ""))))
  1303. (should
  1304. (equal "*a* b *c*"
  1305. (org-element-interpret-data
  1306. (org-cite-mapconcat
  1307. #'identity
  1308. (list (org-element-create 'bold nil "a")
  1309. (list " b " (org-element-create 'bold nil "c"))) ""))))
  1310. (should
  1311. (equal "*a* *b*"
  1312. (org-element-interpret-data
  1313. (org-cite-mapconcat
  1314. (lambda (s) (org-element-create 'bold nil s))
  1315. '("a" "b") " "))))
  1316. (should
  1317. (equal "*a* b*c*"
  1318. (org-element-interpret-data
  1319. (org-cite-mapconcat
  1320. #'identity
  1321. (list (org-element-create 'bold nil "a")
  1322. (list "b" (org-element-create 'bold nil "c"))) " ")))))
  1323. ;;; Test capabilities.
  1324. (ert-deftest test-org-cite/activate-capability ()
  1325. "Test \"activate\" capability."
  1326. ;; Standard test.
  1327. (should
  1328. (eq 'success
  1329. (catch :exit
  1330. (org-test-with-temp-text "[cite:@key]"
  1331. (let ((org-cite--processors nil)
  1332. (org-cite-activate-processor 'foo))
  1333. (org-cite-register-processor 'foo
  1334. :activate (lambda (_) (throw :exit 'success)))
  1335. (font-lock-ensure))))))
  1336. ;; If there is no "follow" processor, or if processor does not
  1337. ;; handle this capability, fall back to fontifying whole citation
  1338. ;; with `org-cite' face and each key with `org-cite-key' face.
  1339. (should
  1340. (eq 'org-cite
  1341. (org-test-with-temp-text "[cite:@key]"
  1342. (let ((org-cite-activate-processor nil))
  1343. (font-lock-ensure)
  1344. (face-at-point)))))
  1345. (should
  1346. (eq 'org-cite-key
  1347. (org-test-with-temp-text "[cite:@<point>key]"
  1348. (let ((org-cite-activate-processor nil))
  1349. (font-lock-ensure)
  1350. (face-at-point)))))
  1351. (should
  1352. (eq 'org-cite
  1353. (org-test-with-temp-text "[cite:@key]"
  1354. (let ((org-cite--processors nil)
  1355. (org-cite-activate-processor 'foo))
  1356. (org-cite-register-processor 'foo)
  1357. (font-lock-ensure)
  1358. (face-at-point))))))
  1359. (ert-deftest test-org-cite/export-capability ()
  1360. "Test \"export\" capability."
  1361. ;; Regular citations export.
  1362. (should
  1363. (eq 'success
  1364. (catch :exit
  1365. (org-test-with-temp-text "[cite:@key]"
  1366. (let ((org-cite--processors nil)
  1367. (org-cite-export-processors '((t . (foo nil nil)))))
  1368. (org-cite-register-processor 'foo
  1369. :export-citation (lambda (&rest _) (throw :exit 'success)))
  1370. (org-export-as (org-export-create-backend)))))))
  1371. ;; Export citation as string.
  1372. (should
  1373. (equal "citation\n"
  1374. (org-test-with-temp-text "[cite:@key]"
  1375. (let ((org-cite--processors nil)
  1376. (org-cite-export-processors '((t . (foo nil nil)))))
  1377. (org-cite-register-processor 'foo
  1378. :export-citation (lambda (&rest _) "citation"))
  1379. (org-export-as (org-export-create-backend
  1380. :transcoders
  1381. '((section . (lambda (_ c _) c))
  1382. (paragraph . (lambda (_ c _) c)))))))))
  1383. ;; Export citation as parsed object.
  1384. (should
  1385. (equal "success\n"
  1386. (org-test-with-temp-text "[cite:@key]"
  1387. (let ((org-cite--processors nil)
  1388. (org-cite-export-processors '((t . (foo nil nil)))))
  1389. (org-cite-register-processor 'foo
  1390. :export-citation (lambda (&rest _)
  1391. (org-element-create 'bold nil "cite")))
  1392. (org-export-as (org-export-create-backend
  1393. :transcoders
  1394. '((section . (lambda (_ c _) c))
  1395. (paragraph . (lambda (_ c _) c))
  1396. (bold . (lambda (&rest _) "success")))))))))
  1397. ;; Export citation as a secondary string.
  1398. (should
  1399. (equal "boldtwo\n"
  1400. (org-test-with-temp-text "[cite:@key]"
  1401. (let ((org-cite--processors nil)
  1402. (org-cite-export-processors '((t . (foo nil nil)))))
  1403. (org-cite-register-processor 'foo
  1404. :export-citation (lambda (&rest _)
  1405. (list (org-element-create 'bold nil "one")
  1406. "two")))
  1407. (org-export-as (org-export-create-backend
  1408. :transcoders
  1409. '((section . (lambda (_ c _) c))
  1410. (paragraph . (lambda (_ c _) c))
  1411. (bold . (lambda (&rest _) "bold")))))))))
  1412. ;; When exporting citation as a secondary string, last object
  1413. ;; inherits post-blank from initial citation.
  1414. (should
  1415. (equal "twobold one-space\n"
  1416. (org-test-with-temp-text "[cite:@key] one-space"
  1417. (let ((org-cite--processors nil)
  1418. (org-cite-export-processors '((t . (foo nil nil)))))
  1419. (org-cite-register-processor 'foo
  1420. :export-citation (lambda (&rest _)
  1421. (list "two"
  1422. (org-element-create 'bold nil "one"))))
  1423. (org-export-as (org-export-create-backend
  1424. :transcoders
  1425. '((section . (lambda (_ c _) c))
  1426. (paragraph . (lambda (_ c _) c))
  1427. (bold . (lambda (&rest _) "bold")))))))))
  1428. (should
  1429. (equal "boldtwo one-space\n"
  1430. (org-test-with-temp-text "[cite:@key] one-space"
  1431. (let ((org-cite--processors nil)
  1432. (org-cite-export-processors '((t . (foo nil nil)))))
  1433. (org-cite-register-processor 'foo
  1434. :export-citation (lambda (&rest _)
  1435. (list (org-element-create 'bold nil "one")
  1436. "two")))
  1437. (org-export-as (org-export-create-backend
  1438. :transcoders
  1439. '((section . (lambda (_ c _) c))
  1440. (paragraph . (lambda (_ c _) c))
  1441. (bold . (lambda (&rest _) "bold")))))))))
  1442. ;; Make sure to have a space between a quote and a citation.
  1443. (should
  1444. (equal "\"quotation\" citation\n"
  1445. (org-test-with-temp-text "\"quotation\"[cite:@key]"
  1446. (let ((org-cite--processors nil)
  1447. (org-cite-export-processors '((t . (foo nil nil)))))
  1448. (org-cite-register-processor 'foo
  1449. :export-citation (lambda (&rest _) "citation"))
  1450. (org-export-as (org-export-create-backend
  1451. :transcoders
  1452. '((section . (lambda (_ c _) c))
  1453. (paragraph . (lambda (_ c _) c)))))))))
  1454. (should
  1455. (equal "\"quotation\" citation\n"
  1456. (org-test-with-temp-text "\"quotation\" [cite:@key]"
  1457. (let ((org-cite--processors nil)
  1458. (org-cite-export-processors '((t . (foo nil nil)))))
  1459. (org-cite-register-processor 'foo
  1460. :export-citation (lambda (&rest _) "citation"))
  1461. (org-export-as (org-export-create-backend
  1462. :transcoders
  1463. '((section . (lambda (_ c _) c))
  1464. (paragraph . (lambda (_ c _) c)))))))))
  1465. ;; Regular bibliography export.
  1466. (should
  1467. (eq 'success
  1468. (catch :exit
  1469. (org-test-with-temp-text "#+print_bibliography:"
  1470. (let ((org-cite--processors nil)
  1471. (org-cite-export-processors '((t . (foo nil nil)))))
  1472. (org-cite-register-processor 'foo
  1473. :export-bibliography (lambda (&rest _) (throw :exit 'success))
  1474. :export-citation #'ignore)
  1475. (org-export-as (org-export-create-backend)))))))
  1476. (should
  1477. (equal ""
  1478. (org-test-with-temp-text "#+print_bibliography:"
  1479. (let ((org-cite--processors nil)
  1480. (org-cite-export-processors '((t . (foo nil nil)))))
  1481. (org-cite-register-processor 'foo
  1482. :export-citation #'ignore)
  1483. (org-export-as (org-export-create-backend
  1484. :transcoders
  1485. '((section . (lambda (_ c _) c))
  1486. (paragraph . (lambda (_ c _) c)))))))))
  1487. ;; Export bibliography as string.
  1488. (should
  1489. (equal "bibliography\n"
  1490. (org-test-with-temp-text "#+print_bibliography:"
  1491. (let ((org-cite--processors nil)
  1492. (org-cite-export-processors '((t . (foo nil nil)))))
  1493. (org-cite-register-processor 'foo
  1494. :export-bibliography (lambda (&rest _) "bibliography")
  1495. :export-citation #'ignore)
  1496. (org-export-as (org-export-create-backend
  1497. :transcoders
  1498. '((section . (lambda (_ c _) c))
  1499. (paragraph . (lambda (_ c _) c)))))))))
  1500. ;; Export bibliography as a parsed element.
  1501. (should
  1502. (equal "success\n"
  1503. (org-test-with-temp-text "#+print_bibliography:"
  1504. (let ((org-cite--processors nil)
  1505. (org-cite-export-processors '((t . (foo nil nil)))))
  1506. (org-cite-register-processor 'foo
  1507. :export-bibliography
  1508. (lambda (&rest _)
  1509. (org-element-create 'example-block '(:value "foo")))
  1510. :export-citation #'ignore)
  1511. (org-export-as
  1512. (org-export-create-backend
  1513. :transcoders
  1514. '((section . (lambda (_ c _) c))
  1515. (example-block . (lambda (&rest _) "success")))))))))
  1516. ;; Export bibliography as a list of parsed elements.
  1517. (should
  1518. (equal "success\nsuccess\n"
  1519. (org-test-with-temp-text "#+print_bibliography:"
  1520. (let ((org-cite--processors nil)
  1521. (org-cite-export-processors '((t . (foo nil nil)))))
  1522. (org-cite-register-processor 'foo
  1523. :export-bibliography
  1524. (lambda (&rest _)
  1525. (list (org-element-create 'example-block '(:value "foo"))
  1526. (org-element-create 'example-block '(:value "bar"))))
  1527. :export-citation #'ignore)
  1528. (org-export-as
  1529. (org-export-create-backend
  1530. :transcoders
  1531. '((section . (lambda (_ c _) c))
  1532. (example-block . (lambda (&rest _) "success")))))))))
  1533. ;; When exporting bibliography as a list of parsed elements, the
  1534. ;; last element inherits post-blank from initial keyword.
  1535. (should
  1536. (equal "success\nsuccess\n\nText\n"
  1537. (org-test-with-temp-text "#+print_bibliography:\n\nText"
  1538. (let ((org-cite--processors nil)
  1539. (org-cite-export-processors '((t . (foo nil nil)))))
  1540. (org-cite-register-processor 'foo
  1541. :export-bibliography
  1542. (lambda (&rest _)
  1543. (list (org-element-create 'example-block '(:value "foo"))
  1544. (org-element-create 'example-block '(:value "bar"))))
  1545. :export-citation #'ignore)
  1546. (org-export-as
  1547. (org-export-create-backend
  1548. :transcoders
  1549. '((section . (lambda (_ c _) c))
  1550. (example-block . (lambda (&rest _) "success"))
  1551. (paragraph . (lambda (_ c _) c)))))))))
  1552. ;; Use more appropriate citation processor.
  1553. (should
  1554. (equal
  1555. '(p1 p1 p1 p3)
  1556. (org-test-with-temp-text "[cite:@a]"
  1557. (let ((org-export-registered-backends nil)
  1558. (org-cite--processors nil)
  1559. (org-cite-export-processors
  1560. '((b1 . (p1))
  1561. (t . (p3)))))
  1562. (org-cite-register-processor 'p1
  1563. :export-citation (lambda (&rest _) (throw :exit 'p1)))
  1564. (org-cite-register-processor 'p2
  1565. :export-citation (lambda (&rest _) (throw :exit 'p2)))
  1566. (org-cite-register-processor 'p3
  1567. :export-citation (lambda (&rest _) (throw :exit 'p3)))
  1568. (org-export-define-backend 'b1 nil)
  1569. (org-export-define-derived-backend 'b2 'b1)
  1570. (org-export-define-derived-backend 'b3 'b2)
  1571. (list (catch :exit (org-export-as 'b1))
  1572. (catch :exit (org-export-as 'b2))
  1573. (catch :exit (org-export-as 'b3))
  1574. (catch :exit (org-export-as (org-export-create-backend))))))))
  1575. (should
  1576. (eq 'p2
  1577. (org-test-with-temp-text "#+cite_export: p2\n[cite:@a]"
  1578. (let ((org-export-registered-backends nil)
  1579. (org-cite--processors nil)
  1580. (org-cite-export-processors '((t . (p1)))))
  1581. (org-cite-register-processor 'p1
  1582. :export-citation (lambda (&rest _) (throw :exit 'p1)))
  1583. (org-cite-register-processor 'p2
  1584. :export-citation (lambda (&rest _) (throw :exit 'p2)))
  1585. (catch :exit (org-export-as (org-export-create-backend)))))))
  1586. ;; Test finalizer.
  1587. (should
  1588. (eq 'success
  1589. (catch :exit
  1590. (org-test-with-temp-text "[cite:@key]"
  1591. (let ((org-cite--processors nil)
  1592. (org-cite-export-processors '((t . (foo nil nil)))))
  1593. (org-cite-register-processor 'foo
  1594. :export-citation (lambda (&rest _) "")
  1595. :export-finalizer (lambda (&rest _) (throw :exit 'success)))
  1596. (org-export-as (org-export-create-backend)))))))
  1597. (should
  1598. (equal "finalized!"
  1599. (org-test-with-temp-text "[cite:@key]"
  1600. (let ((org-cite--processors nil)
  1601. (org-cite-export-processors '((t . (foo nil nil)))))
  1602. (org-cite-register-processor 'foo
  1603. :export-citation #'ignore
  1604. :export-finalizer (lambda (&rest _) "finalized!"))
  1605. (org-export-as (org-export-create-backend))))))
  1606. ;; Ignore citations when there is no selected "export" processor.
  1607. ;; In that case, white space is removed before the citation, not
  1608. ;; after.
  1609. (should
  1610. (equal ""
  1611. (org-test-with-temp-text "[cite:@key]"
  1612. (let ((org-cite-export-processors nil))
  1613. (org-export-as (org-export-create-backend
  1614. :transcoders
  1615. '((section . (lambda (_ c _) c))
  1616. (paragraph . (lambda (_ c _) c)))))))))
  1617. (should
  1618. (equal "Text.\n"
  1619. (org-test-with-temp-text "Text [cite:@key]."
  1620. (let ((org-cite-export-processors nil))
  1621. (org-export-as (org-export-create-backend
  1622. :transcoders
  1623. '((section . (lambda (_ c _) c))
  1624. (paragraph . (lambda (_ c _) c)))))))))
  1625. ;; Throw an error if selected processor does not handle "export"
  1626. ;; capability.
  1627. (should-error
  1628. (org-test-with-temp-text "[cite:@key]"
  1629. (let ((org-cite--processors nil)
  1630. (org-cite-export-processors '((t . (foo nil nil)))))
  1631. (org-cite-register-processor 'foo)
  1632. (org-export-as (org-export-create-backend))))))
  1633. (ert-deftest test-org-cite/follow-capability ()
  1634. "Test \"follow\" capability."
  1635. ;; Standard test.
  1636. (should
  1637. (eq 'success
  1638. (catch :exit
  1639. (org-test-with-temp-text "[cite:@key]"
  1640. (let ((org-cite--processors nil)
  1641. (org-cite-follow-processor 'foo))
  1642. (org-cite-register-processor 'foo
  1643. :follow (lambda (_ _) (throw :exit 'success)))
  1644. (org-open-at-point))))))
  1645. ;; Throw an error if there is no "follow" processor, or if it is
  1646. ;; unable to follow a citation.
  1647. (should-error
  1648. (org-test-with-temp-text "[cite:@key]"
  1649. (let ((org-cite-follow-processor nil))
  1650. (org-open-at-point))))
  1651. (should-error
  1652. (org-test-with-temp-text "[cite:@key]"
  1653. (let ((org-cite--processors nil)
  1654. (org-cite-follow-processor 'foo))
  1655. (org-cite-register-processor 'foo)
  1656. (org-open-at-point)))))
  1657. (ert-deftest test-org-cite/make-insert-processor ()
  1658. "Test `org-cite-make-insert-processor'."
  1659. (should-error (org-cite-make-insert-processor 1 2))
  1660. (should-error
  1661. (org-test-with-temp-text "[cite:@<point>a]"
  1662. (let ((org-cite--processors nil)
  1663. (org-cite-insert-processor 'foo))
  1664. (org-cite-register-processor 'foo
  1665. :insert (org-cite-make-insert-processor
  1666. #'ignore (lambda (&rest _) "s")))
  1667. (org-cite-insert nil))))
  1668. (should
  1669. (equal "[cite:@k]"
  1670. (org-test-with-temp-text "[cite:@<point>a]"
  1671. (let ((org-cite--processors nil)
  1672. (org-cite-insert-processor 'foo))
  1673. (org-cite-register-processor 'foo
  1674. :insert (org-cite-make-insert-processor
  1675. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1676. (org-cite-insert nil)
  1677. (buffer-string)))))
  1678. (should
  1679. (equal "[cite:@k;@a]"
  1680. (org-test-with-temp-text "[cite:<point>@a]"
  1681. (let ((org-cite--processors nil)
  1682. (org-cite-insert-processor 'foo))
  1683. (org-cite-register-processor 'foo
  1684. :insert (org-cite-make-insert-processor
  1685. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1686. (org-cite-insert nil)
  1687. (buffer-string)))))
  1688. (should
  1689. (equal "[cite:@k;pre @a]"
  1690. (org-test-with-temp-text "[cite:<point>pre @a]"
  1691. (let ((org-cite--processors nil)
  1692. (org-cite-insert-processor 'foo))
  1693. (org-cite-register-processor 'foo
  1694. :insert (org-cite-make-insert-processor
  1695. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1696. (org-cite-insert nil)
  1697. (buffer-string)))))
  1698. (should
  1699. (equal "[cite:pre;@k;@a]"
  1700. (org-test-with-temp-text "[cite:<point>pre;@a]"
  1701. (let ((org-cite--processors nil)
  1702. (org-cite-insert-processor 'foo))
  1703. (org-cite-register-processor 'foo
  1704. :insert (org-cite-make-insert-processor
  1705. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1706. (org-cite-insert nil)
  1707. (buffer-string)))))
  1708. (should
  1709. (equal "[cite:@a;@k]"
  1710. (org-test-with-temp-text "[cite:@a<point>]"
  1711. (let ((org-cite--processors nil)
  1712. (org-cite-insert-processor 'foo))
  1713. (org-cite-register-processor 'foo
  1714. :insert (org-cite-make-insert-processor
  1715. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1716. (org-cite-insert nil)
  1717. (buffer-string)))))
  1718. (should
  1719. (equal "[cite:@a post;@k]"
  1720. (org-test-with-temp-text "[cite:@a post<point>]"
  1721. (let ((org-cite--processors nil)
  1722. (org-cite-insert-processor 'foo))
  1723. (org-cite-register-processor 'foo
  1724. :insert (org-cite-make-insert-processor
  1725. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1726. (org-cite-insert nil)
  1727. (buffer-string)))))
  1728. (should
  1729. (equal "[cite:@a;@k;post]"
  1730. (org-test-with-temp-text "[cite:@a;post<point>]"
  1731. (let ((org-cite--processors nil)
  1732. (org-cite-insert-processor 'foo))
  1733. (org-cite-register-processor 'foo
  1734. :insert (org-cite-make-insert-processor
  1735. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1736. (org-cite-insert nil)
  1737. (buffer-string)))))
  1738. (should
  1739. (equal ""
  1740. (org-test-with-temp-text "[cite:@<point>a]"
  1741. (let ((org-cite--processors nil)
  1742. (org-cite-insert-processor 'foo))
  1743. (org-cite-register-processor 'foo
  1744. :insert (org-cite-make-insert-processor
  1745. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1746. (org-cite-insert t)
  1747. (buffer-string)))))
  1748. (should
  1749. (equal "[cite/s:@a]"
  1750. (org-test-with-temp-text "[cite<point>:@a]"
  1751. (let ((org-cite--processors nil)
  1752. (org-cite-insert-processor 'foo))
  1753. (org-cite-register-processor 'foo
  1754. :insert (org-cite-make-insert-processor
  1755. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1756. (org-cite-insert nil)
  1757. (buffer-string)))))
  1758. (should
  1759. (equal "[cite:@a]"
  1760. (org-test-with-temp-text "[cite<point>/style:@a]"
  1761. (let ((org-cite--processors nil)
  1762. (org-cite-insert-processor 'foo))
  1763. (org-cite-register-processor 'foo
  1764. :insert (org-cite-make-insert-processor
  1765. (lambda (&rest _) "k") (lambda (&rest _) "")))
  1766. (org-cite-insert nil)
  1767. (buffer-string)))))
  1768. (should-error
  1769. (org-test-with-temp-text "[cite<point>/style:@a]"
  1770. (let ((org-cite--processors nil)
  1771. (org-cite-insert-processor 'foo))
  1772. (org-cite-register-processor 'foo
  1773. :insert (org-cite-make-insert-processor
  1774. (lambda (&rest _) "k") #'ignore))
  1775. (org-cite-insert nil))))
  1776. (should
  1777. (equal "[cite:@a][cite:@k]"
  1778. (org-test-with-temp-text "[cite:@a]<point>"
  1779. (let ((org-cite--processors nil)
  1780. (org-cite-insert-processor 'foo))
  1781. (org-cite-register-processor 'foo
  1782. :insert (org-cite-make-insert-processor
  1783. (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
  1784. (org-cite-insert nil)
  1785. (buffer-string)))))
  1786. (should
  1787. (equal "[cite:@k][cite:@a]"
  1788. (org-test-with-temp-text "<point>[cite:@a]"
  1789. (let ((org-cite--processors nil)
  1790. (org-cite-insert-processor 'foo))
  1791. (org-cite-register-processor 'foo
  1792. :insert (org-cite-make-insert-processor
  1793. (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
  1794. (org-cite-insert nil)
  1795. (buffer-string)))))
  1796. (should
  1797. (equal "[cite/s:@k][cite:@a]"
  1798. (org-test-with-temp-text "<point>[cite:@a]"
  1799. (let ((org-cite--processors nil)
  1800. (org-cite-insert-processor 'foo))
  1801. (org-cite-register-processor 'foo
  1802. :insert (org-cite-make-insert-processor
  1803. (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
  1804. (org-cite-insert t)
  1805. (buffer-string))))))
  1806. (ert-deftest test-org-cite/insert-capability ()
  1807. "Test \"insert\" capability."
  1808. ;; Standard test.
  1809. (should
  1810. (eq 'success
  1811. (catch :exit
  1812. (org-test-with-temp-text ""
  1813. (let ((org-cite--processors nil)
  1814. (org-cite-insert-processor 'foo))
  1815. (org-cite-register-processor 'foo
  1816. :insert (lambda (_ _) (throw :exit 'success)))
  1817. (call-interactively #'org-cite-insert))))))
  1818. ;; Throw an error if there is no "insert" processor, or if it is
  1819. ;; unable to insert a citation.
  1820. (should-error
  1821. (org-test-with-temp-text ""
  1822. (let ((org-cite-insert-processor nil))
  1823. (call-interactively #'org-cite-insert))))
  1824. (should-error
  1825. (org-test-with-temp-text ""
  1826. (let ((org-cite--processors nil)
  1827. (org-cite-insert-processor 'foo))
  1828. (org-cite-register-processor 'foo)
  1829. (call-interactively #'org-cite-insert))))
  1830. ;; Throw an error if the location is inappropriate for a citation.
  1831. (should-error
  1832. (org-test-with-temp-text "=verbatim<point> text="
  1833. (let ((org-cite--processors nil)
  1834. (org-cite-insert-processor 'foo))
  1835. (org-cite-register-processor 'foo
  1836. :insert (lambda (_ _) (throw :exit 'success)))
  1837. (call-interactively #'org-cite-insert))))
  1838. ;; Allow inserting citations at the beginning of a footnote
  1839. ;; definition, right after the label.
  1840. (should
  1841. (eq 'success
  1842. (catch :exit
  1843. (org-test-with-temp-text "[fn:1]<point>"
  1844. (let ((org-cite--processors nil)
  1845. (org-cite-insert-processor 'foo))
  1846. (org-cite-register-processor 'foo
  1847. :insert (lambda (_ _) (throw :exit 'success)))
  1848. (call-interactively #'org-cite-insert))))))
  1849. (should
  1850. (eq 'success
  1851. (catch :exit
  1852. (org-test-with-temp-text "[fn:1] <point>"
  1853. (let ((org-cite--processors nil)
  1854. (org-cite-insert-processor 'foo))
  1855. (org-cite-register-processor 'foo
  1856. :insert (lambda (_ _) (throw :exit 'success)))
  1857. (call-interactively #'org-cite-insert))))))
  1858. (should
  1859. (eq 'success
  1860. (catch :exit
  1861. (org-test-with-temp-text "[fn:1]<point>\nParagraph"
  1862. (let ((org-cite--processors nil)
  1863. (org-cite-insert-processor 'foo))
  1864. (org-cite-register-processor 'foo
  1865. :insert (lambda (_ _) (throw :exit 'success)))
  1866. (call-interactively #'org-cite-insert))))))
  1867. (should-error
  1868. (org-test-with-temp-text "[fn:1<point>]"
  1869. (let ((org-cite--processors nil)
  1870. (org-cite-insert-processor 'foo))
  1871. (org-cite-register-processor 'foo
  1872. :insert (lambda (_ _) (throw :exit 'success)))
  1873. (call-interactively #'org-cite-insert))))
  1874. (should-error
  1875. (org-test-with-temp-text "<point>[fn:1]"
  1876. (let ((org-cite--processors nil)
  1877. (org-cite-insert-processor 'foo))
  1878. (org-cite-register-processor 'foo
  1879. :insert (lambda (_ _) (throw :exit 'success)))
  1880. (call-interactively #'org-cite-insert))))
  1881. ;; Allow inserting citations in captions.
  1882. (should
  1883. (eq 'success
  1884. (catch :exit
  1885. (org-test-with-temp-text "#+caption: <point>\n| table |"
  1886. (let ((org-cite--processors nil)
  1887. (org-cite-insert-processor 'foo))
  1888. (org-cite-register-processor 'foo
  1889. :insert (lambda (_ _) (throw :exit 'success)))
  1890. (call-interactively #'org-cite-insert))))))
  1891. ;; Allow inserting citations in table cells.
  1892. (should
  1893. (eq 'success
  1894. (catch :exit
  1895. (org-test-with-temp-text "| <point>table |"
  1896. (let ((org-cite--processors nil)
  1897. (org-cite-insert-processor 'foo))
  1898. (org-cite-register-processor 'foo
  1899. :insert (lambda (_ _) (throw :exit 'success)))
  1900. (call-interactively #'org-cite-insert))))))
  1901. (should
  1902. (eq 'success
  1903. (catch :exit
  1904. (org-test-with-temp-text "| table<point> |"
  1905. (let ((org-cite--processors nil)
  1906. (org-cite-insert-processor 'foo))
  1907. (org-cite-register-processor 'foo
  1908. :insert (lambda (_ _) (throw :exit 'success)))
  1909. (call-interactively #'org-cite-insert))))))
  1910. (should
  1911. (eq 'success
  1912. (catch :exit
  1913. (org-test-with-temp-text "| table <point> |"
  1914. (let ((org-cite--processors nil)
  1915. (org-cite-insert-processor 'foo))
  1916. (org-cite-register-processor 'foo
  1917. :insert (lambda (_ _) (throw :exit 'success)))
  1918. (call-interactively #'org-cite-insert)))))))
  1919. (provide 'test-oc)
  1920. ;;; test-oc.el ends here