test-org-table.el 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. "
  528. | 0 | replace |
  529. | a b | replace |
  530. | c d | replace |
  531. | | replace |
  532. | 2012-12 | replace |
  533. | [2012-12-31 Mon] | replace |
  534. "))
  535. ;; Lisp formula to copy literally
  536. (org-test-table-target-expect
  537. target
  538. "
  539. | 0 | 0 |
  540. | a b | a b |
  541. | c d | c d |
  542. | | |
  543. | 2012-12 | 2012-12 |
  544. | [2012-12-31 Mon] | [2012-12-31 Mon] |
  545. "
  546. 1 "#+TBLFM: $2 = '(identity $1)")
  547. ;; Calc formula to copy quite literally
  548. (org-test-table-target-expect
  549. target
  550. "
  551. | 0 | 0 |
  552. | a b | a b |
  553. | c d | c d |
  554. | | |
  555. | 2012-12 | 2012-12 |
  556. | [2012-12-31 Mon] | <2012-12-31 Mon> |
  557. "
  558. 1 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  559. "string(\"\"), string(subvec(\"$1\", 2, vlen(\"$1\")))); E"))
  560. ;; Calc formula simple
  561. (org-test-table-target-expect
  562. target
  563. "
  564. | 0 | 0 |
  565. | a b | a b |
  566. | c d | c d |
  567. | | |
  568. | 2012-12 | 2000 |
  569. | [2012-12-31 Mon] | <2012-12-31 Mon> |
  570. "
  571. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1); E")))
  572. ;; End of table examples and beginning of internal tests.
  573. (ert-deftest test-org-table/org-table-make-reference/mode-string-EL ()
  574. (fset 'f 'org-table-make-reference)
  575. ;; For Lisp formula only
  576. (should (equal "0" (f "0" t nil 'literal)))
  577. (should (equal "z" (f "z" t nil 'literal)))
  578. (should (equal "" (f "" t nil 'literal)))
  579. (should (equal "0 1" (f '("0" "1") t nil 'literal)))
  580. (should (equal "z 1" (f '("z" "1") t nil 'literal)))
  581. (should (equal " 1" (f '("" "1") t nil 'literal)))
  582. (should (equal " " (f '("" "" ) t nil 'literal))))
  583. (ert-deftest test-org-table/org-table-make-reference/mode-string-E ()
  584. (fset 'f 'org-table-make-reference)
  585. ;; For Lisp formula
  586. (should (equal "\"0\"" (f "0" t nil t)))
  587. (should (equal "\"z\"" (f "z" t nil t)))
  588. (should (equal "\"\"" (f "" t nil t)))
  589. (should (equal "\"0\" \"1\"" (f '("0" "1") t nil t)))
  590. (should (equal "\"z\" \"1\"" (f '("z" "1") t nil t)))
  591. (should (equal "\"\" \"1\"" (f '("" "1") t nil t)))
  592. (should (equal "\"\" \"\"" (f '("" "" ) t nil t)))
  593. ;; For Calc formula
  594. (should (equal "(0)" (f "0" t nil nil)))
  595. (should (equal "(z)" (f "z" t nil nil)))
  596. (should (equal "nan" (f "" t nil nil)))
  597. (should (equal "[0,1]" (f '("0" "1") t nil nil)))
  598. (should (equal "[z,1]" (f '("z" "1") t nil nil)))
  599. (should (equal "[nan,1]" (f '("" "1") t nil nil)))
  600. (should (equal "[nan,nan]" (f '("" "" ) t nil nil)))
  601. ;; For Calc formula, special numbers
  602. (should (equal "(nan)" (f "nan" t nil nil)))
  603. (should (equal "(uinf)" (f "uinf" t nil nil)))
  604. (should (equal "(-inf)" (f "-inf" t nil nil)))
  605. (should (equal "(inf)" (f "inf" t nil nil)))
  606. (should (equal "[nan,1]" (f '( "nan" "1") t nil nil)))
  607. (should (equal "[uinf,1]" (f '("uinf" "1") t nil nil)))
  608. (should (equal "[-inf,1]" (f '("-inf" "1") t nil nil)))
  609. (should (equal "[inf,1]" (f '( "inf" "1") t nil nil))))
  610. (ert-deftest test-org-table/org-table-make-reference/mode-string-EN ()
  611. (fset 'f 'org-table-make-reference)
  612. ;; For Lisp formula
  613. (should (equal "0" (f "0" t t t)))
  614. (should (equal "0" (f "z" t t t)))
  615. (should (equal "0" (f "" t t t)))
  616. (should (equal "0 1" (f '("0" "1") t t t)))
  617. (should (equal "0 1" (f '("z" "1") t t t)))
  618. (should (equal "0 1" (f '("" "1") t t t)))
  619. (should (equal "0 0" (f '("" "" ) t t t)))
  620. ;; For Calc formula
  621. (should (equal "(0)" (f "0" t t nil)))
  622. (should (equal "(0)" (f "z" t t nil)))
  623. (should (equal "(0)" (f "" t t nil)))
  624. (should (equal "[0,1]" (f '("0" "1") t t nil)))
  625. (should (equal "[0,1]" (f '("z" "1") t t nil)))
  626. (should (equal "[0,1]" (f '("" "1") t t nil)))
  627. (should (equal "[0,0]" (f '("" "" ) t t nil)))
  628. ;; For Calc formula, special numbers
  629. (should (equal "(0)" (f "nan" t t nil)))
  630. (should (equal "(0)" (f "uinf" t t nil)))
  631. (should (equal "(0)" (f "-inf" t t nil)))
  632. (should (equal "(0)" (f "inf" t t nil)))
  633. (should (equal "[0,1]" (f '( "nan" "1") t t nil)))
  634. (should (equal "[0,1]" (f '("uinf" "1") t t nil)))
  635. (should (equal "[0,1]" (f '("-inf" "1") t t nil)))
  636. (should (equal "[0,1]" (f '( "inf" "1") t t nil))))
  637. (ert-deftest test-org-table/org-table-make-reference/mode-string-L ()
  638. (fset 'f 'org-table-make-reference)
  639. ;; For Lisp formula only
  640. (should (equal "0" (f "0" nil nil 'literal)))
  641. (should (equal "z" (f "z" nil nil 'literal)))
  642. (should (equal "" (f "" nil nil 'literal)))
  643. (should (equal "0 1" (f '("0" "1") nil nil 'literal)))
  644. (should (equal "z 1" (f '("z" "1") nil nil 'literal)))
  645. (should (equal "1" (f '("" "1") nil nil 'literal)))
  646. (should (equal "" (f '("" "" ) nil nil 'literal))))
  647. (ert-deftest test-org-table/org-table-make-reference/mode-string-none ()
  648. (fset 'f 'org-table-make-reference)
  649. ;; For Lisp formula
  650. (should (equal "\"0\"" (f "0" nil nil t)))
  651. (should (equal "\"z\"" (f "z" nil nil t)))
  652. (should (equal "" (f "" nil nil t)))
  653. (should (equal "\"0\" \"1\"" (f '("0" "1") nil nil t)))
  654. (should (equal "\"z\" \"1\"" (f '("z" "1") nil nil t)))
  655. (should (equal "\"1\"" (f '("" "1") nil nil t)))
  656. (should (equal "" (f '("" "" ) nil nil t)))
  657. ;; For Calc formula
  658. (should (equal "(0)" (f "0" nil nil nil)))
  659. (should (equal "(z)" (f "z" nil nil nil)))
  660. (should (equal "(0)" (f "" nil nil nil)))
  661. (should (equal "[0,1]" (f '("0" "1") nil nil nil)))
  662. (should (equal "[z,1]" (f '("z" "1") nil nil nil)))
  663. (should (equal "[1]" (f '("" "1") nil nil nil)))
  664. (should (equal "[]" (f '("" "" ) nil nil nil)))
  665. ;; For Calc formula, special numbers
  666. (should (equal "(nan)" (f "nan" nil nil nil)))
  667. (should (equal "(uinf)" (f "uinf" nil nil nil)))
  668. (should (equal "(-inf)" (f "-inf" nil nil nil)))
  669. (should (equal "(inf)" (f "inf" nil nil nil)))
  670. (should (equal "[nan,1]" (f '( "nan" "1") nil nil nil)))
  671. (should (equal "[uinf,1]" (f '("uinf" "1") nil nil nil)))
  672. (should (equal "[-inf,1]" (f '("-inf" "1") nil nil nil)))
  673. (should (equal "[inf,1]" (f '( "inf" "1") nil nil nil))))
  674. (ert-deftest test-org-table/org-table-make-reference/mode-string-N ()
  675. (fset 'f 'org-table-make-reference)
  676. ;; For Lisp formula
  677. (should (equal "0" (f "0" nil t t)))
  678. (should (equal "0" (f "z" nil t t)))
  679. (should (equal "" (f "" nil t t)))
  680. (should (equal "0 1" (f '("0" "1") nil t t)))
  681. (should (equal "0 1" (f '("z" "1") nil t t)))
  682. (should (equal "1" (f '("" "1") nil t t)))
  683. (should (equal "" (f '("" "" ) nil t t)))
  684. ;; For Calc formula
  685. (should (equal "(0)" (f "0" nil t nil)))
  686. (should (equal "(0)" (f "z" nil t nil)))
  687. (should (equal "(0)" (f "" nil t nil)))
  688. (should (equal "[0,1]" (f '("0" "1") nil t nil)))
  689. (should (equal "[0,1]" (f '("z" "1") nil t nil)))
  690. (should (equal "[1]" (f '("" "1") nil t nil)))
  691. (should (equal "[]" (f '("" "" ) nil t nil)))
  692. ;; For Calc formula, special numbers
  693. (should (equal "(0)" (f "nan" nil t nil)))
  694. (should (equal "(0)" (f "uinf" nil t nil)))
  695. (should (equal "(0)" (f "-inf" nil t nil)))
  696. (should (equal "(0)" (f "inf" nil t nil)))
  697. (should (equal "[0,1]" (f '( "nan" "1") nil t nil)))
  698. (should (equal "[0,1]" (f '("uinf" "1") nil t nil)))
  699. (should (equal "[0,1]" (f '("-inf" "1") nil t nil)))
  700. (should (equal "[0,1]" (f '( "inf" "1") nil t nil))))
  701. (ert-deftest test-org-table/org-table-convert-refs-to-an/1 ()
  702. "Simple reference @1$1."
  703. (should
  704. (string= "A1" (org-table-convert-refs-to-an "@1$1"))))
  705. ;; TODO: Test broken
  706. ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
  707. ;; "Self reference @1$1."
  708. ;; (should
  709. ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
  710. (ert-deftest test-org-table/org-table-convert-refs-to-an/3 ()
  711. "Remote reference."
  712. (should
  713. (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
  714. (ert-deftest test-org-table/org-table-convert-refs-to-rc/1 ()
  715. "Simple reference @1$1."
  716. (should
  717. (string= "@1$1" (org-table-convert-refs-to-rc "A1"))))
  718. (ert-deftest test-org-table/org-table-convert-refs-to-rc/2 ()
  719. "Self reference $0."
  720. (should
  721. (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
  722. ;; TODO: Test Broken
  723. ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
  724. ;; "Remote reference."
  725. ;; (should
  726. ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
  727. (ert-deftest test-org-table/remote-reference-access ()
  728. "Access to remote reference.
  729. See also `test-org-table/copy-field'."
  730. (org-test-table-target-expect
  731. "
  732. #+NAME: table
  733. | | x 42 | |
  734. | replace | replace |
  735. "
  736. "
  737. #+NAME: table
  738. | | x 42 | |
  739. | x 42 | 84 x |
  740. "
  741. 1 (concat "#+TBLFM: "
  742. ;; Copy text without calculation: Use Lisp formula
  743. "$1 = '(identity remote(table, @1$2)) :: "
  744. ;; Do a calculation: Use Calc (or Lisp ) formula
  745. "$2 = 2 * remote(table, @1$2)")))
  746. (ert-deftest test-org-table/org-at-TBLFM-p ()
  747. (org-test-with-temp-text-in-file
  748. "
  749. | 1 |
  750. | 2 |
  751. #+TBLFM: $2=$1*2
  752. "
  753. (goto-char (point-min))
  754. (forward-line 2)
  755. (should (equal (org-at-TBLFM-p) nil))
  756. (goto-char (point-min))
  757. (forward-line 3)
  758. (should (equal (org-at-TBLFM-p) t))
  759. (goto-char (point-min))
  760. (forward-line 4)
  761. (should (equal (org-at-TBLFM-p) nil))))
  762. (ert-deftest test-org-table/org-table-TBLFM-begin ()
  763. (org-test-with-temp-text-in-file
  764. "
  765. | 1 |
  766. | 2 |
  767. #+TBLFM: $2=$1*2
  768. "
  769. (goto-char (point-min))
  770. (should (equal (org-table-TBLFM-begin)
  771. nil))
  772. (goto-char (point-min))
  773. (forward-line 1)
  774. (should (equal (org-table-TBLFM-begin)
  775. nil))
  776. (goto-char (point-min))
  777. (forward-line 3)
  778. (should (= (org-table-TBLFM-begin)
  779. 14))
  780. (goto-char (point-min))
  781. (forward-line 4)
  782. (should (= (org-table-TBLFM-begin)
  783. 14))
  784. ))
  785. (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
  786. "For multiple #+TBLFM lines."
  787. (org-test-with-temp-text-in-file
  788. "
  789. | 1 |
  790. | 2 |
  791. #+TBLFM: $2=$1*1
  792. #+TBLFM: $2=$1*2
  793. "
  794. (goto-char (point-min))
  795. (should (equal (org-table-TBLFM-begin)
  796. nil))
  797. (goto-char (point-min))
  798. (forward-line 1)
  799. (should (equal (org-table-TBLFM-begin)
  800. nil))
  801. (goto-char (point-min))
  802. (forward-line 3)
  803. (should (= (org-table-TBLFM-begin)
  804. 14))
  805. (goto-char (point-min))
  806. (forward-line 4)
  807. (should (= (org-table-TBLFM-begin)
  808. 14))
  809. (goto-char (point-min))
  810. (forward-line 5)
  811. (should (= (org-table-TBLFM-begin)
  812. 14))
  813. ))
  814. (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
  815. (org-test-with-temp-text-in-file
  816. "
  817. | 1 |
  818. | 2 |
  819. #+TBLFM: $2=$1*1
  820. #+TBLFM: $2=$1*2
  821. | 6 |
  822. | 7 |
  823. #+TBLFM: $2=$1*1
  824. #+TBLFM: $2=$1*2
  825. "
  826. (goto-char (point-min))
  827. (should (equal (org-table-TBLFM-begin)
  828. nil))
  829. (goto-char (point-min))
  830. (forward-line 1)
  831. (should (equal (org-table-TBLFM-begin)
  832. nil))
  833. (goto-char (point-min))
  834. (forward-line 3)
  835. (should (= (org-table-TBLFM-begin)
  836. 14))
  837. (goto-char (point-min))
  838. (forward-line 4)
  839. (should (= (org-table-TBLFM-begin)
  840. 14))
  841. (goto-char (point-min))
  842. (forward-line 5)
  843. (should (= (org-table-TBLFM-begin)
  844. 14))
  845. (goto-char (point-min))
  846. (forward-line 6)
  847. (should (= (org-table-TBLFM-begin)
  848. 14))
  849. (goto-char (point-min))
  850. (forward-line 8)
  851. (should (= (org-table-TBLFM-begin)
  852. 61))
  853. (goto-char (point-min))
  854. (forward-line 9)
  855. (should (= (org-table-TBLFM-begin)
  856. 61))
  857. (goto-char (point-min))
  858. (forward-line 10)
  859. (should (= (org-table-TBLFM-begin)
  860. 61))))
  861. (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
  862. (org-test-with-temp-text-in-file
  863. "
  864. | 1 | |
  865. | 2 | |
  866. #+TBLFM: $2=$1*1
  867. #+TBLFM: $2=$1*2
  868. #+TBLFM: $2=$1*3
  869. "
  870. (let ((got (progn (goto-char (point-min))
  871. (forward-line 3)
  872. (org-table-calc-current-TBLFM)
  873. (buffer-string)))
  874. (expect "
  875. | 1 | 1 |
  876. | 2 | 2 |
  877. #+TBLFM: $2=$1*1
  878. #+TBLFM: $2=$1*2
  879. #+TBLFM: $2=$1*3
  880. "))
  881. (should (string= got
  882. expect)))
  883. (let ((got (progn (goto-char (point-min))
  884. (forward-line 4)
  885. (org-table-calc-current-TBLFM)
  886. (buffer-string)))
  887. (expect "
  888. | 1 | 2 |
  889. | 2 | 4 |
  890. #+TBLFM: $2=$1*1
  891. #+TBLFM: $2=$1*2
  892. #+TBLFM: $2=$1*3
  893. "))
  894. (should (string= got
  895. expect)))))
  896. (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
  897. "org-table-calc-current-TBLFM should preserve the input as it was."
  898. (org-test-with-temp-text-in-file
  899. "
  900. | 1 | 1 |
  901. | 2 | 2 |
  902. #+TBLFM: $2=$1*1
  903. #+TBLFM: $2=$1*2::$2=$1*2
  904. #+TBLFM: $2=$1*3
  905. "
  906. (let ((expect "
  907. | 1 | 1 |
  908. | 2 | 2 |
  909. #+TBLFM: $2=$1*1
  910. #+TBLFM: $2=$1*2::$2=$1*2
  911. #+TBLFM: $2=$1*3
  912. "))
  913. (goto-char (point-min))
  914. (forward-line 4)
  915. (should-error (org-table-calc-current-TBLFM))
  916. (setq got (buffer-string))
  917. (message "%s" got)
  918. (should (string= got
  919. expect)))))
  920. (provide 'test-org-table)
  921. ;;; test-org-table.el ends here