test-org.el 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306
  1. ;;; test-org.el --- tests for org.el
  2. ;; Copyright (c) David Maus
  3. ;; Authors: David Maus
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Comments:
  16. ;; Template test file for Org-mode tests
  17. ;;; Code:
  18. ;;; Comments
  19. (ert-deftest test-org/toggle-comment ()
  20. "Test `org-toggle-comment' specifications."
  21. ;; Simple headline.
  22. (should
  23. (equal "* Test"
  24. (org-test-with-temp-text "* COMMENT Test"
  25. (org-toggle-comment)
  26. (buffer-string))))
  27. (should
  28. (equal "* COMMENT Test"
  29. (org-test-with-temp-text "* Test"
  30. (org-toggle-comment)
  31. (buffer-string))))
  32. ;; Headline with a regular keyword.
  33. (should
  34. (equal "* TODO Test"
  35. (org-test-with-temp-text "* TODO COMMENT Test"
  36. (org-toggle-comment)
  37. (buffer-string))))
  38. (should
  39. (equal "* TODO COMMENT Test"
  40. (org-test-with-temp-text "* TODO Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. ;; Empty headline.
  44. (should
  45. (equal "* "
  46. (org-test-with-temp-text "* COMMENT"
  47. (org-toggle-comment)
  48. (buffer-string))))
  49. (should
  50. (equal "* COMMENT"
  51. (org-test-with-temp-text "* "
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. ;; Headline with a single keyword.
  55. (should
  56. (equal "* TODO "
  57. (org-test-with-temp-text "* TODO COMMENT"
  58. (org-toggle-comment)
  59. (buffer-string))))
  60. (should
  61. (equal "* TODO COMMENT"
  62. (org-test-with-temp-text "* TODO"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. ;; Headline with a keyword, a priority cookie and contents.
  66. (should
  67. (equal "* TODO [#A] Headline"
  68. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  69. (org-toggle-comment)
  70. (buffer-string))))
  71. (should
  72. (equal "* TODO [#A] COMMENT Headline"
  73. (org-test-with-temp-text "* TODO [#A] Headline"
  74. (org-toggle-comment)
  75. (buffer-string)))))
  76. (ert-deftest test-org/comment-dwim ()
  77. "Test `comment-dwim' behaviour in an Org buffer."
  78. ;; No region selected, no comment on current line and line not
  79. ;; empty: insert comment on line above.
  80. (should
  81. (equal "# \nComment"
  82. (org-test-with-temp-text "Comment"
  83. (progn (call-interactively 'comment-dwim)
  84. (buffer-string)))))
  85. ;; No region selected, no comment on current line and line empty:
  86. ;; insert comment on this line.
  87. (should
  88. (equal "# \nParagraph"
  89. (org-test-with-temp-text "\nParagraph"
  90. (progn (call-interactively 'comment-dwim)
  91. (buffer-string)))))
  92. ;; No region selected, and a comment on this line: indent it.
  93. (should
  94. (equal "* Headline\n # Comment"
  95. (org-test-with-temp-text "* Headline\n# Comment"
  96. (progn (forward-line)
  97. (let ((org-adapt-indentation t))
  98. (call-interactively 'comment-dwim))
  99. (buffer-string)))))
  100. ;; Also recognize single # at column 0 as comments.
  101. (should
  102. (equal "# Comment"
  103. (org-test-with-temp-text "# Comment"
  104. (progn (forward-line)
  105. (call-interactively 'comment-dwim)
  106. (buffer-string)))))
  107. ;; Region selected and only comments and blank lines within it:
  108. ;; un-comment all commented lines.
  109. (should
  110. (equal "Comment 1\n\nComment 2"
  111. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  112. (progn
  113. (transient-mark-mode 1)
  114. (push-mark (point) t t)
  115. (goto-char (point-max))
  116. (call-interactively 'comment-dwim)
  117. (buffer-string)))))
  118. ;; Region selected without comments: comment all lines if
  119. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  120. (should
  121. (equal "# Comment 1\n\n# Comment 2"
  122. (org-test-with-temp-text "Comment 1\n\nComment 2"
  123. (progn
  124. (transient-mark-mode 1)
  125. (push-mark (point) t t)
  126. (goto-char (point-max))
  127. (let ((comment-empty-lines nil))
  128. (call-interactively 'comment-dwim))
  129. (buffer-string)))))
  130. (should
  131. (equal "# Comment 1\n# \n# Comment 2"
  132. (org-test-with-temp-text "Comment 1\n\nComment 2"
  133. (progn
  134. (transient-mark-mode 1)
  135. (push-mark (point) t t)
  136. (goto-char (point-max))
  137. (let ((comment-empty-lines t))
  138. (call-interactively 'comment-dwim))
  139. (buffer-string)))))
  140. ;; In front of a keyword without region, insert a new comment.
  141. (should
  142. (equal "# \n#+KEYWORD: value"
  143. (org-test-with-temp-text "#+KEYWORD: value"
  144. (progn (call-interactively 'comment-dwim)
  145. (buffer-string)))))
  146. ;; In a source block, use appropriate syntax.
  147. (should
  148. (equal " ;; "
  149. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
  150. (forward-line)
  151. (let ((org-edit-src-content-indentation 2))
  152. (call-interactively 'comment-dwim))
  153. (buffer-substring-no-properties (line-beginning-position) (point)))))
  154. (should
  155. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  156. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
  157. (forward-line)
  158. (transient-mark-mode 1)
  159. (push-mark (point) t t)
  160. (forward-line 2)
  161. (let ((org-edit-src-content-indentation 2))
  162. (call-interactively 'comment-dwim))
  163. (buffer-string)))))
  164. ;;; Date and time analysis
  165. (ert-deftest test-org/org-read-date ()
  166. "Test `org-read-date' specifications."
  167. ;; Parse ISO date with abbreviated year and month.
  168. (should (equal "2012-03-29 16:40"
  169. (let ((org-time-was-given t))
  170. (org-read-date t nil "12-3-29 16:40"))))
  171. ;; Parse Europeans dates.
  172. (should (equal "2012-03-29 16:40"
  173. (let ((org-time-was-given t))
  174. (org-read-date t nil "29.03.2012 16:40"))))
  175. ;; Parse Europeans dates without year.
  176. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  177. (let ((org-time-was-given t))
  178. (org-read-date t nil "29.03. 16:40"))))
  179. ;; Relative date applied to current time if there is single
  180. ;; plus/minus, or to default date when there are two of them.
  181. (should
  182. (equal
  183. "2015-03-04"
  184. (flet ((current-time () (apply #'encode-time
  185. (org-parse-time-string "2014-03-04"))))
  186. (org-read-date
  187. t nil "+1y" nil
  188. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  189. (should
  190. (equal
  191. "2013-03-29"
  192. (flet ((current-time () (apply #'encode-time
  193. (org-parse-time-string "2014-03-04"))))
  194. (org-read-date
  195. t nil "++1y" nil
  196. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  197. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  198. ;; dates (relatively to now) when incomplete. Otherwise, use
  199. ;; default date.
  200. (should
  201. (equal
  202. "2014-04-01"
  203. (flet ((current-time () (apply #'encode-time
  204. (org-parse-time-string "2014-03-04"))))
  205. (let ((org-read-date-prefer-future t))
  206. (org-read-date t nil "1")))))
  207. (should
  208. (equal
  209. "2013-03-04"
  210. (flet ((current-time () (apply #'encode-time
  211. (org-parse-time-string "2012-03-29"))))
  212. (let ((org-read-date-prefer-future t))
  213. (org-read-date t nil "3-4")))))
  214. (should
  215. (equal
  216. "2012-03-04"
  217. (flet ((current-time () (apply #'encode-time
  218. (org-parse-time-string "2012-03-29"))))
  219. (let ((org-read-date-prefer-future nil))
  220. (org-read-date t nil "3-4")))))
  221. ;; When set to `org-read-date-prefer-future' is set to `time', read
  222. ;; day is moved to tomorrow if specified hour is before current
  223. ;; time. However, it only happens in no other part of the date is
  224. ;; specified.
  225. (should
  226. (equal
  227. "2012-03-30"
  228. (flet ((current-time () (apply #'encode-time
  229. (org-parse-time-string "2012-03-29 16:40"))))
  230. (let ((org-read-date-prefer-future 'time))
  231. (org-read-date t nil "00:40" nil)))))
  232. (should-not
  233. (equal
  234. "2012-03-30"
  235. (flet ((current-time () (apply #'encode-time
  236. (org-parse-time-string "2012-03-29 16:40"))))
  237. (let ((org-read-date-prefer-future 'time))
  238. (org-read-date t nil "29 00:40" nil)))))
  239. ;; Caveat: `org-read-date-prefer-future' always refers to current
  240. ;; time, not default time, when they differ.
  241. (should
  242. (equal
  243. "2014-04-01"
  244. (flet ((current-time
  245. () (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  246. (let ((org-read-date-prefer-future t))
  247. (org-read-date
  248. t nil "1" nil
  249. (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
  250. (should
  251. (equal
  252. "2014-03-25"
  253. (flet ((current-time
  254. () (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  255. (let ((org-read-date-prefer-future t))
  256. (org-read-date
  257. t nil "25" nil
  258. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
  259. (ert-deftest test-org/org-parse-time-string ()
  260. "Test `org-parse-time-string'."
  261. (should (equal (org-parse-time-string "2012-03-29 16:40")
  262. '(0 40 16 29 3 2012 nil nil nil)))
  263. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  264. '(0 40 16 29 3 2012 nil nil nil)))
  265. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  266. '(0 40 16 29 3 2012 nil nil nil)))
  267. (should (equal (org-parse-time-string "<2012-03-29>")
  268. '(0 0 0 29 3 2012 nil nil nil)))
  269. (should (equal (org-parse-time-string "<2012-03-29>" t)
  270. '(0 nil nil 29 3 2012 nil nil nil))))
  271. (ert-deftest test-org/closest-date ()
  272. "Test `org-closest-date' specifications."
  273. (require 'calendar)
  274. ;; Time stamps without a repeater are returned unchanged.
  275. (should
  276. (equal
  277. '(3 29 2012)
  278. (calendar-gregorian-from-absolute
  279. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  280. ;; Time stamps with a null repeater are returned unchanged.
  281. (should
  282. (equal
  283. '(3 29 2012)
  284. (calendar-gregorian-from-absolute
  285. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  286. ;; if PREFER is set to `past' always return a date before, or equal
  287. ;; to CURRENT.
  288. (should
  289. (equal
  290. '(3 1 2014)
  291. (calendar-gregorian-from-absolute
  292. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  293. (should
  294. (equal
  295. '(3 4 2014)
  296. (calendar-gregorian-from-absolute
  297. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  298. ;; if PREFER is set to `future' always return a date before, or equal
  299. ;; to CURRENT.
  300. (should
  301. (equal
  302. '(3 29 2014)
  303. (calendar-gregorian-from-absolute
  304. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  305. (should
  306. (equal
  307. '(3 4 2014)
  308. (calendar-gregorian-from-absolute
  309. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  310. ;; If PREFER is neither `past' nor `future', select closest date.
  311. (should
  312. (equal
  313. '(3 1 2014)
  314. (calendar-gregorian-from-absolute
  315. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  316. (should
  317. (equal
  318. '(5 4 2014)
  319. (calendar-gregorian-from-absolute
  320. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  321. ;; Test "day" repeater.
  322. (should
  323. (equal '(3 8 2014)
  324. (calendar-gregorian-from-absolute
  325. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  326. (should
  327. (equal '(3 10 2014)
  328. (calendar-gregorian-from-absolute
  329. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  330. ;; Test "month" repeater.
  331. (should
  332. (equal '(1 5 2015)
  333. (calendar-gregorian-from-absolute
  334. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  335. (should
  336. (equal '(3 29 2014)
  337. (calendar-gregorian-from-absolute
  338. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  339. ;; Test "year" repeater.
  340. (should
  341. (equal '(3 5 2014)
  342. (calendar-gregorian-from-absolute
  343. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  344. (should
  345. (equal '(3 29 2014)
  346. (calendar-gregorian-from-absolute
  347. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  348. ;;; Drawers
  349. (ert-deftest test-org/insert-property-drawer ()
  350. "Test `org-insert-property-drawer' specifications."
  351. ;; Error before first headline.
  352. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  353. ;; Insert drawer right after headline if there is no planning line,
  354. ;; or after it otherwise.
  355. (should
  356. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  357. (org-test-with-temp-text "* H\nParagraph<point>"
  358. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  359. (buffer-string))))
  360. (should
  361. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  362. (org-test-with-temp-text
  363. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  364. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  365. (buffer-string))))
  366. ;; Indent inserted drawer.
  367. (should
  368. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  369. (org-test-with-temp-text "* H\nParagraph<point>"
  370. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  371. (buffer-string))))
  372. ;; Handle insertion at eob.
  373. (should
  374. (equal "* H\n:PROPERTIES:\n:END:\n"
  375. (org-test-with-temp-text "* H"
  376. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  377. (buffer-string))))
  378. ;; Skip inlinetasks before point.
  379. (when (featurep 'org-inlinetask)
  380. (should
  381. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  382. (org-test-with-temp-text
  383. "* H\n*************** I\n*************** END\nP<point>"
  384. (let ((org-adapt-indentation nil)
  385. (org-inlinetask-min-level 15))
  386. (org-insert-property-drawer))
  387. (buffer-string)))))
  388. ;; Correctly set drawer in an inlinetask.
  389. (when (featurep 'org-inlinetask)
  390. (should
  391. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  392. (org-test-with-temp-text
  393. "* H\n*************** I\nP<point>\n*************** END"
  394. (let ((org-adapt-indentation nil)
  395. (org-inlinetask-min-level 15))
  396. (org-insert-property-drawer))
  397. (buffer-string))))))
  398. ;;; Filling
  399. (ert-deftest test-org/fill-paragraph ()
  400. "Test `org-fill-paragraph' specifications."
  401. ;; At an Org table, align it.
  402. (should
  403. (equal "| a |\n"
  404. (org-test-with-temp-text "|a|"
  405. (org-fill-paragraph)
  406. (buffer-string))))
  407. (should
  408. (equal "#+name: table\n| a |\n"
  409. (org-test-with-temp-text "#+name: table\n| a |\n"
  410. (org-fill-paragraph)
  411. (buffer-string))))
  412. ;; At a paragraph, preserve line breaks.
  413. (org-test-with-temp-text "some \\\\\nlong\ntext"
  414. (let ((fill-column 20))
  415. (org-fill-paragraph)
  416. (should (equal (buffer-string) "some \\\\\nlong text"))))
  417. ;; Correctly fill a paragraph when point is at its very end.
  418. (should
  419. (equal "A B"
  420. (org-test-with-temp-text "A\nB"
  421. (let ((fill-column 20))
  422. (goto-char (point-max))
  423. (org-fill-paragraph)
  424. (buffer-string)))))
  425. ;; Correctly fill the last paragraph of a greater element.
  426. (should
  427. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  428. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  429. (let ((fill-column 8))
  430. (forward-line)
  431. (end-of-line)
  432. (org-fill-paragraph)
  433. (buffer-string)))))
  434. ;; Correctly fill an element in a narrowed buffer.
  435. (should
  436. (equal "01234\n6"
  437. (org-test-with-temp-text "01234 6789"
  438. (let ((fill-column 5))
  439. (narrow-to-region 1 8)
  440. (org-fill-paragraph)
  441. (buffer-string)))))
  442. ;; Handle `adaptive-fill-regexp' in paragraphs.
  443. (should
  444. (equal "> a b"
  445. (org-test-with-temp-text "> a\n> b"
  446. (let ((fill-column 5)
  447. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  448. (org-fill-paragraph)
  449. (buffer-string)))))
  450. ;; Special case: Fill first paragraph when point is at an item or
  451. ;; a plain-list or a footnote reference.
  452. (should
  453. (equal "- A B"
  454. (org-test-with-temp-text "- A\n B"
  455. (let ((fill-column 20))
  456. (org-fill-paragraph)
  457. (buffer-string)))))
  458. (should
  459. (equal "[fn:1] A B"
  460. (org-test-with-temp-text "[fn:1] A\nB"
  461. (let ((fill-column 20))
  462. (org-fill-paragraph)
  463. (buffer-string)))))
  464. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  465. (let ((fill-column 20))
  466. (org-fill-paragraph)
  467. (should (equal (buffer-string)
  468. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  469. ;; Fill contents of `comment-block' elements.
  470. (should
  471. (equal
  472. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  473. (let ((fill-column 20))
  474. (forward-line)
  475. (org-fill-paragraph)
  476. (buffer-string)))
  477. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  478. ;; Fill `comment' elements.
  479. (should
  480. (equal " # A B"
  481. (org-test-with-temp-text " # A\n # B"
  482. (let ((fill-column 20))
  483. (org-fill-paragraph)
  484. (buffer-string)))))
  485. ;; Do not mix consecutive comments when filling one of them.
  486. (should
  487. (equal "# A B\n\n# C"
  488. (org-test-with-temp-text "# A\n# B\n\n# C"
  489. (let ((fill-column 20))
  490. (org-fill-paragraph)
  491. (buffer-string)))))
  492. ;; Use commented empty lines as separators when filling comments.
  493. (should
  494. (equal "# A B\n#\n# C"
  495. (org-test-with-temp-text "# A\n# B\n#\n# C"
  496. (let ((fill-column 20))
  497. (org-fill-paragraph)
  498. (buffer-string)))))
  499. ;; Handle `adaptive-fill-regexp' in comments.
  500. (should
  501. (equal "# > a b"
  502. (org-test-with-temp-text "# > a\n# > b"
  503. (let ((fill-column 20)
  504. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  505. (org-fill-paragraph)
  506. (buffer-string)))))
  507. ;; Do nothing at affiliated keywords.
  508. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  509. (let ((fill-column 20))
  510. (org-fill-paragraph)
  511. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  512. ;; Do not move point after table when filling a table.
  513. (should-not
  514. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  515. (forward-char)
  516. (org-fill-paragraph)
  517. (eobp))))
  518. (ert-deftest test-org/auto-fill-function ()
  519. "Test auto-filling features."
  520. ;; Auto fill paragraph.
  521. (should
  522. (equal "12345\n7890"
  523. (org-test-with-temp-text "12345 7890"
  524. (let ((fill-column 5))
  525. (end-of-line)
  526. (org-auto-fill-function)
  527. (buffer-string)))))
  528. ;; Auto fill first paragraph in an item.
  529. (should
  530. (equal "- 12345\n 7890"
  531. (org-test-with-temp-text "- 12345 7890"
  532. (let ((fill-column 7))
  533. (end-of-line)
  534. (org-auto-fill-function)
  535. (buffer-string)))))
  536. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  537. (should
  538. (equal "> 12345\n 7890"
  539. (org-test-with-temp-text "> 12345 7890"
  540. (let ((fill-column 10)
  541. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  542. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  543. (end-of-line)
  544. (org-auto-fill-function)
  545. (buffer-string)))))
  546. (should
  547. (equal "> 12345\n> 12345\n> 7890"
  548. (org-test-with-temp-text "> 12345\n> 12345 7890"
  549. (let ((fill-column 10)
  550. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  551. (goto-char (point-max))
  552. (org-auto-fill-function)
  553. (buffer-string)))))
  554. (should-not
  555. (equal " 12345\n *12345\n *12345"
  556. (org-test-with-temp-text " 12345\n *12345 12345"
  557. (let ((fill-column 10)
  558. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  559. (goto-char (point-max))
  560. (org-auto-fill-function)
  561. (buffer-string)))))
  562. ;; Auto fill comments.
  563. (should
  564. (equal " # 12345\n # 7890"
  565. (org-test-with-temp-text " # 12345 7890"
  566. (let ((fill-column 10))
  567. (end-of-line)
  568. (org-auto-fill-function)
  569. (buffer-string)))))
  570. ;; A hash within a line isn't a comment.
  571. (should-not
  572. (equal "12345 # 7890\n# 1"
  573. (org-test-with-temp-text "12345 # 7890 1"
  574. (let ((fill-column 12))
  575. (end-of-line)
  576. (org-auto-fill-function)
  577. (buffer-string)))))
  578. ;; Correctly interpret empty prefix.
  579. (should-not
  580. (equal "# a\n# b\nRegular\n# paragraph"
  581. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  582. (let ((fill-column 12))
  583. (end-of-line 3)
  584. (org-auto-fill-function)
  585. (buffer-string)))))
  586. ;; Comment block: auto fill contents.
  587. (should
  588. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  589. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  590. (let ((fill-column 5))
  591. (forward-line)
  592. (end-of-line)
  593. (org-auto-fill-function)
  594. (buffer-string)))))
  595. (should
  596. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  597. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  598. (let ((fill-column 5))
  599. (forward-line)
  600. (end-of-line)
  601. (org-auto-fill-function)
  602. (buffer-string)))))
  603. ;; Do not fill if a new item could be created.
  604. (should-not
  605. (equal "12345\n- 90"
  606. (org-test-with-temp-text "12345 - 90"
  607. (let ((fill-column 5))
  608. (end-of-line)
  609. (org-auto-fill-function)
  610. (buffer-string)))))
  611. ;; Do not fill if a line break could be introduced.
  612. (should-not
  613. (equal "123\\\\\n7890"
  614. (org-test-with-temp-text "123\\\\ 7890"
  615. (let ((fill-column 6))
  616. (end-of-line)
  617. (org-auto-fill-function)
  618. (buffer-string)))))
  619. ;; Do not fill affiliated keywords.
  620. (should-not
  621. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  622. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  623. (let ((fill-column 20))
  624. (end-of-line)
  625. (org-auto-fill-function)
  626. (buffer-string))))))
  627. ;;; Indentation
  628. (ert-deftest test-org/indent-line ()
  629. "Test `org-indent-line' specifications."
  630. ;; Do not indent diary sexps, footnote definitions or headlines.
  631. (should
  632. (zerop
  633. (org-test-with-temp-text "%%(org-calendar-holiday)"
  634. (org-indent-line)
  635. (org-get-indentation))))
  636. (should
  637. (zerop
  638. (org-test-with-temp-text "[fn:1] fn"
  639. (let ((org-adapt-indentation t)) (org-indent-line))
  640. (org-get-indentation))))
  641. (should
  642. (zerop
  643. (org-test-with-temp-text "* H"
  644. (org-indent-line)
  645. (org-get-indentation))))
  646. ;; Do not indent before first headline.
  647. (should
  648. (zerop
  649. (org-test-with-temp-text ""
  650. (org-indent-line)
  651. (org-get-indentation))))
  652. ;; Indent according to headline level otherwise, unless
  653. ;; `org-adapt-indentation' is nil.
  654. (should
  655. (= 2
  656. (org-test-with-temp-text "* H\nA"
  657. (forward-line)
  658. (let ((org-adapt-indentation t)) (org-indent-line))
  659. (org-get-indentation))))
  660. (should
  661. (= 2
  662. (org-test-with-temp-text "* H\n\nA"
  663. (forward-line)
  664. (let ((org-adapt-indentation t)) (org-indent-line))
  665. (org-get-indentation))))
  666. (should
  667. (zerop
  668. (org-test-with-temp-text "* H\nA"
  669. (forward-line)
  670. (let ((org-adapt-indentation nil)) (org-indent-line))
  671. (org-get-indentation))))
  672. ;; Indenting preserves point position.
  673. (should
  674. (org-test-with-temp-text "* H\nAB"
  675. (forward-line)
  676. (forward-char)
  677. (let ((org-adapt-indentation t)) (org-indent-line))
  678. (looking-at "B")))
  679. ;; Do not change indentation at an item.
  680. (should
  681. (= 1
  682. (org-test-with-temp-text "* H\n - A"
  683. (forward-line)
  684. (let ((org-adapt-indentation t)) (org-indent-line))
  685. (org-get-indentation))))
  686. ;; On blank lines at the end of a list, indent like last element
  687. ;; within it if the line is still in the list. If the last element
  688. ;; is an item, indent like its contents. Otherwise, indent like the
  689. ;; whole list.
  690. (should
  691. (= 4
  692. (org-test-with-temp-text "* H\n- A\n - AA\n"
  693. (goto-char (point-max))
  694. (let ((org-adapt-indentation t)) (org-indent-line))
  695. (org-get-indentation))))
  696. (should
  697. (zerop
  698. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n"
  699. (goto-char (point-max))
  700. (let ((org-adapt-indentation t)) (org-indent-line))
  701. (org-get-indentation))))
  702. (should
  703. (= 4
  704. (org-test-with-temp-text "* H\n- A\n - \n"
  705. (goto-char (point-max))
  706. (let ((org-adapt-indentation t)) (org-indent-line))
  707. (org-get-indentation))))
  708. ;; Likewise, on a blank line at the end of a footnote definition,
  709. ;; indent at column 0 if line belongs to the definition. Otherwise,
  710. ;; indent like the definition itself.
  711. (should
  712. (zerop
  713. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  714. (goto-char (point-max))
  715. (let ((org-adapt-indentation t)) (org-indent-line))
  716. (org-get-indentation))))
  717. (should
  718. (zerop
  719. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  720. (goto-char (point-max))
  721. (let ((org-adapt-indentation t)) (org-indent-line))
  722. (org-get-indentation))))
  723. ;; After the end of the contents of a greater element, indent like
  724. ;; the beginning of the element.
  725. (should
  726. (= 1
  727. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  728. (forward-line 2)
  729. (org-indent-line)
  730. (org-get-indentation))))
  731. ;; On blank lines after a paragraph, indent like its last non-empty
  732. ;; line.
  733. (should
  734. (= 1
  735. (org-test-with-temp-text " Paragraph\n\n<point>"
  736. (org-indent-line)
  737. (org-get-indentation))))
  738. ;; At the first line of an element, indent like previous element's
  739. ;; first line, ignoring footnotes definitions and inline tasks, or
  740. ;; according to parent.
  741. (should
  742. (= 2
  743. (org-test-with-temp-text "A\n\n B\n\nC"
  744. (goto-char (point-max))
  745. (org-indent-line)
  746. (org-get-indentation))))
  747. (should
  748. (= 1
  749. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  750. (goto-char (point-max))
  751. (org-indent-line)
  752. (org-get-indentation))))
  753. (should
  754. (= 1
  755. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  756. (forward-line 1)
  757. (org-indent-line)
  758. (org-get-indentation))))
  759. ;; Within code part of a source block, use language major mode if
  760. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  761. ;; according to line above.
  762. (should
  763. (= 6
  764. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  765. (forward-line 2)
  766. (let ((org-src-tab-acts-natively t)
  767. (org-edit-src-content-indentation 0))
  768. (org-indent-line))
  769. (org-get-indentation))))
  770. (should
  771. (= 1
  772. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  773. (forward-line 2)
  774. (let ((org-src-tab-acts-natively nil)
  775. (org-edit-src-content-indentation 0))
  776. (org-indent-line))
  777. (org-get-indentation))))
  778. ;; Otherwise, indent like the first non-blank line above.
  779. (should
  780. (zerop
  781. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  782. (forward-line 3)
  783. (org-indent-line)
  784. (org-get-indentation))))
  785. ;; Align node properties according to `org-property-format'. Handle
  786. ;; nicely empty values.
  787. (should
  788. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  789. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  790. (let ((org-property-format "%-10s %s")) (org-indent-line))
  791. (buffer-string))))
  792. (should
  793. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  794. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  795. (let ((org-property-format "%-10s %s")) (org-indent-line))
  796. (buffer-string)))))
  797. (ert-deftest test-org/indent-region ()
  798. "Test `org-indent-region' specifications."
  799. ;; Indent paragraph.
  800. (should
  801. (equal "A\nB\nC"
  802. (org-test-with-temp-text " A\nB\n C"
  803. (org-indent-region (point-min) (point-max))
  804. (buffer-string))))
  805. ;; Indent greater elements along with their contents.
  806. (should
  807. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  808. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  809. (org-indent-region (point-min) (point-max))
  810. (buffer-string))))
  811. ;; Ignore contents of verse blocks and example blocks.
  812. (should
  813. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  814. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  815. (org-indent-region (point-min) (point-max))
  816. (buffer-string))))
  817. (should
  818. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  819. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  820. (org-indent-region (point-min) (point-max))
  821. (buffer-string))))
  822. ;; Indent according to mode if `org-src-tab-acts-natively' is
  823. ;; non-nil. Otherwise, do not indent code at all.
  824. (should
  825. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  826. (org-test-with-temp-text
  827. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  828. (let ((org-src-tab-acts-natively t)
  829. (org-edit-src-content-indentation 0))
  830. (org-indent-region (point-min) (point-max)))
  831. (buffer-string))))
  832. (should
  833. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  834. (org-test-with-temp-text
  835. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  836. (let ((org-src-tab-acts-natively nil)
  837. (org-edit-src-content-indentation 0))
  838. (org-indent-region (point-min) (point-max)))
  839. (buffer-string))))
  840. ;; Align node properties according to `org-property-format'. Handle
  841. ;; nicely empty values.
  842. (should
  843. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  844. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  845. (let ((org-property-format "%-10s %s")
  846. (org-adapt-indentation nil))
  847. (org-indent-region (point) (point-max)))
  848. (buffer-string))))
  849. (should
  850. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  851. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  852. (let ((org-property-format "%-10s %s")
  853. (org-adapt-indentation nil))
  854. (org-indent-region (point) (point-max)))
  855. (buffer-string))))
  856. ;; Indent plain lists.
  857. (should
  858. (equal "- A\n B\n - C\n\n D"
  859. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  860. (org-indent-region (point-min) (point-max))
  861. (buffer-string))))
  862. (should
  863. (equal "- A\n\n- B"
  864. (org-test-with-temp-text " - A\n\n - B"
  865. (org-indent-region (point-min) (point-max))
  866. (buffer-string))))
  867. ;; Indent footnote definitions.
  868. (should
  869. (equal "[fn:1] Definition\n\nDefinition"
  870. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  871. (org-indent-region (point-min) (point-max))
  872. (buffer-string))))
  873. ;; Special case: Start indenting on a blank line.
  874. (should
  875. (equal "\nParagraph"
  876. (org-test-with-temp-text "\n Paragraph"
  877. (org-indent-region (point-min) (point-max))
  878. (buffer-string)))))
  879. ;;; Editing
  880. (ert-deftest test-org/delete-indentation ()
  881. "Test `org-delete-indentation' specifications."
  882. ;; Regular test.
  883. (should (equal "foo bar"
  884. (org-test-with-temp-text
  885. "foo \n bar<point>"
  886. (org-delete-indentation)
  887. (buffer-string))))
  888. ;; With optional argument.
  889. (should (equal "foo bar"
  890. (org-test-with-temp-text
  891. "foo<point> \n bar"
  892. (org-delete-indentation t)
  893. (buffer-string))))
  894. ;; At headline text should be appended to the headline text.
  895. (should
  896. (equal"* foo bar :tag:"
  897. (let (org-auto-align-tags)
  898. (org-test-with-temp-text
  899. "* foo :tag:\n bar<point>"
  900. (org-delete-indentation)
  901. (buffer-string)))))
  902. (should
  903. (equal "* foo bar :tag:"
  904. (let (org-auto-align-tags)
  905. (org-test-with-temp-text
  906. "* foo <point>:tag:\n bar"
  907. (org-delete-indentation t)
  908. (buffer-string))))))
  909. (ert-deftest test-org/return ()
  910. "Test `org-return' specifications."
  911. ;; Regular test.
  912. (should
  913. (equal "Para\ngraph"
  914. (org-test-with-temp-text "Para<point>graph"
  915. (org-return)
  916. (buffer-string))))
  917. ;; With optional argument, indent line.
  918. (should
  919. (equal " Para\n graph"
  920. (org-test-with-temp-text " Para<point>graph"
  921. (org-return t)
  922. (buffer-string))))
  923. ;; On a table, call `org-table-next-row'.
  924. (should
  925. (org-test-with-temp-text "| <point>a |\n| b |"
  926. (org-return)
  927. (org-looking-at-p "b")))
  928. ;; Open link or timestamp under point when `org-return-follows-link'
  929. ;; is non-nil.
  930. (should
  931. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  932. (let ((org-return-follows-link t)
  933. (org-link-search-must-match-exact-headline nil))
  934. (org-return))
  935. (org-looking-at-p "<<target>>")))
  936. (should-not
  937. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  938. (let ((org-return-follows-link nil)) (org-return))
  939. (org-looking-at-p "<<target>>")))
  940. (should
  941. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  942. (let ((org-return-follows-link t)) (org-return))
  943. (org-looking-at-p "* b")))
  944. (should
  945. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  946. (let ((org-return-follows-link t)
  947. (org-link-search-must-match-exact-headline nil))
  948. (org-return))
  949. (org-looking-at-p "<<target>>")))
  950. ;; When `org-return-follows-link' is non-nil, tolerate links and
  951. ;; timestamps in comments, node properties, etc.
  952. (should
  953. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  954. (let ((org-return-follows-link t)
  955. (org-link-search-must-match-exact-headline nil))
  956. (org-return))
  957. (org-looking-at-p "<<target>>")))
  958. ;; However, do not open link when point is in a table.
  959. (should
  960. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  961. (let ((org-return-follows-link t)) (org-return))
  962. (org-looking-at-p "between")))
  963. ;; Special case: in a list, when indenting, do not break structure.
  964. (should
  965. (equal "- A\n B"
  966. (org-test-with-temp-text "- A <point>B"
  967. (org-return t)
  968. (buffer-string))))
  969. (should
  970. (equal "- A\n\n- B"
  971. (org-test-with-temp-text "- A\n<point>- B"
  972. (org-return t)
  973. (buffer-string))))
  974. ;; On tags part of a headline, add a newline below it instead of
  975. ;; breaking it.
  976. (should
  977. (equal "* H :tag:\n"
  978. (org-test-with-temp-text "* H :<point>tag:"
  979. (org-return)
  980. (buffer-string))))
  981. ;; Before headline text, add a newline below it instead of breaking
  982. ;; it.
  983. (should
  984. (equal "* TODO H :tag:\n"
  985. (org-test-with-temp-text "* <point>TODO H :tag:"
  986. (org-return)
  987. (buffer-string))))
  988. (should
  989. (equal "* TODO [#B] H :tag:\n"
  990. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  991. (org-return)
  992. (buffer-string))))
  993. ;; At headline text, break headline text but preserve tags.
  994. (should
  995. (equal "* TODO [#B] foo :tag:\nbar"
  996. (let (org-auto-align-tags)
  997. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  998. (org-return)
  999. (buffer-string)))))
  1000. ;; At bol of headline insert newline.
  1001. (should
  1002. (equal "\n* h"
  1003. (org-test-with-temp-text "<point>* h"
  1004. (org-return)
  1005. (buffer-string))))
  1006. ;; Refuse to leave invalid headline in buffer.
  1007. (should
  1008. (equal "* h\n"
  1009. (org-test-with-temp-text "*<point> h"
  1010. (org-return)
  1011. (buffer-string)))))
  1012. (ert-deftest test-org/meta-return ()
  1013. "Test M-RET (`org-meta-return') specifications."
  1014. ;; In a table field insert a row above.
  1015. (should
  1016. (org-test-with-temp-text "| a |"
  1017. (forward-char)
  1018. (org-meta-return)
  1019. (forward-line -1)
  1020. (looking-at "| |$")))
  1021. ;; In a paragraph change current line into a header.
  1022. (should
  1023. (org-test-with-temp-text "a"
  1024. (org-meta-return)
  1025. (beginning-of-line)
  1026. (looking-at "\* a$")))
  1027. ;; In an item insert an item, in this case above.
  1028. (should
  1029. (org-test-with-temp-text "- a"
  1030. (org-meta-return)
  1031. (beginning-of-line)
  1032. (looking-at "- $")))
  1033. ;; In a drawer and item insert an item, in this case above.
  1034. (should
  1035. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1036. (forward-line)
  1037. (org-meta-return)
  1038. (beginning-of-line)
  1039. (looking-at "- $"))))
  1040. (ert-deftest test-org/insert-heading ()
  1041. "Test `org-insert-heading' specifications."
  1042. ;; In an empty buffer, insert a new headline.
  1043. (should
  1044. (equal "* "
  1045. (org-test-with-temp-text ""
  1046. (org-insert-heading)
  1047. (buffer-string))))
  1048. ;; At the beginning of a line, turn it into a headline
  1049. (should
  1050. (equal "* P"
  1051. (org-test-with-temp-text "<point>P"
  1052. (org-insert-heading)
  1053. (buffer-string))))
  1054. ;; In the middle of a line, split the line if allowed, otherwise,
  1055. ;; insert the headline at its end.
  1056. (should
  1057. (equal "Para\n* graph"
  1058. (org-test-with-temp-text "Para<point>graph"
  1059. (let ((org-M-RET-may-split-line '((default . t))))
  1060. (org-insert-heading))
  1061. (buffer-string))))
  1062. (should
  1063. (equal "Paragraph\n* "
  1064. (org-test-with-temp-text "Para<point>graph"
  1065. (let ((org-M-RET-may-split-line '((default . nil))))
  1066. (org-insert-heading))
  1067. (buffer-string))))
  1068. ;; When on a list, insert an item instead, unless called with an
  1069. ;; universal argument or if list is invisible. In this case, create
  1070. ;; a new headline after contents.
  1071. (should
  1072. (equal "* H\n- item\n- "
  1073. (org-test-with-temp-text "* H\n- item<point>"
  1074. (let ((org-insert-heading-respect-content nil))
  1075. (org-insert-heading))
  1076. (buffer-string))))
  1077. (should
  1078. (equal "* H\n- item\n- item 2\n* "
  1079. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  1080. (let ((org-insert-heading-respect-content nil))
  1081. (org-insert-heading '(4)))
  1082. (buffer-string))))
  1083. (should
  1084. (equal "* H\n- item\n* "
  1085. (org-test-with-temp-text "* H\n- item"
  1086. (org-cycle)
  1087. (goto-char (point-max))
  1088. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1089. (buffer-string))))
  1090. ;; When called with two universal arguments, insert a new headline
  1091. ;; at the end of the grandparent subtree.
  1092. (should
  1093. (equal "* H1\n** H3\n- item\n** H2\n** "
  1094. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1095. (let ((org-insert-heading-respect-content nil))
  1096. (org-insert-heading '(16)))
  1097. (buffer-string))))
  1098. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1099. ;; a level 1 heading.
  1100. (should
  1101. (equal "* H1\n** H2\n* "
  1102. (org-test-with-temp-text "* H1\n** H2<point>"
  1103. (org-insert-heading nil nil t)
  1104. (buffer-string))))
  1105. (should
  1106. (equal "* H1\n- item\n* "
  1107. (org-test-with-temp-text "* H1\n- item<point>"
  1108. (org-insert-heading nil nil t)
  1109. (buffer-string))))
  1110. ;; Corner case: correctly insert a headline after an empty one.
  1111. (should
  1112. (equal "* \n* "
  1113. (org-test-with-temp-text "* <point>"
  1114. (org-insert-heading)
  1115. (buffer-string)))))
  1116. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1117. "Test `org-insert-todo-heading-respect-content' specifications."
  1118. ;; Create a TODO heading.
  1119. (should
  1120. (org-test-with-temp-text "* H1\n Body"
  1121. (org-insert-todo-heading-respect-content)
  1122. (nth 2 (org-heading-components))))
  1123. ;; Add headline at the end of the first subtree
  1124. (should
  1125. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  1126. (search-forward "H1Body")
  1127. (org-insert-todo-heading-respect-content)
  1128. (and (eobp) (org-at-heading-p))))
  1129. ;; In a list, do not create a new item.
  1130. (should
  1131. (org-test-with-temp-text "* H\n- an item\n- another one"
  1132. (search-forward "an ")
  1133. (org-insert-todo-heading-respect-content)
  1134. (and (eobp) (org-at-heading-p)))))
  1135. (ert-deftest test-org/clone-with-time-shift ()
  1136. "Test `org-clone-subtree-with-time-shift'."
  1137. ;; Clone non-repeating once.
  1138. (should
  1139. (equal "\
  1140. * H1\n<2015-06-21>
  1141. * H1\n<2015-06-23>
  1142. "
  1143. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1144. (org-clone-subtree-with-time-shift 1 "+2d")
  1145. (replace-regexp-in-string
  1146. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1147. nil nil 1))))
  1148. ;; Clone repeating once.
  1149. (should
  1150. (equal "\
  1151. * H1\n<2015-06-21>
  1152. * H1\n<2015-06-23>
  1153. * H1\n<2015-06-25 +1w>
  1154. "
  1155. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1156. (org-clone-subtree-with-time-shift 1 "+2d")
  1157. (replace-regexp-in-string
  1158. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1159. nil nil 1))))
  1160. ;; Clone non-repeating zero times.
  1161. (should
  1162. (equal "\
  1163. * H1\n<2015-06-21>
  1164. "
  1165. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1166. (org-clone-subtree-with-time-shift 0 "+2d")
  1167. (replace-regexp-in-string
  1168. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1169. nil nil 1))))
  1170. ;; Clone repeating "zero" times.
  1171. (should
  1172. (equal "\
  1173. * H1\n<2015-06-21>
  1174. * H1\n<2015-06-23 +1w>
  1175. "
  1176. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1177. (org-clone-subtree-with-time-shift 0 "+2d")
  1178. (replace-regexp-in-string
  1179. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1180. nil nil 1)))))
  1181. ;;; Fixed-Width Areas
  1182. (ert-deftest test-org/toggle-fixed-width ()
  1183. "Test `org-toggle-fixed-width' specifications."
  1184. ;; No region: Toggle on fixed-width marker in paragraphs.
  1185. (should
  1186. (equal ": A"
  1187. (org-test-with-temp-text "A"
  1188. (org-toggle-fixed-width)
  1189. (buffer-string))))
  1190. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1191. (should
  1192. (equal "A"
  1193. (org-test-with-temp-text ": A"
  1194. (org-toggle-fixed-width)
  1195. (buffer-string))))
  1196. ;; No region: Toggle on marker in blank lines after elements or just
  1197. ;; after a headline.
  1198. (should
  1199. (equal "* H\n: "
  1200. (org-test-with-temp-text "* H\n"
  1201. (forward-line)
  1202. (org-toggle-fixed-width)
  1203. (buffer-string))))
  1204. (should
  1205. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1206. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1207. (goto-char (point-max))
  1208. (org-toggle-fixed-width)
  1209. (buffer-string))))
  1210. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1211. ;; headlines, clocks)
  1212. (should
  1213. (equal ": * Headline"
  1214. (org-test-with-temp-text "* Headline"
  1215. (org-toggle-fixed-width)
  1216. (buffer-string))))
  1217. (should
  1218. (equal ": #+KEYWORD: value"
  1219. (org-test-with-temp-text "#+KEYWORD: value"
  1220. (org-toggle-fixed-width)
  1221. (buffer-string))))
  1222. ;; No region: error in other situations.
  1223. (should-error
  1224. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1225. (forward-line)
  1226. (org-toggle-fixed-width)
  1227. (buffer-string)))
  1228. ;; No region: Indentation is preserved.
  1229. (should
  1230. (equal "- A\n : B"
  1231. (org-test-with-temp-text "- A\n B"
  1232. (forward-line)
  1233. (org-toggle-fixed-width)
  1234. (buffer-string))))
  1235. ;; Region: If it contains only fixed-width elements and blank lines,
  1236. ;; toggle off fixed-width markup.
  1237. (should
  1238. (equal "A\n\nB"
  1239. (org-test-with-temp-text ": A\n\n: B"
  1240. (transient-mark-mode 1)
  1241. (push-mark (point) t t)
  1242. (goto-char (point-max))
  1243. (org-toggle-fixed-width)
  1244. (buffer-string))))
  1245. ;; Region: If it contains anything else, toggle on fixed-width but
  1246. ;; not on fixed-width areas.
  1247. (should
  1248. (equal ": A\n: \n: B\n: \n: C"
  1249. (org-test-with-temp-text "A\n\n: B\n\nC"
  1250. (transient-mark-mode 1)
  1251. (push-mark (point) t t)
  1252. (goto-char (point-max))
  1253. (org-toggle-fixed-width)
  1254. (buffer-string))))
  1255. ;; Region: Ignore blank lines at its end, unless it contains only
  1256. ;; such lines.
  1257. (should
  1258. (equal ": A\n\n"
  1259. (org-test-with-temp-text "A\n\n"
  1260. (transient-mark-mode 1)
  1261. (push-mark (point) t t)
  1262. (goto-char (point-max))
  1263. (org-toggle-fixed-width)
  1264. (buffer-string))))
  1265. (should
  1266. (equal ": \n: \n"
  1267. (org-test-with-temp-text "\n\n"
  1268. (transient-mark-mode 1)
  1269. (push-mark (point) t t)
  1270. (goto-char (point-max))
  1271. (org-toggle-fixed-width)
  1272. (buffer-string)))))
  1273. ;;; Headline
  1274. (ert-deftest test-org/in-commented-heading-p ()
  1275. "Test `org-in-commented-heading-p' specifications."
  1276. ;; Commented headline.
  1277. (should
  1278. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1279. (goto-char (point-max))
  1280. (org-in-commented-heading-p)))
  1281. ;; Commented ancestor.
  1282. (should
  1283. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1284. (goto-char (point-max))
  1285. (org-in-commented-heading-p)))
  1286. ;; Comment keyword is case-sensitive.
  1287. (should-not
  1288. (org-test-with-temp-text "* Comment Headline\nBody"
  1289. (goto-char (point-max))
  1290. (org-in-commented-heading-p)))
  1291. ;; Keyword is standalone.
  1292. (should-not
  1293. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1294. (goto-char (point-max))
  1295. (org-in-commented-heading-p)))
  1296. ;; Optional argument.
  1297. (should-not
  1298. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1299. (goto-char (point-max))
  1300. (org-in-commented-heading-p t))))
  1301. (ert-deftest test-org/entry-blocked-p ()
  1302. ;; Check other dependencies.
  1303. (should
  1304. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1305. (let ((org-enforce-todo-dependencies t)
  1306. (org-blocker-hook
  1307. '(org-block-todo-from-children-or-siblings-or-parent)))
  1308. (org-entry-blocked-p))))
  1309. (should-not
  1310. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1311. (let ((org-enforce-todo-dependencies t)
  1312. (org-blocker-hook
  1313. '(org-block-todo-from-children-or-siblings-or-parent)))
  1314. (org-entry-blocked-p))))
  1315. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1316. ;; blocked.
  1317. (should-not
  1318. (org-test-with-temp-text "* Blocked\n** TODO one"
  1319. (let ((org-enforce-todo-dependencies t)
  1320. (org-blocker-hook
  1321. '(org-block-todo-from-children-or-siblings-or-parent)))
  1322. (org-entry-blocked-p))))
  1323. (should-not
  1324. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1325. (let ((org-enforce-todo-dependencies t)
  1326. (org-blocker-hook
  1327. '(org-block-todo-from-children-or-siblings-or-parent)))
  1328. (org-entry-blocked-p))))
  1329. ;; Follow :ORDERED: specifications.
  1330. (should
  1331. (org-test-with-temp-text
  1332. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1333. (let ((org-enforce-todo-dependencies t)
  1334. (org-blocker-hook
  1335. '(org-block-todo-from-children-or-siblings-or-parent)))
  1336. (org-entry-blocked-p))))
  1337. (should-not
  1338. (org-test-with-temp-text
  1339. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1340. (let ((org-enforce-todo-dependencies t)
  1341. (org-blocker-hook
  1342. '(org-block-todo-from-children-or-siblings-or-parent)))
  1343. (org-entry-blocked-p)))))
  1344. ;;; Keywords
  1345. (ert-deftest test-org/set-regexps-and-options ()
  1346. "Test `org-set-regexps-and-options' specifications."
  1347. ;; TAGS keyword.
  1348. (should
  1349. (equal '(("A" . ?a) ("B") ("C"))
  1350. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1351. (org-mode-restart)
  1352. org-tag-alist)))
  1353. (should
  1354. (equal '(("A") (:newline) ("B"))
  1355. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1356. (org-mode-restart)
  1357. org-tag-alist)))
  1358. (should
  1359. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1360. (org-test-with-temp-text "#+TAGS: { A B } C"
  1361. (org-mode-restart)
  1362. org-tag-alist)))
  1363. (should
  1364. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1365. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1366. (org-mode-restart)
  1367. org-tag-alist)))
  1368. (should
  1369. (equal '(("A" "B" "C"))
  1370. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1371. (org-mode-restart)
  1372. org-tag-groups-alist)))
  1373. (should
  1374. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1375. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1376. (org-mode-restart)
  1377. org-tag-alist)))
  1378. (should
  1379. (equal '(("A" "B" "C"))
  1380. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1381. (org-mode-restart)
  1382. org-tag-groups-alist)))
  1383. ;; FILETAGS keyword.
  1384. (should
  1385. (equal '("A" "B" "C")
  1386. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1387. (org-mode-restart)
  1388. org-file-tags)))
  1389. ;; PROPERTY keyword. Property names are case-insensitive.
  1390. (should
  1391. (equal "foo=1"
  1392. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1393. (org-mode-restart)
  1394. (cdr (assoc "var" org-file-properties)))))
  1395. (should
  1396. (equal
  1397. "foo=1 bar=2"
  1398. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1399. (org-mode-restart)
  1400. (cdr (assoc "var" org-file-properties)))))
  1401. (should
  1402. (equal
  1403. "foo=1 bar=2"
  1404. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1405. (org-mode-restart)
  1406. (cdr (assoc "var" org-file-properties)))))
  1407. ;; ARCHIVE keyword.
  1408. (should
  1409. (equal "%s_done::"
  1410. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1411. (org-mode-restart)
  1412. org-archive-location)))
  1413. ;; CATEGORY keyword.
  1414. (should
  1415. (eq 'test
  1416. (org-test-with-temp-text "#+CATEGORY: test"
  1417. (org-mode-restart)
  1418. org-category)))
  1419. (should
  1420. (equal "test"
  1421. (org-test-with-temp-text "#+CATEGORY: test"
  1422. (org-mode-restart)
  1423. (cdr (assoc "CATEGORY" org-file-properties)))))
  1424. ;; COLUMNS keyword.
  1425. (should
  1426. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1427. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1428. (org-mode-restart)
  1429. org-columns-default-format)))
  1430. ;; CONSTANTS keyword. Constants names are case sensitive.
  1431. (should
  1432. (equal '("299792458." "3.14")
  1433. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1434. (org-mode-restart)
  1435. (mapcar
  1436. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1437. '("c" "pi")))))
  1438. (should
  1439. (equal "3.14"
  1440. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1441. (org-mode-restart)
  1442. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1443. (should
  1444. (equal "22/7"
  1445. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1446. (org-mode-restart)
  1447. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1448. ;; LINK keyword.
  1449. (should
  1450. (equal
  1451. '("url1" "url2")
  1452. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1453. (org-mode-restart)
  1454. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1455. '("a" "b")))))
  1456. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1457. (should
  1458. (equal
  1459. '(?X ?Z ?Y)
  1460. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1461. (org-mode-restart)
  1462. (list org-highest-priority org-lowest-priority org-default-priority))))
  1463. (should
  1464. (equal
  1465. '(?A ?C ?B)
  1466. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1467. (org-mode-restart)
  1468. (list org-highest-priority org-lowest-priority org-default-priority))))
  1469. ;; STARTUP keyword.
  1470. (should
  1471. (equal '(t t)
  1472. (org-test-with-temp-text "#+STARTUP: fold odd"
  1473. (org-mode-restart)
  1474. (list org-startup-folded org-odd-levels-only))))
  1475. ;; TODO keywords.
  1476. (should
  1477. (equal '(("A" "B") ("C"))
  1478. (org-test-with-temp-text "#+TODO: A B | C"
  1479. (org-mode-restart)
  1480. (list org-not-done-keywords org-done-keywords))))
  1481. (should
  1482. (equal '(("A" "C") ("B" "D"))
  1483. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1484. (org-mode-restart)
  1485. (list org-not-done-keywords org-done-keywords))))
  1486. (should
  1487. (equal '(("A" "B") ("C"))
  1488. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1489. (org-mode-restart)
  1490. (list org-not-done-keywords org-done-keywords))))
  1491. (should
  1492. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1493. (org-test-with-temp-text "#+TODO: A(a)"
  1494. (org-mode-restart)
  1495. org-todo-key-alist)))
  1496. (should
  1497. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1498. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1499. (org-mode-restart)
  1500. org-todo-log-states)))
  1501. ;; Enter SETUPFILE keyword.
  1502. (should
  1503. (equal "1"
  1504. (org-test-with-temp-text
  1505. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1506. (org-mode-restart)
  1507. (cdr (assoc "a" org-file-properties))))))
  1508. ;;; Links
  1509. ;;;; Coderefs
  1510. (ert-deftest test-org/coderef ()
  1511. "Test coderef links specifications."
  1512. (should
  1513. (org-test-with-temp-text "
  1514. #+BEGIN_SRC emacs-lisp
  1515. \(+ 1 1) (ref:sc)
  1516. #+END_SRC
  1517. \[[(sc)]]<point>"
  1518. (org-open-at-point)
  1519. (looking-at "(ref:sc)")))
  1520. ;; Find coderef even with alternate label format.
  1521. (should
  1522. (org-test-with-temp-text "
  1523. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  1524. \(+ 1 1) {ref:sc}
  1525. #+END_SRC
  1526. \[[(sc)]]<point>"
  1527. (org-open-at-point)
  1528. (looking-at "{ref:sc}"))))
  1529. ;;;; Custom ID
  1530. (ert-deftest test-org/custom-id ()
  1531. "Test custom ID links specifications."
  1532. (should
  1533. (org-test-with-temp-text
  1534. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
  1535. (org-open-at-point)
  1536. (org-looking-at-p "\\* H1")))
  1537. ;; Throw an error on false positives.
  1538. (should-error
  1539. (org-test-with-temp-text
  1540. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom]]<point>"
  1541. (org-open-at-point)
  1542. (org-looking-at-p "\\* H1"))))
  1543. ;;;; Fuzzy Links
  1544. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1545. ;; a named element (#+name: text) and to headlines (* Text).
  1546. (ert-deftest test-org/fuzzy-links ()
  1547. "Test fuzzy links specifications."
  1548. ;; Fuzzy link goes in priority to a matching target.
  1549. (should
  1550. (org-test-with-temp-text
  1551. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  1552. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1553. (looking-at "<<Test>>")))
  1554. ;; Then fuzzy link points to an element with a given name.
  1555. (should
  1556. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  1557. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1558. (looking-at "#\\+NAME: Test")))
  1559. ;; A target still lead to a matching headline otherwise.
  1560. (should
  1561. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  1562. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1563. (looking-at "\\* Head2")))
  1564. ;; With a leading star in link, enforce heading match.
  1565. (should
  1566. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  1567. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1568. (looking-at "\\* Test")))
  1569. ;; With a leading star in link, enforce exact heading match, even
  1570. ;; with `org-link-search-must-match-exact-headline' set to nil.
  1571. (should-error
  1572. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  1573. (let ((org-link-search-must-match-exact-headline nil))
  1574. (org-open-at-point))))
  1575. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  1576. (should
  1577. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  1578. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  1579. (looking-at "\\* Test")))
  1580. (should
  1581. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  1582. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  1583. (looking-at "\\* Test")))
  1584. ;; Heading match should not care about spaces, cookies, TODO
  1585. ;; keywords, priorities, and tags.
  1586. (should
  1587. (let ((first-line
  1588. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1589. (org-test-with-temp-text
  1590. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  1591. (let ((org-link-search-must-match-exact-headline nil)
  1592. (org-todo-regexp "TODO"))
  1593. (org-open-at-point))
  1594. (looking-at (regexp-quote first-line)))))
  1595. ;; Heading match should still be exact.
  1596. (should-error
  1597. (let ((first-line
  1598. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1599. (org-test-with-temp-text
  1600. (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
  1601. (let ((org-link-search-must-match-exact-headline nil)
  1602. (org-todo-regexp "TODO"))
  1603. (org-open-at-point)))))
  1604. ;; Heading match ignores COMMENT keyword.
  1605. (should
  1606. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  1607. (org-open-at-point)
  1608. (looking-at "\\* COMMENT Test")))
  1609. ;; Correctly un-hexify fuzzy links.
  1610. (should
  1611. (org-test-with-temp-text "* With space\n[[*With%20space][With space]]"
  1612. (goto-char (point-max))
  1613. (org-open-at-point)
  1614. (bobp))))
  1615. ;;;; Link Escaping
  1616. (ert-deftest test-org/org-link-escape-ascii-character ()
  1617. "Escape an ascii character."
  1618. (should
  1619. (string=
  1620. "%5B"
  1621. (org-link-escape "["))))
  1622. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  1623. "Escape an ascii control character."
  1624. (should
  1625. (string=
  1626. "%09"
  1627. (org-link-escape "\t"))))
  1628. (ert-deftest test-org/org-link-escape-multibyte-character ()
  1629. "Escape an unicode multibyte character."
  1630. (should
  1631. (string=
  1632. "%E2%82%AC"
  1633. (org-link-escape "€"))))
  1634. (ert-deftest test-org/org-link-escape-custom-table ()
  1635. "Escape string with custom character table."
  1636. (should
  1637. (string=
  1638. "Foo%3A%42ar%0A"
  1639. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  1640. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  1641. "Escape string with custom table merged with default table."
  1642. (should
  1643. (string=
  1644. "%5BF%6F%6F%3A%42ar%0A%5D"
  1645. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  1646. (ert-deftest test-org/org-link-unescape-ascii-character ()
  1647. "Unescape an ascii character."
  1648. (should
  1649. (string=
  1650. "["
  1651. (org-link-unescape "%5B"))))
  1652. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  1653. "Unescpae an ascii control character."
  1654. (should
  1655. (string=
  1656. "\n"
  1657. (org-link-unescape "%0A"))))
  1658. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  1659. "Unescape unicode multibyte character."
  1660. (should
  1661. (string=
  1662. "€"
  1663. (org-link-unescape "%E2%82%AC"))))
  1664. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  1665. "Unescape old style percent escaped character."
  1666. (should
  1667. (string=
  1668. "àâçèéêîôùû"
  1669. (decode-coding-string
  1670. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  1671. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  1672. "Escape and unescape a URL that includes an escaped char.
  1673. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  1674. (should
  1675. (string=
  1676. "http://some.host.com/form?&id=blah%2Bblah25"
  1677. (org-link-unescape
  1678. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  1679. (ert-deftest test-org/org-link-escape-chars-browser ()
  1680. "Test of the constant `org-link-escape-chars-browser'.
  1681. See there why this test is a candidate to be removed once Org
  1682. drops support for Emacs 24.1 and 24.2."
  1683. (should
  1684. (string=
  1685. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1686. "%22Release%208.2%22&idxname=emacs-orgmode")
  1687. (org-link-escape-browser ; Do not replace with `url-encode-url',
  1688. ; see docstring above.
  1689. (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
  1690. "\"Release 8.2\"&idxname=emacs-orgmode")))))
  1691. ;;;; Open at point
  1692. (ert-deftest test-org/open-at-point-in-keyword ()
  1693. "Does `org-open-at-point' open link in a keyword line?"
  1694. (should
  1695. (org-test-with-temp-text
  1696. "#+KEYWORD: <point>[[info:emacs#Top]]"
  1697. (org-open-at-point) t)))
  1698. (ert-deftest test-org/open-at-point-in-property ()
  1699. "Does `org-open-at-point' open link in property drawer?"
  1700. (should
  1701. (org-test-with-temp-text
  1702. "* Headline
  1703. :PROPERTIES:
  1704. :URL: <point>[[info:emacs#Top]]
  1705. :END:"
  1706. (org-open-at-point) t)))
  1707. (ert-deftest test-org/open-at-point-in-comment ()
  1708. "Does `org-open-at-point' open link in a commented line?"
  1709. (should
  1710. (org-test-with-temp-text
  1711. "# <point>[[info:emacs#Top]]"
  1712. (org-open-at-point) t)))
  1713. (ert-deftest test-org/open-at-point/info ()
  1714. "Test `org-open-at-point' on info links."
  1715. (should
  1716. (org-test-with-temp-text
  1717. "<point>[[info:emacs#Top]]"
  1718. (org-open-at-point)
  1719. (and (switch-to-buffer "*info*")
  1720. (prog1
  1721. (looking-at "\nThe Emacs Editor")
  1722. (kill-buffer))))))
  1723. (ert-deftest test-org/open-at-point/inline-image ()
  1724. "Test `org-open-at-point' on nested links."
  1725. (should
  1726. (org-test-with-temp-text "[[info:org#Top][info:<point>emacs#Top]]"
  1727. (org-open-at-point)
  1728. (prog1 (with-current-buffer "*info*" (looking-at "\nOrg Mode Manual"))
  1729. (kill-buffer "*info*")))))
  1730. (ert-deftest test-org/open-at-point/radio-target ()
  1731. "Test `org-open-at-point' on radio targets."
  1732. (should
  1733. (org-test-with-temp-text "<<<target>>> <point>target"
  1734. (org-update-radio-target-regexp)
  1735. (org-open-at-point)
  1736. (eq (org-element-type (org-element-context)) 'radio-target))))
  1737. ;;; Node Properties
  1738. (ert-deftest test-org/accumulated-properties-in-drawers ()
  1739. "Ensure properties accumulate in subtree drawers."
  1740. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1741. (org-babel-next-src-block)
  1742. (should (equal '(2 1) (org-babel-execute-src-block)))))
  1743. (ert-deftest test-org/custom-properties ()
  1744. "Test custom properties specifications."
  1745. ;; Standard test.
  1746. (should
  1747. (let ((org-custom-properties '("FOO")))
  1748. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1749. (org-toggle-custom-properties-visibility)
  1750. (org-invisible-p2))))
  1751. ;; Properties are case-insensitive.
  1752. (should
  1753. (let ((org-custom-properties '("FOO")))
  1754. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  1755. (org-toggle-custom-properties-visibility)
  1756. (org-invisible-p2))))
  1757. (should
  1758. (let ((org-custom-properties '("foo")))
  1759. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  1760. (org-toggle-custom-properties-visibility)
  1761. (org-invisible-p2))))
  1762. ;; Multiple custom properties in the same drawer.
  1763. (should
  1764. (let ((org-custom-properties '("FOO" "BAR")))
  1765. (org-test-with-temp-text
  1766. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  1767. (org-toggle-custom-properties-visibility)
  1768. (and (org-invisible-p2)
  1769. (not (progn (forward-line) (org-invisible-p2)))
  1770. (progn (forward-line) (org-invisible-p2))))))
  1771. ;; Hide custom properties with an empty value.
  1772. (should
  1773. (let ((org-custom-properties '("FOO")))
  1774. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  1775. (org-toggle-custom-properties-visibility)
  1776. (org-invisible-p2))))
  1777. ;; Do not hide fake properties.
  1778. (should-not
  1779. (let ((org-custom-properties '("FOO")))
  1780. (org-test-with-temp-text ":FOO: val\n"
  1781. (org-toggle-custom-properties-visibility)
  1782. (org-invisible-p2))))
  1783. (should-not
  1784. (let ((org-custom-properties '("A")))
  1785. (org-test-with-temp-text
  1786. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  1787. (org-toggle-custom-properties-visibility)
  1788. (org-invisible-p2)))))
  1789. ;;; Mark Region
  1790. (ert-deftest test-org/mark-subtree ()
  1791. "Test `org-mark-subtree' specifications."
  1792. ;; Error when point is before first headline.
  1793. (should-error
  1794. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  1795. (progn (transient-mark-mode 1)
  1796. (org-mark-subtree))))
  1797. ;; Without argument, mark current subtree.
  1798. (should
  1799. (equal
  1800. '(12 32)
  1801. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1802. (progn (transient-mark-mode 1)
  1803. (forward-line 2)
  1804. (org-mark-subtree)
  1805. (list (region-beginning) (region-end))))))
  1806. ;; With an argument, move ARG up.
  1807. (should
  1808. (equal
  1809. '(1 32)
  1810. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  1811. (progn (transient-mark-mode 1)
  1812. (forward-line 2)
  1813. (org-mark-subtree 1)
  1814. (list (region-beginning) (region-end))))))
  1815. ;; Do not get fooled by inlinetasks.
  1816. (when (featurep 'org-inlinetask)
  1817. (should
  1818. (= 1
  1819. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  1820. (progn (transient-mark-mode 1)
  1821. (forward-line 1)
  1822. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  1823. (region-beginning)))))))
  1824. ;;; Navigation
  1825. (ert-deftest test-org/end-of-meta-data ()
  1826. "Test `org-end-of-meta-data' specifications."
  1827. ;; Skip planning line.
  1828. (should
  1829. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  1830. (org-end-of-meta-data)
  1831. (eobp)))
  1832. ;; Skip properties drawer.
  1833. (should
  1834. (org-test-with-temp-text
  1835. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  1836. (org-end-of-meta-data)
  1837. (eobp)))
  1838. ;; Skip both.
  1839. (should
  1840. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  1841. (org-end-of-meta-data)
  1842. (eobp)))
  1843. ;; Nothing to skip, go to first line.
  1844. (should
  1845. (org-test-with-temp-text "* Headline\nContents"
  1846. (org-end-of-meta-data)
  1847. (looking-at "Contents")))
  1848. ;; With option argument, skip empty lines, regular drawers and
  1849. ;; clocking lines.
  1850. (should
  1851. (org-test-with-temp-text "* Headline\n\nContents"
  1852. (org-end-of-meta-data t)
  1853. (looking-at "Contents")))
  1854. (should
  1855. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  1856. (org-end-of-meta-data t)
  1857. (looking-at "Contents")))
  1858. (should
  1859. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  1860. (org-end-of-meta-data t)
  1861. (looking-at "Contents")))
  1862. ;; Special case: do not skip incomplete drawers.
  1863. (should
  1864. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  1865. (org-end-of-meta-data t)
  1866. (looking-at ":LOGBOOK:")))
  1867. ;; Special case: Be careful about consecutive headlines.
  1868. (should-not
  1869. (org-test-with-temp-text "* H1\n*H2\nContents"
  1870. (org-end-of-meta-data t)
  1871. (looking-at "Contents"))))
  1872. (ert-deftest test-org/beginning-of-line ()
  1873. "Test `org-beginning-of-line' specifications."
  1874. ;; Standard test.
  1875. (should
  1876. (org-test-with-temp-text "Some text\nSome other text"
  1877. (progn (org-beginning-of-line) (bolp))))
  1878. ;; Standard test with `visual-line-mode'.
  1879. (should-not
  1880. (org-test-with-temp-text "A long line of text\nSome other text"
  1881. (progn (visual-line-mode)
  1882. (forward-char 2)
  1883. (dotimes (i 1000) (insert "very "))
  1884. (org-beginning-of-line)
  1885. (bolp))))
  1886. ;; At an headline with special movement.
  1887. (should
  1888. (org-test-with-temp-text "* TODO Headline"
  1889. (let ((org-special-ctrl-a/e t))
  1890. (org-end-of-line)
  1891. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  1892. (progn (org-beginning-of-line) (bolp))
  1893. (progn (org-beginning-of-line) (looking-at "Headline"))))))
  1894. ;; Special case: Do not error when the buffer contains only a single
  1895. ;; asterisk.
  1896. (should
  1897. (org-test-with-temp-text "*<point>"
  1898. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))))
  1899. (should
  1900. (org-test-with-temp-text "*<point>"
  1901. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line)))))
  1902. (ert-deftest test-org/end-of-line ()
  1903. "Test `org-end-of-line' specifications."
  1904. ;; Standard test.
  1905. (should
  1906. (org-test-with-temp-text "Some text\nSome other text"
  1907. (progn (org-end-of-line) (eolp))))
  1908. ;; Standard test with `visual-line-mode'.
  1909. (should-not
  1910. (org-test-with-temp-text "A long line of text\nSome other text"
  1911. (progn (visual-line-mode)
  1912. (forward-char 2)
  1913. (dotimes (i 1000) (insert "very "))
  1914. (goto-char (point-min))
  1915. (org-end-of-line)
  1916. (eolp))))
  1917. ;; At an headline with special movement.
  1918. (should
  1919. (org-test-with-temp-text "* Headline1 :tag:\n"
  1920. (let ((org-special-ctrl-a/e t))
  1921. (and (progn (org-end-of-line) (looking-at " :tag:"))
  1922. (progn (org-end-of-line) (eolp))
  1923. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1924. ;; At an headline without special movement.
  1925. (should
  1926. (org-test-with-temp-text "* Headline2 :tag:\n"
  1927. (let ((org-special-ctrl-a/e nil))
  1928. (and (progn (org-end-of-line) (eolp))
  1929. (progn (org-end-of-line) (eolp))))))
  1930. ;; At an headline, with reversed movement.
  1931. (should
  1932. (org-test-with-temp-text "* Headline3 :tag:\n"
  1933. (let ((org-special-ctrl-a/e 'reversed)
  1934. (this-command last-command))
  1935. (and (progn (org-end-of-line) (eolp))
  1936. (progn (org-end-of-line) (looking-at " :tag:"))))))
  1937. ;; At a block without hidden contents.
  1938. (should
  1939. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1940. (progn (org-end-of-line) (eolp))))
  1941. ;; At a block with hidden contents.
  1942. (should-not
  1943. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  1944. (let ((org-special-ctrl-a/e t))
  1945. (org-hide-block-toggle)
  1946. (org-end-of-line)
  1947. (eobp)))))
  1948. (ert-deftest test-org/forward-sentence ()
  1949. "Test `org-forward-sentence' specifications."
  1950. ;; At the end of a table cell, move to the end of the next one.
  1951. (should
  1952. (org-test-with-temp-text "| a<point> | b |"
  1953. (org-forward-sentence)
  1954. (looking-at " |$")))
  1955. ;; Elsewhere in a cell, move to its end.
  1956. (should
  1957. (org-test-with-temp-text "| a<point>c | b |"
  1958. (org-forward-sentence)
  1959. (looking-at " | b |$")))
  1960. ;; Otherwise, simply call `forward-sentence'.
  1961. (should
  1962. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  1963. (org-forward-sentence)
  1964. (looking-at " Sentence 2.")))
  1965. (should
  1966. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  1967. (org-forward-sentence)
  1968. (org-forward-sentence)
  1969. (eobp)))
  1970. ;; At the end of an element, jump to the next one, without stopping
  1971. ;; on blank lines in-between.
  1972. (should
  1973. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  1974. (org-forward-sentence)
  1975. (eobp))))
  1976. (ert-deftest test-org/backward-sentence ()
  1977. "Test `org-backward-sentence' specifications."
  1978. ;; At the beginning of a table cell, move to the beginning of the
  1979. ;; previous one.
  1980. (should
  1981. (org-test-with-temp-text "| a | <point>b |"
  1982. (org-backward-sentence)
  1983. (looking-at "a | b |$")))
  1984. ;; Elsewhere in a cell, move to its beginning.
  1985. (should
  1986. (org-test-with-temp-text "| a | b<point>c |"
  1987. (org-backward-sentence)
  1988. (looking-at "bc |$")))
  1989. ;; Otherwise, simply call `backward-sentence'.
  1990. (should
  1991. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  1992. (org-backward-sentence)
  1993. (looking-at "Sentence 2.")))
  1994. (should
  1995. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  1996. (org-backward-sentence)
  1997. (org-backward-sentence)
  1998. (bobp)))
  1999. ;; Make sure to hit the beginning of a sentence on the same line as
  2000. ;; an item.
  2001. (should
  2002. (org-test-with-temp-text "- Line 1\n line <point>2."
  2003. (org-backward-sentence)
  2004. (looking-at "Line 1"))))
  2005. (ert-deftest test-org/forward-paragraph ()
  2006. "Test `org-forward-paragraph' specifications."
  2007. ;; At end of buffer, return an error.
  2008. (should-error
  2009. (org-test-with-temp-text "Paragraph"
  2010. (goto-char (point-max))
  2011. (org-forward-paragraph)))
  2012. ;; Standard test.
  2013. (should
  2014. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2015. (org-forward-paragraph)
  2016. (looking-at "P2")))
  2017. ;; Ignore depth.
  2018. (should
  2019. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  2020. (org-forward-paragraph)
  2021. (looking-at "P1")))
  2022. ;; Do not enter elements with invisible contents.
  2023. (should
  2024. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  2025. (org-hide-block-toggle)
  2026. (org-forward-paragraph)
  2027. (looking-at "P3")))
  2028. ;; On an affiliated keyword, jump to the beginning of the element.
  2029. (should
  2030. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  2031. (org-forward-paragraph)
  2032. (looking-at "Para")))
  2033. ;; On an item or a footnote definition, move to the second element
  2034. ;; inside, if any.
  2035. (should
  2036. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  2037. (org-forward-paragraph)
  2038. (looking-at " Paragraph")))
  2039. (should
  2040. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  2041. (org-forward-paragraph)
  2042. (looking-at "Paragraph")))
  2043. ;; On an item, or a footnote definition, when the first line is
  2044. ;; empty, move to the first item.
  2045. (should
  2046. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  2047. (org-forward-paragraph)
  2048. (looking-at " Paragraph")))
  2049. (should
  2050. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  2051. (org-forward-paragraph)
  2052. (looking-at "Paragraph")))
  2053. ;; On a table (resp. a property drawer) do not move through table
  2054. ;; rows (resp. node properties).
  2055. (should
  2056. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  2057. (org-forward-paragraph)
  2058. (looking-at "Paragraph")))
  2059. (should
  2060. (org-test-with-temp-text
  2061. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  2062. (org-forward-paragraph)
  2063. (looking-at "Paragraph")))
  2064. ;; On a verse or source block, stop after blank lines.
  2065. (should
  2066. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  2067. (org-forward-paragraph)
  2068. (looking-at "L2")))
  2069. (should
  2070. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  2071. (org-forward-paragraph)
  2072. (looking-at "L2"))))
  2073. (ert-deftest test-org/backward-paragraph ()
  2074. "Test `org-backward-paragraph' specifications."
  2075. ;; Error at beginning of buffer.
  2076. (should-error
  2077. (org-test-with-temp-text "Paragraph"
  2078. (org-backward-paragraph)))
  2079. ;; Regular test.
  2080. (should
  2081. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2082. (goto-char (point-max))
  2083. (org-backward-paragraph)
  2084. (looking-at "P3")))
  2085. (should
  2086. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2087. (goto-char (point-max))
  2088. (beginning-of-line)
  2089. (org-backward-paragraph)
  2090. (looking-at "P2")))
  2091. ;; Ignore depth.
  2092. (should
  2093. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  2094. (goto-char (point-max))
  2095. (beginning-of-line)
  2096. (org-backward-paragraph)
  2097. (looking-at "P2")))
  2098. ;; Ignore invisible elements.
  2099. (should
  2100. (org-test-with-temp-text "* H1\n P1\n* H2"
  2101. (org-cycle)
  2102. (goto-char (point-max))
  2103. (beginning-of-line)
  2104. (org-backward-paragraph)
  2105. (bobp)))
  2106. ;; On an affiliated keyword, jump to the first one.
  2107. (should
  2108. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  2109. (search-forward "c2")
  2110. (org-backward-paragraph)
  2111. (looking-at "#\\+name")))
  2112. ;; On the second element in an item or a footnote definition, jump
  2113. ;; to item or the definition.
  2114. (should
  2115. (org-test-with-temp-text "- line1\n\n line2"
  2116. (goto-char (point-max))
  2117. (beginning-of-line)
  2118. (org-backward-paragraph)
  2119. (looking-at "- line1")))
  2120. (should
  2121. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  2122. (goto-char (point-max))
  2123. (beginning-of-line)
  2124. (org-backward-paragraph)
  2125. (looking-at "\\[fn:1\\] line1")))
  2126. ;; On a table (resp. a property drawer), ignore table rows
  2127. ;; (resp. node properties).
  2128. (should
  2129. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  2130. (goto-char (point-max))
  2131. (beginning-of-line)
  2132. (org-backward-paragraph)
  2133. (bobp)))
  2134. (should
  2135. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  2136. (org-backward-paragraph)
  2137. (looking-at ":PROPERTIES:")))
  2138. ;; On a source or verse block, stop before blank lines.
  2139. (should
  2140. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  2141. (search-forward "L3")
  2142. (beginning-of-line)
  2143. (org-backward-paragraph)
  2144. (looking-at "L2")))
  2145. (should
  2146. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  2147. (search-forward "L3")
  2148. (beginning-of-line)
  2149. (org-backward-paragraph)
  2150. (looking-at "L2"))))
  2151. (ert-deftest test-org/forward-element ()
  2152. "Test `org-forward-element' specifications."
  2153. ;; 1. At EOB: should error.
  2154. (org-test-with-temp-text "Some text\n"
  2155. (goto-char (point-max))
  2156. (should-error (org-forward-element)))
  2157. ;; 2. Standard move: expected to ignore blank lines.
  2158. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  2159. (org-forward-element)
  2160. (should (looking-at (regexp-quote "Second paragraph."))))
  2161. ;; 3. Headline tests.
  2162. (org-test-with-temp-text "
  2163. * Head 1
  2164. ** Head 1.1
  2165. *** Head 1.1.1
  2166. ** Head 1.2"
  2167. ;; 3.1. At an headline beginning: move to next headline at the
  2168. ;; same level.
  2169. (goto-line 3)
  2170. (org-forward-element)
  2171. (should (looking-at (regexp-quote "** Head 1.2")))
  2172. ;; 3.2. At an headline beginning: move to parent headline if no
  2173. ;; headline at the same level.
  2174. (goto-line 3)
  2175. (org-forward-element)
  2176. (should (looking-at (regexp-quote "** Head 1.2"))))
  2177. ;; 4. Greater element tests.
  2178. (org-test-with-temp-text
  2179. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  2180. ;; 4.1. At a greater element: expected to skip contents.
  2181. (org-forward-element)
  2182. (should (looking-at (regexp-quote "Outside.")))
  2183. ;; 4.2. At the end of greater element contents: expected to skip
  2184. ;; to the end of the greater element.
  2185. (goto-line 2)
  2186. (org-forward-element)
  2187. (should (looking-at (regexp-quote "Outside."))))
  2188. ;; 5. List tests.
  2189. (org-test-with-temp-text "
  2190. - item1
  2191. - sub1
  2192. - sub2
  2193. - sub3
  2194. Inner paragraph.
  2195. - item2
  2196. Outside."
  2197. ;; 5.1. At list top point: expected to move to the element after
  2198. ;; the list.
  2199. (goto-line 2)
  2200. (org-forward-element)
  2201. (should (looking-at (regexp-quote "Outside.")))
  2202. ;; 5.2. Special case: at the first line of a sub-list, but not at
  2203. ;; beginning of line, move to next item.
  2204. (goto-line 2)
  2205. (forward-char)
  2206. (org-forward-element)
  2207. (should (looking-at "- item2"))
  2208. (goto-line 4)
  2209. (forward-char)
  2210. (org-forward-element)
  2211. (should (looking-at " - sub2"))
  2212. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  2213. (goto-line 4)
  2214. (org-forward-element)
  2215. (should (looking-at (regexp-quote " Inner paragraph.")))
  2216. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  2217. (goto-line 8)
  2218. (org-forward-element)
  2219. (should (looking-at (regexp-quote " Inner paragraph.")))
  2220. ;; 5.5. At an item: expected to move to next item, if any.
  2221. (goto-line 6)
  2222. (org-forward-element)
  2223. (should (looking-at " - sub3"))))
  2224. (ert-deftest test-org/backward-element ()
  2225. "Test `org-backward-element' specifications."
  2226. ;; 1. Should error at BOB.
  2227. (org-test-with-temp-text " \nParagraph."
  2228. (should-error (org-backward-element)))
  2229. ;; 2. Should move at BOB when called on the first element in buffer.
  2230. (should
  2231. (org-test-with-temp-text "\n#+TITLE: test"
  2232. (progn (forward-line)
  2233. (org-backward-element)
  2234. (bobp))))
  2235. ;; 3. Not at the beginning of an element: move at its beginning.
  2236. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2237. (goto-line 3)
  2238. (end-of-line)
  2239. (org-backward-element)
  2240. (should (looking-at (regexp-quote "Paragraph2."))))
  2241. ;; 4. Headline tests.
  2242. (org-test-with-temp-text "
  2243. * Head 1
  2244. ** Head 1.1
  2245. *** Head 1.1.1
  2246. ** Head 1.2"
  2247. ;; 4.1. At an headline beginning: move to previous headline at the
  2248. ;; same level.
  2249. (goto-line 5)
  2250. (org-backward-element)
  2251. (should (looking-at (regexp-quote "** Head 1.1")))
  2252. ;; 4.2. At an headline beginning: move to parent headline if no
  2253. ;; headline at the same level.
  2254. (goto-line 3)
  2255. (org-backward-element)
  2256. (should (looking-at (regexp-quote "* Head 1")))
  2257. ;; 4.3. At the first top-level headline: should error.
  2258. (goto-line 2)
  2259. (should-error (org-backward-element)))
  2260. ;; 5. At beginning of first element inside a greater element:
  2261. ;; expected to move to greater element's beginning.
  2262. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  2263. (goto-line 3)
  2264. (org-backward-element)
  2265. (should (looking-at "#\\+BEGIN_CENTER")))
  2266. ;; 6. At the beginning of the first element in a section: should
  2267. ;; move back to headline, if any.
  2268. (should
  2269. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  2270. (progn (goto-char (point-max))
  2271. (beginning-of-line)
  2272. (org-backward-element)
  2273. (org-at-heading-p))))
  2274. ;; 7. List tests.
  2275. (org-test-with-temp-text "
  2276. - item1
  2277. - sub1
  2278. - sub2
  2279. - sub3
  2280. Inner paragraph.
  2281. - item2
  2282. Outside."
  2283. ;; 7.1. At beginning of sub-list: expected to move to the
  2284. ;; paragraph before it.
  2285. (goto-line 4)
  2286. (org-backward-element)
  2287. (should (looking-at "item1"))
  2288. ;; 7.2. At an item in a list: expected to move at previous item.
  2289. (goto-line 8)
  2290. (org-backward-element)
  2291. (should (looking-at " - sub2"))
  2292. (goto-line 12)
  2293. (org-backward-element)
  2294. (should (looking-at "- item1"))
  2295. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  2296. ;; beginning.
  2297. (goto-line 10)
  2298. (org-backward-element)
  2299. (should (looking-at " - sub1"))
  2300. (goto-line 15)
  2301. (org-backward-element)
  2302. (should (looking-at "- item1"))
  2303. ;; 7.4. At blank-lines before list end: expected to move to top
  2304. ;; item.
  2305. (goto-line 14)
  2306. (org-backward-element)
  2307. (should (looking-at "- item1"))))
  2308. (ert-deftest test-org/up-element ()
  2309. "Test `org-up-element' specifications."
  2310. ;; 1. At BOB or with no surrounding element: should error.
  2311. (org-test-with-temp-text "Paragraph."
  2312. (should-error (org-up-element)))
  2313. (org-test-with-temp-text "* Head1\n* Head2"
  2314. (goto-line 2)
  2315. (should-error (org-up-element)))
  2316. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2317. (goto-line 3)
  2318. (should-error (org-up-element)))
  2319. ;; 2. At an headline: move to parent headline.
  2320. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  2321. (goto-line 3)
  2322. (org-up-element)
  2323. (should (looking-at "\\* Head1")))
  2324. ;; 3. Inside a greater element: move to greater element beginning.
  2325. (org-test-with-temp-text
  2326. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  2327. (goto-line 3)
  2328. (org-up-element)
  2329. (should (looking-at "#\\+BEGIN_CENTER")))
  2330. ;; 4. List tests.
  2331. (org-test-with-temp-text "* Top
  2332. - item1
  2333. - sub1
  2334. - sub2
  2335. Paragraph within sub2.
  2336. - item2"
  2337. ;; 4.1. Within an item: move to the item beginning.
  2338. (goto-line 8)
  2339. (org-up-element)
  2340. (should (looking-at " - sub2"))
  2341. ;; 4.2. At an item in a sub-list: move to parent item.
  2342. (goto-line 4)
  2343. (org-up-element)
  2344. (should (looking-at "- item1"))
  2345. ;; 4.3. At an item in top list: move to beginning of whole list.
  2346. (goto-line 10)
  2347. (org-up-element)
  2348. (should (looking-at "- item1"))
  2349. ;; 4.4. Special case. At very top point: should move to parent of
  2350. ;; list.
  2351. (goto-line 2)
  2352. (org-up-element)
  2353. (should (looking-at "\\* Top"))))
  2354. (ert-deftest test-org/down-element ()
  2355. "Test `org-down-element' specifications."
  2356. ;; Error when the element hasn't got a recursive type.
  2357. (org-test-with-temp-text "Paragraph."
  2358. (should-error (org-down-element)))
  2359. ;; Error when the element has no contents
  2360. (org-test-with-temp-text "* Headline"
  2361. (should-error (org-down-element)))
  2362. ;; When at a plain-list, move to first item.
  2363. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  2364. (goto-line 2)
  2365. (org-down-element)
  2366. (should (looking-at " - Item 1.1")))
  2367. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  2368. (org-down-element)
  2369. (should (looking-at " Item 1")))
  2370. ;; When at a table, move to first row
  2371. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  2372. (org-down-element)
  2373. (should (looking-at " a | b |")))
  2374. ;; Otherwise, move inside the greater element.
  2375. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  2376. (org-down-element)
  2377. (should (looking-at "Paragraph"))))
  2378. (ert-deftest test-org/drag-element-backward ()
  2379. "Test `org-drag-element-backward' specifications."
  2380. ;; Standard test.
  2381. (should
  2382. (equal
  2383. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  2384. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  2385. (org-drag-element-backward)
  2386. (buffer-string))))
  2387. (should
  2388. (equal
  2389. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  2390. (org-test-with-temp-text
  2391. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  2392. (org-drag-element-backward)
  2393. (buffer-string))))
  2394. ;; Preserve blank lines.
  2395. (should
  2396. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  2397. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  2398. (org-drag-element-backward)
  2399. (buffer-string))))
  2400. ;; Preserve column.
  2401. (should
  2402. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  2403. (org-drag-element-backward)
  2404. (org-looking-at-p "2")))
  2405. ;; Error when trying to move first element of buffer.
  2406. (should-error
  2407. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2408. (org-drag-element-backward)))
  2409. ;; Error when trying to swap nested elements.
  2410. (should-error
  2411. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2412. (forward-line)
  2413. (org-drag-element-backward)))
  2414. ;; Error when trying to swap an headline element and a non-headline
  2415. ;; element.
  2416. (should-error
  2417. (org-test-with-temp-text "Test.\n* Head 1"
  2418. (forward-line)
  2419. (org-drag-element-backward)))
  2420. ;; Preserve visibility of elements and their contents.
  2421. (should
  2422. (equal '((63 . 82) (26 . 48))
  2423. (org-test-with-temp-text "
  2424. #+BEGIN_CENTER
  2425. Text.
  2426. #+END_CENTER
  2427. - item 1
  2428. #+BEGIN_QUOTE
  2429. Text.
  2430. #+END_QUOTE"
  2431. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2432. (search-backward "- item 1")
  2433. (org-drag-element-backward)
  2434. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2435. (overlays-in (point-min) (point-max)))))))
  2436. (ert-deftest test-org/drag-element-forward ()
  2437. "Test `org-drag-element-forward' specifications."
  2438. ;; 1. Error when trying to move first element of buffer.
  2439. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2440. (goto-line 3)
  2441. (should-error (org-drag-element-forward)))
  2442. ;; 2. Error when trying to swap nested elements.
  2443. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2444. (forward-line)
  2445. (should-error (org-drag-element-forward)))
  2446. ;; 3. Error when trying to swap a non-headline element and an
  2447. ;; headline.
  2448. (org-test-with-temp-text "Test.\n* Head 1"
  2449. (should-error (org-drag-element-forward)))
  2450. ;; 4. Otherwise, swap elements, preserving column and blank lines
  2451. ;; between elements.
  2452. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  2453. (search-forward "graph")
  2454. (org-drag-element-forward)
  2455. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  2456. (should (looking-at " 1")))
  2457. ;; 5. Preserve visibility of elements and their contents.
  2458. (org-test-with-temp-text "
  2459. #+BEGIN_CENTER
  2460. Text.
  2461. #+END_CENTER
  2462. - item 1
  2463. #+BEGIN_QUOTE
  2464. Text.
  2465. #+END_QUOTE"
  2466. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2467. (search-backward "#+BEGIN_CENTER")
  2468. (org-drag-element-forward)
  2469. (should
  2470. (equal
  2471. '((63 . 82) (26 . 48))
  2472. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2473. (overlays-in (point-min) (point-max)))))))
  2474. (ert-deftest test-org/next-block ()
  2475. "Test `org-next-block' specifications."
  2476. ;; Regular test.
  2477. (should
  2478. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2479. (org-next-block 1)
  2480. (looking-at "#\\+BEGIN_CENTER")))
  2481. ;; Ignore case.
  2482. (should
  2483. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  2484. (let ((case-fold-search nil))
  2485. (org-next-block 1)
  2486. (looking-at "#\\+begin_center"))))
  2487. ;; Ignore current line.
  2488. (should
  2489. (org-test-with-temp-text
  2490. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  2491. (org-next-block 1)
  2492. (looking-at "#\\+BEGIN_CENTER")))
  2493. ;; Throw an error when no block is found.
  2494. (should-error
  2495. (org-test-with-temp-text "Paragraph"
  2496. (org-next-block 1)))
  2497. ;; With an argument, skip many blocks at once.
  2498. (should
  2499. (org-test-with-temp-text
  2500. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  2501. (org-next-block 2)
  2502. (looking-at "#\\+BEGIN_QUOTE")))
  2503. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  2504. (should
  2505. (org-test-with-temp-text
  2506. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  2507. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  2508. (looking-at "#\\+BEGIN_QUOTE")))
  2509. ;; Optional argument is also case-insensitive.
  2510. (should
  2511. (org-test-with-temp-text
  2512. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  2513. (let ((case-fold-search nil))
  2514. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  2515. (looking-at "#\\+begin_quote")))))
  2516. (ert-deftest test-org/previous-block ()
  2517. "Test `org-previous-block' specifications."
  2518. ;; Regular test.
  2519. (should
  2520. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  2521. (org-previous-block 1)
  2522. (looking-at "#\\+BEGIN_CENTER")))
  2523. ;; Ignore case.
  2524. (should
  2525. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  2526. (let ((case-fold-search nil))
  2527. (org-previous-block 1)
  2528. (looking-at "#\\+begin_center"))))
  2529. ;; Ignore current line.
  2530. (should
  2531. (org-test-with-temp-text
  2532. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  2533. (org-previous-block 1)
  2534. (looking-at "#\\+BEGIN_QUOTE")))
  2535. ;; Throw an error when no block is found.
  2536. (should-error
  2537. (org-test-with-temp-text "Paragraph<point>"
  2538. (org-previous-block 1)))
  2539. ;; With an argument, skip many blocks at once.
  2540. (should
  2541. (org-test-with-temp-text
  2542. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  2543. (org-previous-block 2)
  2544. (looking-at "#\\+BEGIN_CENTER")))
  2545. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  2546. (should
  2547. (org-test-with-temp-text
  2548. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  2549. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  2550. (looking-at "#\\+BEGIN_QUOTE")))
  2551. ;; Optional argument is also case-insensitive.
  2552. (should
  2553. (org-test-with-temp-text
  2554. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  2555. (let ((case-fold-search nil))
  2556. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  2557. (looking-at "#\\+begin_quote")))))
  2558. ;;; Outline structure
  2559. (ert-deftest test-org/demote ()
  2560. "Test `org-demote' specifications."
  2561. ;; Add correct number of stars according to `org-odd-levels-only'.
  2562. (should
  2563. (= 2
  2564. (org-test-with-temp-text "* H"
  2565. (let ((org-odd-levels-only nil)) (org-demote))
  2566. (org-current-level))))
  2567. (should
  2568. (= 3
  2569. (org-test-with-temp-text "* H"
  2570. (let ((org-odd-levels-only t)) (org-demote))
  2571. (org-current-level))))
  2572. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  2573. (should
  2574. (org-test-with-temp-text "* H :tag:"
  2575. (let ((org-tags-column 10)
  2576. (org-auto-align-tags t)
  2577. (org-odd-levels-only nil))
  2578. (org-demote))
  2579. (org-move-to-column 10)
  2580. (org-looking-at-p ":tag:$")))
  2581. (should-not
  2582. (org-test-with-temp-text "* H :tag:"
  2583. (let ((org-tags-column 10)
  2584. (org-auto-align-tags nil)
  2585. (org-odd-levels-only nil))
  2586. (org-demote))
  2587. (org-move-to-column 10)
  2588. (org-looking-at-p ":tag:$")))
  2589. ;; When `org-adapt-indentation' is non-nil, always indent planning
  2590. ;; info and property drawers accordingly.
  2591. (should
  2592. (= 3
  2593. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  2594. (let ((org-odd-levels-only nil)
  2595. (org-adapt-indentation t))
  2596. (org-demote))
  2597. (forward-line)
  2598. (org-get-indentation))))
  2599. (should
  2600. (= 3
  2601. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2602. (let ((org-odd-levels-only nil)
  2603. (org-adapt-indentation t))
  2604. (org-demote))
  2605. (forward-line)
  2606. (org-get-indentation))))
  2607. (should-not
  2608. (= 3
  2609. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  2610. (let ((org-odd-levels-only nil)
  2611. (org-adapt-indentation nil))
  2612. (org-demote))
  2613. (forward-line)
  2614. (org-get-indentation))))
  2615. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2616. ;; section accordingly. Ignore, however, footnote definitions and
  2617. ;; inlinetasks boundaries.
  2618. (should
  2619. (= 3
  2620. (org-test-with-temp-text "* H\n Paragraph"
  2621. (let ((org-odd-levels-only nil)
  2622. (org-adapt-indentation t))
  2623. (org-demote))
  2624. (forward-line)
  2625. (org-get-indentation))))
  2626. (should
  2627. (= 2
  2628. (org-test-with-temp-text "* H\n Paragraph"
  2629. (let ((org-odd-levels-only nil)
  2630. (org-adapt-indentation nil))
  2631. (org-demote))
  2632. (forward-line)
  2633. (org-get-indentation))))
  2634. (should
  2635. (zerop
  2636. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  2637. (let ((org-odd-levels-only nil)
  2638. (org-adapt-indentation t))
  2639. (org-demote))
  2640. (goto-char (point-max))
  2641. (org-get-indentation))))
  2642. (should
  2643. (= 3
  2644. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  2645. (let ((org-odd-levels-only nil)
  2646. (org-adapt-indentation t))
  2647. (org-demote))
  2648. (goto-char (point-max))
  2649. (org-get-indentation))))
  2650. (when (featurep 'org-inlinetask)
  2651. (should
  2652. (zerop
  2653. (let ((org-inlinetask-min-level 5)
  2654. (org-adapt-indentation t))
  2655. (org-test-with-temp-text "* H\n***** I\n***** END"
  2656. (org-demote)
  2657. (forward-line)
  2658. (org-get-indentation))))))
  2659. (when (featurep 'org-inlinetask)
  2660. (should
  2661. (= 3
  2662. (let ((org-inlinetask-min-level 5)
  2663. (org-adapt-indentation t))
  2664. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  2665. (org-demote)
  2666. (forward-line 2)
  2667. (org-get-indentation))))))
  2668. ;; Ignore contents of source blocks or example blocks when
  2669. ;; indentation should be preserved (through
  2670. ;; `org-src-preserve-indentation' or "-i" flag).
  2671. (should-not
  2672. (zerop
  2673. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2674. (let ((org-adapt-indentation t)
  2675. (org-src-preserve-indentation nil))
  2676. (org-demote))
  2677. (forward-line 2)
  2678. (org-get-indentation))))
  2679. (should
  2680. (zerop
  2681. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2682. (let ((org-adapt-indentation t)
  2683. (org-src-preserve-indentation t))
  2684. (org-demote))
  2685. (forward-line 2)
  2686. (org-get-indentation))))
  2687. (should
  2688. (zerop
  2689. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2690. (let ((org-adapt-indentation t)
  2691. (org-src-preserve-indentation t))
  2692. (org-demote))
  2693. (forward-line 2)
  2694. (org-get-indentation))))
  2695. (should
  2696. (zerop
  2697. (org-test-with-temp-text
  2698. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  2699. (let ((org-adapt-indentation t)
  2700. (org-src-preserve-indentation nil))
  2701. (org-demote))
  2702. (forward-line 2)
  2703. (org-get-indentation)))))
  2704. (ert-deftest test-org/promote ()
  2705. "Test `org-promote' specifications."
  2706. ;; Return an error if headline is to be promoted to level 0, unless
  2707. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  2708. ;; headline becomes a comment.
  2709. (should-error
  2710. (org-test-with-temp-text "* H"
  2711. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  2712. (should
  2713. (equal "# H"
  2714. (org-test-with-temp-text "* H"
  2715. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  2716. (buffer-string))))
  2717. ;; Remove correct number of stars according to
  2718. ;; `org-odd-levels-only'.
  2719. (should
  2720. (= 2
  2721. (org-test-with-temp-text "*** H"
  2722. (let ((org-odd-levels-only nil)) (org-promote))
  2723. (org-current-level))))
  2724. (should
  2725. (= 1
  2726. (org-test-with-temp-text "*** H"
  2727. (let ((org-odd-levels-only t)) (org-promote))
  2728. (org-current-level))))
  2729. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  2730. (should
  2731. (org-test-with-temp-text "** H :tag:"
  2732. (let ((org-tags-column 10)
  2733. (org-auto-align-tags t)
  2734. (org-odd-levels-only nil))
  2735. (org-promote))
  2736. (org-move-to-column 10)
  2737. (org-looking-at-p ":tag:$")))
  2738. (should-not
  2739. (org-test-with-temp-text "** H :tag:"
  2740. (let ((org-tags-column 10)
  2741. (org-auto-align-tags nil)
  2742. (org-odd-levels-only nil))
  2743. (org-promote))
  2744. (org-move-to-column 10)
  2745. (org-looking-at-p ":tag:$")))
  2746. ;; When `org-adapt-indentation' is non-nil, always indent planning
  2747. ;; info and property drawers.
  2748. (should
  2749. (= 2
  2750. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2751. (let ((org-odd-levels-only nil)
  2752. (org-adapt-indentation t))
  2753. (org-promote))
  2754. (forward-line)
  2755. (org-get-indentation))))
  2756. (should
  2757. (= 2
  2758. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  2759. (let ((org-odd-levels-only nil)
  2760. (org-adapt-indentation t))
  2761. (org-promote))
  2762. (forward-line)
  2763. (org-get-indentation))))
  2764. (should-not
  2765. (= 2
  2766. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  2767. (let ((org-odd-levels-only nil)
  2768. (org-adapt-indentation nil))
  2769. (org-promote))
  2770. (forward-line)
  2771. (org-get-indentation))))
  2772. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  2773. ;; section accordingly. Ignore, however, footnote definitions and
  2774. ;; inlinetasks boundaries.
  2775. (should
  2776. (= 2
  2777. (org-test-with-temp-text "** H\n Paragraph"
  2778. (let ((org-odd-levels-only nil)
  2779. (org-adapt-indentation t))
  2780. (org-promote))
  2781. (forward-line)
  2782. (org-get-indentation))))
  2783. (should-not
  2784. (= 2
  2785. (org-test-with-temp-text "** H\n Paragraph"
  2786. (let ((org-odd-levels-only nil)
  2787. (org-adapt-indentation nil))
  2788. (org-promote))
  2789. (forward-line)
  2790. (org-get-indentation))))
  2791. (should
  2792. (= 2
  2793. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  2794. (let ((org-odd-levels-only nil)
  2795. (org-adapt-indentation t))
  2796. (org-promote))
  2797. (forward-line)
  2798. (org-get-indentation))))
  2799. (when (featurep 'org-inlinetask)
  2800. (should
  2801. (zerop
  2802. (let ((org-inlinetask-min-level 5)
  2803. (org-adapt-indentation t))
  2804. (org-test-with-temp-text "** H\n***** I\n***** END"
  2805. (org-promote)
  2806. (forward-line)
  2807. (org-get-indentation))))))
  2808. (when (featurep 'org-inlinetask)
  2809. (should
  2810. (= 2
  2811. (let ((org-inlinetask-min-level 5)
  2812. (org-adapt-indentation t))
  2813. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  2814. (org-promote)
  2815. (forward-line 2)
  2816. (org-get-indentation))))))
  2817. ;; Give up shifting if it would break document's structure
  2818. ;; otherwise.
  2819. (should
  2820. (= 3
  2821. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  2822. (let ((org-odd-levels-only nil)
  2823. (org-adapt-indentation t))
  2824. (org-promote))
  2825. (forward-line)
  2826. (org-get-indentation))))
  2827. (should
  2828. (= 3
  2829. (org-test-with-temp-text "** H\n Paragraph\n * list."
  2830. (let ((org-odd-levels-only nil)
  2831. (org-adapt-indentation t))
  2832. (org-promote))
  2833. (forward-line)
  2834. (org-get-indentation))))
  2835. ;; Ignore contents of source blocks or example blocks when
  2836. ;; indentation should be preserved (through
  2837. ;; `org-src-preserve-indentation' or "-i" flag).
  2838. (should-not
  2839. (zerop
  2840. (org-test-with-temp-text
  2841. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2842. (let ((org-adapt-indentation t)
  2843. (org-src-preserve-indentation nil)
  2844. (org-odd-levels-only nil))
  2845. (org-promote))
  2846. (forward-line)
  2847. (org-get-indentation))))
  2848. (should
  2849. (zerop
  2850. (org-test-with-temp-text
  2851. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  2852. (let ((org-adapt-indentation t)
  2853. (org-src-preserve-indentation t)
  2854. (org-odd-levels-only nil))
  2855. (org-promote))
  2856. (forward-line)
  2857. (org-get-indentation))))
  2858. (should
  2859. (zerop
  2860. (org-test-with-temp-text
  2861. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  2862. (let ((org-adapt-indentation t)
  2863. (org-src-preserve-indentation t)
  2864. (org-odd-levels-only nil))
  2865. (org-promote))
  2866. (forward-line)
  2867. (org-get-indentation))))
  2868. (should
  2869. (zerop
  2870. (org-test-with-temp-text
  2871. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  2872. (let ((org-adapt-indentation t)
  2873. (org-src-preserve-indentation nil)
  2874. (org-odd-levels-only nil))
  2875. (org-promote))
  2876. (forward-line)
  2877. (org-get-indentation)))))
  2878. ;;; Planning
  2879. (ert-deftest test-org/at-planning-p ()
  2880. "Test `org-at-planning-p' specifications."
  2881. ;; Regular test.
  2882. (should
  2883. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  2884. (org-at-planning-p)))
  2885. (should-not
  2886. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  2887. (org-at-planning-p)))
  2888. ;; Correctly find planning attached to inlinetasks.
  2889. (when (featurep 'org-inlinetask)
  2890. (should
  2891. (org-test-with-temp-text
  2892. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  2893. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2894. (should-not
  2895. (org-test-with-temp-text
  2896. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  2897. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2898. (should-not
  2899. (org-test-with-temp-text
  2900. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  2901. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  2902. (should-not
  2903. (org-test-with-temp-text
  2904. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  2905. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  2906. (ert-deftest test-org/add-planning-info ()
  2907. "Test `org-add-planning-info'."
  2908. ;; Create deadline when `org-adapt-indentation' is non-nil.
  2909. (should
  2910. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  2911. (org-test-with-temp-text "* H\nParagraph<point>"
  2912. (let ((org-adapt-indentation t))
  2913. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  2914. (replace-regexp-in-string
  2915. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2916. nil nil 1))))
  2917. ;; Create deadline when `org-adapt-indentation' is nil.
  2918. (should
  2919. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  2920. (org-test-with-temp-text "* H\nParagraph<point>"
  2921. (let ((org-adapt-indentation nil))
  2922. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  2923. (replace-regexp-in-string
  2924. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2925. nil nil 1))))
  2926. ;; Update deadline when `org-adapt-indentation' is non-nil.
  2927. (should
  2928. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  2929. (org-test-with-temp-text "\
  2930. * H
  2931. DEADLINE: <2015-06-24 Wed>
  2932. Paragraph<point>"
  2933. (let ((org-adapt-indentation t))
  2934. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  2935. (replace-regexp-in-string
  2936. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2937. nil nil 1))))
  2938. ;; Update deadline when `org-adapt-indentation' is nil.
  2939. (should
  2940. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  2941. (org-test-with-temp-text "\
  2942. * H
  2943. DEADLINE: <2015-06-24 Wed>
  2944. Paragraph<point>"
  2945. (let ((org-adapt-indentation nil))
  2946. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  2947. (replace-regexp-in-string
  2948. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2949. nil nil 1))))
  2950. ;; Schedule when `org-adapt-indentation' is non-nil.
  2951. (should
  2952. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  2953. (org-test-with-temp-text "* H\nParagraph<point>"
  2954. (let ((org-adapt-indentation t))
  2955. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  2956. (replace-regexp-in-string
  2957. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2958. nil nil 1))))
  2959. ;; Schedule when `org-adapt-indentation' is nil.
  2960. (should
  2961. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  2962. (org-test-with-temp-text "* H\nParagraph<point>"
  2963. (let ((org-adapt-indentation nil))
  2964. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  2965. (replace-regexp-in-string
  2966. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2967. nil nil 1))))
  2968. ;; Add deadline when scheduled.
  2969. (should
  2970. (equal "\
  2971. * H
  2972. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  2973. Paragraph"
  2974. (org-test-with-temp-text "\
  2975. * H
  2976. SCHEDULED: <2015-06-24 Wed>
  2977. Paragraph<point>"
  2978. (let ((org-adapt-indentation t))
  2979. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  2980. (replace-regexp-in-string
  2981. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  2982. nil nil 1))))
  2983. ;; Remove middle entry.
  2984. (should
  2985. (equal "\
  2986. * H
  2987. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  2988. Paragraph"
  2989. (org-test-with-temp-text "\
  2990. * H
  2991. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  2992. Paragraph<point>"
  2993. (let ((org-adapt-indentation t))
  2994. (org-add-planning-info nil nil 'deadline))
  2995. (replace-regexp-in-string
  2996. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  2997. nil nil 1))))
  2998. ;; Remove last entry and then middle entry (order should not
  2999. ;; matter).
  3000. (should
  3001. (equal "\
  3002. * H
  3003. CLOSED: [2015-06-24]
  3004. Paragraph"
  3005. (org-test-with-temp-text "\
  3006. * H
  3007. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3008. Paragraph<point>"
  3009. (let ((org-adapt-indentation t))
  3010. (org-add-planning-info nil nil 'scheduled 'deadline))
  3011. (replace-regexp-in-string
  3012. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3013. nil nil 1))))
  3014. ;; Remove closed when `org-adapt-indentation' is non-nil.
  3015. (should
  3016. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3017. (org-test-with-temp-text "\
  3018. * H
  3019. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3020. Paragraph<point>"
  3021. (let ((org-adapt-indentation t))
  3022. (org-add-planning-info nil nil 'closed))
  3023. (replace-regexp-in-string
  3024. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3025. nil nil 1))))
  3026. (should
  3027. (equal "* H\n Paragraph"
  3028. (org-test-with-temp-text "\
  3029. * H
  3030. CLOSED: [2015-06-25 Thu]
  3031. Paragraph<point>"
  3032. (let ((org-adapt-indentation t))
  3033. (org-add-planning-info nil nil 'closed))
  3034. (replace-regexp-in-string
  3035. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3036. nil nil 1))))
  3037. ;; Remove closed when `org-adapt-indentation' is nil.
  3038. (should
  3039. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3040. (org-test-with-temp-text "\
  3041. * H
  3042. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3043. Paragraph<point>"
  3044. (let ((org-adapt-indentation nil))
  3045. (org-add-planning-info nil nil 'closed))
  3046. (replace-regexp-in-string
  3047. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3048. nil nil 1))))
  3049. (should
  3050. (equal "* H\nParagraph"
  3051. (org-test-with-temp-text "\
  3052. * H
  3053. CLOSED: [2015-06-25 Thu]
  3054. Paragraph<point>"
  3055. (let ((org-adapt-indentation nil))
  3056. (org-add-planning-info nil nil 'closed))
  3057. (replace-regexp-in-string
  3058. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3059. nil nil 1))))
  3060. ;; Remove closed entry and delete empty line.
  3061. (should
  3062. (equal "\
  3063. * H
  3064. Paragraph"
  3065. (org-test-with-temp-text "\
  3066. * H
  3067. CLOSED: [2015-06-24 Wed]
  3068. Paragraph<point>"
  3069. (let ((org-adapt-indentation t))
  3070. (org-add-planning-info nil nil 'closed))
  3071. (replace-regexp-in-string
  3072. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3073. nil nil 1))))
  3074. ;; Remove one entry and update another.
  3075. (should
  3076. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3077. (org-test-with-temp-text "\
  3078. * H
  3079. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  3080. Paragraph<point>"
  3081. (let ((org-adapt-indentation t))
  3082. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  3083. (replace-regexp-in-string
  3084. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3085. nil nil 1)))))
  3086. ;;; Property API
  3087. (ert-deftest test-org/buffer-property-keys ()
  3088. "Test `org-buffer-property-keys' specifications."
  3089. ;; Retrieve properties accross siblings.
  3090. (should
  3091. (equal '("A" "B")
  3092. (org-test-with-temp-text "
  3093. * H1
  3094. :PROPERTIES:
  3095. :A: 1
  3096. :END:
  3097. * H2
  3098. :PROPERTIES:
  3099. :B: 1
  3100. :END:"
  3101. (org-buffer-property-keys))))
  3102. ;; Retrieve properties accross children.
  3103. (should
  3104. (equal '("A" "B")
  3105. (org-test-with-temp-text "
  3106. * H1
  3107. :PROPERTIES:
  3108. :A: 1
  3109. :END:
  3110. ** H2
  3111. :PROPERTIES:
  3112. :B: 1
  3113. :END:"
  3114. (org-buffer-property-keys))))
  3115. ;; Retrieve muliple properties in the same drawer.
  3116. (should
  3117. (equal '("A" "B")
  3118. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3119. (org-buffer-property-keys))))
  3120. ;; Ignore extension symbol in property name.
  3121. (should
  3122. (equal '("A")
  3123. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3124. (org-buffer-property-keys))))
  3125. ;; With non-nil COLUMNS, extract property names from columns.
  3126. (should
  3127. (equal '("A" "B")
  3128. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  3129. (org-buffer-property-keys nil nil t))))
  3130. (should
  3131. (equal '("A" "B" "COLUMNS")
  3132. (org-test-with-temp-text
  3133. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  3134. (org-buffer-property-keys nil nil t)))))
  3135. (ert-deftest test-org/property-values ()
  3136. "Test `org-property-values' specifications."
  3137. ;; Regular test.
  3138. (should
  3139. (equal '("2" "1")
  3140. (org-test-with-temp-text
  3141. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  3142. (org-property-values "A"))))
  3143. ;; Ignore empty values.
  3144. (should-not
  3145. (org-test-with-temp-text
  3146. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  3147. (org-property-values "A")))
  3148. ;; Take into consideration extended values.
  3149. (should
  3150. (equal '("1 2")
  3151. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3152. (org-property-values "A")))))
  3153. (ert-deftest test-org/find-property ()
  3154. "Test `org-find-property' specifications."
  3155. ;; Regular test.
  3156. (should
  3157. (= 1
  3158. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  3159. (org-find-property "prop"))))
  3160. ;; Ignore false positives.
  3161. (should
  3162. (= 27
  3163. (org-test-with-temp-text
  3164. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  3165. (org-find-property "A"))))
  3166. ;; Return first entry found in buffer.
  3167. (should
  3168. (= 1
  3169. (org-test-with-temp-text
  3170. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3171. (org-find-property "A"))))
  3172. ;; Only search visible part of the buffer.
  3173. (should
  3174. (= 31
  3175. (org-test-with-temp-text
  3176. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3177. (org-narrow-to-subtree)
  3178. (org-find-property "A"))))
  3179. ;; With optional argument, only find entries with a specific value.
  3180. (should-not
  3181. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3182. (org-find-property "A" "2")))
  3183. (should
  3184. (= 31
  3185. (org-test-with-temp-text
  3186. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  3187. (org-find-property "A" "2"))))
  3188. ;; Use "nil" for explicit nil values.
  3189. (should
  3190. (= 31
  3191. (org-test-with-temp-text
  3192. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  3193. (org-find-property "A" "nil")))))
  3194. (ert-deftest test-org/entry-delete ()
  3195. "Test `org-entry-delete' specifications."
  3196. ;; Regular test.
  3197. (should
  3198. (string-match
  3199. " *:PROPERTIES:\n *:B: +2\n *:END:"
  3200. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3201. (org-entry-delete (point) "A")
  3202. (buffer-string))))
  3203. ;; Also remove accumulated properties.
  3204. (should-not
  3205. (string-match
  3206. ":A"
  3207. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  3208. (org-entry-delete (point) "A")
  3209. (buffer-string))))
  3210. ;; When last property is removed, remove the property drawer.
  3211. (should-not
  3212. (string-match
  3213. ":PROPERTIES:"
  3214. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  3215. (org-entry-delete (point) "A")
  3216. (buffer-string))))
  3217. ;; Return a non-nil value when some property was removed.
  3218. (should
  3219. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3220. (org-entry-delete (point) "A")))
  3221. (should-not
  3222. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3223. (org-entry-delete (point) "C"))))
  3224. (ert-deftest test-org/entry-get ()
  3225. "Test `org-entry-get' specifications."
  3226. ;; Regular test.
  3227. (should
  3228. (equal "1"
  3229. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3230. (org-entry-get (point) "A"))))
  3231. ;; Ignore case.
  3232. (should
  3233. (equal "1"
  3234. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3235. (org-entry-get (point) "a"))))
  3236. ;; Handle extended values, both before and after base value.
  3237. (should
  3238. (equal "1 2 3"
  3239. (org-test-with-temp-text
  3240. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3241. (org-entry-get (point) "A"))))
  3242. ;; Empty values are returned as the empty string.
  3243. (should
  3244. (equal ""
  3245. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  3246. (org-entry-get (point) "A"))))
  3247. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  3248. ;; otherwise, return nil.
  3249. (should-not
  3250. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3251. (org-entry-get (point) "A")))
  3252. (should
  3253. (equal "nil"
  3254. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3255. (org-entry-get (point) "A" nil t))))
  3256. ;; Return nil when no property is found, independently on the
  3257. ;; LITERAL-NIL argument.
  3258. (should-not
  3259. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3260. (org-entry-get (point) "B")))
  3261. (should-not
  3262. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3263. (org-entry-get (point) "B" nil t)))
  3264. ;; Handle inheritance, when allowed. Include extended values and
  3265. ;; possibly global values.
  3266. (should
  3267. (equal
  3268. "1"
  3269. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3270. (org-entry-get (point) "A" t))))
  3271. (should
  3272. (equal
  3273. "1"
  3274. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3275. (let ((org-use-property-inheritance t))
  3276. (org-entry-get (point) "A" 'selective)))))
  3277. (should-not
  3278. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3279. (let ((org-use-property-inheritance nil))
  3280. (org-entry-get (point) "A" 'selective))))
  3281. (should
  3282. (equal
  3283. "1 2"
  3284. (org-test-with-temp-text
  3285. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  3286. (org-entry-get (point-max) "A" t))))
  3287. (should
  3288. (equal "1"
  3289. (org-test-with-temp-text
  3290. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  3291. (org-mode-restart)
  3292. (org-entry-get (point-max) "A" t))))
  3293. (should
  3294. (equal "0 1"
  3295. (org-test-with-temp-text
  3296. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  3297. (org-mode-restart)
  3298. (org-entry-get (point-max) "A" t)))))
  3299. (ert-deftest test-org/entry-properties ()
  3300. "Test `org-entry-properties' specifications."
  3301. ;; Get "ITEM" property.
  3302. (should
  3303. (equal "* H"
  3304. (org-test-with-temp-text "* TODO H"
  3305. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  3306. (should
  3307. (equal "* H"
  3308. (org-test-with-temp-text "* TODO H"
  3309. (cdr (assoc "ITEM" (org-entry-properties))))))
  3310. ;; Get "TODO" property. TODO keywords are case sensitive.
  3311. (should
  3312. (equal "TODO"
  3313. (org-test-with-temp-text "* TODO H"
  3314. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  3315. (should
  3316. (equal "TODO"
  3317. (org-test-with-temp-text "* TODO H"
  3318. (cdr (assoc "TODO" (org-entry-properties))))))
  3319. (should-not
  3320. (org-test-with-temp-text "* H"
  3321. (assoc "TODO" (org-entry-properties nil "TODO"))))
  3322. (should-not
  3323. (org-test-with-temp-text "* todo H"
  3324. (assoc "TODO" (org-entry-properties nil "TODO"))))
  3325. ;; Get "PRIORITY" property.
  3326. (should
  3327. (equal "A"
  3328. (org-test-with-temp-text "* [#A] H"
  3329. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  3330. (should
  3331. (equal "A"
  3332. (org-test-with-temp-text "* [#A] H"
  3333. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  3334. (should
  3335. (equal (char-to-string org-default-priority)
  3336. (org-test-with-temp-text "* H"
  3337. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  3338. ;; Get "FILE" property.
  3339. (should
  3340. (org-test-with-temp-text-in-file "* H\nParagraph"
  3341. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  3342. (buffer-file-name))))
  3343. (should
  3344. (org-test-with-temp-text-in-file "* H\nParagraph"
  3345. (org-file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  3346. (buffer-file-name))))
  3347. (should-not
  3348. (org-test-with-temp-text "* H\nParagraph"
  3349. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  3350. ;; Get "TAGS" property.
  3351. (should
  3352. (equal ":tag1:tag2:"
  3353. (org-test-with-temp-text "* H :tag1:tag2:"
  3354. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  3355. (should
  3356. (equal ":tag1:tag2:"
  3357. (org-test-with-temp-text "* H :tag1:tag2:"
  3358. (cdr (assoc "TAGS" (org-entry-properties))))))
  3359. (should-not
  3360. (org-test-with-temp-text "* H"
  3361. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  3362. ;; Get "ALLTAGS" property.
  3363. (should
  3364. (equal ":tag1:tag2:"
  3365. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  3366. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  3367. (should
  3368. (equal ":tag1:tag2:"
  3369. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  3370. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  3371. (should-not
  3372. (org-test-with-temp-text "* H"
  3373. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  3374. ;; Get "BLOCKED" property.
  3375. (should
  3376. (equal "t"
  3377. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  3378. (let ((org-enforce-todo-dependencies t)
  3379. (org-blocker-hook
  3380. '(org-block-todo-from-children-or-siblings-or-parent)))
  3381. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  3382. (should
  3383. (equal ""
  3384. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  3385. (let ((org-enforce-todo-dependencies t)
  3386. (org-blocker-hook
  3387. '(org-block-todo-from-children-or-siblings-or-parent)))
  3388. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  3389. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  3390. (should
  3391. (equal
  3392. "[2012-03-29 thu.]"
  3393. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  3394. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  3395. (should
  3396. (equal
  3397. "[2012-03-29 thu.]"
  3398. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  3399. (cdr (assoc "CLOSED" (org-entry-properties))))))
  3400. (should-not
  3401. (org-test-with-temp-text "* H"
  3402. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  3403. (should
  3404. (equal
  3405. "<2014-03-04 tue.>"
  3406. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3407. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  3408. (should
  3409. (equal
  3410. "<2014-03-04 tue.>"
  3411. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3412. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  3413. (should-not
  3414. (org-test-with-temp-text "* H"
  3415. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  3416. (should
  3417. (equal
  3418. "<2014-03-04 tue.>"
  3419. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3420. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  3421. (should
  3422. (equal
  3423. "<2014-03-04 tue.>"
  3424. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3425. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  3426. (should-not
  3427. (org-test-with-temp-text "* H"
  3428. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  3429. ;; Get "CATEGORY"
  3430. (should
  3431. (equal "cat"
  3432. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  3433. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  3434. (should
  3435. (equal "cat"
  3436. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  3437. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3438. (should
  3439. (equal "cat"
  3440. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  3441. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3442. (should
  3443. (equal "cat2"
  3444. (org-test-with-temp-text
  3445. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  3446. "\n"
  3447. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  3448. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3449. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  3450. (should
  3451. (equal "<2012-03-29 thu.>"
  3452. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  3453. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  3454. (should
  3455. (equal "[2012-03-29 thu.]"
  3456. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  3457. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  3458. (should
  3459. (equal "<2012-03-29 thu.>"
  3460. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  3461. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  3462. (should
  3463. (equal "[2014-03-04 tue.]"
  3464. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  3465. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
  3466. ;; Get standard properties.
  3467. (should
  3468. (equal "1"
  3469. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3470. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3471. ;; Handle extended properties.
  3472. (should
  3473. (equal "1 2 3"
  3474. (org-test-with-temp-text
  3475. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3476. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3477. (should
  3478. (equal "1 2 3"
  3479. (org-test-with-temp-text
  3480. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  3481. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3482. ;; Ignore forbidden (special) properties.
  3483. (should-not
  3484. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  3485. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  3486. (ert-deftest test-org/entry-put ()
  3487. "Test `org-entry-put' specifications."
  3488. ;; Error when not a string or nil.
  3489. (should-error
  3490. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  3491. (org-entry-put 1 "test" 2)))
  3492. ;; Set "TODO" property.
  3493. (should
  3494. (string-match (regexp-quote " TODO H")
  3495. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  3496. (org-entry-put (point) "TODO" "TODO")
  3497. (buffer-string))))
  3498. (should
  3499. (string-match (regexp-quote "* H")
  3500. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  3501. (org-entry-put (point) "TODO" nil)
  3502. (buffer-string))))
  3503. ;; Set "PRIORITY" property.
  3504. (should
  3505. (equal "* [#A] H"
  3506. (org-test-with-temp-text "* [#B] H"
  3507. (org-entry-put (point) "PRIORITY" "A")
  3508. (buffer-string))))
  3509. (should
  3510. (equal "* H"
  3511. (org-test-with-temp-text "* [#B] H"
  3512. (org-entry-put (point) "PRIORITY" nil)
  3513. (buffer-string))))
  3514. ;; Set "SCHEDULED" property.
  3515. (should
  3516. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  3517. (org-test-with-temp-text "* H"
  3518. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  3519. (buffer-string))))
  3520. (should
  3521. (string= "* H\n"
  3522. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3523. (org-entry-put (point) "SCHEDULED" nil)
  3524. (buffer-string))))
  3525. (should
  3526. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  3527. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3528. (org-entry-put (point) "SCHEDULED" "earlier")
  3529. (buffer-string))))
  3530. (should
  3531. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  3532. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3533. (org-entry-put (point) "SCHEDULED" "later")
  3534. (buffer-string))))
  3535. ;; Set "DEADLINE" property.
  3536. (should
  3537. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  3538. (org-test-with-temp-text "* H"
  3539. (org-entry-put (point) "DEADLINE" "2014-03-04")
  3540. (buffer-string))))
  3541. (should
  3542. (string= "* H\n"
  3543. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3544. (org-entry-put (point) "DEADLINE" nil)
  3545. (buffer-string))))
  3546. (should
  3547. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  3548. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3549. (org-entry-put (point) "DEADLINE" "earlier")
  3550. (buffer-string))))
  3551. (should
  3552. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  3553. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3554. (org-entry-put (point) "DEADLINE" "later")
  3555. (buffer-string))))
  3556. ;; Set "CATEGORY" property
  3557. (should
  3558. (string-match "^ *:CATEGORY: cat"
  3559. (org-test-with-temp-text "* H"
  3560. (org-entry-put (point) "CATEGORY" "cat")
  3561. (buffer-string))))
  3562. ;; Regular properties, with or without pre-existing drawer.
  3563. (should
  3564. (string-match "^ *:A: +2$"
  3565. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3566. (org-entry-put (point) "A" "2")
  3567. (buffer-string))))
  3568. (should
  3569. (string-match "^ *:A: +1$"
  3570. (org-test-with-temp-text "* H"
  3571. (org-entry-put (point) "A" "1")
  3572. (buffer-string))))
  3573. ;; Special case: two consecutive headlines.
  3574. (should
  3575. (string-match "\\* A\n *:PROPERTIES:"
  3576. (org-test-with-temp-text "* A\n** B"
  3577. (org-entry-put (point) "A" "1")
  3578. (buffer-string)))))
  3579. ;;; Radio Targets
  3580. (ert-deftest test-org/update-radio-target-regexp ()
  3581. "Test `org-update-radio-target-regexp' specifications."
  3582. ;; Properly update cache with no previous radio target regexp.
  3583. (should
  3584. (eq 'link
  3585. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  3586. (save-excursion (goto-char (point-max)) (org-element-context))
  3587. (insert "<<<")
  3588. (search-forward "o")
  3589. (insert ">>>")
  3590. (org-update-radio-target-regexp)
  3591. (goto-char (point-max))
  3592. (org-element-type (org-element-context)))))
  3593. ;; Properly update cache with previous radio target regexp.
  3594. (should
  3595. (eq 'link
  3596. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  3597. (save-excursion (goto-char (point-max)) (org-element-context))
  3598. (insert "<<<")
  3599. (search-forward "o")
  3600. (insert ">>>")
  3601. (org-update-radio-target-regexp)
  3602. (search-backward "r")
  3603. (delete-char 5)
  3604. (insert "new")
  3605. (org-update-radio-target-regexp)
  3606. (goto-char (point-max))
  3607. (delete-region (line-beginning-position) (point))
  3608. (insert "new")
  3609. (org-element-type (org-element-context))))))
  3610. ;;; Sparse trees
  3611. (ert-deftest test-org/match-sparse-tree ()
  3612. "Test `org-match-sparse-tree' specifications."
  3613. ;; Match tags.
  3614. (should-not
  3615. (org-test-with-temp-text "* H\n** H1 :tag:"
  3616. (org-match-sparse-tree nil "tag")
  3617. (search-forward "H1")
  3618. (org-invisible-p2)))
  3619. (should
  3620. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  3621. (org-match-sparse-tree nil "tag")
  3622. (search-forward "H2")
  3623. (org-invisible-p2)))
  3624. ;; "-" operator for tags.
  3625. (should-not
  3626. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3627. (org-match-sparse-tree nil "tag1-tag2")
  3628. (search-forward "H1")
  3629. (org-invisible-p2)))
  3630. (should
  3631. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3632. (org-match-sparse-tree nil "tag1-tag2")
  3633. (search-forward "H2")
  3634. (org-invisible-p2)))
  3635. ;; "&" operator for tags.
  3636. (should
  3637. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3638. (org-match-sparse-tree nil "tag1&tag2")
  3639. (search-forward "H1")
  3640. (org-invisible-p2)))
  3641. (should-not
  3642. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3643. (org-match-sparse-tree nil "tag1&tag2")
  3644. (search-forward "H2")
  3645. (org-invisible-p2)))
  3646. ;; "|" operator for tags.
  3647. (should-not
  3648. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3649. (org-match-sparse-tree nil "tag1|tag2")
  3650. (search-forward "H1")
  3651. (org-invisible-p2)))
  3652. (should-not
  3653. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  3654. (org-match-sparse-tree nil "tag1|tag2")
  3655. (search-forward "H2")
  3656. (org-invisible-p2)))
  3657. ;; Regexp match on tags.
  3658. (should-not
  3659. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  3660. (org-match-sparse-tree nil "{^tag.*}")
  3661. (search-forward "H1")
  3662. (org-invisible-p2)))
  3663. (should
  3664. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  3665. (org-match-sparse-tree nil "{^tag.*}")
  3666. (search-forward "H2")
  3667. (org-invisible-p2)))
  3668. ;; Match group tags.
  3669. (should-not
  3670. (org-test-with-temp-text
  3671. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  3672. (org-match-sparse-tree nil "work")
  3673. (search-forward "H1")
  3674. (org-invisible-p2)))
  3675. (should-not
  3676. (org-test-with-temp-text
  3677. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  3678. (org-match-sparse-tree nil "work")
  3679. (search-forward "H2")
  3680. (org-invisible-p2)))
  3681. ;; Match group tags with hard brackets.
  3682. (should-not
  3683. (org-test-with-temp-text
  3684. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  3685. (org-match-sparse-tree nil "work")
  3686. (search-forward "H1")
  3687. (org-invisible-p2)))
  3688. (should-not
  3689. (org-test-with-temp-text
  3690. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  3691. (org-match-sparse-tree nil "work")
  3692. (search-forward "H2")
  3693. (org-invisible-p2)))
  3694. ;; Match tags in hierarchies
  3695. (should-not
  3696. (org-test-with-temp-text
  3697. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  3698. #+TAGS: [ Lev_2 : Lev_3 ]\n
  3699. #+TAGS: { Lev_3 : Lev_4 }\n
  3700. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  3701. (org-match-sparse-tree nil "Lev_1")
  3702. (search-forward "H4")
  3703. (org-invisible-p2)))
  3704. ;; Match regular expressions in tags
  3705. (should-not
  3706. (org-test-with-temp-text
  3707. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  3708. (org-match-sparse-tree nil "Lev")
  3709. (search-forward "H1")
  3710. (org-invisible-p2)))
  3711. (should
  3712. (org-test-with-temp-text
  3713. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  3714. (org-match-sparse-tree nil "Lev")
  3715. (search-forward "H1")
  3716. (org-invisible-p2)))
  3717. ;; Match properties.
  3718. (should
  3719. (org-test-with-temp-text
  3720. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  3721. (org-match-sparse-tree nil "A=\"1\"")
  3722. (search-forward "H2")
  3723. (org-invisible-p2)))
  3724. (should-not
  3725. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  3726. (org-match-sparse-tree nil "A=\"1\"")
  3727. (search-forward "H2")
  3728. (org-invisible-p2)))
  3729. ;; Case is not significant when matching properties.
  3730. (should-not
  3731. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  3732. (org-match-sparse-tree nil "a=\"1\"")
  3733. (search-forward "H2")
  3734. (org-invisible-p2)))
  3735. (should-not
  3736. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  3737. (org-match-sparse-tree nil "A=\"1\"")
  3738. (search-forward "H2")
  3739. (org-invisible-p2)))
  3740. ;; Match special LEVEL property.
  3741. (should-not
  3742. (org-test-with-temp-text "* H\n** H1\n*** H2"
  3743. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  3744. (search-forward "H1")
  3745. (org-invisible-p2)))
  3746. (should
  3747. (org-test-with-temp-text "* H\n** H1\n*** H2"
  3748. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  3749. (search-forward "H2")
  3750. (org-invisible-p2)))
  3751. ;; Comparison operators when matching properties.
  3752. (should
  3753. (org-test-with-temp-text
  3754. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  3755. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  3756. (search-forward "H1")
  3757. (org-invisible-p2)))
  3758. (should-not
  3759. (org-test-with-temp-text
  3760. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  3761. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  3762. (search-forward "H2")
  3763. (org-invisible-p2)))
  3764. ;; Regexp match on properties values.
  3765. (should-not
  3766. (org-test-with-temp-text
  3767. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  3768. (org-match-sparse-tree nil "A={f.*}")
  3769. (search-forward "H1")
  3770. (org-invisible-p2)))
  3771. (should
  3772. (org-test-with-temp-text
  3773. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  3774. (org-match-sparse-tree nil "A={f.*}")
  3775. (search-forward "H2")
  3776. (org-invisible-p2)))
  3777. ;; With an optional argument, limit match to TODO entries.
  3778. (should-not
  3779. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  3780. (org-match-sparse-tree t "tag")
  3781. (search-forward "H1")
  3782. (org-invisible-p2)))
  3783. (should
  3784. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  3785. (org-match-sparse-tree t "tag")
  3786. (search-forward "H2")
  3787. (org-invisible-p2))))
  3788. ;;; Timestamps API
  3789. (ert-deftest test-org/time-stamp ()
  3790. "Test `org-time-stamp' specifications."
  3791. ;; Insert chosen time stamp at point.
  3792. (should
  3793. (string-match
  3794. "Te<2014-03-04 .*?>xt"
  3795. (org-test-with-temp-text "Te<point>xt"
  3796. (flet ((org-read-date
  3797. (&rest args)
  3798. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3799. (org-time-stamp nil)
  3800. (buffer-string)))))
  3801. ;; With a prefix argument, also insert time.
  3802. (should
  3803. (string-match
  3804. "Te<2014-03-04 .*? 00:41>xt"
  3805. (org-test-with-temp-text "Te<point>xt"
  3806. (flet ((org-read-date
  3807. (&rest args)
  3808. (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
  3809. (org-time-stamp '(4))
  3810. (buffer-string)))))
  3811. ;; With two universal prefix arguments, insert an active timestamp
  3812. ;; with the current time without prompting the user.
  3813. (should
  3814. (string-match
  3815. "Te<2014-03-04 .*? 00:41>xt"
  3816. (org-test-with-temp-text "Te<point>xt"
  3817. (flet ((current-time
  3818. ()
  3819. (apply #'encode-time (org-parse-time-string "2014-03-04 00:41"))))
  3820. (org-time-stamp '(16))
  3821. (buffer-string)))))
  3822. ;; When optional argument is non-nil, insert an inactive timestamp.
  3823. (should
  3824. (string-match
  3825. "Te\\[2014-03-04 .*?\\]xt"
  3826. (org-test-with-temp-text "Te<point>xt"
  3827. (flet ((org-read-date
  3828. (&rest args)
  3829. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3830. (org-time-stamp nil t)
  3831. (buffer-string)))))
  3832. ;; When called from a timestamp, replace existing one.
  3833. (should
  3834. (string-match
  3835. "<2014-03-04 .*?>"
  3836. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  3837. (flet ((org-read-date
  3838. (&rest args)
  3839. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3840. (org-time-stamp nil)
  3841. (buffer-string)))))
  3842. (should
  3843. (string-match
  3844. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  3845. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  3846. (flet ((org-read-date
  3847. (&rest args)
  3848. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3849. (org-time-stamp nil)
  3850. (buffer-string)))))
  3851. ;; When replacing a timestamp, preserve repeater, if any.
  3852. (should
  3853. (string-match
  3854. "<2014-03-04 .*? \\+2y>"
  3855. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  3856. (flet ((org-read-date
  3857. (&rest args)
  3858. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3859. (org-time-stamp nil)
  3860. (buffer-string)))))
  3861. ;; When called twice in a raw, build a date range.
  3862. (should
  3863. (string-match
  3864. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  3865. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  3866. (flet ((org-read-date
  3867. (&rest args)
  3868. (apply #'encode-time (org-parse-time-string "2014-03-04"))))
  3869. (let ((last-command 'org-time-stamp)
  3870. (this-command 'org-time-stamp))
  3871. (org-time-stamp nil))
  3872. (buffer-string))))))
  3873. (ert-deftest test-org/timestamp-has-time-p ()
  3874. "Test `org-timestamp-has-time-p' specifications."
  3875. ;; With time.
  3876. (should
  3877. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  3878. (org-timestamp-has-time-p (org-element-context))))
  3879. ;; Without time.
  3880. (should-not
  3881. (org-test-with-temp-text "<2012-03-29 Thu>"
  3882. (org-timestamp-has-time-p (org-element-context)))))
  3883. (ert-deftest test-org/timestamp-format ()
  3884. "Test `org-timestamp-format' specifications."
  3885. ;; Regular test.
  3886. (should
  3887. (equal
  3888. "2012-03-29 16:40"
  3889. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  3890. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  3891. ;; Range end.
  3892. (should
  3893. (equal
  3894. "2012-03-29"
  3895. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  3896. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  3897. (ert-deftest test-org/timestamp-split-range ()
  3898. "Test `org-timestamp-split-range' specifications."
  3899. ;; Extract range start (active).
  3900. (should
  3901. (equal '(2012 3 29)
  3902. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3903. (let ((ts (org-timestamp-split-range (org-element-context))))
  3904. (mapcar (lambda (p) (org-element-property p ts))
  3905. '(:year-end :month-end :day-end))))))
  3906. ;; Extract range start (inactive)
  3907. (should
  3908. (equal '(2012 3 29)
  3909. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  3910. (let ((ts (org-timestamp-split-range (org-element-context))))
  3911. (mapcar (lambda (p) (org-element-property p ts))
  3912. '(:year-end :month-end :day-end))))))
  3913. ;; Extract range end (active).
  3914. (should
  3915. (equal '(2012 3 30)
  3916. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3917. (let ((ts (org-timestamp-split-range
  3918. (org-element-context) t)))
  3919. (mapcar (lambda (p) (org-element-property p ts))
  3920. '(:year-end :month-end :day-end))))))
  3921. ;; Extract range end (inactive)
  3922. (should
  3923. (equal '(2012 3 30)
  3924. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  3925. (let ((ts (org-timestamp-split-range
  3926. (org-element-context) t)))
  3927. (mapcar (lambda (p) (org-element-property p ts))
  3928. '(:year-end :month-end :day-end))))))
  3929. ;; Return the timestamp if not a range.
  3930. (should
  3931. (org-test-with-temp-text "[2012-03-29 Thu]"
  3932. (let* ((ts-orig (org-element-context))
  3933. (ts-copy (org-timestamp-split-range ts-orig)))
  3934. (eq ts-orig ts-copy))))
  3935. (should
  3936. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  3937. (let* ((ts-orig (org-element-context))
  3938. (ts-copy (org-timestamp-split-range ts-orig)))
  3939. (eq ts-orig ts-copy)))))
  3940. (ert-deftest test-org/timestamp-translate ()
  3941. "Test `org-timestamp-translate' specifications."
  3942. ;; Translate whole date range.
  3943. (should
  3944. (equal "<29>--<30>"
  3945. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3946. (let ((org-display-custom-times t)
  3947. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3948. (org-timestamp-translate (org-element-context))))))
  3949. ;; Translate date range start.
  3950. (should
  3951. (equal "<29>"
  3952. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3953. (let ((org-display-custom-times t)
  3954. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3955. (org-timestamp-translate (org-element-context) 'start)))))
  3956. ;; Translate date range end.
  3957. (should
  3958. (equal "<30>"
  3959. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  3960. (let ((org-display-custom-times t)
  3961. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3962. (org-timestamp-translate (org-element-context) 'end)))))
  3963. ;; Translate time range.
  3964. (should
  3965. (equal "<08>--<16>"
  3966. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  3967. (let ((org-display-custom-times t)
  3968. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  3969. (org-timestamp-translate (org-element-context))))))
  3970. ;; Translate non-range timestamp.
  3971. (should
  3972. (equal "<29>"
  3973. (org-test-with-temp-text "<2012-03-29 Thu>"
  3974. (let ((org-display-custom-times t)
  3975. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3976. (org-timestamp-translate (org-element-context))))))
  3977. ;; Do not change `diary' timestamps.
  3978. (should
  3979. (equal "<%%(org-float t 4 2)>"
  3980. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  3981. (let ((org-display-custom-times t)
  3982. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  3983. (org-timestamp-translate (org-element-context)))))))
  3984. ;;; Visibility
  3985. (ert-deftest test-org/flag-drawer ()
  3986. "Test `org-flag-drawer' specifications."
  3987. ;; Hide drawer.
  3988. (should
  3989. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3990. (org-flag-drawer t)
  3991. (get-char-property (line-end-position) 'invisible)))
  3992. ;; Show drawer.
  3993. (should-not
  3994. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  3995. (org-flag-drawer t)
  3996. (org-flag-drawer nil)
  3997. (get-char-property (line-end-position) 'invisible)))
  3998. ;; Test optional argument.
  3999. (should
  4000. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4001. (let ((drawer (save-excursion (search-forward ":D2")
  4002. (org-element-at-point))))
  4003. (org-flag-drawer t drawer)
  4004. (get-char-property (progn (search-forward ":D2") (line-end-position))
  4005. 'invisible))))
  4006. (should-not
  4007. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4008. (let ((drawer (save-excursion (search-forward ":D2")
  4009. (org-element-at-point))))
  4010. (org-flag-drawer t drawer)
  4011. (get-char-property (line-end-position) 'invisible))))
  4012. ;; Do not hide fake drawers.
  4013. (should-not
  4014. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  4015. (forward-line 1)
  4016. (org-flag-drawer t)
  4017. (get-char-property (line-end-position) 'invisible)))
  4018. ;; Do not hide incomplete drawers.
  4019. (should-not
  4020. (org-test-with-temp-text ":D:\nparagraph"
  4021. (forward-line 1)
  4022. (org-flag-drawer t)
  4023. (get-char-property (line-end-position) 'invisible)))
  4024. ;; Do not hide drawers when called from final blank lines.
  4025. (should-not
  4026. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  4027. (goto-char (point-max))
  4028. (org-flag-drawer t)
  4029. (goto-char (point-min))
  4030. (get-char-property (line-end-position) 'invisible)))
  4031. ;; Don't leave point in an invisible part of the buffer when hiding
  4032. ;; a drawer away.
  4033. (should-not
  4034. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4035. (goto-char (point-max))
  4036. (org-flag-drawer t)
  4037. (get-char-property (point) 'invisible))))
  4038. (ert-deftest test-org/hide-block-toggle ()
  4039. "Test `org-hide-block-toggle' specifications."
  4040. ;; Error when not at a block.
  4041. (should-error
  4042. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  4043. (org-hide-block-toggle 'off)
  4044. (get-char-property (line-end-position) 'invisible)))
  4045. ;; Hide block.
  4046. (should
  4047. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  4048. (org-hide-block-toggle)
  4049. (get-char-property (line-end-position) 'invisible)))
  4050. (should
  4051. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  4052. (org-hide-block-toggle)
  4053. (get-char-property (line-end-position) 'invisible)))
  4054. ;; Show block unconditionally when optional argument is `off'.
  4055. (should-not
  4056. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4057. (org-hide-block-toggle)
  4058. (org-hide-block-toggle 'off)
  4059. (get-char-property (line-end-position) 'invisible)))
  4060. (should-not
  4061. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4062. (org-hide-block-toggle 'off)
  4063. (get-char-property (line-end-position) 'invisible)))
  4064. ;; Hide block unconditionally when optional argument is non-nil.
  4065. (should
  4066. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4067. (org-hide-block-toggle t)
  4068. (get-char-property (line-end-position) 'invisible)))
  4069. (should
  4070. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4071. (org-hide-block-toggle)
  4072. (org-hide-block-toggle t)
  4073. (get-char-property (line-end-position) 'invisible)))
  4074. ;; Do not hide block when called from final blank lines.
  4075. (should-not
  4076. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  4077. (org-hide-block-toggle)
  4078. (goto-char (point-min))
  4079. (get-char-property (line-end-position) 'invisible)))
  4080. ;; Don't leave point in an invisible part of the buffer when hiding
  4081. ;; a block away.
  4082. (should-not
  4083. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  4084. (org-hide-block-toggle)
  4085. (get-char-property (point) 'invisible))))
  4086. (ert-deftest test-org/hide-block-toggle-maybe ()
  4087. "Test `org-hide-block-toggle-maybe' specifications."
  4088. (should
  4089. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  4090. (org-hide-block-toggle-maybe)))
  4091. (should-not
  4092. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  4093. (ert-deftest test-org/show-set-visibility ()
  4094. "Test `org-show-set-visibility' specifications."
  4095. ;; Do not throw an error before first heading.
  4096. (should
  4097. (org-test-with-temp-text "Preamble\n* Headline"
  4098. (org-show-set-visibility 'tree)
  4099. t))
  4100. ;; Test all visibility spans, both on headline and in entry.
  4101. (let ((list-visible-lines
  4102. (lambda (state headerp)
  4103. (org-test-with-temp-text "* Grandmother (0)
  4104. ** Uncle (1)
  4105. *** Heir (2)
  4106. ** Father (3)
  4107. Ancestor text (4)
  4108. *** Sister (5)
  4109. Sibling text (6)
  4110. *** Self (7)
  4111. Match (8)
  4112. **** First born (9)
  4113. Child text (10)
  4114. **** The other child (11)
  4115. *** Brother (12)
  4116. ** Aunt (13)
  4117. "
  4118. (org-cycle t)
  4119. (search-forward (if headerp "Self" "Match"))
  4120. (org-show-set-visibility state)
  4121. (goto-char (point-min))
  4122. (let (result (line 0))
  4123. (while (not (eobp))
  4124. (unless (org-invisible-p2) (push line result))
  4125. (incf line)
  4126. (forward-line))
  4127. (nreverse result))))))
  4128. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  4129. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  4130. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  4131. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  4132. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  4133. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  4134. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  4135. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  4136. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  4137. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  4138. (funcall list-visible-lines 'tree nil)))
  4139. (should (equal '(0 1 3 4 5 7 12 13)
  4140. (funcall list-visible-lines 'canonical t)))
  4141. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  4142. (funcall list-visible-lines 'canonical nil)))))
  4143. (provide 'test-org)
  4144. ;;; test-org.el ends here