test-org-element.el 136 KB

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