test-org-element.el 135 KB

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