test-org-table.el 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  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. (fset 'f 'org-table-make-reference)
  696. ;; For Lisp formula only
  697. (should (equal "0" (f "0" t nil 'literal)))
  698. (should (equal "z" (f "z" t nil 'literal)))
  699. (should (equal "" (f "" t nil 'literal)))
  700. (should (equal "0 1" (f '("0" "1") t nil 'literal)))
  701. (should (equal "z 1" (f '("z" "1") t nil 'literal)))
  702. (should (equal " 1" (f '("" "1") t nil 'literal)))
  703. (should (equal " " (f '("" "" ) t nil 'literal))))
  704. (ert-deftest test-org-table/org-table-make-reference/mode-string-E ()
  705. (fset 'f 'org-table-make-reference)
  706. ;; For Lisp formula
  707. (should (equal "\"0\"" (f "0" t nil t)))
  708. (should (equal "\"z\"" (f "z" t nil t)))
  709. (should (equal "\"\"" (f "" t nil t)))
  710. (should (equal "\"0\" \"1\"" (f '("0" "1") t nil t)))
  711. (should (equal "\"z\" \"1\"" (f '("z" "1") t nil t)))
  712. (should (equal "\"\" \"1\"" (f '("" "1") t nil t)))
  713. (should (equal "\"\" \"\"" (f '("" "" ) t nil t)))
  714. ;; For Calc formula
  715. (should (equal "(0)" (f "0" t nil nil)))
  716. (should (equal "(z)" (f "z" t nil nil)))
  717. (should (equal "nan" (f "" t nil nil)))
  718. (should (equal "[0,1]" (f '("0" "1") t nil nil)))
  719. (should (equal "[z,1]" (f '("z" "1") t nil nil)))
  720. (should (equal "[nan,1]" (f '("" "1") t nil nil)))
  721. (should (equal "[nan,nan]" (f '("" "" ) t nil nil)))
  722. ;; For Calc formula, special numbers
  723. (should (equal "(nan)" (f "nan" t nil nil)))
  724. (should (equal "(uinf)" (f "uinf" t nil nil)))
  725. (should (equal "(-inf)" (f "-inf" t nil nil)))
  726. (should (equal "(inf)" (f "inf" t nil nil)))
  727. (should (equal "[nan,1]" (f '( "nan" "1") t nil nil)))
  728. (should (equal "[uinf,1]" (f '("uinf" "1") t nil nil)))
  729. (should (equal "[-inf,1]" (f '("-inf" "1") t nil nil)))
  730. (should (equal "[inf,1]" (f '( "inf" "1") t nil nil))))
  731. (ert-deftest test-org-table/org-table-make-reference/mode-string-EN ()
  732. (fset 'f 'org-table-make-reference)
  733. ;; For Lisp formula
  734. (should (equal "0" (f "0" t t t)))
  735. (should (equal "0" (f "z" t t t)))
  736. (should (equal "0" (f "" t t t)))
  737. (should (equal "0 1" (f '("0" "1") t t t)))
  738. (should (equal "0 1" (f '("z" "1") t t t)))
  739. (should (equal "0 1" (f '("" "1") t t t)))
  740. (should (equal "0 0" (f '("" "" ) t t t)))
  741. ;; For Calc formula
  742. (should (equal "(0)" (f "0" t t nil)))
  743. (should (equal "(0)" (f "z" t t nil)))
  744. (should (equal "(0)" (f "" t t nil)))
  745. (should (equal "[0,1]" (f '("0" "1") t t nil)))
  746. (should (equal "[0,1]" (f '("z" "1") t t nil)))
  747. (should (equal "[0,1]" (f '("" "1") t t nil)))
  748. (should (equal "[0,0]" (f '("" "" ) t t nil)))
  749. ;; For Calc formula, special numbers
  750. (should (equal "(0)" (f "nan" t t nil)))
  751. (should (equal "(0)" (f "uinf" t t nil)))
  752. (should (equal "(0)" (f "-inf" t t nil)))
  753. (should (equal "(0)" (f "inf" t t nil)))
  754. (should (equal "[0,1]" (f '( "nan" "1") t t nil)))
  755. (should (equal "[0,1]" (f '("uinf" "1") t t nil)))
  756. (should (equal "[0,1]" (f '("-inf" "1") t t nil)))
  757. (should (equal "[0,1]" (f '( "inf" "1") t t nil))))
  758. (ert-deftest test-org-table/org-table-make-reference/mode-string-L ()
  759. (fset 'f 'org-table-make-reference)
  760. ;; For Lisp formula only
  761. (should (equal "0" (f "0" nil nil 'literal)))
  762. (should (equal "z" (f "z" nil nil 'literal)))
  763. (should (equal "" (f "" nil nil 'literal)))
  764. (should (equal "0 1" (f '("0" "1") nil nil 'literal)))
  765. (should (equal "z 1" (f '("z" "1") nil nil 'literal)))
  766. (should (equal "1" (f '("" "1") nil nil 'literal)))
  767. (should (equal "" (f '("" "" ) nil nil 'literal))))
  768. (ert-deftest test-org-table/org-table-make-reference/mode-string-none ()
  769. (fset 'f 'org-table-make-reference)
  770. ;; For Lisp formula
  771. (should (equal "\"0\"" (f "0" nil nil t)))
  772. (should (equal "\"z\"" (f "z" nil nil t)))
  773. (should (equal "" (f "" nil nil t)))
  774. (should (equal "\"0\" \"1\"" (f '("0" "1") nil nil t)))
  775. (should (equal "\"z\" \"1\"" (f '("z" "1") nil nil t)))
  776. (should (equal "\"1\"" (f '("" "1") nil nil t)))
  777. (should (equal "" (f '("" "" ) nil nil t)))
  778. ;; For Calc formula
  779. (should (equal "(0)" (f "0" nil nil nil)))
  780. (should (equal "(z)" (f "z" nil nil nil)))
  781. (should (equal "(0)" (f "" nil nil nil)))
  782. (should (equal "[0,1]" (f '("0" "1") nil nil nil)))
  783. (should (equal "[z,1]" (f '("z" "1") nil nil nil)))
  784. (should (equal "[1]" (f '("" "1") nil nil nil)))
  785. (should (equal "[]" (f '("" "" ) nil nil nil)))
  786. ;; For Calc formula, special numbers
  787. (should (equal "(nan)" (f "nan" nil nil nil)))
  788. (should (equal "(uinf)" (f "uinf" nil nil nil)))
  789. (should (equal "(-inf)" (f "-inf" nil nil nil)))
  790. (should (equal "(inf)" (f "inf" nil nil nil)))
  791. (should (equal "[nan,1]" (f '( "nan" "1") nil nil nil)))
  792. (should (equal "[uinf,1]" (f '("uinf" "1") nil nil nil)))
  793. (should (equal "[-inf,1]" (f '("-inf" "1") nil nil nil)))
  794. (should (equal "[inf,1]" (f '( "inf" "1") nil nil nil))))
  795. (ert-deftest test-org-table/org-table-make-reference/mode-string-N ()
  796. (fset 'f 'org-table-make-reference)
  797. ;; For Lisp formula
  798. (should (equal "0" (f "0" nil t t)))
  799. (should (equal "0" (f "z" nil t t)))
  800. (should (equal "" (f "" nil t t)))
  801. (should (equal "0 1" (f '("0" "1") nil t t)))
  802. (should (equal "0 1" (f '("z" "1") nil t t)))
  803. (should (equal "1" (f '("" "1") nil t t)))
  804. (should (equal "" (f '("" "" ) nil t t)))
  805. ;; For Calc formula
  806. (should (equal "(0)" (f "0" nil t nil)))
  807. (should (equal "(0)" (f "z" nil t nil)))
  808. (should (equal "(0)" (f "" nil t nil)))
  809. (should (equal "[0,1]" (f '("0" "1") nil t nil)))
  810. (should (equal "[0,1]" (f '("z" "1") nil t nil)))
  811. (should (equal "[1]" (f '("" "1") nil t nil)))
  812. (should (equal "[]" (f '("" "" ) nil t nil)))
  813. ;; For Calc formula, special numbers
  814. (should (equal "(0)" (f "nan" nil t nil)))
  815. (should (equal "(0)" (f "uinf" nil t nil)))
  816. (should (equal "(0)" (f "-inf" nil t nil)))
  817. (should (equal "(0)" (f "inf" nil t nil)))
  818. (should (equal "[0,1]" (f '( "nan" "1") nil t nil)))
  819. (should (equal "[0,1]" (f '("uinf" "1") nil t nil)))
  820. (should (equal "[0,1]" (f '("-inf" "1") nil t nil)))
  821. (should (equal "[0,1]" (f '( "inf" "1") nil t nil))))
  822. (ert-deftest test-org-table/org-table-convert-refs-to-an/1 ()
  823. "Simple reference @2$1."
  824. (should
  825. (string= "A2" (org-table-convert-refs-to-an "@2$1"))))
  826. ;; TODO: Test broken
  827. ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
  828. ;; "Self reference @1$1."
  829. ;; (should
  830. ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
  831. (ert-deftest test-org-table/org-table-convert-refs-to-an/3 ()
  832. "Remote reference."
  833. (should
  834. (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
  835. (ert-deftest test-org-table/org-table-convert-refs-to-rc/1 ()
  836. "Simple reference @2$1."
  837. (should
  838. (string= "@2$1" (org-table-convert-refs-to-rc "A2"))))
  839. (ert-deftest test-org-table/org-table-convert-refs-to-rc/2 ()
  840. "Self reference $0."
  841. (should
  842. (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
  843. ;; TODO: Test Broken
  844. ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
  845. ;; "Remote reference."
  846. ;; (should
  847. ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
  848. (ert-deftest test-org-table/remote-reference-access ()
  849. "Access to remote reference.
  850. See also `test-org-table/copy-field'."
  851. (org-test-table-target-expect
  852. "
  853. #+NAME: table
  854. | | x 42 | |
  855. | replace | replace |
  856. "
  857. "
  858. #+NAME: table
  859. | | x 42 | |
  860. | x 42 | 84 x |
  861. "
  862. 1 (concat "#+TBLFM: "
  863. ;; Copy text without calculation: Use Lisp formula
  864. "$1 = '(identity remote(table, @1$2)) :: "
  865. ;; Do a calculation: Use Calc (or Lisp ) formula
  866. "$2 = 2 * remote(table, @1$2)")))
  867. (ert-deftest test-org-table/remote-reference-indirect ()
  868. "Access to remote reference with indirection of name or ID."
  869. (let ((source-tables "
  870. #+NAME: 2012
  871. | amount |
  872. |--------|
  873. | 1 |
  874. | 2 |
  875. |--------|
  876. | 3 |
  877. #+TBLFM: @>$1 = vsum(@I..@II)
  878. #+NAME: 2013
  879. | amount |
  880. |--------|
  881. | 4 |
  882. | 8 |
  883. |--------|
  884. | 12 |
  885. #+TBLFM: @>$1 = vsum(@I..@II)
  886. "))
  887. ;; Read several remote references from same column
  888. (org-test-table-target-expect
  889. (concat source-tables "
  890. #+NAME: summary
  891. | year | amount |
  892. |-------+---------|
  893. | 2012 | replace |
  894. | 2013 | replace |
  895. |-------+---------|
  896. | total | replace |
  897. ")
  898. (concat source-tables "
  899. #+NAME: summary
  900. | year | amount |
  901. |-------+--------|
  902. | 2012 | 3 |
  903. | 2013 | 12 |
  904. |-------+--------|
  905. | total | 15 |
  906. ")
  907. 1
  908. ;; Calc formula
  909. "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
  910. ;; Lisp formula
  911. (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
  912. "@>$2 = '(+ @I..@II); N"))
  913. ;; Read several remote references from same row
  914. (org-test-table-target-expect
  915. (concat source-tables "
  916. #+NAME: summary
  917. | year | 2012 | 2013 | total |
  918. |--------+---------+---------+---------|
  919. | amount | replace | replace | replace |
  920. ")
  921. (concat source-tables "
  922. #+NAME: summary
  923. | year | 2012 | 2013 | total |
  924. |--------+------+------+-------|
  925. | amount | 3 | 12 | 15 |
  926. ")
  927. 1
  928. ;; Calc formula
  929. "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
  930. ;; Lisp formula
  931. (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
  932. "@2$> = '(+ $<<..$>>); N"))))
  933. (ert-deftest test-org-table/org-at-TBLFM-p ()
  934. (org-test-with-temp-text-in-file
  935. "
  936. | 1 |
  937. | 2 |
  938. #+TBLFM: $2=$1*2
  939. "
  940. (goto-char (point-min))
  941. (forward-line 2)
  942. (should (equal (org-at-TBLFM-p) nil))
  943. (goto-char (point-min))
  944. (forward-line 3)
  945. (should (equal (org-at-TBLFM-p) t))
  946. (goto-char (point-min))
  947. (forward-line 4)
  948. (should (equal (org-at-TBLFM-p) nil))))
  949. (ert-deftest test-org-table/org-table-TBLFM-begin ()
  950. (org-test-with-temp-text-in-file
  951. "
  952. | 1 |
  953. | 2 |
  954. #+TBLFM: $2=$1*2
  955. "
  956. (goto-char (point-min))
  957. (should (equal (org-table-TBLFM-begin)
  958. nil))
  959. (goto-char (point-min))
  960. (forward-line 1)
  961. (should (equal (org-table-TBLFM-begin)
  962. nil))
  963. (goto-char (point-min))
  964. (forward-line 3)
  965. (should (= (org-table-TBLFM-begin)
  966. 14))
  967. (goto-char (point-min))
  968. (forward-line 4)
  969. (should (= (org-table-TBLFM-begin)
  970. 14))
  971. ))
  972. (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
  973. "For multiple #+TBLFM lines."
  974. (org-test-with-temp-text-in-file
  975. "
  976. | 1 |
  977. | 2 |
  978. #+TBLFM: $2=$1*1
  979. #+TBLFM: $2=$1*2
  980. "
  981. (goto-char (point-min))
  982. (should (equal (org-table-TBLFM-begin)
  983. nil))
  984. (goto-char (point-min))
  985. (forward-line 1)
  986. (should (equal (org-table-TBLFM-begin)
  987. nil))
  988. (goto-char (point-min))
  989. (forward-line 3)
  990. (should (= (org-table-TBLFM-begin)
  991. 14))
  992. (goto-char (point-min))
  993. (forward-line 4)
  994. (should (= (org-table-TBLFM-begin)
  995. 14))
  996. (goto-char (point-min))
  997. (forward-line 5)
  998. (should (= (org-table-TBLFM-begin)
  999. 14))
  1000. ))
  1001. (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
  1002. (org-test-with-temp-text-in-file
  1003. "
  1004. | 1 |
  1005. | 2 |
  1006. #+TBLFM: $2=$1*1
  1007. #+TBLFM: $2=$1*2
  1008. | 6 |
  1009. | 7 |
  1010. #+TBLFM: $2=$1*1
  1011. #+TBLFM: $2=$1*2
  1012. "
  1013. (goto-char (point-min))
  1014. (should (equal (org-table-TBLFM-begin)
  1015. nil))
  1016. (goto-char (point-min))
  1017. (forward-line 1)
  1018. (should (equal (org-table-TBLFM-begin)
  1019. nil))
  1020. (goto-char (point-min))
  1021. (forward-line 3)
  1022. (should (= (org-table-TBLFM-begin)
  1023. 14))
  1024. (goto-char (point-min))
  1025. (forward-line 4)
  1026. (should (= (org-table-TBLFM-begin)
  1027. 14))
  1028. (goto-char (point-min))
  1029. (forward-line 5)
  1030. (should (= (org-table-TBLFM-begin)
  1031. 14))
  1032. (goto-char (point-min))
  1033. (forward-line 6)
  1034. (should (= (org-table-TBLFM-begin)
  1035. 14))
  1036. (goto-char (point-min))
  1037. (forward-line 8)
  1038. (should (= (org-table-TBLFM-begin)
  1039. 61))
  1040. (goto-char (point-min))
  1041. (forward-line 9)
  1042. (should (= (org-table-TBLFM-begin)
  1043. 61))
  1044. (goto-char (point-min))
  1045. (forward-line 10)
  1046. (should (= (org-table-TBLFM-begin)
  1047. 61))))
  1048. (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
  1049. (org-test-with-temp-text-in-file
  1050. "
  1051. | 1 | |
  1052. | 2 | |
  1053. #+TBLFM: $2=$1*1
  1054. #+TBLFM: $2=$1*2
  1055. #+TBLFM: $2=$1*3
  1056. "
  1057. (let ((got (progn (goto-char (point-min))
  1058. (forward-line 3)
  1059. (org-table-calc-current-TBLFM)
  1060. (buffer-string)))
  1061. (expect "
  1062. | 1 | 1 |
  1063. | 2 | 2 |
  1064. #+TBLFM: $2=$1*1
  1065. #+TBLFM: $2=$1*2
  1066. #+TBLFM: $2=$1*3
  1067. "))
  1068. (should (string= got
  1069. expect)))
  1070. (let ((got (progn (goto-char (point-min))
  1071. (forward-line 4)
  1072. (org-table-calc-current-TBLFM)
  1073. (buffer-string)))
  1074. (expect "
  1075. | 1 | 2 |
  1076. | 2 | 4 |
  1077. #+TBLFM: $2=$1*1
  1078. #+TBLFM: $2=$1*2
  1079. #+TBLFM: $2=$1*3
  1080. "))
  1081. (should (string= got
  1082. expect)))))
  1083. (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
  1084. "org-table-calc-current-TBLFM should preserve the input as it was."
  1085. (org-test-with-temp-text-in-file
  1086. "
  1087. | 1 | 1 |
  1088. | 2 | 2 |
  1089. #+TBLFM: $2=$1*1
  1090. #+TBLFM: $2=$1*2::$2=$1*2
  1091. #+TBLFM: $2=$1*3
  1092. "
  1093. (let ((expect "
  1094. | 1 | 1 |
  1095. | 2 | 2 |
  1096. #+TBLFM: $2=$1*1
  1097. #+TBLFM: $2=$1*2::$2=$1*2
  1098. #+TBLFM: $2=$1*3
  1099. "))
  1100. (goto-char (point-min))
  1101. (forward-line 4)
  1102. (should-error (org-table-calc-current-TBLFM))
  1103. (setq got (buffer-string))
  1104. (message "%s" got)
  1105. (should (string= got
  1106. expect)))))
  1107. ;;; Radio Tables
  1108. (ert-deftest test-org-table/to-generic ()
  1109. "Test `orgtbl-to-generic' specifications."
  1110. ;; Test :hline parameter.
  1111. (should
  1112. (equal "a\nb"
  1113. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1114. '(:hline nil))))
  1115. (should
  1116. (equal "a\n~\nb"
  1117. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1118. '(:hline "~"))))
  1119. ;; Test :sep 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 "!"))))
  1125. ;; Test :hsep parameter.
  1126. (should
  1127. (equal "a!b\nc?d"
  1128. (orgtbl-to-generic
  1129. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1130. '(:sep "?" :hsep "!"))))
  1131. ;; Test :tstart parameter.
  1132. (should
  1133. (equal "<begin>\na"
  1134. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tstart "<begin>"))))
  1135. (should
  1136. (equal "<begin>\na"
  1137. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1138. '(:tstart (lambda () "<begin>")))))
  1139. (should
  1140. (equal "a"
  1141. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1142. '(:tstart "<begin>" :splice t))))
  1143. ;; Test :tend parameter.
  1144. (should
  1145. (equal "a\n<end>"
  1146. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tend "<end>"))))
  1147. (should
  1148. (equal "a\n<end>"
  1149. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1150. '(:tend (lambda () "<end>")))))
  1151. (should
  1152. (equal "a"
  1153. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1154. '(:tend "<end>" :splice t))))
  1155. ;; Test :lstart parameter.
  1156. (should
  1157. (equal "> a"
  1158. (orgtbl-to-generic
  1159. (org-table-to-lisp "| a |") '(:lstart "> "))))
  1160. (should
  1161. (equal "> a"
  1162. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1163. '(:lstart (lambda () "> ")))))
  1164. ;; Test :llstart parameter.
  1165. (should
  1166. (equal "> a\n>> b"
  1167. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1168. '(:lstart "> " :llstart ">> "))))
  1169. ;; Test :hlstart parameter.
  1170. (should
  1171. (equal "!> a\n> b"
  1172. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1173. '(:lstart "> " :hlstart "!> "))))
  1174. ;; Test :hllstart parameter.
  1175. (should
  1176. (equal "!> a\n!!> b\n> c"
  1177. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1178. '(:lstart "> " :hlstart "!> " :hllstart "!!> "))))
  1179. ;; Test :lend parameter.
  1180. (should
  1181. (equal "a <"
  1182. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:lend " <"))))
  1183. ;; Test :llend parameter.
  1184. (should
  1185. (equal "a <\nb <<"
  1186. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1187. '(:lend " <" :llend " <<"))))
  1188. ;; Test :hlend parameter.
  1189. (should
  1190. (equal "a <!\nb <"
  1191. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1192. '(:lend " <" :hlend " <!"))))
  1193. ;; Test :hllend parameter.
  1194. (should
  1195. (equal "a <!\nb <!!\nc <"
  1196. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1197. '(:lend " <" :hlend " <!" :hllend " <!!"))))
  1198. ;; Test :lfmt parameter.
  1199. (should
  1200. (equal "a!b"
  1201. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1202. '(:lfmt "%s!%s"))))
  1203. (should
  1204. (equal "a+b"
  1205. (orgtbl-to-generic
  1206. (org-table-to-lisp "| a | b |")
  1207. '(:lfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1208. (should
  1209. (equal "a!b"
  1210. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1211. '(:lfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1212. ;; Test :llfmt parameter.
  1213. (should
  1214. (equal "a!b"
  1215. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1216. '(:llfmt "%s!%s"))))
  1217. (should
  1218. (equal "a!b\nc+d"
  1219. (orgtbl-to-generic
  1220. (org-table-to-lisp "| a | b |\n| c | d |")
  1221. '(:lfmt "%s!%s" :llfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1222. (should
  1223. (equal "a!b"
  1224. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1225. '(:llfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1226. ;; Test :hlfmt parameter.
  1227. (should
  1228. (equal "a!b\ncd"
  1229. (orgtbl-to-generic
  1230. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1231. '(:hlfmt "%s!%s"))))
  1232. (should
  1233. (equal "a+b\ncd"
  1234. (orgtbl-to-generic
  1235. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1236. '(:hlfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1237. (should
  1238. (equal "a!b\n>c d<"
  1239. (orgtbl-to-generic
  1240. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1241. '(:hlfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1242. ;; Test :hllfmt parameter.
  1243. (should
  1244. (equal "a!b\ncd"
  1245. (orgtbl-to-generic
  1246. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1247. '(:hllfmt "%s!%s"))))
  1248. (should
  1249. (equal "a+b\ncd"
  1250. (orgtbl-to-generic
  1251. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1252. '(:hllfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1253. (should
  1254. (equal "a!b\n>c d<"
  1255. (orgtbl-to-generic
  1256. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1257. '(:hllfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1258. ;; Test :fmt parameter.
  1259. (should
  1260. (equal ">a<\n>b<"
  1261. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1262. '(:fmt ">%s<"))))
  1263. (should
  1264. (equal ">a<b"
  1265. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1266. '(:fmt (1 ">%s<" 2 (lambda (c) c))))))
  1267. (should
  1268. (equal "a b"
  1269. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1270. '(:fmt (2 " %s")))))
  1271. (should
  1272. (equal ">a<"
  1273. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1274. '(:fmt (lambda (c) (format ">%s<" c))))))
  1275. ;; Test :hfmt parameter.
  1276. (should
  1277. (equal ">a<\nb"
  1278. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1279. '(:hfmt ">%s<"))))
  1280. (should
  1281. (equal ">a<b\ncd"
  1282. (orgtbl-to-generic
  1283. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1284. '(:hfmt (1 ">%s<" 2 identity)))))
  1285. (should
  1286. (equal "a b\ncd"
  1287. (orgtbl-to-generic
  1288. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1289. '(:hfmt (2 " %s")))))
  1290. (should
  1291. (equal ">a<\nb"
  1292. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1293. '(:hfmt (lambda (c) (format ">%s<" c))))))
  1294. ;; Test :efmt parameter.
  1295. (should
  1296. (equal "2x10^3"
  1297. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1298. '(:efmt "%sx10^%s"))))
  1299. (should
  1300. (equal "2x10^3"
  1301. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1302. '(:efmt (lambda (m e) (concat m "x10^" e))))))
  1303. (should
  1304. (equal "2x10^3"
  1305. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1306. '(:efmt (1 "%sx10^%s")))))
  1307. (should
  1308. (equal "2x10^3"
  1309. (orgtbl-to-generic
  1310. (org-table-to-lisp "| 2e3 |")
  1311. '(:efmt (1 (lambda (m e) (format "%sx10^%s" m e)))))))
  1312. (should
  1313. (equal "2e3"
  1314. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |") '(:efmt nil))))
  1315. ;; Test :skip parameter.
  1316. (should
  1317. (equal "cd"
  1318. (orgtbl-to-generic
  1319. (org-table-to-lisp "| \ | <c> |\n| a | b |\n|---+---|\n| c | d |")
  1320. '(:skip 2))))
  1321. ;; Test :skipcols parameter.
  1322. (should
  1323. (equal "a\nc"
  1324. (orgtbl-to-generic
  1325. (org-table-to-lisp "| a | b |\n| c | d |") '(:skipcols (2)))))
  1326. (should
  1327. (equal "a\nc"
  1328. (orgtbl-to-generic
  1329. (org-table-to-lisp
  1330. "| / | <c> | <c> |\n| # | a | b |\n|---+---+---|\n| | c | d |")
  1331. '(:skipcols (2)))))
  1332. ;; Test :raw parameter.
  1333. (when (featurep 'ox-latex)
  1334. (should
  1335. (string-match-p
  1336. "/a/"
  1337. (orgtbl-to-generic (org-table-to-lisp "| /a/ | b |")
  1338. '(:backend latex :raw t)))))
  1339. ;; Hooks are ignored.
  1340. (should
  1341. (equal
  1342. "a\nb"
  1343. (let* ((fun-list (list (lambda (backend) (search-forward "a") (insert "hook"))))
  1344. (org-export-before-parsing-hook fun-list)
  1345. (org-export-before-processing-hook fun-list))
  1346. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1347. '(:hline nil)))))
  1348. ;; User-defined export filters are ignored.
  1349. (should
  1350. (equal
  1351. "a\nb"
  1352. (let ((org-export-filter-table-cell-functions (list (lambda (c b i) "filter"))))
  1353. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1354. '(:hline nil)))))
  1355. ;; Macros, even if unknown, are returned as-is.
  1356. (should
  1357. (equal "{{{macro}}}"
  1358. (orgtbl-to-generic (org-table-to-lisp "| {{{macro}}} |") nil))))
  1359. (ert-deftest test-org-table/to-latex ()
  1360. "Test `orgtbl-to-latex' specifications."
  1361. (should
  1362. (equal "\\begin{tabular}{l}\na\\\\\n\\end{tabular}"
  1363. (orgtbl-to-latex (org-table-to-lisp "| a |") nil)))
  1364. ;; Test :environment parameter.
  1365. (should
  1366. (equal "\\begin{tabularx}{l}\na\\\\\n\\end{tabularx}"
  1367. (orgtbl-to-latex (org-table-to-lisp "| a |")
  1368. '(:environment "tabularx"))))
  1369. ;; Test :booktabs parameter.
  1370. (should
  1371. (string-match-p
  1372. "\\toprule" (orgtbl-to-latex (org-table-to-lisp "| a |") '(:booktabs t))))
  1373. ;; Handle LaTeX snippets.
  1374. (should
  1375. (equal "\\begin{tabular}{l}\n\\(x\\)\\\\\n\\end{tabular}"
  1376. (orgtbl-to-latex (org-table-to-lisp "| $x$ |") nil)))
  1377. ;; Test pseudo objects and :raw parameter.
  1378. (should
  1379. (string-match-p
  1380. "\\$x\\$" (orgtbl-to-latex (org-table-to-lisp "| $x$ |") '(:raw t)))))
  1381. (ert-deftest test-org-table/to-html ()
  1382. "Test `orgtbl-to-html' specifications."
  1383. (should
  1384. (equal (orgtbl-to-html (org-table-to-lisp "| a |") nil)
  1385. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">
  1386. <colgroup>
  1387. <col class=\"org-left\" />
  1388. </colgroup>
  1389. <tbody>
  1390. <tr>
  1391. <td class=\"org-left\">a</td>
  1392. </tr>
  1393. </tbody>
  1394. </table>"))
  1395. ;; Test :attributes parameter.
  1396. (should
  1397. (string-match-p
  1398. "<table>"
  1399. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes nil))))
  1400. (should
  1401. (string-match-p
  1402. "<table border=\"2\">"
  1403. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes (:border "2"))))))
  1404. (ert-deftest test-org-table/to-texinfo ()
  1405. "Test `orgtbl-to-texinfo' specifications."
  1406. (should
  1407. (equal "@multitable {a}\n@item a\n@end multitable"
  1408. (orgtbl-to-texinfo (org-table-to-lisp "| a |") nil)))
  1409. ;; Test :columns parameter.
  1410. (should
  1411. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1412. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1413. '(:columns ".4 .6"))))
  1414. (should
  1415. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1416. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1417. '(:columns "@columnfractions .4 .6"))))
  1418. (should
  1419. (equal "@multitable {xxx} {xx}\n@item a\n@tab b\n@end multitable"
  1420. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1421. '(:columns "{xxx} {xx}")))))
  1422. (ert-deftest test-org-table/to-orgtbl ()
  1423. "Test `orgtbl-to-orgtbl' specifications."
  1424. (should
  1425. (equal "| a | b |\n|---+---|\n| c | d |"
  1426. (orgtbl-to-orgtbl
  1427. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |") nil))))
  1428. (ert-deftest test-org-table/to-unicode ()
  1429. "Test `orgtbl-to-unicode' specifications."
  1430. (should
  1431. (equal "━━━\n a \n━━━"
  1432. (orgtbl-to-unicode (org-table-to-lisp "| a |") nil)))
  1433. ;; Test :narrow parameter.
  1434. (should
  1435. (equal "━━━━\n => \n━━━━"
  1436. (orgtbl-to-unicode (org-table-to-lisp "| <2> |\n| xxx |")
  1437. '(:narrow t)))))
  1438. (ert-deftest test-org-table/send-region ()
  1439. "Test `orgtbl-send-table' specifications."
  1440. ;; Error when not at a table.
  1441. (should-error
  1442. (org-test-with-temp-text "Paragraph"
  1443. (orgtbl-send-table)))
  1444. ;; Error when destination is missing.
  1445. (should-error
  1446. (org-test-with-temp-text "#+ORGTBL: SEND\n<point>| a |"
  1447. (orgtbl-send-table)))
  1448. ;; Error when transformation function is not specified.
  1449. (should-error
  1450. (org-test-with-temp-text "
  1451. # BEGIN RECEIVE ORGTBL table
  1452. # END RECEIVE ORGTBL table
  1453. #+ORGTBL: SEND table
  1454. <point>| a |"
  1455. (orgtbl-send-table)))
  1456. ;; Standard test.
  1457. (should
  1458. (equal "| a |\n|---|\n| b |\n"
  1459. (org-test-with-temp-text "
  1460. # BEGIN RECEIVE ORGTBL table
  1461. # END RECEIVE ORGTBL table
  1462. #+ORGTBL: SEND table orgtbl-to-orgtbl :hlines nil
  1463. <point>| a |\n|---|\n| b |"
  1464. (orgtbl-send-table)
  1465. (goto-char (point-min))
  1466. (buffer-substring-no-properties
  1467. (search-forward "# BEGIN RECEIVE ORGTBL table\n")
  1468. (progn (search-forward "# END RECEIVE ORGTBL table")
  1469. (match-beginning 0))))))
  1470. ;; Allow multiple receiver locations.
  1471. (should
  1472. (org-test-with-temp-text "
  1473. # BEGIN RECEIVE ORGTBL table
  1474. # END RECEIVE ORGTBL table
  1475. #+ORGTBL: SEND table orgtbl-to-orgtbl :hlines nil
  1476. <point>| a |
  1477. # BEGIN RECEIVE ORGTBL table
  1478. # END RECEIVE ORGTBL table"
  1479. (orgtbl-send-table)
  1480. (goto-char (point-min))
  1481. (search-forward "| a |" nil t 3))))
  1482. ;;; Align
  1483. (ert-deftest test-org-table/align ()
  1484. "Test `org-table-align' specifications."
  1485. ;; Regular test.
  1486. (should
  1487. (equal "| a |\n"
  1488. (org-test-with-temp-text "| a |"
  1489. (org-table-align)
  1490. (buffer-string))))
  1491. ;; Preserve alignment.
  1492. (should
  1493. (equal " | a |\n"
  1494. (org-test-with-temp-text " | a |"
  1495. (org-table-align)
  1496. (buffer-string))))
  1497. ;; Handle horizontal lines.
  1498. (should
  1499. (equal "| 123 |\n|-----|\n"
  1500. (org-test-with-temp-text "| 123 |\n|-|"
  1501. (org-table-align)
  1502. (buffer-string))))
  1503. (should
  1504. (equal "| a | b |\n|---+---|\n"
  1505. (org-test-with-temp-text "| a | b |\n|-+-|"
  1506. (org-table-align)
  1507. (buffer-string))))
  1508. ;; Handle empty fields.
  1509. (should
  1510. (equal "| a | bc |\n| bcd | |\n"
  1511. (org-test-with-temp-text "| a | bc |\n| bcd | |"
  1512. (org-table-align)
  1513. (buffer-string))))
  1514. (should
  1515. (equal "| abc | bc |\n| | bcd |\n"
  1516. (org-test-with-temp-text "| abc | bc |\n| | bcd |"
  1517. (org-table-align)
  1518. (buffer-string))))
  1519. ;; Handle missing fields.
  1520. (should
  1521. (equal "| a | b |\n| c | |\n"
  1522. (org-test-with-temp-text "| a | b |\n| c |"
  1523. (org-table-align)
  1524. (buffer-string))))
  1525. (should
  1526. (equal "| a | b |\n|---+---|\n"
  1527. (org-test-with-temp-text "| a | b |\n|---|"
  1528. (org-table-align)
  1529. (buffer-string))))
  1530. ;; Alignment is done to the right when the ratio of numbers in the
  1531. ;; column is superior to `org-table-number-fraction'.
  1532. (should
  1533. (equal "| 1 |\n| 12 |\n| abc |"
  1534. (org-test-with-temp-text "| 1 |\n| 12 |\n| abc |"
  1535. (let ((org-table-number-fraction 0.5)) (org-table-align))
  1536. (buffer-string))))
  1537. (should
  1538. (equal "| 1 |\n| ab |\n| abc |"
  1539. (org-test-with-temp-text "| 1 |\n| ab |\n| abc |"
  1540. (let ((org-table-number-fraction 0.5)) (org-table-align))
  1541. (buffer-string))))
  1542. ;; Obey to alignment cookies.
  1543. (should
  1544. (equal "| <r> |\n| ab |\n| abc |"
  1545. (org-test-with-temp-text "| <r> |\n| ab |\n| abc |"
  1546. (let ((org-table-number-fraction 0.5)) (org-table-align))
  1547. (buffer-string))))
  1548. (should
  1549. (equal "| <l> |\n| 12 |\n| 123 |"
  1550. (org-test-with-temp-text "| <l> |\n| 12 |\n| 123 |"
  1551. (let ((org-table-number-fraction 0.5)) (org-table-align))
  1552. (buffer-string))))
  1553. (should
  1554. (equal "| <c> |\n| 1 |\n| 123 |"
  1555. (org-test-with-temp-text "| <c> |\n| 1 |\n| 123 |"
  1556. (let ((org-table-number-fraction 0.5)) (org-table-align))
  1557. (buffer-string)))))
  1558. (ert-deftest test-org-table/align-buffer-tables ()
  1559. "Align all tables when updating buffer."
  1560. (let ((before "
  1561. | a b |
  1562. | c d |
  1563. ")
  1564. (after "
  1565. | a b |
  1566. | c d |
  1567. "))
  1568. (should (equal (org-test-with-temp-text before
  1569. (org-table-recalculate-buffer-tables)
  1570. (buffer-string))
  1571. after))
  1572. (should (equal (org-test-with-temp-text before
  1573. (org-table-iterate-buffer-tables)
  1574. (buffer-string))
  1575. after))))
  1576. ;;; Sorting
  1577. (ert-deftest test-org-table/sort-lines ()
  1578. "Test `org-table-sort-lines' specifications."
  1579. ;; Sort numerically.
  1580. (should
  1581. (equal "| 1 | 2 |\n| 2 | 4 |\n| 5 | 3 |\n"
  1582. (org-test-with-temp-text "| <point>1 | 2 |\n| 5 | 3 |\n| 2 | 4 |\n"
  1583. (org-table-sort-lines nil ?n)
  1584. (buffer-string))))
  1585. (should
  1586. (equal "| 5 | 3 |\n| 2 | 4 |\n| 1 | 2 |\n"
  1587. (org-test-with-temp-text "| <point>1 | 2 |\n| 5 | 3 |\n| 2 | 4 |\n"
  1588. (org-table-sort-lines nil ?N)
  1589. (buffer-string))))
  1590. ;; Sort alphabetically.
  1591. (should
  1592. (equal "| a | x |\n| b | 4 |\n| c | 3 |\n"
  1593. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| b | 4 |\n"
  1594. (org-table-sort-lines nil ?a)
  1595. (buffer-string))))
  1596. (should
  1597. (equal "| c | 3 |\n| b | 4 |\n| a | x |\n"
  1598. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| b | 4 |\n"
  1599. (org-table-sort-lines nil ?A)
  1600. (buffer-string))))
  1601. ;; Sort alphabetically with case.
  1602. (should
  1603. (equal "| C |\n| a |\n| b |\n"
  1604. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1605. (org-table-sort-lines t ?a)
  1606. (buffer-string))))
  1607. (should
  1608. (equal "| b |\n| a |\n| C |\n"
  1609. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1610. (org-table-sort-lines nil ?A)
  1611. (buffer-string))))
  1612. ;; Sort by time (timestamps)
  1613. (should
  1614. (equal
  1615. "| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n| <2014-03-04 tue.> |\n"
  1616. (org-test-with-temp-text
  1617. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1618. (org-table-sort-lines nil ?t)
  1619. (buffer-string))))
  1620. (should
  1621. (equal
  1622. "| <2014-03-04 tue.> |\n| <2012-03-29 thu.> |\n| <2008-08-08 sat.> |\n"
  1623. (org-test-with-temp-text
  1624. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1625. (org-table-sort-lines nil ?T)
  1626. (buffer-string))))
  1627. ;; Sort by time (HH:MM values)
  1628. (should
  1629. (equal "| 1:00 |\n| 17:00 |\n| 114:00 |\n"
  1630. (org-test-with-temp-text "| 114:00 |\n| 17:00 |\n| 1:00 |\n"
  1631. (org-table-sort-lines nil ?t)
  1632. (buffer-string))))
  1633. (should
  1634. (equal "| 114:00 |\n| 17:00 |\n| 1:00 |\n"
  1635. (org-test-with-temp-text "| 114:00 |\n| 17:00 |\n| 1:00 |\n"
  1636. (org-table-sort-lines nil ?T)
  1637. (buffer-string))))
  1638. ;; Sort by time (durations)
  1639. (should
  1640. (equal "| 1d 3:00 |\n| 28:00 |\n"
  1641. (org-test-with-temp-text "| 28:00 |\n| 1d 3:00 |\n"
  1642. (org-table-sort-lines nil ?t)
  1643. (buffer-string))))
  1644. ;; Sort with custom functions.
  1645. (should
  1646. (equal "| 22 |\n| 15 |\n| 18 |\n"
  1647. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1648. (org-table-sort-lines nil ?f
  1649. (lambda (s) (% (string-to-number s) 10))
  1650. #'<)
  1651. (buffer-string))))
  1652. (should
  1653. (equal "| 18 |\n| 15 |\n| 22 |\n"
  1654. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1655. (org-table-sort-lines nil ?F
  1656. (lambda (s) (% (string-to-number s) 10))
  1657. #'<)
  1658. (buffer-string))))
  1659. ;; Sort according to current column.
  1660. (should
  1661. (equal "| 1 | 2 |\n| 7 | 3 |\n| 5 | 4 |\n"
  1662. (org-test-with-temp-text "| 1 | <point>2 |\n| 5 | 4 |\n| 7 | 3 |\n"
  1663. (org-table-sort-lines nil ?n)
  1664. (buffer-string))))
  1665. ;; Sort between horizontal separators if possible.
  1666. (should
  1667. (equal
  1668. "| 9 | 8 |\n|---+---|\n| 5 | 3 |\n| 7 | 4 |\n|---+---|\n| 1 | 2 |\n"
  1669. (org-test-with-temp-text
  1670. "| 9 | 8 |\n|---+---|\n| <point>7 | 4 |\n| 5 | 3 |\n|---+---|\n| 1 | 2 |\n"
  1671. (org-table-sort-lines nil ?n)
  1672. (buffer-string)))))
  1673. ;;; Formulas
  1674. (ert-deftest test-org-table/eval-formula ()
  1675. "Test `org-table-eval-formula' specifications."
  1676. ;; Error when not on a table field.
  1677. (should-error
  1678. (org-test-with-temp-text "Text"
  1679. (org-table-eval-formula)))
  1680. (should-error
  1681. (org-test-with-temp-text "| a |\n|---|<point>"
  1682. (org-table-eval-formula)))
  1683. (should-error
  1684. (org-test-with-temp-text "| a |\n#+TBLFM:<point>"
  1685. (org-table-eval-formula)))
  1686. ;; Handle @<, @>, $< and $>.
  1687. (should
  1688. (equal "| 1 |\n| 1 |"
  1689. (org-test-with-temp-text "| <point> |\n| 1 |"
  1690. (org-table-eval-formula nil "@>" nil nil t)
  1691. (buffer-string))))
  1692. (should
  1693. (equal "| 1 |\n| 1 |"
  1694. (org-test-with-temp-text "| 1 |\n| <point> |"
  1695. (org-table-eval-formula nil "@<" nil nil t)
  1696. (buffer-string))))
  1697. (should
  1698. (equal "| 1 | 1 |"
  1699. (org-test-with-temp-text "| <point> | 1 |"
  1700. (org-table-eval-formula nil "$>" nil nil t)
  1701. (buffer-string))))
  1702. (should
  1703. (equal "| 1 | 1 |"
  1704. (org-test-with-temp-text "| 1 | <point> |"
  1705. (org-table-eval-formula nil "$<" nil nil t)
  1706. (buffer-string)))))
  1707. (ert-deftest test-org-table/field-formula-outside-table ()
  1708. "If `org-table-formula-create-columns' is nil, then a formula
  1709. that references an out-of-bounds column should do nothing. If it
  1710. is t, then new columns should be added as needed"
  1711. (let ((org-table-formula-create-columns nil))
  1712. (should-error
  1713. (org-test-table-target-expect
  1714. "
  1715. | 2 |
  1716. | 4 |
  1717. | 8 |
  1718. "
  1719. "
  1720. | 2 |
  1721. | 4 |
  1722. | 8 |
  1723. "
  1724. 1
  1725. "#+TBLFM: @1$2=5")
  1726. :type (list 'error 'user-error)))
  1727. (let ((org-table-formula-create-columns t))
  1728. ;; make sure field formulas work
  1729. (org-test-table-target-expect
  1730. "
  1731. | 2 |
  1732. | 4 |
  1733. | 8 |
  1734. "
  1735. "
  1736. | 2 | 5 |
  1737. | 4 | |
  1738. | 8 | |
  1739. "
  1740. 1
  1741. "#+TBLFM: @1$2=5")
  1742. ;; and make sure column formulas work too
  1743. (org-test-table-target-expect
  1744. "
  1745. | 2 |
  1746. | 4 |
  1747. | 8 |
  1748. "
  1749. "
  1750. | 2 | | 15 |
  1751. | 4 | | 15 |
  1752. | 8 | | 15 |
  1753. "
  1754. 1
  1755. "#+TBLFM: $3=15")))
  1756. (ert-deftest test-org-table/duration ()
  1757. "Test durations in table formulas."
  1758. ;; Durations in cells.
  1759. (should
  1760. (string-match "| 2:12 | 1:47 | 03:59:00 |"
  1761. (org-test-with-temp-text "
  1762. | 2:12 | 1:47 | |
  1763. <point>#+TBLFM: @1$3=$1+$2;T"
  1764. (org-table-calc-current-TBLFM)
  1765. (buffer-string))))
  1766. (should
  1767. (string-match "| 2:12 | 1:47 | 03:59 |"
  1768. (org-test-with-temp-text "
  1769. | 2:12 | 1:47 | |
  1770. <point>#+TBLFM: @1$3=$1+$2;U"
  1771. (org-table-calc-current-TBLFM)
  1772. (buffer-string))))
  1773. (should
  1774. (string-match "| 3:02:20 | -2:07:00 | 0.92 |"
  1775. (org-test-with-temp-text "
  1776. | 3:02:20 | -2:07:00 | |
  1777. <point>#+TBLFM: @1$3=$1+$2;t"
  1778. (org-table-calc-current-TBLFM)
  1779. (buffer-string))))
  1780. ;; Durations set through properties.
  1781. (should
  1782. (string-match "| 16:00:00 |"
  1783. (org-test-with-temp-text "* H
  1784. :PROPERTIES:
  1785. :time_constant: 08:00:00
  1786. :END:
  1787. | |
  1788. <point>#+TBLFM: $1=2*$PROP_time_constant;T"
  1789. (org-table-calc-current-TBLFM)
  1790. (buffer-string))))
  1791. (should
  1792. (string-match "| 16.00 |"
  1793. (org-test-with-temp-text "* H
  1794. :PROPERTIES:
  1795. :time_constant: 08:00:00
  1796. :END:
  1797. | |
  1798. <point>#+TBLFM: $1=2*$PROP_time_constant;t"
  1799. (org-table-calc-current-TBLFM)
  1800. (buffer-string)))))
  1801. (ert-deftest test-org-table/end-on-hline ()
  1802. "Test with a table ending on a hline."
  1803. (should
  1804. (equal
  1805. (org-test-with-temp-text
  1806. "
  1807. | 1 | 2 | 3 |
  1808. | 4 | 5 | 6 |
  1809. | | | |
  1810. |---+---+---|
  1811. <point>#+TBLFM: @3$2..@3$>=vsum(@1..@2)"
  1812. (org-table-calc-current-TBLFM)
  1813. (buffer-string))
  1814. "
  1815. | 1 | 2 | 3 |
  1816. | 4 | 5 | 6 |
  1817. | | 7 | 9 |
  1818. |---+---+---|
  1819. #+TBLFM: @3$2..@3$>=vsum(@1..@2)")))
  1820. (ert-deftest test-org-table/named-field ()
  1821. "Test formula with a named field."
  1822. (should
  1823. (string-match-p
  1824. "| +| +1 +|"
  1825. (org-test-with-temp-text "
  1826. | | |
  1827. | ^ | name |
  1828. <point>#+TBLFM: $name=1"
  1829. (org-table-calc-current-TBLFM)
  1830. (buffer-string))))
  1831. (should
  1832. (string-match-p
  1833. "| +| +1 +|"
  1834. (org-test-with-temp-text "
  1835. | _ | name |
  1836. | | |
  1837. <point>#+TBLFM: $name=1"
  1838. (org-table-calc-current-TBLFM)
  1839. (buffer-string)))))
  1840. (ert-deftest test-org-table/named-column ()
  1841. "Test formula with a named field."
  1842. (should
  1843. (string-match-p
  1844. "| +| +1 +| +1 +|"
  1845. (org-test-with-temp-text "
  1846. | ! | name | |
  1847. | | 1 | |
  1848. <point>#+TBLFM: @2$3=$name"
  1849. (org-table-calc-current-TBLFM)
  1850. (buffer-string)))))
  1851. (ert-deftest test-org-table/formula-priority ()
  1852. "Test field formula priority over column formula."
  1853. ;; Field formulas bind stronger than column formulas.
  1854. (should
  1855. (equal
  1856. "| 1 | 3 |\n| 2 | 99 |\n"
  1857. (org-test-with-temp-text
  1858. "| 1 | |\n| 2 | |\n<point>#+tblfm: $2=3*$1::@2$2=99"
  1859. (org-table-calc-current-TBLFM)
  1860. (buffer-substring-no-properties (point-min) (point)))))
  1861. ;; When field formula is removed, table formulas is applied again.
  1862. (should
  1863. (equal
  1864. "| 1 | 3 |\n| 2 | 6 |\n"
  1865. (org-test-with-temp-text
  1866. "| 1 | |\n| 2 | |\n#+tblfm: $2=3*$1<point>::@2$2=99"
  1867. (org-table-calc-current-TBLFM)
  1868. (delete-region (point) (line-end-position))
  1869. (org-table-calc-current-TBLFM)
  1870. (buffer-substring-no-properties (point-min) (line-beginning-position))))))
  1871. (ert-deftest test-org-table/tab-indent ()
  1872. "Test named fields with tab indentation."
  1873. (should
  1874. (string-match-p
  1875. "| # | 111 |"
  1876. (org-test-with-temp-text
  1877. "
  1878. | ! | sum | | a | b | c |
  1879. |---+------+------+---+----+-----|
  1880. | # | 1011 | 1000 | 1 | 10 | 100 |
  1881. <point>#+TBLFM: $2=$a+$b+$c
  1882. "
  1883. (org-table-calc-current-TBLFM)
  1884. (buffer-string)))))
  1885. (ert-deftest test-org-table/first-rc ()
  1886. "Test \"$<\" and \"@<\" constructs in formulas."
  1887. (should
  1888. (string-match-p
  1889. "| 1 | 2 |"
  1890. (org-test-with-temp-text
  1891. "| | 2 |
  1892. <point>#+TBLFM: $<=1"
  1893. (org-table-calc-current-TBLFM)
  1894. (buffer-string))))
  1895. (should
  1896. (string-match-p
  1897. "| 2 |\n| 2 |"
  1898. (org-test-with-temp-text
  1899. "| 2 |\n| |
  1900. <point>#+TBLFM: @2$1=@<"
  1901. (org-table-calc-current-TBLFM)
  1902. (buffer-string)))))
  1903. (ert-deftest test-org-table/last-rc ()
  1904. "Test \"$>\" and \"@>\" constructs in formulas."
  1905. (should
  1906. (string-match-p
  1907. "| 2 | 1 |"
  1908. (org-test-with-temp-text
  1909. "| 2 | |\n<point>#+TBLFM: $>=1"
  1910. (org-table-calc-current-TBLFM)
  1911. (buffer-string))))
  1912. (should
  1913. (string-match-p
  1914. "| 2 |\n| 2 |"
  1915. (org-test-with-temp-text
  1916. "| 2 |\n| |\n<point>#+TBLFM: @>$1=@<"
  1917. (org-table-calc-current-TBLFM)
  1918. (buffer-string)))))
  1919. (ert-deftest test-org-table/time-stamps ()
  1920. "Test time-stamps handling."
  1921. ;; Standard test.
  1922. (should
  1923. (string-match-p
  1924. "| 1 |"
  1925. (org-test-with-temp-text
  1926. "| <2016-07-07 Sun> | <2016-07-08 Fri> | |\n<point>#+TBLFM: $3=$2-$1"
  1927. (org-table-calc-current-TBLFM)
  1928. (buffer-string))))
  1929. ;; Handle locale specific time-stamps.
  1930. (should
  1931. (string-match-p
  1932. "| 1 |"
  1933. (org-test-with-temp-text
  1934. "| <2016-07-07 Do> | <2016-07-08 Fr> | |\n<point>#+TBLFM: $3=$2-$1"
  1935. (org-table-calc-current-TBLFM)
  1936. (buffer-string)))))
  1937. (ert-deftest test-org-table/orgtbl-ascii-draw ()
  1938. "Test `orgtbl-ascii-draw'."
  1939. ;; First value: Make sure that an integer input value is converted to a
  1940. ;; float before division. Further values: Show some float input value
  1941. ;; ranges corresponding to the same bar width.
  1942. (should
  1943. (equal
  1944. (org-test-with-temp-text
  1945. "
  1946. | Value | <l> |
  1947. |----------+---------|
  1948. | 19 | replace |
  1949. |----------+---------|
  1950. | -0.50001 | replace |
  1951. | -0.49999 | replace |
  1952. | 0.49999 | replace |
  1953. | 0.50001 | replace |
  1954. | 1.49999 | replace |
  1955. | 22.50001 | replace |
  1956. | 23.49999 | replace |
  1957. | 23.50001 | replace |
  1958. | 24.49999 | replace |
  1959. | 24.50001 | replace |
  1960. <point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"
  1961. (org-table-calc-current-TBLFM)
  1962. (buffer-string))
  1963. "
  1964. | Value | <l> |
  1965. |----------+-----------|
  1966. | 19 | 883 |
  1967. |----------+-----------|
  1968. | -0.50001 | too small |
  1969. | -0.49999 | |
  1970. | 0.49999 | |
  1971. | 0.50001 | 1 |
  1972. | 1.49999 | 1 |
  1973. | 22.50001 | 887 |
  1974. | 23.49999 | 887 |
  1975. | 23.50001 | 888 |
  1976. | 24.49999 | 888 |
  1977. | 24.50001 | too large |
  1978. #+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"))
  1979. ;; Draw bars with a bullet. The bullet does not count in the parameter
  1980. ;; WIDTH of `orgtbl-ascii-draw'.
  1981. (should
  1982. (equal
  1983. (org-test-with-temp-text
  1984. "
  1985. | -1 | replace |
  1986. | 0 | replace |
  1987. | 1 | replace |
  1988. | 2 | replace |
  1989. | 3 | replace |
  1990. | 4 | replace |
  1991. <point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 3 3 \"$-\")"
  1992. (org-table-calc-current-TBLFM)
  1993. (buffer-string))
  1994. "
  1995. | -1 | too small |
  1996. | 0 | $ |
  1997. | 1 | -$ |
  1998. | 2 | --$ |
  1999. | 3 | ---$ |
  2000. | 4 | too large |
  2001. #+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 3 3 \"$-\")")))
  2002. (ert-deftest test-org-table/single-rowgroup ()
  2003. "Test column formula in a table with a single rowgroup."
  2004. (should
  2005. (equal
  2006. "
  2007. |---+---|
  2008. | 1 | 0 |
  2009. |---+---|
  2010. #+TBLFM: $2=$1-1"
  2011. (org-test-with-temp-text "
  2012. |---+---|
  2013. | 1 | |
  2014. |---+---|
  2015. <point>#+TBLFM: $2=$1-1"
  2016. (org-table-calc-current-TBLFM)
  2017. (buffer-string))))
  2018. (should
  2019. (equal
  2020. "
  2021. | 1 | 0 |
  2022. #+TBLFM: $2=$1-1"
  2023. (org-test-with-temp-text "
  2024. | 1 | |
  2025. <point>#+TBLFM: $2=$1-1"
  2026. (org-table-calc-current-TBLFM)
  2027. (buffer-string)))))
  2028. ;;; Navigation
  2029. (ert-deftest test-org-table/next-field ()
  2030. "Test `org-table-next-field' specifications."
  2031. ;; Regular test.
  2032. (should
  2033. (equal
  2034. "b"
  2035. (org-test-with-temp-text "| a<point> | b |"
  2036. (org-table-next-field)
  2037. (org-trim (org-table-get-field)))))
  2038. ;; Create new rows as needed.
  2039. (should
  2040. (equal
  2041. "| a |\n| |\n"
  2042. (org-test-with-temp-text "| a<point> |"
  2043. (org-table-next-field)
  2044. (buffer-string))))
  2045. ;; Jump over hlines, if `org-table-tab-jumps-over-hlines' is
  2046. ;; non-nil.
  2047. (should
  2048. (equal
  2049. "b"
  2050. (org-test-with-temp-text "| a<point> |\n|---|\n| b |"
  2051. (let ((org-table-tab-jumps-over-hlines t)) (org-table-next-field))
  2052. (org-trim (org-table-get-field)))))
  2053. ;; If `org-table-tab-jumps-over-hlines' is nil, however, create
  2054. ;; a new row before the rule.
  2055. (should
  2056. (equal
  2057. "| a |\n| |\n|---|\n| b |"
  2058. (org-test-with-temp-text "| a<point> |\n|---|\n| b |"
  2059. (let ((org-table-tab-jumps-over-hlines nil)) (org-table-next-field))
  2060. (buffer-string)))))
  2061. (ert-deftest test-org-table/previous-field ()
  2062. "Test `org-table-previous-field' specifications."
  2063. ;; Regular tests.
  2064. (should
  2065. (eq ?a
  2066. (org-test-with-temp-text "| a | <point>b |"
  2067. (org-table-previous-field)
  2068. (char-after))))
  2069. (should
  2070. (eq ?a
  2071. (org-test-with-temp-text "| a |\n| <point>b |"
  2072. (org-table-previous-field)
  2073. (char-after))))
  2074. ;; Find previous field across horizontal rules.
  2075. (should
  2076. (eq ?a
  2077. (org-test-with-temp-text "| a |\n|---|\n| <point>b |"
  2078. (org-table-previous-field)
  2079. (char-after))))
  2080. ;; When called on a horizontal rule, find previous data field.
  2081. (should
  2082. (eq ?b
  2083. (org-test-with-temp-text "| a | b |\n|---+-<point>--|"
  2084. (org-table-previous-field)
  2085. (char-after))))
  2086. ;; Error when at first field. Make sure to preserve original
  2087. ;; position.
  2088. (should-error
  2089. (org-test-with-temp-text "| <point> a|"
  2090. (org-table-previous-field)))
  2091. (should-error
  2092. (org-test-with-temp-text "|---|\n| <point>a |"
  2093. (org-table-previous-field)))
  2094. (should
  2095. (eq ?a
  2096. (org-test-with-temp-text "|---|\n| <point>a |"
  2097. (ignore-errors (org-table-previous-field))
  2098. (char-after)))))
  2099. ;;; Moving rows, moving columns
  2100. (ert-deftest test-org-table/move-row-down ()
  2101. "Test `org-table-move-row-down' specifications."
  2102. ;; Error out when row cannot be moved, e.g., it is the last row in
  2103. ;; the table.
  2104. (should-error
  2105. (org-test-with-temp-text "| a |"
  2106. (org-table-move-row-down)))
  2107. (should-error
  2108. (org-test-with-temp-text "| a |\n"
  2109. (org-table-move-row-down)))
  2110. (should-error
  2111. (org-test-with-temp-text "| a |\n| <point>b |"
  2112. (org-table-move-row-down)))
  2113. ;; Move data lines.
  2114. (should
  2115. (equal "| b |\n| a |\n"
  2116. (org-test-with-temp-text "| a |\n| b |\n"
  2117. (org-table-move-row-down)
  2118. (buffer-string))))
  2119. (should
  2120. (equal "|---|\n| a |\n"
  2121. (org-test-with-temp-text "| a |\n|---|\n"
  2122. (org-table-move-row-down)
  2123. (buffer-string))))
  2124. ;; Move hlines.
  2125. (should
  2126. (equal "| b |\n|---|\n"
  2127. (org-test-with-temp-text "|---|\n| b |\n"
  2128. (org-table-move-row-down)
  2129. (buffer-string))))
  2130. (should
  2131. (equal "|---|\n|---|\n"
  2132. (org-test-with-temp-text "|---|\n|---|\n"
  2133. (org-table-move-row-down)
  2134. (buffer-string))))
  2135. ;; Move rows even without a final newline.
  2136. (should
  2137. (equal "| b |\n| a |\n"
  2138. (org-test-with-temp-text "| a |\n| b |"
  2139. (org-table-move-row-down)
  2140. (buffer-string)))))
  2141. (ert-deftest test-org-table/move-row-up ()
  2142. "Test `org-table-move-row-up' specifications."
  2143. ;; Error out when row cannot be moved, e.g., it is the first row in
  2144. ;; the table.
  2145. (should-error
  2146. (org-test-with-temp-text "| a |"
  2147. (org-table-move-row-up)))
  2148. (should-error
  2149. (org-test-with-temp-text "| a |\n"
  2150. (org-table-move-row-up)))
  2151. ;; Move data lines.
  2152. (should
  2153. (equal "| b |\n| a |\n"
  2154. (org-test-with-temp-text "| a |\n| <point>b |\n"
  2155. (org-table-move-row-up)
  2156. (buffer-string))))
  2157. (should
  2158. (equal "| b |\n|---|\n"
  2159. (org-test-with-temp-text "|---|\n| <point>b |\n"
  2160. (org-table-move-row-up)
  2161. (buffer-string))))
  2162. ;; Move hlines.
  2163. (should
  2164. (equal "|---|\n| a |\n"
  2165. (org-test-with-temp-text "| a |\n|<point>---|\n"
  2166. (org-table-move-row-up)
  2167. (buffer-string))))
  2168. (should
  2169. (equal "|---|\n|---|\n"
  2170. (org-test-with-temp-text "|---|\n|<point>---|\n"
  2171. (org-table-move-row-up)
  2172. (buffer-string))))
  2173. ;; Move rows even without a final newline.
  2174. (should
  2175. (equal "| b |\n| a |\n"
  2176. (org-test-with-temp-text "| a |\n| <point>b |"
  2177. (org-table-move-row-up)
  2178. (buffer-string)))))
  2179. ;;; Shrunk columns
  2180. (ert-deftest test-org-table/toggle-column-width ()
  2181. "Test `org-table-toggle-columns-width' specifications."
  2182. ;; Error when not at a column.
  2183. (should-error
  2184. (org-test-with-temp-text "<point>a"
  2185. (org-table-toggle-column-width)))
  2186. ;; A shrunk columns is overlaid with
  2187. ;; `org-table-shrunk-column-indicator'.
  2188. (should
  2189. (equal org-table-shrunk-column-indicator
  2190. (org-test-with-temp-text "| <point>a |"
  2191. (org-table-toggle-column-width)
  2192. (overlay-get (car (overlays-at (point))) 'display))))
  2193. (should
  2194. (equal org-table-shrunk-column-indicator
  2195. (org-test-with-temp-text "| a |\n|-<point>--|"
  2196. (org-table-toggle-column-width)
  2197. (overlay-get (car (overlays-at (point))) 'display))))
  2198. ;; Shrink every field in the same column.
  2199. (should
  2200. (equal org-table-shrunk-column-indicator
  2201. (org-test-with-temp-text "| a |\n|-<point>--|"
  2202. (org-table-toggle-column-width)
  2203. (overlay-get (car (overlays-at (1+ (line-beginning-position 0))))
  2204. 'display))))
  2205. ;; When column is already shrunk, expand it, i.e., remove overlays.
  2206. (should-not
  2207. (equal org-table-shrunk-column-indicator
  2208. (org-test-with-temp-text "| <point>a |"
  2209. (org-table-toggle-column-width)
  2210. (org-table-toggle-column-width)
  2211. (overlays-in (point-min) (point-max)))))
  2212. (should-not
  2213. (equal org-table-shrunk-column-indicator
  2214. (org-test-with-temp-text "| a |\n| <point>b |"
  2215. (org-table-toggle-column-width)
  2216. (org-table-toggle-column-width)
  2217. (overlays-in (point-min) (point-max)))))
  2218. ;; With a column width cookie, limit overlay to the specified number
  2219. ;; of characters.
  2220. (should
  2221. (equal (concat " abc" org-table-shrunk-column-indicator)
  2222. (org-test-with-temp-text "| <3> |\n| <point>abcd |"
  2223. (org-table-toggle-column-width)
  2224. (overlay-get (car (overlays-at (point))) 'display))))
  2225. (should
  2226. (equal (concat " a " org-table-shrunk-column-indicator)
  2227. (org-test-with-temp-text "| <3> |\n| <point>a |"
  2228. (org-table-toggle-column-width)
  2229. (overlay-get (car (overlays-at (point))) 'display))))
  2230. ;; Only overlay visible characters of the field.
  2231. (should
  2232. (equal (concat " htt" org-table-shrunk-column-indicator)
  2233. (org-test-with-temp-text "| <3> |\n| <point>[[http://orgmode.org]] |"
  2234. (org-table-toggle-column-width)
  2235. (overlay-get (car (overlays-at (point))) 'display))))
  2236. ;; Before the first column or after the last one, ask for columns
  2237. ;; ranges.
  2238. (should
  2239. (catch :exit
  2240. (org-test-with-temp-text "| a |"
  2241. (cl-letf (((symbol-function 'read-string)
  2242. (lambda (&rest_) (throw :exit t))))
  2243. (org-table-toggle-column-width)
  2244. nil))))
  2245. (should
  2246. (catch :exit
  2247. (org-test-with-temp-text "| a |<point>"
  2248. (cl-letf (((symbol-function 'read-string)
  2249. (lambda (&rest_) (throw :exit t))))
  2250. (org-table-toggle-column-width)
  2251. nil))))
  2252. ;; When optional argument ARG is a string, toggle specified columns.
  2253. (should
  2254. (equal org-table-shrunk-column-indicator
  2255. (org-test-with-temp-text "| <point>a | b |"
  2256. (org-table-toggle-column-width "2")
  2257. (overlay-get (car (overlays-at (- (point-max) 2))) 'display))))
  2258. (should
  2259. (equal '("b" "c")
  2260. (org-test-with-temp-text "| a | b | c | d |"
  2261. (org-table-toggle-column-width "2-3")
  2262. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2263. (overlays-in (point-min) (point-max)))
  2264. #'string-lessp))))
  2265. (should
  2266. (equal '("b" "c" "d")
  2267. (org-test-with-temp-text "| a | b | c | d |"
  2268. (org-table-toggle-column-width "2-")
  2269. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2270. (overlays-in (point-min) (point-max)))
  2271. #'string-lessp))))
  2272. (should
  2273. (equal '("a" "b")
  2274. (org-test-with-temp-text "| a | b | c | d |"
  2275. (org-table-toggle-column-width "-2")
  2276. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2277. (overlays-in (point-min) (point-max)))
  2278. #'string-lessp))))
  2279. (should
  2280. (equal '("a" "b" "c" "d")
  2281. (org-test-with-temp-text "| a | b | c | d |"
  2282. (org-table-toggle-column-width "-")
  2283. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2284. (overlays-in (point-min) (point-max)))
  2285. #'string-lessp))))
  2286. (should
  2287. (equal '("a" "d")
  2288. (org-test-with-temp-text "| a | b | c | d |"
  2289. (org-table-toggle-column-width "1-3")
  2290. (org-table-toggle-column-width "2-4")
  2291. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2292. (overlays-in (point-min) (point-max)))
  2293. #'string-lessp))))
  2294. ;; When ARG is (16), remove any column overlay.
  2295. (should-not
  2296. (org-test-with-temp-text "| <point>a |"
  2297. (org-table-toggle-column-width)
  2298. (org-table-toggle-column-width '(16))
  2299. (overlays-in (point-min) (point-max))))
  2300. (should-not
  2301. (org-test-with-temp-text "| a | b | c | d |"
  2302. (org-table-toggle-column-width "-")
  2303. (org-table-toggle-column-width '(16))
  2304. (overlays-in (point-min) (point-max)))))
  2305. (ert-deftest test-org-table/shrunk-columns ()
  2306. "Test behaviour of shrunk column."
  2307. ;; Edition automatically expands a shrunk column.
  2308. (should-not
  2309. (org-test-with-temp-text "| <point>a |"
  2310. (org-table-toggle-column-width)
  2311. (insert "a")
  2312. (overlays-in (point-min) (point-max))))
  2313. ;; Other columns are not changed.
  2314. (should
  2315. (org-test-with-temp-text "| <point>a | b |"
  2316. (org-table-toggle-column-width "-")
  2317. (insert "a")
  2318. (overlays-in (point-min) (point-max))))
  2319. ;; Moving a shrunk column doesn't alter its state.
  2320. (should
  2321. (equal "a"
  2322. (org-test-with-temp-text "| <point>a | b |"
  2323. (org-table-toggle-column-width)
  2324. (org-table-move-column-right)
  2325. (overlay-get (car (overlays-at (point))) 'help-echo))))
  2326. (should
  2327. (equal "a"
  2328. (org-test-with-temp-text "| <point>a |\n| b |"
  2329. (org-table-toggle-column-width)
  2330. (org-table-move-row-down)
  2331. (overlay-get (car (overlays-at (point))) 'help-echo))))
  2332. ;; State is preserved upon inserting a column.
  2333. (should
  2334. (equal '("a")
  2335. (org-test-with-temp-text "| <point>a |"
  2336. (org-table-toggle-column-width)
  2337. (org-table-insert-column)
  2338. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2339. (overlays-in (point-min) (point-max)))
  2340. #'string-lessp))))
  2341. ;; State is preserved upon deleting a column.
  2342. (should
  2343. (equal '("a" "c")
  2344. (org-test-with-temp-text "| a | <point>b | c |"
  2345. (org-table-toggle-column-width "-")
  2346. (org-table-delete-column)
  2347. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2348. (overlays-in (point-min) (point-max)))
  2349. #'string-lessp))))
  2350. ;; State is preserved upon deleting a row.
  2351. (should
  2352. (equal '("b1" "b2")
  2353. (org-test-with-temp-text "| a1 | a2 |\n| b1 | b2 |"
  2354. (org-table-toggle-column-width "-")
  2355. (org-table-kill-row)
  2356. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2357. (overlays-in (point-min) (point-max)))
  2358. #'string-lessp))))
  2359. (should
  2360. (equal '("a1" "a2")
  2361. (org-test-with-temp-text "| a1 | a2 |\n| <point>b1 | b2 |"
  2362. (org-table-toggle-column-width "-")
  2363. (org-table-kill-row)
  2364. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2365. (overlays-in (point-min) (point-max)))
  2366. #'string-lessp))))
  2367. ;; State is preserved upon inserting a row or hline.
  2368. (should
  2369. (equal '("" "a1" "b1")
  2370. (org-test-with-temp-text "| a1 | a2 |\n| <point>b1 | b2 |"
  2371. (org-table-toggle-column-width)
  2372. (org-table-insert-row)
  2373. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2374. (overlays-in (point-min) (point-max)))
  2375. #'string-lessp))))
  2376. (should
  2377. (equal '("a1" "b1")
  2378. (org-test-with-temp-text "| a1 | a2 |\n| <point>b1 | b2 |"
  2379. (org-table-toggle-column-width)
  2380. (org-table-insert-hline)
  2381. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2382. (overlays-in (point-min) (point-max)))
  2383. #'string-lessp))))
  2384. ;; State is preserved upon sorting a column for all the columns but
  2385. ;; the one being sorted.
  2386. (should
  2387. (equal '("a2" "b2")
  2388. (org-test-with-temp-text "| <point>a1 | a2 |\n| <point>b1 | b2 |"
  2389. (org-table-toggle-column-width "-")
  2390. (org-table-sort-lines nil ?A)
  2391. (sort (mapcar (lambda (o) (overlay-get o 'help-echo))
  2392. (overlays-in (point-min) (point-max)))
  2393. #'string-lessp))))
  2394. ;; State is preserved upon replacing a field non-interactively.
  2395. (should
  2396. (equal '("a")
  2397. (org-test-with-temp-text "| <point>a |"
  2398. (org-table-toggle-column-width)
  2399. (org-table-get-field nil "b")
  2400. (mapcar (lambda (o) (overlay-get o 'help-echo))
  2401. (overlays-in (point-min) (point-max)))))))
  2402. ;;; Miscellaneous
  2403. (ert-deftest test-org-table/get-field ()
  2404. "Test `org-table-get-field' specifications."
  2405. ;; Regular test.
  2406. (should
  2407. (equal " a "
  2408. (org-test-with-temp-text "| <point>a |" (org-table-get-field))))
  2409. ;; Get field in open last column.
  2410. (should
  2411. (equal " a "
  2412. (org-test-with-temp-text "| <point>a " (org-table-get-field))))
  2413. ;; Get empty field.
  2414. (should
  2415. (equal ""
  2416. (org-test-with-temp-text "|<point>|" (org-table-get-field))))
  2417. (should
  2418. (equal " "
  2419. (org-test-with-temp-text "| <point>|" (org-table-get-field))))
  2420. ;; Outside the table, return the empty string.
  2421. (should
  2422. (equal ""
  2423. (org-test-with-temp-text "<point>| a |" (org-table-get-field))))
  2424. (should
  2425. (equal ""
  2426. (org-test-with-temp-text "| a |<point>" (org-table-get-field))))
  2427. ;; With optional N argument, select a particular column in current
  2428. ;; row.
  2429. (should
  2430. (equal " 3 "
  2431. (org-test-with-temp-text "| 1 | 2 | 3 |" (org-table-get-field 3))))
  2432. (should
  2433. (equal " 4 "
  2434. (org-test-with-temp-text "| 1 | 2 |\n<point>| 3 | 4 |"
  2435. (org-table-get-field 2))))
  2436. ;; REPLACE optional argument is used to replace selected field.
  2437. (should
  2438. (equal "| foo |"
  2439. (org-test-with-temp-text "| <point>1 |"
  2440. (org-table-get-field nil " foo ")
  2441. (buffer-string))))
  2442. (should
  2443. (equal "| 1 | 2 | foo |"
  2444. (org-test-with-temp-text "| 1 | 2 | 3 |"
  2445. (org-table-get-field 3 " foo ")
  2446. (buffer-string))))
  2447. (should
  2448. (equal " 4 "
  2449. (org-test-with-temp-text "| 1 | 2 |\n<point>| 3 | 4 |"
  2450. (org-table-get-field 2))))
  2451. ;; An empty REPLACE string clears the field.
  2452. (should
  2453. (equal "| |"
  2454. (org-test-with-temp-text "| <point>1 |"
  2455. (org-table-get-field nil "")
  2456. (buffer-string))))
  2457. ;; When using REPLACE still return old value.
  2458. (should
  2459. (equal " 1 "
  2460. (org-test-with-temp-text "| <point>1 |"
  2461. (org-table-get-field nil " foo ")))))
  2462. (provide 'test-org-table)
  2463. ;;; test-org-table.el ends here