test-org-element.el 133 KB

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