test-org-element.el 134 KB

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