test-org-table.el 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  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-mode 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 | replace |
  97. | 4 | replace |
  98. | 8 | replace |
  99. |---------+---------|
  100. | replace | replace |
  101. "
  102. "
  103. | 2 | replace |
  104. | 4 | replace |
  105. | 8 | replace |
  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. (ert-deftest test-org-table/align ()
  143. "Align columns within Org buffer, depends on `org-table-number-regexp'."
  144. (org-test-table-target-expect "
  145. | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
  146. | ab | 12 | 12.2 | 2.4e-08 | 2x10^12 | 4.034+-0.02 | 2.7(10) | >3.5 |
  147. | ab | ab | ab | ab | ab | ab | ab | ab |
  148. ")
  149. (org-test-table-target-expect "
  150. | 0 | 0 | 0 | 0 | 0 | 0 |
  151. | <-0x0ab.cf | >-36#0vw.yz | nan | uinf | -inf | inf |
  152. | ab | ab | ab | ab | ab | ab |
  153. "))
  154. (defconst references/target-normal "
  155. | 0 | 1 | replace | replace | replace | replace | replace | replace |
  156. | z | 1 | replace | replace | replace | replace | replace | replace |
  157. | | 1 | replace | replace | replace | replace | replace | replace |
  158. | | | replace | replace | replace | replace | replace | replace |
  159. "
  160. "Normal numbers and non-numbers for Lisp and Calc formula.")
  161. (defconst references/target-special "
  162. | nan | 1 | replace | replace | replace | replace | replace | replace |
  163. | uinf | 1 | replace | replace | replace | replace | replace | replace |
  164. | -inf | 1 | replace | replace | replace | replace | replace | replace |
  165. | inf | 1 | replace | replace | replace | replace | replace | replace |
  166. "
  167. "Special numbers for Calc formula.")
  168. (ert-deftest test-org-table/references/mode-string-EL ()
  169. "Basic: Assign field reference, sum of field references, sum
  170. and len of simple range reference (no row) and complex range
  171. reference (with row). Mode string EL."
  172. ;; Empty fields are kept during parsing field but lost as list
  173. ;; elements within Lisp formula syntactically when used literally
  174. ;; and not enclosed with " within fields, see last columns with len.
  175. (org-test-table-target-expect
  176. references/target-normal
  177. ;; All the #ERROR show that for Lisp calculations N has to be used.
  178. "
  179. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  180. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  181. | | 1 | | 1 | 1 | 1 | 1 | 1 |
  182. | | | | 0 | 0 | 0 | 0 | 0 |
  183. "
  184. 1 (concat
  185. "#+TBLFM: $3 = '(identity \"$1\"); EL :: $4 = '(+ $1 $2); EL :: "
  186. "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
  187. "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL"))
  188. ;; Empty fields are kept during parsing field _and_ as list elements
  189. ;; within Lisp formula syntactically even when used literally when
  190. ;; enclosed with " within fields, see last columns with len.
  191. (org-test-table-target-expect
  192. "
  193. | \"0\" | \"1\" | repl | repl | repl | repl | repl | repl |
  194. | \"z\" | \"1\" | repl | repl | repl | repl | repl | repl |
  195. | \"\" | \"1\" | repl | repl | repl | repl | repl | repl |
  196. | \"\" | \"\" | repl | repl | repl | repl | repl | repl |
  197. "
  198. "
  199. | \"0\" | \"1\" | \"0\" | 1 | #ERROR | #ERROR | 2 | 2 |
  200. | \"z\" | \"1\" | \"z\" | 1 | #ERROR | #ERROR | 2 | 2 |
  201. | \"\" | \"1\" | \"\" | 1 | #ERROR | #ERROR | 2 | 2 |
  202. | \"\" | \"\" | \"\" | 0 | #ERROR | #ERROR | 2 | 2 |
  203. "
  204. 1 (concat
  205. "#+TBLFM: $3 = '(concat \"\\\"\" $1 \"\\\"\"); EL :: "
  206. "$4 = '(+ (string-to-number $1) (string-to-number $2)); EL :: "
  207. "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
  208. "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL")))
  209. (ert-deftest test-org-table/references/mode-string-E ()
  210. "Basic: Assign field reference, sum of field references, sum
  211. and len of simple range reference (no row) and complex range
  212. reference (with row). Mode string E."
  213. (let ((lisp
  214. (concat
  215. "#+TBLFM: $3 = '(identity $1); E :: $4 = '(+ $1 $2); E :: "
  216. "$5 = '(+ $1..$2); E :: $6 = '(+ @0$1..@0$2); E :: "
  217. "$7 = '(length '($1..$2)); E :: $8 = '(length '(@0$1..@0$2)); E"))
  218. (calc
  219. (concat
  220. "#+TBLFM: $3 = $1; E :: $4 = $1 + $2; E :: "
  221. "$5 = vsum($1..$2); E :: $6 = vsum(@0$1..@0$2); E :: "
  222. "$7 = vlen($1..$2); E :: $8 = vlen(@0$1..@0$2); E")))
  223. (org-test-table-target-expect
  224. references/target-normal
  225. ;; All the #ERROR show that for Lisp calculations N has to be used.
  226. "
  227. | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
  228. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  229. | | 1 | | #ERROR | #ERROR | #ERROR | 2 | 2 |
  230. | | | | #ERROR | #ERROR | #ERROR | 2 | 2 |
  231. "
  232. 1 lisp)
  233. (org-test-table-target-expect
  234. references/target-normal
  235. "
  236. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  237. | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
  238. | | 1 | nan | nan | nan | nan | 2 | 2 |
  239. | | | nan | nan | nan | nan | 2 | 2 |
  240. "
  241. 1 calc)
  242. (org-test-table-target-expect
  243. references/target-special
  244. "
  245. | nan | 1 | nan | nan | nan | nan | 2 | 2 |
  246. | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
  247. | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
  248. | inf | 1 | inf | inf | inf | inf | 2 | 2 |
  249. "
  250. 1 calc)))
  251. (ert-deftest test-org-table/references/mode-string-EN ()
  252. "Basic: Assign field reference, sum of field references, sum
  253. and len of simple range reference (no row) and complex range
  254. reference (with row). Mode string EN."
  255. (let ((lisp (concat
  256. "#+TBLFM: $3 = '(identity $1); EN :: $4 = '(+ $1 $2); EN :: "
  257. "$5 = '(+ $1..$2); EN :: $6 = '(+ @0$1..@0$2); EN :: "
  258. "$7 = '(length '($1..$2)); EN :: "
  259. "$8 = '(length '(@0$1..@0$2)); EN"))
  260. (calc (concat
  261. "#+TBLFM: $3 = $1; EN :: $4 = $1 + $2; EN :: "
  262. "$5 = vsum($1..$2); EN :: $6 = vsum(@0$1..@0$2); EN :: "
  263. "$7 = vlen($1..$2); EN :: $8 = vlen(@0$1..@0$2); EN")))
  264. (org-test-table-target-expect
  265. references/target-normal
  266. "
  267. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  268. | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  269. | | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  270. | | | 0 | 0 | 0 | 0 | 2 | 2 |
  271. "
  272. 1 lisp calc)
  273. (org-test-table-target-expect
  274. references/target-special
  275. "
  276. | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  277. | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  278. | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  279. | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  280. "
  281. 1 calc)))
  282. (ert-deftest test-org-table/references/mode-string-L ()
  283. "Basic: Assign field reference, sum of field references, sum
  284. and len of simple range reference (no row) and complex range
  285. reference (with row). Mode string L."
  286. (org-test-table-target-expect
  287. references/target-normal
  288. ;; All the #ERROR show that for Lisp calculations N has to be used.
  289. "
  290. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  291. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  292. | | 1 | | 1 | 1 | 1 | 1 | 1 |
  293. | | | | 0 | 0 | 0 | 0 | 0 |
  294. "
  295. 1 (concat
  296. "#+TBLFM: $3 = '(identity \"$1\"); L :: $4 = '(+ $1 $2); L :: "
  297. "$5 = '(+ $1..$2); L :: $6 = '(+ @0$1..@0$2); L :: "
  298. "$7 = '(length '($1..$2)); L :: $8 = '(length '(@0$1..@0$2)); L")))
  299. (ert-deftest test-org-table/references/mode-string-none ()
  300. "Basic: Assign field reference, sum of field references, sum
  301. and len of simple range reference (no row) and complex range
  302. reference (with row). No mode string."
  303. (let ((lisp (concat
  304. "#+TBLFM: $3 = '(identity $1) :: $4 = '(+ $1 $2) :: "
  305. "$5 = '(+ $1..$2) :: $6 = '(+ @0$1..@0$2) :: "
  306. "$7 = '(length '($1..$2)) :: $8 = '(length '(@0$1..@0$2))"))
  307. (calc (concat
  308. "#+TBLFM: $3 = $1 :: $4 = $1 + $2 :: "
  309. "$5 = vsum($1..$2) :: $6 = vsum(@0$1..@0$2) :: "
  310. "$7 = vlen($1..$2) :: $8 = vlen(@0$1..@0$2)")))
  311. (org-test-table-target-expect
  312. references/target-normal
  313. ;; All the #ERROR show that for Lisp calculations N has to be used.
  314. "
  315. | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
  316. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  317. | | 1 | | #ERROR | #ERROR | #ERROR | 1 | 1 |
  318. | | | | #ERROR | 0 | 0 | 0 | 0 |
  319. "
  320. 1 lisp)
  321. (org-test-table-target-expect
  322. references/target-normal
  323. "
  324. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  325. | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
  326. | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
  327. | | | 0 | 0 | 0 | 0 | 0 | 0 |
  328. "
  329. 1 calc)
  330. (org-test-table-target-expect
  331. references/target-special
  332. "
  333. | nan | 1 | nan | nan | nan | nan | 2 | 2 |
  334. | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
  335. | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
  336. | inf | 1 | inf | inf | inf | inf | 2 | 2 |
  337. "
  338. 1 calc)))
  339. (ert-deftest test-org-table/references/mode-string-N ()
  340. "Basic: Assign field reference, sum of field references, sum
  341. and len of simple range reference (no row) and complex range
  342. reference (with row). Mode string N."
  343. (let ((lisp
  344. (concat
  345. "#+TBLFM: $3 = '(identity $1); N :: $4 = '(+ $1 $2); N :: "
  346. "$5 = '(+ $1..$2); N :: $6 = '(+ @0$1..@0$2); N :: "
  347. "$7 = '(length '($1..$2)); N :: $8 = '(length '(@0$1..@0$2)); N"))
  348. (calc
  349. (concat
  350. "#+TBLFM: $3 = $1; N :: $4 = $1 + $2; N :: "
  351. "$5 = vsum($1..$2); N :: $6 = vsum(@0$1..@0$2); N :: "
  352. "$7 = vlen($1..$2); N :: $8 = vlen(@0$1..@0$2); N")))
  353. (org-test-table-target-expect
  354. references/target-normal
  355. "
  356. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  357. | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  358. | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
  359. | | | 0 | 0 | 0 | 0 | 0 | 0 |
  360. "
  361. 1 lisp calc)
  362. (org-test-table-target-expect
  363. references/target-special
  364. "
  365. | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  366. | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  367. | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  368. | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  369. "
  370. 1 calc)))
  371. (ert-deftest test-org-table/lisp-return-value ()
  372. "Basic: Return value of Lisp formulas."
  373. (org-test-table-target-expect
  374. "
  375. | | nil | (list) | '() |
  376. |-------------------------+-------------+--------+-----|
  377. | type-of, no L | replace (r) | r | r |
  378. | type-of identity, no L | r | r | r |
  379. | identity, no L | r | r | r |
  380. |-------------------------+-------------+--------+-----|
  381. | type-of \"@1\" | r | r | r |
  382. | type-of (identity \"@1\") | r | r | r |
  383. | identity \"@1\" | r | r | r |
  384. |-------------------------+-------------+--------+-----|
  385. | type-of @1 | r | r | r |
  386. | type-of (identity @1) | r | r | r |
  387. | identity @1 | r | r | r |
  388. "
  389. "
  390. | | nil | (list) | '() |
  391. |-------------------------+--------+--------+--------|
  392. | type-of, no L | string | string | string |
  393. | type-of identity, no L | string | string | string |
  394. | identity, no L | nil | (list) | '() |
  395. |-------------------------+--------+--------+--------|
  396. | type-of \"@1\" | string | string | string |
  397. | type-of (identity \"@1\") | string | string | string |
  398. | identity \"@1\" | nil | (list) | '() |
  399. |-------------------------+--------+--------+--------|
  400. | type-of @1 | symbol | symbol | symbol |
  401. | type-of (identity @1) | symbol | symbol | symbol |
  402. | identity @1 | nil | nil | nil |
  403. "
  404. 1 (concat "#+TBLFM: @2$<<..@2$> = '(type-of @1) :: "
  405. "@3$<<..@3$> = '(type-of (identity @1)) :: "
  406. "@4$<<..@4$> = '(identity @1) :: @5$<<..@>$> = '(@0$1); L")))
  407. (ert-deftest test-org-table/compare ()
  408. "Basic: Compare field references in Calc."
  409. (org-test-table-target-expect
  410. "
  411. | | 0 | z | | nan | uinf | -inf | inf |
  412. |------+------+------+------+------+------+------+------|
  413. | 0 | repl | repl | repl | repl | repl | repl | repl |
  414. | z | repl | repl | repl | repl | repl | repl | repl |
  415. | | repl | repl | repl | repl | repl | repl | repl |
  416. | nan | repl | repl | repl | repl | repl | repl | repl |
  417. | uinf | repl | repl | repl | repl | repl | repl | repl |
  418. | -inf | repl | repl | repl | repl | repl | repl | repl |
  419. | inf | repl | repl | repl | repl | repl | repl | repl |
  420. "
  421. "
  422. | | 0 | z | | nan | uinf | -inf | inf |
  423. |------+---+---+---+-----+------+------+-----|
  424. | 0 | x | | | | | | |
  425. | z | | x | | | | | |
  426. | | | | x | | | | |
  427. | nan | | | | x | | | |
  428. | uinf | | | | | x | | |
  429. | -inf | | | | | | x | |
  430. | inf | | | | | | | x |
  431. "
  432. 1
  433. ;; Compare field reference ($1) with field reference (@1)
  434. "#+TBLFM: @<<$<<..@>$> = if(\"$1\" == \"@1\", x, string(\"\")); E"
  435. ;; Compare field reference ($1) with absolute term
  436. (concat "#+TBLFM: "
  437. "$2 = if(\"$1\" == \"(0)\" , x, string(\"\")); E :: "
  438. "$3 = if(\"$1\" == \"(z)\" , x, string(\"\")); E :: "
  439. "$4 = if(\"$1\" == \"nan\" , x, string(\"\")); E :: "
  440. "$5 = if(\"$1\" == \"(nan)\" , x, string(\"\")); E :: "
  441. "$6 = if(\"$1\" == \"(uinf)\", x, string(\"\")); E :: "
  442. "$7 = if(\"$1\" == \"(-inf)\", x, string(\"\")); E :: "
  443. "$8 = if(\"$1\" == \"(inf)\" , x, string(\"\")); E"))
  444. ;; Check field reference converted from an empty field: Despite this
  445. ;; field reference will not end up in a result, Calc evaluates it.
  446. ;; Make sure that also then there is no Calc error.
  447. (org-test-table-target-expect
  448. "
  449. | 0 | replace |
  450. | z | replace |
  451. | | replace |
  452. | nan | replace |
  453. "
  454. "
  455. | 0 | 1 |
  456. | z | z + 1 |
  457. | | |
  458. | nan | nan |
  459. "
  460. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"))
  461. (ert-deftest test-org-table/empty-field ()
  462. "Examples how to deal with empty fields."
  463. ;; Test if one field is empty, else do a calculation
  464. (org-test-table-target-expect
  465. "
  466. | -1 | replace |
  467. | 0 | replace |
  468. | | replace |
  469. "
  470. "
  471. | -1 | 0 |
  472. | 0 | 1 |
  473. | | |
  474. "
  475. 1
  476. ;; Calc formula
  477. "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"
  478. ;; Lisp formula
  479. "#+TBLFM: $2 = '(if (eq \"$1\" \"\") \"\" (1+ $1)); L")
  480. ;; Test if several fields are empty, else do a calculation
  481. (org-test-table-target-expect
  482. "
  483. | 1 | 2 | replace |
  484. | 4 | | replace |
  485. | | 8 | replace |
  486. | | | replace |
  487. "
  488. "
  489. | 1 | 2 | 3 |
  490. | 4 | | |
  491. | | 8 | |
  492. | | | |
  493. "
  494. 1
  495. ;; Calc formula
  496. (concat "#+TBLFM: $3 = if(\"$1\" == \"nan\" || \"$2\" == \"nan\", "
  497. "string(\"\"), $1 + $2); E")
  498. ;; Lisp formula
  499. (concat "#+TBLFM: $3 = '(if (or (eq \"$1\" \"\") (eq \"$2\" \"\")) "
  500. "\"\" (+ $1 $2)); L"))
  501. ;; $2: Use $1 + 0.5 if $1 available, else only reformat $2 if $2 available
  502. (org-test-table-target-expect
  503. "
  504. | 1.5 | 0 |
  505. | 3.5 | |
  506. | | 5 |
  507. | | |
  508. "
  509. "
  510. | 1.5 | 2.0 |
  511. | 3.5 | 4.0 |
  512. | | 5.0 |
  513. | | |
  514. "
  515. 1
  516. ;; Calc formula
  517. (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  518. "if(\"$2\" == \"nan\", string(\"\"), $2 +.0), $1 + 0.5); E f-1")
  519. ;; Lisp formula not implemented yet
  520. )
  521. ;; Empty fields in simple and complex range reference
  522. (org-test-table-target-expect
  523. "
  524. | | | | | repl | repl | repl | repl | repl | repl |
  525. | | | 5 | 7 | repl | repl | repl | repl | repl | repl |
  526. | 1 | 3 | 5 | 7 | repl | repl | repl | repl | repl | repl |
  527. "
  528. "
  529. | | | | | | | | | 0 | 0 |
  530. | | | 5 | 7 | | | 6 | 6 | 3 | 3 |
  531. | 1 | 3 | 5 | 7 | 4 | 4 | 4 | 4 | 4 | 4 |
  532. "
  533. 1
  534. ;; Calc formula
  535. (concat
  536. "#+TBLFM: "
  537. "$5 = if(typeof(vmean($1..$4)) == 12, "
  538. "string(\"\"), vmean($1..$4)); E :: "
  539. "$6 = if(typeof(vmean(@0$1..@0$4)) == 12, "
  540. "string(\"\"), vmean(@0$1..@0$4)); E :: "
  541. "$7 = if(\"$1..$4\" == \"[]\", string(\"\"), vmean($1..$4)) :: "
  542. "$8 = if(\"@0$1..@0$4\" == \"[]\", string(\"\"), vmean(@0$1..@0$4)) :: "
  543. "$9 = vmean($1..$4); EN :: "
  544. "$10 = vmean(@0$1..@0$4); EN")
  545. ;; Lisp formula
  546. (concat
  547. "#+TBLFM: "
  548. "$5 = '(let ((l '($1..$4))) (if (member \"\" l) \"\" "
  549. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  550. "$6 = '(let ((l '(@0$1..@0$4))) (if (member \"\" l) \"\" "
  551. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  552. "$7 = '(let ((l '($1..$4))) "
  553. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  554. "$8 = '(let ((l '(@0$1..@0$4))) "
  555. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  556. "$9 = '(/ (+ $1..$4) (length '($1..$4))); EN :: "
  557. "$10 = '(/ (+ @0$1..@0$4) (length '(@0$1..@0$4))); EN")
  558. ))
  559. (ert-deftest test-org-table/copy-field ()
  560. "Experiments on how to copy one field into another field.
  561. See also `test-org-table/remote-reference-access'."
  562. (let ((target "
  563. | 0 | replace |
  564. | a b | replace |
  565. | c d | replace |
  566. | | replace |
  567. | 2012-12 | replace |
  568. | [2012-12-31 Mon] | replace |
  569. "))
  570. ;; Lisp formula to copy 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 "#+TBLFM: $2 = '(identity $1)")
  582. ;; Calc formula to copy quite literally
  583. (org-test-table-target-expect
  584. target
  585. "
  586. | 0 | 0 |
  587. | a b | a b |
  588. | c d | c d |
  589. | | |
  590. | 2012-12 | 2012-12 |
  591. | [2012-12-31 Mon] | <2012-12-31 Mon> |
  592. "
  593. 1 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  594. "string(\"\"), string(subvec(\"$1\", 2, vlen(\"$1\")))); E"))
  595. ;; Calc formula simple
  596. (org-test-table-target-expect
  597. target
  598. "
  599. | 0 | 0 |
  600. | a b | a b |
  601. | c d | c d |
  602. | | |
  603. | 2012-12 | 2000 |
  604. | [2012-12-31 Mon] | <2012-12-31 Mon> |
  605. "
  606. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1); E")))
  607. (ert-deftest test-org-table/sub-total ()
  608. "Grouped rows with sub-total.
  609. Begin range with \"@II\" to handle multiline header. Convert
  610. integer to float with \"+.0\" for sub-total of items c1 and c2.
  611. Sum empty fields as value zero but without ignoring them for
  612. \"vlen\" with format specifier \"EN\". Format possibly empty
  613. results with the Calc formatter \"f-1\" instead of the printf
  614. formatter \"%.1f\"."
  615. (org-test-table-target-expect
  616. "
  617. |-------+---------+---------|
  618. | Item | Item | Sub- |
  619. | name | value | total |
  620. |-------+---------+---------|
  621. | a1 | 4.1 | replace |
  622. | a2 | 8.2 | replace |
  623. | a3 | | replace |
  624. |-------+---------+---------|
  625. | b1 | 16.0 | replace |
  626. |-------+---------+---------|
  627. | c1 | 32 | replace |
  628. | c2 | 64 | replace |
  629. |-------+---------+---------|
  630. | Total | replace | replace |
  631. |-------+---------+---------|
  632. "
  633. "
  634. |-------+-------+-------|
  635. | Item | Item | Sub- |
  636. | name | value | total |
  637. |-------+-------+-------|
  638. | a1 | 4.1 | |
  639. | a2 | 8.2 | |
  640. | a3 | | 12.3 |
  641. |-------+-------+-------|
  642. | b1 | 16.0 | 16.0 |
  643. |-------+-------+-------|
  644. | c1 | 32 | |
  645. | c2 | 64 | 96.0 |
  646. |-------+-------+-------|
  647. | Total | 124.3 | |
  648. |-------+-------+-------|
  649. "
  650. 1 (concat "#+TBLFM: @>$2 = vsum(@II..@>>) ::"
  651. "$3 = if(vlen(@0..@+I) == 1, "
  652. "vsum(@-I$2..@+I$2) +.0, string(\"\")); EN f-1 :: "
  653. "@>$3 = string(\"\")")))
  654. (ert-deftest test-org-table/org-lookup-all ()
  655. "Use `org-lookup-all' for several GROUP BY as in SQL and for ranking.
  656. See also http://orgmode.org/worg/org-tutorials/org-lookups.html ."
  657. (let ((data "
  658. #+NAME: data
  659. | Purchase | Product | Shop | Rating |
  660. |----------+---------+------+--------|
  661. | a | p1 | s1 | 1 |
  662. | b | p1 | s2 | 4 |
  663. | c | p2 | s1 | 2 |
  664. | d | p3 | s2 | 8 |
  665. "))
  666. ;; Product rating and ranking by average purchase from "#+NAME: data"
  667. (org-test-table-target-expect
  668. (concat data "
  669. | Product | Rating | Ranking |
  670. |---------+---------+---------|
  671. | p1 | replace | replace |
  672. | p2 | replace | replace |
  673. | p3 | replace | replace |
  674. ")
  675. (concat data "
  676. | Product | Rating | Ranking |
  677. |---------+--------+---------|
  678. | p1 | 2.5 | 2 |
  679. | p2 | 2.0 | 3 |
  680. | p3 | 8.0 | 1 |
  681. ")
  682. 2 (concat
  683. "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
  684. "'(remote(data, @I$2..@>$2)) '(remote(data, @I$4..@>$4))))) "
  685. "(/ (apply '+ all) (length all) 1.0)); L :: "
  686. "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))
  687. ;; Shop rating and ranking by average purchase from "#+NAME: data"
  688. (org-test-table-target-expect
  689. (concat data "
  690. | Shop | Rating | Ranking |
  691. |------+---------+---------|
  692. | s1 | replace | replace |
  693. | s2 | replace | replace |
  694. ")
  695. (concat data "
  696. | Shop | Rating | Ranking |
  697. |------+--------+---------|
  698. | s1 | 1.5 | 2 |
  699. | s2 | 6.0 | 1 |
  700. ")
  701. 2 (concat
  702. "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
  703. "'(remote(data, @I$3..@>$3)) '(remote(data, @I$4..@>$4))))) "
  704. "(/ (apply '+ all) (length all) 1.0)); L :: "
  705. "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))))
  706. (ert-deftest test-org-table/org-table-make-reference/mode-string-EL ()
  707. (fset 'f 'org-table-make-reference)
  708. ;; For Lisp formula only
  709. (should (equal "0" (f "0" t nil 'literal)))
  710. (should (equal "z" (f "z" t nil 'literal)))
  711. (should (equal "" (f "" t nil 'literal)))
  712. (should (equal "0 1" (f '("0" "1") t nil 'literal)))
  713. (should (equal "z 1" (f '("z" "1") t nil 'literal)))
  714. (should (equal " 1" (f '("" "1") t nil 'literal)))
  715. (should (equal " " (f '("" "" ) t nil 'literal))))
  716. (ert-deftest test-org-table/org-table-make-reference/mode-string-E ()
  717. (fset 'f 'org-table-make-reference)
  718. ;; For Lisp formula
  719. (should (equal "\"0\"" (f "0" t nil t)))
  720. (should (equal "\"z\"" (f "z" t nil t)))
  721. (should (equal "\"\"" (f "" t nil t)))
  722. (should (equal "\"0\" \"1\"" (f '("0" "1") t nil t)))
  723. (should (equal "\"z\" \"1\"" (f '("z" "1") t nil t)))
  724. (should (equal "\"\" \"1\"" (f '("" "1") t nil t)))
  725. (should (equal "\"\" \"\"" (f '("" "" ) t nil t)))
  726. ;; For Calc formula
  727. (should (equal "(0)" (f "0" t nil nil)))
  728. (should (equal "(z)" (f "z" t nil nil)))
  729. (should (equal "nan" (f "" t nil nil)))
  730. (should (equal "[0,1]" (f '("0" "1") t nil nil)))
  731. (should (equal "[z,1]" (f '("z" "1") t nil nil)))
  732. (should (equal "[nan,1]" (f '("" "1") t nil nil)))
  733. (should (equal "[nan,nan]" (f '("" "" ) t nil nil)))
  734. ;; For Calc formula, special numbers
  735. (should (equal "(nan)" (f "nan" t nil nil)))
  736. (should (equal "(uinf)" (f "uinf" t nil nil)))
  737. (should (equal "(-inf)" (f "-inf" t nil nil)))
  738. (should (equal "(inf)" (f "inf" t nil nil)))
  739. (should (equal "[nan,1]" (f '( "nan" "1") t nil nil)))
  740. (should (equal "[uinf,1]" (f '("uinf" "1") t nil nil)))
  741. (should (equal "[-inf,1]" (f '("-inf" "1") t nil nil)))
  742. (should (equal "[inf,1]" (f '( "inf" "1") t nil nil))))
  743. (ert-deftest test-org-table/org-table-make-reference/mode-string-EN ()
  744. (fset 'f 'org-table-make-reference)
  745. ;; For Lisp formula
  746. (should (equal "0" (f "0" t t t)))
  747. (should (equal "0" (f "z" t t t)))
  748. (should (equal "0" (f "" t t t)))
  749. (should (equal "0 1" (f '("0" "1") t t t)))
  750. (should (equal "0 1" (f '("z" "1") t t t)))
  751. (should (equal "0 1" (f '("" "1") t t t)))
  752. (should (equal "0 0" (f '("" "" ) t t t)))
  753. ;; For Calc formula
  754. (should (equal "(0)" (f "0" t t nil)))
  755. (should (equal "(0)" (f "z" t t nil)))
  756. (should (equal "(0)" (f "" t t nil)))
  757. (should (equal "[0,1]" (f '("0" "1") t t nil)))
  758. (should (equal "[0,1]" (f '("z" "1") t t nil)))
  759. (should (equal "[0,1]" (f '("" "1") t t nil)))
  760. (should (equal "[0,0]" (f '("" "" ) t t nil)))
  761. ;; For Calc formula, special numbers
  762. (should (equal "(0)" (f "nan" t t nil)))
  763. (should (equal "(0)" (f "uinf" t t nil)))
  764. (should (equal "(0)" (f "-inf" t t nil)))
  765. (should (equal "(0)" (f "inf" t t nil)))
  766. (should (equal "[0,1]" (f '( "nan" "1") t t nil)))
  767. (should (equal "[0,1]" (f '("uinf" "1") t t nil)))
  768. (should (equal "[0,1]" (f '("-inf" "1") t t nil)))
  769. (should (equal "[0,1]" (f '( "inf" "1") t t nil))))
  770. (ert-deftest test-org-table/org-table-make-reference/mode-string-L ()
  771. (fset 'f 'org-table-make-reference)
  772. ;; For Lisp formula only
  773. (should (equal "0" (f "0" nil nil 'literal)))
  774. (should (equal "z" (f "z" nil nil 'literal)))
  775. (should (equal "" (f "" nil nil 'literal)))
  776. (should (equal "0 1" (f '("0" "1") nil nil 'literal)))
  777. (should (equal "z 1" (f '("z" "1") nil nil 'literal)))
  778. (should (equal "1" (f '("" "1") nil nil 'literal)))
  779. (should (equal "" (f '("" "" ) nil nil 'literal))))
  780. (ert-deftest test-org-table/org-table-make-reference/mode-string-none ()
  781. (fset 'f 'org-table-make-reference)
  782. ;; For Lisp formula
  783. (should (equal "\"0\"" (f "0" nil nil t)))
  784. (should (equal "\"z\"" (f "z" nil nil t)))
  785. (should (equal "" (f "" nil nil t)))
  786. (should (equal "\"0\" \"1\"" (f '("0" "1") nil nil t)))
  787. (should (equal "\"z\" \"1\"" (f '("z" "1") nil nil t)))
  788. (should (equal "\"1\"" (f '("" "1") nil nil t)))
  789. (should (equal "" (f '("" "" ) nil nil t)))
  790. ;; For Calc formula
  791. (should (equal "(0)" (f "0" nil nil nil)))
  792. (should (equal "(z)" (f "z" nil nil nil)))
  793. (should (equal "(0)" (f "" nil nil nil)))
  794. (should (equal "[0,1]" (f '("0" "1") nil nil nil)))
  795. (should (equal "[z,1]" (f '("z" "1") nil nil nil)))
  796. (should (equal "[1]" (f '("" "1") nil nil nil)))
  797. (should (equal "[]" (f '("" "" ) nil nil nil)))
  798. ;; For Calc formula, special numbers
  799. (should (equal "(nan)" (f "nan" nil nil nil)))
  800. (should (equal "(uinf)" (f "uinf" nil nil nil)))
  801. (should (equal "(-inf)" (f "-inf" nil nil nil)))
  802. (should (equal "(inf)" (f "inf" nil nil nil)))
  803. (should (equal "[nan,1]" (f '( "nan" "1") nil nil nil)))
  804. (should (equal "[uinf,1]" (f '("uinf" "1") nil nil nil)))
  805. (should (equal "[-inf,1]" (f '("-inf" "1") nil nil nil)))
  806. (should (equal "[inf,1]" (f '( "inf" "1") nil nil nil))))
  807. (ert-deftest test-org-table/org-table-make-reference/mode-string-N ()
  808. (fset 'f 'org-table-make-reference)
  809. ;; For Lisp formula
  810. (should (equal "0" (f "0" nil t t)))
  811. (should (equal "0" (f "z" nil t t)))
  812. (should (equal "" (f "" nil t t)))
  813. (should (equal "0 1" (f '("0" "1") nil t t)))
  814. (should (equal "0 1" (f '("z" "1") nil t t)))
  815. (should (equal "1" (f '("" "1") nil t t)))
  816. (should (equal "" (f '("" "" ) nil t t)))
  817. ;; For Calc formula
  818. (should (equal "(0)" (f "0" nil t nil)))
  819. (should (equal "(0)" (f "z" nil t nil)))
  820. (should (equal "(0)" (f "" nil t nil)))
  821. (should (equal "[0,1]" (f '("0" "1") nil t nil)))
  822. (should (equal "[0,1]" (f '("z" "1") nil t nil)))
  823. (should (equal "[1]" (f '("" "1") nil t nil)))
  824. (should (equal "[]" (f '("" "" ) nil t nil)))
  825. ;; For Calc formula, special numbers
  826. (should (equal "(0)" (f "nan" nil t nil)))
  827. (should (equal "(0)" (f "uinf" nil t nil)))
  828. (should (equal "(0)" (f "-inf" nil t nil)))
  829. (should (equal "(0)" (f "inf" nil t nil)))
  830. (should (equal "[0,1]" (f '( "nan" "1") nil t nil)))
  831. (should (equal "[0,1]" (f '("uinf" "1") nil t nil)))
  832. (should (equal "[0,1]" (f '("-inf" "1") nil t nil)))
  833. (should (equal "[0,1]" (f '( "inf" "1") nil t nil))))
  834. (ert-deftest test-org-table/org-table-convert-refs-to-an/1 ()
  835. "Simple reference @1$1."
  836. (should
  837. (string= "A1" (org-table-convert-refs-to-an "@1$1"))))
  838. ;; TODO: Test broken
  839. ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
  840. ;; "Self reference @1$1."
  841. ;; (should
  842. ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
  843. (ert-deftest test-org-table/org-table-convert-refs-to-an/3 ()
  844. "Remote reference."
  845. (should
  846. (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
  847. (ert-deftest test-org-table/org-table-convert-refs-to-rc/1 ()
  848. "Simple reference @1$1."
  849. (should
  850. (string= "@1$1" (org-table-convert-refs-to-rc "A1"))))
  851. (ert-deftest test-org-table/org-table-convert-refs-to-rc/2 ()
  852. "Self reference $0."
  853. (should
  854. (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
  855. ;; TODO: Test Broken
  856. ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
  857. ;; "Remote reference."
  858. ;; (should
  859. ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
  860. (ert-deftest test-org-table/remote-reference-access ()
  861. "Access to remote reference.
  862. See also `test-org-table/copy-field'."
  863. (org-test-table-target-expect
  864. "
  865. #+NAME: table
  866. | | x 42 | |
  867. | replace | replace |
  868. "
  869. "
  870. #+NAME: table
  871. | | x 42 | |
  872. | x 42 | 84 x |
  873. "
  874. 1 (concat "#+TBLFM: "
  875. ;; Copy text without calculation: Use Lisp formula
  876. "$1 = '(identity remote(table, @1$2)) :: "
  877. ;; Do a calculation: Use Calc (or Lisp ) formula
  878. "$2 = 2 * remote(table, @1$2)")))
  879. (ert-deftest test-org-table/remote-reference-indirect ()
  880. "Access to remote reference with indirection of name or ID."
  881. (let ((source-tables "
  882. #+NAME: 2012
  883. | amount |
  884. |--------|
  885. | 1 |
  886. | 2 |
  887. |--------|
  888. | 3 |
  889. #+TBLFM: @>$1 = vsum(@I..@II)
  890. #+NAME: 2013
  891. | amount |
  892. |--------|
  893. | 4 |
  894. | 8 |
  895. |--------|
  896. | 12 |
  897. #+TBLFM: @>$1 = vsum(@I..@II)
  898. "))
  899. ;; Read several remote references from same column
  900. (org-test-table-target-expect
  901. (concat source-tables "
  902. #+NAME: summary
  903. | year | amount |
  904. |-------+---------|
  905. | 2012 | replace |
  906. | 2013 | replace |
  907. |-------+---------|
  908. | total | replace |
  909. ")
  910. (concat source-tables "
  911. #+NAME: summary
  912. | year | amount |
  913. |-------+--------|
  914. | 2012 | 3 |
  915. | 2013 | 12 |
  916. |-------+--------|
  917. | total | 15 |
  918. ")
  919. 1
  920. ;; Calc formula
  921. "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
  922. ;; Lisp formula
  923. (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
  924. "@>$2 = '(+ @I..@II); N"))
  925. ;; Read several remote references from same row
  926. (org-test-table-target-expect
  927. (concat source-tables "
  928. #+NAME: summary
  929. | year | 2012 | 2013 | total |
  930. |--------+---------+---------+---------|
  931. | amount | replace | replace | replace |
  932. ")
  933. (concat source-tables "
  934. #+NAME: summary
  935. | year | 2012 | 2013 | total |
  936. |--------+------+------+-------|
  937. | amount | 3 | 12 | 15 |
  938. ")
  939. 1
  940. ;; Calc formula
  941. "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
  942. ;; Lisp formula
  943. (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
  944. "@2$> = '(+ $<<..$>>); N"))))
  945. (ert-deftest test-org-table/org-at-TBLFM-p ()
  946. (org-test-with-temp-text-in-file
  947. "
  948. | 1 |
  949. | 2 |
  950. #+TBLFM: $2=$1*2
  951. "
  952. (goto-char (point-min))
  953. (forward-line 2)
  954. (should (equal (org-at-TBLFM-p) nil))
  955. (goto-char (point-min))
  956. (forward-line 3)
  957. (should (equal (org-at-TBLFM-p) t))
  958. (goto-char (point-min))
  959. (forward-line 4)
  960. (should (equal (org-at-TBLFM-p) nil))))
  961. (ert-deftest test-org-table/org-table-TBLFM-begin ()
  962. (org-test-with-temp-text-in-file
  963. "
  964. | 1 |
  965. | 2 |
  966. #+TBLFM: $2=$1*2
  967. "
  968. (goto-char (point-min))
  969. (should (equal (org-table-TBLFM-begin)
  970. nil))
  971. (goto-char (point-min))
  972. (forward-line 1)
  973. (should (equal (org-table-TBLFM-begin)
  974. nil))
  975. (goto-char (point-min))
  976. (forward-line 3)
  977. (should (= (org-table-TBLFM-begin)
  978. 14))
  979. (goto-char (point-min))
  980. (forward-line 4)
  981. (should (= (org-table-TBLFM-begin)
  982. 14))
  983. ))
  984. (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
  985. "For multiple #+TBLFM lines."
  986. (org-test-with-temp-text-in-file
  987. "
  988. | 1 |
  989. | 2 |
  990. #+TBLFM: $2=$1*1
  991. #+TBLFM: $2=$1*2
  992. "
  993. (goto-char (point-min))
  994. (should (equal (org-table-TBLFM-begin)
  995. nil))
  996. (goto-char (point-min))
  997. (forward-line 1)
  998. (should (equal (org-table-TBLFM-begin)
  999. nil))
  1000. (goto-char (point-min))
  1001. (forward-line 3)
  1002. (should (= (org-table-TBLFM-begin)
  1003. 14))
  1004. (goto-char (point-min))
  1005. (forward-line 4)
  1006. (should (= (org-table-TBLFM-begin)
  1007. 14))
  1008. (goto-char (point-min))
  1009. (forward-line 5)
  1010. (should (= (org-table-TBLFM-begin)
  1011. 14))
  1012. ))
  1013. (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
  1014. (org-test-with-temp-text-in-file
  1015. "
  1016. | 1 |
  1017. | 2 |
  1018. #+TBLFM: $2=$1*1
  1019. #+TBLFM: $2=$1*2
  1020. | 6 |
  1021. | 7 |
  1022. #+TBLFM: $2=$1*1
  1023. #+TBLFM: $2=$1*2
  1024. "
  1025. (goto-char (point-min))
  1026. (should (equal (org-table-TBLFM-begin)
  1027. nil))
  1028. (goto-char (point-min))
  1029. (forward-line 1)
  1030. (should (equal (org-table-TBLFM-begin)
  1031. nil))
  1032. (goto-char (point-min))
  1033. (forward-line 3)
  1034. (should (= (org-table-TBLFM-begin)
  1035. 14))
  1036. (goto-char (point-min))
  1037. (forward-line 4)
  1038. (should (= (org-table-TBLFM-begin)
  1039. 14))
  1040. (goto-char (point-min))
  1041. (forward-line 5)
  1042. (should (= (org-table-TBLFM-begin)
  1043. 14))
  1044. (goto-char (point-min))
  1045. (forward-line 6)
  1046. (should (= (org-table-TBLFM-begin)
  1047. 14))
  1048. (goto-char (point-min))
  1049. (forward-line 8)
  1050. (should (= (org-table-TBLFM-begin)
  1051. 61))
  1052. (goto-char (point-min))
  1053. (forward-line 9)
  1054. (should (= (org-table-TBLFM-begin)
  1055. 61))
  1056. (goto-char (point-min))
  1057. (forward-line 10)
  1058. (should (= (org-table-TBLFM-begin)
  1059. 61))))
  1060. (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
  1061. (org-test-with-temp-text-in-file
  1062. "
  1063. | 1 | |
  1064. | 2 | |
  1065. #+TBLFM: $2=$1*1
  1066. #+TBLFM: $2=$1*2
  1067. #+TBLFM: $2=$1*3
  1068. "
  1069. (let ((got (progn (goto-char (point-min))
  1070. (forward-line 3)
  1071. (org-table-calc-current-TBLFM)
  1072. (buffer-string)))
  1073. (expect "
  1074. | 1 | 1 |
  1075. | 2 | 2 |
  1076. #+TBLFM: $2=$1*1
  1077. #+TBLFM: $2=$1*2
  1078. #+TBLFM: $2=$1*3
  1079. "))
  1080. (should (string= got
  1081. expect)))
  1082. (let ((got (progn (goto-char (point-min))
  1083. (forward-line 4)
  1084. (org-table-calc-current-TBLFM)
  1085. (buffer-string)))
  1086. (expect "
  1087. | 1 | 2 |
  1088. | 2 | 4 |
  1089. #+TBLFM: $2=$1*1
  1090. #+TBLFM: $2=$1*2
  1091. #+TBLFM: $2=$1*3
  1092. "))
  1093. (should (string= got
  1094. expect)))))
  1095. (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
  1096. "org-table-calc-current-TBLFM should preserve the input as it was."
  1097. (org-test-with-temp-text-in-file
  1098. "
  1099. | 1 | 1 |
  1100. | 2 | 2 |
  1101. #+TBLFM: $2=$1*1
  1102. #+TBLFM: $2=$1*2::$2=$1*2
  1103. #+TBLFM: $2=$1*3
  1104. "
  1105. (let ((expect "
  1106. | 1 | 1 |
  1107. | 2 | 2 |
  1108. #+TBLFM: $2=$1*1
  1109. #+TBLFM: $2=$1*2::$2=$1*2
  1110. #+TBLFM: $2=$1*3
  1111. "))
  1112. (goto-char (point-min))
  1113. (forward-line 4)
  1114. (should-error (org-table-calc-current-TBLFM))
  1115. (setq got (buffer-string))
  1116. (message "%s" got)
  1117. (should (string= got
  1118. expect)))))
  1119. ;;; Radio Tables
  1120. (ert-deftest test-org-table/to-generic ()
  1121. "Test `orgtbl-to-generic' specifications."
  1122. ;; Test :hline parameter.
  1123. (should
  1124. (equal "a\nb"
  1125. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1126. '(:hline nil))))
  1127. (should
  1128. (equal "a\n~\nb"
  1129. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1130. '(:hline "~"))))
  1131. ;; Test :sep parameter.
  1132. (should
  1133. (equal "a!b\nc!d"
  1134. (orgtbl-to-generic
  1135. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1136. '(:sep "!"))))
  1137. ;; Test :hsep parameter.
  1138. (should
  1139. (equal "a!b\nc?d"
  1140. (orgtbl-to-generic
  1141. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1142. '(:sep "?" :hsep "!"))))
  1143. ;; Test :tstart parameter.
  1144. (should
  1145. (equal "<begin>\na"
  1146. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tstart "<begin>"))))
  1147. (should
  1148. (equal "<begin>\na"
  1149. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1150. '(:tstart (lambda () "<begin>")))))
  1151. (should
  1152. (equal "a"
  1153. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1154. '(:tstart "<begin>" :splice t))))
  1155. ;; Test :tend parameter.
  1156. (should
  1157. (equal "a\n<end>"
  1158. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tend "<end>"))))
  1159. (should
  1160. (equal "a\n<end>"
  1161. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1162. '(:tend (lambda () "<end>")))))
  1163. (should
  1164. (equal "a"
  1165. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1166. '(:tend "<end>" :splice t))))
  1167. ;; Test :lstart parameter.
  1168. (should
  1169. (equal "> a"
  1170. (orgtbl-to-generic
  1171. (org-table-to-lisp "| a |") '(:lstart "> "))))
  1172. (should
  1173. (equal "> a"
  1174. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1175. '(:lstart (lambda () "> ")))))
  1176. ;; Test :llstart parameter.
  1177. (should
  1178. (equal "> a\n>> b"
  1179. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1180. '(:lstart "> " :llstart ">> "))))
  1181. ;; Test :hlstart parameter.
  1182. (should
  1183. (equal "!> a\n> b"
  1184. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1185. '(:lstart "> " :hlstart "!> "))))
  1186. ;; Test :hllstart parameter.
  1187. (should
  1188. (equal "!> a\n!!> b\n> c"
  1189. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1190. '(:lstart "> " :hlstart "!> " :hllstart "!!> "))))
  1191. ;; Test :lend parameter.
  1192. (should
  1193. (equal "a <"
  1194. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:lend " <"))))
  1195. ;; Test :llend parameter.
  1196. (should
  1197. (equal "a <\nb <<"
  1198. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1199. '(:lend " <" :llend " <<"))))
  1200. ;; Test :hlend parameter.
  1201. (should
  1202. (equal "a <!\nb <"
  1203. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1204. '(:lend " <" :hlend " <!"))))
  1205. ;; Test :hllend parameter.
  1206. (should
  1207. (equal "a <!\nb <!!\nc <"
  1208. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1209. '(:lend " <" :hlend " <!" :hllend " <!!"))))
  1210. ;; Test :lfmt parameter.
  1211. (should
  1212. (equal "a!b"
  1213. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1214. '(:lfmt "%s!%s"))))
  1215. (should
  1216. (equal "a+b"
  1217. (orgtbl-to-generic
  1218. (org-table-to-lisp "| a | b |")
  1219. '(:lfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1220. (should
  1221. (equal "a!b"
  1222. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1223. '(:lfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1224. ;; Test :llfmt parameter.
  1225. (should
  1226. (equal "a!b"
  1227. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1228. '(:llfmt "%s!%s"))))
  1229. (should
  1230. (equal "a!b\nc+d"
  1231. (orgtbl-to-generic
  1232. (org-table-to-lisp "| a | b |\n| c | d |")
  1233. '(:lfmt "%s!%s" :llfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1234. (should
  1235. (equal "a!b"
  1236. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1237. '(:llfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1238. ;; Test :hlfmt parameter.
  1239. (should
  1240. (equal "a!b\ncd"
  1241. (orgtbl-to-generic
  1242. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1243. '(:hlfmt "%s!%s"))))
  1244. (should
  1245. (equal "a+b\ncd"
  1246. (orgtbl-to-generic
  1247. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1248. '(:hlfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1249. (should
  1250. (equal "a!b\n>c d<"
  1251. (orgtbl-to-generic
  1252. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1253. '(:hlfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1254. ;; Test :hllfmt parameter.
  1255. (should
  1256. (equal "a!b\ncd"
  1257. (orgtbl-to-generic
  1258. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1259. '(:hllfmt "%s!%s"))))
  1260. (should
  1261. (equal "a+b\ncd"
  1262. (orgtbl-to-generic
  1263. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1264. '(:hllfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1265. (should
  1266. (equal "a!b\n>c d<"
  1267. (orgtbl-to-generic
  1268. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1269. '(:hllfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1270. ;; Test :fmt parameter.
  1271. (should
  1272. (equal ">a<\n>b<"
  1273. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1274. '(:fmt ">%s<"))))
  1275. (should
  1276. (equal ">a<b"
  1277. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1278. '(:fmt (1 ">%s<" 2 (lambda (c) c))))))
  1279. (should
  1280. (equal "a b"
  1281. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1282. '(:fmt (2 " %s")))))
  1283. (should
  1284. (equal ">a<"
  1285. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1286. '(:fmt (lambda (c) (format ">%s<" c))))))
  1287. ;; Test :hfmt parameter.
  1288. (should
  1289. (equal ">a<\nb"
  1290. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1291. '(:hfmt ">%s<"))))
  1292. (should
  1293. (equal ">a<b\ncd"
  1294. (orgtbl-to-generic
  1295. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1296. '(:hfmt (1 ">%s<" 2 identity)))))
  1297. (should
  1298. (equal "a b\ncd"
  1299. (orgtbl-to-generic
  1300. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1301. '(:hfmt (2 " %s")))))
  1302. (should
  1303. (equal ">a<\nb"
  1304. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1305. '(:hfmt (lambda (c) (format ">%s<" c))))))
  1306. ;; Test :efmt parameter.
  1307. (should
  1308. (equal "2x10^3"
  1309. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1310. '(:efmt "%sx10^%s"))))
  1311. (should
  1312. (equal "2x10^3"
  1313. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1314. '(:efmt (lambda (m e) (concat m "x10^" e))))))
  1315. (should
  1316. (equal "2x10^3"
  1317. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1318. '(:efmt (1 "%sx10^%s")))))
  1319. (should
  1320. (equal "2x10^3"
  1321. (orgtbl-to-generic
  1322. (org-table-to-lisp "| 2e3 |")
  1323. '(:efmt (1 (lambda (m e) (format "%sx10^%s" m e)))))))
  1324. (should
  1325. (equal "2e3"
  1326. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |") '(:efmt nil))))
  1327. ;; Test :skip parameter.
  1328. (should
  1329. (equal "cd"
  1330. (orgtbl-to-generic
  1331. (org-table-to-lisp "| \ | <c> |\n| a | b |\n|---+---|\n| c | d |")
  1332. '(:skip 2))))
  1333. ;; Test :skipcols parameter.
  1334. (should
  1335. (equal "a\nc"
  1336. (orgtbl-to-generic
  1337. (org-table-to-lisp "| a | b |\n| c | d |") '(:skipcols (2)))))
  1338. (should
  1339. (equal "a\nc"
  1340. (orgtbl-to-generic
  1341. (org-table-to-lisp
  1342. "| / | <c> | <c> |\n| # | a | b |\n|---+---+---|\n| | c | d |")
  1343. '(:skipcols (2)))))
  1344. ;; Test :raw parameter.
  1345. (when (featurep 'ox-latex)
  1346. (should
  1347. (org-string-match-p
  1348. "/a/"
  1349. (orgtbl-to-generic (org-table-to-lisp "| /a/ | b |")
  1350. '(:backend latex :raw t)))))
  1351. ;; Hooks are ignored.
  1352. (should
  1353. (equal
  1354. "a\nb"
  1355. (let* ((fun-list (list (lambda (backend) (search-forward "a") (insert "hook"))))
  1356. (org-export-before-parsing-hook fun-list)
  1357. (org-export-before-processing-hook fun-list))
  1358. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1359. '(:hline nil)))))
  1360. ;; User-defined export filters are ignored.
  1361. (should
  1362. (equal
  1363. "a\nb"
  1364. (let ((org-export-filter-table-cell-functions (list (lambda (c b i) "filter"))))
  1365. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1366. '(:hline nil)))))
  1367. ;; Macros, even if unknown, are returned as-is.
  1368. (should
  1369. (equal "{{{macro}}}"
  1370. (orgtbl-to-generic (org-table-to-lisp "| {{{macro}}} |") nil))))
  1371. (ert-deftest test-org-table/to-latex ()
  1372. "Test `orgtbl-to-latex' specifications."
  1373. (should
  1374. (equal "\\begin{tabular}{l}\na\\\\\n\\end{tabular}"
  1375. (orgtbl-to-latex (org-table-to-lisp "| a |") nil)))
  1376. ;; Test :environment parameter.
  1377. (should
  1378. (equal "\\begin{tabularx}{l}\na\\\\\n\\end{tabularx}"
  1379. (orgtbl-to-latex (org-table-to-lisp "| a |")
  1380. '(:environment "tabularx"))))
  1381. ;; Test :booktabs parameter.
  1382. (should
  1383. (org-string-match-p
  1384. "\\toprule" (orgtbl-to-latex (org-table-to-lisp "| a |") '(:booktabs t))))
  1385. ;; Test pseudo objects and :raw parameter.
  1386. (should
  1387. (org-string-match-p
  1388. "\\$x\\$" (orgtbl-to-latex (org-table-to-lisp "| $x$ |") '(:raw t)))))
  1389. (ert-deftest test-org-table/to-html ()
  1390. "Test `orgtbl-to-html' specifications."
  1391. (should
  1392. (equal (orgtbl-to-html (org-table-to-lisp "| a |") nil)
  1393. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">
  1394. <colgroup>
  1395. <col class=\"org-left\" />
  1396. </colgroup>
  1397. <tbody>
  1398. <tr>
  1399. <td class=\"org-left\">a</td>
  1400. </tr>
  1401. </tbody>
  1402. </table>"))
  1403. ;; Test :attributes parameter.
  1404. (should
  1405. (org-string-match-p
  1406. "<table>"
  1407. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes nil))))
  1408. (should
  1409. (org-string-match-p
  1410. "<table border=\"2\">"
  1411. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes (:border "2"))))))
  1412. (ert-deftest test-org-table/to-texinfo ()
  1413. "Test `orgtbl-to-texinfo' specifications."
  1414. (should
  1415. (equal "@multitable {a}\n@item a\n@end multitable"
  1416. (orgtbl-to-texinfo (org-table-to-lisp "| a |") nil)))
  1417. ;; Test :columns parameter.
  1418. (should
  1419. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1420. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1421. '(:columns ".4 .6"))))
  1422. (should
  1423. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1424. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1425. '(:columns "@columnfractions .4 .6"))))
  1426. (should
  1427. (equal "@multitable {xxx} {xx}\n@item a\n@tab b\n@end multitable"
  1428. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1429. '(:columns "{xxx} {xx}")))))
  1430. (ert-deftest test-org-table/to-orgtbl ()
  1431. "Test `orgtbl-to-orgtbl' specifications."
  1432. (should
  1433. (equal "| a | b |\n|---+---|\n| c | d |"
  1434. (orgtbl-to-orgtbl
  1435. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |") nil))))
  1436. (ert-deftest test-org-table/to-unicode ()
  1437. "Test `orgtbl-to-unicode' specifications."
  1438. (should
  1439. (equal "━━━\n a \n━━━"
  1440. (orgtbl-to-unicode (org-table-to-lisp "| a |") nil)))
  1441. ;; Test :narrow parameter.
  1442. (should
  1443. (equal "━━━━\n => \n━━━━"
  1444. (orgtbl-to-unicode (org-table-to-lisp "| <2> |\n| xxx |")
  1445. '(:narrow t)))))
  1446. (ert-deftest test-org-table/send-region ()
  1447. "Test `orgtbl-send-table' specifications."
  1448. ;; Error when not at a table.
  1449. (should-error
  1450. (org-test-with-temp-text "Paragraph"
  1451. (orgtbl-send-table)))
  1452. ;; Error when destination is missing.
  1453. (should-error
  1454. (org-test-with-temp-text "#+ORGTBL: SEND\n<point>| a |"
  1455. (orgtbl-send-table)))
  1456. ;; Error when transformation function is not specified.
  1457. (should-error
  1458. (org-test-with-temp-text "
  1459. # BEGIN RECEIVE ORGTBL table
  1460. # END RECEIVE ORGTBL table
  1461. #+ORGTBL: SEND table
  1462. <point>| a |"
  1463. (orgtbl-send-table)))
  1464. ;; Standard test.
  1465. (should
  1466. (equal "| a |\n|---|\n| b |\n"
  1467. (org-test-with-temp-text "
  1468. # BEGIN RECEIVE ORGTBL table
  1469. # END RECEIVE ORGTBL table
  1470. #+ORGTBL: SEND table orgtbl-to-orgtbl :hlines nil
  1471. <point>| a |\n|---|\n| b |"
  1472. (orgtbl-send-table)
  1473. (goto-char (point-min))
  1474. (buffer-substring-no-properties
  1475. (search-forward "# BEGIN RECEIVE ORGTBL table\n")
  1476. (progn (search-forward "# END RECEIVE ORGTBL table")
  1477. (match-beginning 0)))))))
  1478. ;;; Sorting
  1479. (ert-deftest test-org-table/sort-lines ()
  1480. "Test `org-table-sort-lines' specifications."
  1481. ;; Sort numerically.
  1482. (should
  1483. (equal "| 1 | 2 |\n| 2 | 4 |\n| 5 | 3 |\n"
  1484. (org-test-with-temp-text "| <point>1 | 2 |\n| 5 | 3 |\n| 2 | 4 |\n"
  1485. (org-table-sort-lines nil ?n)
  1486. (buffer-string))))
  1487. (should
  1488. (equal "| 5 | 3 |\n| 2 | 4 |\n| 1 | 2 |\n"
  1489. (org-test-with-temp-text "| <point>1 | 2 |\n| 5 | 3 |\n| 2 | 4 |\n"
  1490. (org-table-sort-lines nil ?N)
  1491. (buffer-string))))
  1492. ;; Sort alphabetically.
  1493. (should
  1494. (equal "| a | x |\n| b | 4 |\n| c | 3 |\n"
  1495. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| b | 4 |\n"
  1496. (org-table-sort-lines nil ?a)
  1497. (buffer-string))))
  1498. (should
  1499. (equal "| c | 3 |\n| b | 4 |\n| a | x |\n"
  1500. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| b | 4 |\n"
  1501. (org-table-sort-lines nil ?A)
  1502. (buffer-string))))
  1503. ;; Sort alphabetically with case.
  1504. (should
  1505. (equal "| C |\n| a |\n| b |\n"
  1506. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1507. (org-table-sort-lines t ?a)
  1508. (buffer-string))))
  1509. (should
  1510. (equal "| b |\n| a |\n| C |\n"
  1511. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1512. (org-table-sort-lines nil ?A)
  1513. (buffer-string))))
  1514. ;; Sort by time (timestamps)
  1515. (should
  1516. (equal
  1517. "| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n| <2014-03-04 tue.> |\n"
  1518. (org-test-with-temp-text
  1519. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1520. (org-table-sort-lines nil ?t)
  1521. (buffer-string))))
  1522. (should
  1523. (equal
  1524. "| <2014-03-04 tue.> |\n| <2012-03-29 thu.> |\n| <2008-08-08 sat.> |\n"
  1525. (org-test-with-temp-text
  1526. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1527. (org-table-sort-lines nil ?T)
  1528. (buffer-string))))
  1529. ;; Sort by time (HH:MM values)
  1530. (should
  1531. (equal "| 1:00 |\n| 14:00 |\n| 17:00 |\n"
  1532. (org-test-with-temp-text "| 14:00 |\n| 17:00 |\n| 1:00 |\n"
  1533. (org-table-sort-lines nil ?t)
  1534. (buffer-string))))
  1535. (should
  1536. (equal "| 17:00 |\n| 14:00 |\n| 1:00 |\n"
  1537. (org-test-with-temp-text "| 14:00 |\n| 17:00 |\n| 1:00 |\n"
  1538. (org-table-sort-lines nil ?T)
  1539. (buffer-string))))
  1540. ;; Sort with custom functions.
  1541. (should
  1542. (equal "| 22 |\n| 15 |\n| 18 |\n"
  1543. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1544. (org-table-sort-lines nil ?f
  1545. (lambda (s) (% (string-to-number s) 10))
  1546. #'<)
  1547. (buffer-string))))
  1548. (should
  1549. (equal "| 18 |\n| 15 |\n| 22 |\n"
  1550. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1551. (org-table-sort-lines nil ?F
  1552. (lambda (s) (% (string-to-number s) 10))
  1553. #'<)
  1554. (buffer-string))))
  1555. ;; Sort according to current column.
  1556. (should
  1557. (equal "| 1 | 2 |\n| 7 | 3 |\n| 5 | 4 |\n"
  1558. (org-test-with-temp-text "| 1 | <point>2 |\n| 5 | 4 |\n| 7 | 3 |\n"
  1559. (org-table-sort-lines nil ?n)
  1560. (buffer-string))))
  1561. ;; Sort between horizontal separators if possible.
  1562. (should
  1563. (equal
  1564. "| 9 | 8 |\n|---+---|\n| 5 | 3 |\n| 7 | 4 |\n|---+---|\n| 1 | 2 |\n"
  1565. (org-test-with-temp-text
  1566. "| 9 | 8 |\n|---+---|\n| <point>7 | 4 |\n| 5 | 3 |\n|---+---|\n| 1 | 2 |\n"
  1567. (org-table-sort-lines nil ?n)
  1568. (buffer-string)))))
  1569. ;;; Field formulas
  1570. (ert-deftest test-org-table/field-formula-outside-table ()
  1571. "If `org-table-formula-create-columns' is nil, then a formula
  1572. that references an out-of-bounds column should do nothing. If it
  1573. is t, then new columns should be added as needed"
  1574. (let ((org-table-formula-create-columns nil))
  1575. (should-error
  1576. (org-test-table-target-expect
  1577. "
  1578. | 2 |
  1579. | 4 |
  1580. | 8 |
  1581. "
  1582. "
  1583. | 2 |
  1584. | 4 |
  1585. | 8 |
  1586. "
  1587. 1
  1588. "#+TBLFM: @1$2=5")
  1589. :type (list 'error 'user-error)))
  1590. (let ((org-table-formula-create-columns t))
  1591. ;; make sure field formulas work
  1592. (org-test-table-target-expect
  1593. "
  1594. | 2 |
  1595. | 4 |
  1596. | 8 |
  1597. "
  1598. "
  1599. | 2 | 5 |
  1600. | 4 | |
  1601. | 8 | |
  1602. "
  1603. 1
  1604. "#+TBLFM: @1$2=5")
  1605. ;; and make sure column formulas work too
  1606. (org-test-table-target-expect
  1607. "
  1608. | 2 |
  1609. | 4 |
  1610. | 8 |
  1611. "
  1612. "
  1613. | 2 | | 15 |
  1614. | 4 | | 15 |
  1615. | 8 | | 15 |
  1616. "
  1617. 1
  1618. "#+TBLFM: $3=15")))
  1619. (ert-deftest test-org-table/duration ()
  1620. "Test durations in table formulas."
  1621. ;; Durations in cells.
  1622. (should
  1623. (string-match "| 2:12 | 1:47 | 03:59:00 |"
  1624. (org-test-with-temp-text "
  1625. | 2:12 | 1:47 | |
  1626. <point>#+TBLFM: @1$3=$1+$2;T"
  1627. (org-table-calc-current-TBLFM)
  1628. (buffer-string))))
  1629. (should
  1630. (string-match "| 3:02:20 | -2:07:00 | 0.92 |"
  1631. (org-test-with-temp-text "
  1632. | 3:02:20 | -2:07:00 | |
  1633. <point>#+TBLFM: @1$3=$1+$2;t"
  1634. (org-table-calc-current-TBLFM)
  1635. (buffer-string))))
  1636. ;; Durations set through properties.
  1637. (should
  1638. (string-match "| 16:00:00 |"
  1639. (org-test-with-temp-text "* H
  1640. :PROPERTIES:
  1641. :time_constant: 08:00:00
  1642. :END:
  1643. | |
  1644. <point>#+TBLFM: $1=2*$PROP_time_constant;T"
  1645. (org-table-calc-current-TBLFM)
  1646. (buffer-string))))
  1647. (should
  1648. (string-match "| 16.00 |"
  1649. (org-test-with-temp-text "* H
  1650. :PROPERTIES:
  1651. :time_constant: 08:00:00
  1652. :END:
  1653. | |
  1654. <point>#+TBLFM: $1=2*$PROP_time_constant;t"
  1655. (org-table-calc-current-TBLFM)
  1656. (buffer-string)))))
  1657. (ert-deftest test-org-table/end-on-hline ()
  1658. "Test with a table ending on a hline."
  1659. (should
  1660. (equal
  1661. (org-test-with-temp-text
  1662. "
  1663. | 1 | 2 | 3 |
  1664. | 4 | 5 | 6 |
  1665. | | | |
  1666. |---+---+---|
  1667. <point>#+TBLFM: @3$2..@3$>=vsum(@1..@2)"
  1668. (org-table-calc-current-TBLFM)
  1669. (buffer-string))
  1670. "
  1671. | 1 | 2 | 3 |
  1672. | 4 | 5 | 6 |
  1673. | | 7 | 9 |
  1674. |---+---+---|
  1675. #+TBLFM: @3$2..@3$>=vsum(@1..@2)")))
  1676. (ert-deftest test-org-table/named-field ()
  1677. "Test formula with a named field."
  1678. (should
  1679. (org-string-match-p
  1680. "| +| +1 +|"
  1681. (org-test-with-temp-text "
  1682. | | |
  1683. | ^ | name |
  1684. <point>#+TBLFM: $name=1"
  1685. (org-table-calc-current-TBLFM)
  1686. (buffer-string))))
  1687. (should
  1688. (org-string-match-p
  1689. "| +| +1 +|"
  1690. (org-test-with-temp-text "
  1691. | _ | name |
  1692. | | |
  1693. <point>#+TBLFM: $name=1"
  1694. (org-table-calc-current-TBLFM)
  1695. (buffer-string)))))
  1696. (ert-deftest test-org-table/named-column ()
  1697. "Test formula with a named field."
  1698. (should
  1699. (org-string-match-p
  1700. "| +| +1 +| +1 +|"
  1701. (org-test-with-temp-text "
  1702. | ! | name | |
  1703. | | 1 | |
  1704. <point>#+TBLFM: @2$3=$name"
  1705. (org-table-calc-current-TBLFM)
  1706. (buffer-string)))))
  1707. (ert-deftest test-org-table/tab-indent ()
  1708. "Test named fields with tab indentation."
  1709. (should
  1710. (org-string-match-p
  1711. "| # | 111 |"
  1712. (org-test-with-temp-text
  1713. "
  1714. | ! | sum | | a | b | c |
  1715. |---+------+------+---+----+-----|
  1716. | # | 1011 | 1000 | 1 | 10 | 100 |
  1717. <point>#+TBLFM: $2=$a+$b+$c
  1718. "
  1719. (org-table-calc-current-TBLFM)
  1720. (buffer-string)))))
  1721. (ert-deftest test-org-table/first-rc ()
  1722. "Test \"$<\" constructs in formulas."
  1723. (should
  1724. (org-string-match-p
  1725. "| 1 | 2 |"
  1726. (org-test-with-temp-text
  1727. "| | 2 |
  1728. <point>#+TBLFM: $<=1"
  1729. (org-table-calc-current-TBLFM)
  1730. (buffer-string)))))
  1731. (provide 'test-org-table)
  1732. ;;; test-org-table.el ends here