test-oc.el 78 KB

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