test-org-element.el 131 KB

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