test-oc.el 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  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/list-citations ()
  746. "Test `org-cite-list-citations'."
  747. (should
  748. (equal '("a")
  749. (org-test-with-parsed-data "Test [cite:@a]"
  750. (cl-mapcan (lambda (c)
  751. (mapcar (lambda (ref)
  752. (org-element-property :key ref))
  753. (org-element-contents c)))
  754. (org-cite-list-citations info)))))
  755. (should
  756. (equal '("a" "b")
  757. (org-test-with-parsed-data "Test [cite:@a] [cite:@b]"
  758. (cl-mapcan (lambda (c)
  759. (mapcar (lambda (ref)
  760. (org-element-property :key ref))
  761. (org-element-contents c)))
  762. (org-cite-list-citations info)))))
  763. (should
  764. (equal '("a")
  765. (org-test-with-parsed-data "Test[fn:1]\n[fn:1] [cite:@a]"
  766. (cl-mapcan (lambda (c)
  767. (mapcar (lambda (ref)
  768. (org-element-property :key ref))
  769. (org-element-contents c)))
  770. (org-cite-list-citations info)))))
  771. (should
  772. (equal '("a" "b")
  773. (org-test-with-parsed-data "First[cite:@a] Second[fn:1]\n[fn:1] [cite:@b]"
  774. (cl-mapcan (lambda (c)
  775. (mapcar (lambda (ref)
  776. (org-element-property :key ref))
  777. (org-element-contents c)))
  778. (org-cite-list-citations info)))))
  779. (should
  780. (equal '("b" "a")
  781. (org-test-with-parsed-data "First[fn:1] Second[cite:@a]\n[fn:1] [cite:@b]"
  782. (cl-mapcan (lambda (c)
  783. (mapcar (lambda (ref)
  784. (org-element-property :key ref))
  785. (org-element-contents c)))
  786. (org-cite-list-citations info)))))
  787. (should
  788. (equal '("a" "b")
  789. (org-test-with-parsed-data
  790. "Text[fn:1][fn:2]\n[fn:1] [cite:@a]\n\n[fn:2] [cite:@b]"
  791. (cl-mapcan (lambda (c)
  792. (mapcar (lambda (ref)
  793. (org-element-property :key ref))
  794. (org-element-contents c)))
  795. (org-cite-list-citations info)))))
  796. (should
  797. (equal '("b" "a")
  798. (org-test-with-parsed-data
  799. "Text[fn:1]\n[fn:1] [fn:2][cite:@a]\n\n[fn:2] [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" "b")
  807. (org-test-with-parsed-data
  808. "Text[fn:1]\n[fn:1] [cite:@a][fn:2]\n\n[fn:2] [cite:@b]"
  809. (cl-mapcan (lambda (c)
  810. (mapcar (lambda (ref)
  811. (org-element-property :key ref))
  812. (org-element-contents c)))
  813. (org-cite-list-citations info)))))
  814. (should
  815. (equal '("a")
  816. (org-test-with-parsed-data "Text[fn::[cite:@a]]"
  817. (cl-mapcan (lambda (c)
  818. (mapcar (lambda (ref)
  819. (org-element-property :key ref))
  820. (org-element-contents c)))
  821. (org-cite-list-citations info))))))
  822. (ert-deftest test-org-cite/list-keys ()
  823. "Test `org-cite-list-keys'."
  824. (should
  825. (equal '("a")
  826. (org-test-with-parsed-data "Test [cite:@a]"
  827. (org-cite-list-keys info))))
  828. (should
  829. (equal '("a" "b")
  830. (org-test-with-parsed-data "Test [cite:@a] [cite:@b]"
  831. (org-cite-list-keys info))))
  832. ;; Remove duplicates.
  833. (should
  834. (equal '("a")
  835. (org-test-with-parsed-data "Test [cite:@a] [cite:@a]"
  836. (org-cite-list-keys info))))
  837. ;; Keys are ordered by first appearance in the document.
  838. (should
  839. (equal '("a" "b")
  840. (org-test-with-parsed-data "Test [cite:@a] [cite:@b] [cite:@a]"
  841. (org-cite-list-keys info))))
  842. (should
  843. (equal '("a" "b" "c")
  844. (org-test-with-parsed-data
  845. "Test [cite:@a][fn:1] [cite:@c] [cite:@a]\n[fn:1] [cite:@b]"
  846. (org-cite-list-keys info)))))
  847. (ert-deftest test-org-cite/key-number ()
  848. "Test `org-cite-key-number'."
  849. (should
  850. (= 1 (org-test-with-parsed-data "[cite:@key]"
  851. (org-cite-key-number "key" info))))
  852. (should
  853. (equal '(1 2)
  854. (org-test-with-parsed-data "[cite:@key] [cite:@key2] [cite:@key]"
  855. (list (org-cite-key-number "key" info)
  856. (org-cite-key-number "key2" info)))))
  857. ;; When "predicate" is nil, keys are sorted by appearance order in
  858. ;; the buffer.
  859. (should
  860. (equal '((1 . "a") (2 . "c") (3 . "b"))
  861. (org-test-with-parsed-data
  862. "[cite:@a][fn:1] [cite:@b]\n[fn:1] [cite:@c]"
  863. (sort (mapcar (lambda (key)
  864. (cons (org-cite-key-number key info) key))
  865. '("a" "b" "c"))
  866. #'car-less-than-car))))
  867. (should
  868. (equal '((1 . "a") (2 . "b") (3 . "c"))
  869. (org-test-with-parsed-data
  870. "[cite:@a][fn:1] [cite:@b]\n[fn:1] [cite:@c]"
  871. (sort (mapcar (lambda (key)
  872. (cons (org-cite-key-number key info #'string<) key))
  873. '("a" "b" "c"))
  874. #'car-less-than-car)))))
  875. (ert-deftest test-org-cite/wrap-citation ()
  876. "Test `org-cite-wrap-citation'."
  877. ;; Reference test.
  878. (should
  879. (org-test-with-parsed-data "[cite:@key]"
  880. (org-element-map tree 'citation
  881. (lambda (c)
  882. (org-cite-wrap-citation c info)
  883. (org-cite-inside-footnote-p c))
  884. info)))
  885. ;; Created footnote is anonymous.
  886. (should-not
  887. (org-test-with-parsed-data "[cite:@key] "
  888. (org-element-map tree 'citation
  889. (lambda (c)
  890. (org-cite-wrap-citation c info)
  891. (org-element-property :label (org-cite-inside-footnote-p c)))
  892. info)))
  893. ;; Created footnote is inline.
  894. (should
  895. (equal '(inline)
  896. (org-test-with-parsed-data "[cite:@key]"
  897. (org-element-map tree 'citation
  898. (lambda (c)
  899. (org-cite-wrap-citation c info)
  900. (org-element-property :type
  901. (org-cite-inside-footnote-p c)))
  902. info))))
  903. ;; Preserve `:post-blank' property.
  904. (should
  905. (equal '(2)
  906. (org-test-with-parsed-data "[cite:@key] "
  907. (org-element-map tree 'citation
  908. (lambda (c)
  909. (org-cite-wrap-citation c info)
  910. (org-element-property :post-blank
  911. (org-cite-inside-footnote-p c)))
  912. info))))
  913. ;; Set `:post-blank' to 0 in the element before new footnote.
  914. (should-not
  915. (org-test-with-parsed-data "Text [cite:@key]"
  916. (org-element-map tree 'citation
  917. (lambda (c)
  918. (org-cite-wrap-citation c info)
  919. (let ((previous
  920. (org-export-get-previous-element
  921. (org-cite-inside-footnote-p c) info)))
  922. (string-match (rx blank string-end) previous)))
  923. info)))
  924. (should
  925. (equal '(0)
  926. (org-test-with-parsed-data "*Text* [cite:@key]"
  927. (org-element-map tree 'citation
  928. (lambda (c)
  929. (org-cite-wrap-citation c info)
  930. (let ((previous
  931. (org-export-get-previous-element
  932. (org-cite-inside-footnote-p c) info)))
  933. (org-element-property :post-blank previous)))
  934. info))))
  935. (should
  936. (equal '("Text")
  937. (org-test-with-parsed-data "Text [cite:@key]"
  938. (org-element-map tree 'citation
  939. (lambda (c)
  940. (org-cite-wrap-citation c info)
  941. (org-export-get-previous-element
  942. (org-cite-inside-footnote-p c) info))
  943. info)))))
  944. (defun test-org-cite--export-with-rule (text &optional rule punct)
  945. "Export TEXT string using RULE for punctuation positioning.
  946. Call `org-cite-adjust-note' on each citation object with RULE and, PUNCT
  947. arguments. Replace citation with \"@\" character in the output."
  948. (org-test-with-temp-text text
  949. (let ((org-cite--processors nil))
  950. (org-cite-register-processor 'test
  951. :export-citation
  952. (lambda (citation _s _b info)
  953. (org-cite-adjust-note citation info rule punct)
  954. "@"))
  955. (let ((org-cite-export-processors '((t . (test nil nil)))))
  956. (org-trim
  957. (org-export-as
  958. (org-export-create-backend
  959. :transcoders
  960. '((section . (lambda (_s c _i) (replace-regexp-in-string " @" "@" c)))
  961. (paragraph . (lambda (_s c _i) c))))))))))
  962. (ert-deftest test-org-cite/adjust-note ()
  963. "Test `org-cite-adjust-note' function."
  964. ;; Basic tests for all rules. In the output, @ replaces citation.
  965. (let ((cases '("\"[cite:@k]!"
  966. ".\"[cite:@k]!"
  967. "\"[cite:@k]"
  968. ".\"[cite:@k]"
  969. ".[cite:@k]"
  970. "[cite:@k]!")))
  971. (should ;test (inside inside after)
  972. (equal
  973. '(iia "!@\"" ".@\"!" "@\"" ".@\"" ".@" "!@")
  974. (cons 'iia
  975. (mapcar (lambda (c)
  976. (test-org-cite--export-with-rule
  977. c '(inside inside after)))
  978. cases))))
  979. (should ;test (inside inside before)
  980. (equal
  981. '(iib "@!\"" "@.\"!" "@\"" "@.\"" "@." "@!")
  982. (cons 'iib
  983. (mapcar (lambda (c)
  984. (test-org-cite--export-with-rule
  985. c '(inside inside before)))
  986. cases))))
  987. (should ;test (inside outside after)
  988. (equal
  989. '(ioa "!\"@" ".\"!@" "\"@" ".\"@" ".@" "!@")
  990. (cons 'ioa
  991. (mapcar (lambda (c)
  992. (test-org-cite--export-with-rule
  993. c '(inside outside after)))
  994. cases))))
  995. (should ;test (inside outside before)
  996. (equal
  997. '(iob "!\"@" ".\"@!" "\"@" ".\"@" "@." "@!")
  998. (cons 'iob
  999. (mapcar (lambda (c)
  1000. (test-org-cite--export-with-rule
  1001. c '(inside outside before)))
  1002. cases))))
  1003. (should ;test (inside same after)
  1004. (equal
  1005. '(isa "!@\"" ".\"!@" "\"@" ".@\"" ".@" "!@")
  1006. (cons 'isa
  1007. (mapcar (lambda (c)
  1008. (test-org-cite--export-with-rule
  1009. c '(inside same after)))
  1010. cases))))
  1011. (should ;test (inside same before)
  1012. (equal
  1013. '(isb "@!\"" ".\"@!" "\"@" "@.\"" "@." "@!")
  1014. (cons 'isb
  1015. (mapcar (lambda (c)
  1016. (test-org-cite--export-with-rule
  1017. c '(inside same before)))
  1018. cases))))
  1019. (should ;test (outside inside after)
  1020. (equal
  1021. '(oia "@\"!" ".@\"!" "@\"" "@\"." ".@" "!@")
  1022. (cons 'oia
  1023. (mapcar (lambda (c)
  1024. (test-org-cite--export-with-rule
  1025. c '(outside inside after)))
  1026. cases))))
  1027. (should ;test (outside inside before)
  1028. (equal
  1029. '(oib "@\"!" "@.\"!" "@\"" "@\"." "@." "@!")
  1030. (cons 'oib
  1031. (mapcar (lambda (c)
  1032. (test-org-cite--export-with-rule
  1033. c '(outside inside before)))
  1034. cases))))
  1035. (should ;test (outside outside after)
  1036. (equal
  1037. '(ooa "\"!@" ".\"!@" "\"@" "\".@" ".@" "!@")
  1038. (cons 'ooa
  1039. (mapcar (lambda (c)
  1040. (test-org-cite--export-with-rule
  1041. c '(outside outside after)))
  1042. cases))))
  1043. (should ;test (outside outside before)
  1044. (equal
  1045. '(oob "\"@!" ".\"@!" "\"@" "\"@." "@." "@!")
  1046. (cons 'oob
  1047. (mapcar (lambda (c)
  1048. (test-org-cite--export-with-rule
  1049. c '(outside outside before)))
  1050. cases))))
  1051. (should ;test (outside same after)
  1052. (equal
  1053. '(osa "\"!@" ".\"!@" "\"@" "\".@" ".@" "!@")
  1054. (cons 'osa
  1055. (mapcar (lambda (c)
  1056. (test-org-cite--export-with-rule
  1057. c '(outside same after)))
  1058. cases))))
  1059. (should ;test (outside same before)
  1060. (equal
  1061. '(osb "\"@!" ".\"@!" "\"@" "\"@." "@." "@!")
  1062. (cons 'osb
  1063. (mapcar (lambda (c)
  1064. (test-org-cite--export-with-rule
  1065. c '(outside same before)))
  1066. cases)))))
  1067. ;; Test `adaptive' behaviour.
  1068. (should
  1069. (equal "@\"."
  1070. (test-org-cite--export-with-rule ".\" [cite:@k]"
  1071. '(adaptive inside after))))
  1072. (should
  1073. (equal "@\"!"
  1074. (test-org-cite--export-with-rule "\" [cite:@k]!"
  1075. '(adaptive inside after))))
  1076. (should
  1077. (equal ".@\""
  1078. (test-org-cite--export-with-rule ".\"[cite:@k]"
  1079. '(adaptive inside after))))
  1080. (should
  1081. (equal "!@\""
  1082. (test-org-cite--export-with-rule "\"[cite:@k]!"
  1083. '(adaptive inside after))))
  1084. ;; Handle white space when inserting citation before quotation mark
  1085. ;; or punctuation.
  1086. (should
  1087. (equal ",@\" next"
  1088. (test-org-cite--export-with-rule ",\" [cite:@k] next"
  1089. '(inside inside after))))
  1090. (should
  1091. (equal "@,\" next"
  1092. (test-org-cite--export-with-rule ",\" [cite:@k] next"
  1093. '(inside inside before))))
  1094. (should
  1095. (equal "@\"."
  1096. (test-org-cite--export-with-rule "\" [cite:@k]."
  1097. '(outside inside before))))
  1098. (should
  1099. (equal "@\" !"
  1100. (test-org-cite--export-with-rule "\" [cite:@k] !"
  1101. '(outside inside before))))
  1102. (should
  1103. (equal "text@ !"
  1104. (test-org-cite--export-with-rule "text ![cite:@k]"
  1105. '(inside outside before))))
  1106. ;; Preserve white space between citation and final punctuation when
  1107. ;; moving citation past final punctuation.
  1108. (should
  1109. (equal "text !@"
  1110. (test-org-cite--export-with-rule "text [cite:@k] !"
  1111. '(inside inside after))))
  1112. (should
  1113. (equal "text\n !@"
  1114. (test-org-cite--export-with-rule "text [cite:@k]\n !"
  1115. '(inside inside after))))
  1116. ;; Choose punctuation with optional argument.
  1117. (should-not
  1118. (equal "!@"
  1119. (test-org-cite--export-with-rule "[cite:@k]!"
  1120. '(inside outside after)
  1121. '("."))))
  1122. (should
  1123. (equal ".@"
  1124. (test-org-cite--export-with-rule "[cite:@k]."
  1125. '(inside outside after)
  1126. '(".")))))
  1127. (ert-deftest test-org-cite/parse-elements ()
  1128. "Test `org-cite-parse-elements' function."
  1129. (should-error (org-cite-parse-elements "* H"))
  1130. (should-error (org-cite-parse-elements "Paragraph\n* H"))
  1131. (should
  1132. (equal '(paragraph)
  1133. (mapcar #'org-element-type (org-cite-parse-elements "s"))))
  1134. (should
  1135. (equal '(paragraph paragraph)
  1136. (mapcar #'org-element-type (org-cite-parse-elements "Text\n\nText")))))
  1137. (ert-deftest test-org-cite/parse-objects ()
  1138. "Test `org-cite-parse-objects' function."
  1139. (should
  1140. (equal '(plain-text)
  1141. (mapcar #'org-element-type (org-cite-parse-objects "s"))))
  1142. (should
  1143. (equal '(plain-text bold)
  1144. (mapcar #'org-element-type (org-cite-parse-objects "s *b*"))))
  1145. (should
  1146. (equal '(link)
  1147. (mapcar #'org-element-type (org-cite-parse-objects "[[link]]"))))
  1148. ;; When optional argument is non-nil, only recognize types allowed
  1149. ;; in as a citation reference affix.
  1150. (should-not
  1151. (equal '(link)
  1152. (mapcar #'org-element-type (org-cite-parse-objects "[[link]]" t))))
  1153. (should
  1154. (equal '(bold)
  1155. (mapcar #'org-element-type (org-cite-parse-objects "*b*" t)))))
  1156. (ert-deftest test-org-cite/make-paragraph ()
  1157. "Test `org-cite-make-paragraph' function."
  1158. ;; Check string as argument.
  1159. (should
  1160. (eq 'paragraph
  1161. (org-element-type (org-cite-make-paragraph "a"))))
  1162. (should
  1163. (equal '("a")
  1164. (org-element-contents (org-cite-make-paragraph "a"))))
  1165. ;; Check object as argument.
  1166. (should
  1167. (eq 'paragraph
  1168. (org-element-type
  1169. (org-cite-make-paragraph (org-element-create 'bold nil "b")))))
  1170. (should
  1171. (equal '(bold)
  1172. (mapcar #'org-element-type
  1173. (org-element-contents
  1174. (org-cite-make-paragraph (org-element-create 'bold nil "b"))))))
  1175. ;; Check secondary string as argument.
  1176. (should
  1177. (eq 'paragraph
  1178. (org-element-type (org-cite-make-paragraph '("a")))))
  1179. (should
  1180. (equal '("a")
  1181. (org-element-contents (org-cite-make-paragraph '("a")))))
  1182. ;; Mix all types of arguments.
  1183. (should
  1184. (equal '(plain-text bold plain-text)
  1185. (mapcar #'org-element-type
  1186. (org-element-contents
  1187. (org-cite-make-paragraph
  1188. "a" (org-element-create 'bold nil "b") '("c"))))))
  1189. ;; Check `:parent' property.
  1190. (should
  1191. (eq 'paragraph
  1192. (org-element-type
  1193. (org-element-property
  1194. :parent
  1195. (car (org-element-contents (org-cite-make-paragraph "a"))))))))
  1196. (ert-deftest test-org-cite/emphasize ()
  1197. "Test `org-cite-emphasize' function."
  1198. ;; Raise an error if first argument has wrong type.
  1199. (should-error (org-cite-emphasize 'code "a"))
  1200. ;; Check string argument.
  1201. (should (eq 'bold (org-element-type (org-cite-emphasize 'bold "a"))))
  1202. (should (equal '("a") (org-element-contents (org-cite-emphasize 'bold "a"))))
  1203. ;; Check object argument.
  1204. (should
  1205. (eq 'bold
  1206. (org-element-type
  1207. (org-cite-emphasize 'bold (org-element-create 'bold nil "a")))))
  1208. (should
  1209. (equal '(italic)
  1210. (mapcar #'org-element-type
  1211. (org-element-contents
  1212. (org-cite-emphasize 'bold
  1213. (org-element-create 'italic nil "a"))))))
  1214. ;; Check secondary string argument.
  1215. (should (eq 'bold (org-element-type (org-cite-emphasize 'bold '("a")))))
  1216. (should (equal '("a") (org-element-contents (org-cite-emphasize 'bold '("a")))))
  1217. ;; Mix all types of arguments.
  1218. (should
  1219. (equal '(plain-text italic plain-text)
  1220. (mapcar #'org-element-type
  1221. (org-element-contents
  1222. (org-cite-emphasize 'bold
  1223. "a" (org-element-create 'italic nil "b") '("c"))))))
  1224. ;; Check `:parent' property.
  1225. (should
  1226. (eq 'bold
  1227. (org-element-type
  1228. (org-element-property
  1229. :parent
  1230. (car (org-element-contents (org-cite-emphasize 'bold "a"))))))))
  1231. (ert-deftest test-org-cite/concat ()
  1232. "Test `org-cite-concat' function."
  1233. ;; Return nil when there is no data.
  1234. (should
  1235. (equal "" (org-element-interpret-data (org-cite-concat))))
  1236. ;; Concatenate strings, objects and secondary strings.
  1237. (should
  1238. (equal "ab"
  1239. (org-element-interpret-data (org-cite-concat "a" "b"))))
  1240. (should
  1241. (equal "*a* b"
  1242. (org-element-interpret-data
  1243. (org-cite-concat (org-element-create 'bold nil "a") " b"))))
  1244. (should
  1245. (equal "*a* b"
  1246. (org-element-interpret-data
  1247. (org-cite-concat
  1248. (list (org-element-create 'bold nil "a")) " b"))))
  1249. ;; Return an error for any other object type.
  1250. (should-error (org-cite-concat 2)))
  1251. (ert-deftest test-org-cite/mapconcat ()
  1252. "Test `org-cite-mapconcat' function."
  1253. (should
  1254. (equal ""
  1255. (org-element-interpret-data
  1256. (org-cite-mapconcat #'identity nil ""))))
  1257. (should
  1258. (equal "ab"
  1259. (org-element-interpret-data
  1260. (org-cite-mapconcat #'identity '("a" "b") ""))))
  1261. (should
  1262. (equal "*a* b *c*"
  1263. (org-element-interpret-data
  1264. (org-cite-mapconcat
  1265. #'identity
  1266. (list (org-element-create 'bold nil "a")
  1267. (list " b " (org-element-create 'bold nil "c"))) ""))))
  1268. (should
  1269. (equal "*a* *b*"
  1270. (org-element-interpret-data
  1271. (org-cite-mapconcat
  1272. (lambda (s) (org-element-create 'bold nil s))
  1273. '("a" "b") " "))))
  1274. (should
  1275. (equal "*a* b*c*"
  1276. (org-element-interpret-data
  1277. (org-cite-mapconcat
  1278. #'identity
  1279. (list (org-element-create 'bold nil "a")
  1280. (list "b" (org-element-create 'bold nil "c"))) " ")))))
  1281. ;;; Test capabilities.
  1282. (ert-deftest test-org-cite/activate-capability ()
  1283. "Test \"activate\" capability."
  1284. ;; Standard test.
  1285. (should
  1286. (eq 'success
  1287. (catch :exit
  1288. (org-test-with-temp-text "[cite:@key]"
  1289. (let ((org-cite--processors nil)
  1290. (org-cite-activate-processor 'foo))
  1291. (org-cite-register-processor 'foo
  1292. :activate (lambda (_) (throw :exit 'success)))
  1293. (font-lock-ensure))))))
  1294. ;; If there is no "follow" processor, or if processor does not
  1295. ;; handle this capability, fall back to fontifying whole citation
  1296. ;; with `org-cite' face and each key with `org-cite-key' face.
  1297. (should
  1298. (eq 'org-cite
  1299. (org-test-with-temp-text "[cite:@key]"
  1300. (let ((org-cite-activate-processor nil))
  1301. (font-lock-ensure)
  1302. (face-at-point)))))
  1303. (should
  1304. (eq 'org-cite-key
  1305. (org-test-with-temp-text "[cite:@<point>key]"
  1306. (let ((org-cite-activate-processor nil))
  1307. (font-lock-ensure)
  1308. (face-at-point)))))
  1309. (should
  1310. (eq 'org-cite
  1311. (org-test-with-temp-text "[cite:@key]"
  1312. (let ((org-cite--processors nil)
  1313. (org-cite-activate-processor 'foo))
  1314. (org-cite-register-processor 'foo)
  1315. (font-lock-ensure)
  1316. (face-at-point))))))
  1317. (ert-deftest test-org-cite/export-capability ()
  1318. "Test \"export\" capability."
  1319. ;; Regular citations export.
  1320. (should
  1321. (eq 'success
  1322. (catch :exit
  1323. (org-test-with-temp-text "[cite:@key]"
  1324. (let ((org-cite--processors nil)
  1325. (org-cite-export-processors '((t . (foo nil nil)))))
  1326. (org-cite-register-processor 'foo
  1327. :export-citation (lambda (&rest _) (throw :exit 'success)))
  1328. (org-export-as (org-export-create-backend)))))))
  1329. ;; Export citation as string.
  1330. (should
  1331. (equal "citation\n"
  1332. (org-test-with-temp-text "[cite:@key]"
  1333. (let ((org-cite--processors nil)
  1334. (org-cite-export-processors '((t . (foo nil nil)))))
  1335. (org-cite-register-processor 'foo
  1336. :export-citation (lambda (&rest _) "citation"))
  1337. (org-export-as (org-export-create-backend
  1338. :transcoders
  1339. '((section . (lambda (_ c _) c))
  1340. (paragraph . (lambda (_ c _) c)))))))))
  1341. ;; Export citation as parsed object.
  1342. (should
  1343. (equal "success\n"
  1344. (org-test-with-temp-text "[cite:@key]"
  1345. (let ((org-cite--processors nil)
  1346. (org-cite-export-processors '((t . (foo nil nil)))))
  1347. (org-cite-register-processor 'foo
  1348. :export-citation (lambda (&rest _)
  1349. (org-element-create 'bold nil "cite")))
  1350. (org-export-as (org-export-create-backend
  1351. :transcoders
  1352. '((section . (lambda (_ c _) c))
  1353. (paragraph . (lambda (_ c _) c))
  1354. (bold . (lambda (&rest _) "success")))))))))
  1355. ;; Export citation as a secondary string.
  1356. (should
  1357. (equal "boldtwo\n"
  1358. (org-test-with-temp-text "[cite:@key]"
  1359. (let ((org-cite--processors nil)
  1360. (org-cite-export-processors '((t . (foo nil nil)))))
  1361. (org-cite-register-processor 'foo
  1362. :export-citation (lambda (&rest _)
  1363. (list (org-element-create 'bold nil "one")
  1364. "two")))
  1365. (org-export-as (org-export-create-backend
  1366. :transcoders
  1367. '((section . (lambda (_ c _) c))
  1368. (paragraph . (lambda (_ c _) c))
  1369. (bold . (lambda (&rest _) "bold")))))))))
  1370. ;; When exporting citation as a secondary string, last object
  1371. ;; inherits post-blank from initial citation.
  1372. (should
  1373. (equal "twobold one-space\n"
  1374. (org-test-with-temp-text "[cite:@key] one-space"
  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 _)
  1379. (list "two"
  1380. (org-element-create 'bold nil "one"))))
  1381. (org-export-as (org-export-create-backend
  1382. :transcoders
  1383. '((section . (lambda (_ c _) c))
  1384. (paragraph . (lambda (_ c _) c))
  1385. (bold . (lambda (&rest _) "bold")))))))))
  1386. (should
  1387. (equal "boldtwo one-space\n"
  1388. (org-test-with-temp-text "[cite:@key] one-space"
  1389. (let ((org-cite--processors nil)
  1390. (org-cite-export-processors '((t . (foo nil nil)))))
  1391. (org-cite-register-processor 'foo
  1392. :export-citation (lambda (&rest _)
  1393. (list (org-element-create 'bold nil "one")
  1394. "two")))
  1395. (org-export-as (org-export-create-backend
  1396. :transcoders
  1397. '((section . (lambda (_ c _) c))
  1398. (paragraph . (lambda (_ c _) c))
  1399. (bold . (lambda (&rest _) "bold")))))))))
  1400. ;; Make sure to have a space between a quote and a citation.
  1401. (should
  1402. (equal "\"quotation\" citation\n"
  1403. (org-test-with-temp-text "\"quotation\"[cite:@key]"
  1404. (let ((org-cite--processors nil)
  1405. (org-cite-export-processors '((t . (foo nil nil)))))
  1406. (org-cite-register-processor 'foo
  1407. :export-citation (lambda (&rest _) "citation"))
  1408. (org-export-as (org-export-create-backend
  1409. :transcoders
  1410. '((section . (lambda (_ c _) c))
  1411. (paragraph . (lambda (_ c _) c)))))))))
  1412. (should
  1413. (equal "\"quotation\" citation\n"
  1414. (org-test-with-temp-text "\"quotation\" [cite:@key]"
  1415. (let ((org-cite--processors nil)
  1416. (org-cite-export-processors '((t . (foo nil nil)))))
  1417. (org-cite-register-processor 'foo
  1418. :export-citation (lambda (&rest _) "citation"))
  1419. (org-export-as (org-export-create-backend
  1420. :transcoders
  1421. '((section . (lambda (_ c _) c))
  1422. (paragraph . (lambda (_ c _) c)))))))))
  1423. ;; Regular bibliography export.
  1424. (should
  1425. (eq 'success
  1426. (catch :exit
  1427. (org-test-with-temp-text "#+print_bibliography:"
  1428. (let ((org-cite--processors nil)
  1429. (org-cite-export-processors '((t . (foo nil nil)))))
  1430. (org-cite-register-processor 'foo
  1431. :export-bibliography (lambda (&rest _) (throw :exit 'success))
  1432. :export-citation #'ignore)
  1433. (org-export-as (org-export-create-backend)))))))
  1434. (should
  1435. (equal ""
  1436. (org-test-with-temp-text "#+print_bibliography:"
  1437. (let ((org-cite--processors nil)
  1438. (org-cite-export-processors '((t . (foo nil nil)))))
  1439. (org-cite-register-processor 'foo
  1440. :export-citation #'ignore)
  1441. (org-export-as (org-export-create-backend
  1442. :transcoders
  1443. '((section . (lambda (_ c _) c))
  1444. (paragraph . (lambda (_ c _) c)))))))))
  1445. ;; Export bibliography as string.
  1446. (should
  1447. (equal "bibliography\n"
  1448. (org-test-with-temp-text "#+print_bibliography:"
  1449. (let ((org-cite--processors nil)
  1450. (org-cite-export-processors '((t . (foo nil nil)))))
  1451. (org-cite-register-processor 'foo
  1452. :export-bibliography (lambda (&rest _) "bibliography")
  1453. :export-citation #'ignore)
  1454. (org-export-as (org-export-create-backend
  1455. :transcoders
  1456. '((section . (lambda (_ c _) c))
  1457. (paragraph . (lambda (_ c _) c)))))))))
  1458. ;; Export bibliography as a parsed element.
  1459. (should
  1460. (equal "success\n"
  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
  1466. (lambda (&rest _)
  1467. (org-element-create 'example-block '(:value "foo")))
  1468. :export-citation #'ignore)
  1469. (org-export-as
  1470. (org-export-create-backend
  1471. :transcoders
  1472. '((section . (lambda (_ c _) c))
  1473. (example-block . (lambda (&rest _) "success")))))))))
  1474. ;; Export bibliography as a list of parsed elements.
  1475. (should
  1476. (equal "success\nsuccess\n"
  1477. (org-test-with-temp-text "#+print_bibliography:"
  1478. (let ((org-cite--processors nil)
  1479. (org-cite-export-processors '((t . (foo nil nil)))))
  1480. (org-cite-register-processor 'foo
  1481. :export-bibliography
  1482. (lambda (&rest _)
  1483. (list (org-element-create 'example-block '(:value "foo"))
  1484. (org-element-create 'example-block '(:value "bar"))))
  1485. :export-citation #'ignore)
  1486. (org-export-as
  1487. (org-export-create-backend
  1488. :transcoders
  1489. '((section . (lambda (_ c _) c))
  1490. (example-block . (lambda (&rest _) "success")))))))))
  1491. ;; When exporting bibliography as a list of parsed elements, the
  1492. ;; last element inherits post-blank from initial keyword.
  1493. (should
  1494. (equal "success\nsuccess\n\nText\n"
  1495. (org-test-with-temp-text "#+print_bibliography:\n\nText"
  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. (list (org-element-create 'example-block '(:value "foo"))
  1502. (org-element-create 'example-block '(:value "bar"))))
  1503. :export-citation #'ignore)
  1504. (org-export-as
  1505. (org-export-create-backend
  1506. :transcoders
  1507. '((section . (lambda (_ c _) c))
  1508. (example-block . (lambda (&rest _) "success"))
  1509. (paragraph . (lambda (_ c _) c)))))))))
  1510. ;; Use more appropriate citation processor.
  1511. (should
  1512. (equal
  1513. '(p1 p1 p1 p3)
  1514. (org-test-with-temp-text "[cite:@a]"
  1515. (let ((org-export-registered-backends nil)
  1516. (org-cite--procesors nil)
  1517. (org-cite-export-processors
  1518. '((b1 . (p1))
  1519. (t . (p3)))))
  1520. (org-cite-register-processor 'p1
  1521. :export-citation (lambda (&rest _) (throw :exit 'p1)))
  1522. (org-cite-register-processor 'p2
  1523. :export-citation (lambda (&rest _) (throw :exit 'p2)))
  1524. (org-cite-register-processor 'p3
  1525. :export-citation (lambda (&rest _) (throw :exit 'p3)))
  1526. (org-export-define-backend 'b1 nil)
  1527. (org-export-define-derived-backend 'b2 'b1)
  1528. (org-export-define-derived-backend 'b3 'b2)
  1529. (list (catch :exit (org-export-as 'b1))
  1530. (catch :exit (org-export-as 'b2))
  1531. (catch :exit (org-export-as 'b3))
  1532. (catch :exit (org-export-as (org-export-create-backend))))))))
  1533. (should
  1534. (eq 'p2
  1535. (org-test-with-temp-text "#+cite_export: p2\n[cite:@a]"
  1536. (let ((org-export-registered-backends nil)
  1537. (org-cite--procesors nil)
  1538. (org-cite-export-processors '((t . (p1)))))
  1539. (org-cite-register-processor 'p1
  1540. :export-citation (lambda (&rest _) (throw :exit 'p1)))
  1541. (org-cite-register-processor 'p2
  1542. :export-citation (lambda (&rest _) (throw :exit 'p2)))
  1543. (catch :exit (org-export-as (org-export-create-backend)))))))
  1544. ;; Test finalizer.
  1545. (should
  1546. (eq 'success
  1547. (catch :exit
  1548. (org-test-with-temp-text "[cite:@key]"
  1549. (let ((org-cite--processors nil)
  1550. (org-cite-export-processors '((t . (foo nil nil)))))
  1551. (org-cite-register-processor 'foo
  1552. :export-citation (lambda (&rest _) "")
  1553. :export-finalizer (lambda (&rest _) (throw :exit 'success)))
  1554. (org-export-as (org-export-create-backend)))))))
  1555. (should
  1556. (equal "finalized!"
  1557. (org-test-with-temp-text "[cite:@key]"
  1558. (let ((org-cite--processors nil)
  1559. (org-cite-export-processors '((t . (foo nil nil)))))
  1560. (org-cite-register-processor 'foo
  1561. :export-citation #'ignore
  1562. :export-finalizer (lambda (&rest _) "finalized!"))
  1563. (org-export-as (org-export-create-backend))))))
  1564. ;; Ignore citations when there is no selected "export" processor.
  1565. ;; In that case, white space is removed before the citation, not
  1566. ;; after.
  1567. (should
  1568. (equal ""
  1569. (org-test-with-temp-text "[cite:@key]"
  1570. (let ((org-cite-export-processors nil))
  1571. (org-export-as (org-export-create-backend
  1572. :transcoders
  1573. '((section . (lambda (_ c _) c))
  1574. (paragraph . (lambda (_ c _) c)))))))))
  1575. (should
  1576. (equal "Text.\n"
  1577. (org-test-with-temp-text "Text [cite:@key]."
  1578. (let ((org-cite-export-processors nil))
  1579. (org-export-as (org-export-create-backend
  1580. :transcoders
  1581. '((section . (lambda (_ c _) c))
  1582. (paragraph . (lambda (_ c _) c)))))))))
  1583. ;; Throw an error if selected processor does not handle "export"
  1584. ;; capability.
  1585. (should-error
  1586. (org-test-with-temp-text "[cite:@key]"
  1587. (let ((org-cite--processors nil)
  1588. (org-cite-export-processors '((t . (foo nil nil)))))
  1589. (org-cite-register-processor 'foo)
  1590. (org-export-as (org-export-create-backend))))))
  1591. (ert-deftest test-org-cite/follow-capability ()
  1592. "Test \"follow\" capability."
  1593. ;; Standard test.
  1594. (should
  1595. (eq 'success
  1596. (catch :exit
  1597. (org-test-with-temp-text "[cite:@key]"
  1598. (let ((org-cite--processors nil)
  1599. (org-cite-follow-processor 'foo))
  1600. (org-cite-register-processor 'foo
  1601. :follow (lambda (_ _) (throw :exit 'success)))
  1602. (org-open-at-point))))))
  1603. ;; Throw an error if there is no "follow" processor, or if it is
  1604. ;; unable to follow a citation.
  1605. (should-error
  1606. (org-test-with-temp-text "[cite:@key]"
  1607. (let ((org-cite-follow-processor nil))
  1608. (org-open-at-point))))
  1609. (should-error
  1610. (org-test-with-temp-text "[cite:@key]"
  1611. (let ((org-cite--processors nil)
  1612. (org-cite-follow-processor 'foo))
  1613. (org-cite-register-processor 'foo)
  1614. (org-open-at-point)))))
  1615. (ert-deftest test-org-cite/make-insert-processor ()
  1616. "Test `org-cite-make-insert-processor'."
  1617. (should-error (org-cite-make-insert-processor 1 2))
  1618. (should-error
  1619. (org-test-with-temp-text "[cite:@<point>a]"
  1620. (let ((org-cite--processors nil)
  1621. (org-cite-insert-processor 'foo))
  1622. (org-cite-register-processor 'foo
  1623. :insert (org-cite-make-insert-processor
  1624. #'ignore (lambda (&rest _) "s")))
  1625. (org-cite-insert nil))))
  1626. (should
  1627. (equal "[cite:@k]"
  1628. (org-test-with-temp-text "[cite:@<point>a]"
  1629. (let ((org-cite--processors nil)
  1630. (org-cite-insert-processor 'foo))
  1631. (org-cite-register-processor 'foo
  1632. :insert (org-cite-make-insert-processor
  1633. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1634. (org-cite-insert nil)
  1635. (buffer-string)))))
  1636. (should
  1637. (equal "[cite:@k;@a]"
  1638. (org-test-with-temp-text "[cite:<point>@a]"
  1639. (let ((org-cite--processors nil)
  1640. (org-cite-insert-processor 'foo))
  1641. (org-cite-register-processor 'foo
  1642. :insert (org-cite-make-insert-processor
  1643. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1644. (org-cite-insert nil)
  1645. (buffer-string)))))
  1646. (should
  1647. (equal "[cite:@k;pre @a]"
  1648. (org-test-with-temp-text "[cite:<point>pre @a]"
  1649. (let ((org-cite--processors nil)
  1650. (org-cite-insert-processor 'foo))
  1651. (org-cite-register-processor 'foo
  1652. :insert (org-cite-make-insert-processor
  1653. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1654. (org-cite-insert nil)
  1655. (buffer-string)))))
  1656. (should
  1657. (equal "[cite:pre;@k;@a]"
  1658. (org-test-with-temp-text "[cite:<point>pre;@a]"
  1659. (let ((org-cite--processors nil)
  1660. (org-cite-insert-processor 'foo))
  1661. (org-cite-register-processor 'foo
  1662. :insert (org-cite-make-insert-processor
  1663. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1664. (org-cite-insert nil)
  1665. (buffer-string)))))
  1666. (should
  1667. (equal "[cite:@a;@k]"
  1668. (org-test-with-temp-text "[cite:@a<point>]"
  1669. (let ((org-cite--processors nil)
  1670. (org-cite-insert-processor 'foo))
  1671. (org-cite-register-processor 'foo
  1672. :insert (org-cite-make-insert-processor
  1673. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1674. (org-cite-insert nil)
  1675. (buffer-string)))))
  1676. (should
  1677. (equal "[cite:@a post;@k]"
  1678. (org-test-with-temp-text "[cite:@a post<point>]"
  1679. (let ((org-cite--processors nil)
  1680. (org-cite-insert-processor 'foo))
  1681. (org-cite-register-processor 'foo
  1682. :insert (org-cite-make-insert-processor
  1683. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1684. (org-cite-insert nil)
  1685. (buffer-string)))))
  1686. (should
  1687. (equal "[cite:@a;@k;post]"
  1688. (org-test-with-temp-text "[cite:@a;post<point>]"
  1689. (let ((org-cite--processors nil)
  1690. (org-cite-insert-processor 'foo))
  1691. (org-cite-register-processor 'foo
  1692. :insert (org-cite-make-insert-processor
  1693. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1694. (org-cite-insert nil)
  1695. (buffer-string)))))
  1696. (should
  1697. (equal ""
  1698. (org-test-with-temp-text "[cite:@<point>a]"
  1699. (let ((org-cite--processors nil)
  1700. (org-cite-insert-processor 'foo))
  1701. (org-cite-register-processor 'foo
  1702. :insert (org-cite-make-insert-processor
  1703. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1704. (org-cite-insert t)
  1705. (buffer-string)))))
  1706. (should
  1707. (equal "[cite/s:@a]"
  1708. (org-test-with-temp-text "[cite<point>:@a]"
  1709. (let ((org-cite--processors nil)
  1710. (org-cite-insert-processor 'foo))
  1711. (org-cite-register-processor 'foo
  1712. :insert (org-cite-make-insert-processor
  1713. (lambda (&rest _) "k") (lambda (&rest _) "s")))
  1714. (org-cite-insert nil)
  1715. (buffer-string)))))
  1716. (should
  1717. (equal "[cite:@a]"
  1718. (org-test-with-temp-text "[cite<point>/style:@a]"
  1719. (let ((org-cite--processors nil)
  1720. (org-cite-insert-processor 'foo))
  1721. (org-cite-register-processor 'foo
  1722. :insert (org-cite-make-insert-processor
  1723. (lambda (&rest _) "k") (lambda (&rest _) "")))
  1724. (org-cite-insert nil)
  1725. (buffer-string)))))
  1726. (should-error
  1727. (org-test-with-temp-text "[cite<point>/style:@a]"
  1728. (let ((org-cite--processors nil)
  1729. (org-cite-insert-processor 'foo))
  1730. (org-cite-register-processor 'foo
  1731. :insert (org-cite-make-insert-processor
  1732. (lambda (&rest _) "k") #'ignore))
  1733. (org-cite-insert nil))))
  1734. (should
  1735. (equal "[cite:@a][cite:@k]"
  1736. (org-test-with-temp-text "[cite:@a]<point>"
  1737. (let ((org-cite--processors nil)
  1738. (org-cite-insert-processor 'foo))
  1739. (org-cite-register-processor 'foo
  1740. :insert (org-cite-make-insert-processor
  1741. (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
  1742. (org-cite-insert nil)
  1743. (buffer-string)))))
  1744. (should
  1745. (equal "[cite:@k][cite:@a]"
  1746. (org-test-with-temp-text "<point>[cite:@a]"
  1747. (let ((org-cite--processors nil)
  1748. (org-cite-insert-processor 'foo))
  1749. (org-cite-register-processor 'foo
  1750. :insert (org-cite-make-insert-processor
  1751. (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
  1752. (org-cite-insert nil)
  1753. (buffer-string)))))
  1754. (should
  1755. (equal "[cite/s:@k][cite:@a]"
  1756. (org-test-with-temp-text "<point>[cite:@a]"
  1757. (let ((org-cite--processors nil)
  1758. (org-cite-insert-processor 'foo))
  1759. (org-cite-register-processor 'foo
  1760. :insert (org-cite-make-insert-processor
  1761. (lambda (&rest _) '("k")) (lambda (&rest _) "s")))
  1762. (org-cite-insert t)
  1763. (buffer-string))))))
  1764. (ert-deftest test-org-cite/insert-capability ()
  1765. "Test \"insert\" capability."
  1766. ;; Standard test.
  1767. (should
  1768. (eq 'success
  1769. (catch :exit
  1770. (org-test-with-temp-text ""
  1771. (let ((org-cite--processors nil)
  1772. (org-cite-insert-processor 'foo))
  1773. (org-cite-register-processor 'foo
  1774. :insert (lambda (_ _) (throw :exit 'success)))
  1775. (call-interactively #'org-cite-insert))))))
  1776. ;; Throw an error if there is no "insert" processor, or if it is
  1777. ;; unable to insert a citation.
  1778. (should-error
  1779. (org-test-with-temp-text ""
  1780. (let ((org-cite-insert-processor nil))
  1781. (call-interactively #'org-cite-insert))))
  1782. (should-error
  1783. (org-test-with-temp-text ""
  1784. (let ((org-cite--processors nil)
  1785. (org-cite-insert-processor 'foo))
  1786. (org-cite-register-processor 'foo)
  1787. (call-interactively #'org-cite-insert))))
  1788. ;; Throw an error if the location is inappropriate for a citation.
  1789. (should-error
  1790. (org-test-with-temp-text "=verbatim<point> text="
  1791. (let ((org-cite--processors nil)
  1792. (org-cite-insert-processor 'foo))
  1793. (org-cite-register-processor 'foo
  1794. :insert (lambda (_ _) (throw :exit 'success)))
  1795. (call-interactively #'org-cite-insert))))
  1796. ;; Allow inserting citations at the beginning of a footnote
  1797. ;; definition, right after the label.
  1798. (should
  1799. (eq 'success
  1800. (catch :exit
  1801. (org-test-with-temp-text "[fn:1]<point>"
  1802. (let ((org-cite--processors nil)
  1803. (org-cite-insert-processor 'foo))
  1804. (org-cite-register-processor 'foo
  1805. :insert (lambda (_ _) (throw :exit 'success)))
  1806. (call-interactively #'org-cite-insert))))))
  1807. (should
  1808. (eq 'success
  1809. (catch :exit
  1810. (org-test-with-temp-text "[fn:1] <point>"
  1811. (let ((org-cite--processors nil)
  1812. (org-cite-insert-processor 'foo))
  1813. (org-cite-register-processor 'foo
  1814. :insert (lambda (_ _) (throw :exit 'success)))
  1815. (call-interactively #'org-cite-insert))))))
  1816. (should
  1817. (eq 'success
  1818. (catch :exit
  1819. (org-test-with-temp-text "[fn:1]<point>\nParagraph"
  1820. (let ((org-cite--processors nil)
  1821. (org-cite-insert-processor 'foo))
  1822. (org-cite-register-processor 'foo
  1823. :insert (lambda (_ _) (throw :exit 'success)))
  1824. (call-interactively #'org-cite-insert))))))
  1825. (should-error
  1826. (org-test-with-temp-text "[fn:1<point>]"
  1827. (let ((org-cite--processors nil)
  1828. (org-cite-insert-processor 'foo))
  1829. (org-cite-register-processor 'foo
  1830. :insert (lambda (_ _) (throw :exit 'success)))
  1831. (call-interactively #'org-cite-insert))))
  1832. (should-error
  1833. (org-test-with-temp-text "<point>[fn:1]"
  1834. (let ((org-cite--processors nil)
  1835. (org-cite-insert-processor 'foo))
  1836. (org-cite-register-processor 'foo
  1837. :insert (lambda (_ _) (throw :exit 'success)))
  1838. (call-interactively #'org-cite-insert))))
  1839. ;; Allow inserting citations in captions.
  1840. (should
  1841. (eq 'success
  1842. (catch :exit
  1843. (org-test-with-temp-text "#+caption: <point>\n| table |"
  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. ;; Allow inserting citations in table cells.
  1850. (should
  1851. (eq 'success
  1852. (catch :exit
  1853. (org-test-with-temp-text "| <point>table |"
  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
  1860. (eq 'success
  1861. (catch :exit
  1862. (org-test-with-temp-text "| table<point> |"
  1863. (let ((org-cite--processors nil)
  1864. (org-cite-insert-processor 'foo))
  1865. (org-cite-register-processor 'foo
  1866. :insert (lambda (_ _) (throw :exit 'success)))
  1867. (call-interactively #'org-cite-insert))))))
  1868. (should
  1869. (eq 'success
  1870. (catch :exit
  1871. (org-test-with-temp-text "| table <point> |"
  1872. (let ((org-cite--processors nil)
  1873. (org-cite-insert-processor 'foo))
  1874. (org-cite-register-processor 'foo
  1875. :insert (lambda (_ _) (throw :exit 'success)))
  1876. (call-interactively #'org-cite-insert)))))))
  1877. (provide 'test-oc)
  1878. ;;; test-oc.el ends here