test-org-table.el 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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/compare ()
  372. "Basic: Compare field references in Calc."
  373. (org-test-table-target-expect
  374. "
  375. | | 0 | z | | nan | uinf | -inf | inf |
  376. |------+------+------+------+------+------+------+------|
  377. | 0 | repl | repl | repl | repl | repl | repl | repl |
  378. | z | repl | repl | repl | repl | repl | repl | repl |
  379. | | repl | repl | repl | repl | repl | repl | repl |
  380. | nan | repl | repl | repl | repl | repl | repl | repl |
  381. | uinf | repl | repl | repl | repl | repl | repl | repl |
  382. | -inf | repl | repl | repl | repl | repl | repl | repl |
  383. | inf | repl | repl | repl | repl | repl | repl | repl |
  384. "
  385. "
  386. | | 0 | z | | nan | uinf | -inf | inf |
  387. |------+---+---+---+-----+------+------+-----|
  388. | 0 | x | | | | | | |
  389. | z | | x | | | | | |
  390. | | | | x | | | | |
  391. | nan | | | | x | | | |
  392. | uinf | | | | | x | | |
  393. | -inf | | | | | | x | |
  394. | inf | | | | | | | x |
  395. "
  396. 1
  397. ;; Compare field reference ($1) with field reference (@1)
  398. "#+TBLFM: @<<$<<..@>$> = if(\"$1\" == \"@1\", x, string(\"\")); E"
  399. ;; Compare field reference ($1) with absolute term
  400. (concat "#+TBLFM: "
  401. "$2 = if(\"$1\" == \"(0)\" , x, string(\"\")); E :: "
  402. "$3 = if(\"$1\" == \"(z)\" , x, string(\"\")); E :: "
  403. "$4 = if(\"$1\" == \"nan\" , x, string(\"\")); E :: "
  404. "$5 = if(\"$1\" == \"(nan)\" , x, string(\"\")); E :: "
  405. "$6 = if(\"$1\" == \"(uinf)\", x, string(\"\")); E :: "
  406. "$7 = if(\"$1\" == \"(-inf)\", x, string(\"\")); E :: "
  407. "$8 = if(\"$1\" == \"(inf)\" , x, string(\"\")); E"))
  408. ;; Check field reference converted from an empty field: Despite this
  409. ;; field reference will not end up in a result, Calc evaluates it.
  410. ;; Make sure that also then there is no Calc error.
  411. (org-test-table-target-expect
  412. "
  413. | 0 | replace |
  414. | z | replace |
  415. | | replace |
  416. | nan | replace |
  417. "
  418. "
  419. | 0 | 1 |
  420. | z | z + 1 |
  421. | | |
  422. | nan | nan |
  423. "
  424. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"))
  425. (ert-deftest test-org-table/empty-field ()
  426. "Examples how to deal with empty fields."
  427. ;; Test if one field is empty, else do a calculation
  428. (org-test-table-target-expect
  429. "
  430. | -1 | replace |
  431. | 0 | replace |
  432. | | replace |
  433. "
  434. "
  435. | -1 | 0 |
  436. | 0 | 1 |
  437. | | |
  438. "
  439. 1
  440. ;; Calc formula
  441. "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"
  442. ;; Lisp formula
  443. "#+TBLFM: $2 = '(if (eq \"$1\" \"\") \"\" (1+ $1)); L")
  444. ;; Test if several fields are empty, else do a calculation
  445. (org-test-table-target-expect
  446. "
  447. | 1 | 2 | replace |
  448. | 4 | | replace |
  449. | | 8 | replace |
  450. | | | replace |
  451. "
  452. "
  453. | 1 | 2 | 3 |
  454. | 4 | | |
  455. | | 8 | |
  456. | | | |
  457. "
  458. 1
  459. ;; Calc formula
  460. (concat "#+TBLFM: $3 = if(\"$1\" == \"nan\" || \"$2\" == \"nan\", "
  461. "string(\"\"), $1 + $2); E")
  462. ;; Lisp formula
  463. (concat "#+TBLFM: $3 = '(if (or (eq \"$1\" \"\") (eq \"$2\" \"\")) "
  464. "\"\" (+ $1 $2)); L"))
  465. ;; $2: Use $1 + 0.5 if $1 available, else only reformat $2 if $2 available
  466. (org-test-table-target-expect
  467. "
  468. | 1.5 | 0 |
  469. | 3.5 | |
  470. | | 5 |
  471. | | |
  472. "
  473. "
  474. | 1.5 | 2.0 |
  475. | 3.5 | 4.0 |
  476. | | 5.0 |
  477. | | |
  478. "
  479. 1
  480. ;; Calc formula
  481. (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  482. "if(\"$2\" == \"nan\", string(\"\"), $2 +.0), $1 + 0.5); E f-1")
  483. ;; Lisp formula not implemented yet
  484. )
  485. ;; Empty fields in simple and complex range reference
  486. (org-test-table-target-expect
  487. "
  488. | | | | | repl | repl | repl | repl | repl | repl |
  489. | | | 5 | 7 | repl | repl | repl | repl | repl | repl |
  490. | 1 | 3 | 5 | 7 | repl | repl | repl | repl | repl | repl |
  491. "
  492. "
  493. | | | | | | | | | 0 | 0 |
  494. | | | 5 | 7 | | | 6 | 6 | 3 | 3 |
  495. | 1 | 3 | 5 | 7 | 4 | 4 | 4 | 4 | 4 | 4 |
  496. "
  497. 1
  498. ;; Calc formula
  499. (concat
  500. "#+TBLFM: "
  501. "$5 = if(typeof(vmean($1..$4)) == 12, "
  502. "string(\"\"), vmean($1..$4)); E :: "
  503. "$6 = if(typeof(vmean(@0$1..@0$4)) == 12, "
  504. "string(\"\"), vmean(@0$1..@0$4)); E :: "
  505. "$7 = if(\"$1..$4\" == \"[]\", string(\"\"), vmean($1..$4)) :: "
  506. "$8 = if(\"@0$1..@0$4\" == \"[]\", string(\"\"), vmean(@0$1..@0$4)) :: "
  507. "$9 = vmean($1..$4); EN :: "
  508. "$10 = vmean(@0$1..@0$4); EN")
  509. ;; Lisp formula
  510. (concat
  511. "#+TBLFM: "
  512. "$5 = '(let ((l '($1..$4))) (if (member \"\" l) \"\" "
  513. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  514. "$6 = '(let ((l '(@0$1..@0$4))) (if (member \"\" l) \"\" "
  515. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  516. "$7 = '(let ((l '($1..$4))) "
  517. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  518. "$8 = '(let ((l '(@0$1..@0$4))) "
  519. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  520. "$9 = '(/ (+ $1..$4) (length '($1..$4))); EN :: "
  521. "$10 = '(/ (+ @0$1..@0$4) (length '(@0$1..@0$4))); EN")
  522. ))
  523. (ert-deftest test-org-table/copy-field ()
  524. "Experiments on how to copy one field into another field.
  525. See also `test-org-table/remote-reference-access'."
  526. (let ((target "
  527. | 0 | replace |
  528. | a b | replace |
  529. | c d | replace |
  530. | | replace |
  531. | 2012-12 | replace |
  532. | [2012-12-31 Mon] | replace |
  533. "))
  534. ;; Lisp formula to copy literally
  535. (org-test-table-target-expect
  536. target
  537. "
  538. | 0 | 0 |
  539. | a b | a b |
  540. | c d | c d |
  541. | | |
  542. | 2012-12 | 2012-12 |
  543. | [2012-12-31 Mon] | [2012-12-31 Mon] |
  544. "
  545. 1 "#+TBLFM: $2 = '(identity $1)")
  546. ;; Calc formula to copy quite literally
  547. (org-test-table-target-expect
  548. target
  549. "
  550. | 0 | 0 |
  551. | a b | a b |
  552. | c d | c d |
  553. | | |
  554. | 2012-12 | 2012-12 |
  555. | [2012-12-31 Mon] | <2012-12-31 Mon> |
  556. "
  557. 1 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  558. "string(\"\"), string(subvec(\"$1\", 2, vlen(\"$1\")))); E"))
  559. ;; Calc formula simple
  560. (org-test-table-target-expect
  561. target
  562. "
  563. | 0 | 0 |
  564. | a b | a b |
  565. | c d | c d |
  566. | | |
  567. | 2012-12 | 2000 |
  568. | [2012-12-31 Mon] | <2012-12-31 Mon> |
  569. "
  570. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1); E")))
  571. (ert-deftest test-org-table/sub-total ()
  572. "Grouped rows with sub-total.
  573. Begin range with \"@II\" to handle multiline header. Convert
  574. integer to float with \"+.0\" for sub-total of items c1 and c2.
  575. Sum empty fields as value zero but without ignoring them for
  576. \"vlen\" with format specifier \"EN\". Format possibly empty
  577. results with the Calc formatter \"f-1\" instead of the printf
  578. formatter \"%.1f\"."
  579. (org-test-table-target-expect
  580. "
  581. |-------+---------+---------|
  582. | Item | Item | Sub- |
  583. | name | value | total |
  584. |-------+---------+---------|
  585. | a1 | 4.1 | replace |
  586. | a2 | 8.2 | replace |
  587. | a3 | | replace |
  588. |-------+---------+---------|
  589. | b1 | 16.0 | replace |
  590. |-------+---------+---------|
  591. | c1 | 32 | replace |
  592. | c2 | 64 | replace |
  593. |-------+---------+---------|
  594. | Total | replace | replace |
  595. |-------+---------+---------|
  596. "
  597. "
  598. |-------+-------+-------|
  599. | Item | Item | Sub- |
  600. | name | value | total |
  601. |-------+-------+-------|
  602. | a1 | 4.1 | |
  603. | a2 | 8.2 | |
  604. | a3 | | 12.3 |
  605. |-------+-------+-------|
  606. | b1 | 16.0 | 16.0 |
  607. |-------+-------+-------|
  608. | c1 | 32 | |
  609. | c2 | 64 | 96.0 |
  610. |-------+-------+-------|
  611. | Total | 124.3 | |
  612. |-------+-------+-------|
  613. "
  614. 1 (concat "#+TBLFM: @>$2 = vsum(@II..@>>) ::"
  615. "$3 = if(vlen(@0..@+I) == 1, "
  616. "vsum(@-I$2..@+I$2) +.0, string(\"\")); EN f-1 :: "
  617. "@>$3 = string(\"\")")))
  618. (ert-deftest test-org-table/org-lookup-all ()
  619. "Use `org-lookup-all' for several GROUP BY as in SQL and for ranking.
  620. See also http://orgmode.org/worg/org-tutorials/org-lookups.html ."
  621. (let ((data "
  622. #+NAME: data
  623. | Purchase | Product | Shop | Rating |
  624. |----------+---------+------+--------|
  625. | a | p1 | s1 | 1 |
  626. | b | p1 | s2 | 4 |
  627. | c | p2 | s1 | 2 |
  628. | d | p3 | s2 | 8 |
  629. "))
  630. ;; Product rating and ranking by average purchase from "#+NAME: data"
  631. (org-test-table-target-expect
  632. (concat data "
  633. | Product | Rating | Ranking |
  634. |---------+---------+---------|
  635. | p1 | replace | replace |
  636. | p2 | replace | replace |
  637. | p3 | replace | replace |
  638. ")
  639. (concat data "
  640. | Product | Rating | Ranking |
  641. |---------+--------+---------|
  642. | p1 | 2.5 | 2 |
  643. | p2 | 2.0 | 3 |
  644. | p3 | 8.0 | 1 |
  645. ")
  646. 2 (concat
  647. "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
  648. "'(remote(data, @I$2..@>$2)) '(remote(data, @I$4..@>$4))))) "
  649. "(/ (apply '+ all) (length all) 1.0)); L :: "
  650. "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))
  651. ;; Shop rating and ranking by average purchase from "#+NAME: data"
  652. (org-test-table-target-expect
  653. (concat data "
  654. | Shop | Rating | Ranking |
  655. |------+---------+---------|
  656. | s1 | replace | replace |
  657. | s2 | replace | replace |
  658. ")
  659. (concat data "
  660. | Shop | Rating | Ranking |
  661. |------+--------+---------|
  662. | s1 | 1.5 | 2 |
  663. | s2 | 6.0 | 1 |
  664. ")
  665. 2 (concat
  666. "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
  667. "'(remote(data, @I$3..@>$3)) '(remote(data, @I$4..@>$4))))) "
  668. "(/ (apply '+ all) (length all) 1.0)); L :: "
  669. "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))))
  670. (ert-deftest test-org-table/org-table-make-reference/mode-string-EL ()
  671. (fset 'f 'org-table-make-reference)
  672. ;; For Lisp formula only
  673. (should (equal "0" (f "0" t nil 'literal)))
  674. (should (equal "z" (f "z" t nil 'literal)))
  675. (should (equal "" (f "" t nil 'literal)))
  676. (should (equal "0 1" (f '("0" "1") t nil 'literal)))
  677. (should (equal "z 1" (f '("z" "1") t nil 'literal)))
  678. (should (equal " 1" (f '("" "1") t nil 'literal)))
  679. (should (equal " " (f '("" "" ) t nil 'literal))))
  680. (ert-deftest test-org-table/org-table-make-reference/mode-string-E ()
  681. (fset 'f 'org-table-make-reference)
  682. ;; For Lisp formula
  683. (should (equal "\"0\"" (f "0" t nil t)))
  684. (should (equal "\"z\"" (f "z" t nil t)))
  685. (should (equal "\"\"" (f "" t nil t)))
  686. (should (equal "\"0\" \"1\"" (f '("0" "1") t nil t)))
  687. (should (equal "\"z\" \"1\"" (f '("z" "1") t nil t)))
  688. (should (equal "\"\" \"1\"" (f '("" "1") t nil t)))
  689. (should (equal "\"\" \"\"" (f '("" "" ) t nil t)))
  690. ;; For Calc formula
  691. (should (equal "(0)" (f "0" t nil nil)))
  692. (should (equal "(z)" (f "z" t nil nil)))
  693. (should (equal "nan" (f "" t nil nil)))
  694. (should (equal "[0,1]" (f '("0" "1") t nil nil)))
  695. (should (equal "[z,1]" (f '("z" "1") t nil nil)))
  696. (should (equal "[nan,1]" (f '("" "1") t nil nil)))
  697. (should (equal "[nan,nan]" (f '("" "" ) t nil nil)))
  698. ;; For Calc formula, special numbers
  699. (should (equal "(nan)" (f "nan" t nil nil)))
  700. (should (equal "(uinf)" (f "uinf" t nil nil)))
  701. (should (equal "(-inf)" (f "-inf" t nil nil)))
  702. (should (equal "(inf)" (f "inf" t nil nil)))
  703. (should (equal "[nan,1]" (f '( "nan" "1") t nil nil)))
  704. (should (equal "[uinf,1]" (f '("uinf" "1") t nil nil)))
  705. (should (equal "[-inf,1]" (f '("-inf" "1") t nil nil)))
  706. (should (equal "[inf,1]" (f '( "inf" "1") t nil nil))))
  707. (ert-deftest test-org-table/org-table-make-reference/mode-string-EN ()
  708. (fset 'f 'org-table-make-reference)
  709. ;; For Lisp formula
  710. (should (equal "0" (f "0" t t t)))
  711. (should (equal "0" (f "z" t t t)))
  712. (should (equal "0" (f "" t t t)))
  713. (should (equal "0 1" (f '("0" "1") t t t)))
  714. (should (equal "0 1" (f '("z" "1") t t t)))
  715. (should (equal "0 1" (f '("" "1") t t t)))
  716. (should (equal "0 0" (f '("" "" ) t t t)))
  717. ;; For Calc formula
  718. (should (equal "(0)" (f "0" t t nil)))
  719. (should (equal "(0)" (f "z" t t nil)))
  720. (should (equal "(0)" (f "" t t nil)))
  721. (should (equal "[0,1]" (f '("0" "1") t t nil)))
  722. (should (equal "[0,1]" (f '("z" "1") t t nil)))
  723. (should (equal "[0,1]" (f '("" "1") t t nil)))
  724. (should (equal "[0,0]" (f '("" "" ) t t nil)))
  725. ;; For Calc formula, special numbers
  726. (should (equal "(0)" (f "nan" t t nil)))
  727. (should (equal "(0)" (f "uinf" t t nil)))
  728. (should (equal "(0)" (f "-inf" t t nil)))
  729. (should (equal "(0)" (f "inf" t t nil)))
  730. (should (equal "[0,1]" (f '( "nan" "1") t t nil)))
  731. (should (equal "[0,1]" (f '("uinf" "1") t t nil)))
  732. (should (equal "[0,1]" (f '("-inf" "1") t t nil)))
  733. (should (equal "[0,1]" (f '( "inf" "1") t t nil))))
  734. (ert-deftest test-org-table/org-table-make-reference/mode-string-L ()
  735. (fset 'f 'org-table-make-reference)
  736. ;; For Lisp formula only
  737. (should (equal "0" (f "0" nil nil 'literal)))
  738. (should (equal "z" (f "z" nil nil 'literal)))
  739. (should (equal "" (f "" nil nil 'literal)))
  740. (should (equal "0 1" (f '("0" "1") nil nil 'literal)))
  741. (should (equal "z 1" (f '("z" "1") nil nil 'literal)))
  742. (should (equal "1" (f '("" "1") nil nil 'literal)))
  743. (should (equal "" (f '("" "" ) nil nil 'literal))))
  744. (ert-deftest test-org-table/org-table-make-reference/mode-string-none ()
  745. (fset 'f 'org-table-make-reference)
  746. ;; For Lisp formula
  747. (should (equal "\"0\"" (f "0" nil nil t)))
  748. (should (equal "\"z\"" (f "z" nil nil t)))
  749. (should (equal "" (f "" nil nil t)))
  750. (should (equal "\"0\" \"1\"" (f '("0" "1") nil nil t)))
  751. (should (equal "\"z\" \"1\"" (f '("z" "1") nil nil t)))
  752. (should (equal "\"1\"" (f '("" "1") nil nil t)))
  753. (should (equal "" (f '("" "" ) nil nil t)))
  754. ;; For Calc formula
  755. (should (equal "(0)" (f "0" nil nil nil)))
  756. (should (equal "(z)" (f "z" nil nil nil)))
  757. (should (equal "(0)" (f "" nil nil nil)))
  758. (should (equal "[0,1]" (f '("0" "1") nil nil nil)))
  759. (should (equal "[z,1]" (f '("z" "1") nil nil nil)))
  760. (should (equal "[1]" (f '("" "1") nil nil nil)))
  761. (should (equal "[]" (f '("" "" ) nil nil nil)))
  762. ;; For Calc formula, special numbers
  763. (should (equal "(nan)" (f "nan" nil nil nil)))
  764. (should (equal "(uinf)" (f "uinf" nil nil nil)))
  765. (should (equal "(-inf)" (f "-inf" nil nil nil)))
  766. (should (equal "(inf)" (f "inf" nil nil nil)))
  767. (should (equal "[nan,1]" (f '( "nan" "1") nil nil nil)))
  768. (should (equal "[uinf,1]" (f '("uinf" "1") nil nil nil)))
  769. (should (equal "[-inf,1]" (f '("-inf" "1") nil nil nil)))
  770. (should (equal "[inf,1]" (f '( "inf" "1") nil nil nil))))
  771. (ert-deftest test-org-table/org-table-make-reference/mode-string-N ()
  772. (fset 'f 'org-table-make-reference)
  773. ;; For Lisp formula
  774. (should (equal "0" (f "0" nil t t)))
  775. (should (equal "0" (f "z" nil t t)))
  776. (should (equal "" (f "" nil t t)))
  777. (should (equal "0 1" (f '("0" "1") nil t t)))
  778. (should (equal "0 1" (f '("z" "1") nil t t)))
  779. (should (equal "1" (f '("" "1") nil t t)))
  780. (should (equal "" (f '("" "" ) nil t t)))
  781. ;; For Calc formula
  782. (should (equal "(0)" (f "0" nil t nil)))
  783. (should (equal "(0)" (f "z" nil t nil)))
  784. (should (equal "(0)" (f "" nil t nil)))
  785. (should (equal "[0,1]" (f '("0" "1") nil t nil)))
  786. (should (equal "[0,1]" (f '("z" "1") nil t nil)))
  787. (should (equal "[1]" (f '("" "1") nil t nil)))
  788. (should (equal "[]" (f '("" "" ) nil t nil)))
  789. ;; For Calc formula, special numbers
  790. (should (equal "(0)" (f "nan" nil t nil)))
  791. (should (equal "(0)" (f "uinf" nil t nil)))
  792. (should (equal "(0)" (f "-inf" nil t nil)))
  793. (should (equal "(0)" (f "inf" nil t nil)))
  794. (should (equal "[0,1]" (f '( "nan" "1") nil t nil)))
  795. (should (equal "[0,1]" (f '("uinf" "1") nil t nil)))
  796. (should (equal "[0,1]" (f '("-inf" "1") nil t nil)))
  797. (should (equal "[0,1]" (f '( "inf" "1") nil t nil))))
  798. (ert-deftest test-org-table/org-table-convert-refs-to-an/1 ()
  799. "Simple reference @1$1."
  800. (should
  801. (string= "A1" (org-table-convert-refs-to-an "@1$1"))))
  802. ;; TODO: Test broken
  803. ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
  804. ;; "Self reference @1$1."
  805. ;; (should
  806. ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
  807. (ert-deftest test-org-table/org-table-convert-refs-to-an/3 ()
  808. "Remote reference."
  809. (should
  810. (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
  811. (ert-deftest test-org-table/org-table-convert-refs-to-rc/1 ()
  812. "Simple reference @1$1."
  813. (should
  814. (string= "@1$1" (org-table-convert-refs-to-rc "A1"))))
  815. (ert-deftest test-org-table/org-table-convert-refs-to-rc/2 ()
  816. "Self reference $0."
  817. (should
  818. (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
  819. ;; TODO: Test Broken
  820. ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
  821. ;; "Remote reference."
  822. ;; (should
  823. ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
  824. (ert-deftest test-org-table/remote-reference-access ()
  825. "Access to remote reference.
  826. See also `test-org-table/copy-field'."
  827. (org-test-table-target-expect
  828. "
  829. #+NAME: table
  830. | | x 42 | |
  831. | replace | replace |
  832. "
  833. "
  834. #+NAME: table
  835. | | x 42 | |
  836. | x 42 | 84 x |
  837. "
  838. 1 (concat "#+TBLFM: "
  839. ;; Copy text without calculation: Use Lisp formula
  840. "$1 = '(identity remote(table, @1$2)) :: "
  841. ;; Do a calculation: Use Calc (or Lisp ) formula
  842. "$2 = 2 * remote(table, @1$2)")))
  843. (ert-deftest test-org-table/remote-reference-indirect ()
  844. "Access to remote reference with indirection of name or ID."
  845. (let ((source-tables "
  846. #+NAME: 2012
  847. | amount |
  848. |--------|
  849. | 1 |
  850. | 2 |
  851. |--------|
  852. | 3 |
  853. #+TBLFM: @>$1 = vsum(@I..@II)
  854. #+NAME: 2013
  855. | amount |
  856. |--------|
  857. | 4 |
  858. | 8 |
  859. |--------|
  860. | 12 |
  861. #+TBLFM: @>$1 = vsum(@I..@II)
  862. "))
  863. ;; Read several remote references from same column
  864. (org-test-table-target-expect
  865. (concat source-tables "
  866. #+NAME: summary
  867. | year | amount |
  868. |-------+---------|
  869. | 2012 | replace |
  870. | 2013 | replace |
  871. |-------+---------|
  872. | total | replace |
  873. ")
  874. (concat source-tables "
  875. #+NAME: summary
  876. | year | amount |
  877. |-------+--------|
  878. | 2012 | 3 |
  879. | 2013 | 12 |
  880. |-------+--------|
  881. | total | 15 |
  882. ")
  883. 1
  884. ;; Calc formula
  885. "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
  886. ;; Lisp formula
  887. (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
  888. "@>$2 = '(+ @I..@II); N"))
  889. ;; Read several remote references from same row
  890. (org-test-table-target-expect
  891. (concat source-tables "
  892. #+NAME: summary
  893. | year | 2012 | 2013 | total |
  894. |--------+---------+---------+---------|
  895. | amount | replace | replace | replace |
  896. ")
  897. (concat source-tables "
  898. #+NAME: summary
  899. | year | 2012 | 2013 | total |
  900. |--------+------+------+-------|
  901. | amount | 3 | 12 | 15 |
  902. ")
  903. 1
  904. ;; Calc formula
  905. "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
  906. ;; Lisp formula
  907. (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
  908. "@2$> = '(+ $<<..$>>); N"))))
  909. (ert-deftest test-org-table/org-at-TBLFM-p ()
  910. (org-test-with-temp-text-in-file
  911. "
  912. | 1 |
  913. | 2 |
  914. #+TBLFM: $2=$1*2
  915. "
  916. (goto-char (point-min))
  917. (forward-line 2)
  918. (should (equal (org-at-TBLFM-p) nil))
  919. (goto-char (point-min))
  920. (forward-line 3)
  921. (should (equal (org-at-TBLFM-p) t))
  922. (goto-char (point-min))
  923. (forward-line 4)
  924. (should (equal (org-at-TBLFM-p) nil))))
  925. (ert-deftest test-org-table/org-table-TBLFM-begin ()
  926. (org-test-with-temp-text-in-file
  927. "
  928. | 1 |
  929. | 2 |
  930. #+TBLFM: $2=$1*2
  931. "
  932. (goto-char (point-min))
  933. (should (equal (org-table-TBLFM-begin)
  934. nil))
  935. (goto-char (point-min))
  936. (forward-line 1)
  937. (should (equal (org-table-TBLFM-begin)
  938. nil))
  939. (goto-char (point-min))
  940. (forward-line 3)
  941. (should (= (org-table-TBLFM-begin)
  942. 14))
  943. (goto-char (point-min))
  944. (forward-line 4)
  945. (should (= (org-table-TBLFM-begin)
  946. 14))
  947. ))
  948. (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
  949. "For multiple #+TBLFM lines."
  950. (org-test-with-temp-text-in-file
  951. "
  952. | 1 |
  953. | 2 |
  954. #+TBLFM: $2=$1*1
  955. #+TBLFM: $2=$1*2
  956. "
  957. (goto-char (point-min))
  958. (should (equal (org-table-TBLFM-begin)
  959. nil))
  960. (goto-char (point-min))
  961. (forward-line 1)
  962. (should (equal (org-table-TBLFM-begin)
  963. nil))
  964. (goto-char (point-min))
  965. (forward-line 3)
  966. (should (= (org-table-TBLFM-begin)
  967. 14))
  968. (goto-char (point-min))
  969. (forward-line 4)
  970. (should (= (org-table-TBLFM-begin)
  971. 14))
  972. (goto-char (point-min))
  973. (forward-line 5)
  974. (should (= (org-table-TBLFM-begin)
  975. 14))
  976. ))
  977. (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
  978. (org-test-with-temp-text-in-file
  979. "
  980. | 1 |
  981. | 2 |
  982. #+TBLFM: $2=$1*1
  983. #+TBLFM: $2=$1*2
  984. | 6 |
  985. | 7 |
  986. #+TBLFM: $2=$1*1
  987. #+TBLFM: $2=$1*2
  988. "
  989. (goto-char (point-min))
  990. (should (equal (org-table-TBLFM-begin)
  991. nil))
  992. (goto-char (point-min))
  993. (forward-line 1)
  994. (should (equal (org-table-TBLFM-begin)
  995. nil))
  996. (goto-char (point-min))
  997. (forward-line 3)
  998. (should (= (org-table-TBLFM-begin)
  999. 14))
  1000. (goto-char (point-min))
  1001. (forward-line 4)
  1002. (should (= (org-table-TBLFM-begin)
  1003. 14))
  1004. (goto-char (point-min))
  1005. (forward-line 5)
  1006. (should (= (org-table-TBLFM-begin)
  1007. 14))
  1008. (goto-char (point-min))
  1009. (forward-line 6)
  1010. (should (= (org-table-TBLFM-begin)
  1011. 14))
  1012. (goto-char (point-min))
  1013. (forward-line 8)
  1014. (should (= (org-table-TBLFM-begin)
  1015. 61))
  1016. (goto-char (point-min))
  1017. (forward-line 9)
  1018. (should (= (org-table-TBLFM-begin)
  1019. 61))
  1020. (goto-char (point-min))
  1021. (forward-line 10)
  1022. (should (= (org-table-TBLFM-begin)
  1023. 61))))
  1024. (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
  1025. (org-test-with-temp-text-in-file
  1026. "
  1027. | 1 | |
  1028. | 2 | |
  1029. #+TBLFM: $2=$1*1
  1030. #+TBLFM: $2=$1*2
  1031. #+TBLFM: $2=$1*3
  1032. "
  1033. (let ((got (progn (goto-char (point-min))
  1034. (forward-line 3)
  1035. (org-table-calc-current-TBLFM)
  1036. (buffer-string)))
  1037. (expect "
  1038. | 1 | 1 |
  1039. | 2 | 2 |
  1040. #+TBLFM: $2=$1*1
  1041. #+TBLFM: $2=$1*2
  1042. #+TBLFM: $2=$1*3
  1043. "))
  1044. (should (string= got
  1045. expect)))
  1046. (let ((got (progn (goto-char (point-min))
  1047. (forward-line 4)
  1048. (org-table-calc-current-TBLFM)
  1049. (buffer-string)))
  1050. (expect "
  1051. | 1 | 2 |
  1052. | 2 | 4 |
  1053. #+TBLFM: $2=$1*1
  1054. #+TBLFM: $2=$1*2
  1055. #+TBLFM: $2=$1*3
  1056. "))
  1057. (should (string= got
  1058. expect)))))
  1059. (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
  1060. "org-table-calc-current-TBLFM should preserve the input as it was."
  1061. (org-test-with-temp-text-in-file
  1062. "
  1063. | 1 | 1 |
  1064. | 2 | 2 |
  1065. #+TBLFM: $2=$1*1
  1066. #+TBLFM: $2=$1*2::$2=$1*2
  1067. #+TBLFM: $2=$1*3
  1068. "
  1069. (let ((expect "
  1070. | 1 | 1 |
  1071. | 2 | 2 |
  1072. #+TBLFM: $2=$1*1
  1073. #+TBLFM: $2=$1*2::$2=$1*2
  1074. #+TBLFM: $2=$1*3
  1075. "))
  1076. (goto-char (point-min))
  1077. (forward-line 4)
  1078. (should-error (org-table-calc-current-TBLFM))
  1079. (setq got (buffer-string))
  1080. (message "%s" got)
  1081. (should (string= got
  1082. expect)))))
  1083. ;;; Radio Tables
  1084. (ert-deftest test-org-table/to-generic ()
  1085. "Test `orgtbl-to-generic' specifications."
  1086. ;; Test :hline parameter.
  1087. (should
  1088. (equal "a\nb"
  1089. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1090. '(:hline nil))))
  1091. (should
  1092. (equal "a\n~\nb"
  1093. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1094. '(:hline "~"))))
  1095. ;; Test :sep parameter.
  1096. (should
  1097. (equal "a!b\nc!d"
  1098. (orgtbl-to-generic
  1099. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1100. '(:sep "!"))))
  1101. ;; Test :hsep parameter.
  1102. (should
  1103. (equal "a!b\nc?d"
  1104. (orgtbl-to-generic
  1105. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1106. '(:sep "?" :hsep "!"))))
  1107. ;; Test :tstart parameter.
  1108. (should
  1109. (equal "<begin>\na"
  1110. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tstart "<begin>"))))
  1111. (should
  1112. (equal "<begin>\na"
  1113. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1114. '(:tstart (lambda () "<begin>")))))
  1115. (should
  1116. (equal "a"
  1117. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1118. '(:tstart "<begin>" :splice t))))
  1119. ;; Test :tend parameter.
  1120. (should
  1121. (equal "a\n<end>"
  1122. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tend "<end>"))))
  1123. (should
  1124. (equal "a\n<end>"
  1125. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1126. '(:tend (lambda () "<end>")))))
  1127. (should
  1128. (equal "a"
  1129. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1130. '(:tend "<end>" :splice t))))
  1131. ;; Test :lstart parameter.
  1132. (should
  1133. (equal "> a"
  1134. (orgtbl-to-generic
  1135. (org-table-to-lisp "| a |") '(:lstart "> "))))
  1136. (should
  1137. (equal "> a"
  1138. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1139. '(:lstart (lambda () "> ")))))
  1140. ;; Test :llstart parameter.
  1141. (should
  1142. (equal "> a\n>> b"
  1143. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1144. '(:lstart "> " :llstart ">> "))))
  1145. ;; Test :hlstart parameter.
  1146. (should
  1147. (equal "!> a\n> b"
  1148. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1149. '(:lstart "> " :hlstart "!> "))))
  1150. ;; Test :hllstart parameter.
  1151. (should
  1152. (equal "!> a\n!!> b\n> c"
  1153. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1154. '(:lstart "> " :hlstart "!> " :hllstart "!!> "))))
  1155. ;; Test :lend parameter.
  1156. (should
  1157. (equal "a <"
  1158. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:lend " <"))))
  1159. ;; Test :llend parameter.
  1160. (should
  1161. (equal "a <\nb <<"
  1162. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1163. '(:lend " <" :llend " <<"))))
  1164. ;; Test :hlend parameter.
  1165. (should
  1166. (equal "a <!\nb <"
  1167. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1168. '(:lend " <" :hlend " <!"))))
  1169. ;; Test :hllend parameter.
  1170. (should
  1171. (equal "a <!\nb <!!\nc <"
  1172. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1173. '(:lend " <" :hlend " <!" :hllend " <!!"))))
  1174. ;; Test :lfmt parameter.
  1175. (should
  1176. (equal "a!b"
  1177. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1178. '(:lfmt "%s!%s"))))
  1179. (should
  1180. (equal "a+b"
  1181. (orgtbl-to-generic
  1182. (org-table-to-lisp "| a | b |")
  1183. '(:lfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1184. (should
  1185. (equal "a!b"
  1186. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1187. '(:lfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1188. ;; Test :llfmt parameter.
  1189. (should
  1190. (equal "a!b"
  1191. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1192. '(:llfmt "%s!%s"))))
  1193. (should
  1194. (equal "a!b\nc+d"
  1195. (orgtbl-to-generic
  1196. (org-table-to-lisp "| a | b |\n| c | d |")
  1197. '(:lfmt "%s!%s" :llfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1198. (should
  1199. (equal "a!b"
  1200. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1201. '(:llfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1202. ;; Test :hlfmt parameter.
  1203. (should
  1204. (equal "a!b\ncd"
  1205. (orgtbl-to-generic
  1206. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1207. '(:hlfmt "%s!%s"))))
  1208. (should
  1209. (equal "a+b\ncd"
  1210. (orgtbl-to-generic
  1211. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1212. '(:hlfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1213. (should
  1214. (equal "a!b\n>c d<"
  1215. (orgtbl-to-generic
  1216. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1217. '(:hlfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1218. ;; Test :hllfmt parameter.
  1219. (should
  1220. (equal "a!b\ncd"
  1221. (orgtbl-to-generic
  1222. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1223. '(:hllfmt "%s!%s"))))
  1224. (should
  1225. (equal "a+b\ncd"
  1226. (orgtbl-to-generic
  1227. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1228. '(:hllfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1229. (should
  1230. (equal "a!b\n>c d<"
  1231. (orgtbl-to-generic
  1232. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1233. '(:hllfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1234. ;; Test :fmt parameter.
  1235. (should
  1236. (equal ">a<\n>b<"
  1237. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1238. '(:fmt ">%s<"))))
  1239. (should
  1240. (equal ">a<b"
  1241. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1242. '(:fmt (1 ">%s<" 2 (lambda (c) c))))))
  1243. (should
  1244. (equal "a b"
  1245. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1246. '(:fmt (2 " %s")))))
  1247. (should
  1248. (equal ">a<"
  1249. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1250. '(:fmt (lambda (c) (format ">%s<" c))))))
  1251. ;; Test :hfmt parameter.
  1252. (should
  1253. (equal ">a<\nb"
  1254. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1255. '(:hfmt ">%s<"))))
  1256. (should
  1257. (equal ">a<b\ncd"
  1258. (orgtbl-to-generic
  1259. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1260. '(:hfmt (1 ">%s<" 2 identity)))))
  1261. (should
  1262. (equal "a b\ncd"
  1263. (orgtbl-to-generic
  1264. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1265. '(:hfmt (2 " %s")))))
  1266. (should
  1267. (equal ">a<\nb"
  1268. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1269. '(:hfmt (lambda (c) (format ">%s<" c))))))
  1270. ;; Test :efmt parameter.
  1271. (should
  1272. (equal "2x10^3"
  1273. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1274. '(:efmt "%sx10^%s"))))
  1275. (should
  1276. (equal "2x10^3"
  1277. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1278. '(:efmt (lambda (m e) (concat m "x10^" e))))))
  1279. (should
  1280. (equal "2x10^3"
  1281. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1282. '(:efmt (1 "%sx10^%s")))))
  1283. (should
  1284. (equal "2x10^3"
  1285. (orgtbl-to-generic
  1286. (org-table-to-lisp "| 2e3 |")
  1287. '(:efmt (1 (lambda (m e) (format "%sx10^%s" m e)))))))
  1288. (should
  1289. (equal "2e3"
  1290. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |") '(:efmt nil))))
  1291. ;; Test :skip parameter.
  1292. (should
  1293. (equal "cd"
  1294. (orgtbl-to-generic
  1295. (org-table-to-lisp "| \ | <c> |\n| a | b |\n|---+---|\n| c | d |")
  1296. '(:skip 2))))
  1297. ;; Test :skipcols parameter.
  1298. (should
  1299. (equal "a\nc"
  1300. (orgtbl-to-generic
  1301. (org-table-to-lisp "| a | b |\n| c | d |") '(:skipcols (2)))))
  1302. (should
  1303. (equal "a\nc"
  1304. (orgtbl-to-generic
  1305. (org-table-to-lisp
  1306. "| / | <c> | <c> |\n| # | a | b |\n|---+---+---|\n| | c | d |")
  1307. '(:skipcols (2)))))
  1308. ;; Test :raw parameter.
  1309. (when (featurep 'ox-latex)
  1310. (should
  1311. (org-string-match-p
  1312. "/a/"
  1313. (orgtbl-to-generic (org-table-to-lisp "| /a/ | b |")
  1314. '(:backend latex :raw t))))))
  1315. (ert-deftest test-org-table/to-latex ()
  1316. "Test `orgtbl-to-latex' specifications."
  1317. (should
  1318. (equal "\\begin{tabular}{l}\na\\\\\n\\end{tabular}"
  1319. (orgtbl-to-latex (org-table-to-lisp "| a |") nil)))
  1320. ;; Test :environment parameter.
  1321. (should
  1322. (equal "\\begin{tabularx}{l}\na\\\\\n\\end{tabularx}"
  1323. (orgtbl-to-latex (org-table-to-lisp "| a |")
  1324. '(:environment "tabularx"))))
  1325. ;; Test :booktabs parameter.
  1326. (should
  1327. (org-string-match-p
  1328. "\\toprule" (orgtbl-to-latex (org-table-to-lisp "| a |") '(:booktabs t)))))
  1329. (ert-deftest test-org-table/to-html ()
  1330. "Test `orgtbl-to-html' specifications."
  1331. (should
  1332. (equal (orgtbl-to-html (org-table-to-lisp "| a |") nil)
  1333. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">
  1334. <colgroup>
  1335. <col class=\"left\" />
  1336. </colgroup>
  1337. <tbody>
  1338. <tr>
  1339. <td class=\"left\">a</td>
  1340. </tr>
  1341. </tbody>
  1342. </table>"))
  1343. ;; Test :attributes parameter.
  1344. (should
  1345. (org-string-match-p
  1346. "<table>"
  1347. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes nil))))
  1348. (should
  1349. (org-string-match-p
  1350. "<table border=\"2\">"
  1351. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes (:border "2"))))))
  1352. (ert-deftest test-org-table/to-texinfo ()
  1353. "Test `orgtbl-to-texinfo' specifications."
  1354. (should
  1355. (equal "@multitable {a}\n@item a\n@end multitable"
  1356. (orgtbl-to-texinfo (org-table-to-lisp "| a |") nil)))
  1357. ;; Test :columns parameter.
  1358. (should
  1359. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1360. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1361. '(:columns ".4 .6"))))
  1362. (should
  1363. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1364. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1365. '(:columns "@columnfractions .4 .6"))))
  1366. (should
  1367. (equal "@multitable {xxx} {xx}\n@item a\n@tab b\n@end multitable"
  1368. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1369. '(:columns "{xxx} {xx}")))))
  1370. (ert-deftest test-org-table/to-orgtbl ()
  1371. "Test `orgtbl-to-orgtbl' specifications."
  1372. (should
  1373. (equal "| a | b |\n|---+---|\n| c | d |"
  1374. (orgtbl-to-orgtbl
  1375. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |") nil))))
  1376. (ert-deftest test-org-table/to-unicode ()
  1377. "Test `orgtbl-to-unicode' specifications."
  1378. (should
  1379. (equal "━━━\n a \n━━━"
  1380. (orgtbl-to-unicode (org-table-to-lisp "| a |") nil)))
  1381. ;; Test :narrow parameter.
  1382. (should
  1383. (equal "━━━━\n => \n━━━━"
  1384. (orgtbl-to-unicode (org-table-to-lisp "| <2> |\n| xxx |")
  1385. '(:narrow t)))))
  1386. (ert-deftest test-org-table/send-region ()
  1387. "Test `orgtbl-send-table' specifications."
  1388. ;; Error when not at a table.
  1389. (should-error
  1390. (org-test-with-temp-text "Paragraph"
  1391. (orgtbl-send-table)))
  1392. ;; Error when destination is missing.
  1393. (should-error
  1394. (org-test-with-temp-text "#+ORGTBL: SEND\n<point>| a |"
  1395. (orgtbl-send-table)))
  1396. ;; Error when transformation function is not specified.
  1397. (should-error
  1398. (org-test-with-temp-text "
  1399. # BEGIN RECEIVE ORGTBL table
  1400. # END RECEIVE ORGTBL table
  1401. #+ORGTBL: SEND table
  1402. <point>| a |"
  1403. (orgtbl-send-table)))
  1404. ;; Standard test.
  1405. (should
  1406. (equal "| a |\n|---|\n| b |\n"
  1407. (org-test-with-temp-text "
  1408. # BEGIN RECEIVE ORGTBL table
  1409. # END RECEIVE ORGTBL table
  1410. #+ORGTBL: SEND table orgtbl-to-orgtbl :hlines nil
  1411. <point>| a |\n|---|\n| b |"
  1412. (orgtbl-send-table)
  1413. (goto-char (point-min))
  1414. (buffer-substring-no-properties
  1415. (search-forward "# BEGIN RECEIVE ORGTBL table\n")
  1416. (progn (search-forward "# END RECEIVE ORGTBL table")
  1417. (match-beginning 0)))))))
  1418. (provide 'test-org-table)
  1419. ;;; test-org-table.el ends here