test-org-element.el 137 KB

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