test-org-element.el 131 KB

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