test-org-element.el 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502
  1. ;;; test-org-element.el --- Tests for org-element.el
  2. ;; Copyright (C) 2012-2015 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (unless (featurep 'org-element)
  16. (signal 'missing-test-dependency "org-element"))
  17. (defun org-test-parse-and-interpret (text)
  18. "Parse TEXT as Org syntax and interpret it.
  19. Return interpreted string."
  20. (with-temp-buffer
  21. (org-mode)
  22. (insert text)
  23. (org-element-interpret-data (org-element-parse-buffer))))
  24. ;;; Test `org-element-map'
  25. (ert-deftest test-org-element/map ()
  26. "Test `org-element-map'."
  27. ;; Can map to `plain-text' objects.
  28. (should
  29. (= 2
  30. (org-test-with-temp-text "Some text \alpha
  31. #+BEGIN_CENTER
  32. Some other text
  33. #+END_CENTER"
  34. (let ((count 0))
  35. (org-element-map
  36. (org-element-parse-buffer) 'plain-text
  37. (lambda (s) (when (string-match "text" s) (incf count))))
  38. count))))
  39. ;; Applies to secondary strings
  40. (should
  41. (org-element-map '("some " (bold nil "bold") "text") 'bold 'identity))
  42. ;; Enter secondary strings before entering contents.
  43. (should
  44. (equal
  45. "alpha"
  46. (org-element-property
  47. :name
  48. (org-test-with-temp-text "* Some \\alpha headline\n\\beta entity."
  49. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t)))))
  50. ;; Apply NO-RECURSION argument.
  51. (should-not
  52. (org-test-with-temp-text "#+BEGIN_CENTER\n\\alpha\n#+END_CENTER"
  53. (org-element-map
  54. (org-element-parse-buffer) 'entity 'identity nil nil 'center-block)))
  55. ;; Use WITH-AFFILIATED argument.
  56. (should
  57. (equal
  58. '("a" "1" "b" "2")
  59. (org-test-with-temp-text "#+CAPTION[a]: 1\n#+CAPTION[b]: 2\nParagraph"
  60. (org-element-map
  61. (org-element-at-point) 'plain-text 'identity nil nil nil t)))))
  62. ;;; Test Setters
  63. (ert-deftest test-org-element/put-property ()
  64. "Test `org-element-put-property' specifications."
  65. ;; Standard test.
  66. (org-test-with-temp-text "* Headline\n *a*"
  67. (let ((tree (org-element-parse-buffer)))
  68. (org-element-put-property
  69. (org-element-map tree 'bold 'identity nil t) :test 1)
  70. (should (org-element-property
  71. :test (org-element-map tree 'bold 'identity nil t)))))
  72. ;; Put property on a string.
  73. (should
  74. (org-element-property :test (org-element-put-property "Paragraph" :test t))))
  75. (ert-deftest test-org-element/set-contents ()
  76. "Test `org-element-set-contents' specifications."
  77. ;; Accept multiple entries.
  78. (should
  79. (equal '("b" (italic nil "a"))
  80. (org-test-with-temp-text "* Headline\n *a*"
  81. (let ((tree (org-element-parse-buffer)))
  82. (org-element-set-contents
  83. (org-element-map tree 'bold 'identity nil t) "b" '(italic nil "a"))
  84. (org-element-contents
  85. (org-element-map tree 'bold 'identity nil t))))))
  86. ;; Accept atoms and elements.
  87. (should
  88. (equal '("b")
  89. (org-test-with-temp-text "* Headline\n *a*"
  90. (let ((tree (org-element-parse-buffer)))
  91. (org-element-set-contents
  92. (org-element-map tree 'bold 'identity nil t) "b")
  93. (org-element-contents
  94. (org-element-map tree 'bold 'identity nil t))))))
  95. (should
  96. (equal '((italic nil "b"))
  97. (org-test-with-temp-text "* Headline\n *a*"
  98. (let ((tree (org-element-parse-buffer)))
  99. (org-element-set-contents
  100. (org-element-map tree 'bold 'identity nil t) '(italic nil "b"))
  101. (org-element-contents
  102. (org-element-map tree 'bold 'identity nil t))))))
  103. ;; Allow nil contents.
  104. (should-not
  105. (org-test-with-temp-text "* Headline\n *a*"
  106. (let ((tree (org-element-parse-buffer)))
  107. (org-element-set-contents (org-element-map tree 'bold 'identity nil t))
  108. (org-element-contents (org-element-map tree 'bold 'identity nil t))))))
  109. (ert-deftest test-org-element/secondary-p ()
  110. "Test `org-element-secondary-p' specifications."
  111. ;; In a secondary string, return property name.
  112. (should
  113. (eq :title
  114. (org-test-with-temp-text "* Headline *object*"
  115. (org-element-map (org-element-parse-buffer) 'bold
  116. (lambda (object) (org-element-secondary-p object))
  117. nil t))))
  118. ;; Outside a secondary string, return nil.
  119. (should-not
  120. (org-test-with-temp-text "Paragraph *object*"
  121. (org-element-map (org-element-parse-buffer) 'bold
  122. (lambda (object) (org-element-type (org-element-secondary-p object)))
  123. nil t))))
  124. (ert-deftest test-org-element/adopt-elements ()
  125. "Test `org-element-adopt-elements' specifications."
  126. ;; Adopt an element.
  127. (should
  128. (equal '(plain-text italic)
  129. (org-test-with-temp-text "* Headline\n *a*"
  130. (let ((tree (org-element-parse-buffer)))
  131. (org-element-adopt-elements
  132. (org-element-map tree 'bold 'identity nil t) '(italic nil "a"))
  133. (mapcar (lambda (blob) (org-element-type blob))
  134. (org-element-contents
  135. (org-element-map tree 'bold 'identity nil t)))))))
  136. ;; Adopt a string.
  137. (should
  138. (equal '("a" "b")
  139. (org-test-with-temp-text "* Headline\n *a*"
  140. (let ((tree (org-element-parse-buffer)))
  141. (org-element-adopt-elements
  142. (org-element-map tree 'bold 'identity nil t) "b")
  143. (org-element-contents
  144. (org-element-map tree 'bold 'identity nil t)))))))
  145. (ert-deftest test-org-element/extract-element ()
  146. "Test `org-element-extract-element' specifications."
  147. ;; Extract a greater element.
  148. (should
  149. (equal '(org-data nil)
  150. (org-test-with-temp-text "* Headline"
  151. (let* ((tree (org-element-parse-buffer))
  152. (element (org-element-map tree 'headline 'identity nil t)))
  153. (org-element-extract-element element)
  154. tree))))
  155. ;; Extract an element.
  156. (should-not
  157. (org-element-map
  158. (org-test-with-temp-text "Paragraph"
  159. (let* ((tree (org-element-parse-buffer))
  160. (element (org-element-map tree 'paragraph 'identity nil t)))
  161. (org-element-extract-element element)
  162. tree))
  163. 'paragraph
  164. 'identity))
  165. ;; Extract an object, even in a secondary string.
  166. (should-not
  167. (org-element-map
  168. (org-test-with-temp-text "*bold*"
  169. (let* ((tree (org-element-parse-buffer))
  170. (element (org-element-map tree 'bold 'identity nil t)))
  171. (org-element-extract-element element)
  172. tree))
  173. 'bold
  174. 'identity))
  175. (should-not
  176. (org-element-map
  177. (org-test-with-temp-text "* Headline *bold*"
  178. (let* ((tree (org-element-parse-buffer))
  179. (element (org-element-map tree 'bold 'identity nil t)))
  180. (org-element-extract-element element)
  181. tree))
  182. 'bold
  183. 'identity))
  184. ;; Return value doesn't have any :parent set.
  185. (should-not
  186. (org-element-property
  187. :parent
  188. (org-test-with-temp-text "* Headline\n Paragraph with *bold* text."
  189. (let* ((tree (org-element-parse-buffer))
  190. (element (org-element-map tree 'bold 'identity nil t)))
  191. (org-element-extract-element element))))))
  192. (ert-deftest test-org-element/insert-before ()
  193. "Test `org-element-insert-before' specifications."
  194. ;; Standard test.
  195. (should
  196. (equal
  197. '(italic entity bold)
  198. (org-test-with-temp-text "/some/ *paragraph*"
  199. (let* ((tree (org-element-parse-buffer))
  200. (paragraph (org-element-map tree 'paragraph 'identity nil t))
  201. (bold (org-element-map tree 'bold 'identity nil t)))
  202. (org-element-insert-before '(entity (:name "\\alpha")) bold)
  203. (org-element-map tree '(bold entity italic) #'org-element-type nil)))))
  204. ;; Insert an object in a secondary string.
  205. (should
  206. (equal
  207. '(entity italic)
  208. (org-test-with-temp-text "* /A/\n Paragraph."
  209. (let* ((tree (org-element-parse-buffer))
  210. (headline (org-element-map tree 'headline 'identity nil t))
  211. (italic (org-element-map tree 'italic 'identity nil t)))
  212. (org-element-insert-before '(entity (:name "\\alpha")) italic)
  213. (org-element-map (org-element-property :title headline) '(entity italic)
  214. #'org-element-type))))))
  215. (ert-deftest test-org-element/set-element ()
  216. "Test `org-element-set-element' specifications."
  217. ;; Check if new element is inserted.
  218. (should
  219. (org-test-with-temp-text "* Headline\n*a*"
  220. (let* ((tree (org-element-parse-buffer))
  221. (bold (org-element-map tree 'bold 'identity nil t)))
  222. (org-element-set-element bold '(italic nil "b"))
  223. (org-element-map tree 'italic 'identity))))
  224. ;; Check if old element is removed.
  225. (should-not
  226. (org-test-with-temp-text "* Headline\n*a*"
  227. (let* ((tree (org-element-parse-buffer))
  228. (bold (org-element-map tree 'bold 'identity nil t)))
  229. (org-element-set-element bold '(italic nil "b"))
  230. (org-element-map tree 'bold 'identity))))
  231. ;; Check if :parent property is correctly set.
  232. (should
  233. (eq 'paragraph
  234. (org-test-with-temp-text "* Headline\n*a*"
  235. (let* ((tree (org-element-parse-buffer))
  236. (bold (org-element-map tree 'bold 'identity nil t)))
  237. (org-element-set-element bold '(italic nil "b"))
  238. (org-element-type
  239. (org-element-property
  240. :parent (org-element-map tree 'italic 'identity nil t)))))))
  241. ;; Allow to replace strings with elements.
  242. (should
  243. (equal '("b")
  244. (org-test-with-temp-text "* Headline"
  245. (let* ((tree (org-element-parse-buffer))
  246. (text (org-element-map tree 'plain-text 'identity nil t)))
  247. (org-element-set-element text (list 'bold nil "b"))
  248. (org-element-map tree 'plain-text 'identity)))))
  249. ;; Allow to replace elements with strings.
  250. (should
  251. (equal "a"
  252. (org-test-with-temp-text "* =verbatim="
  253. (let* ((tree (org-element-parse-buffer))
  254. (verb (org-element-map tree 'verbatim 'identity nil t)))
  255. (org-element-set-element verb "a")
  256. (org-element-map tree 'plain-text 'identity nil t)))))
  257. ;; Allow to replace strings with strings.
  258. (should
  259. (equal "b"
  260. (org-test-with-temp-text "a"
  261. (let* ((tree (org-element-parse-buffer))
  262. (text (org-element-map tree 'plain-text 'identity nil t)))
  263. (org-element-set-element text "b")
  264. (org-element-map tree 'plain-text 'identity nil t))))))
  265. (ert-deftest test-org-element/copy ()
  266. "Test `org-element-copy' specifications."
  267. ;; Preserve type.
  268. (should (eq 'bold
  269. (org-test-with-temp-text "*bold*"
  270. (org-element-type (org-element-copy (org-element-context))))))
  271. (should (eq 'plain-text
  272. (org-test-with-temp-text "*bold*"
  273. (org-element-type
  274. (org-element-map (org-element-parse-buffer) 'plain-text
  275. #'org-element-copy nil t)))))
  276. ;; Preserve properties except `:parent'.
  277. (should (= 7
  278. (org-test-with-temp-text "*bold*"
  279. (org-element-property
  280. :end (org-element-copy (org-element-context))))))
  281. (should-not
  282. (org-test-with-temp-text "*bold*"
  283. (org-element-property
  284. :parent (org-element-copy (org-element-context)))))
  285. (should-not
  286. (org-test-with-temp-text "*bold*"
  287. (org-element-property
  288. :parent
  289. (org-element-map (org-element-parse-buffer) 'plain-text
  290. #'org-element-copy nil t))))
  291. ;; Copying nil returns nil.
  292. (should-not (org-element-copy nil))
  293. ;; Return a copy secondary strings.
  294. (should (equal '("text") (org-element-copy '("text"))))
  295. (should-not (eq '("text") (org-element-copy '("text")))))
  296. ;;; Test Parsers
  297. ;;;; Affiliated Keywords
  298. (ert-deftest test-org-element/affiliated-keywords-parser ()
  299. "Test affiliated keywords parsing."
  300. ;; Read simple keywords.
  301. (should
  302. (equal "para"
  303. (org-element-property
  304. :name
  305. (org-test-with-temp-text "#+NAME: para\nParagraph"
  306. (org-element-at-point)))))
  307. (should
  308. (= 1
  309. (org-element-property
  310. :begin
  311. (org-test-with-temp-text "#+NAME: para\nParagraph"
  312. (org-element-at-point)))))
  313. ;; Parse multiple keywords.
  314. (should
  315. (equal
  316. '("line2" "line1")
  317. (org-element-property
  318. :attr_ascii
  319. (org-test-with-temp-text
  320. "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph"
  321. (org-element-at-point)))))
  322. ;; Parse "parsed" keywords.
  323. (should
  324. (equal
  325. '(("caption"))
  326. (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
  327. (car (org-element-property :caption (org-element-at-point))))))
  328. ;; Parse dual keywords.
  329. (should
  330. (equal
  331. '((("long") "short"))
  332. (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
  333. (org-element-property :caption (org-element-at-point)))))
  334. ;; Allow multiple caption keywords.
  335. (should
  336. (equal
  337. '((("l2") "s2") (("l1") "s1"))
  338. (org-test-with-temp-text "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph"
  339. (org-element-property :caption (org-element-at-point)))))
  340. (should
  341. (equal
  342. '((("l1")) (nil "s1"))
  343. (org-test-with-temp-text "#+CAPTION[s1]:\n#+CAPTION: l1\nParagraph"
  344. (org-element-property :caption (org-element-at-point)))))
  345. ;; Corner case: orphaned keyword at the end of an element.
  346. (should
  347. (eq 'keyword
  348. (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
  349. (progn (search-forward "name")
  350. (org-element-type (org-element-at-point))))))
  351. (should-not
  352. (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
  353. (progn (search-forward "Some")
  354. (org-element-property :name (org-element-at-point))))))
  355. ;;;; Babel Call
  356. (ert-deftest test-org-element/babel-call-parser ()
  357. "Test `babel-call' parsing."
  358. ;; Standard test.
  359. (should
  360. (org-test-with-temp-text "#+CALL: test()"
  361. (org-element-map (org-element-parse-buffer) 'babel-call 'identity)))
  362. ;; Ignore case.
  363. (should
  364. (org-test-with-temp-text "#+call: test()"
  365. (org-element-map (org-element-parse-buffer) 'babel-call 'identity)))
  366. ;; Handle non-empty blank line at the end of buffer.
  367. (should
  368. (org-test-with-temp-text "#+CALL: test()\n "
  369. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  370. ;;;; Bold
  371. (ert-deftest test-org-element/bold-parser ()
  372. "Test `bold' parser."
  373. ;; Standard test.
  374. (should
  375. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  376. (org-test-with-temp-text "*bold*"
  377. (org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
  378. ;; Multi-line markup.
  379. (should
  380. (equal
  381. (org-element-contents
  382. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  383. (org-test-with-temp-text "*first line\nsecond line*"
  384. (org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
  385. '("first line\nsecond line"))))
  386. ;;;; Center Block
  387. (ert-deftest test-org-element/center-block-parser ()
  388. "Test `center-block' parser."
  389. ;; Standard test.
  390. (should
  391. (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
  392. (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
  393. ;; Ignore case.
  394. (should
  395. (org-test-with-temp-text "#+begin_center\nText\n#+end_center"
  396. (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
  397. ;; Ignore incomplete block.
  398. (should-not
  399. (org-test-with-temp-text "#+BEGIN_CENTER"
  400. (org-element-map (org-element-parse-buffer) 'center-block
  401. 'identity nil t)))
  402. ;; Handle non-empty blank line at the end of buffer.
  403. (should
  404. (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
  405. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  406. ;;;; Clock
  407. (ert-deftest test-org-element/clock-parser ()
  408. "Test `clock' parser."
  409. ;; Running clock.
  410. (let ((clock (org-test-with-temp-text "CLOCK: [2012-01-01 sun. 00:01]"
  411. (org-element-at-point))))
  412. (should (eq (org-element-property :status clock) 'running))
  413. (should
  414. (equal (org-element-property :raw-value
  415. (org-element-property :value clock))
  416. "[2012-01-01 sun. 00:01]"))
  417. (should-not (org-element-property :duration clock)))
  418. ;; Closed clock.
  419. (let ((clock
  420. (org-test-with-temp-text
  421. "CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
  422. (org-element-at-point))))
  423. (should (eq (org-element-property :status clock) 'closed))
  424. (should (equal (org-element-property :raw-value
  425. (org-element-property :value clock))
  426. "[2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02]"))
  427. (should (equal (org-element-property :duration clock) "0:01"))))
  428. ;;;; Code
  429. (ert-deftest test-org-element/code-parser ()
  430. "Test `code' parser."
  431. ;; Regular test.
  432. (should
  433. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  434. (org-test-with-temp-text "~code~"
  435. (org-element-map (org-element-parse-buffer) 'code 'identity))))
  436. ;; Multi-line markup.
  437. (should
  438. (equal
  439. (org-element-property
  440. :value
  441. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  442. (org-test-with-temp-text "~first line\nsecond line~"
  443. (org-element-map
  444. (org-element-parse-buffer) 'code 'identity nil t))))
  445. "first line\nsecond line")))
  446. ;;;; Comment
  447. (ert-deftest test-org-element/comment-parser ()
  448. "Test `comment' parser."
  449. ;; Regular comment.
  450. (should
  451. (org-test-with-temp-text "# Comment"
  452. (org-element-map (org-element-parse-buffer) 'comment 'identity)))
  453. ;; Inline comment.
  454. (should
  455. (org-test-with-temp-text " # Comment"
  456. (org-element-map (org-element-parse-buffer) 'comment 'identity)))
  457. ;; Preserve indentation.
  458. (should
  459. (equal
  460. (org-element-property
  461. :value
  462. (org-test-with-temp-text "# No blank\n# One blank"
  463. (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
  464. "No blank\n One blank"))
  465. ;; Comment with blank lines.
  466. (should
  467. (equal
  468. (org-element-property
  469. :value
  470. (org-test-with-temp-text "# First part\n# \n#\n# Second part"
  471. (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
  472. "First part\n\n\nSecond part"))
  473. ;; Do not mix comments and keywords.
  474. (should
  475. (eq 1
  476. (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
  477. (length (org-element-map (org-element-parse-buffer) 'comment
  478. 'identity)))))
  479. (should
  480. (equal "comment"
  481. (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
  482. (org-element-property
  483. :value
  484. (org-element-map (org-element-parse-buffer) 'comment
  485. 'identity nil t)))))
  486. ;; Correctly handle non-empty blank lines at the end of buffer.
  487. (should
  488. (org-test-with-temp-text "# A\n "
  489. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  490. ;;;; Comment Block
  491. (ert-deftest test-org-element/comment-block-parser ()
  492. "Test `comment-block' parser."
  493. ;; Standard test.
  494. (should
  495. (org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
  496. (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
  497. ;; Ignore case.
  498. (should
  499. (org-test-with-temp-text "#+begin_comment\nText\n#+end_comment"
  500. (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
  501. ;; Ignore incomplete block.
  502. (should-not
  503. (org-test-with-temp-text "#+BEGIN_COMMENT"
  504. (org-element-map (org-element-parse-buffer) 'comment-block
  505. 'identity nil t)))
  506. ;; Handle non-empty blank line at the end of buffer.
  507. (should
  508. (org-test-with-temp-text "#+BEGIN_COMMENT\nC\n#+END_COMMENT\n "
  509. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  510. ;;;; Diary Sexp
  511. (ert-deftest test-org-element/diary-sexp-parser ()
  512. "Test `diary-sexp' parser."
  513. ;; Standard test.
  514. (should
  515. (eq 'diary-sexp
  516. (org-test-with-temp-text
  517. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old"
  518. (org-element-type (org-element-at-point)))))
  519. ;; Diary sexp must live at beginning of line
  520. (should-not
  521. (eq 'diary-sexp
  522. (org-test-with-temp-text " %%(org-bbdb-anniversaries)"
  523. (org-element-type (org-element-at-point)))))
  524. ;; Handle non-empty blank line at the end of buffer.
  525. (should
  526. (org-test-with-temp-text "%%(org-bbdb-anniversaries)\n "
  527. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  528. ;;;; Drawer
  529. (ert-deftest test-org-element/drawer-parser ()
  530. "Test `drawer' parser."
  531. ;; Standard test.
  532. (should
  533. (org-test-with-temp-text ":TEST:\nText\n:END:"
  534. (org-element-map (org-element-parse-buffer) 'drawer 'identity)))
  535. ;; Ignore incomplete drawer.
  536. (should-not
  537. (org-test-with-temp-text ":TEST:"
  538. (org-element-map (org-element-parse-buffer) 'drawer 'identity nil t)))
  539. ;; Handle non-empty blank line at the end of buffer.
  540. (should
  541. (org-test-with-temp-text ":TEST:\nC\n:END:\n "
  542. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  543. ;;;; Dynamic Block
  544. (ert-deftest test-org-element/dynamic-block-parser ()
  545. "Test `dynamic-block' parser."
  546. ;; Standard test.
  547. (should
  548. (org-test-with-temp-text
  549. "#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
  550. (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
  551. ;; Ignore case.
  552. (should
  553. (org-test-with-temp-text
  554. "#+begin: myblock :param1 val1 :param2 val2\nText\n#+end:"
  555. (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
  556. ;; Ignore incomplete block.
  557. (should-not
  558. (org-test-with-temp-text "#+BEGIN: myblock :param1 val1 :param2 val2"
  559. (org-element-map (org-element-parse-buffer) 'dynamic-block
  560. 'identity nil t)))
  561. ;; Handle non-empty blank line at the end of buffer.
  562. (should
  563. (org-test-with-temp-text "#+BEGIN: myblock :param val1\nC\n#+END:\n "
  564. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  565. ;;;; Entity
  566. (ert-deftest test-org-element/entity-parser ()
  567. "Test `entity' parser."
  568. ;; Without brackets.
  569. (should
  570. (org-test-with-temp-text "\\sin"
  571. (org-element-map (org-element-parse-buffer) 'entity 'identity)))
  572. ;; With brackets.
  573. (should
  574. (org-element-property
  575. :use-brackets-p
  576. (org-test-with-temp-text "\\alpha{}text"
  577. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
  578. ;; User-defined entity.
  579. (should
  580. (equal
  581. (org-element-property
  582. :name
  583. (let ((org-entities-user
  584. '(("test" "test" nil "test" "test" "test" "test"))))
  585. (org-test-with-temp-text "\\test"
  586. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
  587. "test"))
  588. ;; Special case: entity at the end of a container.
  589. (should
  590. (eq 'entity
  591. (org-test-with-temp-text "*\\alpha \\beta*"
  592. (search-forward "be")
  593. (org-element-type (org-element-context))))))
  594. ;;;; Example Block
  595. (ert-deftest test-org-element/example-block-parser ()
  596. "Test `example-block' parser."
  597. ;; Standard test.
  598. (should
  599. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
  600. (org-element-map (org-element-parse-buffer) 'example-block 'identity)))
  601. ;; Ignore incomplete block.
  602. (should-not
  603. (eq 'example-block
  604. (org-test-with-temp-text "#+BEGIN_EXAMPLE"
  605. (org-element-type (org-element-at-point)))))
  606. ;; Properly un-escape code.
  607. (should
  608. (equal "* Headline\n #+keyword\nText\n"
  609. (org-test-with-temp-text
  610. "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText\n#+END_EXAMPLE"
  611. (org-element-property :value (org-element-at-point)))))
  612. ;; Remove block indentation according to block boundaries, unless
  613. ;; block contents are less indented than block boundaries.
  614. (should
  615. (equal " L1\nL2\n"
  616. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n L1\n L2\n #+END_EXAMPLE"
  617. (let ((org-src-preserve-indentation t))
  618. (org-element-property :value (org-element-at-point))))))
  619. (should
  620. (equal
  621. " L1\n L2\n"
  622. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n L1\n L2\n #+END_EXAMPLE"
  623. (let ((org-src-preserve-indentation t))
  624. (org-element-property :value (org-element-at-point))))))
  625. ;; Handle non-empty blank line at the end of buffer.
  626. (should
  627. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nC\n#+END_EXAMPLE\n "
  628. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  629. (ert-deftest test-org-element/block-switches ()
  630. "Test `example-block' and `src-block' switches parsing."
  631. (let ((org-coderef-label-format "(ref:%s)"))
  632. ;; 1. Test "-i" switch.
  633. (should-not
  634. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  635. (org-element-property :preserve-indent (org-element-at-point))))
  636. (should
  637. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  638. (org-element-property :preserve-indent (org-element-at-point))))
  639. (should-not
  640. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  641. (org-element-property :preserve-indent (org-element-at-point))))
  642. (should
  643. (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
  644. (org-element-property :preserve-indent (org-element-at-point))))
  645. ;; 2. "-n -r -k" combination should number lines, retain labels but
  646. ;; not use them in coderefs.
  647. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
  648. (let ((element (org-element-at-point)))
  649. (should (org-element-property :number-lines element))
  650. (should (org-element-property :retain-labels element))
  651. (should-not (org-element-property :use-labels element))))
  652. (org-test-with-temp-text
  653. "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
  654. (let ((element (org-element-at-point)))
  655. (should (org-element-property :number-lines element))
  656. (should (org-element-property :retain-labels element))
  657. (should-not (org-element-property :use-labels element))))
  658. ;; 3. "-n -r" combination should number-lines remove labels and not
  659. ;; use them in coderefs.
  660. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
  661. (let ((element (org-element-at-point)))
  662. (should (org-element-property :number-lines element))
  663. (should-not (org-element-property :retain-labels element))
  664. (should-not (org-element-property :use-labels element))))
  665. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
  666. (let ((element (org-element-at-point)))
  667. (should (org-element-property :number-lines element))
  668. (should-not (org-element-property :retain-labels element))
  669. (should-not (org-element-property :use-labels element))))
  670. ;; 4. "-n" or "+n" should number lines, retain labels and use them
  671. ;; in coderefs.
  672. (should
  673. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
  674. (let ((element (org-element-at-point)))
  675. (and (org-element-property :number-lines element)
  676. (org-element-property :retain-labels element)
  677. (org-element-property :use-labels element)))))
  678. (should
  679. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
  680. (let ((element (org-element-at-point)))
  681. (and (org-element-property :number-lines element)
  682. (org-element-property :retain-labels element)
  683. (org-element-property :use-labels element)))))
  684. (should
  685. (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
  686. (let ((element (org-element-at-point)))
  687. (and (org-element-property :number-lines element)
  688. (org-element-property :retain-labels element)
  689. (org-element-property :use-labels element)))))
  690. (should
  691. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
  692. (let ((element (org-element-at-point)))
  693. (and (org-element-property :number-lines element)
  694. (org-element-property :retain-labels element)
  695. (org-element-property :use-labels element)))))
  696. ;; 5. No switch should not number lines, but retain labels and use
  697. ;; them in coderefs.
  698. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  699. (let ((element (org-element-at-point)))
  700. (should (not (org-element-property :number-lines element)))
  701. (should (org-element-property :retain-labels element))
  702. (should (org-element-property :use-labels element))))
  703. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  704. (let ((element (org-element-at-point)))
  705. (should (not (org-element-property :number-lines element)))
  706. (should (org-element-property :retain-labels element))
  707. (should (org-element-property :use-labels element))))
  708. ;; 6. "-r" switch only: do not number lines, remove labels, and
  709. ;; don't use labels in coderefs.
  710. (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
  711. (let ((element (org-element-at-point)))
  712. (should (not (org-element-property :number-lines element)))
  713. (should (not (org-element-property :retain-labels element)))
  714. (should (not (org-element-property :use-labels element)))))
  715. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
  716. (let ((element (org-element-at-point)))
  717. (should (not (org-element-property :number-lines element)))
  718. (should (not (org-element-property :retain-labels element)))
  719. (should (not (org-element-property :use-labels element)))))
  720. ;; 7. Recognize coderefs with user-defined syntax.
  721. (should
  722. (equal
  723. "[ref:%s]"
  724. (org-test-with-temp-text
  725. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
  726. (org-element-property :label-fmt (org-element-at-point)))))
  727. (should
  728. (equal
  729. "[ref:%s]"
  730. (org-test-with-temp-text
  731. "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
  732. (org-element-property :label-fmt (org-element-at-point)))))))
  733. ;;;; Export Block
  734. (ert-deftest test-org-element/export-block-parser ()
  735. "Test `export-block' parser."
  736. ;; Standard test.
  737. (should
  738. (org-test-with-temp-text "#+BEGIN_LATEX\nText\n#+END_LATEX"
  739. (org-element-map
  740. (let ((org-element-block-name-alist
  741. '(("LATEX" . org-element-export-block-parser))))
  742. (org-element-parse-buffer))
  743. 'export-block 'identity)))
  744. ;; Ignore case.
  745. (should
  746. (let ((org-element-block-name-alist
  747. '(("LATEX" . org-element-export-block-parser))))
  748. (org-test-with-temp-text "#+begin_latex\nText\n#+end_latex"
  749. (org-element-map (org-element-parse-buffer) 'export-block 'identity))))
  750. ;; Ignore incomplete block.
  751. (should-not
  752. (let ((org-element-block-name-alist
  753. '(("LATEX" . org-element-export-block-parser))))
  754. (org-test-with-temp-text "#+BEGIN_LATEX"
  755. (org-element-map (org-element-parse-buffer) 'export-block
  756. 'identity nil t))))
  757. ;; Handle non-empty blank line at the end of buffer.
  758. (should
  759. (let ((org-element-block-name-alist
  760. '(("LATEX" . org-element-export-block-parser))))
  761. (org-test-with-temp-text "#+BEGIN_LATEX\nC\n#+END_LATEX\n "
  762. (= (org-element-property :end (org-element-at-point)) (point-max))))))
  763. ;;;; Export Snippet
  764. (ert-deftest test-org-element/export-snippet-parser ()
  765. "Test `export-snippet' parser."
  766. (should
  767. (equal
  768. '("back-end" . "contents")
  769. (org-test-with-temp-text "@@back-end:contents@@"
  770. (org-element-map
  771. (org-element-parse-buffer) 'export-snippet
  772. (lambda (snippet) (cons (org-element-property :back-end snippet)
  773. (org-element-property :value snippet)))
  774. nil t)))))
  775. ;;;; Fixed Width
  776. (ert-deftest test-org-element/fixed-width-parser ()
  777. "Test fixed-width area parsing."
  778. ;; Preserve indentation.
  779. (should
  780. (org-test-with-temp-text ": no blank\n: one blank"
  781. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
  782. ;; Fixed-width with empty lines.
  783. (should
  784. (org-test-with-temp-text ": first part\n:\n: \n: second part"
  785. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
  786. ;; Parse indented fixed-width markers.
  787. (should
  788. (org-test-with-temp-text "Text\n : no blank\n : one blank"
  789. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
  790. ;; Distinguish fixed-width areas within a list and outside of it.
  791. (should
  792. (= 2
  793. (length
  794. (org-test-with-temp-text "
  795. - Item
  796. : fixed-width inside
  797. : fixed-width outside"
  798. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))))
  799. ;; Handle non-empty blank line at the end of buffer.
  800. (should
  801. (org-test-with-temp-text ": A\n "
  802. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  803. ;;;; Footnote Definition
  804. (ert-deftest test-org-element/footnote-definition-parser ()
  805. "Test `footnote-definition' parser."
  806. (should
  807. (org-test-with-temp-text "[fn:1] Definition"
  808. (org-element-map (org-element-parse-buffer) 'footnote-definition
  809. 'identity nil t)))
  810. ;; Footnote with more contents
  811. (should
  812. (= 29
  813. (org-element-property
  814. :end
  815. (org-test-with-temp-text "[fn:1] Definition\n\n| a | b |"
  816. (org-element-map (org-element-parse-buffer) 'footnote-definition
  817. 'identity nil t)))))
  818. ;; Footnote starting with special syntax.
  819. (should-not
  820. (org-test-with-temp-text "[fn:1] - no item"
  821. (org-element-map (org-element-parse-buffer) 'item 'identity)))
  822. ;; Correctly handle footnote starting with an empty line.
  823. (should
  824. (= 9
  825. (org-test-with-temp-text "[fn:1]\n\n Body"
  826. (org-element-property :contents-begin (org-element-at-point)))))
  827. ;; Handle non-empty blank line at the end of buffer.
  828. (should
  829. (org-test-with-temp-text "[fn:1] Definition\n "
  830. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  831. ;;;; Footnotes Reference.
  832. (ert-deftest test-org-element/footnote-reference-parser ()
  833. "Test `footnote-reference' parser."
  834. ;; Parse a standard reference.
  835. (should
  836. (org-test-with-temp-text "Text[fn:label]"
  837. (org-element-map
  838. (org-element-parse-buffer) 'footnote-reference 'identity)))
  839. ;; Parse a normalized reference.
  840. (should
  841. (org-test-with-temp-text "Text[1]"
  842. (org-element-map
  843. (org-element-parse-buffer) 'footnote-reference 'identity)))
  844. ;; Parse an inline reference.
  845. (should
  846. (org-test-with-temp-text "Text[fn:test:def]"
  847. (org-element-map
  848. (org-element-parse-buffer) 'footnote-reference 'identity)))
  849. ;; Parse an anonymous reference.
  850. (should
  851. (org-test-with-temp-text "Text[fn::def]"
  852. (org-element-map
  853. (org-element-parse-buffer) 'footnote-reference 'identity)))
  854. ;; Parse nested footnotes.
  855. (should
  856. (= 2
  857. (length
  858. (org-test-with-temp-text "Text[fn::def [fn:label]]"
  859. (org-element-map
  860. (org-element-parse-buffer) 'footnote-reference 'identity)))))
  861. ;; Parse adjacent footnotes.
  862. (should
  863. (org-test-with-temp-text "Text[fn:label1][fn:label2]"
  864. (= 2
  865. (length
  866. (org-element-map
  867. (org-element-parse-buffer) 'footnote-reference 'identity)))))
  868. ;; Only properly closed footnotes are recognized as such.
  869. (should-not
  870. (org-test-with-temp-text "Text[fn:label"
  871. (org-element-map
  872. (org-element-parse-buffer) 'footnote-reference 'identity))))
  873. ;;;; Headline
  874. (ert-deftest test-org-element/headline-comment-keyword ()
  875. "Test COMMENT keyword recognition."
  876. ;; Reference test.
  877. (org-test-with-temp-text "* Headline"
  878. (let ((org-comment-string "COMMENT"))
  879. (should-not (org-element-property :commentedp (org-element-at-point)))))
  880. ;; Standard position.
  881. (org-test-with-temp-text "* COMMENT Headline"
  882. (let ((org-comment-string "COMMENT")
  883. (headline (org-element-at-point)))
  884. (should (org-element-property :commentedp headline))
  885. (should (equal (org-element-property :raw-value headline) "Headline"))))
  886. ;; Case sensitivity.
  887. (org-test-with-temp-text "* COMMENT Headline"
  888. (let* ((org-comment-string "Comment")
  889. (headline (org-element-at-point)))
  890. (should-not (org-element-property :commentedp headline))
  891. (should (equal (org-element-property :raw-value headline)
  892. "COMMENT Headline"))))
  893. ;; With another keyword.
  894. (org-test-with-temp-text "* TODO COMMENT Headline"
  895. (let* ((org-comment-string "COMMENT")
  896. (org-todo-keywords '((sequence "TODO" "DONE")))
  897. (headline (org-element-at-point)))
  898. (should (org-element-property :commentedp headline))
  899. (should (equal (org-element-property :raw-value headline) "Headline"))))
  900. ;; With the keyword only.
  901. (org-test-with-temp-text "* COMMENT"
  902. (let* ((org-comment-string "COMMENT")
  903. (headline (org-element-at-point)))
  904. (should (org-element-property :commentedp headline))
  905. (should (equal (org-element-property :raw-value headline) "")))))
  906. (ert-deftest test-org-element/headline-archive-tag ()
  907. "Test ARCHIVE tag recognition."
  908. ;; Reference test.
  909. (should-not
  910. (org-test-with-temp-text "* Headline"
  911. (let ((org-archive-tag "ARCHIVE"))
  912. (org-element-property :archivedp (org-element-at-point)))))
  913. ;; Single tag.
  914. (org-test-with-temp-text "* Headline :ARCHIVE:"
  915. (let ((org-archive-tag "ARCHIVE"))
  916. (let ((headline (org-element-at-point)))
  917. (should (org-element-property :archivedp headline))
  918. ;; Test tag removal.
  919. (should-not (org-element-property :tags headline)))))
  920. ;; Multiple tags.
  921. (org-test-with-temp-text "* Headline :test:ARCHIVE:"
  922. (let ((org-archive-tag "ARCHIVE"))
  923. (let ((headline (org-element-at-point)))
  924. (should (org-element-property :archivedp headline))
  925. ;; Test tag removal.
  926. (should (equal (org-element-property :tags headline) '("test"))))))
  927. ;; Tag is case-sensitive.
  928. (should-not
  929. (org-test-with-temp-text "* Headline :ARCHIVE:"
  930. (let ((org-archive-tag "Archive"))
  931. (org-element-property :archivedp (org-element-at-point))))))
  932. (ert-deftest test-org-element/headline-properties ()
  933. "Test properties from property drawer."
  934. ;; All properties from property drawer have their symbol upper
  935. ;; cased.
  936. (should
  937. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
  938. (org-element-property :FOO (org-element-at-point))))
  939. (should-not
  940. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
  941. (org-element-property :foo (org-element-at-point))))
  942. ;; Also parse properties associated in inlinetasks.
  943. (when (featurep 'org-inlinetask)
  944. (should
  945. (org-test-with-temp-text "*************** Inlinetask
  946. :PROPERTIES:
  947. :foo: bar
  948. :END:
  949. *************** END"
  950. (org-element-property :FOO (org-element-at-point)))))
  951. ;; Do not find property drawer in a verbatim area.
  952. (should-not
  953. (org-test-with-temp-text
  954. "* Headline
  955. #+BEGIN_EXAMPLE
  956. :PROPERTIES:
  957. :foo: bar
  958. :END:
  959. #+END_EXAMPLE"
  960. (org-element-property :FOO (org-element-at-point))))
  961. ;; Do not use properties from a drawer associated to an inlinetask.
  962. (when (featurep 'org-inlinetask)
  963. (should-not
  964. (org-test-with-temp-text
  965. "* Headline
  966. *************** Inlinetask
  967. :PROPERTIES:
  968. :foo: bar
  969. :END:
  970. *************** END"
  971. (org-element-property
  972. :FOO (let ((org-inlinetask-min-level 15)) (org-element-at-point))))))
  973. ;; Do not find incomplete drawers.
  974. (should-not
  975. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar"
  976. (org-element-property :FOO (org-element-at-point)))))
  977. ;;;; Horizontal Rule
  978. (ert-deftest test-org-element/horizontal-rule-parser ()
  979. "Test `horizontal-rule' parser."
  980. ;; Standard.
  981. (should
  982. (org-test-with-temp-text "-----"
  983. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  984. ;; Indented.
  985. (should
  986. (org-test-with-temp-text " -----"
  987. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  988. ;; Hyphen must be alone on the line.
  989. (should-not
  990. (org-test-with-temp-text "-----wrong"
  991. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  992. ;; 4 hyphens is too small.
  993. (should-not
  994. (org-test-with-temp-text "----"
  995. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  996. ;; Handle non-empty blank line at the end of buffer.
  997. (should
  998. (org-test-with-temp-text "-----\n "
  999. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1000. ;;;; Inline Babel Call
  1001. (ert-deftest test-org-element/inline-babel-call-parser ()
  1002. "Test `inline-babel-call' parser."
  1003. (should
  1004. (org-test-with-temp-text "call_test()"
  1005. (org-element-map
  1006. (org-element-parse-buffer) 'inline-babel-call 'identity))))
  1007. ;;;; Inline Src Block
  1008. (ert-deftest test-org-element/inline-src-block-parser ()
  1009. "Test `inline-src-block' parser."
  1010. (should
  1011. (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
  1012. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1013. ;; With switches.
  1014. (should
  1015. (org-test-with-temp-text "src_emacs-lisp[:foo bar]{(+ 1 1)}"
  1016. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1017. (should
  1018. (org-test-with-temp-text "src_emacs-lisp[ :foo bar]{(+ 1 1)}"
  1019. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1020. ;; Empty switches.
  1021. (should
  1022. (org-test-with-temp-text "src_emacs-lisp[]{(+ 1 1)}"
  1023. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1024. ;; Invalid syntax.
  1025. (should-not
  1026. (org-test-with-temp-text "foosrc_emacs-lisp[]{(+ 1 1)}"
  1027. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1028. (should-not
  1029. (org-test-with-temp-text "src_emacs-lisp[]foo{(+ 1 1)}"
  1030. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1031. ;; Invalid language name
  1032. (should-not
  1033. (org-test-with-temp-text "src_emacs-\tlisp{(+ 1 1)}"
  1034. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1035. ;; Test parsing at the beginning of an item.
  1036. (should
  1037. (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
  1038. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity))))
  1039. ;;;; Inlinetask
  1040. (ert-deftest test-org-element/inlinetask-parser ()
  1041. "Test `inlinetask' parser."
  1042. (when (featurep 'org-inlinetask)
  1043. (let ((org-inlinetask-min-level 15))
  1044. ;; Regular inlinetask.
  1045. (should
  1046. (eq 'inlinetask
  1047. (org-test-with-temp-text
  1048. "*************** Task\nTest\n*************** END"
  1049. (org-element-type (org-element-at-point)))))
  1050. ;; Degenerate inlinetask.
  1051. (should
  1052. (eq 'inlinetask
  1053. (org-test-with-temp-text "*************** Task"
  1054. (org-element-type (org-element-at-point)))))
  1055. ;; Mixed inlinetasks.
  1056. (should-not
  1057. (org-test-with-temp-text
  1058. "
  1059. *************** Task
  1060. *************** Task2
  1061. Contents
  1062. *************** END"
  1063. (forward-line)
  1064. (goto-char (org-element-property :end (org-element-at-point)))
  1065. (eobp)))
  1066. ;; TODO keyword.
  1067. (should
  1068. (equal
  1069. "TODO"
  1070. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  1071. (org-test-with-temp-text "*************** TODO Task"
  1072. (org-element-property :todo-keyword (org-element-at-point))))))
  1073. ;; Planning info.
  1074. (should
  1075. (org-test-with-temp-text "
  1076. *************** Task
  1077. DEADLINE: <2012-03-29 thu.>
  1078. *************** END"
  1079. (forward-line)
  1080. (org-element-property :deadline (org-element-at-point))))
  1081. (should-not
  1082. (org-test-with-temp-text "
  1083. *************** Task
  1084. DEADLINE: <2012-03-29 thu.>"
  1085. (forward-line)
  1086. (org-element-property :deadline (org-element-at-point))))
  1087. ;; Priority.
  1088. (should
  1089. (eq
  1090. ?A
  1091. (org-test-with-temp-text "
  1092. *************** [#A] Task"
  1093. (forward-line)
  1094. (org-element-property :priority (org-element-at-point)))))
  1095. ;; Tags.
  1096. (should
  1097. (equal
  1098. '("test")
  1099. (org-test-with-temp-text "
  1100. *************** Task :test:"
  1101. (forward-line)
  1102. (org-element-property :tags (org-element-at-point)))))
  1103. ;; Regular properties are accessed through upper case keywords.
  1104. (should
  1105. (org-test-with-temp-text "
  1106. *************** Task
  1107. :PROPERTIES:
  1108. :foo: bar
  1109. :END:
  1110. *************** END"
  1111. (forward-line)
  1112. (org-element-property :FOO (org-element-at-point))))
  1113. (should-not
  1114. (org-test-with-temp-text "
  1115. *************** Task
  1116. :PROPERTIES:
  1117. :foo: bar
  1118. :END:
  1119. *************** END"
  1120. (forward-line)
  1121. (org-element-property :foo (org-element-at-point))))
  1122. ;; Handle non-empty blank line at the end of buffer.
  1123. (should
  1124. (org-test-with-temp-text "*************** Task\n*************** END\n "
  1125. (= (org-element-property :end (org-element-at-point)) (point-max)))))))
  1126. ;;;; Italic
  1127. (ert-deftest test-org-element/italic-parser ()
  1128. "Test `italic' parser."
  1129. ;; Regular test.
  1130. (should
  1131. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1132. (org-test-with-temp-text "/italic/"
  1133. (org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
  1134. ;; Multi-line markup.
  1135. (should
  1136. (equal
  1137. (org-element-contents
  1138. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1139. (org-test-with-temp-text "/first line\nsecond line/"
  1140. (org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
  1141. '("first line\nsecond line"))))
  1142. ;;;; Item
  1143. (ert-deftest test-org-element/item-parser ()
  1144. "Test `item' parser."
  1145. ;; Standard test.
  1146. (should
  1147. (org-test-with-temp-text "- item"
  1148. (org-element-map (org-element-parse-buffer) 'item 'identity)))
  1149. ;; Counter.
  1150. (should
  1151. (= 6
  1152. (org-element-property
  1153. :counter
  1154. (org-test-with-temp-text "6. [@6] item"
  1155. (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
  1156. ;; Tag
  1157. (should
  1158. (equal
  1159. '("tag")
  1160. (org-element-property
  1161. :tag
  1162. (org-test-with-temp-text "- tag :: description"
  1163. (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
  1164. ;; No tags in ordered lists.
  1165. (should-not
  1166. (org-element-property
  1167. :tag
  1168. (org-test-with-temp-text "1. tag :: description"
  1169. (org-element-map (org-element-parse-buffer) 'item 'identity nil t))))
  1170. ;; Check-boxes
  1171. (should
  1172. (equal
  1173. '(trans on off)
  1174. (org-test-with-temp-text "
  1175. - [-] item 1
  1176. - [X] item 1.1
  1177. - [ ] item 1.2"
  1178. (org-element-map
  1179. (org-element-parse-buffer) 'item
  1180. (lambda (item) (org-element-property :checkbox item))))))
  1181. ;; Item starting with special syntax.
  1182. (should
  1183. (equal '(("- item"))
  1184. (org-test-with-temp-text "- - item"
  1185. (org-element-map (org-element-parse-buffer) 'paragraph
  1186. 'org-element-contents))))
  1187. ;; Block in an item: ignore indentation within the block.
  1188. (should
  1189. (org-test-with-temp-text "- item\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1190. (forward-char)
  1191. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1192. ;;;; Keyword
  1193. (ert-deftest test-org-element/keyword-parser ()
  1194. "Test `keyword' parser."
  1195. ;; Standard test.
  1196. (should
  1197. (org-test-with-temp-text "#+KEYWORD: value"
  1198. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1199. ;; Keywords are case-insensitive.
  1200. (should
  1201. (org-test-with-temp-text "#+keyword: value"
  1202. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1203. ;; Affiliated keywords are not keywords.
  1204. (should-not
  1205. (org-test-with-temp-text "#+NAME: value
  1206. Paragraph"
  1207. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1208. ;; Do not mix keywords with Babel calls and dynamic blocks.
  1209. (should-not
  1210. (org-test-with-temp-text "#+CALL: fun()"
  1211. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1212. (should-not
  1213. (org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
  1214. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1215. ;; Handle non-empty blank line at the end of buffer.
  1216. (should
  1217. (org-test-with-temp-text "#+KEYWORD: value\n "
  1218. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1219. ;;;; Latex Environment
  1220. (ert-deftest test-org-element/latex-environment-parser ()
  1221. "Test `latex-environment' parser."
  1222. (should
  1223. (org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
  1224. (org-element-map (org-element-parse-buffer) 'latex-environment 'identity)))
  1225. ;; Allow nested environments.
  1226. (should
  1227. (equal
  1228. "\\begin{outer}
  1229. \\begin{inner}
  1230. e^{i\\pi}+1=0
  1231. \\end{inner}
  1232. \\end{outer}"
  1233. (org-test-with-temp-text "
  1234. \\begin{outer}
  1235. \\begin{inner}
  1236. e^{i\\pi}+1=0
  1237. \\end{inner}
  1238. \\end{outer}"
  1239. (org-element-property
  1240. :value
  1241. (org-element-map
  1242. (org-element-parse-buffer) 'latex-environment 'identity nil t)))))
  1243. ;; Allow environments with options and arguments.
  1244. (should
  1245. (eq 'latex-environment
  1246. (org-test-with-temp-text
  1247. "\\begin{theorem}[Euler]\ne^{i\\pi}+1=0\n\\end{theorem}"
  1248. (org-element-type (org-element-at-point)))))
  1249. (should
  1250. (eq 'latex-environment
  1251. (org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
  1252. (org-element-type (org-element-at-point)))))
  1253. ;; Allow environments without newline after \begin{.}.
  1254. (should
  1255. (eq 'latex-environment
  1256. (org-test-with-temp-text "\\begin{env}{arg}something\nvalue\n\\end{env}"
  1257. (org-element-type (org-element-at-point)))))
  1258. ;; Allow one-line environments.
  1259. (should
  1260. (eq 'latex-environment
  1261. (org-test-with-temp-text "\\begin{env}{arg}something\\end{env}"
  1262. (org-element-type (org-element-at-point)))))
  1263. ;; Should not allow different tags.
  1264. (should-not
  1265. (eq 'latex-environment
  1266. (org-test-with-temp-text "\\begin{env*}{arg}something\\end{env}"
  1267. (org-element-type (org-element-at-point)))))
  1268. ;; LaTeX environments must be on separate lines.
  1269. (should-not
  1270. (eq 'latex-environment
  1271. (org-test-with-temp-text "\\begin{env} x \\end{env} y"
  1272. (org-element-type (org-element-at-point)))))
  1273. (should-not
  1274. (eq 'latex-environment
  1275. (org-test-with-temp-text "y \\begin{env} x<point> \\end{env}"
  1276. (org-element-type (org-element-at-point)))))
  1277. ;; Handle non-empty blank line at the end of buffer.
  1278. (should
  1279. (org-test-with-temp-text "\\begin{env}\n\\end{env}\n "
  1280. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1281. ;;;; Latex Fragment
  1282. (ert-deftest test-org-element/latex-fragment-parser ()
  1283. "Test `latex-fragment' parser."
  1284. (should
  1285. (org-test-with-temp-text "$a$"
  1286. (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
  1287. (should
  1288. (org-test-with-temp-text "$$a$$"
  1289. (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
  1290. (should
  1291. (org-test-with-temp-text "\\(a\\)"
  1292. (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
  1293. (should
  1294. (org-test-with-temp-text "\\[a\\]"
  1295. (org-element-map
  1296. (org-element-parse-buffer) 'latex-fragment 'identity)))
  1297. ;; Test fragment at the beginning of an item.
  1298. (should
  1299. (eq 'latex-fragment
  1300. (org-test-with-temp-text "- $x$"
  1301. (progn (search-forward "$")
  1302. (org-element-type (org-element-context)))))))
  1303. ;;;; Line Break
  1304. (ert-deftest test-org-element/line-break-parser ()
  1305. "Test `line-break' parser."
  1306. ;; Regular test.
  1307. (should
  1308. (org-test-with-temp-text "Text \\\\"
  1309. (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
  1310. ;; Line break with trailing white spaces.
  1311. (should
  1312. (org-test-with-temp-text "Text \\\\ "
  1313. (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
  1314. ;; Three backslashes are too much.
  1315. (should-not
  1316. (org-test-with-temp-text "Text \\\\\\"
  1317. (org-element-map (org-element-parse-buffer) 'line-break 'identity))))
  1318. ;;;; Link
  1319. (ert-deftest test-org-element/link-parser ()
  1320. "Test `link' parser."
  1321. ;; Radio target.
  1322. (should
  1323. (equal
  1324. "radio"
  1325. (org-test-with-temp-text "<<<radio>>>A radio link"
  1326. (org-update-radio-target-regexp)
  1327. (org-element-property
  1328. :type
  1329. (org-element-map (org-element-parse-buffer) 'link #'identity nil t)))))
  1330. ;; Standard link.
  1331. ;;
  1332. ;; ... with description.
  1333. (should
  1334. (equal
  1335. '("Orgmode.org")
  1336. (org-test-with-temp-text "[[http://orgmode.org][Orgmode.org]]"
  1337. (org-element-contents
  1338. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1339. ;; ... without description.
  1340. (should
  1341. (equal
  1342. "http"
  1343. (org-test-with-temp-text "[[http://orgmode.org]]"
  1344. (org-element-property
  1345. :type
  1346. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1347. ;; ... with expansion.
  1348. (should
  1349. (equal
  1350. "//orgmode.org/worg"
  1351. (org-test-with-temp-text "[[Org:worg]]"
  1352. (let ((org-link-abbrev-alist '(("Org" . "http://orgmode.org/"))))
  1353. (org-element-property
  1354. :path
  1355. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))))))
  1356. ;; ... with translation.
  1357. (should
  1358. (equal
  1359. "127.0.0.1"
  1360. (org-test-with-temp-text "[[http://orgmode.org]]"
  1361. (flet ((link-translate (type path) (cons type "127.0.0.1")))
  1362. (let ((org-link-translation-function 'link-translate))
  1363. (org-element-property
  1364. :path
  1365. (org-element-map (org-element-parse-buffer) 'link
  1366. 'identity nil t)))))))
  1367. ;; ... id link.
  1368. (should
  1369. (equal
  1370. "id"
  1371. (org-test-with-temp-text "[[id:aaaa]]"
  1372. (org-element-property
  1373. :type
  1374. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1375. ;; ... custom-id link.
  1376. (should
  1377. (equal
  1378. "custom-id"
  1379. (org-test-with-temp-text "[[#some-id]]"
  1380. (org-element-property
  1381. :type
  1382. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1383. ;; ... coderef link.
  1384. (should
  1385. (equal
  1386. "coderef"
  1387. (org-test-with-temp-text "[[(reference)]]"
  1388. (org-element-property
  1389. :type
  1390. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1391. ;; ... fuzzy link.
  1392. (should
  1393. (equal
  1394. "fuzzy"
  1395. (org-test-with-temp-text "[[target-or-title]]"
  1396. (org-element-property
  1397. :type
  1398. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1399. ;; ... file-type link with search option.
  1400. (should
  1401. (equal
  1402. '(("file" "projects.org" "*task title"))
  1403. (org-test-with-temp-text "[[file:projects.org::*task title]]"
  1404. (org-element-map (org-element-parse-buffer) 'link
  1405. (lambda (l) (list (org-element-property :type l)
  1406. (org-element-property :path l)
  1407. (org-element-property :search-option l)))))))
  1408. ;; ... file-type link with application...
  1409. (should
  1410. (equal
  1411. '("file" "projects.org" "emacs")
  1412. (org-test-with-temp-text "[[file+emacs:projects.org]]"
  1413. (let ((l (org-element-context)))
  1414. (list (org-element-property :type l)
  1415. (org-element-property :path l)
  1416. (org-element-property :application l))))))
  1417. ;; ... `:path' in a file-type link must be compatible with "file"
  1418. ;; scheme in URI syntax, even if Org syntax isn't...
  1419. (should
  1420. (org-test-with-temp-text-in-file ""
  1421. (let ((file (expand-file-name (buffer-file-name))))
  1422. (insert (format "[[file://%s]]" file))
  1423. (equal (org-element-property :path (org-element-context)) file))))
  1424. (should
  1425. (org-test-with-temp-text-in-file ""
  1426. (let ((file (expand-file-name (buffer-file-name))))
  1427. (insert (format "[[file:%s]]" file))
  1428. (equal (org-element-property :path (org-element-context)) file))))
  1429. (should
  1430. (org-test-with-temp-text-in-file ""
  1431. (let ((file (expand-file-name (buffer-file-name))))
  1432. (insert (format "[[file:%s]]" file))
  1433. (equal (org-element-property :path (org-element-context)) file))))
  1434. ;; ... multi-line link.
  1435. (should
  1436. (equal "//orgmode.org"
  1437. (org-test-with-temp-text "[[http://orgmode.\norg]]"
  1438. (org-element-property :path (org-element-context)))))
  1439. ;; Plain link.
  1440. (should
  1441. (org-test-with-temp-text "A link: http://orgmode.org"
  1442. (org-element-map (org-element-parse-buffer) 'link 'identity)))
  1443. ;; Angular link.
  1444. (should
  1445. (org-test-with-temp-text "A link: <http://orgmode.org>"
  1446. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))
  1447. ;; Link abbreviation.
  1448. (should
  1449. (equal "http"
  1450. (org-test-with-temp-text
  1451. "#+LINK: orgmode http://www.orgmode.org/\n[[orgmode:#docs]]"
  1452. (progn (org-mode-restart)
  1453. (goto-char (1- (point-max)))
  1454. (org-element-property :type (org-element-context))))))
  1455. ;; Link abbreviation in a secondary string.
  1456. (should
  1457. (equal "http"
  1458. (org-test-with-temp-text
  1459. "#+LINK: orgmode http://www.orgmode.org/\n* H [[orgmode:#docs]]"
  1460. (progn (org-mode-restart)
  1461. (org-element-map (org-element-parse-buffer) 'link
  1462. (lambda (link) (org-element-property :type link))
  1463. nil t nil t)))))
  1464. ;; Plain links are allowed as description of regular links.
  1465. (should
  1466. (equal "file"
  1467. (org-test-with-temp-text "[[http://orgmode.org][file:unicorn.jpg]]"
  1468. (search-forward "file:")
  1469. (org-element-property :type (org-element-context))))))
  1470. ;;;; Macro
  1471. (ert-deftest test-org-element/macro-parser ()
  1472. "Test `macro' parser."
  1473. ;; Without arguments.
  1474. (should
  1475. (org-test-with-temp-text "{{{macro}}}"
  1476. (org-element-map (org-element-parse-buffer) 'macro 'identity)))
  1477. ;; With arguments.
  1478. (should
  1479. (org-test-with-temp-text "{{{macro(arg1,arg2)}}}"
  1480. (org-element-map (org-element-parse-buffer) 'macro 'identity)))
  1481. ;; Properly handle protected commas in arguments...
  1482. (should
  1483. (= 2
  1484. (length
  1485. (org-test-with-temp-text "{{{macro(arg1\\,arg1,arg2)}}}"
  1486. (org-element-property :args (org-element-context))))))
  1487. ;; ... even when last argument ends with a protected comma.
  1488. (should
  1489. (equal '("C-,")
  1490. (org-test-with-temp-text "{{{macro(C-\\,)}}}"
  1491. (org-element-property :args (org-element-context)))))
  1492. ;; Allow to escape escaping character.
  1493. (should
  1494. (equal '("C-\\" "")
  1495. (org-test-with-temp-text "{{{macro(C-\\\\,)}}}"
  1496. (org-element-property :args (org-element-context)))))
  1497. ;; No need to escape backslashes elsewhere.
  1498. (should
  1499. (equal '("\\")
  1500. (org-test-with-temp-text "{{{macro(\\)}}}"
  1501. (org-element-property :args (org-element-context))))))
  1502. ;;;; Node Property
  1503. (ert-deftest test-org-element/node-property ()
  1504. "Test `node-property' parser."
  1505. ;; Standard test.
  1506. (should
  1507. (equal '("abc" "value")
  1508. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:abc: value\n:END:"
  1509. (let ((element (org-element-at-point)))
  1510. (list (org-element-property :key element)
  1511. (org-element-property :value element))))))
  1512. ;; Value should be trimmed.
  1513. (should
  1514. (equal "value"
  1515. (org-test-with-temp-text
  1516. "* H\n:PROPERTIES:\n<point>:abc: value \n:END:"
  1517. (org-element-property :value (org-element-at-point)))))
  1518. ;; A node property requires to be wrapped within a property drawer.
  1519. (should-not
  1520. (eq 'node-property
  1521. (org-test-with-temp-text ":abc: value"
  1522. (org-element-type (org-element-at-point)))))
  1523. ;; Accept empty properties.
  1524. (should
  1525. (equal '(("foo" "value") ("bar" ""))
  1526. (org-test-with-temp-text "* H\n:PROPERTIES:\n:foo: value\n:bar:\n:END:"
  1527. (org-element-map (org-element-parse-buffer) 'node-property
  1528. (lambda (p)
  1529. (list (org-element-property :key p)
  1530. (org-element-property :value p))))))))
  1531. ;;;; Paragraph
  1532. (ert-deftest test-org-element/paragraph-parser ()
  1533. "Test `paragraph' parser."
  1534. ;; Standard test.
  1535. (should
  1536. (org-test-with-temp-text "Paragraph"
  1537. (org-element-map (org-element-parse-buffer) 'paragraph 'identity nil t)))
  1538. ;; Property find end of a paragraph stuck to another element.
  1539. (should
  1540. (eq ?#
  1541. (org-test-with-temp-text "Paragraph\n# Comment"
  1542. (org-element-map (org-element-parse-buffer) 'paragraph
  1543. (lambda (p) (char-after (org-element-property :end p)))
  1544. nil t))))
  1545. ;; Include ill-formed Keywords.
  1546. (should
  1547. (org-test-with-temp-text "#+wrong_keyword something"
  1548. (org-element-map (org-element-parse-buffer) 'paragraph 'identity)))
  1549. ;; Include incomplete-drawers.
  1550. (should
  1551. (org-test-with-temp-text ":TEST:\nParagraph"
  1552. (let ((elem (org-element-at-point)))
  1553. (and (eq (org-element-type elem) 'paragraph)
  1554. (= (point-max) (org-element-property :end elem))))))
  1555. ;; Include incomplete blocks.
  1556. (should
  1557. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph"
  1558. (let ((elem (org-element-at-point)))
  1559. (and (eq (org-element-type elem) 'paragraph)
  1560. (= (point-max) (org-element-property :end elem))))))
  1561. ;; Include incomplete dynamic blocks.
  1562. (should
  1563. (org-test-with-temp-text "#+BEGIN: \nParagraph"
  1564. (let ((elem (org-element-at-point)))
  1565. (and (eq (org-element-type elem) 'paragraph)
  1566. (= (point-max) (org-element-property :end elem))))))
  1567. ;; Include incomplete latex environments.
  1568. (should
  1569. (org-test-with-temp-text "\begin{equation}\nParagraph"
  1570. (let ((elem (org-element-at-point)))
  1571. (and (eq (org-element-type elem) 'paragraph)
  1572. (= (point-max) (org-element-property :end elem))))))
  1573. ;; Do not steal affiliated keywords from container.
  1574. (should
  1575. (org-test-with-temp-text "#+ATTR_LATEX: test\n- item 1"
  1576. (let ((elem (progn (search-forward "item") (org-element-at-point))))
  1577. (and (eq (org-element-type elem) 'paragraph)
  1578. (not (org-element-property :attr_latex elem))
  1579. (/= (org-element-property :begin elem) 1)))))
  1580. ;; Handle non-empty blank line at the end of buffer.
  1581. (should
  1582. (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
  1583. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1584. ;;;; Plain List
  1585. (ert-deftest test-org-element/plain-list-parser ()
  1586. "Test `plain-list' parser."
  1587. (org-test-with-temp-text "- item"
  1588. (should (org-element-map (org-element-parse-buffer) 'plain-list 'identity)))
  1589. ;; Blank lines after the list only belong to outer plain list.
  1590. (should
  1591. (equal
  1592. '(t t)
  1593. (org-test-with-temp-text "
  1594. - outer
  1595. - inner
  1596. Outside list"
  1597. (let ((endings (org-element-map (org-element-parse-buffer) 'plain-list
  1598. (lambda (pl) (org-element-property :end pl)))))
  1599. (list
  1600. ;; Move to ending of outer list.
  1601. (progn (goto-char (car endings)) (looking-at "Outside list"))
  1602. ;; Move to ending of inner list.
  1603. (progn (goto-char (nth 1 endings)) (looking-at "^$")))))))
  1604. ;; Correctly compute end of list if it doesn't end at a line
  1605. ;; beginning.
  1606. (should
  1607. (org-test-with-temp-text "- list\n \n "
  1608. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1609. ;;;; Planning
  1610. (ert-deftest test-org-element/planning-parser ()
  1611. "Test `planning' parser."
  1612. ;; Test various keywords.
  1613. (should
  1614. (org-element-property
  1615. :closed
  1616. (org-test-with-temp-text "* H\n<point>CLOSED: [2012-03-29 thu.]"
  1617. (org-element-at-point))))
  1618. (should
  1619. (org-element-property
  1620. :deadline
  1621. (org-test-with-temp-text "* H\n<point>DEADLINE: <2012-03-29 thu.>"
  1622. (org-element-at-point))))
  1623. (should
  1624. (org-element-property
  1625. :scheduled
  1626. (org-test-with-temp-text "* H\n<point>SCHEDULED: <2012-03-29 thu.>"
  1627. (org-element-at-point))))
  1628. ;; Planning line only exists right after a headline.
  1629. (should-not
  1630. (eq 'planning
  1631. (org-test-with-temp-text "DEADLINE: <2012-03-29 thu.>"
  1632. (org-element-type (org-element-at-point)))))
  1633. (should-not
  1634. (eq 'planning
  1635. (org-test-with-temp-text
  1636. "* H\n# Comment\n<point>DEADLINE: <2012-03-29 thu.>"
  1637. (org-element-type (org-element-at-point))))))
  1638. ;;;; Property Drawer
  1639. (ert-deftest test-org-element/property-drawer-parser ()
  1640. "Test `property-drawer' parser."
  1641. ;; Standard test.
  1642. (should
  1643. (eq 'property-drawer
  1644. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value\n:END:"
  1645. (org-element-type (org-element-at-point)))))
  1646. (should
  1647. (eq 'property-drawer
  1648. (org-test-with-temp-text
  1649. "* H\nDEADLINE: <2014-03-04 tue.>\n<point>:PROPERTIES:\n:prop: value\n:END:"
  1650. (org-element-type (org-element-at-point)))))
  1651. ;; Allow properties without value and no property at all.
  1652. (should
  1653. (eq 'property-drawer
  1654. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop:\n:END:"
  1655. (org-element-type (org-element-at-point)))))
  1656. (should
  1657. (eq 'property-drawer
  1658. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:"
  1659. (org-element-type (org-element-at-point)))))
  1660. ;; Ignore incomplete drawer, drawer at a wrong location or with
  1661. ;; wrong contents.
  1662. (should-not
  1663. (eq 'property-drawer
  1664. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value"
  1665. (org-element-type (org-element-at-point)))))
  1666. (should-not
  1667. (eq 'property-drawer
  1668. (org-test-with-temp-text
  1669. "* H\nParagraph\n<point>:PROPERTIES:\n:prop: value\n:END:"
  1670. (org-element-type (org-element-at-point)))))
  1671. (should-not
  1672. (eq 'property-drawer
  1673. (org-test-with-temp-text
  1674. "* H\nParagraph\n<point>:PROPERTIES:\nparagraph\n:END:"
  1675. (org-element-type (org-element-at-point)))))
  1676. ;; Handle non-empty blank line at the end of buffer.
  1677. (should
  1678. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:\n "
  1679. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1680. ;;;; Quote Block
  1681. (ert-deftest test-org-element/quote-block-parser ()
  1682. "Test `quote-block' parser."
  1683. ;; Regular test.
  1684. (should
  1685. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  1686. (org-element-map (org-element-parse-buffer) 'quote-block 'identity)))
  1687. ;; Ignore incomplete block.
  1688. (should-not
  1689. (org-test-with-temp-text "#+BEGIN_QUOTE"
  1690. (org-element-map (org-element-parse-buffer) 'quote-block 'identity nil t)))
  1691. ;; Handle non-empty blank line at the end of buffer.
  1692. (should
  1693. (org-test-with-temp-text "#+BEGIN_QUOTE\nC\n#+END_QUOTE\n "
  1694. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1695. ;;;; Radio Target
  1696. (ert-deftest test-org-element/radio-target-parser ()
  1697. "Test `radio-target' parser."
  1698. ;; Standard test.
  1699. (should
  1700. (eq 'radio-target
  1701. (org-test-with-temp-text "<<<radio>>>"
  1702. (org-element-type (org-element-context)))))
  1703. ;; Radio targets with objects.
  1704. (should
  1705. (eq 'radio-target
  1706. (org-test-with-temp-text "<<<radio \\alpha>>>"
  1707. (org-element-type (org-element-context)))))
  1708. ;; Radio targets starting with an object.
  1709. (should
  1710. (eq 'radio-target
  1711. (org-test-with-temp-text "<<<\\alpha radio>>>"
  1712. (org-element-type (org-element-context)))))
  1713. ;; Radio targets cannot begin or end with white space.
  1714. (should-not
  1715. (eq 'radio-target
  1716. (org-test-with-temp-text "<<< radio>>>"
  1717. (org-element-type (org-element-context)))))
  1718. (should-not
  1719. (eq 'radio-target
  1720. (org-test-with-temp-text "<<<radio >>>"
  1721. (org-element-type (org-element-context))))))
  1722. ;;;; Section
  1723. (ert-deftest test-org-element/section-parser ()
  1724. "Test `section' parser."
  1725. ;; Standard test.
  1726. (should
  1727. (org-test-with-temp-text "* Headline\nText"
  1728. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  1729. ;; There's a section before the first headline.
  1730. (should
  1731. (org-test-with-temp-text "Text"
  1732. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  1733. ;; A section cannot be empty.
  1734. (should-not
  1735. (org-test-with-temp-text "* Headline 1\n* Headline 2"
  1736. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  1737. ;; A section doesn't contain sub-trees.
  1738. (should-not
  1739. (org-test-with-temp-text "* Head\nText\n** Sub-Head"
  1740. (org-element-map
  1741. (org-element-map (org-element-parse-buffer) 'section 'identity nil t)
  1742. 'headline 'identity))))
  1743. ;;;; Special Block
  1744. (ert-deftest test-org-element/special-block-parser ()
  1745. "Test `special-block' parser."
  1746. ;; Standard test.
  1747. (should
  1748. (equal "SPECIAL"
  1749. (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
  1750. (org-element-property :type (org-element-at-point)))))
  1751. ;; Special blocks are case sensitive.
  1752. (should
  1753. (equal "CaSe"
  1754. (org-test-with-temp-text "#+BEGIN_CaSe\nText\n#+END_CaSe"
  1755. (org-element-property :type (org-element-at-point)))))
  1756. ;; Special blocks can contain paragraphs.
  1757. (should
  1758. (eq 'paragraph
  1759. (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
  1760. (forward-line)
  1761. (org-element-type (org-element-at-point)))))
  1762. ;; Ignore incomplete block.
  1763. (should-not
  1764. (eq 'special-block
  1765. (org-test-with-temp-text "#+BEGIN_SPECIAL"
  1766. (org-element-type (org-element-at-point)))))
  1767. ;; Allow special characters in type.
  1768. (should
  1769. (equal '(special-block "SPECIAL*")
  1770. (org-test-with-temp-text "#+BEGIN_SPECIAL*\nContents\n#+END_SPECIAL*"
  1771. (let ((element (org-element-at-point)))
  1772. (list (org-element-type element)
  1773. (org-element-property :type element))))))
  1774. ;; Handle non-empty blank line at the end of buffer.
  1775. (should
  1776. (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n "
  1777. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1778. ;;;; Src Block
  1779. (ert-deftest test-org-element/src-block-parser ()
  1780. "Test `src-block' parser."
  1781. ;; Regular tests.
  1782. (should
  1783. (org-test-with-temp-text "#+BEGIN_SRC org\nText\n#+END_SRC"
  1784. (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
  1785. ;; Ignore incomplete block.
  1786. (should-not
  1787. (org-test-with-temp-text "#+BEGIN_SRC"
  1788. (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
  1789. ;; Properly un-escape code.
  1790. (should
  1791. (equal "* Headline\n #+keyword\nText\n"
  1792. (org-test-with-temp-text
  1793. "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
  1794. (org-element-property :value (org-element-at-point)))))
  1795. ;; Remove block indentation according to block boundaries, unless
  1796. ;; block contents are less indented than block boundaries.
  1797. (should
  1798. (equal " L1\nL2\n"
  1799. (org-test-with-temp-text " #+BEGIN_SRC org\n L1\n L2\n #+END_SRC"
  1800. (let ((org-src-preserve-indentation t))
  1801. (org-element-property :value (org-element-at-point))))))
  1802. (should
  1803. (equal
  1804. " L1\n L2\n"
  1805. (org-test-with-temp-text " #+BEGIN_SRC org\n L1\n L2\n #+END_SRC"
  1806. (let ((org-src-preserve-indentation t))
  1807. (org-element-property :value (org-element-at-point))))))
  1808. ;; Handle non-empty blank line at the end of buffer.
  1809. (should
  1810. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\nC\n#+END_SRC\n "
  1811. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1812. ;;;; Statistics Cookie
  1813. (ert-deftest test-org-element/statistics-cookie ()
  1814. "Test `statistics-cookie' parser."
  1815. ;; With numbers.
  1816. (should
  1817. (org-test-with-temp-text "[1/2]"
  1818. (org-element-map (org-element-parse-buffer) 'statistics-cookie 'identity)))
  1819. ;; With percents.
  1820. (should
  1821. (org-test-with-temp-text "[33%]"
  1822. (org-element-map
  1823. (org-element-parse-buffer) 'statistics-cookie 'identity))))
  1824. ;;;; Strike Through
  1825. (ert-deftest test-org-element/strike-through-parser ()
  1826. "Test `strike-through' parser."
  1827. ;; Regular test.
  1828. (should
  1829. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1830. (org-test-with-temp-text "+strike-through+"
  1831. (org-element-map (org-element-parse-buffer) 'strike-through 'identity))))
  1832. ;; Multi-line markup.
  1833. (should
  1834. (equal
  1835. (org-element-contents
  1836. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1837. (org-test-with-temp-text "+first line\nsecond line+"
  1838. (org-element-map
  1839. (org-element-parse-buffer) 'strike-through 'identity nil t))))
  1840. '("first line\nsecond line"))))
  1841. ;;;; Subscript
  1842. (ert-deftest test-org-element/subscript-parser ()
  1843. "Test `subscript' parser."
  1844. ;; Without braces.
  1845. (should
  1846. (org-test-with-temp-text "a_b"
  1847. (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
  1848. ;; With braces.
  1849. (should
  1850. (org-test-with-temp-text "a_{b}"
  1851. (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
  1852. ;; Multiple subscripts in a paragraph.
  1853. (should
  1854. (= 2
  1855. (org-test-with-temp-text "a_b and c_d"
  1856. (length
  1857. (org-element-map (org-element-parse-buffer) 'subscript 'identity))))))
  1858. ;;;; Superscript
  1859. (ert-deftest test-org-element/superscript-parser ()
  1860. "Test `superscript' parser."
  1861. ;; Without braces.
  1862. (should
  1863. (org-test-with-temp-text "a^b"
  1864. (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
  1865. ;; With braces.
  1866. (should
  1867. (org-test-with-temp-text "a^{b}"
  1868. (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
  1869. ;; Multiple superscript in a paragraph.
  1870. (should
  1871. (= 2
  1872. (org-test-with-temp-text "a^b and c^d"
  1873. (length
  1874. (org-element-map
  1875. (org-element-parse-buffer) 'superscript 'identity))))))
  1876. ;;;; Table
  1877. (ert-deftest test-org-element/table-parser ()
  1878. "Test `table' parser."
  1879. (should
  1880. (org-test-with-temp-text "| a |"
  1881. (org-element-map (org-element-parse-buffer) 'table 'identity)))
  1882. ;; TBLFM keyword is case insensitive.
  1883. (should
  1884. (org-test-with-temp-text "| a |\n#+tblfm: test"
  1885. (org-element-property
  1886. :tblfm
  1887. (org-element-map (org-element-parse-buffer) 'table 'identity nil t))))
  1888. ;; Handle multiple TBLFM lines.
  1889. (should
  1890. (= 2
  1891. (org-test-with-temp-text "| a |\n#+TBLFM: test1\n#+TBLFM: test2"
  1892. (length (org-element-property
  1893. :tblfm
  1894. (org-element-map
  1895. (org-element-parse-buffer) 'table 'identity nil t))))))
  1896. ;; Handle non-empty blank line at the end of buffer.
  1897. (should
  1898. (org-test-with-temp-text "| a |\n "
  1899. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1900. ;;;; Table Cell
  1901. (ert-deftest test-org-element/table-cell-parser ()
  1902. "Test `table-cell' parser."
  1903. ;; Regular table cell.
  1904. (should
  1905. (org-test-with-temp-text "| a |"
  1906. (org-element-map (org-element-parse-buffer) 'table-cell 'identity)))
  1907. ;; Last vertical bar may be omitted.
  1908. (should
  1909. (org-test-with-temp-text "| a "
  1910. (org-element-map (org-element-parse-buffer) 'table-cell 'identity))))
  1911. ;;;; Table Row
  1912. (ert-deftest test-org-element/table-row-parser ()
  1913. "Test `table-row' parser."
  1914. (should
  1915. (equal '(standard rule)
  1916. (org-test-with-temp-text "| a |\n|---|"
  1917. (org-element-map
  1918. (org-element-parse-buffer) 'table-row
  1919. (lambda (row) (org-element-property :type row)))))))
  1920. ;;;; Target
  1921. (ert-deftest test-org-element/target-parser ()
  1922. "Test `target' parser."
  1923. (should
  1924. (org-test-with-temp-text "<<target>>"
  1925. (org-element-map (org-element-parse-buffer) 'target 'identity))))
  1926. ;;;; Timestamp
  1927. (ert-deftest test-org-element/timestamp ()
  1928. "Test `timestamp' parser."
  1929. ;; Active timestamp.
  1930. (should
  1931. (org-test-with-temp-text "<2012-03-29 16:40>"
  1932. (eq (org-element-property :type (org-element-context)) 'active)))
  1933. (should-not
  1934. (org-test-with-temp-text "<2012-03-29 Thu>"
  1935. (let ((timestamp (org-element-context)))
  1936. (or (org-element-property :hour-start timestamp)
  1937. (org-element-property :minute-start timestamp)))))
  1938. (should
  1939. (equal '(2012 3 29 16 40)
  1940. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1941. (let ((object (org-element-context)))
  1942. (list (org-element-property :year-start object)
  1943. (org-element-property :month-start object)
  1944. (org-element-property :day-start object)
  1945. (org-element-property :hour-start object)
  1946. (org-element-property :minute-start object))))))
  1947. ;; Inactive timestamp.
  1948. (should
  1949. (org-test-with-temp-text "[2012-03-29 Thu 16:40]"
  1950. (eq (org-element-property :type (org-element-context)) 'inactive)))
  1951. ;; Time range.
  1952. (should
  1953. (equal '(2012 3 29 16 40 7 30)
  1954. (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
  1955. (let ((object (org-element-context)))
  1956. (list (org-element-property :year-end object)
  1957. (org-element-property :month-end object)
  1958. (org-element-property :day-end object)
  1959. (org-element-property :hour-end object)
  1960. (org-element-property :minute-end object)
  1961. (org-element-property :hour-start object)
  1962. (org-element-property :minute-start object))))))
  1963. (should
  1964. (eq 'active-range
  1965. (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
  1966. (org-element-property :type (org-element-context)))))
  1967. ;; Date range.
  1968. (should
  1969. (org-test-with-temp-text "[2012-03-29 Thu 16:40]--[2012-03-29 Thu 16:41]"
  1970. (eq (org-element-property :type (org-element-context)) 'inactive-range)))
  1971. (should-not
  1972. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1973. (let ((timestamp (org-element-context)))
  1974. (or (org-element-property :hour-end timestamp)
  1975. (org-element-property :minute-end timestamp)))))
  1976. ;; With repeater, warning delay and both.
  1977. (should
  1978. (eq 'catch-up
  1979. (org-test-with-temp-text "<2012-03-29 Thu ++1y>"
  1980. (org-element-property :repeater-type (org-element-context)))))
  1981. (should
  1982. (eq 'first
  1983. (org-test-with-temp-text "<2012-03-29 Thu --1y>"
  1984. (org-element-property :warning-type (org-element-context)))))
  1985. (should
  1986. (equal '(cumulate all)
  1987. (org-test-with-temp-text "<2012-03-29 Thu +1y -1y>"
  1988. (let ((ts (org-element-context)))
  1989. (list (org-element-property :repeater-type ts)
  1990. (org-element-property :warning-type ts)))))))
  1991. ;;;; Underline
  1992. (ert-deftest test-org-element/underline-parser ()
  1993. "Test `underline' parser."
  1994. ;; Regular test.
  1995. (should
  1996. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1997. (org-test-with-temp-text "_underline_"
  1998. (org-element-map (org-element-parse-buffer) 'underline 'identity))))
  1999. ;; Multi-line markup.
  2000. (should
  2001. (equal
  2002. (org-element-contents
  2003. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  2004. (org-test-with-temp-text "_first line\nsecond line_"
  2005. (org-element-map
  2006. (org-element-parse-buffer) 'underline 'identity nil t))))
  2007. '("first line\nsecond line"))))
  2008. ;;;; Verbatim
  2009. (ert-deftest test-org-element/verbatim-parser ()
  2010. "Test `verbatim' parser."
  2011. ;; Regular test.
  2012. (should
  2013. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  2014. (org-test-with-temp-text "=verbatim="
  2015. (org-element-map (org-element-parse-buffer) 'verbatim 'identity))))
  2016. ;; Multi-line markup.
  2017. (should
  2018. (equal
  2019. (org-element-property
  2020. :value
  2021. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  2022. (org-test-with-temp-text "=first line\nsecond line="
  2023. (org-element-map
  2024. (org-element-parse-buffer) 'verbatim 'identity nil t))))
  2025. "first line\nsecond line")))
  2026. ;;;; Verse Block
  2027. (ert-deftest test-org-element/verse-block-parser ()
  2028. "Test `verse-block' parser."
  2029. ;; Standard test.
  2030. (should
  2031. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
  2032. (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
  2033. ;; Ignore case.
  2034. (should
  2035. (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
  2036. (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
  2037. ;; Parse objects in verse blocks.
  2038. (should
  2039. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
  2040. (org-element-map (org-element-parse-buffer) 'entity 'identity)))
  2041. ;; Ignore incomplete verse block.
  2042. (should-not
  2043. (org-test-with-temp-text "#+BEGIN_VERSE"
  2044. (org-element-map (org-element-parse-buffer) 'verse-block 'identity nil t)))
  2045. ;; Handle non-empty blank line at the end of buffer.
  2046. (should
  2047. (org-test-with-temp-text "#+BEGIN_VERSE\nC\n#+END_VERSE\n "
  2048. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  2049. ;;; Test Interpreters.
  2050. (ert-deftest test-org-element/interpret-data ()
  2051. "Test `org-element-interpret-data' specifications."
  2052. ;; Interpret simple affiliated keywords.
  2053. (should
  2054. (equal
  2055. (org-element-interpret-data
  2056. '(org-data nil (paragraph (:name "para") "Paragraph")))
  2057. "#+NAME: para\nParagraph\n"))
  2058. ;; Interpret multiple affiliated keywords.
  2059. (should
  2060. (equal
  2061. (org-element-interpret-data
  2062. '(org-data nil (paragraph (:attr_ascii ("line2" "line1")) "Paragraph")))
  2063. "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph\n"))
  2064. ;; Interpret parsed affiliated keywords.
  2065. (should
  2066. (equal
  2067. (org-element-interpret-data
  2068. '(org-data nil (paragraph (:caption (("caption"))) "Paragraph")))
  2069. "#+CAPTION: caption\nParagraph\n"))
  2070. ;; Interpret dual affiliated keywords.
  2071. (should
  2072. (equal
  2073. (org-element-interpret-data
  2074. '(org-data nil (paragraph (:caption ((("long") "short"))) "Paragraph")))
  2075. "#+CAPTION[short]: long\nParagraph\n"))
  2076. ;; Interpret multiple parsed dual keywords.
  2077. (should
  2078. (equal
  2079. (org-element-interpret-data
  2080. '(org-data nil (paragraph
  2081. (:caption ((("l2") "s2") (("l1") "s1"))) "Paragraph")))
  2082. "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph\n"))
  2083. ;; Pseudo objects and elements are transparent.
  2084. (should
  2085. (equal "A B"
  2086. (org-trim
  2087. (org-element-interpret-data
  2088. '(paragraph nil (pseudo-object (:post-blank 1) "A") "B")))))
  2089. (should
  2090. (equal "A\n\nB\n"
  2091. (org-element-interpret-data
  2092. '(center nil
  2093. (pseudo-element (:post-blank 1) (paragraph nil "A"))
  2094. (paragraph nil "B"))))))
  2095. (ert-deftest test-org-element/center-block-interpreter ()
  2096. "Test center block interpreter."
  2097. (should
  2098. (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
  2099. "#+BEGIN_CENTER\nTest\n#+END_CENTER\n")))
  2100. (ert-deftest test-org-element/drawer-interpreter ()
  2101. "Test drawer interpreter."
  2102. (should
  2103. (equal (org-test-parse-and-interpret ":TEST:\nTest\n:END:")
  2104. ":TEST:\nTest\n:END:\n")))
  2105. (ert-deftest test-org-element/dynamic-block-interpreter ()
  2106. "Test dynamic block interpreter."
  2107. (should
  2108. (equal (org-test-parse-and-interpret
  2109. "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
  2110. "#+BEGIN: myblock :parameter value1\nTest\n#+END:\n")))
  2111. (ert-deftest test-org-element/footnote-definition-interpreter ()
  2112. "Test footnote definition interpreter."
  2113. (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n")))
  2114. (ert-deftest test-org-element/headline-interpreter ()
  2115. "Test headline and section interpreters."
  2116. ;; 1. Standard test.
  2117. (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
  2118. ;; 2. With TODO keywords.
  2119. (should
  2120. (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  2121. (org-test-parse-and-interpret "* TODO Headline"))
  2122. "* TODO Headline\n"))
  2123. ;; 3. With tags...
  2124. ;;
  2125. ;; 3.1. ... and a positive `org-tags-column' value.
  2126. (should
  2127. (equal (let ((org-tags-column 20))
  2128. (org-test-parse-and-interpret "* Headline :tag:"))
  2129. "* Headline :tag:\n"))
  2130. ;; 3.2. ... and a negative `org-tags-column' value.
  2131. (should
  2132. (equal (let ((org-tags-column -20))
  2133. (org-test-parse-and-interpret "* Headline :tag:"))
  2134. "* Headline :tag:\n"))
  2135. ;; 3.3. ... and a null `org-tags-column' value.
  2136. (should
  2137. (equal (let ((org-tags-column 0))
  2138. (org-test-parse-and-interpret "* Headline :tag:"))
  2139. "* Headline :tag:\n"))
  2140. ;; 4. With priority cookie.
  2141. (should
  2142. (equal (org-test-parse-and-interpret "* [#B] Headline")
  2143. "* [#B] Headline\n"))
  2144. ;; 5. With comment keyword.
  2145. (should
  2146. (equal (let ((org-comment-string "COMMENT"))
  2147. (org-test-parse-and-interpret "* COMMENT Headline"))
  2148. "* COMMENT Headline\n"))
  2149. ;; 6. Keep same number of blank lines before body.
  2150. (should
  2151. (equal (org-test-parse-and-interpret
  2152. "* Headline\n\n\nText after two blank lines.")
  2153. "* Headline\n\n\nText after two blank lines.\n"))
  2154. ;; 8. Preserve `org-odd-levels-only' state.
  2155. (should
  2156. (equal "* H\n*** H2\n"
  2157. (let ((org-odd-levels-only t))
  2158. (org-test-parse-and-interpret "* H\n*** H2")))))
  2159. (ert-deftest test-org-element/inlinetask-interpreter ()
  2160. "Test inlinetask interpretation."
  2161. (when (featurep 'org-inlinetask)
  2162. (let ((org-inlinetask-min-level 15))
  2163. ;; 1. Regular inlinetask.
  2164. (should (equal (org-test-parse-and-interpret
  2165. "*************** Task\nTest\n*************** END")
  2166. "*************** Task\nTest\n*************** END\n"))
  2167. ;; 2. Degenerate inlinetask.
  2168. (should (equal (org-test-parse-and-interpret "*************** Task")
  2169. "*************** Task\n"))
  2170. ;; 3. Prefer degenerate form when there are no contents.
  2171. (should (equal (org-test-parse-and-interpret
  2172. "*************** Task\n*************** END")
  2173. "*************** Task\n"))
  2174. ;; 4. With TODO keywords.
  2175. (should
  2176. (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  2177. (org-test-parse-and-interpret "*************** TODO Task"))
  2178. "*************** TODO Task\n"))
  2179. ;; 5. With tags...
  2180. ;;
  2181. ;; 5.1. ... and a positive `org-tags-column' value.
  2182. (should
  2183. (equal (let ((org-tags-column 30))
  2184. (org-test-parse-and-interpret "*************** Task :tag:"))
  2185. "*************** Task :tag:\n"))
  2186. ;; 5.2. ... and a negative `org-tags-column' value.
  2187. (should
  2188. (equal (let ((org-tags-column -30))
  2189. (org-test-parse-and-interpret "*************** Task :tag:"))
  2190. "*************** Task :tag:\n"))
  2191. ;; 5.3. ... and a null `org-tags-column' value.
  2192. (should
  2193. (equal (let ((org-tags-column 0))
  2194. (org-test-parse-and-interpret "*************** Task :tag:"))
  2195. "*************** Task :tag:\n"))
  2196. ;; 6. With priority cookie.
  2197. (should
  2198. (equal (org-test-parse-and-interpret "*************** [#B] Task")
  2199. "*************** [#B] Task\n")))))
  2200. (ert-deftest test-org-element/plain-list-interpreter ()
  2201. "Test plain-list and item interpreters."
  2202. (let ((org-list-two-spaces-after-bullet-regexp nil))
  2203. ;; Unordered list.
  2204. (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
  2205. ;; Description list.
  2206. (should
  2207. (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
  2208. ;; Ordered list.
  2209. (should
  2210. (equal (let ((org-plain-list-ordered-item-terminator t))
  2211. (org-test-parse-and-interpret "1. Item"))
  2212. "1. Item\n"))
  2213. (should
  2214. (equal (let ((org-plain-list-ordered-item-terminator ?\)))
  2215. (org-test-parse-and-interpret "1) Item"))
  2216. "1) Item\n"))
  2217. ;; Ordered list with counter.
  2218. (should
  2219. (equal (let ((org-plain-list-ordered-item-terminator t))
  2220. (org-test-parse-and-interpret "1. [@5] Item"))
  2221. "5. [@5] Item\n"))
  2222. ;; List with check-boxes.
  2223. (should
  2224. (equal (org-test-parse-and-interpret
  2225. "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3")
  2226. "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3\n"))
  2227. ;; Item not starting with a paragraph.
  2228. (should
  2229. (equal (org-test-parse-and-interpret "-\n | a | b |")
  2230. "- \n | a | b |\n"))
  2231. ;; Special case: correctly handle "*" bullets.
  2232. (should (org-test-parse-and-interpret " * item"))
  2233. ;; Special case: correctly handle empty items.
  2234. (should (org-test-parse-and-interpret "-"))))
  2235. (ert-deftest test-org-element/quote-block-interpreter ()
  2236. "Test quote block interpreter."
  2237. (should (equal (org-test-parse-and-interpret
  2238. "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
  2239. "#+BEGIN_QUOTE\nTest\n#+END_QUOTE\n")))
  2240. (ert-deftest test-org-element/special-block-interpreter ()
  2241. "Test special block interpreter."
  2242. (should (equal (org-test-parse-and-interpret
  2243. "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
  2244. "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL\n")))
  2245. (ert-deftest test-org-element/babel-call-interpreter ()
  2246. "Test babel call interpreter."
  2247. ;; 1. Without argument.
  2248. (should (equal (org-test-parse-and-interpret "#+CALL: test()")
  2249. "#+CALL: test()\n"))
  2250. ;; 2. With argument.
  2251. (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
  2252. "#+CALL: test(x=2)\n"))
  2253. ;; 3. With header arguments.
  2254. (should (equal (org-test-parse-and-interpret
  2255. "#+CALL: test[:results output]()[:results html]")
  2256. "#+CALL: test[:results output]()[:results html]\n")))
  2257. (ert-deftest test-org-element/clock-interpreter ()
  2258. "Test clock interpreter."
  2259. ;; Running clock.
  2260. (should
  2261. (string-match
  2262. "CLOCK: \\[2012-01-01 .* 00:01\\]"
  2263. (org-test-parse-and-interpret "CLOCK: [2012-01-01 sun. 00:01]")))
  2264. ;; Closed clock.
  2265. (should
  2266. (string-match
  2267. "CLOCK: \\[2012-01-01 .* 00:01\\]--\\[2012-01-01 .* 00:02\\] => 0:01"
  2268. (org-test-parse-and-interpret "
  2269. CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))))
  2270. (ert-deftest test-org-element/comment-interpreter ()
  2271. "Test comment interpreter."
  2272. ;; Regular comment.
  2273. (should (equal (org-test-parse-and-interpret "# Comment") "# Comment\n"))
  2274. ;; Inline comment.
  2275. (should (equal (org-test-parse-and-interpret " # Comment")
  2276. "# Comment\n"))
  2277. ;; Preserve indentation.
  2278. (should (equal (org-test-parse-and-interpret " # No blank\n# One blank")
  2279. "# No blank\n# One blank\n")))
  2280. (ert-deftest test-org-element/comment-block-interpreter ()
  2281. "Test comment block interpreter."
  2282. (should (equal (org-test-parse-and-interpret
  2283. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
  2284. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT\n"))
  2285. ;; Accept missing final newline in value.
  2286. (should
  2287. (equal
  2288. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT\n"
  2289. (org-element-interpret-data '(comment-block (:value "Test"))))))
  2290. (ert-deftest test-org-element/diary-sexp ()
  2291. "Test diary-sexp interpreter."
  2292. (should
  2293. (equal
  2294. (org-test-parse-and-interpret
  2295. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old")
  2296. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old\n")))
  2297. (ert-deftest test-org-element/example-block-interpreter ()
  2298. "Test example block interpreter."
  2299. ;; Without switches.
  2300. (should (equal (org-test-parse-and-interpret
  2301. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE")
  2302. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE\n"))
  2303. ;; With switches.
  2304. (should
  2305. (equal (org-test-parse-and-interpret
  2306. "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE")
  2307. "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE\n"))
  2308. ;; Preserve code escaping.
  2309. (should
  2310. (equal (org-test-parse-and-interpret
  2311. "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE")
  2312. "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE\n"))
  2313. ;; Accept missing final newline in value.
  2314. (should
  2315. (equal
  2316. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE\n"
  2317. (org-element-interpret-data '(example-block (:value "Test"))))))
  2318. (ert-deftest test-org-element/export-block-interpreter ()
  2319. "Test export block interpreter."
  2320. (should (equal (org-test-parse-and-interpret
  2321. "#+BEGIN_HTML\nTest\n#+END_HTML")
  2322. "#+BEGIN_HTML\nTest\n#+END_HTML\n")))
  2323. (ert-deftest test-org-element/fixed-width-interpreter ()
  2324. "Test fixed width interpreter."
  2325. ;; Standard test.
  2326. (should (equal (org-test-parse-and-interpret ": Test") ": Test\n"))
  2327. ;; Preserve indentation.
  2328. (should (equal (org-test-parse-and-interpret ": 2 blanks\n: 1 blank")
  2329. ": 2 blanks\n: 1 blank\n"))
  2330. ;; Remove last newline character
  2331. (should
  2332. (equal (org-element-fixed-width-interpreter
  2333. '(fixed-width (:value "Test\n")) nil)
  2334. ": Test"))
  2335. (should
  2336. (equal (org-element-fixed-width-interpreter
  2337. '(fixed-width (:value "Test")) nil)
  2338. ": Test"))
  2339. ;; Handle empty string.
  2340. (should
  2341. (equal (org-element-fixed-width-interpreter
  2342. '(fixed-width (:value "")) nil)
  2343. ""))
  2344. ;; Handle nil value.
  2345. (should-not
  2346. (org-element-fixed-width-interpreter
  2347. '(fixed-width (:value nil)) nil)))
  2348. (ert-deftest test-org-element/horizontal-rule-interpreter ()
  2349. "Test horizontal rule interpreter."
  2350. (should (equal (org-test-parse-and-interpret "-------") "-----\n")))
  2351. (ert-deftest test-org-element/keyword-interpreter ()
  2352. "Test keyword interpreter."
  2353. (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
  2354. "#+KEYWORD: value\n")))
  2355. (ert-deftest test-org-element/latex-environment-interpreter ()
  2356. "Test latex environment interpreter."
  2357. (should (equal (org-test-parse-and-interpret
  2358. "\\begin{equation}\n1+1=2\n\\end{equation}")
  2359. "\\begin{equation}\n1+1=2\n\\end{equation}\n"))
  2360. (should (equal (org-test-parse-and-interpret
  2361. "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}")
  2362. "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}\n")))
  2363. (ert-deftest test-org-element/planning-interpreter ()
  2364. "Test planning interpreter."
  2365. (should
  2366. (string-match
  2367. "\\* Headline
  2368. DEADLINE: <2012-03-29 .*?> SCHEDULED: <2012-03-29 .*?> CLOSED: \\[2012-03-29 .*?\\]"
  2369. (org-test-parse-and-interpret
  2370. "* Headline
  2371. DEADLINE: <2012-03-29 thu.> SCHEDULED: <2012-03-29 thu.> CLOSED: [2012-03-29 thu.]"))))
  2372. (ert-deftest test-org-element/property-drawer-interpreter ()
  2373. "Test property drawer interpreter."
  2374. (should (equal (let ((org-property-format "%-10s %s"))
  2375. (org-test-parse-and-interpret
  2376. "* H\n:PROPERTIES:\n:prop: value\n:END:"))
  2377. "* H\n:PROPERTIES:\n:prop: value\n:END:\n")))
  2378. (ert-deftest test-org-element/src-block-interpreter ()
  2379. "Test src block interpreter."
  2380. ;; With arguments.
  2381. (should
  2382. (equal (let ((org-edit-src-content-indentation 2)
  2383. (org-src-preserve-indentation nil))
  2384. (org-test-parse-and-interpret
  2385. "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC"))
  2386. "#+BEGIN_SRC emacs-lisp :results silent\n (+ 1 1)\n#+END_SRC\n"))
  2387. ;; With switches.
  2388. (should
  2389. (equal (let ((org-edit-src-content-indentation 2)
  2390. (org-src-preserve-indentation nil))
  2391. (org-test-parse-and-interpret
  2392. "#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
  2393. "#+BEGIN_SRC emacs-lisp -n -k\n (+ 1 1)\n#+END_SRC\n"))
  2394. ;; Preserve code escaping.
  2395. (should
  2396. (equal (let ((org-edit-src-content-indentation 2)
  2397. (org-src-preserve-indentation nil))
  2398. (org-test-parse-and-interpret
  2399. "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC"))
  2400. "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC\n"))
  2401. ;; Do not apply `org-edit-src-content-indentation' when preserving
  2402. ;; indentation.
  2403. (should
  2404. (equal (let ((org-edit-src-content-indentation 2)
  2405. (org-src-preserve-indentation t))
  2406. (org-test-parse-and-interpret
  2407. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"))
  2408. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n"))
  2409. (should
  2410. (equal (let ((org-edit-src-content-indentation 2)
  2411. (org-src-preserve-indentation nil))
  2412. (org-test-parse-and-interpret
  2413. "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"))
  2414. "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC\n"))
  2415. ;; Accept missing final newline in value.
  2416. (should
  2417. (equal
  2418. "#+BEGIN_SRC emacs-lisp\n Test\n#+END_SRC\n"
  2419. (let ((org-edit-src-content-indentation 2)
  2420. (org-src-preserve-indentation nil))
  2421. (org-element-interpret-data
  2422. '(src-block (:language "emacs-lisp" :value "Test")))))))
  2423. (ert-deftest test-org-element/table-interpreter ()
  2424. "Test table, table-row and table-cell interpreters."
  2425. ;; 1. Simple table.
  2426. (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
  2427. "| a | b |\n| c | d |\n"))
  2428. ;; 2. With horizontal rules.
  2429. (should (equal (org-test-parse-and-interpret
  2430. "| a | b |\n|---+---|\n| c | d |")
  2431. "| a | b |\n|---+---|\n| c | d |\n"))
  2432. ;; 3. With meta-data.
  2433. (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
  2434. "| / | < | > |\n| * | 1 | 2 |\n"))
  2435. ;; 4. With a formula.
  2436. (should
  2437. (equal (org-test-parse-and-interpret
  2438. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)")
  2439. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)\n"))
  2440. ;; 5. With multiple formulas.
  2441. (should
  2442. (equal (org-test-parse-and-interpret
  2443. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2")
  2444. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2\n")))
  2445. (ert-deftest test-org-element/timestamp-interpreter ()
  2446. "Test timestamp interpreter."
  2447. ;; Active.
  2448. (should
  2449. (string-match "<2012-03-29 .* 16:40>"
  2450. (org-test-parse-and-interpret "<2012-03-29 thu. 16:40>")))
  2451. (should
  2452. (string-match "<2012-03-29 .* 16:40>"
  2453. (org-element-timestamp-interpreter
  2454. '(timestamp
  2455. (:type active :year-start 2012 :month-start 3 :day-start 29
  2456. :hour-start 16 :minute-start 40)) nil)))
  2457. ;; Inactive.
  2458. (should
  2459. (string-match "\\[2012-03-29 .* 16:40\\]"
  2460. (org-test-parse-and-interpret "[2012-03-29 thu. 16:40]")))
  2461. (should
  2462. (string-match
  2463. "\\[2012-03-29 .* 16:40\\]"
  2464. (org-element-timestamp-interpreter
  2465. '(timestamp
  2466. (:type inactive :year-start 2012 :month-start 3 :day-start 29
  2467. :hour-start 16 :minute-start 40)) nil)))
  2468. ;; Active range.
  2469. (should
  2470. (string-match "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
  2471. (org-test-parse-and-interpret
  2472. "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>")))
  2473. (should
  2474. (string-match
  2475. "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
  2476. (org-element-timestamp-interpreter
  2477. '(timestamp
  2478. (:type active-range :year-start 2012 :month-start 3 :day-start 29
  2479. :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
  2480. :day-end 29 :hour-end 16 :minute-end 41)) nil)))
  2481. ;; Inactive range.
  2482. (should
  2483. (string-match "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
  2484. (org-test-parse-and-interpret
  2485. "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]")))
  2486. (should
  2487. (string-match
  2488. "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
  2489. (org-element-timestamp-interpreter
  2490. '(timestamp
  2491. (:type inactive-range :year-start 2012 :month-start 3 :day-start 29
  2492. :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
  2493. :day-end 29 :hour-end 16 :minute-end 41)) nil)))
  2494. ;; Diary.
  2495. (should (equal (org-test-parse-and-interpret "<%%diary-float t 4 2>")
  2496. "<%%diary-float t 4 2>\n"))
  2497. ;; Timestamp with repeater interval, with delay, with both.
  2498. (should
  2499. (string-match "<2012-03-29 .* \\+1y>"
  2500. (org-test-parse-and-interpret "<2012-03-29 thu. +1y>")))
  2501. (should
  2502. (string-match
  2503. "<2012-03-29 .* \\+1y>"
  2504. (org-element-timestamp-interpreter
  2505. '(timestamp
  2506. (:type active :year-start 2012 :month-start 3 :day-start 29
  2507. :repeater-type cumulate :repeater-value 1 :repeater-unit year))
  2508. nil)))
  2509. (should
  2510. (string-match
  2511. "<2012-03-29 .* -1y>"
  2512. (org-element-timestamp-interpreter
  2513. '(timestamp
  2514. (:type active :year-start 2012 :month-start 3 :day-start 29
  2515. :warning-type all :warning-value 1 :warning-unit year))
  2516. nil)))
  2517. (should
  2518. (string-match
  2519. "<2012-03-29 .* \\+1y -1y>"
  2520. (org-element-timestamp-interpreter
  2521. '(timestamp
  2522. (:type active :year-start 2012 :month-start 3 :day-start 29
  2523. :warning-type all :warning-value 1 :warning-unit year
  2524. :repeater-type cumulate :repeater-value 1 :repeater-unit year))
  2525. nil)))
  2526. ;; Timestamp range with repeater interval
  2527. (should
  2528. (string-match "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
  2529. (org-test-parse-and-interpret
  2530. "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>")))
  2531. (should
  2532. (string-match
  2533. "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
  2534. (org-element-timestamp-interpreter
  2535. '(timestamp
  2536. (:type active-range :year-start 2012 :month-start 3 :day-start 29
  2537. :year-end 2012 :month-end 3 :day-end 30 :repeater-type cumulate
  2538. :repeater-value 1 :repeater-unit year))
  2539. nil))))
  2540. (ert-deftest test-org-element/verse-block-interpreter ()
  2541. "Test verse block interpretation."
  2542. (should
  2543. (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
  2544. "#+BEGIN_VERSE\nTest\n#+END_VERSE\n")))
  2545. (ert-deftest test-org-element/bold-interpreter ()
  2546. "Test bold interpreter."
  2547. (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))
  2548. (ert-deftest test-org-element/code-interpreter ()
  2549. "Test code interpreter."
  2550. (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))
  2551. (ert-deftest test-org-element/entity-interpreter ()
  2552. "Test entity interpreter."
  2553. ;; 1. Without brackets.
  2554. (should
  2555. (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
  2556. ;; 2. With brackets.
  2557. (should
  2558. (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))
  2559. (ert-deftest test-org-element/export-snippet-interpreter ()
  2560. "Test export snippet interpreter."
  2561. (should (equal (org-test-parse-and-interpret "@@back-end:contents@@")
  2562. "@@back-end:contents@@\n")))
  2563. (ert-deftest test-org-element/footnote-reference-interpreter ()
  2564. "Test footnote reference interpreter."
  2565. ;; 1. Regular reference.
  2566. (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
  2567. ;; 2. Normalized reference.
  2568. (should (equal (org-test-parse-and-interpret "Text[1]") "Text[1]\n"))
  2569. ;; 3. Named reference.
  2570. (should (equal (org-test-parse-and-interpret "Text[fn:label]")
  2571. "Text[fn:label]\n"))
  2572. ;; 4. Inline reference.
  2573. (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
  2574. "Text[fn:label:def]\n"))
  2575. ;; 5. Anonymous reference.
  2576. (should (equal (org-test-parse-and-interpret "Text[fn::def]")
  2577. "Text[fn::def]\n")))
  2578. (ert-deftest test-org-element/inline-babel-call-interpreter ()
  2579. "Test inline babel call interpreter."
  2580. ;; 1. Without arguments.
  2581. (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
  2582. ;; 2. With arguments.
  2583. (should (equal (org-test-parse-and-interpret "call_test(x=2)")
  2584. "call_test(x=2)\n"))
  2585. ;; 3. With header arguments.
  2586. (should (equal (org-test-parse-and-interpret
  2587. "call_test[:results output]()[:results html]")
  2588. "call_test[:results output]()[:results html]\n")))
  2589. (ert-deftest test-org-element/inline-src-block-interpreter ()
  2590. "Test inline src block interpreter."
  2591. ;; 1. Without header argument.
  2592. (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
  2593. "src_emacs-lisp{(+ 1 1)}\n"))
  2594. ;; 2. With header arguments.
  2595. (should (equal (org-test-parse-and-interpret
  2596. "src_emacs-lisp[:results silent]{(+ 1 1)}")
  2597. "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))
  2598. (ert-deftest test-org-element/italic-interpreter ()
  2599. "Test italic interpreter."
  2600. (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))
  2601. (ert-deftest test-org-element/latex-fragment-interpreter ()
  2602. "Test latex fragment interpreter."
  2603. (should (equal (org-test-parse-and-interpret "\\command{}") "\\command{}\n"))
  2604. (should (equal (org-test-parse-and-interpret "$x$") "$x$\n"))
  2605. (should (equal (org-test-parse-and-interpret "$x+y$") "$x+y$\n"))
  2606. (should (equal (org-test-parse-and-interpret "$$x+y$$") "$$x+y$$\n"))
  2607. (should (equal (org-test-parse-and-interpret "\\(x+y\\)") "\\(x+y\\)\n"))
  2608. (should (equal (org-test-parse-and-interpret "\\[x+y\\]") "\\[x+y\\]\n")))
  2609. (ert-deftest test-org-element/line-break-interpreter ()
  2610. "Test line break interpreter."
  2611. (should (equal (org-test-parse-and-interpret "First line \\\\ \nSecond line")
  2612. "First line \\\\\nSecond line\n")))
  2613. (ert-deftest test-org-element/link-interpreter ()
  2614. "Test link interpreter."
  2615. ;; 1. Links targeted from a radio target.
  2616. (should (equal (let ((org-target-link-regexp "radio-target"))
  2617. (org-test-parse-and-interpret "a radio-target"))
  2618. "a radio-target\n"))
  2619. ;; 2. Regular links.
  2620. ;;
  2621. ;; 2.1. Without description.
  2622. (should (equal (org-test-parse-and-interpret "[[http://orgmode.org]]")
  2623. "[[http://orgmode.org]]\n"))
  2624. ;; 2.2. With a description.
  2625. (should (equal (org-test-parse-and-interpret
  2626. "[[http://orgmode.org][Org mode]]")
  2627. "[[http://orgmode.org][Org mode]]\n"))
  2628. ;; 2.3. Id links.
  2629. (should (equal (org-test-parse-and-interpret "[[id:aaaa]]") "[[id:aaaa]]\n"))
  2630. ;; 2.4. Custom-id links.
  2631. (should (equal (org-test-parse-and-interpret "[[#id]]") "[[#id]]\n"))
  2632. ;; 2.5 Code-ref links.
  2633. (should (equal (org-test-parse-and-interpret "[[(ref)]]") "[[(ref)]]\n"))
  2634. ;; 3. Normalize plain links.
  2635. (should (equal (org-test-parse-and-interpret "http://orgmode.org")
  2636. "[[http://orgmode.org]]\n"))
  2637. ;; 4. Normalize angular links.
  2638. (should (equal (org-test-parse-and-interpret "<http://orgmode.org>")
  2639. "[[http://orgmode.org]]\n")))
  2640. (ert-deftest test-org-element/macro-interpreter ()
  2641. "Test macro interpreter."
  2642. ;; 1. Without argument.
  2643. (should (equal (org-test-parse-and-interpret "{{{test}}}") "{{{test}}}\n"))
  2644. ;; 2. With arguments.
  2645. (should (equal (org-test-parse-and-interpret "{{{test(arg1,arg2)}}}")
  2646. "{{{test(arg1,arg2)}}}\n")))
  2647. (ert-deftest test-org-element/radio-target-interpreter ()
  2648. "Test radio target interpreter."
  2649. (should (equal (org-test-parse-and-interpret "<<<some text>>>")
  2650. "<<<some text>>>\n")))
  2651. (ert-deftest test-org-element/statistics-cookie-interpreter ()
  2652. "Test statistics cookie interpreter."
  2653. ;; 1. Without percent
  2654. (should (equal (org-test-parse-and-interpret "[0/1]") "[0/1]\n"))
  2655. ;; 2. With percent.
  2656. (should (equal (org-test-parse-and-interpret "[66%]") "[66%]\n")))
  2657. (ert-deftest test-org-element/strike-through-interpreter ()
  2658. "Test strike through interpreter."
  2659. (should (equal (org-test-parse-and-interpret "+target+") "+target+\n")))
  2660. (ert-deftest test-org-element/subscript-interpreter ()
  2661. "Test subscript interpreter."
  2662. ;; 1. Without brackets.
  2663. (should (equal (org-test-parse-and-interpret "a_b") "a_b\n"))
  2664. ;; 2. With brackets.
  2665. (should (equal (org-test-parse-and-interpret "a_{b}") "a_{b}\n")))
  2666. (ert-deftest test-org-element/superscript-interpreter ()
  2667. "Test superscript interpreter."
  2668. ;; 1. Without brackets.
  2669. (should (equal (org-test-parse-and-interpret "a^b") "a^b\n"))
  2670. ;; 2. With brackets.
  2671. (should (equal (org-test-parse-and-interpret "a^{b}") "a^{b}\n")))
  2672. (ert-deftest test-org-element/target-interpreter ()
  2673. "Test target interpreter."
  2674. (should (equal (org-test-parse-and-interpret "<<target>>") "<<target>>\n")))
  2675. (ert-deftest test-org-element/underline-interpreter ()
  2676. "Test underline interpreter."
  2677. (should (equal (org-test-parse-and-interpret "_text_") "_text_\n")))
  2678. (ert-deftest test-org-element/verbatim-interpreter ()
  2679. "Test verbatim interpreter."
  2680. (should (equal (org-test-parse-and-interpret "=text=") "=text=\n")))
  2681. ;;; Test Granularity
  2682. (ert-deftest test-org-element/granularity ()
  2683. "Test granularity impact on buffer parsing."
  2684. (org-test-with-temp-text "
  2685. * Head 1
  2686. ** Head 2
  2687. #+BEGIN_CENTER
  2688. Centered paragraph.
  2689. #+END_CENTER
  2690. Paragraph \\alpha."
  2691. ;; 1.1. Granularity set to `headline' should parse every headline
  2692. ;; in buffer, and only them.
  2693. (let ((tree (org-element-parse-buffer 'headline)))
  2694. (should (= 2 (length (org-element-map tree 'headline 'identity))))
  2695. (should-not (org-element-map tree 'paragraph 'identity)))
  2696. ;; 1.2. Granularity set to `greater-element' should not enter
  2697. ;; greater elements excepted headlines and sections.
  2698. (let ((tree (org-element-parse-buffer 'greater-element)))
  2699. (should (= 1 (length (org-element-map tree 'center-block 'identity))))
  2700. (should (= 1 (length (org-element-map tree 'paragraph 'identity))))
  2701. (should-not (org-element-map tree 'entity 'identity)))
  2702. ;; 1.3. Granularity set to `element' should enter every
  2703. ;; greater-element.
  2704. (let ((tree (org-element-parse-buffer 'element)))
  2705. (should (= 2 (length (org-element-map tree 'paragraph 'identity))))
  2706. (should-not (org-element-map tree 'entity 'identity)))
  2707. ;; 1.4. Granularity set to `object' can see everything.
  2708. (let ((tree (org-element-parse-buffer 'object)))
  2709. (should (= 1 (length (org-element-map tree 'entity 'identity)))))))
  2710. (ert-deftest test-org-element/secondary-string-parsing ()
  2711. "Test if granularity correctly toggles secondary strings parsing."
  2712. ;; With a granularity bigger than `object', no secondary string
  2713. ;; should be parsed.
  2714. (should
  2715. (stringp
  2716. (org-test-with-temp-text "* Headline"
  2717. (let ((headline
  2718. (org-element-map (org-element-parse-buffer 'headline) 'headline
  2719. #'identity nil 'first-match)))
  2720. (org-element-property :title headline)))))
  2721. (should
  2722. (stringp
  2723. (org-test-with-temp-text "* Headline\n- tag :: item"
  2724. (let ((item (org-element-map (org-element-parse-buffer 'element) 'item
  2725. #'identity nil 'first-match)))
  2726. (org-element-property :tag item)))))
  2727. (when (featurep 'org-inlinetask)
  2728. (should
  2729. (stringp
  2730. (let ((org-inlinetask-min-level 15))
  2731. (org-test-with-temp-text "*************** Inlinetask"
  2732. (let ((inlinetask (org-element-map (org-element-parse-buffer 'element)
  2733. 'inlinetask
  2734. #'identity nil 'first-match)))
  2735. (org-element-property :title inlinetask)))))))
  2736. ;; With a default granularity, secondary strings should be parsed.
  2737. (should
  2738. (listp
  2739. (org-test-with-temp-text "* Headline"
  2740. (let ((headline
  2741. (org-element-map (org-element-parse-buffer) 'headline
  2742. #'identity nil 'first-match)))
  2743. (org-element-property :title headline)))))
  2744. ;; `org-element-at-point' should never parse a secondary string.
  2745. (should-not
  2746. (listp
  2747. (org-test-with-temp-text "* Headline"
  2748. (org-element-property :title (org-element-at-point)))))
  2749. ;; Preserve current local variables when parsing a secondary string.
  2750. (should
  2751. (let ((org-entities nil)
  2752. (org-entities-user nil))
  2753. (org-test-with-temp-text "
  2754. #+CAPTION: \\foo
  2755. Text
  2756. # Local Variables:
  2757. # org-entities-user: ((\"foo\"))
  2758. # End:"
  2759. (let ((safe-local-variable-values '((org-entities-user . (("foo"))))))
  2760. (hack-local-variables))
  2761. (org-element-map (org-element-parse-buffer) 'entity
  2762. #'identity nil nil nil t)))))
  2763. ;;; Test Visible Only Parsing
  2764. (ert-deftest test-org-element/parse-buffer-visible ()
  2765. "Test `org-element-parse-buffer' with visible only argument."
  2766. (should
  2767. (equal '("H1" "H3" "H5")
  2768. (org-test-with-temp-text
  2769. "* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:"
  2770. (org-occur ":visible:")
  2771. (org-element-map (org-element-parse-buffer nil t) 'headline
  2772. (lambda (hl) (org-element-property :raw-value hl)))))))
  2773. ;;; Test `:parent' Property
  2774. (ert-deftest test-org-element/parent-property ()
  2775. "Test `:parent' property."
  2776. ;; Elements.
  2777. (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
  2778. (let* ((tree (org-element-parse-buffer))
  2779. (parent (org-element-property
  2780. :parent
  2781. (org-element-map tree 'paragraph 'identity nil t))))
  2782. (should parent)
  2783. (should (eq (org-element-map tree 'center-block 'identity nil t)
  2784. parent))))
  2785. ;; Objects.
  2786. (org-test-with-temp-text "a_{/b/}"
  2787. (let* ((tree (org-element-parse-buffer))
  2788. (parent (org-element-property
  2789. :parent
  2790. (org-element-map tree 'italic 'identity nil t))))
  2791. (should parent)
  2792. (should (eq parent
  2793. (org-element-map tree 'subscript 'identity nil t)))))
  2794. ;; Secondary strings
  2795. (org-test-with-temp-text "* /italic/"
  2796. (let* ((tree (org-element-parse-buffer))
  2797. (parent (org-element-property
  2798. :parent (org-element-map tree 'italic 'identity nil t))))
  2799. (should parent)
  2800. (should (eq parent
  2801. (org-element-map tree 'headline 'identity nil t))))))
  2802. ;;; Test Normalize Contents
  2803. (ert-deftest test-org-element/normalize-contents ()
  2804. "Test `org-element-normalize-contents' specifications."
  2805. ;; Remove maximum common indentation from element's contents.
  2806. (should
  2807. (equal
  2808. (org-element-normalize-contents
  2809. '(paragraph nil " Two spaces\n Three spaces"))
  2810. '(paragraph nil "Two spaces\n Three spaces")))
  2811. ;; Ignore objects within contents when computing maximum common
  2812. ;; indentation. However, if contents start with an object, common
  2813. ;; indentation is 0.
  2814. (should
  2815. (equal
  2816. (org-element-normalize-contents
  2817. '(paragraph nil " One " (emphasis nil "space") "\n Two spaces"))
  2818. '(paragraph nil "One " (emphasis nil "space") "\n Two spaces")))
  2819. (should
  2820. (equal
  2821. (org-element-normalize-contents
  2822. '(paragraph nil (verbatim nil "V") "No space\n Two\n Three"))
  2823. '(paragraph nil (verbatim nil "V") "No space\n Two\n Three")))
  2824. ;; Ignore blank lines.
  2825. (should
  2826. (equal
  2827. (org-element-normalize-contents
  2828. '(paragraph nil " Two spaces\n\n \n Two spaces"))
  2829. '(paragraph nil "Two spaces\n\n \nTwo spaces")))
  2830. (should
  2831. (equal
  2832. '(paragraph nil " Two spaces\n" (verbatim nil "V") "\n Two spaces")
  2833. (org-element-normalize-contents
  2834. '(paragraph nil " Two spaces\n " (verbatim nil "V") "\n Two spaces"))))
  2835. (should
  2836. (equal
  2837. '(verse-block nil "line 1\n\nline 2")
  2838. (org-element-normalize-contents
  2839. '(verse-block nil " line 1\n\n line 2"))))
  2840. ;; Recursively enter objects in order to compute common indentation.
  2841. (should
  2842. (equal
  2843. (org-element-normalize-contents
  2844. '(paragraph nil " Two spaces " (bold nil " and\n One space")))
  2845. '(paragraph nil " Two spaces " (bold nil " and\nOne space"))))
  2846. ;; When optional argument is provided, ignore first line
  2847. ;; indentation.
  2848. (should
  2849. (equal
  2850. (org-element-normalize-contents
  2851. '(paragraph nil "No space\n Two spaces\n Three spaces") t)
  2852. '(paragraph nil "No space\nTwo spaces\n Three spaces")))
  2853. (should
  2854. (equal
  2855. (org-element-normalize-contents
  2856. '(paragraph nil (verbatim nil "V") "No space\n Two\n Three") t)
  2857. '(paragraph nil (verbatim nil "V") "No space\nTwo\n Three")))
  2858. ;; Corner case: do not ignore indentation of string right after
  2859. ;; a line break.
  2860. (should
  2861. (equal
  2862. (org-element-normalize-contents
  2863. '(paragraph nil " 1 space" (line-break) " 2 spaces"))
  2864. '(paragraph nil "1 space" (line-break) " 2 spaces"))))
  2865. ;;; Test Navigation Tools.
  2866. (ert-deftest test-org-element/at-point ()
  2867. "Test `org-element-at-point' specifications."
  2868. ;; Return closest element containing point.
  2869. (should
  2870. (eq 'paragraph
  2871. (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
  2872. (progn (search-forward "A")
  2873. (org-element-type (org-element-at-point))))))
  2874. ;; Correctly set `:parent' property.
  2875. (should
  2876. (eq 'center-block
  2877. (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
  2878. (progn (search-forward "A")
  2879. (org-element-type
  2880. (org-element-property :parent (org-element-at-point)))))))
  2881. ;; Special case: at a blank line just below a headline, return that
  2882. ;; headline.
  2883. (should
  2884. (equal "H1" (org-test-with-temp-text "* H1\n \n* H2\n"
  2885. (forward-line)
  2886. (org-element-property :title (org-element-at-point)))))
  2887. ;; Special case: at the very beginning of a table, return `table'
  2888. ;; object instead of `table-row'.
  2889. (should
  2890. (eq 'table
  2891. (org-test-with-temp-text "| a | b |"
  2892. (org-element-type (org-element-at-point)))))
  2893. ;; Special case: at the very beginning of a list or sub-list, return
  2894. ;; `plain-list' object instead of `item'.
  2895. (should
  2896. (eq 'plain-list
  2897. (org-test-with-temp-text "- item"
  2898. (org-element-type (org-element-at-point)))))
  2899. ;; Special case: at the closing line of a greater element, be sure
  2900. ;; to return it instead of the last element in its contents.
  2901. (should
  2902. (eq 'center-block
  2903. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph\n#+END_CENTER"
  2904. (progn (forward-line 2)
  2905. (org-element-type (org-element-at-point))))))
  2906. ;; Special case: at a blank line between two items, be sure to
  2907. ;; return item above instead of the last element of its contents.
  2908. (should
  2909. (eq 'item
  2910. (org-test-with-temp-text "- Para1\n\n- Para2"
  2911. (progn (forward-line)
  2912. (org-element-type
  2913. (let ((org-list-empty-line-terminates-plain-lists nil))
  2914. (org-element-at-point)))))))
  2915. ;; Special case: at the last blank line in a plain list, return it
  2916. ;; instead of the last item.
  2917. (should
  2918. (eq 'plain-list
  2919. (org-test-with-temp-text "- Para1\n- Para2\n\nPara3"
  2920. (progn (forward-line 2)
  2921. (org-element-type (org-element-at-point))))))
  2922. ;; Special case: when a list ends at the end of buffer and there's
  2923. ;; no final newline, return last element in last item.
  2924. (should
  2925. (eq 'paragraph
  2926. (org-test-with-temp-text "- a"
  2927. (end-of-line)
  2928. (org-element-type (org-element-at-point)))))
  2929. ;; Parse a list within a block itself contained in a list.
  2930. (should
  2931. (eq 'plain-list
  2932. (org-test-with-temp-text
  2933. "- outer\n #+begin_center\n - inner\n #+end_center"
  2934. (search-forward "inner")
  2935. (beginning-of-line)
  2936. (org-element-type (org-element-at-point)))))
  2937. ;; Do not error at eob on an empty line.
  2938. (should
  2939. (org-test-with-temp-text "* H\n"
  2940. (forward-line)
  2941. (or (org-element-at-point) t))))
  2942. (ert-deftest test-org-element/context ()
  2943. "Test `org-element-context' specifications."
  2944. ;; Return closest object containing point.
  2945. (should
  2946. (eq 'underline
  2947. (org-test-with-temp-text "Some *text with _underline_ text*"
  2948. (progn (search-forward "under")
  2949. (org-element-type (org-element-context))))))
  2950. ;; Find objects in secondary strings.
  2951. (should
  2952. (eq 'underline
  2953. (org-test-with-temp-text "* Headline _with_ underlining"
  2954. (progn (search-forward "w")
  2955. (org-element-type (org-element-context))))))
  2956. ;; Find objects in objects.
  2957. (should
  2958. (eq 'macro
  2959. (org-test-with-temp-text "| a | {{{macro}}} |"
  2960. (progn (search-forward "{")
  2961. (org-element-type (org-element-context))))))
  2962. (should
  2963. (eq 'table-cell
  2964. (org-test-with-temp-text "| a | b {{{macro}}} |"
  2965. (progn (search-forward "b")
  2966. (org-element-type (org-element-context))))))
  2967. ;; Find objects in planning lines.
  2968. (should
  2969. (eq 'timestamp
  2970. (org-test-with-temp-text "* H\n SCHEDULED: <2012-03-29 thu.>"
  2971. (search-forward "2012")
  2972. (org-element-type (org-element-context)))))
  2973. (should-not
  2974. (eq 'timestamp
  2975. (org-test-with-temp-text "* H\n SCHEDULED: <2012-03-29 thu.>"
  2976. (search-forward "SCHEDULED")
  2977. (org-element-type (org-element-context)))))
  2978. ;; Do not find objects in table rules.
  2979. (should
  2980. (eq 'table-row
  2981. (org-test-with-temp-text "| a | b |\n+---+---+\n| c | d |"
  2982. (forward-line)
  2983. (org-element-type (org-element-context)))))
  2984. ;; Find objects in parsed affiliated keywords.
  2985. (should
  2986. (eq 'macro
  2987. (org-test-with-temp-text "#+CAPTION: {{{macro}}}\n| a | b |."
  2988. (progn (search-forward "{")
  2989. (org-element-type (org-element-context))))))
  2990. (should
  2991. (eq 'bold
  2992. (org-test-with-temp-text "#+caption: *bold*\nParagraph"
  2993. (progn (search-forward "*")
  2994. (org-element-type (org-element-context))))))
  2995. ;; Find objects at the end of buffer.
  2996. (should
  2997. (eq 'bold
  2998. (org-test-with-temp-text "*bold*"
  2999. (goto-char (point-max))
  3000. (org-element-type (org-element-context)))))
  3001. ;; Correctly set `:parent' property.
  3002. (should
  3003. (eq 'paragraph
  3004. (org-test-with-temp-text "Some *bold* text"
  3005. (progn (search-forward "bold")
  3006. (org-element-type
  3007. (org-element-property :parent (org-element-context)))))))
  3008. ;; Between two objects, return the second one.
  3009. (should
  3010. (eq 'macro
  3011. (org-test-with-temp-text "<<target>>{{{test}}}"
  3012. (progn (search-forward "{")
  3013. (backward-char)
  3014. (org-element-type (org-element-context))))))
  3015. ;; Test optional argument.
  3016. (should
  3017. (eq 'underline
  3018. (org-test-with-temp-text "Some *text with _underline_ text*"
  3019. (progn
  3020. (search-forward "under")
  3021. (org-element-type (org-element-context (org-element-at-point)))))))
  3022. ;; Special case: bold object at the beginning of a headline.
  3023. (should
  3024. (eq 'bold
  3025. (org-test-with-temp-text "* *bold*"
  3026. (search-forward "bo")
  3027. (org-element-type (org-element-context)))))
  3028. ;; Special case: incomplete cell at the end of a table row.
  3029. (should
  3030. (eq 'table-cell
  3031. (org-test-with-temp-text "|a|b|c"
  3032. (goto-char (point-max))
  3033. (org-element-type (org-element-context)))))
  3034. ;; Special case: objects in inline footnotes.
  3035. (should
  3036. (eq 'link
  3037. (org-test-with-temp-text "[fn::[[<point>http://orgmode.org]]]"
  3038. (org-element-type (org-element-context))))))
  3039. ;;; Test Tools
  3040. (ert-deftest test-org-element/lineage ()
  3041. "Test `org-element-lineage' specifications."
  3042. ;; Regular tests. When applied to an element or object returned by
  3043. ;; `org-element-at-point' or `org-element-context', the list is
  3044. ;; limited to the current section.
  3045. (should
  3046. (equal '(paragraph center-block)
  3047. (org-test-with-temp-text
  3048. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3049. (mapcar #'car (org-element-lineage (org-element-context))))))
  3050. (should
  3051. (equal '(paragraph center-block section headline headline org-data)
  3052. (org-test-with-temp-text
  3053. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3054. (mapcar #'car
  3055. (org-element-lineage
  3056. (org-element-map (org-element-parse-buffer) 'bold
  3057. #'identity nil t))))))
  3058. ;; Test TYPES optional argument.
  3059. (should
  3060. (eq 'center-block
  3061. (org-test-with-temp-text
  3062. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3063. (org-element-type
  3064. (org-element-lineage (org-element-context) '(center-block))))))
  3065. (should-not
  3066. (org-test-with-temp-text
  3067. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3068. (org-element-lineage (org-element-context) '(example-block))))
  3069. ;; Test WITH-SELF optional argument.
  3070. (should
  3071. (equal '(bold paragraph center-block)
  3072. (org-test-with-temp-text
  3073. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3074. (mapcar #'car (org-element-lineage (org-element-context) nil t)))))
  3075. ;; When TYPES and WITH-SELF are provided, the latter is also checked
  3076. ;; against the former.
  3077. (should
  3078. (org-test-with-temp-text
  3079. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3080. (org-element-lineage (org-element-context) '(bold) t))))
  3081. ;;; Test Cache.
  3082. (ert-deftest test-org-element/cache ()
  3083. "Test basic expectations and common pitfalls for cache."
  3084. ;; Shift positions.
  3085. (should
  3086. (equal '(18 . 23)
  3087. (org-test-with-temp-text "para1\n\npara2\n\npara3"
  3088. (let ((org-element-use-cache t))
  3089. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3090. (insert "add")
  3091. (forward-line 4)
  3092. (let ((element (org-element-at-point)))
  3093. (cons (org-element-property :begin element)
  3094. (org-element-property :end element)))))))
  3095. ;; Partial shifting: when the contents of a greater element are
  3096. ;; modified, only shift ending positions.
  3097. (should
  3098. (org-test-with-temp-text
  3099. "#+BEGIN_CENTER\nPara1\n\nPara2\n\nPara3\n#+END_CENTER"
  3100. (let ((org-element-use-cache t))
  3101. (save-excursion (search-forward "3") (org-element-at-point))
  3102. (search-forward "Para2")
  3103. (insert " ")
  3104. (let ((element (org-element-property :parent (org-element-at-point))))
  3105. (equal (cons (org-element-property :begin element)
  3106. (org-element-property :end element))
  3107. (cons (point-min) (point-max)))))))
  3108. ;; Re-parent shifted elements.
  3109. (should
  3110. (eq 'item
  3111. (org-test-with-temp-text "- item\n\n\n para1\n para2"
  3112. (let ((org-element-use-cache t))
  3113. (end-of-line)
  3114. (org-element-at-point)
  3115. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3116. (forward-line)
  3117. (delete-char 1)
  3118. (goto-char (point-max))
  3119. (org-element-type
  3120. (org-element-property :parent (org-element-at-point)))))))
  3121. ;; Preserve local structures when re-parenting.
  3122. (should
  3123. (eq 'table
  3124. (org-test-with-temp-text
  3125. "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n| c | d |\n#+end_center"
  3126. (let ((org-element-use-cache t))
  3127. (save-excursion (search-forward "| c |") (org-element-at-point))
  3128. (insert "- item")
  3129. (search-forward "| c |")
  3130. (beginning-of-line)
  3131. (org-element-type
  3132. (org-element-property :parent (org-element-at-point)))))))
  3133. (should-not
  3134. (eq 'center-block
  3135. (org-test-with-temp-text
  3136. "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n#+end_center"
  3137. (let ((org-element-use-cache t))
  3138. (save-excursion (search-forward "| a |") (org-element-at-point))
  3139. (insert "- item")
  3140. (search-forward "| a |")
  3141. (beginning-of-line)
  3142. (org-element-type
  3143. (org-element-property :parent (org-element-at-point)))))))
  3144. ;; When re-parenting, also propagate changes to list structures.
  3145. (should
  3146. (= 2
  3147. (org-test-with-temp-text "\n Para\n - item<point>"
  3148. (let ((org-element-use-cache t))
  3149. (org-element-at-point)
  3150. (goto-char (point-min))
  3151. (insert "- Top\n")
  3152. (search-forward "- item")
  3153. (beginning-of-line)
  3154. (length (org-element-property :structure (org-element-at-point)))))))
  3155. ;; Modifying the last line of an element alters the element below.
  3156. (should
  3157. (org-test-with-temp-text "para1\n\npara2"
  3158. (let ((org-element-use-cache t))
  3159. (goto-char (point-max))
  3160. (org-element-at-point)
  3161. (forward-line -1)
  3162. (insert "merge")
  3163. (let ((element (org-element-at-point)))
  3164. (equal (cons (org-element-property :begin element)
  3165. (org-element-property :end element))
  3166. (cons (point-min) (point-max)))))))
  3167. ;; Modifying the first line of an element alters the element above.
  3168. (should
  3169. (org-test-with-temp-text ": fixed-width\n:not-fixed-width"
  3170. (let ((org-element-use-cache t))
  3171. (goto-char (point-max))
  3172. (org-element-at-point)
  3173. (search-backward ":")
  3174. (forward-char)
  3175. (insert " ")
  3176. (let ((element (org-element-at-point)))
  3177. (equal (cons (org-element-property :begin element)
  3178. (org-element-property :end element))
  3179. (cons (point-min) (point-max)))))))
  3180. ;; Sensitive change: adding a line alters document structure both
  3181. ;; above and below.
  3182. (should
  3183. (eq 'example-block
  3184. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n"
  3185. (let ((org-element-use-cache t))
  3186. (goto-char (point-max))
  3187. (org-element-at-point)
  3188. (insert "#+END_EXAMPLE")
  3189. (search-backward "Para1")
  3190. (org-element-type (org-element-at-point))))))
  3191. (should
  3192. (eq 'example-block
  3193. (org-test-with-temp-text "Para1\n\nPara2\n#+END_EXAMPLE"
  3194. (let ((org-element-use-cache t))
  3195. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3196. (insert "#+BEGIN_EXAMPLE\n")
  3197. (search-forward "Para2")
  3198. (org-element-type (org-element-at-point))))))
  3199. ;; Sensitive change: removing a line alters document structure both
  3200. ;; above and below.
  3201. (should
  3202. (eq 'example-block
  3203. (org-test-with-temp-text
  3204. "# +BEGIN_EXAMPLE\nPara1\n\nPara2\n#+END_EXAMPLE"
  3205. (let ((org-element-use-cache t))
  3206. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3207. (forward-char)
  3208. (delete-char 1)
  3209. (search-forward "Para2")
  3210. (org-element-type (org-element-at-point))))))
  3211. (should
  3212. (eq 'example-block
  3213. (org-test-with-temp-text
  3214. "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n# +END_EXAMPLE"
  3215. (let ((org-element-use-cache t))
  3216. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3217. (search-forward "# ")
  3218. (delete-char -1)
  3219. (search-backward "Para1")
  3220. (org-element-type (org-element-at-point))))))
  3221. ;; Corner case: watch out drawers named "PROPERTIES" as they are
  3222. ;; fragile, unlike to other drawers.
  3223. (should
  3224. (eq 'node-property
  3225. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A<point>\n:END:"
  3226. (let ((org-element-use-cache t))
  3227. (org-element-at-point)
  3228. (insert "+:")
  3229. (org-element-type (org-element-at-point)))))))
  3230. (provide 'test-org-element)
  3231. ;;; test-org-element.el ends here