test-org-element.el 136 KB

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