test-org-table.el 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111
  1. ;;; test-org-table.el --- tests for org-table.el
  2. ;; Copyright (c) David Maus
  3. ;; Authors: David Maus, Michael Brand
  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 tests. Many tests are also a howto
  17. ;; example collection as a user documentation, more or less all those
  18. ;; using `org-test-table-target-expect'. See also the doc string of
  19. ;; `org-test-table-target-expect'.
  20. ;;; Code:
  21. (require 'org-table) ; `org-table-make-reference'
  22. (ert-deftest test-org-table/simple-formula/no-grouping/no-title-row ()
  23. "Simple sum without grouping rows, without title row."
  24. (org-test-table-target-expect
  25. "
  26. | 2 |
  27. | 4 |
  28. | 8 |
  29. | replace |
  30. "
  31. "
  32. | 2 |
  33. | 4 |
  34. | 8 |
  35. | 14 |
  36. "
  37. 1
  38. ;; Calc formula
  39. "#+TBLFM: @>$1 = vsum(@<..@>>)"
  40. ;; Lisp formula
  41. "#+TBLFM: @>$1 = '(+ @<..@>>); N"))
  42. (ert-deftest test-org-table/simple-formula/no-grouping/with-title-row ()
  43. "Simple sum without grouping rows, with title row."
  44. (org-test-table-target-expect
  45. "
  46. | foo |
  47. |---------|
  48. | 2 |
  49. | 4 |
  50. | 8 |
  51. | replace |
  52. "
  53. "
  54. | foo |
  55. |-----|
  56. | 2 |
  57. | 4 |
  58. | 8 |
  59. | 14 |
  60. "
  61. 1
  62. ;; Calc formula
  63. "#+TBLFM: @>$1 = vsum(@I..@>>)"
  64. ;; Lisp formula
  65. "#+TBLFM: @>$1 = '(+ @I..@>>); N"))
  66. (ert-deftest test-org-table/simple-formula/with-grouping/no-title-row ()
  67. "Simple sum with grouping rows, how not to do."
  68. ;; The first example has a problem, see the second example in this
  69. ;; ert-deftest.
  70. (org-test-table-target-expect
  71. "
  72. | 2 |
  73. | 4 |
  74. | 8 |
  75. |---------|
  76. | replace |
  77. "
  78. "
  79. | 2 |
  80. | 4 |
  81. | 8 |
  82. |----|
  83. | 14 |
  84. "
  85. 1
  86. ;; Calc formula
  87. "#+TBLFM: $1 = vsum(@<..@>>)"
  88. ;; Lisp formula
  89. "#+TBLFM: $1 = '(+ @<..@>>); N")
  90. ;; The problem is that the first three rows with the summands are
  91. ;; considered the header and therefore column formulas are not
  92. ;; applied on them as shown below. Also export behaves unexpected.
  93. ;; See next ert-deftest how to group rows right.
  94. (org-test-table-target-expect
  95. "
  96. | 2 | header |
  97. | 4 | header |
  98. | 8 | header |
  99. |---------+---------|
  100. | replace | replace |
  101. "
  102. "
  103. | 2 | header |
  104. | 4 | header |
  105. | 8 | header |
  106. |----+--------|
  107. | 14 | 28 |
  108. "
  109. 2
  110. ;; Calc formula
  111. "#+TBLFM: @>$1 = vsum(@<..@>>) :: $2 = 2 * $1"
  112. ;; Lisp formula
  113. "#+TBLFM: @>$1 = '(+ @<..@>>); N :: $2 = '(* 2 $1); N"))
  114. (ert-deftest test-org-table/simple-formula/with-grouping/with-title-row ()
  115. "Simple sum with grouping rows, how to do it right."
  116. ;; Always add a top row with the column names separated by hline to
  117. ;; get the desired header when you want to group rows.
  118. (org-test-table-target-expect
  119. "
  120. | foo | bar |
  121. |---------+---------|
  122. | 2 | replace |
  123. | 4 | replace |
  124. | 8 | replace |
  125. |---------+---------|
  126. | replace | replace |
  127. "
  128. "
  129. | foo | bar |
  130. |-----+-----|
  131. | 2 | 4 |
  132. | 4 | 8 |
  133. | 8 | 16 |
  134. |-----+-----|
  135. | 14 | 28 |
  136. "
  137. 2
  138. ;; Calc formula
  139. "#+TBLFM: @>$1 = vsum(@I..@>>) :: $2 = 2 * $1"
  140. ;; Lisp formula
  141. "#+TBLFM: @>$1 = '(+ @I..@>>); N :: $2 = '(* 2 $1); N"))
  142. (defconst references/target-normal "
  143. | 0 | 1 | replace | replace | replace | replace | replace | replace |
  144. | z | 1 | replace | replace | replace | replace | replace | replace |
  145. | | 1 | replace | replace | replace | replace | replace | replace |
  146. | | | replace | replace | replace | replace | replace | replace |
  147. "
  148. "Normal numbers and non-numbers for Lisp and Calc formula.")
  149. (defconst references/target-special "
  150. | nan | 1 | replace | replace | replace | replace | replace | replace |
  151. | uinf | 1 | replace | replace | replace | replace | replace | replace |
  152. | -inf | 1 | replace | replace | replace | replace | replace | replace |
  153. | inf | 1 | replace | replace | replace | replace | replace | replace |
  154. "
  155. "Special numbers for Calc formula.")
  156. (ert-deftest test-org-table/references/mode-string-EL ()
  157. "Basic: Assign field reference, sum of field references, sum
  158. and len of simple range reference (no row) and complex range
  159. reference (with row). Mode string EL."
  160. ;; Empty fields are kept during parsing field but lost as list
  161. ;; elements within Lisp formula syntactically when used literally
  162. ;; and not enclosed with " within fields, see last columns with len.
  163. (org-test-table-target-expect
  164. references/target-normal
  165. ;; All the #ERROR show that for Lisp calculations N has to be used.
  166. "
  167. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  168. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  169. | | 1 | | 1 | 1 | 1 | 1 | 1 |
  170. | | | | 0 | 0 | 0 | 0 | 0 |
  171. "
  172. 1 (concat
  173. "#+TBLFM: $3 = '(identity \"$1\"); EL :: $4 = '(+ $1 $2); EL :: "
  174. "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
  175. "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL"))
  176. ;; Empty fields are kept during parsing field _and_ as list elements
  177. ;; within Lisp formula syntactically even when used literally when
  178. ;; enclosed with " within fields, see last columns with len.
  179. (org-test-table-target-expect
  180. "
  181. | \"0\" | \"1\" | repl | repl | repl | repl | repl | repl |
  182. | \"z\" | \"1\" | repl | repl | repl | repl | repl | repl |
  183. | \"\" | \"1\" | repl | repl | repl | repl | repl | repl |
  184. | \"\" | \"\" | repl | repl | repl | repl | repl | repl |
  185. "
  186. "
  187. | \"0\" | \"1\" | \"0\" | 1 | #ERROR | #ERROR | 2 | 2 |
  188. | \"z\" | \"1\" | \"z\" | 1 | #ERROR | #ERROR | 2 | 2 |
  189. | \"\" | \"1\" | \"\" | 1 | #ERROR | #ERROR | 2 | 2 |
  190. | \"\" | \"\" | \"\" | 0 | #ERROR | #ERROR | 2 | 2 |
  191. "
  192. 1 (concat
  193. "#+TBLFM: $3 = '(concat \"\\\"\" $1 \"\\\"\"); EL :: "
  194. "$4 = '(+ (string-to-number $1) (string-to-number $2)); EL :: "
  195. "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
  196. "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL")))
  197. (ert-deftest test-org-table/references/mode-string-E ()
  198. "Basic: Assign field reference, sum of field references, sum
  199. and len of simple range reference (no row) and complex range
  200. reference (with row). Mode string E."
  201. (let ((lisp
  202. (concat
  203. "#+TBLFM: $3 = '(identity $1); E :: $4 = '(+ $1 $2); E :: "
  204. "$5 = '(+ $1..$2); E :: $6 = '(+ @0$1..@0$2); E :: "
  205. "$7 = '(length '($1..$2)); E :: $8 = '(length '(@0$1..@0$2)); E"))
  206. (calc
  207. (concat
  208. "#+TBLFM: $3 = $1; E :: $4 = $1 + $2; E :: "
  209. "$5 = vsum($1..$2); E :: $6 = vsum(@0$1..@0$2); E :: "
  210. "$7 = vlen($1..$2); E :: $8 = vlen(@0$1..@0$2); E")))
  211. (org-test-table-target-expect
  212. references/target-normal
  213. ;; All the #ERROR show that for Lisp calculations N has to be used.
  214. "
  215. | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
  216. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  217. | | 1 | | #ERROR | #ERROR | #ERROR | 2 | 2 |
  218. | | | | #ERROR | #ERROR | #ERROR | 2 | 2 |
  219. "
  220. 1 lisp)
  221. (org-test-table-target-expect
  222. references/target-normal
  223. "
  224. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  225. | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
  226. | | 1 | nan | nan | nan | nan | 2 | 2 |
  227. | | | nan | nan | nan | nan | 2 | 2 |
  228. "
  229. 1 calc)
  230. (org-test-table-target-expect
  231. references/target-special
  232. "
  233. | nan | 1 | nan | nan | nan | nan | 2 | 2 |
  234. | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
  235. | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
  236. | inf | 1 | inf | inf | inf | inf | 2 | 2 |
  237. "
  238. 1 calc)))
  239. (ert-deftest test-org-table/references/mode-string-EN ()
  240. "Basic: Assign field reference, sum of field references, sum
  241. and len of simple range reference (no row) and complex range
  242. reference (with row). Mode string EN."
  243. (let ((lisp (concat
  244. "#+TBLFM: $3 = '(identity $1); EN :: $4 = '(+ $1 $2); EN :: "
  245. "$5 = '(+ $1..$2); EN :: $6 = '(+ @0$1..@0$2); EN :: "
  246. "$7 = '(length '($1..$2)); EN :: "
  247. "$8 = '(length '(@0$1..@0$2)); EN"))
  248. (calc (concat
  249. "#+TBLFM: $3 = $1; EN :: $4 = $1 + $2; EN :: "
  250. "$5 = vsum($1..$2); EN :: $6 = vsum(@0$1..@0$2); EN :: "
  251. "$7 = vlen($1..$2); EN :: $8 = vlen(@0$1..@0$2); EN")))
  252. (org-test-table-target-expect
  253. references/target-normal
  254. "
  255. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  256. | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  257. | | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  258. | | | 0 | 0 | 0 | 0 | 2 | 2 |
  259. "
  260. 1 lisp calc)
  261. (org-test-table-target-expect
  262. references/target-special
  263. "
  264. | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  265. | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  266. | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  267. | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  268. "
  269. 1 calc)))
  270. (ert-deftest test-org-table/references/mode-string-L ()
  271. "Basic: Assign field reference, sum of field references, sum
  272. and len of simple range reference (no row) and complex range
  273. reference (with row). Mode string L."
  274. (org-test-table-target-expect
  275. references/target-normal
  276. ;; All the #ERROR show that for Lisp calculations N has to be used.
  277. "
  278. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  279. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  280. | | 1 | | 1 | 1 | 1 | 1 | 1 |
  281. | | | | 0 | 0 | 0 | 0 | 0 |
  282. "
  283. 1 (concat
  284. "#+TBLFM: $3 = '(identity \"$1\"); L :: $4 = '(+ $1 $2); L :: "
  285. "$5 = '(+ $1..$2); L :: $6 = '(+ @0$1..@0$2); L :: "
  286. "$7 = '(length '($1..$2)); L :: $8 = '(length '(@0$1..@0$2)); L")))
  287. (ert-deftest test-org-table/references/mode-string-none ()
  288. "Basic: Assign field reference, sum of field references, sum
  289. and len of simple range reference (no row) and complex range
  290. reference (with row). No mode string."
  291. (let ((lisp (concat
  292. "#+TBLFM: $3 = '(identity $1) :: $4 = '(+ $1 $2) :: "
  293. "$5 = '(+ $1..$2) :: $6 = '(+ @0$1..@0$2) :: "
  294. "$7 = '(length '($1..$2)) :: $8 = '(length '(@0$1..@0$2))"))
  295. (calc (concat
  296. "#+TBLFM: $3 = $1 :: $4 = $1 + $2 :: "
  297. "$5 = vsum($1..$2) :: $6 = vsum(@0$1..@0$2) :: "
  298. "$7 = vlen($1..$2) :: $8 = vlen(@0$1..@0$2)")))
  299. (org-test-table-target-expect
  300. references/target-normal
  301. ;; All the #ERROR show that for Lisp calculations N has to be used.
  302. "
  303. | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
  304. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  305. | | 1 | | #ERROR | #ERROR | #ERROR | 1 | 1 |
  306. | | | | #ERROR | 0 | 0 | 0 | 0 |
  307. "
  308. 1 lisp)
  309. (org-test-table-target-expect
  310. references/target-normal
  311. "
  312. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  313. | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
  314. | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
  315. | | | 0 | 0 | 0 | 0 | 0 | 0 |
  316. "
  317. 1 calc)
  318. (org-test-table-target-expect
  319. references/target-special
  320. "
  321. | nan | 1 | nan | nan | nan | nan | 2 | 2 |
  322. | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
  323. | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
  324. | inf | 1 | inf | inf | inf | inf | 2 | 2 |
  325. "
  326. 1 calc)))
  327. (ert-deftest test-org-table/references/mode-string-N ()
  328. "Basic: Assign field reference, sum of field references, sum
  329. and len of simple range reference (no row) and complex range
  330. reference (with row). Mode string N."
  331. (let ((lisp
  332. (concat
  333. "#+TBLFM: $3 = '(identity $1); N :: $4 = '(+ $1 $2); N :: "
  334. "$5 = '(+ $1..$2); N :: $6 = '(+ @0$1..@0$2); N :: "
  335. "$7 = '(length '($1..$2)); N :: $8 = '(length '(@0$1..@0$2)); N"))
  336. (calc
  337. (concat
  338. "#+TBLFM: $3 = $1; N :: $4 = $1 + $2; N :: "
  339. "$5 = vsum($1..$2); N :: $6 = vsum(@0$1..@0$2); N :: "
  340. "$7 = vlen($1..$2); N :: $8 = vlen(@0$1..@0$2); N")))
  341. (org-test-table-target-expect
  342. references/target-normal
  343. "
  344. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  345. | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  346. | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
  347. | | | 0 | 0 | 0 | 0 | 0 | 0 |
  348. "
  349. 1 lisp calc)
  350. (org-test-table-target-expect
  351. references/target-special
  352. "
  353. | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  354. | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  355. | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  356. | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  357. "
  358. 1 calc)))
  359. (ert-deftest test-org-table/lisp-return-value ()
  360. "Basic: Return value of Lisp formulas."
  361. (org-test-table-target-expect
  362. "
  363. | | nil | (list) | '() |
  364. |-------------------------+-------------+--------+-----|
  365. | type-of, no L | replace (r) | r | r |
  366. | type-of identity, no L | r | r | r |
  367. | identity, no L | r | r | r |
  368. |-------------------------+-------------+--------+-----|
  369. | type-of \"@1\" | r | r | r |
  370. | type-of (identity \"@1\") | r | r | r |
  371. | identity \"@1\" | r | r | r |
  372. |-------------------------+-------------+--------+-----|
  373. | type-of @1 | r | r | r |
  374. | type-of (identity @1) | r | r | r |
  375. | identity @1 | r | r | r |
  376. "
  377. "
  378. | | nil | (list) | '() |
  379. |-------------------------+--------+--------+--------|
  380. | type-of, no L | string | string | string |
  381. | type-of identity, no L | string | string | string |
  382. | identity, no L | nil | (list) | '() |
  383. |-------------------------+--------+--------+--------|
  384. | type-of \"@1\" | string | string | string |
  385. | type-of (identity \"@1\") | string | string | string |
  386. | identity \"@1\" | nil | (list) | '() |
  387. |-------------------------+--------+--------+--------|
  388. | type-of @1 | symbol | symbol | symbol |
  389. | type-of (identity @1) | symbol | symbol | symbol |
  390. | identity @1 | nil | nil | nil |
  391. "
  392. 1 (concat "#+TBLFM: @2$<<..@2$> = '(type-of @1) :: "
  393. "@3$<<..@3$> = '(type-of (identity @1)) :: "
  394. "@4$<<..@4$> = '(identity @1) :: @5$<<..@>$> = '(@0$1); L")))
  395. (ert-deftest test-org-table/compare ()
  396. "Basic: Compare field references in Calc."
  397. (org-test-table-target-expect
  398. "
  399. | | 0 | z | | nan | uinf | -inf | inf |
  400. |------+------+------+------+------+------+------+------|
  401. | 0 | repl | repl | repl | repl | repl | repl | repl |
  402. | z | repl | repl | repl | repl | repl | repl | repl |
  403. | | repl | repl | repl | repl | repl | repl | repl |
  404. | nan | repl | repl | repl | repl | repl | repl | repl |
  405. | uinf | repl | repl | repl | repl | repl | repl | repl |
  406. | -inf | repl | repl | repl | repl | repl | repl | repl |
  407. | inf | repl | repl | repl | repl | repl | repl | repl |
  408. "
  409. "
  410. | | 0 | z | | nan | uinf | -inf | inf |
  411. |------+---+---+---+-----+------+------+-----|
  412. | 0 | x | | | | | | |
  413. | z | | x | | | | | |
  414. | | | | x | | | | |
  415. | nan | | | | x | | | |
  416. | uinf | | | | | x | | |
  417. | -inf | | | | | | x | |
  418. | inf | | | | | | | x |
  419. "
  420. 1
  421. ;; Compare field reference ($1) with field reference (@1)
  422. "#+TBLFM: @<<$<<..@>$> = if(\"$1\" == \"@1\", x, string(\"\")); E"
  423. ;; Compare field reference ($1) with absolute term
  424. (concat "#+TBLFM: "
  425. "$2 = if(\"$1\" == \"(0)\" , x, string(\"\")); E :: "
  426. "$3 = if(\"$1\" == \"(z)\" , x, string(\"\")); E :: "
  427. "$4 = if(\"$1\" == \"nan\" , x, string(\"\")); E :: "
  428. "$5 = if(\"$1\" == \"(nan)\" , x, string(\"\")); E :: "
  429. "$6 = if(\"$1\" == \"(uinf)\", x, string(\"\")); E :: "
  430. "$7 = if(\"$1\" == \"(-inf)\", x, string(\"\")); E :: "
  431. "$8 = if(\"$1\" == \"(inf)\" , x, string(\"\")); E"))
  432. ;; Check field reference converted from an empty field: Despite this
  433. ;; field reference will not end up in a result, Calc evaluates it.
  434. ;; Make sure that also then there is no Calc error.
  435. (org-test-table-target-expect
  436. "
  437. | 0 | replace |
  438. | z | replace |
  439. | | replace |
  440. | nan | replace |
  441. "
  442. "
  443. | 0 | 1 |
  444. | z | z + 1 |
  445. | | |
  446. | nan | nan |
  447. "
  448. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"))
  449. (ert-deftest test-org-table/empty-field ()
  450. "Examples how to deal with empty fields."
  451. ;; Test if one field is empty, else do a calculation
  452. (org-test-table-target-expect
  453. "
  454. | -1 | replace |
  455. | 0 | replace |
  456. | | replace |
  457. "
  458. "
  459. | -1 | 0 |
  460. | 0 | 1 |
  461. | | |
  462. "
  463. 1
  464. ;; Calc formula
  465. "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"
  466. ;; Lisp formula
  467. "#+TBLFM: $2 = '(if (eq \"$1\" \"\") \"\" (1+ $1)); L")
  468. ;; Test if several fields are empty, else do a calculation
  469. (org-test-table-target-expect
  470. "
  471. | 1 | 2 | replace |
  472. | 4 | | replace |
  473. | | 8 | replace |
  474. | | | replace |
  475. "
  476. "
  477. | 1 | 2 | 3 |
  478. | 4 | | |
  479. | | 8 | |
  480. | | | |
  481. "
  482. 1
  483. ;; Calc formula
  484. (concat "#+TBLFM: $3 = if(\"$1\" == \"nan\" || \"$2\" == \"nan\", "
  485. "string(\"\"), $1 + $2); E")
  486. ;; Lisp formula
  487. (concat "#+TBLFM: $3 = '(if (or (eq \"$1\" \"\") (eq \"$2\" \"\")) "
  488. "\"\" (+ $1 $2)); L"))
  489. ;; $2: Use $1 + 0.5 if $1 available, else only reformat $2 if $2 available
  490. (org-test-table-target-expect
  491. "
  492. | 1.5 | 0 |
  493. | 3.5 | |
  494. | | 5 |
  495. | | |
  496. "
  497. "
  498. | 1.5 | 2.0 |
  499. | 3.5 | 4.0 |
  500. | | 5.0 |
  501. | | |
  502. "
  503. 1
  504. ;; Calc formula
  505. (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  506. "if(\"$2\" == \"nan\", string(\"\"), $2 +.0), $1 + 0.5); E f-1")
  507. ;; Lisp formula not implemented yet
  508. )
  509. ;; Empty fields in simple and complex range reference
  510. (org-test-table-target-expect
  511. "
  512. | | | | | repl | repl | repl | repl | repl | repl |
  513. | | | 5 | 7 | repl | repl | repl | repl | repl | repl |
  514. | 1 | 3 | 5 | 7 | repl | repl | repl | repl | repl | repl |
  515. "
  516. "
  517. | | | | | | | | | 0 | 0 |
  518. | | | 5 | 7 | | | 6 | 6 | 3 | 3 |
  519. | 1 | 3 | 5 | 7 | 4 | 4 | 4 | 4 | 4 | 4 |
  520. "
  521. 1
  522. ;; Calc formula
  523. (concat
  524. "#+TBLFM: "
  525. "$5 = if(typeof(vmean($1..$4)) == 12, "
  526. "string(\"\"), vmean($1..$4)); E :: "
  527. "$6 = if(typeof(vmean(@0$1..@0$4)) == 12, "
  528. "string(\"\"), vmean(@0$1..@0$4)); E :: "
  529. "$7 = if(\"$1..$4\" == \"[]\", string(\"\"), vmean($1..$4)) :: "
  530. "$8 = if(\"@0$1..@0$4\" == \"[]\", string(\"\"), vmean(@0$1..@0$4)) :: "
  531. "$9 = vmean($1..$4); EN :: "
  532. "$10 = vmean(@0$1..@0$4); EN")
  533. ;; Lisp formula
  534. (concat
  535. "#+TBLFM: "
  536. "$5 = '(let ((l '($1..$4))) (if (member \"\" l) \"\" "
  537. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  538. "$6 = '(let ((l '(@0$1..@0$4))) (if (member \"\" l) \"\" "
  539. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  540. "$7 = '(let ((l '($1..$4))) "
  541. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  542. "$8 = '(let ((l '(@0$1..@0$4))) "
  543. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  544. "$9 = '(/ (+ $1..$4) (length '($1..$4))); EN :: "
  545. "$10 = '(/ (+ @0$1..@0$4) (length '(@0$1..@0$4))); EN")
  546. ))
  547. (ert-deftest test-org-table/copy-field ()
  548. "Experiments on how to copy one field into another field.
  549. See also `test-org-table/remote-reference-access'."
  550. (let ((target "
  551. | 0 | replace |
  552. | a b | replace |
  553. | c d | replace |
  554. | | replace |
  555. | 2012-12 | replace |
  556. | [2012-12-31 Mon] | replace |
  557. "))
  558. ;; Lisp formula to copy literally
  559. (org-test-table-target-expect
  560. target
  561. "
  562. | 0 | 0 |
  563. | a b | a b |
  564. | c d | c d |
  565. | | |
  566. | 2012-12 | 2012-12 |
  567. | [2012-12-31 Mon] | [2012-12-31 Mon] |
  568. "
  569. 1 "#+TBLFM: $2 = '(identity $1)")
  570. ;; Calc formula to copy quite literally
  571. (org-test-table-target-expect
  572. target
  573. "
  574. | 0 | 0 |
  575. | a b | a b |
  576. | c d | c d |
  577. | | |
  578. | 2012-12 | 2012-12 |
  579. | [2012-12-31 Mon] | [2012-12-31 Mon] |
  580. "
  581. 1 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  582. "string(\"\"), string(subvec(\"$1\", 2, vlen(\"$1\")))); E"))
  583. ;; Calc formula simple
  584. (org-test-table-target-expect
  585. target
  586. "
  587. | 0 | 0 |
  588. | a b | a b |
  589. | c d | c d |
  590. | | |
  591. | 2012-12 | 2000 |
  592. | [2012-12-31 Mon] | [2012-12-31 Mon] |
  593. "
  594. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1); E")))
  595. (ert-deftest test-org-table/sub-total ()
  596. "Grouped rows with sub-total.
  597. Begin range with \"@II\" to handle multiline header. Convert
  598. integer to float with \"+.0\" for sub-total of items c1 and c2.
  599. Sum empty fields as value zero but without ignoring them for
  600. \"vlen\" with format specifier \"EN\". Format possibly empty
  601. results with the Calc formatter \"f-1\" instead of the printf
  602. formatter \"%.1f\"."
  603. (org-test-table-target-expect
  604. "
  605. |-------+---------+---------|
  606. | Item | Item | Sub- |
  607. | name | value | total |
  608. |-------+---------+---------|
  609. | a1 | 4.1 | replace |
  610. | a2 | 8.2 | replace |
  611. | a3 | | replace |
  612. |-------+---------+---------|
  613. | b1 | 16.0 | replace |
  614. |-------+---------+---------|
  615. | c1 | 32 | replace |
  616. | c2 | 64 | replace |
  617. |-------+---------+---------|
  618. | Total | replace | replace |
  619. |-------+---------+---------|
  620. "
  621. "
  622. |-------+-------+-------|
  623. | Item | Item | Sub- |
  624. | name | value | total |
  625. |-------+-------+-------|
  626. | a1 | 4.1 | |
  627. | a2 | 8.2 | |
  628. | a3 | | 12.3 |
  629. |-------+-------+-------|
  630. | b1 | 16.0 | 16.0 |
  631. |-------+-------+-------|
  632. | c1 | 32 | |
  633. | c2 | 64 | 96.0 |
  634. |-------+-------+-------|
  635. | Total | 124.3 | |
  636. |-------+-------+-------|
  637. "
  638. 1 (concat "#+TBLFM: @>$2 = vsum(@II..@>>) ::"
  639. "$3 = if(vlen(@0..@+I) == 1, "
  640. "vsum(@-I$2..@+I$2) +.0, string(\"\")); EN f-1 :: "
  641. "@>$3 = string(\"\")")))
  642. (ert-deftest test-org-table/org-lookup-all ()
  643. "Use `org-lookup-all' for several GROUP BY as in SQL and for ranking.
  644. See also URL `https://orgmode.org/worg/org-tutorials/org-lookups.html'."
  645. (let ((data "
  646. #+NAME: data
  647. | Purchase | Product | Shop | Rating |
  648. |----------+---------+------+--------|
  649. | a | p1 | s1 | 1 |
  650. | b | p1 | s2 | 4 |
  651. | c | p2 | s1 | 2 |
  652. | d | p3 | s2 | 8 |
  653. "))
  654. ;; Product rating and ranking by average purchase from "#+NAME: data"
  655. (org-test-table-target-expect
  656. (concat data "
  657. | Product | Rating | Ranking |
  658. |---------+---------+---------|
  659. | p1 | replace | replace |
  660. | p2 | replace | replace |
  661. | p3 | replace | replace |
  662. ")
  663. (concat data "
  664. | Product | Rating | Ranking |
  665. |---------+--------+---------|
  666. | p1 | 2.5 | 2 |
  667. | p2 | 2.0 | 3 |
  668. | p3 | 8.0 | 1 |
  669. ")
  670. 2 (concat
  671. "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
  672. "'(remote(data, @I$2..@>$2)) '(remote(data, @I$4..@>$4))))) "
  673. "(/ (apply '+ all) (length all) 1.0)); L :: "
  674. "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))
  675. ;; Shop rating and ranking by average purchase from "#+NAME: data"
  676. (org-test-table-target-expect
  677. (concat data "
  678. | Shop | Rating | Ranking |
  679. |------+---------+---------|
  680. | s1 | replace | replace |
  681. | s2 | replace | replace |
  682. ")
  683. (concat data "
  684. | Shop | Rating | Ranking |
  685. |------+--------+---------|
  686. | s1 | 1.5 | 2 |
  687. | s2 | 6.0 | 1 |
  688. ")
  689. 2 (concat
  690. "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
  691. "'(remote(data, @I$3..@>$3)) '(remote(data, @I$4..@>$4))))) "
  692. "(/ (apply '+ all) (length all) 1.0)); L :: "
  693. "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))))
  694. (ert-deftest test-org-table/org-table-make-reference/mode-string-EL ()
  695. ;; For Lisp formula only
  696. (should (equal "0" (org-table-make-reference "0" t nil 'literal)))
  697. (should (equal "z" (org-table-make-reference "z" t nil 'literal)))
  698. (should (equal "" (org-table-make-reference "" t nil 'literal)))
  699. (should (equal "0 1" (org-table-make-reference '("0" "1") t nil 'literal)))
  700. (should (equal "z 1" (org-table-make-reference '("z" "1") t nil 'literal)))
  701. (should (equal " 1" (org-table-make-reference '("" "1") t nil 'literal)))
  702. (should (equal " " (org-table-make-reference '("" "") t nil 'literal))))
  703. (ert-deftest test-org-table/org-table-make-reference/mode-string-E ()
  704. ;; For Lisp formula
  705. (should (equal "\"0\"" (org-table-make-reference "0" t nil t)))
  706. (should (equal "\"z\"" (org-table-make-reference "z" t nil t)))
  707. (should (equal"\"\"" (org-table-make-reference "" t nil t)))
  708. (should (equal "\"0\" \"1\"" (org-table-make-reference '("0""1") t nil t)))
  709. (should (equal "\"z\" \"1\"" (org-table-make-reference '("z""1") t nil t)))
  710. (should (equal"\"\" \"1\"" (org-table-make-reference '("""1") t nil t)))
  711. (should (equal"\"\" \"\""(org-table-make-reference '("""" ) t nil t)))
  712. ;; For Calc formula
  713. (should (equal "(0)" (org-table-make-reference "0" t nil nil)))
  714. (should (equal "(z)" (org-table-make-reference "z" t nil nil)))
  715. (should (equal "nan" (org-table-make-reference "" t nil nil)))
  716. (should (equal "[0,1]" (org-table-make-reference '("0" "1") t nil nil)))
  717. (should (equal "[z,1]" (org-table-make-reference '("z" "1") t nil nil)))
  718. (should (equal "[nan,1]" (org-table-make-reference '("" "1") t nil nil)))
  719. (should (equal "[nan,nan]" (org-table-make-reference '("" "") t nil nil)))
  720. ;; For Calc formula, special numbers
  721. (should (equal "(nan)" (org-table-make-reference "nan" t nil nil)))
  722. (should (equal "(uinf)" (org-table-make-reference "uinf" t nil nil)))
  723. (should (equal "(-inf)" (org-table-make-reference "-inf" t nil nil)))
  724. (should (equal "(inf)" (org-table-make-reference "inf" t nil nil)))
  725. (should (equal "[nan,1]" (org-table-make-reference '("nan" "1") t nil nil)))
  726. (should (equal "[uinf,1]" (org-table-make-reference '("uinf" "1") t nil nil)))
  727. (should (equal "[-inf,1]" (org-table-make-reference '("-inf" "1") t nil nil)))
  728. (should (equal "[inf,1]" (org-table-make-reference '("inf" "1") t nil nil))))
  729. (ert-deftest test-org-table/org-table-make-reference/mode-string-EN ()
  730. ;; For Lisp formula
  731. (should (equal "0" (org-table-make-reference "0" t t t)))
  732. (should (equal "0" (org-table-make-reference "z" t t t)))
  733. (should (equal "0" (org-table-make-reference "" t t t)))
  734. (should (equal "0 1" (org-table-make-reference '("0" "1") t t t)))
  735. (should (equal "0 1" (org-table-make-reference '("z" "1") t t t)))
  736. (should (equal "0 1" (org-table-make-reference '("" "1") t t t)))
  737. (should (equal "0 0" (org-table-make-reference '("" "" ) t t t)))
  738. ;; For Calc formula
  739. (should (equal "(0)" (org-table-make-reference "0" t t nil)))
  740. (should (equal "(0)" (org-table-make-reference "z" t t nil)))
  741. (should (equal "(0)" (org-table-make-reference "" t t nil)))
  742. (should (equal "[0,1]" (org-table-make-reference '("0" "1") t t nil)))
  743. (should (equal "[0,1]" (org-table-make-reference '("z" "1") t t nil)))
  744. (should (equal "[0,1]" (org-table-make-reference '("" "1") t t nil)))
  745. (should (equal "[0,0]" (org-table-make-reference '("" "" ) t t nil)))
  746. ;; For Calc formula, special numbers
  747. (should (equal "(0)" (org-table-make-reference "nan" t t nil)))
  748. (should (equal "(0)" (org-table-make-reference "uinf" t t nil)))
  749. (should (equal "(0)" (org-table-make-reference "-inf" t t nil)))
  750. (should (equal "(0)" (org-table-make-reference "inf" t t nil)))
  751. (should (equal "[0,1]" (org-table-make-reference '( "nan" "1") t t nil)))
  752. (should (equal "[0,1]" (org-table-make-reference '("uinf" "1") t t nil)))
  753. (should (equal "[0,1]" (org-table-make-reference '("-inf" "1") t t nil)))
  754. (should (equal "[0,1]" (org-table-make-reference '( "inf" "1") t t nil))))
  755. (ert-deftest test-org-table/org-table-make-reference/mode-string-L ()
  756. ;; For Lisp formula only
  757. (should (equal "0" (org-table-make-reference "0" nil nil 'literal)))
  758. (should (equal "z" (org-table-make-reference "z" nil nil 'literal)))
  759. (should (equal "" (org-table-make-reference "" nil nil 'literal)))
  760. (should (equal "0 1" (org-table-make-reference '("0" "1") nil nil 'literal)))
  761. (should (equal "z 1" (org-table-make-reference '("z" "1") nil nil 'literal)))
  762. (should (equal "1" (org-table-make-reference '("" "1") nil nil 'literal)))
  763. (should (equal "" (org-table-make-reference '("" "" ) nil nil 'literal))))
  764. (ert-deftest test-org-table/org-table-make-reference/mode-string-none ()
  765. ;; For Lisp formula
  766. (should (equal "\"0\"" (org-table-make-reference "0" nil nil t)))
  767. (should (equal "\"z\"" (org-table-make-reference "z" nil nil t)))
  768. (should (equal "" (org-table-make-reference "" nil nil t)))
  769. (should (equal "\"0\" \"1\"" (org-table-make-reference '("0" "1") nil nil t)))
  770. (should (equal "\"z\" \"1\"" (org-table-make-reference '("z" "1") nil nil t)))
  771. (should (equal "\"1\"" (org-table-make-reference '("" "1") nil nil t)))
  772. (should (equal "" (org-table-make-reference '("" "" ) nil nil t)))
  773. ;; For Calc formula
  774. (should (equal "(0)" (org-table-make-reference "0" nil nil nil)))
  775. (should (equal "(z)" (org-table-make-reference "z" nil nil nil)))
  776. (should (equal "(0)" (org-table-make-reference "" nil nil nil)))
  777. (should (equal "[0,1]" (org-table-make-reference '("0" "1") nil nil nil)))
  778. (should (equal "[z,1]" (org-table-make-reference '("z" "1") nil nil nil)))
  779. (should (equal "[1]" (org-table-make-reference '("" "1") nil nil nil)))
  780. (should (equal "[]" (org-table-make-reference '("" "" ) nil nil nil)))
  781. ;; For Calc formula, special numbers
  782. (should (equal "(nan)" (org-table-make-reference "nan" nil nil nil)))
  783. (should (equal "(uinf)" (org-table-make-reference "uinf" nil nil nil)))
  784. (should (equal "(-inf)" (org-table-make-reference "-inf" nil nil nil)))
  785. (should (equal "(inf)" (org-table-make-reference "inf" nil nil nil)))
  786. (should (equal "[nan,1]" (org-table-make-reference '( "nan" "1") nil nil nil)))
  787. (should (equal "[uinf,1]" (org-table-make-reference '("uinf" "1") nil nil nil)))
  788. (should (equal "[-inf,1]" (org-table-make-reference '("-inf" "1") nil nil nil)))
  789. (should (equal "[inf,1]" (org-table-make-reference '( "inf" "1") nil nil nil))))
  790. (ert-deftest test-org-table/org-table-make-reference/mode-string-N ()
  791. ;; For Lisp formula
  792. (should (equal "0" (org-table-make-reference "0" nil t t)))
  793. (should (equal "0" (org-table-make-reference "z" nil t t)))
  794. (should (equal "" (org-table-make-reference "" nil t t)))
  795. (should (equal "0 1" (org-table-make-reference '("0" "1") nil t t)))
  796. (should (equal "0 1" (org-table-make-reference '("z" "1") nil t t)))
  797. (should (equal "1" (org-table-make-reference '("" "1") nil t t)))
  798. (should (equal "" (org-table-make-reference '("" "" ) nil t t)))
  799. ;; For Calc formula
  800. (should (equal "(0)" (org-table-make-reference "0" nil t nil)))
  801. (should (equal "(0)" (org-table-make-reference "z" nil t nil)))
  802. (should (equal "(0)" (org-table-make-reference "" nil t nil)))
  803. (should (equal "[0,1]" (org-table-make-reference '("0" "1") nil t nil)))
  804. (should (equal "[0,1]" (org-table-make-reference '("z" "1") nil t nil)))
  805. (should (equal "[1]" (org-table-make-reference '("" "1") nil t nil)))
  806. (should (equal "[]" (org-table-make-reference '("" "" ) nil t nil)))
  807. ;; For Calc formula, special numbers
  808. (should (equal "(0)" (org-table-make-reference "nan" nil t nil)))
  809. (should (equal "(0)" (org-table-make-reference "uinf" nil t nil)))
  810. (should (equal "(0)" (org-table-make-reference "-inf" nil t nil)))
  811. (should (equal "(0)" (org-table-make-reference "inf" nil t nil)))
  812. (should (equal "[0,1]" (org-table-make-reference '( "nan" "1") nil t nil)))
  813. (should (equal "[0,1]" (org-table-make-reference '("uinf" "1") nil t nil)))
  814. (should (equal "[0,1]" (org-table-make-reference '("-inf" "1") nil t nil)))
  815. (should (equal "[0,1]" (org-table-make-reference '( "inf" "1") nil t nil))))
  816. (ert-deftest test-org-table/org-table-convert-refs-to-an/1 ()
  817. "Simple reference @2$1."
  818. (should
  819. (string= "A2" (org-table-convert-refs-to-an "@2$1"))))
  820. ;; TODO: Test broken
  821. ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
  822. ;; "Self reference @1$1."
  823. ;; (should
  824. ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
  825. (ert-deftest test-org-table/org-table-convert-refs-to-an/3 ()
  826. "Remote reference."
  827. (should
  828. (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
  829. (ert-deftest test-org-table/org-table-convert-refs-to-rc/1 ()
  830. "Simple reference @2$1."
  831. (should
  832. (string= "@2$1" (org-table-convert-refs-to-rc "A2"))))
  833. (ert-deftest test-org-table/org-table-convert-refs-to-rc/2 ()
  834. "Self reference $0."
  835. (should
  836. (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
  837. ;; TODO: Test Broken
  838. ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
  839. ;; "Remote reference."
  840. ;; (should
  841. ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
  842. (ert-deftest test-org-table/remote-reference-access ()
  843. "Access to remote reference.
  844. See also `test-org-table/copy-field'."
  845. (org-test-table-target-expect
  846. "
  847. #+NAME: table
  848. | | x 42 | |
  849. | replace | replace |
  850. "
  851. "
  852. #+NAME: table
  853. | | x 42 | |
  854. | x 42 | 84 x |
  855. "
  856. 1 (concat "#+TBLFM: "
  857. ;; Copy text without calculation: Use Lisp formula
  858. "$1 = '(identity remote(table, @1$2)) :: "
  859. ;; Do a calculation: Use Calc (or Lisp ) formula
  860. "$2 = 2 * remote(table, @1$2)")))
  861. (ert-deftest test-org-table/remote-reference-indirect ()
  862. "Access to remote reference with indirection of name or ID."
  863. (let ((source-tables "
  864. #+NAME: 2012
  865. | amount |
  866. |--------|
  867. | 1 |
  868. | 2 |
  869. |--------|
  870. | 3 |
  871. #+TBLFM: @>$1 = vsum(@I..@II)
  872. #+NAME: 2013
  873. | amount |
  874. |--------|
  875. | 4 |
  876. | 8 |
  877. |--------|
  878. | 12 |
  879. #+TBLFM: @>$1 = vsum(@I..@II)
  880. "))
  881. ;; Read several remote references from same column
  882. (org-test-table-target-expect
  883. (concat source-tables "
  884. #+NAME: summary
  885. | year | amount |
  886. |-------+---------|
  887. | 2012 | replace |
  888. | 2013 | replace |
  889. |-------+---------|
  890. | total | replace |
  891. ")
  892. (concat source-tables "
  893. #+NAME: summary
  894. | year | amount |
  895. |-------+--------|
  896. | 2012 | 3 |
  897. | 2013 | 12 |
  898. |-------+--------|
  899. | total | 15 |
  900. ")
  901. 1
  902. ;; Calc formula
  903. "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
  904. ;; Lisp formula
  905. (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
  906. "@>$2 = '(+ @I..@II); N"))
  907. ;; Read several remote references from same row
  908. (org-test-table-target-expect
  909. (concat source-tables "
  910. #+NAME: summary
  911. | year | 2012 | 2013 | total |
  912. |--------+---------+---------+---------|
  913. | amount | replace | replace | replace |
  914. ")
  915. (concat source-tables "
  916. #+NAME: summary
  917. | year | 2012 | 2013 | total |
  918. |--------+------+------+-------|
  919. | amount | 3 | 12 | 15 |
  920. ")
  921. 1
  922. ;; Calc formula
  923. "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
  924. ;; Lisp formula
  925. (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
  926. "@2$> = '(+ $<<..$>>); N"))))
  927. (ert-deftest test-org-table/org-at-TBLFM-p ()
  928. (org-test-with-temp-text-in-file
  929. "
  930. | 1 |
  931. | 2 |
  932. #+TBLFM: $2=$1*2
  933. "
  934. (goto-char (point-min))
  935. (forward-line 2)
  936. (should (equal (org-at-TBLFM-p) nil))
  937. (goto-char (point-min))
  938. (forward-line 3)
  939. (should (equal (org-at-TBLFM-p) t))
  940. (goto-char (point-min))
  941. (forward-line 4)
  942. (should (equal (org-at-TBLFM-p) nil))))
  943. (ert-deftest test-org-table/org-table-TBLFM-begin ()
  944. (org-test-with-temp-text-in-file
  945. "
  946. | 1 |
  947. | 2 |
  948. #+TBLFM: $2=$1*2
  949. "
  950. (goto-char (point-min))
  951. (should (equal (org-table-TBLFM-begin)
  952. nil))
  953. (goto-char (point-min))
  954. (forward-line 1)
  955. (should (equal (org-table-TBLFM-begin)
  956. nil))
  957. (goto-char (point-min))
  958. (forward-line 3)
  959. (should (= (org-table-TBLFM-begin)
  960. 14))
  961. (goto-char (point-min))
  962. (forward-line 4)
  963. (should (= (org-table-TBLFM-begin)
  964. 14))
  965. ))
  966. (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
  967. "For multiple #+TBLFM lines."
  968. (org-test-with-temp-text-in-file
  969. "
  970. | 1 |
  971. | 2 |
  972. #+TBLFM: $2=$1*1
  973. #+TBLFM: $2=$1*2
  974. "
  975. (goto-char (point-min))
  976. (should (equal (org-table-TBLFM-begin)
  977. nil))
  978. (goto-char (point-min))
  979. (forward-line 1)
  980. (should (equal (org-table-TBLFM-begin)
  981. nil))
  982. (goto-char (point-min))
  983. (forward-line 3)
  984. (should (= (org-table-TBLFM-begin)
  985. 14))
  986. (goto-char (point-min))
  987. (forward-line 4)
  988. (should (= (org-table-TBLFM-begin)
  989. 14))
  990. (goto-char (point-min))
  991. (forward-line 5)
  992. (should (= (org-table-TBLFM-begin)
  993. 14))
  994. ))
  995. (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
  996. (org-test-with-temp-text-in-file
  997. "
  998. | 1 |
  999. | 2 |
  1000. #+TBLFM: $2=$1*1
  1001. #+TBLFM: $2=$1*2
  1002. | 6 |
  1003. | 7 |
  1004. #+TBLFM: $2=$1*1
  1005. #+TBLFM: $2=$1*2
  1006. "
  1007. (goto-char (point-min))
  1008. (should (equal (org-table-TBLFM-begin)
  1009. nil))
  1010. (goto-char (point-min))
  1011. (forward-line 1)
  1012. (should (equal (org-table-TBLFM-begin)
  1013. nil))
  1014. (goto-char (point-min))
  1015. (forward-line 3)
  1016. (should (= (org-table-TBLFM-begin)
  1017. 14))
  1018. (goto-char (point-min))
  1019. (forward-line 4)
  1020. (should (= (org-table-TBLFM-begin)
  1021. 14))
  1022. (goto-char (point-min))
  1023. (forward-line 5)
  1024. (should (= (org-table-TBLFM-begin)
  1025. 14))
  1026. (goto-char (point-min))
  1027. (forward-line 6)
  1028. (should (= (org-table-TBLFM-begin)
  1029. 14))
  1030. (goto-char (point-min))
  1031. (forward-line 8)
  1032. (should (= (org-table-TBLFM-begin)
  1033. 61))
  1034. (goto-char (point-min))
  1035. (forward-line 9)
  1036. (should (= (org-table-TBLFM-begin)
  1037. 61))
  1038. (goto-char (point-min))
  1039. (forward-line 10)
  1040. (should (= (org-table-TBLFM-begin)
  1041. 61))))
  1042. (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
  1043. (org-test-with-temp-text-in-file
  1044. "
  1045. | 1 | |
  1046. | 2 | |
  1047. #+TBLFM: $2=$1*1
  1048. #+TBLFM: $2=$1*2
  1049. #+TBLFM: $2=$1*3
  1050. "
  1051. (let ((got (progn (goto-char (point-min))
  1052. (forward-line 3)
  1053. (org-table-calc-current-TBLFM)
  1054. (buffer-string)))
  1055. (expect "
  1056. | 1 | 1 |
  1057. | 2 | 2 |
  1058. #+TBLFM: $2=$1*1
  1059. #+TBLFM: $2=$1*2
  1060. #+TBLFM: $2=$1*3
  1061. "))
  1062. (should (string= got
  1063. expect)))
  1064. (let ((got (progn (goto-char (point-min))
  1065. (forward-line 4)
  1066. (org-table-calc-current-TBLFM)
  1067. (buffer-string)))
  1068. (expect "
  1069. | 1 | 2 |
  1070. | 2 | 4 |
  1071. #+TBLFM: $2=$1*1
  1072. #+TBLFM: $2=$1*2
  1073. #+TBLFM: $2=$1*3
  1074. "))
  1075. (should (string= got
  1076. expect)))))
  1077. (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
  1078. "org-table-calc-current-TBLFM should preserve the input as it was."
  1079. (org-test-with-temp-text-in-file
  1080. "
  1081. | 1 | 1 |
  1082. | 2 | 2 |
  1083. #+TBLFM: $2=$1*1
  1084. #+TBLFM: $2=$1*2::$2=$1*2
  1085. #+TBLFM: $2=$1*3
  1086. "
  1087. (let ((expect "
  1088. | 1 | 1 |
  1089. | 2 | 2 |
  1090. #+TBLFM: $2=$1*1
  1091. #+TBLFM: $2=$1*2::$2=$1*2
  1092. #+TBLFM: $2=$1*3
  1093. "))
  1094. (goto-char (point-min))
  1095. (forward-line 4)
  1096. (should-error (org-table-calc-current-TBLFM))
  1097. (setq got (buffer-string))
  1098. (message "%s" got)
  1099. (should (string= got
  1100. expect)))))
  1101. ;;; Radio Tables
  1102. (ert-deftest test-org-table/to-generic ()
  1103. "Test `orgtbl-to-generic' specifications."
  1104. ;; Test :hline parameter.
  1105. (should
  1106. (equal "a\nb"
  1107. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1108. '(:hline nil))))
  1109. (should
  1110. (equal "a\n~\nb"
  1111. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1112. '(:hline "~"))))
  1113. ;; Test :sep parameter.
  1114. (should
  1115. (equal "a!b\nc!d"
  1116. (orgtbl-to-generic
  1117. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1118. '(:sep "!"))))
  1119. ;; Test :hsep parameter.
  1120. (should
  1121. (equal "a!b\nc?d"
  1122. (orgtbl-to-generic
  1123. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1124. '(:sep "?" :hsep "!"))))
  1125. ;; Test :tstart parameter.
  1126. (should
  1127. (equal "<begin>\na"
  1128. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tstart "<begin>"))))
  1129. (should
  1130. (equal "<begin>\na"
  1131. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1132. '(:tstart (lambda () "<begin>")))))
  1133. (should
  1134. (equal "a"
  1135. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1136. '(:tstart "<begin>" :splice t))))
  1137. ;; Test :tend parameter.
  1138. (should
  1139. (equal "a\n<end>"
  1140. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tend "<end>"))))
  1141. (should
  1142. (equal "a\n<end>"
  1143. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1144. '(:tend (lambda () "<end>")))))
  1145. (should
  1146. (equal "a"
  1147. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1148. '(:tend "<end>" :splice t))))
  1149. ;; Test :lstart parameter.
  1150. (should
  1151. (equal "> a"
  1152. (orgtbl-to-generic
  1153. (org-table-to-lisp "| a |") '(:lstart "> "))))
  1154. (should
  1155. (equal "> a"
  1156. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1157. '(:lstart (lambda () "> ")))))
  1158. ;; Test :llstart parameter.
  1159. (should
  1160. (equal "> a\n>> b"
  1161. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1162. '(:lstart "> " :llstart ">> "))))
  1163. ;; Test :hlstart parameter.
  1164. (should
  1165. (equal "!> a\n> b"
  1166. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1167. '(:lstart "> " :hlstart "!> "))))
  1168. ;; Test :hllstart parameter.
  1169. (should
  1170. (equal "!> a\n!!> b\n> c"
  1171. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1172. '(:lstart "> " :hlstart "!> " :hllstart "!!> "))))
  1173. ;; Test :lend parameter.
  1174. (should
  1175. (equal "a <"
  1176. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:lend " <"))))
  1177. ;; Test :llend parameter.
  1178. (should
  1179. (equal "a <\nb <<"
  1180. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1181. '(:lend " <" :llend " <<"))))
  1182. ;; Test :hlend parameter.
  1183. (should
  1184. (equal "a <!\nb <"
  1185. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1186. '(:lend " <" :hlend " <!"))))
  1187. ;; Test :hllend parameter.
  1188. (should
  1189. (equal "a <!\nb <!!\nc <"
  1190. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1191. '(:lend " <" :hlend " <!" :hllend " <!!"))))
  1192. ;; Test :lfmt parameter.
  1193. (should
  1194. (equal "a!b"
  1195. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1196. '(:lfmt "%s!%s"))))
  1197. (should
  1198. (equal "a+b"
  1199. (orgtbl-to-generic
  1200. (org-table-to-lisp "| a | b |")
  1201. '(:lfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1202. (should
  1203. (equal "a!b"
  1204. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1205. '(:lfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1206. ;; Test :llfmt parameter.
  1207. (should
  1208. (equal "a!b"
  1209. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1210. '(:llfmt "%s!%s"))))
  1211. (should
  1212. (equal "a!b\nc+d"
  1213. (orgtbl-to-generic
  1214. (org-table-to-lisp "| a | b |\n| c | d |")
  1215. '(:lfmt "%s!%s" :llfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1216. (should
  1217. (equal "a!b"
  1218. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1219. '(:llfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1220. ;; Test :hlfmt parameter.
  1221. (should
  1222. (equal "a!b\ncd"
  1223. (orgtbl-to-generic
  1224. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1225. '(:hlfmt "%s!%s"))))
  1226. (should
  1227. (equal "a+b\ncd"
  1228. (orgtbl-to-generic
  1229. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1230. '(:hlfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1231. (should
  1232. (equal "a!b\n>c d<"
  1233. (orgtbl-to-generic
  1234. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1235. '(:hlfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1236. ;; Test :hllfmt parameter.
  1237. (should
  1238. (equal "a!b\ncd"
  1239. (orgtbl-to-generic
  1240. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1241. '(:hllfmt "%s!%s"))))
  1242. (should
  1243. (equal "a+b\ncd"
  1244. (orgtbl-to-generic
  1245. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1246. '(:hllfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1247. (should
  1248. (equal "a!b\n>c d<"
  1249. (orgtbl-to-generic
  1250. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1251. '(:hllfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1252. ;; Test :fmt parameter.
  1253. (should
  1254. (equal ">a<\n>b<"
  1255. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1256. '(:fmt ">%s<"))))
  1257. (should
  1258. (equal ">a<b"
  1259. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1260. '(:fmt (1 ">%s<" 2 (lambda (c) c))))))
  1261. (should
  1262. (equal "a b"
  1263. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1264. '(:fmt (2 " %s")))))
  1265. (should
  1266. (equal ">a<"
  1267. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1268. '(:fmt (lambda (c) (format ">%s<" c))))))
  1269. ;; Test :hfmt parameter.
  1270. (should
  1271. (equal ">a<\nb"
  1272. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1273. '(:hfmt ">%s<"))))
  1274. (should
  1275. (equal ">a<b\ncd"
  1276. (orgtbl-to-generic
  1277. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1278. '(:hfmt (1 ">%s<" 2 identity)))))
  1279. (should
  1280. (equal "a b\ncd"
  1281. (orgtbl-to-generic
  1282. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1283. '(:hfmt (2 " %s")))))
  1284. (should
  1285. (equal ">a<\nb"
  1286. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1287. '(:hfmt (lambda (c) (format ">%s<" c))))))
  1288. ;; Test :efmt parameter.
  1289. (should
  1290. (equal "2x10^3"
  1291. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1292. '(:efmt "%sx10^%s"))))
  1293. (should
  1294. (equal "2x10^3"
  1295. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1296. '(:efmt (lambda (m e) (concat m "x10^" e))))))
  1297. (should
  1298. (equal "2x10^3"
  1299. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1300. '(:efmt (1 "%sx10^%s")))))
  1301. (should
  1302. (equal "2x10^3"
  1303. (orgtbl-to-generic
  1304. (org-table-to-lisp "| 2e3 |")
  1305. '(:efmt (1 (lambda (m e) (format "%sx10^%s" m e)))))))
  1306. (should
  1307. (equal "2e3"
  1308. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |") '(:efmt nil))))
  1309. ;; Test :skip parameter.
  1310. (should
  1311. (equal "cd"
  1312. (orgtbl-to-generic
  1313. (org-table-to-lisp "| \ | <c> |\n| a | b |\n|---+---|\n| c | d |")
  1314. '(:skip 2))))
  1315. ;; Test :skipcols parameter.
  1316. (should
  1317. (equal "a\nc"
  1318. (orgtbl-to-generic
  1319. (org-table-to-lisp "| a | b |\n| c | d |") '(:skipcols (2)))))
  1320. (should
  1321. (equal "a\nc"
  1322. (orgtbl-to-generic
  1323. (org-table-to-lisp
  1324. "| / | <c> | <c> |\n| # | a | b |\n|---+---+---|\n| | c | d |")
  1325. '(:skipcols (2)))))
  1326. ;; Test :raw parameter.
  1327. (when (featurep 'ox-latex)
  1328. (should
  1329. (string-match-p
  1330. "/a/"
  1331. (orgtbl-to-generic (org-table-to-lisp "| /a/ | b |")
  1332. '(:backend latex :raw t)))))
  1333. ;; Hooks are ignored.
  1334. (should
  1335. (equal
  1336. "a\nb"
  1337. (let* ((fun-list (list (lambda (backend) (search-forward "a") (insert "hook"))))
  1338. (org-export-before-parsing-hook fun-list)
  1339. (org-export-before-processing-hook fun-list))
  1340. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1341. '(:hline nil)))))
  1342. ;; User-defined export filters are ignored.
  1343. (should
  1344. (equal
  1345. "a\nb"
  1346. (let ((org-export-filter-table-cell-functions (list (lambda (c b i) "filter"))))
  1347. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1348. '(:hline nil)))))
  1349. ;; Macros, even if unknown, are returned as-is.
  1350. (should
  1351. (equal "{{{macro}}}"
  1352. (orgtbl-to-generic (org-table-to-lisp "| {{{macro}}} |") nil))))
  1353. (ert-deftest test-org-table/to-latex ()
  1354. "Test `orgtbl-to-latex' specifications."
  1355. (should
  1356. (equal "\\begin{tabular}{l}\na\\\\\n\\end{tabular}"
  1357. (orgtbl-to-latex (org-table-to-lisp "| a |") nil)))
  1358. ;; Test :environment parameter.
  1359. (should
  1360. (equal "\\begin{tabularx}{l}\na\\\\\n\\end{tabularx}"
  1361. (orgtbl-to-latex (org-table-to-lisp "| a |")
  1362. '(:environment "tabularx"))))
  1363. ;; Test :booktabs parameter.
  1364. (should
  1365. (string-match-p
  1366. "\\toprule" (orgtbl-to-latex (org-table-to-lisp "| a |") '(:booktabs t))))
  1367. ;; Handle LaTeX snippets.
  1368. (should
  1369. (equal "\\begin{tabular}{l}\n\\(x\\)\\\\\n\\end{tabular}"
  1370. (orgtbl-to-latex (org-table-to-lisp "| $x$ |") nil)))
  1371. ;; Test pseudo objects and :raw parameter.
  1372. (should
  1373. (string-match-p
  1374. "\\$x\\$" (orgtbl-to-latex (org-table-to-lisp "| $x$ |") '(:raw t)))))
  1375. (ert-deftest test-org-table/to-html ()
  1376. "Test `orgtbl-to-html' specifications."
  1377. (should
  1378. (equal (orgtbl-to-html (org-table-to-lisp "| a |") nil)
  1379. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">
  1380. <colgroup>
  1381. <col class=\"org-left\" />
  1382. </colgroup>
  1383. <tbody>
  1384. <tr>
  1385. <td class=\"org-left\">a</td>
  1386. </tr>
  1387. </tbody>
  1388. </table>"))
  1389. ;; Test :attributes parameter.
  1390. (should
  1391. (string-match-p
  1392. "<table>"
  1393. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes nil))))
  1394. (should
  1395. (string-match-p
  1396. "<table border=\"2\">"
  1397. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes (:border "2"))))))
  1398. (ert-deftest test-org-table/to-texinfo ()
  1399. "Test `orgtbl-to-texinfo' specifications."
  1400. (should
  1401. (equal "@multitable {a}\n@item a\n@end multitable"
  1402. (orgtbl-to-texinfo (org-table-to-lisp "| a |") nil)))
  1403. ;; Test :columns parameter.
  1404. (should
  1405. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1406. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1407. '(:columns ".4 .6"))))
  1408. (should
  1409. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1410. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1411. '(:columns "@columnfractions .4 .6"))))
  1412. (should
  1413. (equal "@multitable {xxx} {xx}\n@item a\n@tab b\n@end multitable"
  1414. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1415. '(:columns "{xxx} {xx}")))))
  1416. (ert-deftest test-org-table/to-orgtbl ()
  1417. "Test `orgtbl-to-orgtbl' specifications."
  1418. (should
  1419. (equal "| a | b |\n|---+---|\n| c | d |"
  1420. (orgtbl-to-orgtbl
  1421. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |") nil))))
  1422. (ert-deftest test-org-table/to-unicode ()
  1423. "Test `orgtbl-to-unicode' specifications."
  1424. (should
  1425. (equal "━━━\n a \n━━━"
  1426. (orgtbl-to-unicode (org-table-to-lisp "| a |") nil)))
  1427. ;; Test :narrow parameter.
  1428. (should
  1429. (equal "━━━━\n => \n━━━━"
  1430. (orgtbl-to-unicode (org-table-to-lisp "| <2> |\n| xxx |")
  1431. '(:narrow t)))))
  1432. (ert-deftest test-org-table/send-region ()
  1433. "Test `orgtbl-send-table' specifications."
  1434. ;; Error when not at a table.
  1435. (should-error
  1436. (org-test-with-temp-text "Paragraph"
  1437. (orgtbl-send-table)))
  1438. ;; Error when destination is missing.
  1439. (should-error
  1440. (org-test-with-temp-text "#+ORGTBL: SEND\n<point>| a |"
  1441. (orgtbl-send-table)))
  1442. ;; Error when transformation function is not specified.
  1443. (should-error
  1444. (org-test-with-temp-text "
  1445. # BEGIN RECEIVE ORGTBL table
  1446. # END RECEIVE ORGTBL table
  1447. #+ORGTBL: SEND table
  1448. <point>| a |"
  1449. (orgtbl-send-table)))
  1450. ;; Standard test.
  1451. (should
  1452. (equal "| a |\n|---|\n| b |\n"
  1453. (org-test-with-temp-text "
  1454. # BEGIN RECEIVE ORGTBL table
  1455. # END RECEIVE ORGTBL table
  1456. #+ORGTBL: SEND table orgtbl-to-orgtbl :hlines nil
  1457. <point>| a |\n|---|\n| b |"
  1458. (orgtbl-send-table)
  1459. (goto-char (point-min))
  1460. (buffer-substring-no-properties
  1461. (search-forward "# BEGIN RECEIVE ORGTBL table\n")
  1462. (progn (search-forward "# END RECEIVE ORGTBL table")
  1463. (match-beginning 0))))))
  1464. ;; Allow multiple receiver locations.
  1465. (should
  1466. (org-test-with-temp-text "
  1467. # BEGIN RECEIVE ORGTBL table
  1468. # END RECEIVE ORGTBL table
  1469. #+ORGTBL: SEND table orgtbl-to-orgtbl :hlines nil
  1470. <point>| a |
  1471. # BEGIN RECEIVE ORGTBL table
  1472. # END RECEIVE ORGTBL table"
  1473. (orgtbl-send-table)
  1474. (goto-char (point-min))
  1475. (search-forward "| a |" nil t 3))))
  1476. ;;; Align
  1477. (ert-deftest test-org-table/align ()
  1478. "Test `org-table-align' specifications."
  1479. ;; Regular test.
  1480. (should
  1481. (equal "| a |\n"
  1482. (org-test-with-temp-text "| a |"
  1483. (org-table-align)
  1484. (buffer-string))))
  1485. ;; Preserve alignment.
  1486. (should
  1487. (equal " | a |\n"
  1488. (org-test-with-temp-text " | a |"
  1489. (org-table-align)
  1490. (buffer-string))))
  1491. ;; Handle horizontal lines.
  1492. (should
  1493. (equal "| 123 |\n|-----|\n"
  1494. (org-test-with-temp-text "| 123 |\n|-|"
  1495. (org-table-align)
  1496. (buffer-string))))
  1497. (should
  1498. (equal "| a | b |\n|---+---|\n"
  1499. (org-test-with-temp-text "| a | b |\n|-+-|"
  1500. (org-table-align)
  1501. (buffer-string))))
  1502. ;; Handle empty fields.
  1503. (should
  1504. (equal "| a | bc |\n| bcd | |\n"
  1505. (org-test-with-temp-text "| a | bc |\n| bcd | |"
  1506. (org-table-align)
  1507. (buffer-string))))
  1508. (should
  1509. (equal "| abc | bc |\n| | bcd |\n"
  1510. (org-test-with-temp-text "| abc | bc |\n| | bcd |"
  1511. (org-table-align)
  1512. (buffer-string))))
  1513. ;; Handle missing fields.
  1514. (should
  1515. (equal "| a | b |\n| c | |\n"
  1516. (org-test-with-temp-text "| a | b |\n| c |"
  1517. (org-table-align)
  1518. (buffer-string))))
  1519. (should
  1520. (equal "| a | b |\n|---+---|\n"
  1521. (org-test-with-temp-text "| a | b |\n|---|"
  1522. (org-table-align)
  1523. (buffer-string))))
  1524. ;; Alignment is done to the right when the ratio of numbers in the
  1525. ;; column is superior to `org-table-number-fraction'.
  1526. (should
  1527. (equal "| 1 |\n| 12 |\n| abc |"
  1528. (org-test-with-temp-text "| 1 |\n| 12 |\n| abc |"
  1529. (let ((org-table-number-fraction 0.5)) (org-table-align))
  1530. (buffer-string))))
  1531. (should
  1532. (equal "| 1 |\n| ab |\n| abc |"
  1533. (org-test-with-temp-text "| 1 |\n| ab |\n| abc |"
  1534. (let ((org-table-number-fraction 0.5)) (org-table-align))
  1535. (buffer-string))))
  1536. ;; Obey to alignment cookies.
  1537. (should
  1538. (equal "| <r> |\n| ab |\n| abc |"
  1539. (org-test-with-temp-text "| <r> |\n| ab |\n| abc |"
  1540. (let ((org-table-number-fraction 0.5)) (org-table-align))
  1541. (buffer-string))))
  1542. (should
  1543. (equal "| <l> |\n| 12 |\n| 123 |"
  1544. (org-test-with-temp-text "| <l> |\n| 12 |\n| 123 |"
  1545. (let ((org-table-number-fraction 0.5)) (org-table-align))
  1546. (buffer-string))))
  1547. (should
  1548. (equal "| <c> |\n| 1 |\n| 123 |"
  1549. (org-test-with-temp-text "| <c> |\n| 1 |\n| 123 |"
  1550. (let ((org-table-number-fraction 0.5)) (org-table-align))
  1551. (buffer-string)))))
  1552. (ert-deftest test-org-table/align-buffer-tables ()
  1553. "Align all tables when updating buffer."
  1554. (let ((before "
  1555. | a b |
  1556. | c d |
  1557. ")
  1558. (after "
  1559. | a b |
  1560. | c d |
  1561. "))
  1562. (should (equal (org-test-with-temp-text before
  1563. (org-table-recalculate-buffer-tables)
  1564. (buffer-string))
  1565. after))
  1566. (should (equal (org-test-with-temp-text before
  1567. (org-table-iterate-buffer-tables)
  1568. (buffer-string))
  1569. after))))
  1570. ;;; Sorting
  1571. (ert-deftest test-org-table/sort-lines ()
  1572. "Test `org-table-sort-lines' specifications."
  1573. ;; Sort numerically.
  1574. (should
  1575. (equal "| 1 | 2 |\n| 2 | 4 |\n| 5 | 3 |\n"
  1576. (org-test-with-temp-text "| <point>1 | 2 |\n| 5 | 3 |\n| 2 | 4 |\n"
  1577. (org-table-sort-lines nil ?n)
  1578. (buffer-string))))
  1579. (should
  1580. (equal "| 5 | 3 |\n| 2 | 4 |\n| 1 | 2 |\n"
  1581. (org-test-with-temp-text "| <point>1 | 2 |\n| 5 | 3 |\n| 2 | 4 |\n"
  1582. (org-table-sort-lines nil ?N)
  1583. (buffer-string))))
  1584. ;; Sort alphabetically. Enforce the C locale for consistent results.
  1585. (let ((original-string-collate-lessp (symbol-function 'string-collate-lessp)))
  1586. (cl-letf (((symbol-function 'string-collate-lessp)
  1587. (lambda (s1 s2 &optional locale ignore-case)
  1588. (funcall original-string-collate-lessp
  1589. s1 s2 "C" ignore-case))))
  1590. (should
  1591. (equal "| a | x |\n| B | 4 |\n| c | 3 |\n"
  1592. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| B | 4 |\n"
  1593. (org-table-sort-lines nil ?a)
  1594. (buffer-string))))
  1595. (should
  1596. (equal "| c | 3 |\n| B | 4 |\n| a | x |\n"
  1597. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| B | 4 |\n"
  1598. (org-table-sort-lines nil ?A)
  1599. (buffer-string))))
  1600. ;; Sort alphabetically with case.
  1601. (should
  1602. (equal "| C |\n| a |\n| b |\n"
  1603. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1604. (org-table-sort-lines t ?a)
  1605. (buffer-string))))
  1606. (should
  1607. (equal "| C |\n| b |\n| a |\n"
  1608. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1609. (org-table-sort-lines nil ?A)
  1610. (buffer-string))))))
  1611. ;; Sort by time (timestamps)
  1612. (should
  1613. (equal
  1614. "| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n| <2014-03-04 tue.> |\n"
  1615. (org-test-with-temp-text
  1616. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1617. (org-table-sort-lines nil ?t)
  1618. (buffer-string))))
  1619. (should
  1620. (equal
  1621. "| <2014-03-04 tue.> |\n| <2012-03-29 thu.> |\n| <2008-08-08 sat.> |\n"
  1622. (org-test-with-temp-text
  1623. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1624. (org-table-sort-lines nil ?T)
  1625. (buffer-string))))
  1626. ;; Sort by time (HH:MM values)
  1627. (should
  1628. (equal "| 1:00 |\n| 17:00 |\n| 114:00 |\n"
  1629. (org-test-with-temp-text "| 114:00 |\n| 17:00 |\n| 1:00 |\n"
  1630. (org-table-sort-lines nil ?t)
  1631. (buffer-string))))
  1632. (should
  1633. (equal "| 114:00 |\n| 17:00 |\n| 1:00 |\n"
  1634. (org-test-with-temp-text "| 114:00 |\n| 17:00 |\n| 1:00 |\n"
  1635. (org-table-sort-lines nil ?T)
  1636. (buffer-string))))
  1637. ;; Sort by time (durations)
  1638. (should
  1639. (equal "| 1d 3:00 |\n| 28:00 |\n"
  1640. (org-test-with-temp-text "| 28:00 |\n| 1d 3:00 |\n"
  1641. (org-table-sort-lines nil ?t)
  1642. (buffer-string))))
  1643. ;; Sort with custom functions.
  1644. (should
  1645. (equal "| 22 |\n| 15 |\n| 18 |\n"
  1646. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1647. (org-table-sort-lines nil ?f
  1648. (lambda (s) (% (string-to-number s) 10))
  1649. #'<)
  1650. (buffer-string))))
  1651. (should
  1652. (equal "| 18 |\n| 15 |\n| 22 |\n"
  1653. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1654. (org-table-sort-lines nil ?F
  1655. (lambda (s) (% (string-to-number s) 10))
  1656. #'<)
  1657. (buffer-string))))
  1658. ;; Sort according to current column.
  1659. (should
  1660. (equal "| 1 | 2 |\n| 7 | 3 |\n| 5 | 4 |\n"
  1661. (org-test-with-temp-text "| 1 | <point>2 |\n| 5 | 4 |\n| 7 | 3 |\n"
  1662. (org-table-sort-lines nil ?n)
  1663. (buffer-string))))
  1664. ;; Sort between horizontal separators if possible.
  1665. (should
  1666. (equal
  1667. "| 9 | 8 |\n|---+---|\n| 5 | 3 |\n| 7 | 4 |\n|---+---|\n| 1 | 2 |\n"
  1668. (org-test-with-temp-text
  1669. "| 9 | 8 |\n|---+---|\n| <point>7 | 4 |\n| 5 | 3 |\n|---+---|\n| 1 | 2 |\n"
  1670. (org-table-sort-lines nil ?n)
  1671. (buffer-string)))))
  1672. ;;; Formulas
  1673. (ert-deftest test-org-table/eval-formula ()
  1674. "Test `org-table-eval-formula' specifications."
  1675. ;; Error when not on a table field.
  1676. (should-error
  1677. (org-test-with-temp-text "Text"
  1678. (org-table-eval-formula)))
  1679. (should-error
  1680. (org-test-with-temp-text "| a |\n|---|<point>"
  1681. (org-table-eval-formula)))
  1682. (should-error
  1683. (org-test-with-temp-text "| a |\n#+TBLFM:<point>"
  1684. (org-table-eval-formula)))
  1685. ;; Handle @<, @>, $< and $>.
  1686. (should
  1687. (equal "| 1 |\n| 1 |"
  1688. (org-test-with-temp-text "| <point> |\n| 1 |"
  1689. (org-table-eval-formula nil "@>" nil nil t)
  1690. (buffer-string))))
  1691. (should
  1692. (equal "| 1 |\n| 1 |"
  1693. (org-test-with-temp-text "| 1 |\n| <point> |"
  1694. (org-table-eval-formula nil "@<" nil nil t)
  1695. (buffer-string))))
  1696. (should
  1697. (equal "| 1 | 1 |"
  1698. (org-test-with-temp-text "| <point> | 1 |"
  1699. (org-table-eval-formula nil "$>" nil nil t)
  1700. (buffer-string))))
  1701. (should
  1702. (equal "| 1 | 1 |"
  1703. (org-test-with-temp-text "| 1 | <point> |"
  1704. (org-table-eval-formula nil "$<" nil nil t)
  1705. (buffer-string)))))
  1706. (ert-deftest test-org-table/field-formula-outside-table ()
  1707. "Test `org-table-formula-create-columns' variable."
  1708. ;; Refuse to create column if variable is nil.
  1709. (should-error
  1710. (org-test-with-temp-text "
  1711. | 2 |
  1712. | 4 |
  1713. | 8 |
  1714. <point>#+TBLFM: @1$2=5"
  1715. (let ((org-table-formula-create-columns nil))
  1716. (org-table-calc-current-TBLFM))
  1717. (buffer-string))
  1718. :type (list 'error 'user-error))
  1719. ;; If the variable is non-nil, field formulas and columns formulas
  1720. ;; can create tables.
  1721. (should
  1722. (equal
  1723. "
  1724. | 2 | 5 |
  1725. | 4 | |
  1726. | 8 | |
  1727. #+TBLFM: @1$2=5"
  1728. (org-test-with-temp-text "
  1729. | 2 |
  1730. | 4 |
  1731. | 8 |
  1732. <point>#+TBLFM: @1$2=5"
  1733. (let ((org-table-formula-create-columns t))
  1734. (org-table-calc-current-TBLFM))
  1735. (buffer-string))))
  1736. (should
  1737. (equal
  1738. "
  1739. | 2 | | 15 |
  1740. | 4 | | 15 |
  1741. | 8 | | 15 |
  1742. #+TBLFM: $3=15"
  1743. (org-test-with-temp-text "
  1744. | 2 |
  1745. | 4 |
  1746. | 8 |
  1747. <point>#+TBLFM: $3=15"
  1748. (let ((org-table-formula-create-columns t))
  1749. (org-table-calc-current-TBLFM))
  1750. (buffer-string)))))
  1751. (ert-deftest test-org-table/duration ()
  1752. "Test durations in table formulas."
  1753. ;; Durations in cells.
  1754. (should
  1755. (string-match "| 2:12 | 1:47 | 03:59:00 |"
  1756. (org-test-with-temp-text "
  1757. | 2:12 | 1:47 | |
  1758. <point>#+TBLFM: @1$3=$1+$2;T"
  1759. (org-table-calc-current-TBLFM)
  1760. (buffer-string))))
  1761. (should
  1762. (string-match "| 2:12 | 1:47 | 03:59 |"
  1763. (org-test-with-temp-text "
  1764. | 2:12 | 1:47 | |
  1765. <point>#+TBLFM: @1$3=$1+$2;U"
  1766. (org-table-calc-current-TBLFM)
  1767. (buffer-string))))
  1768. (should
  1769. (string-match "| 3:02:20 | -2:07:00 | 0.92 |"
  1770. (org-test-with-temp-text "
  1771. | 3:02:20 | -2:07:00 | |
  1772. <point>#+TBLFM: @1$3=$1+$2;t"
  1773. (org-table-calc-current-TBLFM)
  1774. (buffer-string))))
  1775. ;; Durations set through properties.
  1776. (should
  1777. (string-match "| 16:00:00 |"
  1778. (org-test-with-temp-text "* H
  1779. :PROPERTIES:
  1780. :time_constant: 08:00:00
  1781. :END:
  1782. | |
  1783. <point>#+TBLFM: $1=2*$PROP_time_constant;T"
  1784. (org-table-calc-current-TBLFM)
  1785. (buffer-string))))
  1786. (should
  1787. (string-match "| 16.00 |"
  1788. (org-test-with-temp-text "* H
  1789. :PROPERTIES:
  1790. :time_constant: 08:00:00
  1791. :END:
  1792. | |
  1793. <point>#+TBLFM: $1=2*$PROP_time_constant;t"
  1794. (org-table-calc-current-TBLFM)
  1795. (buffer-string)))))
  1796. (ert-deftest test-org-table/end-on-hline ()
  1797. "Test with a table ending on a hline."
  1798. (should
  1799. (equal
  1800. (org-test-with-temp-text
  1801. "
  1802. | 1 | 2 | 3 |
  1803. | 4 | 5 | 6 |
  1804. | | | |
  1805. |---+---+---|
  1806. <point>#+TBLFM: @3$2..@3$>=vsum(@1..@2)"
  1807. (org-table-calc-current-TBLFM)
  1808. (buffer-string))
  1809. "
  1810. | 1 | 2 | 3 |
  1811. | 4 | 5 | 6 |
  1812. | | 7 | 9 |
  1813. |---+---+---|
  1814. #+TBLFM: @3$2..@3$>=vsum(@1..@2)")))
  1815. (ert-deftest test-org-table/named-field ()
  1816. "Test formula with a named field."
  1817. (should
  1818. (string-match-p
  1819. "| +| +1 +|"
  1820. (org-test-with-temp-text "
  1821. | | |
  1822. | ^ | name |
  1823. <point>#+TBLFM: $name=1"
  1824. (org-table-calc-current-TBLFM)
  1825. (buffer-string))))
  1826. (should
  1827. (string-match-p
  1828. "| +| +1 +|"
  1829. (org-test-with-temp-text "
  1830. | _ | name |
  1831. | | |
  1832. <point>#+TBLFM: $name=1"
  1833. (org-table-calc-current-TBLFM)
  1834. (buffer-string)))))
  1835. (ert-deftest test-org-table/named-column ()
  1836. "Test formula with a named field."
  1837. (should
  1838. (string-match-p
  1839. "| +| +1 +| +1 +|"
  1840. (org-test-with-temp-text "
  1841. | ! | name | |
  1842. | | 1 | |
  1843. <point>#+TBLFM: @2$3=$name"
  1844. (org-table-calc-current-TBLFM)
  1845. (buffer-string)))))
  1846. (ert-deftest test-org-table/formula-priority ()
  1847. "Test field formula priority over column formula."
  1848. ;; Field formulas bind stronger than column formulas.
  1849. (should
  1850. (equal
  1851. "| 1 | 3 |\n| 2 | 99 |\n"
  1852. (org-test-with-temp-text
  1853. "| 1 | |\n| 2 | |\n<point>#+tblfm: $2=3*$1::@2$2=99"
  1854. (org-table-calc-current-TBLFM)
  1855. (buffer-substring-no-properties (point-min) (point)))))
  1856. ;; When field formula is removed, table formulas is applied again.
  1857. (should
  1858. (equal
  1859. "| 1 | 3 |\n| 2 | 6 |\n"
  1860. (org-test-with-temp-text
  1861. "| 1 | |\n| 2 | |\n#+tblfm: $2=3*$1<point>::@2$2=99"
  1862. (org-table-calc-current-TBLFM)
  1863. (delete-region (point) (line-end-position))
  1864. (org-table-calc-current-TBLFM)
  1865. (buffer-substring-no-properties (point-min) (line-beginning-position))))))
  1866. (ert-deftest test-org-table/tab-indent ()
  1867. "Test named fields with tab indentation."
  1868. (should
  1869. (string-match-p
  1870. "| # | 111 |"
  1871. (org-test-with-temp-text
  1872. "
  1873. | ! | sum | | a | b | c |
  1874. |---+------+------+---+----+-----|
  1875. | # | 1011 | 1000 | 1 | 10 | 100 |
  1876. <point>#+TBLFM: $2=$a+$b+$c
  1877. "
  1878. (org-table-calc-current-TBLFM)
  1879. (buffer-string)))))
  1880. (ert-deftest test-org-table/first-rc ()
  1881. "Test \"$<\" and \"@<\" constructs in formulas."
  1882. (should
  1883. (string-match-p
  1884. "| 1 | 2 |"
  1885. (org-test-with-temp-text
  1886. "| | 2 |
  1887. <point>#+TBLFM: $<=1"
  1888. (org-table-calc-current-TBLFM)
  1889. (buffer-string))))
  1890. (should
  1891. (string-match-p
  1892. "| 2 |\n| 2 |"
  1893. (org-test-with-temp-text
  1894. "| 2 |\n| |
  1895. <point>#+TBLFM: @2$1=@<"
  1896. (org-table-calc-current-TBLFM)
  1897. (buffer-string)))))
  1898. (ert-deftest test-org-table/last-rc ()
  1899. "Test \"$>\" and \"@>\" constructs in formulas."
  1900. (should
  1901. (string-match-p
  1902. "| 2 | 1 |"
  1903. (org-test-with-temp-text
  1904. "| 2 | |\n<point>#+TBLFM: $>=1"
  1905. (org-table-calc-current-TBLFM)
  1906. (buffer-string))))
  1907. (should
  1908. (string-match-p
  1909. "| 2 |\n| 2 |"
  1910. (org-test-with-temp-text
  1911. "| 2 |\n| |\n<point>#+TBLFM: @>$1=@<"
  1912. (org-table-calc-current-TBLFM)
  1913. (buffer-string)))))
  1914. (ert-deftest test-org-table/time-stamps ()
  1915. "Test time-stamps handling."
  1916. ;; Standard test.
  1917. (should
  1918. (string-match-p
  1919. "| 1 |"
  1920. (org-test-with-temp-text
  1921. "| <2016-07-07 Sun> | <2016-07-08 Fri> | |\n<point>#+TBLFM: $3=$2-$1"
  1922. (org-table-calc-current-TBLFM)
  1923. (buffer-string))))
  1924. ;; Handle locale specific time-stamps.
  1925. (should
  1926. (string-match-p
  1927. "| 1 |"
  1928. (org-test-with-temp-text
  1929. "| <2016-07-07 Do> | <2016-07-08 Fr> | |\n<point>#+TBLFM: $3=$2-$1"
  1930. (org-table-calc-current-TBLFM)
  1931. (buffer-string)))))
  1932. (ert-deftest test-org-table/orgtbl-ascii-draw ()
  1933. "Test `orgtbl-ascii-draw'."
  1934. ;; First value: Make sure that an integer input value is converted to a
  1935. ;; float before division. Further values: Show some float input value
  1936. ;; ranges corresponding to the same bar width.
  1937. (should
  1938. (equal
  1939. (org-test-with-temp-text
  1940. "
  1941. | Value | <l> |
  1942. |----------+---------|
  1943. | 19 | replace |
  1944. |----------+---------|
  1945. | -0.50001 | replace |
  1946. | -0.49999 | replace |
  1947. | 0.49999 | replace |
  1948. | 0.50001 | replace |
  1949. | 1.49999 | replace |
  1950. | 22.50001 | replace |
  1951. | 23.49999 | replace |
  1952. | 23.50001 | replace |
  1953. | 24.49999 | replace |
  1954. | 24.50001 | replace |
  1955. <point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"
  1956. (org-table-calc-current-TBLFM)
  1957. (buffer-string))
  1958. "
  1959. | Value | <l> |
  1960. |----------+-----------|
  1961. | 19 | 883 |
  1962. |----------+-----------|
  1963. | -0.50001 | too small |
  1964. | -0.49999 | |
  1965. | 0.49999 | |
  1966. | 0.50001 | 1 |
  1967. | 1.49999 | 1 |
  1968. | 22.50001 | 887 |
  1969. | 23.49999 | 887 |
  1970. | 23.50001 | 888 |
  1971. | 24.49999 | 888 |
  1972. | 24.50001 | too large |
  1973. #+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"))
  1974. ;; Draw bars with a bullet. The bullet does not count in the parameter
  1975. ;; WIDTH of `orgtbl-ascii-draw'.
  1976. (should
  1977. (equal
  1978. (org-test-with-temp-text
  1979. "
  1980. | -1 | replace |
  1981. | 0 | replace |
  1982. | 1 | replace |
  1983. | 2 | replace |
  1984. | 3 | replace |
  1985. | 4 | replace |
  1986. <point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 3 3 \"$-\")"
  1987. (org-table-calc-current-TBLFM)
  1988. (buffer-string))
  1989. "
  1990. | -1 | too small |
  1991. | 0 | $ |
  1992. | 1 | -$ |
  1993. | 2 | --$ |
  1994. | 3 | ---$ |
  1995. | 4 | too large |
  1996. #+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 3 3 \"$-\")")))
  1997. (ert-deftest test-org-table/single-rowgroup ()
  1998. "Test column formula in a table with a single rowgroup."
  1999. (should
  2000. (equal
  2001. "
  2002. |---+---|
  2003. | 1 | 0 |
  2004. |---+---|
  2005. #+TBLFM: $2=$1-1"
  2006. (org-test-with-temp-text "
  2007. |---+---|
  2008. | 1 | |
  2009. |---+---|
  2010. <point>#+TBLFM: $2=$1-1"
  2011. (org-table-calc-current-TBLFM)
  2012. (buffer-string))))
  2013. (should
  2014. (equal
  2015. "
  2016. | 1 | 0 |
  2017. #+TBLFM: $2=$1-1"
  2018. (org-test-with-temp-text "
  2019. | 1 | |
  2020. <point>#+TBLFM: $2=$1-1"
  2021. (org-table-calc-current-TBLFM)
  2022. (buffer-string)))))
  2023. ;;; Navigation
  2024. (ert-deftest test-org-table/next-field ()
  2025. "Test `org-table-next-field' specifications."
  2026. ;; Regular test.
  2027. (should
  2028. (equal
  2029. "b"
  2030. (org-test-with-temp-text "| a<point> | b |"
  2031. (org-table-next-field)
  2032. (org-trim (org-table-get-field)))))
  2033. ;; Create new rows as needed.
  2034. (should
  2035. (equal
  2036. "| a |\n| |\n"
  2037. (org-test-with-temp-text "| a<point> |"
  2038. (org-table-next-field)
  2039. (buffer-string))))
  2040. ;; Jump over hlines, if `org-table-tab-jumps-over-hlines' is
  2041. ;; non-nil.
  2042. (should
  2043. (equal
  2044. "b"
  2045. (org-test-with-temp-text "| a<point> |\n|---|\n| b |"
  2046. (let ((org-table-tab-jumps-over-hlines t)) (org-table-next-field))
  2047. (org-trim (org-table-get-field)))))
  2048. ;; If `org-table-tab-jumps-over-hlines' is nil, however, create
  2049. ;; a new row before the rule.
  2050. (should
  2051. (equal
  2052. "| a |\n| |\n|---|\n| b |"
  2053. (org-test-with-temp-text "| a<point> |\n|---|\n| b |"
  2054. (let ((org-table-tab-jumps-over-hlines nil)) (org-table-next-field))
  2055. (buffer-string)))))
  2056. (ert-deftest test-org-table/previous-field ()
  2057. "Test `org-table-previous-field' specifications."
  2058. ;; Regular tests.
  2059. (should
  2060. (eq ?a
  2061. (org-test-with-temp-text "| a | <point>b |"
  2062. (org-table-previous-field)
  2063. (char-after))))
  2064. (should
  2065. (eq ?a
  2066. (org-test-with-temp-text "| a |\n| <point>b |"
  2067. (org-table-previous-field)
  2068. (char-after))))
  2069. ;; Find previous field across horizontal rules.
  2070. (should
  2071. (eq ?a
  2072. (org-test-with-temp-text "| a |\n|---|\n| <point>b |"
  2073. (org-table-previous-field)
  2074. (char-after))))
  2075. ;; When called on a horizontal rule, find previous data field.
  2076. (should
  2077. (eq ?b
  2078. (org-test-with-temp-text "| a | b |\n|---+-<point>--|"
  2079. (org-table-previous-field)
  2080. (char-after))))
  2081. ;; Error when at first field. Make sure to preserve original
  2082. ;; position.
  2083. (should-error
  2084. (org-test-with-temp-text "| <point> a|"
  2085. (org-table-previous-field)))
  2086. (should-error
  2087. (org-test-with-temp-text "|---|\n| <point>a |"
  2088. (org-table-previous-field)))
  2089. (should
  2090. (eq ?a
  2091. (org-test-with-temp-text "|---|\n| <point>a |"
  2092. (ignore-errors (org-table-previous-field))
  2093. (char-after)))))
  2094. ;;; Inserting rows, inserting columns
  2095. (ert-deftest test-org-table/insert-column ()
  2096. "Test `org-table-insert-column' specifications."
  2097. ;; Error when outside a table.
  2098. (should-error
  2099. (org-test-with-temp-text "Paragraph"
  2100. (org-table-insert-column)))
  2101. ;; Insert new column after current one.
  2102. (should
  2103. (equal "| a | |\n"
  2104. (org-test-with-temp-text "| a |"
  2105. (org-table-insert-column)
  2106. (buffer-string))))
  2107. (should
  2108. (equal "| a | | b |\n"
  2109. (org-test-with-temp-text "| <point>a | b |"
  2110. (org-table-insert-column)
  2111. (buffer-string))))
  2112. ;; Move point into the newly created column.
  2113. (should
  2114. (equal " |"
  2115. (org-test-with-temp-text "| <point>a |"
  2116. (org-table-insert-column)
  2117. (buffer-substring-no-properties (point) (line-end-position)))))
  2118. (should
  2119. (equal " | b |"
  2120. (org-test-with-temp-text "| <point>a | b |"
  2121. (org-table-insert-column)
  2122. (buffer-substring-no-properties (point) (line-end-position)))))
  2123. ;; Handle missing vertical bar in the last column.
  2124. (should
  2125. (equal "| a | |\n"
  2126. (org-test-with-temp-text "| a"
  2127. (org-table-insert-column)
  2128. (buffer-string))))
  2129. (should
  2130. (equal " |"
  2131. (org-test-with-temp-text "| <point>a"
  2132. (org-table-insert-column)
  2133. (buffer-substring-no-properties (point) (line-end-position)))))
  2134. ;; Handle column insertion when point is before first column.
  2135. (should
  2136. (equal " | a | |\n"
  2137. (org-test-with-temp-text " | a |"
  2138. (org-table-insert-column)
  2139. (buffer-string))))
  2140. (should
  2141. (equal " | a | | b |\n"
  2142. (org-test-with-temp-text " | a | b |"
  2143. (org-table-insert-column)
  2144. (buffer-string)))))
  2145. ;;; Moving single cells
  2146. (ert-deftest test-org-table/move-cell-down ()
  2147. "Test `org-table-move-cell-down' specifications."
  2148. ;; Error out when cell cannot be moved due to not in table, in the
  2149. ;; last row of the table, or is on a hline.
  2150. (should-error
  2151. (org-test-with-temp-text "not in\na table\n"
  2152. (org-table-move-cell-down)))
  2153. (should-error
  2154. (org-test-with-temp-text "| a |"
  2155. (org-table-move-cell-down)))
  2156. (should-error
  2157. (org-test-with-temp-text "| a |\n"
  2158. (org-table-move-cell-down)))
  2159. (should-error
  2160. (org-test-with-temp-text "| a | <point>b |\n"
  2161. (org-table-move-cell-down)))
  2162. (should-error
  2163. (org-test-with-temp-text "| a | b |\n| <point>c | d |\n"
  2164. (org-table-move-cell-down)))
  2165. (should-error
  2166. (org-test-with-temp-text "| a | b |\n| c | <point>d |\n"
  2167. (org-table-move-cell-down)))
  2168. (should-error
  2169. (org-test-with-temp-text "| <point>a |\n|---|\n"
  2170. (org-table-move-cell-down)))
  2171. (should-error
  2172. (org-test-with-temp-text "|<point>---|\n| a |\n"
  2173. (org-table-move-cell-down)))
  2174. ;; Check for correct cell movement
  2175. (should (equal (concat "| c | b |\n"
  2176. "| a | d |\n"
  2177. "| e | f |\n")
  2178. (org-test-with-temp-text
  2179. (concat "| <point>a | b |\n"
  2180. "| c | d |\n"
  2181. "| e | f |\n")
  2182. (org-table-move-cell-down)
  2183. (buffer-string))))
  2184. (should (equal (concat "| a | d |\n"
  2185. "| c | b |\n"
  2186. "| e | f |\n")
  2187. (org-test-with-temp-text
  2188. (concat "| a | <point>b |\n"
  2189. "| c | d |\n"
  2190. "| e | f |\n")
  2191. (org-table-move-cell-down)
  2192. (buffer-string))))
  2193. (should (equal (concat "| a | b |\n"
  2194. "| e | d |\n"
  2195. "| c | f |\n")
  2196. (org-test-with-temp-text
  2197. (concat "| a | b |\n"
  2198. "| <point>c | d |\n"
  2199. "| e | f |\n")
  2200. (org-table-move-cell-down)
  2201. (buffer-string))))
  2202. (should (equal (concat "| a | d |\n"
  2203. "| c | f |\n"
  2204. "| e | b |\n")
  2205. (org-test-with-temp-text
  2206. (concat "| a |<point> b |\n"
  2207. "| c | d |\n"
  2208. "| e | f |\n")
  2209. (org-table-move-cell-down)
  2210. (org-table-move-cell-down)
  2211. (buffer-string))))
  2212. ;; Check for correct handling of hlines which should not change
  2213. ;; position on single cell moves.
  2214. (should (equal (concat "| c | b |\n"
  2215. "|---+---|\n"
  2216. "| a | d |\n"
  2217. "| e | f |\n")
  2218. (org-test-with-temp-text
  2219. (concat "| <point>a | b |\n"
  2220. "|---+---|\n"
  2221. "| c | d |\n"
  2222. "| e | f |\n")
  2223. (org-table-move-cell-down)
  2224. (buffer-string))))
  2225. (should (equal (concat "| a | d |\n"
  2226. "|---+---|\n"
  2227. "| c | f |\n"
  2228. "| e | b |\n")
  2229. (org-test-with-temp-text
  2230. (concat "| a | <point>b |\n"
  2231. "|---+---|\n"
  2232. "| c | d |\n"
  2233. "| e | f |\n")
  2234. (org-table-move-cell-down)
  2235. (org-table-move-cell-down)
  2236. (buffer-string))))
  2237. (should (equal (concat "| a | b |\n"
  2238. "|---+---|\n"
  2239. "| c | f |\n"
  2240. "| e | d |\n")
  2241. (org-test-with-temp-text
  2242. (concat "| a | b |\n"
  2243. "|---+---|\n"
  2244. "| c | <point>d |\n"
  2245. "| e | f |\n")
  2246. (org-table-move-cell-down)
  2247. (buffer-string))))
  2248. ;; Move single cell even without a final newline.
  2249. (should (equal (concat "| a | d |\n"
  2250. "|---+---|\n"
  2251. "| c | f |\n"
  2252. "| e | b |\n")
  2253. (org-test-with-temp-text
  2254. (concat "| a | <point>b |\n"
  2255. "|---+---|\n"
  2256. "| c | d |\n"
  2257. "| e | f |")
  2258. (org-table-move-cell-down)
  2259. (org-table-move-cell-down)
  2260. (buffer-string)))))
  2261. (ert-deftest test-org-table/move-cell-up ()
  2262. "Test `org-table-move-cell-up' specifications."
  2263. ;; Error out when cell cannot be moved due to not in table, in the
  2264. ;; last row of the table, or is on a hline.
  2265. (should-error
  2266. (org-test-with-temp-text "not in\na table\n"
  2267. (org-table-move-cell-up)))
  2268. (should-error
  2269. (org-test-with-temp-text "| a |"
  2270. (org-table-move-cell-up)))
  2271. (should-error
  2272. (org-test-with-temp-text "| a |\n"
  2273. (org-table-move-cell-up)))
  2274. (should-error
  2275. (org-test-with-temp-text "| <point>a | b |\n"
  2276. (org-table-move-cell-up)))
  2277. (should-error
  2278. (org-test-with-temp-text "| a | <point>b |\n| c | d |\n"
  2279. (org-table-move-cell-up)))
  2280. (should-error
  2281. (org-test-with-temp-text "| <point>a |\n|---|\n"
  2282. (org-table-move-cell-up)))
  2283. (should-error
  2284. (org-test-with-temp-text "|<point>---|\n| a |\n"
  2285. (org-table-move-cell-up)))
  2286. ;; Check for correct cell movement.
  2287. (should (equal (concat "| c | b |\n"
  2288. "| a | d |\n"
  2289. "| e | f |\n")
  2290. (org-test-with-temp-text
  2291. (concat "| a | b |\n"
  2292. "| <point>c | d |\n"
  2293. "| e | f |\n")
  2294. (org-table-move-cell-up)
  2295. (buffer-string))))
  2296. (should (equal (concat "| a | d |\n"
  2297. "| c | b |\n"
  2298. "| e | f |\n")
  2299. (org-test-with-temp-text
  2300. (concat "| a | b |\n"
  2301. "| c | <point>d |\n"
  2302. "| e | f |\n")
  2303. (org-table-move-cell-up)
  2304. (buffer-string))))
  2305. (should (equal (concat "| a | b |\n"
  2306. "| e | d |\n"
  2307. "| c | f |\n")
  2308. (org-test-with-temp-text
  2309. (concat "| a | b |\n"
  2310. "| c | d |\n"
  2311. "| <point>e | f |\n")
  2312. (org-table-move-cell-up)
  2313. (buffer-string))))
  2314. (should (equal (concat "| a | f |\n"
  2315. "| c | b |\n"
  2316. "| e | d |\n")
  2317. (org-test-with-temp-text
  2318. (concat "| a | b |\n"
  2319. "| c | d |\n"
  2320. "| e |<point> f |\n")
  2321. (org-table-move-cell-up)
  2322. (org-table-move-cell-up)
  2323. (buffer-string))))
  2324. ;; Check for correct handling of hlines which should not change
  2325. ;; position on single cell moves.
  2326. (should (equal (concat "| c | b |\n"
  2327. "|---+---|\n"
  2328. "| a | d |\n"
  2329. "| e | f |\n")
  2330. (org-test-with-temp-text
  2331. (concat "| a | b |\n"
  2332. "|---+---|\n"
  2333. "| <point>c | d |\n"
  2334. "| e | f |\n")
  2335. (org-table-move-cell-up)
  2336. (buffer-string))))
  2337. (should (equal (concat "| a | f |\n"
  2338. "|---+---|\n"
  2339. "| c | b |\n"
  2340. "| e | d |\n")
  2341. (org-test-with-temp-text
  2342. (concat "| a | b |\n"
  2343. "|---+---|\n"
  2344. "| c | d |\n"
  2345. "| e | <point>f |\n")
  2346. (org-table-move-cell-up)
  2347. (org-table-move-cell-up)
  2348. (buffer-string))))
  2349. (should (equal (concat "| a | b |\n"
  2350. "|---+---|\n"
  2351. "| c | f |\n"
  2352. "| e | d |\n")
  2353. (org-test-with-temp-text
  2354. (concat "| a | b |\n"
  2355. "|---+---|\n"
  2356. "| c | d |\n"
  2357. "| e | <point>f |\n")
  2358. (org-table-move-cell-up)
  2359. (buffer-string))))
  2360. ;; Move single cell even without a final newline.
  2361. (should (equal (concat "| a | f |\n"
  2362. "|---+---|\n"
  2363. "| c | b |\n"
  2364. "| e | d |\n")
  2365. (org-test-with-temp-text
  2366. (concat "| a | b |\n"
  2367. "|---+---|\n"
  2368. "| c | d |\n"
  2369. "| e | <point>f |")
  2370. (org-table-move-cell-up)
  2371. (org-table-move-cell-up)
  2372. (buffer-string)))))
  2373. (ert-deftest test-org-table/move-cell-right ()
  2374. "Test `org-table-move-cell-right' specifications."
  2375. ;; Error out when cell cannot be moved due to not in table, in the
  2376. ;; last col of the table, or is on a hline.
  2377. (should-error
  2378. (org-test-with-temp-text "not in\na table\n"
  2379. (org-table-move-cell-right)))
  2380. (should-error
  2381. (org-test-with-temp-text "| a |"
  2382. (org-table-move-cell-right)))
  2383. (should-error
  2384. (org-test-with-temp-text "| a |\n"
  2385. (org-table-move-cell-right)))
  2386. (should-error
  2387. (org-test-with-temp-text "| <point>a |\n| b |\n"
  2388. (org-table-move-cell-right)))
  2389. (should-error
  2390. (org-test-with-temp-text "| a | <point>b |\n| c | d |\n"
  2391. (org-table-move-cell-right)))
  2392. (should-error
  2393. (org-test-with-temp-text "| <point>a |\n|---|\n"
  2394. (org-table-move-cell-right)))
  2395. (should-error
  2396. (org-test-with-temp-text "|<point>---|\n| a |\n"
  2397. (org-table-move-cell-right)))
  2398. ;; Check for correct cell movement.
  2399. (should (equal (concat "| b | a | c |\n"
  2400. "| d | e | f |\n")
  2401. (org-test-with-temp-text
  2402. (concat "| <point>a | b | c |\n"
  2403. "| d | e | f |\n")
  2404. (org-table-move-cell-right)
  2405. (buffer-string))))
  2406. (should (equal (concat "| b | c | a |\n"
  2407. "| d | e | f |\n")
  2408. (org-test-with-temp-text
  2409. (concat "| <point>a | b | c |\n"
  2410. "| d | e | f |\n")
  2411. (org-table-move-cell-right)
  2412. (org-table-move-cell-right)
  2413. (buffer-string))))
  2414. (should (equal (concat "| a | b | c |\n"
  2415. "| e | f | d |\n")
  2416. (org-test-with-temp-text
  2417. (concat "| a | b | c |\n"
  2418. "| <point> d | e | f |\n")
  2419. (org-table-move-cell-right)
  2420. (org-table-move-cell-right)
  2421. (buffer-string))))
  2422. (should (equal (concat "| a | b | c |\n"
  2423. "| d | f | e |\n")
  2424. (org-test-with-temp-text
  2425. (concat "| a | b | c |\n"
  2426. "| d | <point>e | f |\n")
  2427. (org-table-move-cell-right)
  2428. (buffer-string))))
  2429. (should (equal (concat "| a | b | c |\n"
  2430. "|---+---+---|\n"
  2431. "| e | f | d |\n")
  2432. (org-test-with-temp-text
  2433. (concat "| a | b | c |\n"
  2434. "|---+---+---|\n"
  2435. "| <point>d | e | f |\n")
  2436. (org-table-move-cell-right)
  2437. (org-table-move-cell-right)
  2438. (buffer-string))))
  2439. ;; Move single cell even without a final newline.
  2440. (should (equal (concat "| a | b | c |\n"
  2441. "|---+---+---|\n"
  2442. "| e | d | f |\n")
  2443. (org-test-with-temp-text
  2444. (concat "| a | b | c |\n"
  2445. "|---+---+---|\n"
  2446. "| <point>d | e | f |")
  2447. (org-table-move-cell-right)
  2448. (buffer-string)))))
  2449. (ert-deftest test-org-table/move-cell-left ()
  2450. "Test `org-table-move-cell-left' specifications."
  2451. ;; Error out when cell cannot be moved due to not in table, in the
  2452. ;; last col of the table, or is on a hline.
  2453. (should-error
  2454. (org-test-with-temp-text "not in\na table\n"
  2455. (org-table-move-cell-left)))
  2456. (should-error
  2457. (org-test-with-temp-text "| a |"
  2458. (org-table-move-cell-left)))
  2459. (should-error
  2460. (org-test-with-temp-text "| a |\n"
  2461. (org-table-move-cell-left)))
  2462. (should-error
  2463. (org-test-with-temp-text "| <point>a |\n| b |\n"
  2464. (org-table-move-cell-left)))
  2465. (should-error
  2466. (org-test-with-temp-text "| <point>a | b |\n| c | d |\n"
  2467. (org-table-move-cell-left)))
  2468. (should-error
  2469. (org-test-with-temp-text "| <point>a |\n|---|\n"
  2470. (org-table-move-cell-left)))
  2471. (should-error
  2472. (org-test-with-temp-text "|<point>---|\n| a |\n"
  2473. (org-table-move-cell-left)))
  2474. ;; Check for correct cell movement.
  2475. (should (equal (concat "| b | a | c |\n"
  2476. "| d | e | f |\n")
  2477. (org-test-with-temp-text
  2478. (concat "| a | <point>b | c |\n"
  2479. "| d | e | f |\n")
  2480. (org-table-move-cell-left)
  2481. (buffer-string))))
  2482. (should (equal (concat "| c | a | b |\n"
  2483. "| d | e | f |\n")
  2484. (org-test-with-temp-text
  2485. (concat "| a | b | <point>c |\n"
  2486. "| d | e | f |\n")
  2487. (org-table-move-cell-left)
  2488. (org-table-move-cell-left)
  2489. (buffer-string))))
  2490. (should (equal (concat "| a | b | c |\n"
  2491. "| f | d | e |\n")
  2492. (org-test-with-temp-text
  2493. (concat "| a | b | c |\n"
  2494. "| d | e | <point>f |\n")
  2495. (org-table-move-cell-left)
  2496. (org-table-move-cell-left)
  2497. (buffer-string))))
  2498. (should (equal (concat "| a | b | c |\n"
  2499. "| d | f | e |\n")
  2500. (org-test-with-temp-text
  2501. (concat "| a | b | c |\n"
  2502. "| d | e | <point>f |\n")
  2503. (org-table-move-cell-left)
  2504. (buffer-string))))
  2505. (should (equal (concat "| a | b | c |\n"
  2506. "|---+---+---|\n"
  2507. "| f | d | e |\n")
  2508. (org-test-with-temp-text
  2509. (concat "| a | b | c |\n"
  2510. "|---+---+---|\n"
  2511. "| d | e | <point>f |\n")
  2512. (org-table-move-cell-left)
  2513. (org-table-move-cell-left)
  2514. (buffer-string))))
  2515. ;; Move single cell even without a final newline.
  2516. (should (equal (concat "| a | b | c |\n"
  2517. "|---+---+---|\n"
  2518. "| e | d | f |\n")
  2519. (org-test-with-temp-text
  2520. (concat "| a | b | c |\n"
  2521. "|---+---+---|\n"
  2522. "| d | <point>e | f |")
  2523. (org-table-move-cell-left)
  2524. (buffer-string)))))
  2525. ;;; Moving rows, moving columns
  2526. (ert-deftest test-org-table/move-row-down ()
  2527. "Test `org-table-move-row-down' specifications."
  2528. ;; Error out when row cannot be moved, e.g., it is the last row in
  2529. ;; the table.
  2530. (should-error
  2531. (org-test-with-temp-text "| a |"
  2532. (org-table-move-row-down)))
  2533. (should-error
  2534. (org-test-with-temp-text "| a |\n"
  2535. (org-table-move-row-down)))
  2536. (should-error
  2537. (org-test-with-temp-text "| a |\n| <point>b |"
  2538. (org-table-move-row-down)))
  2539. ;; Move data lines.
  2540. (should
  2541. (equal "| b |\n| a |\n"
  2542. (org-test-with-temp-text "| a |\n| b |\n"
  2543. (org-table-move-row-down)
  2544. (buffer-string))))
  2545. (should
  2546. (equal "|---|\n| a |\n"
  2547. (org-test-with-temp-text "| a |\n|---|\n"
  2548. (org-table-move-row-down)
  2549. (buffer-string))))
  2550. ;; Move hlines.
  2551. (should
  2552. (equal "| b |\n|---|\n"
  2553. (org-test-with-temp-text "|---|\n| b |\n"
  2554. (org-table-move-row-down)
  2555. (buffer-string))))
  2556. (should
  2557. (equal "|---|\n|---|\n"
  2558. (org-test-with-temp-text "|---|\n|---|\n"
  2559. (org-table-move-row-down)
  2560. (buffer-string))))
  2561. ;; Move rows even without a final newline.
  2562. (should
  2563. (equal "| b |\n| a |\n"
  2564. (org-test-with-temp-text "| a |\n| b |"
  2565. (org-table-move-row-down)
  2566. (buffer-string)))))
  2567. (ert-deftest test-org-table/move-row-up ()
  2568. "Test `org-table-move-row-up' specifications."
  2569. ;; Error out when row cannot be moved, e.g., it is the first row in
  2570. ;; the table.
  2571. (should-error
  2572. (org-test-with-temp-text "| a |"
  2573. (org-table-move-row-up)))
  2574. (should-error
  2575. (org-test-with-temp-text "| a |\n"
  2576. (org-table-move-row-up)))
  2577. ;; Move data lines.
  2578. (should
  2579. (equal "| b |\n| a |\n"
  2580. (org-test-with-temp-text "| a |\n| <point>b |\n"
  2581. (org-table-move-row-up)
  2582. (buffer-string))))
  2583. (should
  2584. (equal "| b |\n|---|\n"
  2585. (org-test-with-temp-text "|---|\n| <point>b |\n"
  2586. (org-table-move-row-up)
  2587. (buffer-string))))
  2588. ;; Move hlines.
  2589. (should
  2590. (equal "|---|\n| a |\n"
  2591. (org-test-with-temp-text "| a |\n|<point>---|\n"
  2592. (org-table-move-row-up)
  2593. (buffer-string))))
  2594. (should
  2595. (equal "|---|\n|---|\n"
  2596. (org-test-with-temp-text "|---|\n|<point>---|\n"
  2597. (org-table-move-row-up)
  2598. (buffer-string))))
  2599. ;; Move rows even without a final newline.
  2600. (should
  2601. (equal "| b |\n| a |\n"
  2602. (org-test-with-temp-text "| a |\n| <point>b |"
  2603. (org-table-move-row-up)
  2604. (buffer-string)))))
  2605. ;;; Shrunk columns
  2606. (ert-deftest test-org-table/toggle-column-width ()
  2607. "Test `org-table-toggle-columns-width' specifications."
  2608. ;; Error when not at a column.
  2609. (should-error
  2610. (org-test-with-temp-text "<point>a"
  2611. (org-table-toggle-column-width)))
  2612. ;; A shrunk column is overlaid with
  2613. ;; `org-table-shrunk-column-indicator'.
  2614. (should
  2615. (equal org-table-shrunk-column-indicator
  2616. (org-test-with-temp-text "| <point>a |"
  2617. (org-table-toggle-column-width)
  2618. (overlay-get (car (overlays-at (point))) 'display))))
  2619. (should
  2620. (equal org-table-shrunk-column-indicator
  2621. (org-test-with-temp-text "| a |\n|-<point>--|"
  2622. (org-table-toggle-column-width)
  2623. (overlay-get (car (overlays-at (point))) 'display))))
  2624. ;; Shrink every field in the same column.
  2625. (should
  2626. (equal org-table-shrunk-column-indicator
  2627. (org-test-with-temp-text "| a |\n|-<point>--|"
  2628. (org-table-toggle-column-width)
  2629. (overlay-get (car (overlays-at (1+ (line-beginning-position 0))))
  2630. 'display))))
  2631. ;; When column is already shrunk, expand it, i.e., remove overlays.
  2632. (should-not
  2633. (org-test-with-temp-text "| <point>a |"
  2634. (org-table-toggle-column-width)
  2635. (org-table-toggle-column-width)
  2636. (overlays-in (point-min) (point-max))))
  2637. (should-not
  2638. (org-test-with-temp-text "| a |\n| <point>b |"
  2639. (org-table-toggle-column-width)
  2640. (org-table-toggle-column-width)
  2641. (overlays-in (point-min) (point-max))))
  2642. ;; With a column width cookie, limit overlay to the specified number
  2643. ;; of characters.
  2644. (should
  2645. (equal "| abc"
  2646. (org-test-with-temp-text "| <3> |\n| <point>abcd |"
  2647. (org-table-toggle-column-width)
  2648. (buffer-substring (line-beginning-position)
  2649. (overlay-start
  2650. (car (overlays-in (line-beginning-position)
  2651. (line-end-position))))))))
  2652. (should
  2653. (equal "| a "
  2654. (org-test-with-temp-text "| <3> |\n| <point>a |"
  2655. (org-table-toggle-column-width)
  2656. (buffer-substring (line-beginning-position)
  2657. (overlay-start
  2658. (car (overlays-in (line-beginning-position)
  2659. (line-end-position))))))))
  2660. (should
  2661. (equal (concat "----" org-table-shrunk-column-indicator)
  2662. (org-test-with-temp-text "| <3> |\n|--<point>----|"
  2663. (org-table-toggle-column-width)
  2664. (overlay-get
  2665. (car (overlays-in (line-beginning-position)
  2666. (line-end-position)))
  2667. 'display))))
  2668. ;; Width only takes into account visible characters.
  2669. (should
  2670. (equal "| [[http"
  2671. (org-test-with-temp-text "| <4> |\n| <point>[[http://orgmode.org]] |"
  2672. (org-table-toggle-column-width)
  2673. (buffer-substring (line-beginning-position)
  2674. (overlay-start
  2675. (car (overlays-in (line-beginning-position)
  2676. (line-end-position))))))))
  2677. ;; Before the first column or after the last one, ask for columns
  2678. ;; ranges.
  2679. (should
  2680. (catch :exit
  2681. (org-test-with-temp-text "| a |"
  2682. (cl-letf (((symbol-function 'read-string)
  2683. (lambda (&rest_) (throw :exit t))))
  2684. (org-table-toggle-column-width)
  2685. nil))))
  2686. (should
  2687. (catch :exit
  2688. (org-test-with-temp-text "| a |<point>"
  2689. (cl-letf (((symbol-function 'read-string)
  2690. (lambda (&rest_) (throw :exit t))))
  2691. (org-table-toggle-column-width)
  2692. nil))))
  2693. ;; When optional argument ARG is a string, toggle specified columns.
  2694. (should
  2695. (equal org-table-shrunk-column-indicator
  2696. (org-test-with-temp-text "| <point>a | b |"
  2697. (org-table-toggle-column-width "2")
  2698. (overlay-get (car (overlays-at (- (point-max) 2))) 'display))))
  2699. (should
  2700. (equal '("b" "c")
  2701. (org-test-with-temp-text "| a | b | c | d |"
  2702. (org-table-toggle-column-width "2-3")
  2703. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2704. (overlays-in (point-min) (point-max)))
  2705. #'string-lessp))))
  2706. (should
  2707. (equal '("b" "c" "d")
  2708. (org-test-with-temp-text "| a | b | c | d |"
  2709. (org-table-toggle-column-width "2-")
  2710. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2711. (overlays-in (point-min) (point-max)))
  2712. #'string-lessp))))
  2713. (should
  2714. (equal '("a" "b")
  2715. (org-test-with-temp-text "| a | b | c | d |"
  2716. (org-table-toggle-column-width "-2")
  2717. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2718. (overlays-in (point-min) (point-max)))
  2719. #'string-lessp))))
  2720. (should
  2721. (equal '("a" "b" "c" "d")
  2722. (org-test-with-temp-text "| a | b | c | d |"
  2723. (org-table-toggle-column-width "-")
  2724. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2725. (overlays-in (point-min) (point-max)))
  2726. #'string-lessp))))
  2727. (should
  2728. (equal '("a" "d")
  2729. (org-test-with-temp-text "| a | b | c | d |"
  2730. (org-table-toggle-column-width "1-3")
  2731. (org-table-toggle-column-width "2-4")
  2732. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2733. (overlays-in (point-min) (point-max)))
  2734. #'string-lessp))))
  2735. ;; When ARG is (16), remove any column overlay.
  2736. (should-not
  2737. (org-test-with-temp-text "| <point>a |"
  2738. (org-table-toggle-column-width)
  2739. (org-table-toggle-column-width '(16))
  2740. (overlays-in (point-min) (point-max))))
  2741. (should-not
  2742. (org-test-with-temp-text "| a | b | c | d |"
  2743. (org-table-toggle-column-width "-")
  2744. (org-table-toggle-column-width '(16))
  2745. (overlays-in (point-min) (point-max)))))
  2746. (ert-deftest test-org-table/shrunk-columns ()
  2747. "Test behaviour of shrunk column."
  2748. ;; Edition automatically expands a shrunk column.
  2749. (should-not
  2750. (org-test-with-temp-text "| <point>a |"
  2751. (org-table-toggle-column-width)
  2752. (insert "a")
  2753. (overlays-in (point-min) (point-max))))
  2754. ;; Other columns are not changed.
  2755. (should
  2756. (org-test-with-temp-text "| <point>a | b |"
  2757. (org-table-toggle-column-width "-")
  2758. (insert "a")
  2759. (overlays-in (point-min) (point-max))))
  2760. ;; Moving a shrunk column doesn't alter its state.
  2761. (should
  2762. (equal "a"
  2763. (org-test-with-temp-text "| <point>a | b |"
  2764. (org-table-toggle-column-width)
  2765. (org-table-move-column-right)
  2766. (overlay-get (car (overlays-at (point))) 'help-echo))))
  2767. (should
  2768. (equal "a"
  2769. (org-test-with-temp-text "| <point>a |\n| b |"
  2770. (org-table-toggle-column-width)
  2771. (org-table-move-row-down)
  2772. (overlay-get (car (overlays-at (point))) 'help-echo))))
  2773. ;; State is preserved upon inserting a column.
  2774. (should
  2775. (equal '("a")
  2776. (org-test-with-temp-text "| <point>a |"
  2777. (org-table-toggle-column-width)
  2778. (org-table-insert-column)
  2779. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2780. (overlays-in (point-min) (point-max)))
  2781. #'string-lessp))))
  2782. ;; State is preserved upon deleting a column.
  2783. (should
  2784. (equal '("a" "c")
  2785. (org-test-with-temp-text "| a | <point>b | c |"
  2786. (org-table-toggle-column-width "-")
  2787. (org-table-delete-column)
  2788. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2789. (overlays-in (point-min) (point-max)))
  2790. #'string-lessp))))
  2791. ;; State is preserved upon deleting a row.
  2792. (should
  2793. (equal '("b1" "b2")
  2794. (org-test-with-temp-text "| a1 | a2 |\n| b1 | b2 |"
  2795. (org-table-toggle-column-width "-")
  2796. (org-table-kill-row)
  2797. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2798. (overlays-in (point-min) (point-max)))
  2799. #'string-lessp))))
  2800. (should
  2801. (equal '("a1" "a2")
  2802. (org-test-with-temp-text "| a1 | a2 |\n| <point>b1 | b2 |"
  2803. (org-table-toggle-column-width "-")
  2804. (org-table-kill-row)
  2805. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2806. (overlays-in (point-min) (point-max)))
  2807. #'string-lessp))))
  2808. ;; State is preserved upon inserting a row or hline.
  2809. (should
  2810. (equal '("" "a1" "b1")
  2811. (org-test-with-temp-text "| a1 | a2 |\n| <point>b1 | b2 |"
  2812. (org-table-toggle-column-width)
  2813. (org-table-insert-row)
  2814. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2815. (overlays-in (point-min) (point-max)))
  2816. #'string-lessp))))
  2817. (should
  2818. (equal '("a1" "b1")
  2819. (org-test-with-temp-text "| a1 | a2 |\n| <point>b1 | b2 |"
  2820. (org-table-toggle-column-width)
  2821. (org-table-insert-hline)
  2822. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2823. (overlays-in (point-min) (point-max)))
  2824. #'string-lessp))))
  2825. ;; State is preserved upon sorting a column for all the columns but
  2826. ;; the one being sorted.
  2827. (should
  2828. (equal '("a2" "b2")
  2829. (org-test-with-temp-text "| <point>a1 | a2 |\n| <point>b1 | b2 |"
  2830. (org-table-toggle-column-width "-")
  2831. (org-table-sort-lines nil ?A)
  2832. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2833. (overlays-in (point-min) (point-max)))
  2834. #'string-lessp))))
  2835. ;; State is preserved upon replacing a field non-interactively.
  2836. (should
  2837. (equal '("a")
  2838. (org-test-with-temp-text "| <point>a |"
  2839. (org-table-toggle-column-width)
  2840. (org-table-get-field nil "b")
  2841. (mapcar (lambda (o) (overlay-get o 'help-echo))
  2842. (overlays-in (point-min) (point-max))))))
  2843. ;; Moving to next field doesn't change shrunk state.
  2844. (should
  2845. (equal "a"
  2846. (org-test-with-temp-text "| <point>a | b |"
  2847. (org-table-toggle-column-width)
  2848. (org-table-next-field)
  2849. (overlay-get (car (overlays-at (1+ (line-beginning-position))))
  2850. 'help-echo))))
  2851. (should
  2852. (equal "b"
  2853. (org-test-with-temp-text "| a | <point>b |"
  2854. (org-table-toggle-column-width)
  2855. (goto-char 2)
  2856. (org-table-next-field)
  2857. (overlay-get (car (overlays-at (point))) 'help-echo))))
  2858. ;; Aligning table doesn't alter shrunk state.
  2859. (should
  2860. (equal "a"
  2861. (org-test-with-temp-text "| <point>a | b |"
  2862. (org-table-toggle-column-width)
  2863. (org-table-align)
  2864. (overlay-get (car (overlays-at (1+ (line-beginning-position))))
  2865. 'help-echo))))
  2866. (should
  2867. (equal "b"
  2868. (org-test-with-temp-text "|---+-----|\n| a | <point>b |"
  2869. (org-table-toggle-column-width)
  2870. (org-table-align)
  2871. (overlay-get (car (overlays-at (point)))
  2872. 'help-echo))))
  2873. (should
  2874. (equal
  2875. '("b")
  2876. (org-test-with-temp-text "|---+-----|\n| a | <point>b |"
  2877. (org-table-toggle-column-width)
  2878. (org-table-align)
  2879. (mapcar (lambda (o) (overlay-get o 'help-echo))
  2880. (overlays-in (line-beginning-position) (line-end-position)))))))
  2881. ;;; Miscellaneous
  2882. (ert-deftest test-org-table/current-column ()
  2883. "Test `org-table-current-column' specifications."
  2884. (should
  2885. (= 1 (org-test-with-temp-text "| <point>a |"
  2886. (org-table-current-column))))
  2887. (should
  2888. (= 1 (org-test-with-temp-text "|-<point>--|"
  2889. (org-table-current-column))))
  2890. (should
  2891. (= 2 (org-test-with-temp-text "| 1 | <point>2 |"
  2892. (org-table-current-column))))
  2893. (should
  2894. (= 2 (org-test-with-temp-text "|---+-<point>--|"
  2895. (org-table-current-column)))))
  2896. (ert-deftest test-org-table/get-field ()
  2897. "Test `org-table-get-field' specifications."
  2898. ;; Regular test.
  2899. (should
  2900. (equal " a "
  2901. (org-test-with-temp-text "| <point>a |" (org-table-get-field))))
  2902. ;; Get field in open last column.
  2903. (should
  2904. (equal " a "
  2905. (org-test-with-temp-text "| <point>a " (org-table-get-field))))
  2906. ;; Get empty field.
  2907. (should
  2908. (equal ""
  2909. (org-test-with-temp-text "|<point>|" (org-table-get-field))))
  2910. (should
  2911. (equal " "
  2912. (org-test-with-temp-text "| <point>|" (org-table-get-field))))
  2913. ;; Outside the table, return the empty string.
  2914. (should
  2915. (equal ""
  2916. (org-test-with-temp-text "<point>| a |" (org-table-get-field))))
  2917. (should
  2918. (equal ""
  2919. (org-test-with-temp-text "| a |<point>" (org-table-get-field))))
  2920. ;; With optional N argument, select a particular column in current
  2921. ;; row.
  2922. (should
  2923. (equal " 3 "
  2924. (org-test-with-temp-text "| 1 | 2 | 3 |" (org-table-get-field 3))))
  2925. (should
  2926. (equal " 4 "
  2927. (org-test-with-temp-text "| 1 | 2 |\n<point>| 3 | 4 |"
  2928. (org-table-get-field 2))))
  2929. ;; REPLACE optional argument is used to replace selected field.
  2930. (should
  2931. (equal "| foo |"
  2932. (org-test-with-temp-text "| <point>1 |"
  2933. (org-table-get-field nil " foo ")
  2934. (buffer-string))))
  2935. (should
  2936. (equal "| 1 | 2 | foo |"
  2937. (org-test-with-temp-text "| 1 | 2 | 3 |"
  2938. (org-table-get-field 3 " foo ")
  2939. (buffer-string))))
  2940. (should
  2941. (equal " 4 "
  2942. (org-test-with-temp-text "| 1 | 2 |\n<point>| 3 | 4 |"
  2943. (org-table-get-field 2))))
  2944. ;; An empty REPLACE string clears the field.
  2945. (should
  2946. (equal "| |"
  2947. (org-test-with-temp-text "| <point>1 |"
  2948. (org-table-get-field nil "")
  2949. (buffer-string))))
  2950. ;; When using REPLACE still return old value.
  2951. (should
  2952. (equal " 1 "
  2953. (org-test-with-temp-text "| <point>1 |"
  2954. (org-table-get-field nil " foo ")))))
  2955. (provide 'test-org-table)
  2956. ;;; test-org-table.el ends here