test-org-element.el 133 KB

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