test-org-element.el 135 KB

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