test-org-element.el 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174
  1. ;;; test-org-element.el --- Tests for org-element.el
  2. ;; Copyright (C) 2012, 2013, 2014 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. (ert-deftest test-org-element/adopt-elements ()
  125. "Test `org-element-adopt-elements' specifications."
  126. ;; Adopt an element.
  127. (should
  128. (equal '(plain-text italic)
  129. (org-test-with-temp-text "* Headline\n *a*"
  130. (let ((tree (org-element-parse-buffer)))
  131. (org-element-adopt-elements
  132. (org-element-map tree 'bold 'identity nil t) '(italic nil "a"))
  133. (mapcar (lambda (blob) (org-element-type blob))
  134. (org-element-contents
  135. (org-element-map tree 'bold 'identity nil t)))))))
  136. ;; Adopt a string.
  137. (should
  138. (equal '("a" "b")
  139. (org-test-with-temp-text "* Headline\n *a*"
  140. (let ((tree (org-element-parse-buffer)))
  141. (org-element-adopt-elements
  142. (org-element-map tree 'bold 'identity nil t) "b")
  143. (org-element-contents
  144. (org-element-map tree 'bold 'identity nil t)))))))
  145. (ert-deftest test-org-element/extract-element ()
  146. "Test `org-element-extract-element' specifications."
  147. ;; Extract a greater element.
  148. (should
  149. (equal '(org-data nil)
  150. (org-test-with-temp-text "* Headline"
  151. (let* ((tree (org-element-parse-buffer))
  152. (element (org-element-map tree 'headline 'identity nil t)))
  153. (org-element-extract-element element)
  154. tree))))
  155. ;; Extract an element.
  156. (should-not
  157. (org-element-map
  158. (org-test-with-temp-text "Paragraph"
  159. (let* ((tree (org-element-parse-buffer))
  160. (element (org-element-map tree 'paragraph 'identity nil t)))
  161. (org-element-extract-element element)
  162. tree))
  163. 'paragraph
  164. 'identity))
  165. ;; Extract an object, even in a secondary string.
  166. (should-not
  167. (org-element-map
  168. (org-test-with-temp-text "*bold*"
  169. (let* ((tree (org-element-parse-buffer))
  170. (element (org-element-map tree 'bold 'identity nil t)))
  171. (org-element-extract-element element)
  172. tree))
  173. 'bold
  174. 'identity))
  175. (should-not
  176. (org-element-map
  177. (org-test-with-temp-text "* Headline *bold*"
  178. (let* ((tree (org-element-parse-buffer))
  179. (element (org-element-map tree 'bold 'identity nil t)))
  180. (org-element-extract-element element)
  181. tree))
  182. 'bold
  183. 'identity))
  184. ;; Return value doesn't have any :parent set.
  185. (should-not
  186. (org-element-property
  187. :parent
  188. (org-test-with-temp-text "* Headline\n Paragraph with *bold* text."
  189. (let* ((tree (org-element-parse-buffer))
  190. (element (org-element-map tree 'bold 'identity nil t)))
  191. (org-element-extract-element element))))))
  192. (ert-deftest test-org-element/insert-before ()
  193. "Test `org-element-insert-before' specifications."
  194. ;; Standard test.
  195. (should
  196. (equal
  197. '(italic entity bold)
  198. (org-test-with-temp-text "/some/ *paragraph*"
  199. (let* ((tree (org-element-parse-buffer))
  200. (paragraph (org-element-map tree 'paragraph 'identity nil t))
  201. (bold (org-element-map tree 'bold 'identity nil t)))
  202. (org-element-insert-before '(entity (:name "\\alpha")) bold)
  203. (org-element-map tree '(bold entity italic) #'org-element-type nil)))))
  204. ;; Insert an object in a secondary string.
  205. (should
  206. (equal
  207. '(entity italic)
  208. (org-test-with-temp-text "* /A/\n Paragraph."
  209. (let* ((tree (org-element-parse-buffer))
  210. (headline (org-element-map tree 'headline 'identity nil t))
  211. (italic (org-element-map tree 'italic 'identity nil t)))
  212. (org-element-insert-before '(entity (:name "\\alpha")) italic)
  213. (org-element-map (org-element-property :title headline) '(entity italic)
  214. #'org-element-type))))))
  215. (ert-deftest test-org-element/set-element ()
  216. "Test `org-element-set-element' specifications."
  217. ;; Check if new element is inserted.
  218. (should
  219. (org-test-with-temp-text "* Headline\n*a*"
  220. (let* ((tree (org-element-parse-buffer))
  221. (bold (org-element-map tree 'bold 'identity nil t)))
  222. (org-element-set-element bold '(italic nil "b"))
  223. (org-element-map tree 'italic 'identity))))
  224. ;; Check if old element is removed.
  225. (should-not
  226. (org-test-with-temp-text "* Headline\n*a*"
  227. (let* ((tree (org-element-parse-buffer))
  228. (bold (org-element-map tree 'bold 'identity nil t)))
  229. (org-element-set-element bold '(italic nil "b"))
  230. (org-element-map tree 'bold 'identity))))
  231. ;; Check if :parent property is correctly set.
  232. (should
  233. (eq 'paragraph
  234. (org-test-with-temp-text "* Headline\n*a*"
  235. (let* ((tree (org-element-parse-buffer))
  236. (bold (org-element-map tree 'bold 'identity nil t)))
  237. (org-element-set-element bold '(italic nil "b"))
  238. (org-element-type
  239. (org-element-property
  240. :parent (org-element-map tree 'italic 'identity nil t)))))))
  241. ;; Allow to replace strings with elements.
  242. (should
  243. (equal '("b")
  244. (org-test-with-temp-text "* Headline"
  245. (let* ((tree (org-element-parse-buffer))
  246. (text (org-element-map tree 'plain-text 'identity nil t)))
  247. (org-element-set-element text (list 'bold nil "b"))
  248. (org-element-map tree 'plain-text 'identity)))))
  249. ;; Allow to replace elements with strings.
  250. (should
  251. (equal "a"
  252. (org-test-with-temp-text "* =verbatim="
  253. (let* ((tree (org-element-parse-buffer))
  254. (verb (org-element-map tree 'verbatim 'identity nil t)))
  255. (org-element-set-element verb "a")
  256. (org-element-map tree 'plain-text 'identity nil t)))))
  257. ;; Allow to replace strings with strings.
  258. (should
  259. (equal "b"
  260. (org-test-with-temp-text "a"
  261. (let* ((tree (org-element-parse-buffer))
  262. (text (org-element-map tree 'plain-text 'identity nil t)))
  263. (org-element-set-element text "b")
  264. (org-element-map tree 'plain-text 'identity nil t))))))
  265. ;;; Test Parsers
  266. ;;;; Affiliated Keywords
  267. (ert-deftest test-org-element/affiliated-keywords-parser ()
  268. "Test affiliated keywords parsing."
  269. ;; Read simple keywords.
  270. (should
  271. (equal "para"
  272. (org-element-property
  273. :name
  274. (org-test-with-temp-text "#+NAME: para\nParagraph"
  275. (org-element-at-point)))))
  276. (should
  277. (= 1
  278. (org-element-property
  279. :begin
  280. (org-test-with-temp-text "#+NAME: para\nParagraph"
  281. (org-element-at-point)))))
  282. ;; Parse multiple keywords.
  283. (should
  284. (equal
  285. '("line2" "line1")
  286. (org-element-property
  287. :attr_ascii
  288. (org-test-with-temp-text
  289. "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph"
  290. (org-element-at-point)))))
  291. ;; Parse "parsed" keywords.
  292. (should
  293. (equal
  294. '(("caption"))
  295. (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
  296. (car (org-element-property :caption (org-element-at-point))))))
  297. ;; Parse dual keywords.
  298. (should
  299. (equal
  300. '((("long") "short"))
  301. (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
  302. (org-element-property :caption (org-element-at-point)))))
  303. ;; Allow multiple caption keywords.
  304. (should
  305. (equal
  306. '((("l2") "s2") (("l1") "s1"))
  307. (org-test-with-temp-text "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph"
  308. (org-element-property :caption (org-element-at-point)))))
  309. (should
  310. (equal
  311. '((("l1")) (nil "s1"))
  312. (org-test-with-temp-text "#+CAPTION[s1]:\n#+CAPTION: l1\nParagraph"
  313. (org-element-property :caption (org-element-at-point)))))
  314. ;; Corner case: orphaned keyword at the end of an element.
  315. (should
  316. (eq 'keyword
  317. (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
  318. (progn (search-forward "name")
  319. (org-element-type (org-element-at-point))))))
  320. (should-not
  321. (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
  322. (progn (search-forward "Some")
  323. (org-element-property :name (org-element-at-point))))))
  324. ;;;; Babel Call
  325. (ert-deftest test-org-element/babel-call-parser ()
  326. "Test `babel-call' parsing."
  327. ;; Standard test.
  328. (should
  329. (org-test-with-temp-text "#+CALL: test()"
  330. (org-element-map (org-element-parse-buffer) 'babel-call 'identity)))
  331. ;; Ignore case.
  332. (should
  333. (org-test-with-temp-text "#+call: test()"
  334. (org-element-map (org-element-parse-buffer) 'babel-call 'identity)))
  335. ;; Handle non-empty blank line at the end of buffer.
  336. (should
  337. (org-test-with-temp-text "#+CALL: test()\n "
  338. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  339. ;;;; Bold
  340. (ert-deftest test-org-element/bold-parser ()
  341. "Test `bold' parser."
  342. ;; Standard test.
  343. (should
  344. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  345. (org-test-with-temp-text "*bold*"
  346. (org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
  347. ;; Multi-line markup.
  348. (should
  349. (equal
  350. (org-element-contents
  351. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  352. (org-test-with-temp-text "*first line\nsecond line*"
  353. (org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
  354. '("first line\nsecond line"))))
  355. ;;;; Center Block
  356. (ert-deftest test-org-element/center-block-parser ()
  357. "Test `center-block' parser."
  358. ;; Standard test.
  359. (should
  360. (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
  361. (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
  362. ;; Ignore case.
  363. (should
  364. (org-test-with-temp-text "#+begin_center\nText\n#+end_center"
  365. (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
  366. ;; Ignore incomplete block.
  367. (should-not
  368. (org-test-with-temp-text "#+BEGIN_CENTER"
  369. (org-element-map (org-element-parse-buffer) 'center-block
  370. 'identity nil t)))
  371. ;; Handle non-empty blank line at the end of buffer.
  372. (should
  373. (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
  374. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  375. ;;;; Clock
  376. (ert-deftest test-org-element/clock-parser ()
  377. "Test `clock' parser."
  378. ;; Running clock.
  379. (let ((clock (org-test-with-temp-text "CLOCK: [2012-01-01 sun. 00:01]"
  380. (org-element-at-point))))
  381. (should (eq (org-element-property :status clock) 'running))
  382. (should
  383. (equal (org-element-property :raw-value
  384. (org-element-property :value clock))
  385. "[2012-01-01 sun. 00:01]"))
  386. (should-not (org-element-property :duration clock)))
  387. ;; Closed clock.
  388. (let ((clock
  389. (org-test-with-temp-text
  390. "CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
  391. (org-element-at-point))))
  392. (should (eq (org-element-property :status clock) 'closed))
  393. (should (equal (org-element-property :raw-value
  394. (org-element-property :value clock))
  395. "[2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02]"))
  396. (should (equal (org-element-property :duration clock) "0:01"))))
  397. ;;;; Code
  398. (ert-deftest test-org-element/code-parser ()
  399. "Test `code' parser."
  400. ;; Regular test.
  401. (should
  402. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  403. (org-test-with-temp-text "~code~"
  404. (org-element-map (org-element-parse-buffer) 'code 'identity))))
  405. ;; Multi-line markup.
  406. (should
  407. (equal
  408. (org-element-property
  409. :value
  410. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  411. (org-test-with-temp-text "~first line\nsecond line~"
  412. (org-element-map
  413. (org-element-parse-buffer) 'code 'identity nil t))))
  414. "first line\nsecond line")))
  415. ;;;; Comment
  416. (ert-deftest test-org-element/comment-parser ()
  417. "Test `comment' parser."
  418. ;; Regular comment.
  419. (should
  420. (org-test-with-temp-text "# Comment"
  421. (org-element-map (org-element-parse-buffer) 'comment 'identity)))
  422. ;; Inline comment.
  423. (should
  424. (org-test-with-temp-text " # Comment"
  425. (org-element-map (org-element-parse-buffer) 'comment 'identity)))
  426. ;; Preserve indentation.
  427. (should
  428. (equal
  429. (org-element-property
  430. :value
  431. (org-test-with-temp-text "# No blank\n# One blank"
  432. (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
  433. "No blank\n One blank"))
  434. ;; Comment with blank lines.
  435. (should
  436. (equal
  437. (org-element-property
  438. :value
  439. (org-test-with-temp-text "# First part\n# \n#\n# Second part"
  440. (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
  441. "First part\n\n\nSecond part"))
  442. ;; Do not mix comments and keywords.
  443. (should
  444. (eq 1
  445. (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
  446. (length (org-element-map (org-element-parse-buffer) 'comment
  447. 'identity)))))
  448. (should
  449. (equal "comment"
  450. (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
  451. (org-element-property
  452. :value
  453. (org-element-map (org-element-parse-buffer) 'comment
  454. 'identity nil t)))))
  455. ;; Correctly handle non-empty blank lines at the end of buffer.
  456. (should
  457. (org-test-with-temp-text "# A\n "
  458. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  459. ;;;; Comment Block
  460. (ert-deftest test-org-element/comment-block-parser ()
  461. "Test `comment-block' parser."
  462. ;; Standard test.
  463. (should
  464. (org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
  465. (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
  466. ;; Ignore case.
  467. (should
  468. (org-test-with-temp-text "#+begin_comment\nText\n#+end_comment"
  469. (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
  470. ;; Ignore incomplete block.
  471. (should-not
  472. (org-test-with-temp-text "#+BEGIN_COMMENT"
  473. (org-element-map (org-element-parse-buffer) 'comment-block
  474. 'identity nil t)))
  475. ;; Handle non-empty blank line at the end of buffer.
  476. (should
  477. (org-test-with-temp-text "#+BEGIN_COMMENT\nC\n#+END_COMMENT\n "
  478. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  479. ;;;; Diary Sexp
  480. (ert-deftest test-org-element/diary-sexp-parser ()
  481. "Test `diary-sexp' parser."
  482. ;; Standard test.
  483. (should
  484. (eq 'diary-sexp
  485. (org-test-with-temp-text
  486. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old"
  487. (org-element-type (org-element-at-point)))))
  488. ;; Diary sexp must live at beginning of line
  489. (should-not
  490. (eq 'diary-sexp
  491. (org-test-with-temp-text " %%(org-bbdb-anniversaries)"
  492. (org-element-type (org-element-at-point)))))
  493. ;; Handle non-empty blank line at the end of buffer.
  494. (should
  495. (org-test-with-temp-text "%%(org-bbdb-anniversaries)\n "
  496. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  497. ;;;; Drawer
  498. (ert-deftest test-org-element/drawer-parser ()
  499. "Test `drawer' parser."
  500. ;; Standard test.
  501. (should
  502. (org-test-with-temp-text ":TEST:\nText\n:END:"
  503. (org-element-map (org-element-parse-buffer) 'drawer 'identity)))
  504. ;; Do not mix regular drawers and property drawers.
  505. (should-not
  506. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:"
  507. (org-element-map (org-element-parse-buffer) 'drawer 'identity nil t)))
  508. ;; Ignore incomplete drawer.
  509. (should-not
  510. (org-test-with-temp-text ":TEST:"
  511. (org-element-map (org-element-parse-buffer) 'drawer 'identity nil t)))
  512. ;; Handle non-empty blank line at the end of buffer.
  513. (should
  514. (org-test-with-temp-text ":TEST:\nC\n:END:\n "
  515. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  516. ;;;; Dynamic Block
  517. (ert-deftest test-org-element/dynamic-block-parser ()
  518. "Test `dynamic-block' parser."
  519. ;; Standard test.
  520. (should
  521. (org-test-with-temp-text
  522. "#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
  523. (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
  524. ;; Ignore case.
  525. (should
  526. (org-test-with-temp-text
  527. "#+begin: myblock :param1 val1 :param2 val2\nText\n#+end:"
  528. (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
  529. ;; Ignore incomplete block.
  530. (should-not
  531. (org-test-with-temp-text "#+BEGIN: myblock :param1 val1 :param2 val2"
  532. (org-element-map (org-element-parse-buffer) 'dynamic-block
  533. 'identity nil t)))
  534. ;; Handle non-empty blank line at the end of buffer.
  535. (should
  536. (org-test-with-temp-text "#+BEGIN: myblock :param val1\nC\n#+END:\n "
  537. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  538. ;;;; Entity
  539. (ert-deftest test-org-element/entity-parser ()
  540. "Test `entity' parser."
  541. ;; Without brackets.
  542. (should
  543. (org-test-with-temp-text "\\sin"
  544. (org-element-map (org-element-parse-buffer) 'entity 'identity)))
  545. ;; With brackets.
  546. (should
  547. (org-element-property
  548. :use-brackets-p
  549. (org-test-with-temp-text "\\alpha{}text"
  550. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
  551. ;; User-defined entity.
  552. (should
  553. (equal
  554. (org-element-property
  555. :name
  556. (let ((org-entities-user
  557. '(("test" "test" nil "test" "test" "test" "test"))))
  558. (org-test-with-temp-text "\\test"
  559. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
  560. "test"))
  561. ;; Special case: entity at the end of a container.
  562. (should
  563. (eq 'entity
  564. (org-test-with-temp-text "*\\alpha \\beta*"
  565. (search-forward "be")
  566. (org-element-type (org-element-context))))))
  567. ;;;; Example Block
  568. (ert-deftest test-org-element/example-block-parser ()
  569. "Test `example-block' parser."
  570. ;; Standard test.
  571. (should
  572. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
  573. (org-element-map (org-element-parse-buffer) 'example-block 'identity)))
  574. ;; Ignore incomplete block.
  575. (should-not
  576. (eq 'example-block
  577. (org-test-with-temp-text "#+BEGIN_EXAMPLE"
  578. (org-element-type (org-element-at-point)))))
  579. ;; Properly un-escape code.
  580. (should
  581. (equal "* Headline\n #+keyword\nText\n"
  582. (org-test-with-temp-text
  583. "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText\n#+END_EXAMPLE"
  584. (org-element-property :value (org-element-at-point)))))
  585. ;; Remove block indentation according to block boundaries, unless
  586. ;; block contents are less indented than block boundaries.
  587. (should
  588. (equal " L1\nL2\n"
  589. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n L1\n L2\n #+END_EXAMPLE"
  590. (let ((org-src-preserve-indentation t))
  591. (org-element-property :value (org-element-at-point))))))
  592. (should
  593. (equal
  594. " L1\n L2\n"
  595. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n L1\n L2\n #+END_EXAMPLE"
  596. (let ((org-src-preserve-indentation t))
  597. (org-element-property :value (org-element-at-point))))))
  598. ;; Handle non-empty blank line at the end of buffer.
  599. (should
  600. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nC\n#+END_EXAMPLE\n "
  601. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  602. (ert-deftest test-org-element/block-switches ()
  603. "Test `example-block' and `src-block' switches parsing."
  604. (let ((org-coderef-label-format "(ref:%s)"))
  605. ;; 1. Test "-i" switch.
  606. (should-not
  607. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  608. (org-element-property :preserve-indent (org-element-at-point))))
  609. (should
  610. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  611. (org-element-property :preserve-indent (org-element-at-point))))
  612. (should-not
  613. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  614. (org-element-property :preserve-indent (org-element-at-point))))
  615. (should
  616. (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
  617. (org-element-property :preserve-indent (org-element-at-point))))
  618. ;; 2. "-n -r -k" combination should number lines, retain labels but
  619. ;; not use them in coderefs.
  620. (should
  621. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
  622. (let ((element (org-element-at-point)))
  623. (and (org-element-property :number-lines element)
  624. (org-element-property :retain-labels element)
  625. (not (org-element-property :use-labels element))))))
  626. (should
  627. (org-test-with-temp-text
  628. "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
  629. (let ((element (org-element-at-point)))
  630. (and (org-element-property :number-lines element)
  631. (org-element-property :retain-labels element)
  632. (not (org-element-property :use-labels element))))))
  633. ;; 3. "-n -r" combination should number-lines remove labels and not
  634. ;; use them in coderefs.
  635. (should
  636. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
  637. (let ((element (org-element-at-point)))
  638. (and (org-element-property :number-lines element)
  639. (not (org-element-property :retain-labels element))
  640. (not (org-element-property :use-labels element))))))
  641. (should
  642. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
  643. (let ((element (org-element-at-point)))
  644. (and (org-element-property :number-lines element)
  645. (not (org-element-property :retain-labels element))
  646. (not (org-element-property :use-labels element))))))
  647. ;; 4. "-n" or "+n" should number lines, retain labels and use them
  648. ;; in coderefs.
  649. (should
  650. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
  651. (let ((element (org-element-at-point)))
  652. (and (org-element-property :number-lines element)
  653. (org-element-property :retain-labels element)
  654. (org-element-property :use-labels element)))))
  655. (should
  656. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
  657. (let ((element (org-element-at-point)))
  658. (and (org-element-property :number-lines element)
  659. (org-element-property :retain-labels element)
  660. (org-element-property :use-labels element)))))
  661. (should
  662. (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
  663. (let ((element (org-element-at-point)))
  664. (and (org-element-property :number-lines element)
  665. (org-element-property :retain-labels element)
  666. (org-element-property :use-labels element)))))
  667. (should
  668. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
  669. (let ((element (org-element-at-point)))
  670. (and (org-element-property :number-lines element)
  671. (org-element-property :retain-labels element)
  672. (org-element-property :use-labels element)))))
  673. ;; 5. No switch should not number lines, but retain labels and use
  674. ;; them in coderefs.
  675. (should
  676. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  677. (let ((element (org-element-at-point)))
  678. (and (not (org-element-property :number-lines element))
  679. (org-element-property :retain-labels element)
  680. (org-element-property :use-labels element)))))
  681. (should
  682. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  683. (let ((element (org-element-at-point)))
  684. (and (not (org-element-property :number-lines element))
  685. (org-element-property :retain-labels element)
  686. (org-element-property :use-labels element)))))
  687. ;; 6. "-r" switch only: do not number lines, remove labels, and
  688. ;; don't use labels in coderefs.
  689. (should
  690. (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
  691. (let ((element (org-element-at-point)))
  692. (and (not (org-element-property :number-lines element))
  693. (not (org-element-property :retain-labels element))
  694. (not (org-element-property :use-labels element))))))
  695. (should
  696. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
  697. (let ((element (org-element-at-point)))
  698. (and (not (org-element-property :number-lines element))
  699. (not (org-element-property :retain-labels element))
  700. (not (org-element-property :use-labels element))))))
  701. ;; 7. Recognize coderefs with user-defined syntax.
  702. (should
  703. (equal
  704. "[ref:%s]"
  705. (org-test-with-temp-text
  706. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
  707. (org-element-property :label-fmt (org-element-at-point)))))
  708. (should
  709. (equal
  710. "[ref:%s]"
  711. (org-test-with-temp-text
  712. "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
  713. (org-element-property :label-fmt (org-element-at-point)))))))
  714. ;;;; Export Block
  715. (ert-deftest test-org-element/export-block-parser ()
  716. "Test `export-block' parser."
  717. ;; Standard test.
  718. (should
  719. (org-test-with-temp-text "#+BEGIN_LATEX\nText\n#+END_LATEX"
  720. (org-element-map
  721. (let ((org-element-block-name-alist
  722. '(("LATEX" . org-element-export-block-parser))))
  723. (org-element-parse-buffer))
  724. 'export-block 'identity)))
  725. ;; Ignore case.
  726. (should
  727. (let ((org-element-block-name-alist
  728. '(("LATEX" . org-element-export-block-parser))))
  729. (org-test-with-temp-text "#+begin_latex\nText\n#+end_latex"
  730. (org-element-map (org-element-parse-buffer) 'export-block 'identity))))
  731. ;; Ignore incomplete block.
  732. (should-not
  733. (let ((org-element-block-name-alist
  734. '(("LATEX" . org-element-export-block-parser))))
  735. (org-test-with-temp-text "#+BEGIN_LATEX"
  736. (org-element-map (org-element-parse-buffer) 'export-block
  737. 'identity nil t))))
  738. ;; Handle non-empty blank line at the end of buffer.
  739. (should
  740. (let ((org-element-block-name-alist
  741. '(("LATEX" . org-element-export-block-parser))))
  742. (org-test-with-temp-text "#+BEGIN_LATEX\nC\n#+END_LATEX\n "
  743. (= (org-element-property :end (org-element-at-point)) (point-max))))))
  744. ;;;; Export Snippet
  745. (ert-deftest test-org-element/export-snippet-parser ()
  746. "Test `export-snippet' parser."
  747. (should
  748. (equal
  749. '("back-end" . "contents")
  750. (org-test-with-temp-text "@@back-end:contents@@"
  751. (org-element-map
  752. (org-element-parse-buffer) 'export-snippet
  753. (lambda (snippet) (cons (org-element-property :back-end snippet)
  754. (org-element-property :value snippet)))
  755. nil t)))))
  756. ;;;; Fixed Width
  757. (ert-deftest test-org-element/fixed-width-parser ()
  758. "Test fixed-width area parsing."
  759. ;; Preserve indentation.
  760. (should
  761. (org-test-with-temp-text ": no blank\n: one blank"
  762. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
  763. ;; Fixed-width with empty lines.
  764. (should
  765. (org-test-with-temp-text ": first part\n:\n: \n: second part"
  766. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
  767. ;; Parse indented fixed-width markers.
  768. (should
  769. (org-test-with-temp-text "Text\n : no blank\n : one blank"
  770. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
  771. ;; Distinguish fixed-width areas within a list and outside of it.
  772. (should
  773. (= 2
  774. (length
  775. (org-test-with-temp-text "
  776. - Item
  777. : fixed-width inside
  778. : fixed-width outside"
  779. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))))
  780. ;; Handle non-empty blank line at the end of buffer.
  781. (should
  782. (org-test-with-temp-text ": A\n "
  783. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  784. ;;;; Footnote Definition
  785. (ert-deftest test-org-element/footnote-definition-parser ()
  786. "Test `footnote-definition' parser."
  787. (should
  788. (org-test-with-temp-text "[fn:1] Definition"
  789. (org-element-map (org-element-parse-buffer) 'footnote-definition
  790. 'identity nil t)))
  791. ;; Footnote with more contents
  792. (should
  793. (= 29
  794. (org-element-property
  795. :end
  796. (org-test-with-temp-text "[fn:1] Definition\n\n| a | b |"
  797. (org-element-map (org-element-parse-buffer) 'footnote-definition
  798. 'identity nil t)))))
  799. ;; Footnote starting with special syntax.
  800. (should-not
  801. (org-test-with-temp-text "[fn:1] - no item"
  802. (org-element-map (org-element-parse-buffer) 'item 'identity)))
  803. ;; Correctly handle footnote starting with an empty line.
  804. (should
  805. (= 9
  806. (org-test-with-temp-text "[fn:1]\n\n Body"
  807. (org-element-property :contents-begin (org-element-at-point)))))
  808. ;; Handle non-empty blank line at the end of buffer.
  809. (should
  810. (org-test-with-temp-text "[fn:1] Definition\n "
  811. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  812. ;;;; Footnotes Reference.
  813. (ert-deftest test-org-element/footnote-reference-parser ()
  814. "Test `footnote-reference' parser."
  815. ;; 1. Parse a standard reference.
  816. (org-test-with-temp-text "Text[fn:label]"
  817. (should
  818. (org-element-map
  819. (org-element-parse-buffer) 'footnote-reference 'identity)))
  820. ;; 2. Parse a normalized reference.
  821. (org-test-with-temp-text "Text[1]"
  822. (should
  823. (org-element-map
  824. (org-element-parse-buffer) 'footnote-reference 'identity)))
  825. ;; 3. Parse an inline reference.
  826. (org-test-with-temp-text "Text[fn:test:def]"
  827. (should
  828. (org-element-map
  829. (org-element-parse-buffer) 'footnote-reference 'identity)))
  830. ;; 4. Parse an anonymous reference.
  831. (org-test-with-temp-text "Text[fn::def]"
  832. (should
  833. (org-element-map
  834. (org-element-parse-buffer) 'footnote-reference 'identity)))
  835. ;; 5. Parse nested footnotes.
  836. (org-test-with-temp-text "Text[fn::def [fn:label]]"
  837. (should
  838. (= 2
  839. (length
  840. (org-element-map
  841. (org-element-parse-buffer) 'footnote-reference 'identity)))))
  842. ;; 6. Parse adjacent footnotes.
  843. (org-test-with-temp-text "Text[fn:label1][fn:label2]"
  844. (should
  845. (= 2
  846. (length
  847. (org-element-map
  848. (org-element-parse-buffer) 'footnote-reference 'identity)))))
  849. ;; 7. Only properly closed footnotes are recognized as such.
  850. (org-test-with-temp-text "Text[fn:label"
  851. (should-not
  852. (org-element-map
  853. (org-element-parse-buffer) 'footnote-reference 'identity))))
  854. ;;;; Headline
  855. (ert-deftest test-org-element/headline-comment-keyword ()
  856. "Test COMMENT keyword recognition."
  857. ;; Reference test.
  858. (org-test-with-temp-text "* Headline"
  859. (let ((org-comment-string "COMMENT"))
  860. (should-not (org-element-property :commentedp (org-element-at-point)))))
  861. ;; Standard position.
  862. (org-test-with-temp-text "* COMMENT Headline"
  863. (let ((org-comment-string "COMMENT")
  864. (headline (org-element-at-point)))
  865. (should (org-element-property :commentedp headline))
  866. (should (equal (org-element-property :raw-value headline) "Headline"))))
  867. ;; Case sensitivity.
  868. (org-test-with-temp-text "* COMMENT Headline"
  869. (let* ((org-comment-string "Comment")
  870. (headline (org-element-at-point)))
  871. (should-not (org-element-property :commentedp headline))
  872. (should (equal (org-element-property :raw-value headline)
  873. "COMMENT Headline"))))
  874. ;; With another keyword.
  875. (org-test-with-temp-text "* TODO COMMENT Headline"
  876. (let* ((org-comment-string "COMMENT")
  877. (org-todo-keywords '((sequence "TODO" "DONE")))
  878. (headline (org-element-at-point)))
  879. (should (org-element-property :commentedp headline))
  880. (should (equal (org-element-property :raw-value headline) "Headline"))))
  881. ;; With the keyword only.
  882. (org-test-with-temp-text "* COMMENT"
  883. (let* ((org-comment-string "COMMENT")
  884. (headline (org-element-at-point)))
  885. (should (org-element-property :commentedp headline))
  886. (should (equal (org-element-property :raw-value headline) "")))))
  887. (ert-deftest test-org-element/headline-archive-tag ()
  888. "Test ARCHIVE tag recognition."
  889. ;; Reference test.
  890. (should-not
  891. (org-test-with-temp-text "* Headline"
  892. (let ((org-archive-tag "ARCHIVE"))
  893. (org-element-property :archivedp (org-element-at-point)))))
  894. ;; Single tag.
  895. (org-test-with-temp-text "* Headline :ARCHIVE:"
  896. (let ((org-archive-tag "ARCHIVE"))
  897. (let ((headline (org-element-at-point)))
  898. (should (org-element-property :archivedp headline))
  899. ;; Test tag removal.
  900. (should-not (org-element-property :tags headline)))))
  901. ;; Multiple tags.
  902. (org-test-with-temp-text "* Headline :test:ARCHIVE:"
  903. (let ((org-archive-tag "ARCHIVE"))
  904. (let ((headline (org-element-at-point)))
  905. (should (org-element-property :archivedp headline))
  906. ;; Test tag removal.
  907. (should (equal (org-element-property :tags headline) '("test"))))))
  908. ;; Tag is case-sensitive.
  909. (should-not
  910. (org-test-with-temp-text "* Headline :ARCHIVE:"
  911. (let ((org-archive-tag "Archive"))
  912. (org-element-property :archivedp (org-element-at-point))))))
  913. (ert-deftest test-org-element/headline-properties ()
  914. "Test properties from property drawer."
  915. ;; All properties from property drawer have their symbol upper
  916. ;; cased.
  917. (should
  918. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
  919. (org-element-property :FOO (org-element-at-point))))
  920. (should-not
  921. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
  922. (org-element-property :foo (org-element-at-point)))))
  923. ;;;; Horizontal Rule
  924. (ert-deftest test-org-element/horizontal-rule-parser ()
  925. "Test `horizontal-rule' parser."
  926. ;; Standard.
  927. (should
  928. (org-test-with-temp-text "-----"
  929. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  930. ;; Indented.
  931. (should
  932. (org-test-with-temp-text " -----"
  933. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  934. ;; Hyphen must be alone on the line.
  935. (should-not
  936. (org-test-with-temp-text "-----wrong"
  937. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  938. ;; 4 hyphens is too small.
  939. (should-not
  940. (org-test-with-temp-text "----"
  941. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  942. ;; Handle non-empty blank line at the end of buffer.
  943. (should
  944. (org-test-with-temp-text "-----\n "
  945. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  946. ;;;; Inline Babel Call
  947. (ert-deftest test-org-element/inline-babel-call-parser ()
  948. "Test `inline-babel-call' parser."
  949. (should
  950. (org-test-with-temp-text "call_test()"
  951. (org-element-map
  952. (org-element-parse-buffer) 'inline-babel-call 'identity))))
  953. ;;;; Inline Src Block
  954. (ert-deftest test-org-element/inline-src-block-parser ()
  955. "Test `inline-src-block' parser."
  956. (should
  957. (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
  958. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  959. ;; Test parsing at the beginning of an item.
  960. (should
  961. (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
  962. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity))))
  963. ;;;; Inlinetask
  964. (ert-deftest test-org-element/inlinetask-parser ()
  965. "Test `inlinetask' parser."
  966. (when (featurep 'org-inlinetask)
  967. (let ((org-inlinetask-min-level 15))
  968. ;; Regular inlinetask.
  969. (should
  970. (eq 'inlinetask
  971. (org-test-with-temp-text
  972. "*************** Task\nTest\n*************** END"
  973. (org-element-type (org-element-at-point)))))
  974. ;; Degenerate inlinetask.
  975. (should
  976. (eq 'inlinetask
  977. (org-test-with-temp-text "*************** Task"
  978. (org-element-type (org-element-at-point)))))
  979. ;; Mixed inlinetasks.
  980. (should-not
  981. (org-test-with-temp-text
  982. "
  983. *************** Task
  984. *************** Task2
  985. Contents
  986. *************** END"
  987. (forward-line)
  988. (goto-char (org-element-property :end (org-element-at-point)))
  989. (eobp)))
  990. ;; TODO keyword.
  991. (should
  992. (equal
  993. "TODO"
  994. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  995. (org-test-with-temp-text "*************** TODO Task"
  996. (org-element-property :todo-keyword (org-element-at-point))))))
  997. ;; Planning info.
  998. (should
  999. (org-test-with-temp-text "
  1000. *************** Task
  1001. DEADLINE: <2012-03-29 thu.>"
  1002. (forward-line)
  1003. (org-element-property :deadline (org-element-at-point))))
  1004. ;; Priority.
  1005. (should
  1006. (eq
  1007. ?A
  1008. (org-test-with-temp-text "
  1009. *************** [#A] Task"
  1010. (forward-line)
  1011. (org-element-property :priority (org-element-at-point)))))
  1012. ;; Tags.
  1013. (should
  1014. (equal
  1015. '("test")
  1016. (org-test-with-temp-text "
  1017. *************** Task :test:"
  1018. (forward-line)
  1019. (org-element-property :tags (org-element-at-point)))))
  1020. ;; Regular properties are accessed through upper case keywords.
  1021. (should
  1022. (org-test-with-temp-text "
  1023. *************** Task
  1024. :PROPERTIES:
  1025. :foo: bar
  1026. :END:
  1027. *************** END"
  1028. (forward-line)
  1029. (org-element-property :FOO (org-element-at-point))))
  1030. (should-not
  1031. (org-test-with-temp-text "
  1032. *************** Task
  1033. :PROPERTIES:
  1034. :foo: bar
  1035. :END:
  1036. *************** END"
  1037. (forward-line)
  1038. (org-element-property :foo (org-element-at-point))))
  1039. ;; Handle non-empty blank line at the end of buffer.
  1040. (should
  1041. (org-test-with-temp-text "*************** Task\n*************** END\n "
  1042. (= (org-element-property :end (org-element-at-point)) (point-max)))))))
  1043. ;;;; Italic
  1044. (ert-deftest test-org-element/italic-parser ()
  1045. "Test `italic' parser."
  1046. ;; Regular test.
  1047. (should
  1048. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1049. (org-test-with-temp-text "/italic/"
  1050. (org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
  1051. ;; Multi-line markup.
  1052. (should
  1053. (equal
  1054. (org-element-contents
  1055. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1056. (org-test-with-temp-text "/first line\nsecond line/"
  1057. (org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
  1058. '("first line\nsecond line"))))
  1059. ;;;; Item
  1060. (ert-deftest test-org-element/item-parser ()
  1061. "Test `item' parser."
  1062. ;; Standard test.
  1063. (should
  1064. (org-test-with-temp-text "- item"
  1065. (org-element-map (org-element-parse-buffer) 'item 'identity)))
  1066. ;; Counter.
  1067. (should
  1068. (= 6
  1069. (org-element-property
  1070. :counter
  1071. (org-test-with-temp-text "6. [@6] item"
  1072. (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
  1073. ;; Tag
  1074. (should
  1075. (equal
  1076. '("tag")
  1077. (org-element-property
  1078. :tag
  1079. (org-test-with-temp-text "- tag :: description"
  1080. (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
  1081. ;; No tags in ordered lists.
  1082. (should-not
  1083. (org-element-property
  1084. :tag
  1085. (org-test-with-temp-text "1. tag :: description"
  1086. (org-element-map (org-element-parse-buffer) 'item 'identity nil t))))
  1087. ;; Check-boxes
  1088. (should
  1089. (equal
  1090. '(trans on off)
  1091. (org-test-with-temp-text "
  1092. - [-] item 1
  1093. - [X] item 1.1
  1094. - [ ] item 1.2"
  1095. (org-element-map
  1096. (org-element-parse-buffer) 'item
  1097. (lambda (item) (org-element-property :checkbox item))))))
  1098. ;; Item starting with special syntax.
  1099. (should
  1100. (equal '(("- item"))
  1101. (org-test-with-temp-text "- - item"
  1102. (org-element-map (org-element-parse-buffer) 'paragraph
  1103. 'org-element-contents))))
  1104. ;; Block in an item: ignore indentation within the block.
  1105. (should
  1106. (org-test-with-temp-text "- item\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1107. (forward-char)
  1108. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1109. ;;;; Keyword
  1110. (ert-deftest test-org-element/keyword-parser ()
  1111. "Test `keyword' parser."
  1112. ;; Standard test.
  1113. (should
  1114. (org-test-with-temp-text "#+KEYWORD: value"
  1115. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1116. ;; Keywords are case-insensitive.
  1117. (should
  1118. (org-test-with-temp-text "#+keyword: value"
  1119. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1120. ;; Affiliated keywords are not keywords.
  1121. (should-not
  1122. (org-test-with-temp-text "#+NAME: value
  1123. Paragraph"
  1124. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1125. ;; Do not mix keywords with Babel calls and dynamic blocks.
  1126. (should-not
  1127. (org-test-with-temp-text "#+CALL: fun()"
  1128. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1129. (should-not
  1130. (org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
  1131. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1132. ;; Handle non-empty blank line at the end of buffer.
  1133. (should
  1134. (org-test-with-temp-text "#+KEYWORD: value\n "
  1135. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1136. ;;;; Latex Environment
  1137. (ert-deftest test-org-element/latex-environment-parser ()
  1138. "Test `latex-environment' parser."
  1139. (should
  1140. (org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
  1141. (org-element-map (org-element-parse-buffer) 'latex-environment 'identity)))
  1142. ;; Allow nested environments.
  1143. (should
  1144. (equal
  1145. "\\begin{outer}
  1146. \\begin{inner}
  1147. e^{i\\pi}+1=0
  1148. \\end{inner}
  1149. \\end{outer}"
  1150. (org-test-with-temp-text "
  1151. \\begin{outer}
  1152. \\begin{inner}
  1153. e^{i\\pi}+1=0
  1154. \\end{inner}
  1155. \\end{outer}"
  1156. (org-element-property
  1157. :value
  1158. (org-element-map
  1159. (org-element-parse-buffer) 'latex-environment 'identity nil t)))))
  1160. ;; Allow environments with options and arguments.
  1161. (should
  1162. (eq 'latex-environment
  1163. (org-test-with-temp-text
  1164. "\\begin{theorem}[Euler]\ne^{i\\pi}+1=0\n\\end{theorem}"
  1165. (org-element-type (org-element-at-point)))))
  1166. (should
  1167. (eq 'latex-environment
  1168. (org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
  1169. (org-element-type (org-element-at-point)))))
  1170. (should-not
  1171. (eq 'latex-environment
  1172. (org-test-with-temp-text "\\begin{env}{arg} something\nvalue\n\\end{env}"
  1173. (org-element-type (org-element-at-point)))))
  1174. ;; Handle non-empty blank line at the end of buffer.
  1175. (should
  1176. (org-test-with-temp-text "\\begin{env}\n\\end{env}\n "
  1177. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1178. ;;;; Latex Fragment
  1179. (ert-deftest test-org-element/latex-fragment-parser ()
  1180. "Test `latex-fragment' parser."
  1181. (should
  1182. (org-test-with-temp-text "$a$"
  1183. (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
  1184. (should
  1185. (org-test-with-temp-text "$$a$$"
  1186. (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
  1187. (should
  1188. (org-test-with-temp-text "\\(a\\)"
  1189. (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
  1190. (should
  1191. (org-test-with-temp-text "\\[a\\]"
  1192. (org-element-map
  1193. (org-element-parse-buffer) 'latex-fragment 'identity)))
  1194. ;; Test fragment at the beginning of an item.
  1195. (should
  1196. (eq 'latex-fragment
  1197. (org-test-with-temp-text "- $x$"
  1198. (progn (search-forward "$")
  1199. (org-element-type (org-element-context)))))))
  1200. ;;;; Line Break
  1201. (ert-deftest test-org-element/line-break-parser ()
  1202. "Test `line-break' parser."
  1203. ;; Regular test.
  1204. (should
  1205. (org-test-with-temp-text "Text \\\\"
  1206. (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
  1207. ;; Line break with trailing white spaces.
  1208. (should
  1209. (org-test-with-temp-text "Text \\\\ "
  1210. (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
  1211. ;; Three backslashes are too much.
  1212. (should-not
  1213. (org-test-with-temp-text "Text \\\\\\"
  1214. (org-element-map (org-element-parse-buffer) 'line-break 'identity))))
  1215. ;;;; Link
  1216. (ert-deftest test-org-element/link-parser ()
  1217. "Test `link' parser."
  1218. ;; Radio target.
  1219. (should
  1220. (equal
  1221. "radio"
  1222. (org-test-with-temp-text "A radio link"
  1223. (org-element-property
  1224. :type
  1225. (org-element-map
  1226. (let ((org-target-link-regexp "radio")) (org-element-parse-buffer))
  1227. 'link 'identity nil t)))))
  1228. ;; Standard link.
  1229. ;;
  1230. ;; ... with description.
  1231. (should
  1232. (equal
  1233. '("Orgmode.org")
  1234. (org-test-with-temp-text "[[http://orgmode.org][Orgmode.org]]"
  1235. (org-element-contents
  1236. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1237. ;; ... without description.
  1238. (should
  1239. (equal
  1240. "http"
  1241. (org-test-with-temp-text "[[http://orgmode.org]]"
  1242. (org-element-property
  1243. :type
  1244. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1245. ;; ... with expansion.
  1246. (should
  1247. (equal
  1248. "//orgmode.org/worg"
  1249. (org-test-with-temp-text "[[Org:worg]]"
  1250. (let ((org-link-abbrev-alist '(("Org" . "http://orgmode.org/"))))
  1251. (org-element-property
  1252. :path
  1253. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))))))
  1254. ;; ... with translation.
  1255. (should
  1256. (equal
  1257. "127.0.0.1"
  1258. (org-test-with-temp-text "[[http://orgmode.org]]"
  1259. (flet ((link-translate (type path) (cons type "127.0.0.1")))
  1260. (let ((org-link-translation-function 'link-translate))
  1261. (org-element-property
  1262. :path
  1263. (org-element-map (org-element-parse-buffer) 'link
  1264. 'identity nil t)))))))
  1265. ;; ... id link.
  1266. (should
  1267. (equal
  1268. "id"
  1269. (org-test-with-temp-text "[[id:aaaa]]"
  1270. (org-element-property
  1271. :type
  1272. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1273. ;; ... custom-id link.
  1274. (should
  1275. (equal
  1276. "custom-id"
  1277. (org-test-with-temp-text "[[#some-id]]"
  1278. (org-element-property
  1279. :type
  1280. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1281. ;; ... coderef link.
  1282. (should
  1283. (equal
  1284. "coderef"
  1285. (org-test-with-temp-text "[[(reference)]]"
  1286. (org-element-property
  1287. :type
  1288. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1289. ;; ... fuzzy link.
  1290. (should
  1291. (equal
  1292. "fuzzy"
  1293. (org-test-with-temp-text "[[target-or-title]]"
  1294. (org-element-property
  1295. :type
  1296. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1297. ;; ... file-type link with search option.
  1298. (should
  1299. (equal
  1300. '(("file" "projects.org" "*task title"))
  1301. (org-test-with-temp-text "[[file:projects.org::*task title]]"
  1302. (org-element-map (org-element-parse-buffer) 'link
  1303. (lambda (l) (list (org-element-property :type l)
  1304. (org-element-property :path l)
  1305. (org-element-property :search-option l)))))))
  1306. ;; ... file-type link with application.
  1307. (should
  1308. (equal
  1309. '(("file" "projects.org" "docview"))
  1310. (org-test-with-temp-text "[[docview:projects.org]]"
  1311. (org-element-map (org-element-parse-buffer) 'link
  1312. (lambda (l) (list (org-element-property :type l)
  1313. (org-element-property :path l)
  1314. (org-element-property :application l)))))))
  1315. ;; Plain link.
  1316. (should
  1317. (org-test-with-temp-text "A link: http://orgmode.org"
  1318. (org-element-map (org-element-parse-buffer) 'link 'identity)))
  1319. ;; Angular link.
  1320. (should
  1321. (org-test-with-temp-text "A link: <http://orgmode.org>"
  1322. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))
  1323. ;; Link abbreviation.
  1324. (should
  1325. (equal "http"
  1326. (org-test-with-temp-text
  1327. "#+LINK: orgmode http://www.orgmode.org/\n[[orgmode:#docs]]"
  1328. (progn (org-mode-restart)
  1329. (goto-char (1- (point-max)))
  1330. (org-element-property :type (org-element-context))))))
  1331. ;; Link abbreviation in a secondary string.
  1332. (should
  1333. (equal "http"
  1334. (org-test-with-temp-text
  1335. "#+LINK: orgmode http://www.orgmode.org/\n* H [[orgmode:#docs]]"
  1336. (progn (org-mode-restart)
  1337. (org-element-map (org-element-parse-buffer) 'link
  1338. (lambda (link) (org-element-property :type link))
  1339. nil t nil t)))))
  1340. ;; Plain links are allowed as description of regular links.
  1341. (should
  1342. (equal "file"
  1343. (org-test-with-temp-text "[[http://orgmode.org][file:unicorn.jpg]]"
  1344. (search-forward "file:")
  1345. (org-element-property :type (org-element-context))))))
  1346. ;;;; Macro
  1347. (ert-deftest test-org-element/macro-parser ()
  1348. "Test `macro' parser."
  1349. ;; Without arguments.
  1350. (should
  1351. (org-test-with-temp-text "{{{macro}}}"
  1352. (org-element-map (org-element-parse-buffer) 'macro 'identity)))
  1353. ;; With arguments.
  1354. (should
  1355. (org-test-with-temp-text "{{{macro(arg1,arg2)}}}"
  1356. (org-element-map (org-element-parse-buffer) 'macro 'identity)))
  1357. ;; Properly handle protected commas in arguments...
  1358. (should
  1359. (= 2
  1360. (length
  1361. (org-test-with-temp-text "{{{macro(arg1\\,arg1,arg2)}}}"
  1362. (org-element-property :args (org-element-context))))))
  1363. ;; ... even when last argument ends with a protected comma.
  1364. (should
  1365. (equal '("C-,")
  1366. (org-test-with-temp-text "{{{macro(C-\\,)}}}"
  1367. (org-element-property :args (org-element-context)))))
  1368. ;; Allow to escape escaping character.
  1369. (should
  1370. (equal '("C-\\" "")
  1371. (org-test-with-temp-text "{{{macro(C-\\\\,)}}}"
  1372. (org-element-property :args (org-element-context)))))
  1373. ;; No need to escape backslashes elsewhere.
  1374. (should
  1375. (equal '("\\")
  1376. (org-test-with-temp-text "{{{macro(\\)}}}"
  1377. (org-element-property :args (org-element-context))))))
  1378. ;;;; Node Property
  1379. (ert-deftest test-org-element/node-property ()
  1380. "Test `node-property' parser."
  1381. ;; Standard test.
  1382. (should
  1383. (equal '("abc" "value")
  1384. (org-test-with-temp-text ":PROPERTIES:\n:abc: value\n:END:"
  1385. (progn (forward-line)
  1386. (let ((element (org-element-at-point)))
  1387. (list (org-element-property :key element)
  1388. (org-element-property :value element)))))))
  1389. ;; Value should be trimmed.
  1390. (should
  1391. (equal "value"
  1392. (org-test-with-temp-text ":PROPERTIES:\n:abc: value \n:END:"
  1393. (progn (forward-line)
  1394. (let ((element (org-element-at-point)))
  1395. (org-element-property :value element))))))
  1396. ;; A node property requires to be wrapped within a property drawer.
  1397. (should-not
  1398. (eq 'node-property
  1399. (org-test-with-temp-text ":abc: value"
  1400. (org-element-type (org-element-at-point))))))
  1401. ;;;; Paragraph
  1402. (ert-deftest test-org-element/paragraph-parser ()
  1403. "Test `paragraph' parser."
  1404. ;; Standard test.
  1405. (should
  1406. (org-test-with-temp-text "Paragraph"
  1407. (org-element-map (org-element-parse-buffer) 'paragraph 'identity nil t)))
  1408. ;; Property find end of a paragraph stuck to another element.
  1409. (should
  1410. (eq ?#
  1411. (org-test-with-temp-text "Paragraph\n# Comment"
  1412. (org-element-map (org-element-parse-buffer) 'paragraph
  1413. (lambda (p) (char-after (org-element-property :end p)))
  1414. nil t))))
  1415. ;; Include ill-formed Keywords.
  1416. (should
  1417. (org-test-with-temp-text "#+wrong_keyword something"
  1418. (org-element-map (org-element-parse-buffer) 'paragraph 'identity)))
  1419. ;; Include incomplete-drawers.
  1420. (should
  1421. (org-test-with-temp-text ":TEST:\nParagraph"
  1422. (let ((elem (org-element-at-point)))
  1423. (and (eq (org-element-type elem) 'paragraph)
  1424. (= (point-max) (org-element-property :end elem))))))
  1425. ;; Include incomplete blocks.
  1426. (should
  1427. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph"
  1428. (let ((elem (org-element-at-point)))
  1429. (and (eq (org-element-type elem) 'paragraph)
  1430. (= (point-max) (org-element-property :end elem))))))
  1431. ;; Include incomplete dynamic blocks.
  1432. (should
  1433. (org-test-with-temp-text "#+BEGIN: \nParagraph"
  1434. (let ((elem (org-element-at-point)))
  1435. (and (eq (org-element-type elem) 'paragraph)
  1436. (= (point-max) (org-element-property :end elem))))))
  1437. ;; Include incomplete latex environments.
  1438. (should
  1439. (org-test-with-temp-text "\begin{equation}\nParagraph"
  1440. (let ((elem (org-element-at-point)))
  1441. (and (eq (org-element-type elem) 'paragraph)
  1442. (= (point-max) (org-element-property :end elem))))))
  1443. ;; Do not steal affiliated keywords from container.
  1444. (should
  1445. (org-test-with-temp-text "#+ATTR_LATEX: test\n- item 1"
  1446. (let ((elem (progn (search-forward "item") (org-element-at-point))))
  1447. (and (eq (org-element-type elem) 'paragraph)
  1448. (not (org-element-property :attr_latex elem))
  1449. (/= (org-element-property :begin elem) 1)))))
  1450. ;; Handle non-empty blank line at the end of buffer.
  1451. (should
  1452. (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
  1453. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1454. ;;;; Plain List
  1455. (ert-deftest test-org-element/plain-list-parser ()
  1456. "Test `plain-list' parser."
  1457. (org-test-with-temp-text "- item"
  1458. (should (org-element-map (org-element-parse-buffer) 'plain-list 'identity)))
  1459. ;; Blank lines after the list only belong to outer plain list.
  1460. (should
  1461. (equal
  1462. '(t t)
  1463. (org-test-with-temp-text "
  1464. - outer
  1465. - inner
  1466. Outside list"
  1467. (let ((endings (org-element-map (org-element-parse-buffer) 'plain-list
  1468. (lambda (pl) (org-element-property :end pl)))))
  1469. (list
  1470. ;; Move to ending of outer list.
  1471. (progn (goto-char (car endings)) (looking-at "Outside list"))
  1472. ;; Move to ending of inner list.
  1473. (progn (goto-char (nth 1 endings)) (looking-at "^$")))))))
  1474. ;; Correctly compute end of list if it doesn't end at a line
  1475. ;; beginning.
  1476. (should
  1477. (org-test-with-temp-text "- list\n \n "
  1478. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1479. ;;;; Planning
  1480. (ert-deftest test-org-element/planning-parser ()
  1481. "Test `planning' parser."
  1482. (should
  1483. (equal "[2012-03-29 thu.]"
  1484. (org-element-property
  1485. :raw-value
  1486. (org-element-property
  1487. :closed
  1488. (org-test-with-temp-text "CLOSED: [2012-03-29 thu.]"
  1489. (org-element-at-point))))))
  1490. (should
  1491. (equal "<2012-03-29 thu.>"
  1492. (org-element-property
  1493. :raw-value
  1494. (org-element-property
  1495. :deadline
  1496. (org-test-with-temp-text "DEADLINE: <2012-03-29 thu.>"
  1497. (org-element-at-point))))))
  1498. (should
  1499. (equal "<2012-03-29 thu.>"
  1500. (org-element-property
  1501. :raw-value
  1502. (org-element-property
  1503. :scheduled
  1504. (org-test-with-temp-text "SCHEDULED: <2012-03-29 thu.>"
  1505. (org-element-at-point)))))))
  1506. ;;;; Property Drawer
  1507. (ert-deftest test-org-element/property-drawer-parser ()
  1508. "Test `property-drawer' parser."
  1509. ;; Standard test.
  1510. (should
  1511. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:"
  1512. (org-element-map
  1513. (org-element-parse-buffer) 'property-drawer 'identity nil t)))
  1514. ;; Do not mix property drawers and regular drawers.
  1515. (should-not
  1516. (org-test-with-temp-text ":TEST:\n:prop: value\n:END:"
  1517. (org-element-map
  1518. (org-element-parse-buffer) 'property-drawer 'identity nil t)))
  1519. ;; Ignore incomplete drawer.
  1520. (should-not
  1521. (org-test-with-temp-text ":PROPERTIES:\n:prop: value"
  1522. (org-element-map
  1523. (org-element-parse-buffer) 'property-drawer 'identity nil t)))
  1524. ;; Handle non-empty blank line at the end of buffer.
  1525. (should
  1526. (org-test-with-temp-text ":PROPERTIES:\n:END:\n "
  1527. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1528. ;;;; Quote Block
  1529. (ert-deftest test-org-element/quote-block-parser ()
  1530. "Test `quote-block' parser."
  1531. ;; Regular test.
  1532. (should
  1533. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  1534. (org-element-map (org-element-parse-buffer) 'quote-block 'identity)))
  1535. ;; Ignore incomplete block.
  1536. (should-not
  1537. (org-test-with-temp-text "#+BEGIN_QUOTE"
  1538. (org-element-map (org-element-parse-buffer) 'quote-block 'identity nil t)))
  1539. ;; Handle non-empty blank line at the end of buffer.
  1540. (should
  1541. (org-test-with-temp-text "#+BEGIN_QUOTE\nC\n#+END_QUOTE\n "
  1542. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1543. ;;;; Radio Target
  1544. (ert-deftest test-org-element/radio-target-parser ()
  1545. "Test `radio-target' parser."
  1546. ;; Standard test.
  1547. (should
  1548. (org-test-with-temp-text "<<<radio>>>"
  1549. (org-element-map (org-element-parse-buffer) 'radio-target 'identity)))
  1550. ;; Radio targets with objects.
  1551. (should
  1552. (org-test-with-temp-text "<<<radio \\alpha>>>"
  1553. (org-element-map (org-element-parse-buffer) 'radio-target 'identity))))
  1554. ;;;; Section
  1555. (ert-deftest test-org-element/section-parser ()
  1556. "Test `section' parser."
  1557. ;; Standard test.
  1558. (should
  1559. (org-test-with-temp-text "* Headline\nText"
  1560. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  1561. ;; There's a section before the first headline.
  1562. (should
  1563. (org-test-with-temp-text "Text"
  1564. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  1565. ;; A section cannot be empty.
  1566. (should-not
  1567. (org-test-with-temp-text "* Headline 1\n* Headline 2"
  1568. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  1569. ;; A section doesn't contain sub-trees.
  1570. (should-not
  1571. (org-test-with-temp-text "* Head\nText\n** Sub-Head"
  1572. (org-element-map
  1573. (org-element-map (org-element-parse-buffer) 'section 'identity nil t)
  1574. 'headline 'identity))))
  1575. ;;;; Special Block
  1576. (ert-deftest test-org-element/special-block-parser ()
  1577. "Test `special-block' parser."
  1578. ;; Standard test.
  1579. (should
  1580. (equal "SPECIAL"
  1581. (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
  1582. (org-element-property :type (org-element-at-point)))))
  1583. ;; Special blocks can contain paragraphs.
  1584. (should
  1585. (eq 'paragraph
  1586. (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
  1587. (forward-line)
  1588. (org-element-type (org-element-at-point)))))
  1589. ;; Ignore incomplete block.
  1590. (should-not
  1591. (eq 'special-block
  1592. (org-test-with-temp-text "#+BEGIN_SPECIAL"
  1593. (org-element-type (org-element-at-point)))))
  1594. ;; Allow special characters in type.
  1595. (should
  1596. (equal '(special-block "SPECIAL*")
  1597. (org-test-with-temp-text "#+BEGIN_SPECIAL*\nContents\n#+END_SPECIAL*"
  1598. (let ((element (org-element-at-point)))
  1599. (list (org-element-type element)
  1600. (org-element-property :type element))))))
  1601. ;; Handle non-empty blank line at the end of buffer.
  1602. (should
  1603. (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n "
  1604. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1605. ;;;; Src Block
  1606. (ert-deftest test-org-element/src-block-parser ()
  1607. "Test `src-block' parser."
  1608. ;; Regular tests.
  1609. (should
  1610. (org-test-with-temp-text "#+BEGIN_SRC org\nText\n#+END_SRC"
  1611. (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
  1612. ;; Ignore incomplete block.
  1613. (should-not
  1614. (org-test-with-temp-text "#+BEGIN_SRC"
  1615. (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
  1616. ;; Properly un-escape code.
  1617. (should
  1618. (equal "* Headline\n #+keyword\nText\n"
  1619. (org-test-with-temp-text
  1620. "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
  1621. (org-element-property :value (org-element-at-point)))))
  1622. ;; Remove block indentation according to block boundaries, unless
  1623. ;; block contents are less indented than block boundaries.
  1624. (should
  1625. (equal " L1\nL2\n"
  1626. (org-test-with-temp-text " #+BEGIN_SRC org\n L1\n L2\n #+END_SRC"
  1627. (let ((org-src-preserve-indentation t))
  1628. (org-element-property :value (org-element-at-point))))))
  1629. (should
  1630. (equal
  1631. " L1\n L2\n"
  1632. (org-test-with-temp-text " #+BEGIN_SRC org\n L1\n L2\n #+END_SRC"
  1633. (let ((org-src-preserve-indentation t))
  1634. (org-element-property :value (org-element-at-point))))))
  1635. ;; Handle non-empty blank line at the end of buffer.
  1636. (should
  1637. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\nC\n#+END_SRC\n "
  1638. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1639. ;;;; Statistics Cookie
  1640. (ert-deftest test-org-element/statistics-cookie ()
  1641. "Test `statistics-cookie' parser."
  1642. ;; With numbers.
  1643. (should
  1644. (org-test-with-temp-text "[1/2]"
  1645. (org-element-map (org-element-parse-buffer) 'statistics-cookie 'identity)))
  1646. ;; With percents.
  1647. (should
  1648. (org-test-with-temp-text "[33%]"
  1649. (org-element-map
  1650. (org-element-parse-buffer) 'statistics-cookie 'identity))))
  1651. ;;;; Strike Through
  1652. (ert-deftest test-org-element/strike-through-parser ()
  1653. "Test `strike-through' parser."
  1654. ;; Regular test.
  1655. (should
  1656. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1657. (org-test-with-temp-text "+strike-through+"
  1658. (org-element-map (org-element-parse-buffer) 'strike-through 'identity))))
  1659. ;; Multi-line markup.
  1660. (should
  1661. (equal
  1662. (org-element-contents
  1663. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1664. (org-test-with-temp-text "+first line\nsecond line+"
  1665. (org-element-map
  1666. (org-element-parse-buffer) 'strike-through 'identity nil t))))
  1667. '("first line\nsecond line"))))
  1668. ;;;; Subscript
  1669. (ert-deftest test-org-element/subscript-parser ()
  1670. "Test `subscript' parser."
  1671. ;; Without braces.
  1672. (should
  1673. (org-test-with-temp-text "a_b"
  1674. (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
  1675. ;; With braces.
  1676. (should
  1677. (org-test-with-temp-text "a_{b}"
  1678. (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
  1679. ;; Multiple subscripts in a paragraph.
  1680. (should
  1681. (= 2
  1682. (org-test-with-temp-text "a_b and c_d"
  1683. (length
  1684. (org-element-map (org-element-parse-buffer) 'subscript 'identity))))))
  1685. ;;;; Superscript
  1686. (ert-deftest test-org-element/superscript-parser ()
  1687. "Test `superscript' parser."
  1688. ;; Without braces.
  1689. (should
  1690. (org-test-with-temp-text "a^b"
  1691. (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
  1692. ;; With braces.
  1693. (should
  1694. (org-test-with-temp-text "a^{b}"
  1695. (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
  1696. ;; Multiple superscript in a paragraph.
  1697. (should
  1698. (= 2
  1699. (org-test-with-temp-text "a^b and c^d"
  1700. (length
  1701. (org-element-map
  1702. (org-element-parse-buffer) 'superscript 'identity))))))
  1703. ;;;; Table
  1704. (ert-deftest test-org-element/table-parser ()
  1705. "Test `table' parser."
  1706. (should
  1707. (org-test-with-temp-text "| a |"
  1708. (org-element-map (org-element-parse-buffer) 'table 'identity)))
  1709. ;; TBLFM keyword is case insensitive.
  1710. (should
  1711. (org-test-with-temp-text "| a |\n#+tblfm: test"
  1712. (org-element-property
  1713. :tblfm
  1714. (org-element-map (org-element-parse-buffer) 'table 'identity nil t))))
  1715. ;; Handle multiple TBLFM lines.
  1716. (should
  1717. (= 2
  1718. (org-test-with-temp-text "| a |\n#+TBLFM: test1\n#+TBLFM: test2"
  1719. (length (org-element-property
  1720. :tblfm
  1721. (org-element-map
  1722. (org-element-parse-buffer) 'table 'identity nil t))))))
  1723. ;; Handle non-empty blank line at the end of buffer.
  1724. (should
  1725. (org-test-with-temp-text "| a |\n "
  1726. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1727. ;;;; Table Cell
  1728. (ert-deftest test-org-element/table-cell-parser ()
  1729. "Test `table-cell' parser."
  1730. (should
  1731. (org-test-with-temp-text "| a |"
  1732. (org-element-map (org-element-parse-buffer) 'table-cell 'identity))))
  1733. ;;;; Table Row
  1734. (ert-deftest test-org-element/table-row-parser ()
  1735. "Test `table-row' parser."
  1736. (should
  1737. (equal '(standard rule)
  1738. (org-test-with-temp-text "| a |\n|---|"
  1739. (org-element-map
  1740. (org-element-parse-buffer) 'table-row
  1741. (lambda (row) (org-element-property :type row)))))))
  1742. ;;;; Target
  1743. (ert-deftest test-org-element/target-parser ()
  1744. "Test `target' parser."
  1745. (should
  1746. (org-test-with-temp-text "<<target>>"
  1747. (org-element-map (org-element-parse-buffer) 'target 'identity))))
  1748. ;;;; Timestamp
  1749. (ert-deftest test-org-element/timestamp ()
  1750. "Test `timestamp' parser."
  1751. ;; Active timestamp.
  1752. (should
  1753. (org-test-with-temp-text "<2012-03-29 16:40>"
  1754. (eq (org-element-property :type (org-element-context)) 'active)))
  1755. (should-not
  1756. (org-test-with-temp-text "<2012-03-29 Thu>"
  1757. (let ((timestamp (org-element-context)))
  1758. (or (org-element-property :hour-start timestamp)
  1759. (org-element-property :minute-start timestamp)))))
  1760. (should
  1761. (equal '(2012 3 29 16 40)
  1762. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1763. (let ((object (org-element-context)))
  1764. (list (org-element-property :year-start object)
  1765. (org-element-property :month-start object)
  1766. (org-element-property :day-start object)
  1767. (org-element-property :hour-start object)
  1768. (org-element-property :minute-start object))))))
  1769. ;; Inactive timestamp.
  1770. (should
  1771. (org-test-with-temp-text "[2012-03-29 Thu 16:40]"
  1772. (eq (org-element-property :type (org-element-context)) 'inactive)))
  1773. ;; Time range.
  1774. (should
  1775. (equal '(2012 3 29 16 40 7 30)
  1776. (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
  1777. (let ((object (org-element-context)))
  1778. (list (org-element-property :year-end object)
  1779. (org-element-property :month-end object)
  1780. (org-element-property :day-end object)
  1781. (org-element-property :hour-end object)
  1782. (org-element-property :minute-end object)
  1783. (org-element-property :hour-start object)
  1784. (org-element-property :minute-start object))))))
  1785. (should
  1786. (eq 'active-range
  1787. (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
  1788. (org-element-property :type (org-element-context)))))
  1789. ;; Date range.
  1790. (should
  1791. (org-test-with-temp-text "[2012-03-29 Thu 16:40]--[2012-03-29 Thu 16:41]"
  1792. (eq (org-element-property :type (org-element-context)) 'inactive-range)))
  1793. (should-not
  1794. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1795. (let ((timestamp (org-element-context)))
  1796. (or (org-element-property :hour-end timestamp)
  1797. (org-element-property :minute-end timestamp)))))
  1798. ;; With repeater, warning delay and both.
  1799. (should
  1800. (eq 'catch-up
  1801. (org-test-with-temp-text "<2012-03-29 Thu ++1y>"
  1802. (org-element-property :repeater-type (org-element-context)))))
  1803. (should
  1804. (eq 'first
  1805. (org-test-with-temp-text "<2012-03-29 Thu --1y>"
  1806. (org-element-property :warning-type (org-element-context)))))
  1807. (should
  1808. (equal '(cumulate all)
  1809. (org-test-with-temp-text "<2012-03-29 Thu +1y -1y>"
  1810. (let ((ts (org-element-context)))
  1811. (list (org-element-property :repeater-type ts)
  1812. (org-element-property :warning-type ts))))))
  1813. ;; Timestamps are not planning elements.
  1814. (should-not
  1815. (org-test-with-temp-text "SCHEDULED: <2012-03-29 Thu 16:40>"
  1816. (org-element-map (org-element-parse-buffer) 'timestamp 'identity))))
  1817. ;;;; Underline
  1818. (ert-deftest test-org-element/underline-parser ()
  1819. "Test `underline' parser."
  1820. ;; Regular test.
  1821. (should
  1822. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1823. (org-test-with-temp-text "_underline_"
  1824. (org-element-map (org-element-parse-buffer) 'underline 'identity))))
  1825. ;; Multi-line markup.
  1826. (should
  1827. (equal
  1828. (org-element-contents
  1829. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1830. (org-test-with-temp-text "_first line\nsecond line_"
  1831. (org-element-map
  1832. (org-element-parse-buffer) 'underline 'identity nil t))))
  1833. '("first line\nsecond line"))))
  1834. ;;;; Verbatim
  1835. (ert-deftest test-org-element/verbatim-parser ()
  1836. "Test `verbatim' parser."
  1837. ;; Regular test.
  1838. (should
  1839. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1840. (org-test-with-temp-text "=verbatim="
  1841. (org-element-map (org-element-parse-buffer) 'verbatim 'identity))))
  1842. ;; Multi-line markup.
  1843. (should
  1844. (equal
  1845. (org-element-property
  1846. :value
  1847. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1848. (org-test-with-temp-text "=first line\nsecond line="
  1849. (org-element-map
  1850. (org-element-parse-buffer) 'verbatim 'identity nil t))))
  1851. "first line\nsecond line")))
  1852. ;;;; Verse Block
  1853. (ert-deftest test-org-element/verse-block-parser ()
  1854. "Test `verse-block' parser."
  1855. ;; Standard test.
  1856. (should
  1857. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
  1858. (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
  1859. ;; Ignore case.
  1860. (should
  1861. (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
  1862. (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
  1863. ;; Parse objects in verse blocks.
  1864. (should
  1865. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
  1866. (org-element-map (org-element-parse-buffer) 'entity 'identity)))
  1867. ;; Ignore incomplete verse block.
  1868. (should-not
  1869. (org-test-with-temp-text "#+BEGIN_VERSE"
  1870. (org-element-map (org-element-parse-buffer) 'verse-block 'identity nil t)))
  1871. ;; Handle non-empty blank line at the end of buffer.
  1872. (should
  1873. (org-test-with-temp-text "#+BEGIN_VERSE\nC\n#+END_VERSE\n "
  1874. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1875. ;;; Test Interpreters.
  1876. (ert-deftest test-org-element/interpret-data ()
  1877. "Test `org-element-interpret-data' specifications."
  1878. ;; Interpret simple affiliated keywords.
  1879. (should
  1880. (equal
  1881. (org-element-interpret-data
  1882. '(org-data nil (paragraph (:name "para") "Paragraph")))
  1883. "#+NAME: para\nParagraph\n"))
  1884. ;; Interpret multiple affiliated keywords.
  1885. (should
  1886. (equal
  1887. (org-element-interpret-data
  1888. '(org-data nil (paragraph (:attr_ascii ("line2" "line1")) "Paragraph")))
  1889. "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph\n"))
  1890. ;; Interpret parsed affiliated keywords.
  1891. (should
  1892. (equal
  1893. (org-element-interpret-data
  1894. '(org-data nil (paragraph (:caption (("caption"))) "Paragraph")))
  1895. "#+CAPTION: caption\nParagraph\n"))
  1896. ;; Interpret dual affiliated keywords.
  1897. (should
  1898. (equal
  1899. (org-element-interpret-data
  1900. '(org-data nil (paragraph (:caption ((("long") "short"))) "Paragraph")))
  1901. "#+CAPTION[short]: long\nParagraph\n"))
  1902. ;; Interpret multiple parsed dual keywords.
  1903. (should
  1904. (equal
  1905. (org-element-interpret-data
  1906. '(org-data nil (paragraph
  1907. (:caption ((("l2") "s2") (("l1") "s1"))) "Paragraph")))
  1908. "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph\n"))
  1909. ;; Pseudo objects and elements are transparent.
  1910. (should
  1911. (equal "A B\n"
  1912. (org-element-interpret-data
  1913. '(paragraph nil (pseudo-object (:post-blank 1) "A") "B")
  1914. '(pseudo-object))))
  1915. (should
  1916. (equal "A\n\nB\n"
  1917. (org-element-interpret-data
  1918. '(center nil
  1919. (pseudo-element (:post-blank 1) (paragraph nil "A"))
  1920. (paragraph nil "B"))))))
  1921. (ert-deftest test-org-element/center-block-interpreter ()
  1922. "Test center block interpreter."
  1923. (should
  1924. (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
  1925. "#+BEGIN_CENTER\nTest\n#+END_CENTER\n")))
  1926. (ert-deftest test-org-element/drawer-interpreter ()
  1927. "Test drawer interpreter."
  1928. (should
  1929. (equal (org-test-parse-and-interpret ":TEST:\nTest\n:END:")
  1930. ":TEST:\nTest\n:END:\n")))
  1931. (ert-deftest test-org-element/dynamic-block-interpreter ()
  1932. "Test dynamic block interpreter."
  1933. (should
  1934. (equal (org-test-parse-and-interpret
  1935. "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
  1936. "#+BEGIN: myblock :parameter value1\nTest\n#+END:\n")))
  1937. (ert-deftest test-org-element/footnote-definition-interpreter ()
  1938. "Test footnote definition interpreter."
  1939. (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n")))
  1940. (ert-deftest test-org-element/headline-interpreter ()
  1941. "Test headline and section interpreters."
  1942. ;; 1. Standard test.
  1943. (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
  1944. ;; 2. With TODO keywords.
  1945. (should
  1946. (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  1947. (org-test-parse-and-interpret "* TODO Headline"))
  1948. "* TODO Headline\n"))
  1949. ;; 3. With tags...
  1950. ;;
  1951. ;; 3.1. ... and a positive `org-tags-column' value.
  1952. (should
  1953. (equal (let ((org-tags-column 20))
  1954. (org-test-parse-and-interpret "* Headline :tag:"))
  1955. "* Headline :tag:\n"))
  1956. ;; 3.2. ... and a negative `org-tags-column' value.
  1957. (should
  1958. (equal (let ((org-tags-column -20))
  1959. (org-test-parse-and-interpret "* Headline :tag:"))
  1960. "* Headline :tag:\n"))
  1961. ;; 3.3. ... and a null `org-tags-column' value.
  1962. (should
  1963. (equal (let ((org-tags-column 0))
  1964. (org-test-parse-and-interpret "* Headline :tag:"))
  1965. "* Headline :tag:\n"))
  1966. ;; 4. With priority cookie.
  1967. (should
  1968. (equal (org-test-parse-and-interpret "* [#B] Headline")
  1969. "* [#B] Headline\n"))
  1970. ;; 5. With comment keyword.
  1971. (should
  1972. (equal (let ((org-comment-string "COMMENT"))
  1973. (org-test-parse-and-interpret "* COMMENT Headline"))
  1974. "* COMMENT Headline\n"))
  1975. ;; 6. Keep same number of blank lines before body.
  1976. (should
  1977. (equal (org-test-parse-and-interpret
  1978. "* Headline\n\n\nText after two blank lines.")
  1979. "* Headline\n\n\nText after two blank lines.\n")))
  1980. (ert-deftest test-org-element/inlinetask-interpreter ()
  1981. "Test inlinetask interpretation."
  1982. (when (featurep 'org-inlinetask)
  1983. (let ((org-inlinetask-min-level 15))
  1984. ;; 1. Regular inlinetask.
  1985. (should (equal (org-test-parse-and-interpret
  1986. "*************** Task\nTest\n*************** END")
  1987. "*************** Task\nTest\n*************** END\n"))
  1988. ;; 2. Degenerate inlinetask.
  1989. (should (equal (org-test-parse-and-interpret "*************** Task")
  1990. "*************** Task\n"))
  1991. ;; 3. Prefer degenerate form when there are no contents.
  1992. (should (equal (org-test-parse-and-interpret
  1993. "*************** Task\n*************** END")
  1994. "*************** Task\n"))
  1995. ;; 4. With TODO keywords.
  1996. (should
  1997. (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  1998. (org-test-parse-and-interpret "*************** TODO Task"))
  1999. "*************** TODO Task\n"))
  2000. ;; 5. With tags...
  2001. ;;
  2002. ;; 5.1. ... and a positive `org-tags-column' value.
  2003. (should
  2004. (equal (let ((org-tags-column 30))
  2005. (org-test-parse-and-interpret "*************** Task :tag:"))
  2006. "*************** Task :tag:\n"))
  2007. ;; 5.2. ... and a negative `org-tags-column' value.
  2008. (should
  2009. (equal (let ((org-tags-column -30))
  2010. (org-test-parse-and-interpret "*************** Task :tag:"))
  2011. "*************** Task :tag:\n"))
  2012. ;; 5.3. ... and a null `org-tags-column' value.
  2013. (should
  2014. (equal (let ((org-tags-column 0))
  2015. (org-test-parse-and-interpret "*************** Task :tag:"))
  2016. "*************** Task :tag:\n"))
  2017. ;; 6. With priority cookie.
  2018. (should
  2019. (equal (org-test-parse-and-interpret "*************** [#B] Task")
  2020. "*************** [#B] Task\n")))))
  2021. (ert-deftest test-org-element/plain-list-interpreter ()
  2022. "Test plain-list and item interpreters."
  2023. (let ((org-list-two-spaces-after-bullet-regexp nil))
  2024. ;; Unordered list.
  2025. (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
  2026. ;; Description list.
  2027. (should
  2028. (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
  2029. ;; Ordered list.
  2030. (should
  2031. (equal (let ((org-plain-list-ordered-item-terminator t))
  2032. (org-test-parse-and-interpret "1. Item"))
  2033. "1. Item\n"))
  2034. (should
  2035. (equal (let ((org-plain-list-ordered-item-terminator ?\)))
  2036. (org-test-parse-and-interpret "1) Item"))
  2037. "1) Item\n"))
  2038. ;; Ordered list with counter.
  2039. (should
  2040. (equal (let ((org-plain-list-ordered-item-terminator t))
  2041. (org-test-parse-and-interpret "1. [@5] Item"))
  2042. "5. [@5] Item\n"))
  2043. ;; List with check-boxes.
  2044. (should
  2045. (equal (org-test-parse-and-interpret
  2046. "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3")
  2047. "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3\n"))
  2048. ;; Item not starting with a paragraph.
  2049. (should
  2050. (equal (org-test-parse-and-interpret "-\n | a | b |")
  2051. "- \n | a | b |\n"))
  2052. ;; Special case: correctly handle "*" bullets.
  2053. (should (org-test-parse-and-interpret " * item"))
  2054. ;; Special case: correctly handle empty items.
  2055. (should (org-test-parse-and-interpret "-"))))
  2056. (ert-deftest test-org-element/quote-block-interpreter ()
  2057. "Test quote block interpreter."
  2058. (should (equal (org-test-parse-and-interpret
  2059. "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
  2060. "#+BEGIN_QUOTE\nTest\n#+END_QUOTE\n")))
  2061. (ert-deftest test-org-element/special-block-interpreter ()
  2062. "Test special block interpreter."
  2063. (should (equal (org-test-parse-and-interpret
  2064. "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
  2065. "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL\n")))
  2066. (ert-deftest test-org-element/babel-call-interpreter ()
  2067. "Test babel call interpreter."
  2068. ;; 1. Without argument.
  2069. (should (equal (org-test-parse-and-interpret "#+CALL: test()")
  2070. "#+CALL: test()\n"))
  2071. ;; 2. With argument.
  2072. (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
  2073. "#+CALL: test(x=2)\n"))
  2074. ;; 3. With header arguments.
  2075. (should (equal (org-test-parse-and-interpret
  2076. "#+CALL: test[:results output]()[:results html]")
  2077. "#+CALL: test[:results output]()[:results html]\n")))
  2078. (ert-deftest test-org-element/clock-interpreter ()
  2079. "Test clock interpreter."
  2080. ;; Running clock.
  2081. (should
  2082. (string-match
  2083. "CLOCK: \\[2012-01-01 .* 00:01\\]"
  2084. (org-test-parse-and-interpret "CLOCK: [2012-01-01 sun. 00:01]")))
  2085. ;; Closed clock.
  2086. (should
  2087. (string-match
  2088. "CLOCK: \\[2012-01-01 .* 00:01\\]--\\[2012-01-01 .* 00:02\\] => 0:01"
  2089. (org-test-parse-and-interpret "
  2090. CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))))
  2091. (ert-deftest test-org-element/comment-interpreter ()
  2092. "Test comment interpreter."
  2093. ;; Regular comment.
  2094. (should (equal (org-test-parse-and-interpret "# Comment") "# Comment\n"))
  2095. ;; Inline comment.
  2096. (should (equal (org-test-parse-and-interpret " # Comment")
  2097. "# Comment\n"))
  2098. ;; Preserve indentation.
  2099. (should (equal (org-test-parse-and-interpret " # No blank\n# One blank")
  2100. "# No blank\n# One blank\n")))
  2101. (ert-deftest test-org-element/comment-block-interpreter ()
  2102. "Test comment block interpreter."
  2103. (should (equal (org-test-parse-and-interpret
  2104. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
  2105. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT\n")))
  2106. (ert-deftest test-org-element/diary-sexp ()
  2107. "Test diary-sexp interpreter."
  2108. (should
  2109. (equal
  2110. (org-test-parse-and-interpret
  2111. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old")
  2112. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old\n")))
  2113. (ert-deftest test-org-element/example-block-interpreter ()
  2114. "Test example block interpreter."
  2115. ;; Without switches.
  2116. (should (equal (org-test-parse-and-interpret
  2117. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE")
  2118. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE\n"))
  2119. ;; With switches.
  2120. (should
  2121. (equal (org-test-parse-and-interpret
  2122. "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE")
  2123. "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE\n"))
  2124. ;; Preserve code escaping.
  2125. (should
  2126. (equal (org-test-parse-and-interpret
  2127. "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE")
  2128. "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE\n")))
  2129. (ert-deftest test-org-element/export-block-interpreter ()
  2130. "Test export block interpreter."
  2131. (should (equal (org-test-parse-and-interpret
  2132. "#+BEGIN_HTML\nTest\n#+END_HTML")
  2133. "#+BEGIN_HTML\nTest\n#+END_HTML\n")))
  2134. (ert-deftest test-org-element/fixed-width-interpreter ()
  2135. "Test fixed width interpreter."
  2136. ;; Standard test.
  2137. (should (equal (org-test-parse-and-interpret ": Test") ": Test\n"))
  2138. ;; Preserve indentation.
  2139. (should (equal (org-test-parse-and-interpret ": 2 blanks\n: 1 blank")
  2140. ": 2 blanks\n: 1 blank\n"))
  2141. ;; Remove last newline character
  2142. (should
  2143. (equal (org-element-fixed-width-interpreter
  2144. '(fixed-width (:value "Test\n")) nil)
  2145. ": Test"))
  2146. (should
  2147. (equal (org-element-fixed-width-interpreter
  2148. '(fixed-width (:value "Test")) nil)
  2149. ": Test"))
  2150. ;; Handle empty string.
  2151. (should
  2152. (equal (org-element-fixed-width-interpreter
  2153. '(fixed-width (:value "")) nil)
  2154. ""))
  2155. ;; Handle nil value.
  2156. (should-not
  2157. (org-element-fixed-width-interpreter
  2158. '(fixed-width (:value nil)) nil)))
  2159. (ert-deftest test-org-element/horizontal-rule-interpreter ()
  2160. "Test horizontal rule interpreter."
  2161. (should (equal (org-test-parse-and-interpret "-------") "-----\n")))
  2162. (ert-deftest test-org-element/keyword-interpreter ()
  2163. "Test keyword interpreter."
  2164. (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
  2165. "#+KEYWORD: value\n")))
  2166. (ert-deftest test-org-element/latex-environment-interpreter ()
  2167. "Test latex environment interpreter."
  2168. (should (equal (org-test-parse-and-interpret
  2169. "\\begin{equation}\n1+1=2\n\\end{equation}")
  2170. "\\begin{equation}\n1+1=2\n\\end{equation}\n"))
  2171. (should (equal (org-test-parse-and-interpret
  2172. "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}")
  2173. "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}\n")))
  2174. (ert-deftest test-org-element/planning-interpreter ()
  2175. "Test planning interpreter."
  2176. (should
  2177. (string-match
  2178. "\\* Headline
  2179. DEADLINE: <2012-03-29 .*?> SCHEDULED: <2012-03-29 .*?> CLOSED: \\[2012-03-29 .*?\\]"
  2180. (org-test-parse-and-interpret
  2181. "* Headline
  2182. DEADLINE: <2012-03-29 thu.> SCHEDULED: <2012-03-29 thu.> CLOSED: [2012-03-29 thu.]"))))
  2183. (ert-deftest test-org-element/property-drawer-interpreter ()
  2184. "Test property drawer interpreter."
  2185. (should (equal (let ((org-property-format "%-10s %s"))
  2186. (org-test-parse-and-interpret
  2187. ":PROPERTIES:\n:prop: value\n:END:"))
  2188. ":PROPERTIES:\n:prop: value\n:END:\n")))
  2189. (ert-deftest test-org-element/src-block-interpreter ()
  2190. "Test src block interpreter."
  2191. ;; With arguments.
  2192. (should
  2193. (equal (let ((org-edit-src-content-indentation 2))
  2194. (org-test-parse-and-interpret
  2195. "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC"))
  2196. "#+BEGIN_SRC emacs-lisp :results silent\n (+ 1 1)\n#+END_SRC\n"))
  2197. ;; With switches.
  2198. (should
  2199. (equal (let ((org-edit-src-content-indentation 2))
  2200. (org-test-parse-and-interpret
  2201. "#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
  2202. "#+BEGIN_SRC emacs-lisp -n -k\n (+ 1 1)\n#+END_SRC\n"))
  2203. ;; Preserve code escaping.
  2204. (should
  2205. (equal (let ((org-edit-src-content-indentation 2))
  2206. (org-test-parse-and-interpret
  2207. "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC"))
  2208. "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC\n"))
  2209. ;; Do not apply `org-edit-src-content-indentation' when preserving
  2210. ;; indentation.
  2211. (should
  2212. (equal (let ((org-edit-src-content-indentation 2)
  2213. (org-src-preserve-indentation t))
  2214. (org-test-parse-and-interpret
  2215. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"))
  2216. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n"))
  2217. (should
  2218. (equal (let ((org-edit-src-content-indentation 2)
  2219. (org-src-preserve-indentation nil))
  2220. (org-test-parse-and-interpret
  2221. "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"))
  2222. "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC\n")))
  2223. (ert-deftest test-org-element/table-interpreter ()
  2224. "Test table, table-row and table-cell interpreters."
  2225. ;; 1. Simple table.
  2226. (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
  2227. "| a | b |\n| c | d |\n"))
  2228. ;; 2. With horizontal rules.
  2229. (should (equal (org-test-parse-and-interpret
  2230. "| a | b |\n|---+---|\n| c | d |")
  2231. "| a | b |\n|---+---|\n| c | d |\n"))
  2232. ;; 3. With meta-data.
  2233. (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
  2234. "| / | < | > |\n| * | 1 | 2 |\n"))
  2235. ;; 4. With a formula.
  2236. (should
  2237. (equal (org-test-parse-and-interpret
  2238. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)")
  2239. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)\n"))
  2240. ;; 5. With multiple formulas.
  2241. (should
  2242. (equal (org-test-parse-and-interpret
  2243. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2")
  2244. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2\n")))
  2245. (ert-deftest test-org-element/timestamp-interpreter ()
  2246. "Test timestamp interpreter."
  2247. ;; Active.
  2248. (should
  2249. (string-match "<2012-03-29 .* 16:40>"
  2250. (org-test-parse-and-interpret "<2012-03-29 thu. 16:40>")))
  2251. (should
  2252. (string-match "<2012-03-29 .* 16:40>"
  2253. (org-element-timestamp-interpreter
  2254. '(timestamp
  2255. (:type active :year-start 2012 :month-start 3 :day-start 29
  2256. :hour-start 16 :minute-start 40)) nil)))
  2257. ;; Inactive.
  2258. (should
  2259. (string-match "\\[2012-03-29 .* 16:40\\]"
  2260. (org-test-parse-and-interpret "[2012-03-29 thu. 16:40]")))
  2261. (should
  2262. (string-match
  2263. "\\[2012-03-29 .* 16:40\\]"
  2264. (org-element-timestamp-interpreter
  2265. '(timestamp
  2266. (:type inactive :year-start 2012 :month-start 3 :day-start 29
  2267. :hour-start 16 :minute-start 40)) nil)))
  2268. ;; Active range.
  2269. (should
  2270. (string-match "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
  2271. (org-test-parse-and-interpret
  2272. "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>")))
  2273. (should
  2274. (string-match
  2275. "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
  2276. (org-element-timestamp-interpreter
  2277. '(timestamp
  2278. (:type active-range :year-start 2012 :month-start 3 :day-start 29
  2279. :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
  2280. :day-end 29 :hour-end 16 :minute-end 41)) nil)))
  2281. ;; Inactive range.
  2282. (should
  2283. (string-match "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
  2284. (org-test-parse-and-interpret
  2285. "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]")))
  2286. (should
  2287. (string-match
  2288. "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
  2289. (org-element-timestamp-interpreter
  2290. '(timestamp
  2291. (:type inactive-range :year-start 2012 :month-start 3 :day-start 29
  2292. :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
  2293. :day-end 29 :hour-end 16 :minute-end 41)) nil)))
  2294. ;; Diary.
  2295. (should (equal (org-test-parse-and-interpret "<%%diary-float t 4 2>")
  2296. "<%%diary-float t 4 2>\n"))
  2297. ;; Timestamp with repeater interval, with delay, with both.
  2298. (should
  2299. (string-match "<2012-03-29 .* \\+1y>"
  2300. (org-test-parse-and-interpret "<2012-03-29 thu. +1y>")))
  2301. (should
  2302. (string-match
  2303. "<2012-03-29 .* \\+1y>"
  2304. (org-element-timestamp-interpreter
  2305. '(timestamp
  2306. (:type active :year-start 2012 :month-start 3 :day-start 29
  2307. :repeater-type cumulate :repeater-value 1 :repeater-unit year))
  2308. nil)))
  2309. (should
  2310. (string-match
  2311. "<2012-03-29 .* -1y>"
  2312. (org-element-timestamp-interpreter
  2313. '(timestamp
  2314. (:type active :year-start 2012 :month-start 3 :day-start 29
  2315. :warning-type all :warning-value 1 :warning-unit year))
  2316. nil)))
  2317. (should
  2318. (string-match
  2319. "<2012-03-29 .* \\+1y -1y>"
  2320. (org-element-timestamp-interpreter
  2321. '(timestamp
  2322. (:type active :year-start 2012 :month-start 3 :day-start 29
  2323. :warning-type all :warning-value 1 :warning-unit year
  2324. :repeater-type cumulate :repeater-value 1 :repeater-unit year))
  2325. nil)))
  2326. ;; Timestamp range with repeater interval
  2327. (should
  2328. (string-match "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
  2329. (org-test-parse-and-interpret
  2330. "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>")))
  2331. (should
  2332. (string-match
  2333. "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
  2334. (org-element-timestamp-interpreter
  2335. '(timestamp
  2336. (:type active-range :year-start 2012 :month-start 3 :day-start 29
  2337. :year-end 2012 :month-end 3 :day-end 30 :repeater-type cumulate
  2338. :repeater-value 1 :repeater-unit year))
  2339. nil))))
  2340. (ert-deftest test-org-element/verse-block-interpreter ()
  2341. "Test verse block interpretation."
  2342. (should
  2343. (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
  2344. "#+BEGIN_VERSE\nTest\n#+END_VERSE\n")))
  2345. (ert-deftest test-org-element/bold-interpreter ()
  2346. "Test bold interpreter."
  2347. (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))
  2348. (ert-deftest test-org-element/code-interpreter ()
  2349. "Test code interpreter."
  2350. (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))
  2351. (ert-deftest test-org-element/entity-interpreter ()
  2352. "Test entity interpreter."
  2353. ;; 1. Without brackets.
  2354. (should
  2355. (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
  2356. ;; 2. With brackets.
  2357. (should
  2358. (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))
  2359. (ert-deftest test-org-element/export-snippet-interpreter ()
  2360. "Test export snippet interpreter."
  2361. (should (equal (org-test-parse-and-interpret "@@back-end:contents@@")
  2362. "@@back-end:contents@@\n")))
  2363. (ert-deftest test-org-element/footnote-reference-interpreter ()
  2364. "Test footnote reference interpreter."
  2365. ;; 1. Regular reference.
  2366. (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
  2367. ;; 2. Normalized reference.
  2368. (should (equal (org-test-parse-and-interpret "Text[1]") "Text[1]\n"))
  2369. ;; 3. Named reference.
  2370. (should (equal (org-test-parse-and-interpret "Text[fn:label]")
  2371. "Text[fn:label]\n"))
  2372. ;; 4. Inline reference.
  2373. (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
  2374. "Text[fn:label:def]\n"))
  2375. ;; 5. Anonymous reference.
  2376. (should (equal (org-test-parse-and-interpret "Text[fn::def]")
  2377. "Text[fn::def]\n")))
  2378. (ert-deftest test-org-element/inline-babel-call-interpreter ()
  2379. "Test inline babel call interpreter."
  2380. ;; 1. Without arguments.
  2381. (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
  2382. ;; 2. With arguments.
  2383. (should (equal (org-test-parse-and-interpret "call_test(x=2)")
  2384. "call_test(x=2)\n"))
  2385. ;; 3. With header arguments.
  2386. (should (equal (org-test-parse-and-interpret
  2387. "call_test[:results output]()[:results html]")
  2388. "call_test[:results output]()[:results html]\n")))
  2389. (ert-deftest test-org-element/inline-src-block-interpreter ()
  2390. "Test inline src block interpreter."
  2391. ;; 1. Without header argument.
  2392. (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
  2393. "src_emacs-lisp{(+ 1 1)}\n"))
  2394. ;; 2. With header arguments.
  2395. (should (equal (org-test-parse-and-interpret
  2396. "src_emacs-lisp[:results silent]{(+ 1 1)}")
  2397. "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))
  2398. (ert-deftest test-org-element/italic-interpreter ()
  2399. "Test italic interpreter."
  2400. (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))
  2401. (ert-deftest test-org-element/latex-fragment-interpreter ()
  2402. "Test latex fragment interpreter."
  2403. (should (equal (org-test-parse-and-interpret "\\command{}") "\\command{}\n"))
  2404. (should (equal (org-test-parse-and-interpret "$x$") "$x$\n"))
  2405. (should (equal (org-test-parse-and-interpret "$x+y$") "$x+y$\n"))
  2406. (should (equal (org-test-parse-and-interpret "$$x+y$$") "$$x+y$$\n"))
  2407. (should (equal (org-test-parse-and-interpret "\\(x+y\\)") "\\(x+y\\)\n"))
  2408. (should (equal (org-test-parse-and-interpret "\\[x+y\\]") "\\[x+y\\]\n")))
  2409. (ert-deftest test-org-element/line-break-interpreter ()
  2410. "Test line break interpreter."
  2411. (should (equal (org-test-parse-and-interpret "First line \\\\ \nSecond line")
  2412. "First line \\\\\nSecond line\n")))
  2413. (ert-deftest test-org-element/link-interpreter ()
  2414. "Test link interpreter."
  2415. ;; 1. Links targeted from a radio target.
  2416. (should (equal (let ((org-target-link-regexp "radio-target"))
  2417. (org-test-parse-and-interpret "a radio-target"))
  2418. "a radio-target\n"))
  2419. ;; 2. Regular links.
  2420. ;;
  2421. ;; 2.1. Without description.
  2422. (should (equal (org-test-parse-and-interpret "[[http://orgmode.org]]")
  2423. "[[http://orgmode.org]]\n"))
  2424. ;; 2.2. With a description.
  2425. (should (equal (org-test-parse-and-interpret
  2426. "[[http://orgmode.org][Org mode]]")
  2427. "[[http://orgmode.org][Org mode]]\n"))
  2428. ;; 2.3. Id links.
  2429. (should (equal (org-test-parse-and-interpret "[[id:aaaa]]") "[[id:aaaa]]\n"))
  2430. ;; 2.4. Custom-id links.
  2431. (should (equal (org-test-parse-and-interpret "[[#id]]") "[[#id]]\n"))
  2432. ;; 2.5 Code-ref links.
  2433. (should (equal (org-test-parse-and-interpret "[[(ref)]]") "[[(ref)]]\n"))
  2434. ;; 3. Normalize plain links.
  2435. (should (equal (org-test-parse-and-interpret "http://orgmode.org")
  2436. "[[http://orgmode.org]]\n"))
  2437. ;; 4. Normalize angular links.
  2438. (should (equal (org-test-parse-and-interpret "<http://orgmode.org>")
  2439. "[[http://orgmode.org]]\n")))
  2440. (ert-deftest test-org-element/macro-interpreter ()
  2441. "Test macro interpreter."
  2442. ;; 1. Without argument.
  2443. (should (equal (org-test-parse-and-interpret "{{{test}}}") "{{{test}}}\n"))
  2444. ;; 2. With arguments.
  2445. (should (equal (org-test-parse-and-interpret "{{{test(arg1,arg2)}}}")
  2446. "{{{test(arg1,arg2)}}}\n")))
  2447. (ert-deftest test-org-element/radio-target-interpreter ()
  2448. "Test radio target interpreter."
  2449. (should (equal (org-test-parse-and-interpret "<<<some text>>>")
  2450. "<<<some text>>>\n")))
  2451. (ert-deftest test-org-element/statistics-cookie-interpreter ()
  2452. "Test statistics cookie interpreter."
  2453. ;; 1. Without percent
  2454. (should (equal (org-test-parse-and-interpret "[0/1]") "[0/1]\n"))
  2455. ;; 2. With percent.
  2456. (should (equal (org-test-parse-and-interpret "[66%]") "[66%]\n")))
  2457. (ert-deftest test-org-element/strike-through-interpreter ()
  2458. "Test strike through interpreter."
  2459. (should (equal (org-test-parse-and-interpret "+target+") "+target+\n")))
  2460. (ert-deftest test-org-element/subscript-interpreter ()
  2461. "Test subscript interpreter."
  2462. ;; 1. Without brackets.
  2463. (should (equal (org-test-parse-and-interpret "a_b") "a_b\n"))
  2464. ;; 2. With brackets.
  2465. (should (equal (org-test-parse-and-interpret "a_{b}") "a_{b}\n")))
  2466. (ert-deftest test-org-element/superscript-interpreter ()
  2467. "Test superscript interpreter."
  2468. ;; 1. Without brackets.
  2469. (should (equal (org-test-parse-and-interpret "a^b") "a^b\n"))
  2470. ;; 2. With brackets.
  2471. (should (equal (org-test-parse-and-interpret "a^{b}") "a^{b}\n")))
  2472. (ert-deftest test-org-element/target-interpreter ()
  2473. "Test target interpreter."
  2474. (should (equal (org-test-parse-and-interpret "<<target>>") "<<target>>\n")))
  2475. (ert-deftest test-org-element/underline-interpreter ()
  2476. "Test underline interpreter."
  2477. (should (equal (org-test-parse-and-interpret "_text_") "_text_\n")))
  2478. (ert-deftest test-org-element/verbatim-interpreter ()
  2479. "Test verbatim interpreter."
  2480. (should (equal (org-test-parse-and-interpret "=text=") "=text=\n")))
  2481. ;;; Test Granularity
  2482. (ert-deftest test-org-element/granularity ()
  2483. "Test granularity impact on buffer parsing."
  2484. (org-test-with-temp-text "
  2485. * Head 1
  2486. ** Head 2
  2487. #+BEGIN_CENTER
  2488. Centered paragraph.
  2489. #+END_CENTER
  2490. Paragraph \\alpha."
  2491. ;; 1.1. Granularity set to `headline' should parse every headline
  2492. ;; in buffer, and only them.
  2493. (let ((tree (org-element-parse-buffer 'headline)))
  2494. (should (= 2 (length (org-element-map tree 'headline 'identity))))
  2495. (should-not (org-element-map tree 'paragraph 'identity)))
  2496. ;; 1.2. Granularity set to `greater-element' should not enter
  2497. ;; greater elements excepted headlines and sections.
  2498. (let ((tree (org-element-parse-buffer 'greater-element)))
  2499. (should (= 1 (length (org-element-map tree 'center-block 'identity))))
  2500. (should (= 1 (length (org-element-map tree 'paragraph 'identity))))
  2501. (should-not (org-element-map tree 'entity 'identity)))
  2502. ;; 1.3. Granularity set to `element' should enter every
  2503. ;; greater-element.
  2504. (let ((tree (org-element-parse-buffer 'element)))
  2505. (should (= 2 (length (org-element-map tree 'paragraph 'identity))))
  2506. (should-not (org-element-map tree 'entity 'identity)))
  2507. ;; 1.4. Granularity set to `object' can see everything.
  2508. (let ((tree (org-element-parse-buffer 'object)))
  2509. (should (= 1 (length (org-element-map tree 'entity 'identity)))))))
  2510. (ert-deftest test-org-element/secondary-string-parsing ()
  2511. "Test if granularity correctly toggles secondary strings parsing."
  2512. ;; 1. With a granularity bigger than `object', no secondary string
  2513. ;; should be parsed.
  2514. ;;
  2515. ;; 1.1. Test with `headline' type.
  2516. (org-test-with-temp-text "* Headline"
  2517. (let ((headline
  2518. (org-element-map (org-element-parse-buffer 'headline) 'headline
  2519. 'identity
  2520. nil
  2521. 'first-match)))
  2522. (should (stringp (org-element-property :title headline)))))
  2523. ;; 1.2. Test with `item' type.
  2524. (org-test-with-temp-text "* Headline\n- tag :: item"
  2525. (let ((item (org-element-map (org-element-parse-buffer 'element)
  2526. 'item
  2527. 'identity
  2528. nil
  2529. 'first-match)))
  2530. (should (stringp (org-element-property :tag item)))))
  2531. ;; 1.3. Test with `inlinetask' type, if avalaible.
  2532. (when (featurep 'org-inlinetask)
  2533. (let ((org-inlinetask-min-level 15))
  2534. (org-test-with-temp-text "*************** Inlinetask"
  2535. (let ((inlinetask (org-element-map (org-element-parse-buffer 'element)
  2536. 'inlinetask
  2537. 'identity
  2538. nil
  2539. 'first-match)))
  2540. (should (stringp (org-element-property :title inlinetask)))))))
  2541. ;; 2. With a default granularity, secondary strings should be
  2542. ;; parsed.
  2543. (org-test-with-temp-text "* Headline"
  2544. (let ((headline
  2545. (org-element-map (org-element-parse-buffer) 'headline
  2546. 'identity
  2547. nil
  2548. 'first-match)))
  2549. (should (listp (org-element-property :title headline)))))
  2550. ;; 3. `org-element-at-point' should never parse a secondary string.
  2551. (org-test-with-temp-text "* Headline"
  2552. (should (stringp (org-element-property :title (org-element-at-point))))))
  2553. ;;; Test Visible Only Parsing
  2554. (ert-deftest test-org-element/parse-buffer-visible ()
  2555. "Test `org-element-parse-buffer' with visible only argument."
  2556. (should
  2557. (equal '("H1" "H3" "H5")
  2558. (org-test-with-temp-text
  2559. "* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:"
  2560. (org-occur ":visible:")
  2561. (org-element-map (org-element-parse-buffer nil t) 'headline
  2562. (lambda (hl) (org-element-property :raw-value hl)))))))
  2563. ;;; Test `:parent' Property
  2564. (ert-deftest test-org-element/parent-property ()
  2565. "Test `:parent' property."
  2566. ;; Elements.
  2567. (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
  2568. (let* ((tree (org-element-parse-buffer))
  2569. (parent (org-element-property
  2570. :parent
  2571. (org-element-map tree 'paragraph 'identity nil t))))
  2572. (should parent)
  2573. (should (eq (org-element-map tree 'center-block 'identity nil t)
  2574. parent))))
  2575. ;; Objects.
  2576. (org-test-with-temp-text "a_{/b/}"
  2577. (let* ((tree (org-element-parse-buffer))
  2578. (parent (org-element-property
  2579. :parent
  2580. (org-element-map tree 'italic 'identity nil t))))
  2581. (should parent)
  2582. (should (eq parent
  2583. (org-element-map tree 'subscript 'identity nil t)))))
  2584. ;; Secondary strings
  2585. (org-test-with-temp-text "* /italic/"
  2586. (let* ((tree (org-element-parse-buffer))
  2587. (parent (org-element-property
  2588. :parent (org-element-map tree 'italic 'identity nil t))))
  2589. (should parent)
  2590. (should (eq parent
  2591. (org-element-map tree 'headline 'identity nil t))))))
  2592. ;;; Test Normalize Contents
  2593. (ert-deftest test-org-element/normalize-contents ()
  2594. "Test `org-element-normalize-contents' specifications."
  2595. ;; Remove maximum common indentation from element's contents.
  2596. (should
  2597. (equal
  2598. (org-element-normalize-contents
  2599. '(paragraph nil " Two spaces\n Three spaces"))
  2600. '(paragraph nil "Two spaces\n Three spaces")))
  2601. ;; Ignore objects within contents when computing maximum common
  2602. ;; indentation.
  2603. (should
  2604. (equal
  2605. (org-element-normalize-contents
  2606. '(paragraph nil " One " (emphasis nil "space") "\n Two spaces"))
  2607. '(paragraph nil "One " (emphasis nil "space") "\n Two spaces")))
  2608. ;; Ignore blank lines.
  2609. (should
  2610. (equal
  2611. (org-element-normalize-contents
  2612. '(paragraph nil " Two spaces\n\n \n Two spaces"))
  2613. '(paragraph nil "Two spaces\n\n \nTwo spaces")))
  2614. (should
  2615. (equal
  2616. '(paragraph nil " Two spaces\n" (verbatim nil "V") "\n Two spaces")
  2617. (org-element-normalize-contents
  2618. '(paragraph nil " Two spaces\n " (verbatim nil "V") "\n Two spaces"))))
  2619. ;; Recursively enter objects in order to compute common indentation.
  2620. (should
  2621. (equal
  2622. (org-element-normalize-contents
  2623. '(paragraph nil " Two spaces " (bold nil " and\n One space")))
  2624. '(paragraph nil " Two spaces " (bold nil " and\nOne space"))))
  2625. ;; When optional argument is provided, ignore first line
  2626. ;; indentation.
  2627. (should
  2628. (equal
  2629. (org-element-normalize-contents
  2630. '(paragraph nil "No space\n Two spaces\n Three spaces") t)
  2631. '(paragraph nil "No space\nTwo spaces\n Three spaces"))))
  2632. ;;; Test Navigation Tools.
  2633. (ert-deftest test-org-element/at-point ()
  2634. "Test `org-element-at-point' specifications."
  2635. ;; Return closest element containing point.
  2636. (should
  2637. (eq 'paragraph
  2638. (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
  2639. (progn (search-forward "A")
  2640. (org-element-type (org-element-at-point))))))
  2641. ;; Correctly set `:parent' property.
  2642. (should
  2643. (eq 'center-block
  2644. (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
  2645. (progn (search-forward "A")
  2646. (org-element-type
  2647. (org-element-property :parent (org-element-at-point)))))))
  2648. ;; Special case: at a blank line just below a headline, return that
  2649. ;; headline.
  2650. (should
  2651. (equal "H1" (org-test-with-temp-text "* H1\n \n* H2\n"
  2652. (forward-line)
  2653. (org-element-property :title (org-element-at-point)))))
  2654. ;; Special case: at the very beginning of a table, return `table'
  2655. ;; object instead of `table-row'.
  2656. (should
  2657. (eq 'table
  2658. (org-test-with-temp-text "| a | b |"
  2659. (org-element-type (org-element-at-point)))))
  2660. ;; Special case: at the very beginning of a list or sub-list, return
  2661. ;; `plain-list' object instead of `item'.
  2662. (should
  2663. (eq 'plain-list
  2664. (org-test-with-temp-text "- item"
  2665. (org-element-type (org-element-at-point)))))
  2666. ;; Special case: at the closing line of a greater element, be sure
  2667. ;; to return it instead of the last element in its contents.
  2668. (should
  2669. (eq 'center-block
  2670. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph\n#+END_CENTER"
  2671. (progn (forward-line 2)
  2672. (org-element-type (org-element-at-point))))))
  2673. ;; Special case: at a blank line between two items, be sure to
  2674. ;; return item above instead of the last element of its contents.
  2675. (should
  2676. (eq 'item
  2677. (org-test-with-temp-text "- Para1\n\n- Para2"
  2678. (progn (forward-line)
  2679. (org-element-type
  2680. (let ((org-list-empty-line-terminates-plain-lists nil))
  2681. (org-element-at-point)))))))
  2682. ;; Special case: at the last blank line in a plain list, return it
  2683. ;; instead of the last item.
  2684. (should
  2685. (eq 'plain-list
  2686. (org-test-with-temp-text "- Para1\n- Para2\n\nPara3"
  2687. (progn (forward-line 2)
  2688. (org-element-type (org-element-at-point))))))
  2689. ;; Special case: when a list ends at the end of buffer and there's
  2690. ;; no final newline, return last element in last item.
  2691. (should
  2692. (eq 'paragraph
  2693. (org-test-with-temp-text "- a"
  2694. (end-of-line)
  2695. (org-element-type (org-element-at-point)))))
  2696. ;; Parse a list within a block itself contained in a list.
  2697. (should
  2698. (eq 'plain-list
  2699. (org-test-with-temp-text
  2700. "- outer\n #+begin_center\n - inner\n #+end_center"
  2701. (search-forward "inner")
  2702. (beginning-of-line)
  2703. (org-element-type (org-element-at-point)))))
  2704. ;; Do not error at eob on an empty line.
  2705. (should
  2706. (org-test-with-temp-text "* H\n"
  2707. (forward-line)
  2708. (or (org-element-at-point) t))))
  2709. (ert-deftest test-org-element/context ()
  2710. "Test `org-element-context' specifications."
  2711. ;; Return closest object containing point.
  2712. (should
  2713. (eq 'underline
  2714. (org-test-with-temp-text "Some *text with _underline_ text*"
  2715. (progn (search-forward "under")
  2716. (org-element-type (org-element-context))))))
  2717. ;; Find objects in secondary strings.
  2718. (should
  2719. (eq 'underline
  2720. (org-test-with-temp-text "* Headline _with_ underlining"
  2721. (progn (search-forward "w")
  2722. (org-element-type (org-element-context))))))
  2723. ;; Find objects in objects.
  2724. (should
  2725. (eq 'macro
  2726. (org-test-with-temp-text "| a | {{{macro}}} |"
  2727. (progn (search-forward "{")
  2728. (org-element-type (org-element-context))))))
  2729. (should
  2730. (eq 'table-cell
  2731. (org-test-with-temp-text "| a | b {{{macro}}} |"
  2732. (progn (search-forward "b")
  2733. (org-element-type (org-element-context))))))
  2734. ;; Find objects in planning lines.
  2735. (should
  2736. (eq 'timestamp
  2737. (org-test-with-temp-text "* H\n SCHEDULED: <2012-03-29 thu.>"
  2738. (search-forward "2012")
  2739. (org-element-type (org-element-context)))))
  2740. (should-not
  2741. (eq 'timestamp
  2742. (org-test-with-temp-text "* H\n SCHEDULED: <2012-03-29 thu.>"
  2743. (search-forward "SCHEDULED")
  2744. (org-element-type (org-element-context)))))
  2745. ;; Find objects in document keywords.
  2746. (should
  2747. (eq 'macro
  2748. (org-test-with-temp-text "#+DATE: {{{macro}}}"
  2749. (progn (search-forward "{")
  2750. (org-element-type (org-element-context))))))
  2751. ;; Do not find objects in table rules.
  2752. (should
  2753. (eq 'table-row
  2754. (org-test-with-temp-text "| a | b |\n+---+---+\n| c | d |"
  2755. (forward-line)
  2756. (org-element-type (org-element-context)))))
  2757. ;; Find objects in parsed affiliated keywords.
  2758. (should
  2759. (eq 'macro
  2760. (org-test-with-temp-text "#+CAPTION: {{{macro}}}\n| a | b |."
  2761. (progn (search-forward "{")
  2762. (org-element-type (org-element-context))))))
  2763. (should
  2764. (eq 'bold
  2765. (org-test-with-temp-text "#+caption: *bold*\nParagraph"
  2766. (progn (search-forward "*")
  2767. (org-element-type (org-element-context))))))
  2768. ;; Find objects at the end of buffer.
  2769. (should
  2770. (eq 'bold
  2771. (org-test-with-temp-text "*bold*"
  2772. (goto-char (point-max))
  2773. (org-element-type (org-element-context)))))
  2774. ;; Correctly set `:parent' property.
  2775. (should
  2776. (eq 'paragraph
  2777. (org-test-with-temp-text "Some *bold* text"
  2778. (progn (search-forward "bold")
  2779. (org-element-type
  2780. (org-element-property :parent (org-element-context)))))))
  2781. ;; Between two objects, return the second one.
  2782. (should
  2783. (eq 'macro
  2784. (org-test-with-temp-text "<<target>>{{{test}}}"
  2785. (progn (search-forward "{")
  2786. (backward-char)
  2787. (org-element-type (org-element-context))))))
  2788. ;; Test optional argument.
  2789. (should
  2790. (eq 'underline
  2791. (org-test-with-temp-text "Some *text with _underline_ text*"
  2792. (progn
  2793. (search-forward "under")
  2794. (org-element-type (org-element-context (org-element-at-point)))))))
  2795. ;; Special case: bold object at the beginning of a headline.
  2796. (should
  2797. (eq 'bold
  2798. (org-test-with-temp-text "* *bold*"
  2799. (search-forward "bo")
  2800. (org-element-type (org-element-context))))))
  2801. ;;; Test Cache.
  2802. (ert-deftest test-org-element/cache ()
  2803. "Test basic expectations and common pitfalls for cache."
  2804. ;; Shift positions.
  2805. (should
  2806. (equal '(18 . 23)
  2807. (org-test-with-temp-text "para1\n\npara2\n\npara3"
  2808. (let ((org-element-use-cache t))
  2809. (save-excursion (goto-char (point-max)) (org-element-at-point))
  2810. (insert "add")
  2811. (forward-line 4)
  2812. (let ((element (org-element-at-point)))
  2813. (cons (org-element-property :begin element)
  2814. (org-element-property :end element)))))))
  2815. ;; Partial shifting: when the contents of a greater element are
  2816. ;; modified, only shift ending positions.
  2817. (should
  2818. (org-test-with-temp-text
  2819. "#+BEGIN_CENTER\nPara1\n\nPara2\n\nPara3\n#+END_CENTER"
  2820. (let ((org-element-use-cache t))
  2821. (save-excursion (search-forward "3") (org-element-at-point))
  2822. (search-forward "Para2")
  2823. (insert " ")
  2824. (let ((element (org-element-property :parent (org-element-at-point))))
  2825. (equal (cons (org-element-property :begin element)
  2826. (org-element-property :end element))
  2827. (cons (point-min) (point-max)))))))
  2828. ;; Re-parent shifted elements.
  2829. (should
  2830. (eq 'item
  2831. (org-test-with-temp-text "- item\n\n\n para1\n para2"
  2832. (let ((org-element-use-cache t))
  2833. (end-of-line)
  2834. (org-element-at-point)
  2835. (save-excursion (goto-char (point-max)) (org-element-at-point))
  2836. (forward-line)
  2837. (delete-char 1)
  2838. (goto-char (point-max))
  2839. (org-element-type
  2840. (org-element-property :parent (org-element-at-point)))))))
  2841. ;; Modifying the last line of an element alters the element below.
  2842. (should
  2843. (org-test-with-temp-text "para1\n\npara2"
  2844. (let ((org-element-use-cache t))
  2845. (goto-char (point-max))
  2846. (org-element-at-point)
  2847. (forward-line -1)
  2848. (insert "merge")
  2849. (let ((element (org-element-at-point)))
  2850. (equal (cons (org-element-property :begin element)
  2851. (org-element-property :end element))
  2852. (cons (point-min) (point-max)))))))
  2853. ;; Modifying the first line of an element alters the element above.
  2854. (should
  2855. (org-test-with-temp-text ": fixed-width\n:not-fixed-width"
  2856. (let ((org-element-use-cache t))
  2857. (goto-char (point-max))
  2858. (org-element-at-point)
  2859. (search-backward ":")
  2860. (forward-char)
  2861. (insert " ")
  2862. (let ((element (org-element-at-point)))
  2863. (equal (cons (org-element-property :begin element)
  2864. (org-element-property :end element))
  2865. (cons (point-min) (point-max)))))))
  2866. ;; Sensitive change: adding a line alters document structure both
  2867. ;; above and below.
  2868. (should
  2869. (eq 'example-block
  2870. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n"
  2871. (let ((org-element-use-cache t))
  2872. (goto-char (point-max))
  2873. (org-element-at-point)
  2874. (insert "#+END_EXAMPLE")
  2875. (search-backward "Para1")
  2876. (org-element-type (org-element-at-point))))))
  2877. (should
  2878. (eq 'example-block
  2879. (org-test-with-temp-text "Para1\n\nPara2\n#+END_EXAMPLE"
  2880. (let ((org-element-use-cache t))
  2881. (save-excursion (goto-char (point-max)) (org-element-at-point))
  2882. (insert "#+BEGIN_EXAMPLE\n")
  2883. (search-forward "Para2")
  2884. (org-element-type (org-element-at-point))))))
  2885. ;; Sensitive change: removing a line alters document structure both
  2886. ;; above and below.
  2887. (should
  2888. (eq 'example-block
  2889. (org-test-with-temp-text
  2890. "# +BEGIN_EXAMPLE\nPara1\n\nPara2\n#+END_EXAMPLE"
  2891. (let ((org-element-use-cache t))
  2892. (save-excursion (goto-char (point-max)) (org-element-at-point))
  2893. (forward-char)
  2894. (delete-char 1)
  2895. (search-forward "Para2")
  2896. (org-element-type (org-element-at-point))))))
  2897. (should
  2898. (eq 'example-block
  2899. (org-test-with-temp-text
  2900. "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n# +END_EXAMPLE"
  2901. (let ((org-element-use-cache t))
  2902. (save-excursion (goto-char (point-max)) (org-element-at-point))
  2903. (search-forward "# ")
  2904. (delete-char -1)
  2905. (search-backward "Para1")
  2906. (org-element-type (org-element-at-point)))))))
  2907. (provide 'test-org-element)
  2908. ;;; test-org-element.el ends here