test-org-table.el 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715
  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.
  1585. (should
  1586. (equal "| a | x |\n| b | 4 |\n| c | 3 |\n"
  1587. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| b | 4 |\n"
  1588. (org-table-sort-lines nil ?a)
  1589. (buffer-string))))
  1590. (should
  1591. (equal "| c | 3 |\n| b | 4 |\n| a | x |\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. ;; Sort alphabetically with case.
  1596. (should
  1597. (equal "| C |\n| a |\n| b |\n"
  1598. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1599. (org-table-sort-lines t ?a)
  1600. (buffer-string))))
  1601. (should
  1602. (equal "| b |\n| a |\n| C |\n"
  1603. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1604. (org-table-sort-lines nil ?A)
  1605. (buffer-string))))
  1606. ;; Sort by time (timestamps)
  1607. (should
  1608. (equal
  1609. "| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n| <2014-03-04 tue.> |\n"
  1610. (org-test-with-temp-text
  1611. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1612. (org-table-sort-lines nil ?t)
  1613. (buffer-string))))
  1614. (should
  1615. (equal
  1616. "| <2014-03-04 tue.> |\n| <2012-03-29 thu.> |\n| <2008-08-08 sat.> |\n"
  1617. (org-test-with-temp-text
  1618. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1619. (org-table-sort-lines nil ?T)
  1620. (buffer-string))))
  1621. ;; Sort by time (HH:MM values)
  1622. (should
  1623. (equal "| 1:00 |\n| 17:00 |\n| 114:00 |\n"
  1624. (org-test-with-temp-text "| 114:00 |\n| 17:00 |\n| 1:00 |\n"
  1625. (org-table-sort-lines nil ?t)
  1626. (buffer-string))))
  1627. (should
  1628. (equal "| 114:00 |\n| 17:00 |\n| 1: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. ;; Sort by time (durations)
  1633. (should
  1634. (equal "| 1d 3:00 |\n| 28:00 |\n"
  1635. (org-test-with-temp-text "| 28:00 |\n| 1d 3:00 |\n"
  1636. (org-table-sort-lines nil ?t)
  1637. (buffer-string))))
  1638. ;; Sort with custom functions.
  1639. (should
  1640. (equal "| 22 |\n| 15 |\n| 18 |\n"
  1641. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1642. (org-table-sort-lines nil ?f
  1643. (lambda (s) (% (string-to-number s) 10))
  1644. #'<)
  1645. (buffer-string))))
  1646. (should
  1647. (equal "| 18 |\n| 15 |\n| 22 |\n"
  1648. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1649. (org-table-sort-lines nil ?F
  1650. (lambda (s) (% (string-to-number s) 10))
  1651. #'<)
  1652. (buffer-string))))
  1653. ;; Sort according to current column.
  1654. (should
  1655. (equal "| 1 | 2 |\n| 7 | 3 |\n| 5 | 4 |\n"
  1656. (org-test-with-temp-text "| 1 | <point>2 |\n| 5 | 4 |\n| 7 | 3 |\n"
  1657. (org-table-sort-lines nil ?n)
  1658. (buffer-string))))
  1659. ;; Sort between horizontal separators if possible.
  1660. (should
  1661. (equal
  1662. "| 9 | 8 |\n|---+---|\n| 5 | 3 |\n| 7 | 4 |\n|---+---|\n| 1 | 2 |\n"
  1663. (org-test-with-temp-text
  1664. "| 9 | 8 |\n|---+---|\n| <point>7 | 4 |\n| 5 | 3 |\n|---+---|\n| 1 | 2 |\n"
  1665. (org-table-sort-lines nil ?n)
  1666. (buffer-string)))))
  1667. ;;; Formulas
  1668. (ert-deftest test-org-table/eval-formula ()
  1669. "Test `org-table-eval-formula' specifications."
  1670. ;; Error when not on a table field.
  1671. (should-error
  1672. (org-test-with-temp-text "Text"
  1673. (org-table-eval-formula)))
  1674. (should-error
  1675. (org-test-with-temp-text "| a |\n|---|<point>"
  1676. (org-table-eval-formula)))
  1677. (should-error
  1678. (org-test-with-temp-text "| a |\n#+TBLFM:<point>"
  1679. (org-table-eval-formula)))
  1680. ;; Handle @<, @>, $< and $>.
  1681. (should
  1682. (equal "| 1 |\n| 1 |"
  1683. (org-test-with-temp-text "| <point> |\n| 1 |"
  1684. (org-table-eval-formula nil "@>" nil nil t)
  1685. (buffer-string))))
  1686. (should
  1687. (equal "| 1 |\n| 1 |"
  1688. (org-test-with-temp-text "| 1 |\n| <point> |"
  1689. (org-table-eval-formula nil "@<" nil nil t)
  1690. (buffer-string))))
  1691. (should
  1692. (equal "| 1 | 1 |"
  1693. (org-test-with-temp-text "| <point> | 1 |"
  1694. (org-table-eval-formula nil "$>" nil nil t)
  1695. (buffer-string))))
  1696. (should
  1697. (equal "| 1 | 1 |"
  1698. (org-test-with-temp-text "| 1 | <point> |"
  1699. (org-table-eval-formula nil "$<" nil nil t)
  1700. (buffer-string)))))
  1701. (ert-deftest test-org-table/field-formula-outside-table ()
  1702. "Test `org-table-formula-create-columns' variable."
  1703. ;; Refuse to create column if variable is nil.
  1704. (should-error
  1705. (org-test-with-temp-text "
  1706. | 2 |
  1707. | 4 |
  1708. | 8 |
  1709. <point>#+TBLFM: @1$2=5"
  1710. (let ((org-table-formula-create-columns nil))
  1711. (org-table-calc-current-TBLFM))
  1712. (buffer-string))
  1713. :type (list 'error 'user-error))
  1714. ;; If the variable is non-nil, field formulas and columns formulas
  1715. ;; can create tables.
  1716. (should
  1717. (equal
  1718. "
  1719. | 2 | 5 |
  1720. | 4 | |
  1721. | 8 | |
  1722. #+TBLFM: @1$2=5"
  1723. (org-test-with-temp-text "
  1724. | 2 |
  1725. | 4 |
  1726. | 8 |
  1727. <point>#+TBLFM: @1$2=5"
  1728. (let ((org-table-formula-create-columns t))
  1729. (org-table-calc-current-TBLFM))
  1730. (buffer-string))))
  1731. (should
  1732. (equal
  1733. "
  1734. | 2 | | 15 |
  1735. | 4 | | 15 |
  1736. | 8 | | 15 |
  1737. #+TBLFM: $3=15"
  1738. (org-test-with-temp-text "
  1739. | 2 |
  1740. | 4 |
  1741. | 8 |
  1742. <point>#+TBLFM: $3=15"
  1743. (let ((org-table-formula-create-columns t))
  1744. (org-table-calc-current-TBLFM))
  1745. (buffer-string)))))
  1746. (ert-deftest test-org-table/duration ()
  1747. "Test durations in table formulas."
  1748. ;; Durations in cells.
  1749. (should
  1750. (string-match "| 2:12 | 1:47 | 03:59:00 |"
  1751. (org-test-with-temp-text "
  1752. | 2:12 | 1:47 | |
  1753. <point>#+TBLFM: @1$3=$1+$2;T"
  1754. (org-table-calc-current-TBLFM)
  1755. (buffer-string))))
  1756. (should
  1757. (string-match "| 2:12 | 1:47 | 03:59 |"
  1758. (org-test-with-temp-text "
  1759. | 2:12 | 1:47 | |
  1760. <point>#+TBLFM: @1$3=$1+$2;U"
  1761. (org-table-calc-current-TBLFM)
  1762. (buffer-string))))
  1763. (should
  1764. (string-match "| 3:02:20 | -2:07:00 | 0.92 |"
  1765. (org-test-with-temp-text "
  1766. | 3:02:20 | -2:07:00 | |
  1767. <point>#+TBLFM: @1$3=$1+$2;t"
  1768. (org-table-calc-current-TBLFM)
  1769. (buffer-string))))
  1770. ;; Durations set through properties.
  1771. (should
  1772. (string-match "| 16:00:00 |"
  1773. (org-test-with-temp-text "* H
  1774. :PROPERTIES:
  1775. :time_constant: 08:00:00
  1776. :END:
  1777. | |
  1778. <point>#+TBLFM: $1=2*$PROP_time_constant;T"
  1779. (org-table-calc-current-TBLFM)
  1780. (buffer-string))))
  1781. (should
  1782. (string-match "| 16.00 |"
  1783. (org-test-with-temp-text "* H
  1784. :PROPERTIES:
  1785. :time_constant: 08:00:00
  1786. :END:
  1787. | |
  1788. <point>#+TBLFM: $1=2*$PROP_time_constant;t"
  1789. (org-table-calc-current-TBLFM)
  1790. (buffer-string)))))
  1791. (ert-deftest test-org-table/end-on-hline ()
  1792. "Test with a table ending on a hline."
  1793. (should
  1794. (equal
  1795. (org-test-with-temp-text
  1796. "
  1797. | 1 | 2 | 3 |
  1798. | 4 | 5 | 6 |
  1799. | | | |
  1800. |---+---+---|
  1801. <point>#+TBLFM: @3$2..@3$>=vsum(@1..@2)"
  1802. (org-table-calc-current-TBLFM)
  1803. (buffer-string))
  1804. "
  1805. | 1 | 2 | 3 |
  1806. | 4 | 5 | 6 |
  1807. | | 7 | 9 |
  1808. |---+---+---|
  1809. #+TBLFM: @3$2..@3$>=vsum(@1..@2)")))
  1810. (ert-deftest test-org-table/named-field ()
  1811. "Test formula with a named field."
  1812. (should
  1813. (string-match-p
  1814. "| +| +1 +|"
  1815. (org-test-with-temp-text "
  1816. | | |
  1817. | ^ | name |
  1818. <point>#+TBLFM: $name=1"
  1819. (org-table-calc-current-TBLFM)
  1820. (buffer-string))))
  1821. (should
  1822. (string-match-p
  1823. "| +| +1 +|"
  1824. (org-test-with-temp-text "
  1825. | _ | name |
  1826. | | |
  1827. <point>#+TBLFM: $name=1"
  1828. (org-table-calc-current-TBLFM)
  1829. (buffer-string)))))
  1830. (ert-deftest test-org-table/named-column ()
  1831. "Test formula with a named field."
  1832. (should
  1833. (string-match-p
  1834. "| +| +1 +| +1 +|"
  1835. (org-test-with-temp-text "
  1836. | ! | name | |
  1837. | | 1 | |
  1838. <point>#+TBLFM: @2$3=$name"
  1839. (org-table-calc-current-TBLFM)
  1840. (buffer-string)))))
  1841. (ert-deftest test-org-table/formula-priority ()
  1842. "Test field formula priority over column formula."
  1843. ;; Field formulas bind stronger than column formulas.
  1844. (should
  1845. (equal
  1846. "| 1 | 3 |\n| 2 | 99 |\n"
  1847. (org-test-with-temp-text
  1848. "| 1 | |\n| 2 | |\n<point>#+tblfm: $2=3*$1::@2$2=99"
  1849. (org-table-calc-current-TBLFM)
  1850. (buffer-substring-no-properties (point-min) (point)))))
  1851. ;; When field formula is removed, table formulas is applied again.
  1852. (should
  1853. (equal
  1854. "| 1 | 3 |\n| 2 | 6 |\n"
  1855. (org-test-with-temp-text
  1856. "| 1 | |\n| 2 | |\n#+tblfm: $2=3*$1<point>::@2$2=99"
  1857. (org-table-calc-current-TBLFM)
  1858. (delete-region (point) (line-end-position))
  1859. (org-table-calc-current-TBLFM)
  1860. (buffer-substring-no-properties (point-min) (line-beginning-position))))))
  1861. (ert-deftest test-org-table/tab-indent ()
  1862. "Test named fields with tab indentation."
  1863. (should
  1864. (string-match-p
  1865. "| # | 111 |"
  1866. (org-test-with-temp-text
  1867. "
  1868. | ! | sum | | a | b | c |
  1869. |---+------+------+---+----+-----|
  1870. | # | 1011 | 1000 | 1 | 10 | 100 |
  1871. <point>#+TBLFM: $2=$a+$b+$c
  1872. "
  1873. (org-table-calc-current-TBLFM)
  1874. (buffer-string)))))
  1875. (ert-deftest test-org-table/first-rc ()
  1876. "Test \"$<\" and \"@<\" constructs in formulas."
  1877. (should
  1878. (string-match-p
  1879. "| 1 | 2 |"
  1880. (org-test-with-temp-text
  1881. "| | 2 |
  1882. <point>#+TBLFM: $<=1"
  1883. (org-table-calc-current-TBLFM)
  1884. (buffer-string))))
  1885. (should
  1886. (string-match-p
  1887. "| 2 |\n| 2 |"
  1888. (org-test-with-temp-text
  1889. "| 2 |\n| |
  1890. <point>#+TBLFM: @2$1=@<"
  1891. (org-table-calc-current-TBLFM)
  1892. (buffer-string)))))
  1893. (ert-deftest test-org-table/last-rc ()
  1894. "Test \"$>\" and \"@>\" constructs in formulas."
  1895. (should
  1896. (string-match-p
  1897. "| 2 | 1 |"
  1898. (org-test-with-temp-text
  1899. "| 2 | |\n<point>#+TBLFM: $>=1"
  1900. (org-table-calc-current-TBLFM)
  1901. (buffer-string))))
  1902. (should
  1903. (string-match-p
  1904. "| 2 |\n| 2 |"
  1905. (org-test-with-temp-text
  1906. "| 2 |\n| |\n<point>#+TBLFM: @>$1=@<"
  1907. (org-table-calc-current-TBLFM)
  1908. (buffer-string)))))
  1909. (ert-deftest test-org-table/time-stamps ()
  1910. "Test time-stamps handling."
  1911. ;; Standard test.
  1912. (should
  1913. (string-match-p
  1914. "| 1 |"
  1915. (org-test-with-temp-text
  1916. "| <2016-07-07 Sun> | <2016-07-08 Fri> | |\n<point>#+TBLFM: $3=$2-$1"
  1917. (org-table-calc-current-TBLFM)
  1918. (buffer-string))))
  1919. ;; Handle locale specific time-stamps.
  1920. (should
  1921. (string-match-p
  1922. "| 1 |"
  1923. (org-test-with-temp-text
  1924. "| <2016-07-07 Do> | <2016-07-08 Fr> | |\n<point>#+TBLFM: $3=$2-$1"
  1925. (org-table-calc-current-TBLFM)
  1926. (buffer-string)))))
  1927. (ert-deftest test-org-table/orgtbl-ascii-draw ()
  1928. "Test `orgtbl-ascii-draw'."
  1929. ;; First value: Make sure that an integer input value is converted to a
  1930. ;; float before division. Further values: Show some float input value
  1931. ;; ranges corresponding to the same bar width.
  1932. (should
  1933. (equal
  1934. (org-test-with-temp-text
  1935. "
  1936. | Value | <l> |
  1937. |----------+---------|
  1938. | 19 | replace |
  1939. |----------+---------|
  1940. | -0.50001 | replace |
  1941. | -0.49999 | replace |
  1942. | 0.49999 | replace |
  1943. | 0.50001 | replace |
  1944. | 1.49999 | replace |
  1945. | 22.50001 | replace |
  1946. | 23.49999 | replace |
  1947. | 23.50001 | replace |
  1948. | 24.49999 | replace |
  1949. | 24.50001 | replace |
  1950. <point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"
  1951. (org-table-calc-current-TBLFM)
  1952. (buffer-string))
  1953. "
  1954. | Value | <l> |
  1955. |----------+-----------|
  1956. | 19 | 883 |
  1957. |----------+-----------|
  1958. | -0.50001 | too small |
  1959. | -0.49999 | |
  1960. | 0.49999 | |
  1961. | 0.50001 | 1 |
  1962. | 1.49999 | 1 |
  1963. | 22.50001 | 887 |
  1964. | 23.49999 | 887 |
  1965. | 23.50001 | 888 |
  1966. | 24.49999 | 888 |
  1967. | 24.50001 | too large |
  1968. #+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"))
  1969. ;; Draw bars with a bullet. The bullet does not count in the parameter
  1970. ;; WIDTH of `orgtbl-ascii-draw'.
  1971. (should
  1972. (equal
  1973. (org-test-with-temp-text
  1974. "
  1975. | -1 | replace |
  1976. | 0 | replace |
  1977. | 1 | replace |
  1978. | 2 | replace |
  1979. | 3 | replace |
  1980. | 4 | replace |
  1981. <point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 3 3 \"$-\")"
  1982. (org-table-calc-current-TBLFM)
  1983. (buffer-string))
  1984. "
  1985. | -1 | too small |
  1986. | 0 | $ |
  1987. | 1 | -$ |
  1988. | 2 | --$ |
  1989. | 3 | ---$ |
  1990. | 4 | too large |
  1991. #+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 3 3 \"$-\")")))
  1992. (ert-deftest test-org-table/single-rowgroup ()
  1993. "Test column formula in a table with a single rowgroup."
  1994. (should
  1995. (equal
  1996. "
  1997. |---+---|
  1998. | 1 | 0 |
  1999. |---+---|
  2000. #+TBLFM: $2=$1-1"
  2001. (org-test-with-temp-text "
  2002. |---+---|
  2003. | 1 | |
  2004. |---+---|
  2005. <point>#+TBLFM: $2=$1-1"
  2006. (org-table-calc-current-TBLFM)
  2007. (buffer-string))))
  2008. (should
  2009. (equal
  2010. "
  2011. | 1 | 0 |
  2012. #+TBLFM: $2=$1-1"
  2013. (org-test-with-temp-text "
  2014. | 1 | |
  2015. <point>#+TBLFM: $2=$1-1"
  2016. (org-table-calc-current-TBLFM)
  2017. (buffer-string)))))
  2018. ;;; Navigation
  2019. (ert-deftest test-org-table/next-field ()
  2020. "Test `org-table-next-field' specifications."
  2021. ;; Regular test.
  2022. (should
  2023. (equal
  2024. "b"
  2025. (org-test-with-temp-text "| a<point> | b |"
  2026. (org-table-next-field)
  2027. (org-trim (org-table-get-field)))))
  2028. ;; Create new rows as needed.
  2029. (should
  2030. (equal
  2031. "| a |\n| |\n"
  2032. (org-test-with-temp-text "| a<point> |"
  2033. (org-table-next-field)
  2034. (buffer-string))))
  2035. ;; Jump over hlines, if `org-table-tab-jumps-over-hlines' is
  2036. ;; non-nil.
  2037. (should
  2038. (equal
  2039. "b"
  2040. (org-test-with-temp-text "| a<point> |\n|---|\n| b |"
  2041. (let ((org-table-tab-jumps-over-hlines t)) (org-table-next-field))
  2042. (org-trim (org-table-get-field)))))
  2043. ;; If `org-table-tab-jumps-over-hlines' is nil, however, create
  2044. ;; a new row before the rule.
  2045. (should
  2046. (equal
  2047. "| a |\n| |\n|---|\n| b |"
  2048. (org-test-with-temp-text "| a<point> |\n|---|\n| b |"
  2049. (let ((org-table-tab-jumps-over-hlines nil)) (org-table-next-field))
  2050. (buffer-string)))))
  2051. (ert-deftest test-org-table/previous-field ()
  2052. "Test `org-table-previous-field' specifications."
  2053. ;; Regular tests.
  2054. (should
  2055. (eq ?a
  2056. (org-test-with-temp-text "| a | <point>b |"
  2057. (org-table-previous-field)
  2058. (char-after))))
  2059. (should
  2060. (eq ?a
  2061. (org-test-with-temp-text "| a |\n| <point>b |"
  2062. (org-table-previous-field)
  2063. (char-after))))
  2064. ;; Find previous field across horizontal rules.
  2065. (should
  2066. (eq ?a
  2067. (org-test-with-temp-text "| a |\n|---|\n| <point>b |"
  2068. (org-table-previous-field)
  2069. (char-after))))
  2070. ;; When called on a horizontal rule, find previous data field.
  2071. (should
  2072. (eq ?b
  2073. (org-test-with-temp-text "| a | b |\n|---+-<point>--|"
  2074. (org-table-previous-field)
  2075. (char-after))))
  2076. ;; Error when at first field. Make sure to preserve original
  2077. ;; position.
  2078. (should-error
  2079. (org-test-with-temp-text "| <point> a|"
  2080. (org-table-previous-field)))
  2081. (should-error
  2082. (org-test-with-temp-text "|---|\n| <point>a |"
  2083. (org-table-previous-field)))
  2084. (should
  2085. (eq ?a
  2086. (org-test-with-temp-text "|---|\n| <point>a |"
  2087. (ignore-errors (org-table-previous-field))
  2088. (char-after)))))
  2089. ;;; Inserting rows, inserting columns
  2090. (ert-deftest test-org-table/insert-column ()
  2091. "Test `org-table-insert-column' specifications."
  2092. ;; Error when outside a table.
  2093. (should-error
  2094. (org-test-with-temp-text "Paragraph"
  2095. (org-table-insert-column)))
  2096. ;; Insert new column after current one.
  2097. (should
  2098. (equal "| a | |\n"
  2099. (org-test-with-temp-text "| a |"
  2100. (org-table-insert-column)
  2101. (buffer-string))))
  2102. (should
  2103. (equal "| a | | b |\n"
  2104. (org-test-with-temp-text "| <point>a | b |"
  2105. (org-table-insert-column)
  2106. (buffer-string))))
  2107. ;; Move point into the newly created column.
  2108. (should
  2109. (equal " |"
  2110. (org-test-with-temp-text "| <point>a |"
  2111. (org-table-insert-column)
  2112. (buffer-substring-no-properties (point) (line-end-position)))))
  2113. (should
  2114. (equal " | b |"
  2115. (org-test-with-temp-text "| <point>a | b |"
  2116. (org-table-insert-column)
  2117. (buffer-substring-no-properties (point) (line-end-position)))))
  2118. ;; Handle missing vertical bar in the last column.
  2119. (should
  2120. (equal "| a | |\n"
  2121. (org-test-with-temp-text "| a"
  2122. (org-table-insert-column)
  2123. (buffer-string))))
  2124. (should
  2125. (equal " |"
  2126. (org-test-with-temp-text "| <point>a"
  2127. (org-table-insert-column)
  2128. (buffer-substring-no-properties (point) (line-end-position)))))
  2129. ;; Handle column insertion when point is before first column.
  2130. (should
  2131. (equal " | a | |\n"
  2132. (org-test-with-temp-text " | a |"
  2133. (org-table-insert-column)
  2134. (buffer-string))))
  2135. (should
  2136. (equal " | a | | b |\n"
  2137. (org-test-with-temp-text " | a | b |"
  2138. (org-table-insert-column)
  2139. (buffer-string)))))
  2140. ;;; Moving rows, moving columns
  2141. (ert-deftest test-org-table/move-row-down ()
  2142. "Test `org-table-move-row-down' specifications."
  2143. ;; Error out when row cannot be moved, e.g., it is the last row in
  2144. ;; the table.
  2145. (should-error
  2146. (org-test-with-temp-text "| a |"
  2147. (org-table-move-row-down)))
  2148. (should-error
  2149. (org-test-with-temp-text "| a |\n"
  2150. (org-table-move-row-down)))
  2151. (should-error
  2152. (org-test-with-temp-text "| a |\n| <point>b |"
  2153. (org-table-move-row-down)))
  2154. ;; Move data lines.
  2155. (should
  2156. (equal "| b |\n| a |\n"
  2157. (org-test-with-temp-text "| a |\n| b |\n"
  2158. (org-table-move-row-down)
  2159. (buffer-string))))
  2160. (should
  2161. (equal "|---|\n| a |\n"
  2162. (org-test-with-temp-text "| a |\n|---|\n"
  2163. (org-table-move-row-down)
  2164. (buffer-string))))
  2165. ;; Move hlines.
  2166. (should
  2167. (equal "| b |\n|---|\n"
  2168. (org-test-with-temp-text "|---|\n| b |\n"
  2169. (org-table-move-row-down)
  2170. (buffer-string))))
  2171. (should
  2172. (equal "|---|\n|---|\n"
  2173. (org-test-with-temp-text "|---|\n|---|\n"
  2174. (org-table-move-row-down)
  2175. (buffer-string))))
  2176. ;; Move rows even without a final newline.
  2177. (should
  2178. (equal "| b |\n| a |\n"
  2179. (org-test-with-temp-text "| a |\n| b |"
  2180. (org-table-move-row-down)
  2181. (buffer-string)))))
  2182. (ert-deftest test-org-table/move-row-up ()
  2183. "Test `org-table-move-row-up' specifications."
  2184. ;; Error out when row cannot be moved, e.g., it is the first row in
  2185. ;; the table.
  2186. (should-error
  2187. (org-test-with-temp-text "| a |"
  2188. (org-table-move-row-up)))
  2189. (should-error
  2190. (org-test-with-temp-text "| a |\n"
  2191. (org-table-move-row-up)))
  2192. ;; Move data lines.
  2193. (should
  2194. (equal "| b |\n| a |\n"
  2195. (org-test-with-temp-text "| a |\n| <point>b |\n"
  2196. (org-table-move-row-up)
  2197. (buffer-string))))
  2198. (should
  2199. (equal "| b |\n|---|\n"
  2200. (org-test-with-temp-text "|---|\n| <point>b |\n"
  2201. (org-table-move-row-up)
  2202. (buffer-string))))
  2203. ;; Move hlines.
  2204. (should
  2205. (equal "|---|\n| a |\n"
  2206. (org-test-with-temp-text "| a |\n|<point>---|\n"
  2207. (org-table-move-row-up)
  2208. (buffer-string))))
  2209. (should
  2210. (equal "|---|\n|---|\n"
  2211. (org-test-with-temp-text "|---|\n|<point>---|\n"
  2212. (org-table-move-row-up)
  2213. (buffer-string))))
  2214. ;; Move rows even without a final newline.
  2215. (should
  2216. (equal "| b |\n| a |\n"
  2217. (org-test-with-temp-text "| a |\n| <point>b |"
  2218. (org-table-move-row-up)
  2219. (buffer-string)))))
  2220. ;;; Shrunk columns
  2221. (ert-deftest test-org-table/toggle-column-width ()
  2222. "Test `org-table-toggle-columns-width' specifications."
  2223. ;; Error when not at a column.
  2224. (should-error
  2225. (org-test-with-temp-text "<point>a"
  2226. (org-table-toggle-column-width)))
  2227. ;; A shrunk columns is overlaid with
  2228. ;; `org-table-shrunk-column-indicator'.
  2229. (should
  2230. (equal org-table-shrunk-column-indicator
  2231. (org-test-with-temp-text "| <point>a |"
  2232. (org-table-toggle-column-width)
  2233. (overlay-get (car (overlays-at (point))) 'display))))
  2234. (should
  2235. (equal org-table-shrunk-column-indicator
  2236. (org-test-with-temp-text "| a |\n|-<point>--|"
  2237. (org-table-toggle-column-width)
  2238. (overlay-get (car (overlays-at (point))) 'display))))
  2239. ;; Shrink every field in the same column.
  2240. (should
  2241. (equal org-table-shrunk-column-indicator
  2242. (org-test-with-temp-text "| a |\n|-<point>--|"
  2243. (org-table-toggle-column-width)
  2244. (overlay-get (car (overlays-at (1+ (line-beginning-position 0))))
  2245. 'display))))
  2246. ;; When column is already shrunk, expand it, i.e., remove overlays.
  2247. (should-not
  2248. (equal org-table-shrunk-column-indicator
  2249. (org-test-with-temp-text "| <point>a |"
  2250. (org-table-toggle-column-width)
  2251. (org-table-toggle-column-width)
  2252. (overlays-in (point-min) (point-max)))))
  2253. (should-not
  2254. (equal org-table-shrunk-column-indicator
  2255. (org-test-with-temp-text "| a |\n| <point>b |"
  2256. (org-table-toggle-column-width)
  2257. (org-table-toggle-column-width)
  2258. (overlays-in (point-min) (point-max)))))
  2259. ;; With a column width cookie, limit overlay to the specified number
  2260. ;; of characters.
  2261. (should
  2262. (equal "| ab"
  2263. (org-test-with-temp-text "| <3> |\n| <point>abcd |"
  2264. (org-table-toggle-column-width)
  2265. (buffer-substring (line-beginning-position)
  2266. (overlay-start
  2267. (car (overlays-in (line-beginning-position)
  2268. (line-end-position))))))))
  2269. (should
  2270. (equal "| a "
  2271. (org-test-with-temp-text "| <3> |\n| <point>a |"
  2272. (org-table-toggle-column-width)
  2273. (buffer-substring (line-beginning-position)
  2274. (overlay-start
  2275. (car (overlays-in (line-beginning-position)
  2276. (line-end-position))))))))
  2277. ;; Width only takes into account visible characters.
  2278. (should
  2279. (equal "| [[htt"
  2280. (org-test-with-temp-text "| <4> |\n| <point>[[http://orgmode.org]] |"
  2281. (org-table-toggle-column-width)
  2282. (buffer-substring (line-beginning-position)
  2283. (overlay-start
  2284. (car (overlays-in (line-beginning-position)
  2285. (line-end-position))))))))
  2286. ;; Before the first column or after the last one, ask for columns
  2287. ;; ranges.
  2288. (should
  2289. (catch :exit
  2290. (org-test-with-temp-text "| a |"
  2291. (cl-letf (((symbol-function 'read-string)
  2292. (lambda (&rest_) (throw :exit t))))
  2293. (org-table-toggle-column-width)
  2294. nil))))
  2295. (should
  2296. (catch :exit
  2297. (org-test-with-temp-text "| a |<point>"
  2298. (cl-letf (((symbol-function 'read-string)
  2299. (lambda (&rest_) (throw :exit t))))
  2300. (org-table-toggle-column-width)
  2301. nil))))
  2302. ;; When optional argument ARG is a string, toggle specified columns.
  2303. (should
  2304. (equal org-table-shrunk-column-indicator
  2305. (org-test-with-temp-text "| <point>a | b |"
  2306. (org-table-toggle-column-width "2")
  2307. (overlay-get (car (overlays-at (- (point-max) 2))) 'display))))
  2308. (should
  2309. (equal '("b" "c")
  2310. (org-test-with-temp-text "| a | b | c | d |"
  2311. (org-table-toggle-column-width "2-3")
  2312. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2313. (overlays-in (point-min) (point-max)))
  2314. #'string-lessp))))
  2315. (should
  2316. (equal '("b" "c" "d")
  2317. (org-test-with-temp-text "| a | b | c | d |"
  2318. (org-table-toggle-column-width "2-")
  2319. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2320. (overlays-in (point-min) (point-max)))
  2321. #'string-lessp))))
  2322. (should
  2323. (equal '("a" "b")
  2324. (org-test-with-temp-text "| a | b | c | d |"
  2325. (org-table-toggle-column-width "-2")
  2326. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2327. (overlays-in (point-min) (point-max)))
  2328. #'string-lessp))))
  2329. (should
  2330. (equal '("a" "b" "c" "d")
  2331. (org-test-with-temp-text "| a | b | c | d |"
  2332. (org-table-toggle-column-width "-")
  2333. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2334. (overlays-in (point-min) (point-max)))
  2335. #'string-lessp))))
  2336. (should
  2337. (equal '("a" "d")
  2338. (org-test-with-temp-text "| a | b | c | d |"
  2339. (org-table-toggle-column-width "1-3")
  2340. (org-table-toggle-column-width "2-4")
  2341. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2342. (overlays-in (point-min) (point-max)))
  2343. #'string-lessp))))
  2344. ;; When ARG is (16), remove any column overlay.
  2345. (should-not
  2346. (org-test-with-temp-text "| <point>a |"
  2347. (org-table-toggle-column-width)
  2348. (org-table-toggle-column-width '(16))
  2349. (overlays-in (point-min) (point-max))))
  2350. (should-not
  2351. (org-test-with-temp-text "| a | b | c | d |"
  2352. (org-table-toggle-column-width "-")
  2353. (org-table-toggle-column-width '(16))
  2354. (overlays-in (point-min) (point-max)))))
  2355. (ert-deftest test-org-table/shrunk-columns ()
  2356. "Test behaviour of shrunk column."
  2357. ;; Edition automatically expands a shrunk column.
  2358. (should-not
  2359. (org-test-with-temp-text "| <point>a |"
  2360. (org-table-toggle-column-width)
  2361. (insert "a")
  2362. (overlays-in (point-min) (point-max))))
  2363. ;; Other columns are not changed.
  2364. (should
  2365. (org-test-with-temp-text "| <point>a | b |"
  2366. (org-table-toggle-column-width "-")
  2367. (insert "a")
  2368. (overlays-in (point-min) (point-max))))
  2369. ;; Moving a shrunk column doesn't alter its state.
  2370. (should
  2371. (equal "a"
  2372. (org-test-with-temp-text "| <point>a | b |"
  2373. (org-table-toggle-column-width)
  2374. (org-table-move-column-right)
  2375. (overlay-get (car (overlays-at (point))) 'help-echo))))
  2376. (should
  2377. (equal "a"
  2378. (org-test-with-temp-text "| <point>a |\n| b |"
  2379. (org-table-toggle-column-width)
  2380. (org-table-move-row-down)
  2381. (overlay-get (car (overlays-at (point))) 'help-echo))))
  2382. ;; State is preserved upon inserting a column.
  2383. (should
  2384. (equal '("a")
  2385. (org-test-with-temp-text "| <point>a |"
  2386. (org-table-toggle-column-width)
  2387. (org-table-insert-column)
  2388. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2389. (overlays-in (point-min) (point-max)))
  2390. #'string-lessp))))
  2391. ;; State is preserved upon deleting a column.
  2392. (should
  2393. (equal '("a" "c")
  2394. (org-test-with-temp-text "| a | <point>b | c |"
  2395. (org-table-toggle-column-width "-")
  2396. (org-table-delete-column)
  2397. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2398. (overlays-in (point-min) (point-max)))
  2399. #'string-lessp))))
  2400. ;; State is preserved upon deleting a row.
  2401. (should
  2402. (equal '("b1" "b2")
  2403. (org-test-with-temp-text "| a1 | a2 |\n| b1 | b2 |"
  2404. (org-table-toggle-column-width "-")
  2405. (org-table-kill-row)
  2406. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2407. (overlays-in (point-min) (point-max)))
  2408. #'string-lessp))))
  2409. (should
  2410. (equal '("a1" "a2")
  2411. (org-test-with-temp-text "| a1 | a2 |\n| <point>b1 | b2 |"
  2412. (org-table-toggle-column-width "-")
  2413. (org-table-kill-row)
  2414. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2415. (overlays-in (point-min) (point-max)))
  2416. #'string-lessp))))
  2417. ;; State is preserved upon inserting a row or hline.
  2418. (should
  2419. (equal '("" "a1" "b1")
  2420. (org-test-with-temp-text "| a1 | a2 |\n| <point>b1 | b2 |"
  2421. (org-table-toggle-column-width)
  2422. (org-table-insert-row)
  2423. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2424. (overlays-in (point-min) (point-max)))
  2425. #'string-lessp))))
  2426. (should
  2427. (equal '("a1" "b1")
  2428. (org-test-with-temp-text "| a1 | a2 |\n| <point>b1 | b2 |"
  2429. (org-table-toggle-column-width)
  2430. (org-table-insert-hline)
  2431. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2432. (overlays-in (point-min) (point-max)))
  2433. #'string-lessp))))
  2434. ;; State is preserved upon sorting a column for all the columns but
  2435. ;; the one being sorted.
  2436. (should
  2437. (equal '("a2" "b2")
  2438. (org-test-with-temp-text "| <point>a1 | a2 |\n| <point>b1 | b2 |"
  2439. (org-table-toggle-column-width "-")
  2440. (org-table-sort-lines nil ?A)
  2441. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2442. (overlays-in (point-min) (point-max)))
  2443. #'string-lessp))))
  2444. ;; State is preserved upon replacing a field non-interactively.
  2445. (should
  2446. (equal '("a")
  2447. (org-test-with-temp-text "| <point>a |"
  2448. (org-table-toggle-column-width)
  2449. (org-table-get-field nil "b")
  2450. (mapcar (lambda (o) (overlay-get o 'help-echo))
  2451. (overlays-in (point-min) (point-max))))))
  2452. ;; Moving to next field doesn't change shrunk state.
  2453. (should
  2454. (equal "a"
  2455. (org-test-with-temp-text "| <point>a | b |"
  2456. (org-table-toggle-column-width)
  2457. (org-table-next-field)
  2458. (overlay-get (car (overlays-at (1+ (line-beginning-position))))
  2459. 'help-echo))))
  2460. (should
  2461. (equal "b"
  2462. (org-test-with-temp-text "| a | <point>b |"
  2463. (org-table-toggle-column-width)
  2464. (goto-char 2)
  2465. (org-table-next-field)
  2466. (overlay-get (car (overlays-at (point))) 'help-echo))))
  2467. ;; Aligning table doesn't alter shrunk state.
  2468. (should
  2469. (equal "a"
  2470. (org-test-with-temp-text "| <point>a | b |"
  2471. (org-table-toggle-column-width)
  2472. (org-table-align)
  2473. (overlay-get (car (overlays-at (1+ (line-beginning-position))))
  2474. 'help-echo))))
  2475. (should
  2476. (equal "b"
  2477. (org-test-with-temp-text "|---+-----|\n| a | <point>b |"
  2478. (org-table-toggle-column-width)
  2479. (org-table-align)
  2480. (overlay-get (car (overlays-at (point)))
  2481. 'help-echo))))
  2482. (should
  2483. (equal
  2484. '("b")
  2485. (org-test-with-temp-text "|---+-----|\n| a | <point>b |"
  2486. (org-table-toggle-column-width)
  2487. (org-table-align)
  2488. (mapcar (lambda (o) (overlay-get o 'help-echo))
  2489. (overlays-in (line-beginning-position) (line-end-position)))))))
  2490. ;;; Miscellaneous
  2491. (ert-deftest test-org-table/current-column ()
  2492. "Test `org-table-current-column' specifications."
  2493. (should
  2494. (= 1 (org-test-with-temp-text "| <point>a |"
  2495. (org-table-current-column))))
  2496. (should
  2497. (= 1 (org-test-with-temp-text "|-<point>--|"
  2498. (org-table-current-column))))
  2499. (should
  2500. (= 2 (org-test-with-temp-text "| 1 | <point>2 |"
  2501. (org-table-current-column))))
  2502. (should
  2503. (= 2 (org-test-with-temp-text "|---+-<point>--|"
  2504. (org-table-current-column)))))
  2505. (ert-deftest test-org-table/get-field ()
  2506. "Test `org-table-get-field' specifications."
  2507. ;; Regular test.
  2508. (should
  2509. (equal " a "
  2510. (org-test-with-temp-text "| <point>a |" (org-table-get-field))))
  2511. ;; Get field in open last column.
  2512. (should
  2513. (equal " a "
  2514. (org-test-with-temp-text "| <point>a " (org-table-get-field))))
  2515. ;; Get empty field.
  2516. (should
  2517. (equal ""
  2518. (org-test-with-temp-text "|<point>|" (org-table-get-field))))
  2519. (should
  2520. (equal " "
  2521. (org-test-with-temp-text "| <point>|" (org-table-get-field))))
  2522. ;; Outside the table, return the empty string.
  2523. (should
  2524. (equal ""
  2525. (org-test-with-temp-text "<point>| a |" (org-table-get-field))))
  2526. (should
  2527. (equal ""
  2528. (org-test-with-temp-text "| a |<point>" (org-table-get-field))))
  2529. ;; With optional N argument, select a particular column in current
  2530. ;; row.
  2531. (should
  2532. (equal " 3 "
  2533. (org-test-with-temp-text "| 1 | 2 | 3 |" (org-table-get-field 3))))
  2534. (should
  2535. (equal " 4 "
  2536. (org-test-with-temp-text "| 1 | 2 |\n<point>| 3 | 4 |"
  2537. (org-table-get-field 2))))
  2538. ;; REPLACE optional argument is used to replace selected field.
  2539. (should
  2540. (equal "| foo |"
  2541. (org-test-with-temp-text "| <point>1 |"
  2542. (org-table-get-field nil " foo ")
  2543. (buffer-string))))
  2544. (should
  2545. (equal "| 1 | 2 | foo |"
  2546. (org-test-with-temp-text "| 1 | 2 | 3 |"
  2547. (org-table-get-field 3 " foo ")
  2548. (buffer-string))))
  2549. (should
  2550. (equal " 4 "
  2551. (org-test-with-temp-text "| 1 | 2 |\n<point>| 3 | 4 |"
  2552. (org-table-get-field 2))))
  2553. ;; An empty REPLACE string clears the field.
  2554. (should
  2555. (equal "| |"
  2556. (org-test-with-temp-text "| <point>1 |"
  2557. (org-table-get-field nil "")
  2558. (buffer-string))))
  2559. ;; When using REPLACE still return old value.
  2560. (should
  2561. (equal " 1 "
  2562. (org-test-with-temp-text "| <point>1 |"
  2563. (org-table-get-field nil " foo ")))))
  2564. (provide 'test-org-table)
  2565. ;;; test-org-table.el ends here