test-org-table.el 82 KB

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