test-org-table.el 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. First the tests that are
  17. ;; also a howto example collection as a user documentation, more or
  18. ;; less all those using `org-test-table-target-expect'. Then the
  19. ;; internal and more abstract tests. See also the doc string of
  20. ;; `org-test-table-target-expect'.
  21. ;;; Code:
  22. (require 'org-table) ; `org-table-make-reference'
  23. (ert-deftest test-org-table/simple-formula/no-grouping/no-title-row ()
  24. "Simple sum without grouping rows, without title row."
  25. (org-test-table-target-expect
  26. "
  27. | 2 |
  28. | 4 |
  29. | 8 |
  30. | replace |
  31. "
  32. "
  33. | 2 |
  34. | 4 |
  35. | 8 |
  36. | 14 |
  37. "
  38. 1
  39. ;; Calc formula
  40. "#+TBLFM: @>$1 = vsum(@<..@>>)"
  41. ;; Lisp formula
  42. "#+TBLFM: @>$1 = '(+ @<..@>>); N"))
  43. (ert-deftest test-org-table/simple-formula/no-grouping/with-title-row ()
  44. "Simple sum without grouping rows, with title row."
  45. (org-test-table-target-expect
  46. "
  47. | foo |
  48. |---------|
  49. | 2 |
  50. | 4 |
  51. | 8 |
  52. | replace |
  53. "
  54. "
  55. | foo |
  56. |-----|
  57. | 2 |
  58. | 4 |
  59. | 8 |
  60. | 14 |
  61. "
  62. 1
  63. ;; Calc formula
  64. "#+TBLFM: @>$1 = vsum(@I..@>>)"
  65. ;; Lisp formula
  66. "#+TBLFM: @>$1 = '(+ @I..@>>); N"))
  67. (ert-deftest test-org-table/simple-formula/with-grouping/no-title-row ()
  68. "Simple sum with grouping rows, how not to do."
  69. ;; The first example has a problem, see the second example in this
  70. ;; ert-deftest.
  71. (org-test-table-target-expect
  72. "
  73. | 2 |
  74. | 4 |
  75. | 8 |
  76. |---------|
  77. | replace |
  78. "
  79. "
  80. | 2 |
  81. | 4 |
  82. | 8 |
  83. |----|
  84. | 14 |
  85. "
  86. 1
  87. ;; Calc formula
  88. "#+TBLFM: $1 = vsum(@<..@>>)"
  89. ;; Lisp formula
  90. "#+TBLFM: $1 = '(+ @<..@>>); N")
  91. ;; The problem is that the first three rows with the summands are
  92. ;; considered the header and therefore column formulas are not
  93. ;; applied on them as shown below. Also export behaves unexpected.
  94. ;; See next ert-deftest how to group rows right.
  95. (org-test-table-target-expect
  96. "
  97. | 2 | replace |
  98. | 4 | replace |
  99. | 8 | replace |
  100. |---------+---------|
  101. | replace | replace |
  102. "
  103. "
  104. | 2 | replace |
  105. | 4 | replace |
  106. | 8 | replace |
  107. |----+---------|
  108. | 14 | 28 |
  109. "
  110. 2
  111. ;; Calc formula
  112. "#+TBLFM: @>$1 = vsum(@<..@>>) :: $2 = 2 * $1"
  113. ;; Lisp formula
  114. "#+TBLFM: @>$1 = '(+ @<..@>>); N :: $2 = '(* 2 $1); N"))
  115. (ert-deftest test-org-table/simple-formula/with-grouping/with-title-row ()
  116. "Simple sum with grouping rows, how to do it right."
  117. ;; Always add a top row with the column names separated by hline to
  118. ;; get the desired header when you want to group rows.
  119. (org-test-table-target-expect
  120. "
  121. | foo | bar |
  122. |---------+---------|
  123. | 2 | replace |
  124. | 4 | replace |
  125. | 8 | replace |
  126. |---------+---------|
  127. | replace | replace |
  128. "
  129. "
  130. | foo | bar |
  131. |-----+-----|
  132. | 2 | 4 |
  133. | 4 | 8 |
  134. | 8 | 16 |
  135. |-----+-----|
  136. | 14 | 28 |
  137. "
  138. 2
  139. ;; Calc formula
  140. "#+TBLFM: @>$1 = vsum(@I..@>>) :: $2 = 2 * $1"
  141. ;; Lisp formula
  142. "#+TBLFM: @>$1 = '(+ @I..@>>); N :: $2 = '(* 2 $1); N"))
  143. (ert-deftest test-org-table/align ()
  144. "Align columns within Org buffer, depends on `org-table-number-regexp'."
  145. (org-test-table-target-expect "
  146. | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
  147. | ab | 12 | 12.2 | 2.4e-08 | 2x10^12 | 4.034+-0.02 | 2.7(10) | >3.5 |
  148. | ab | ab | ab | ab | ab | ab | ab | ab |
  149. ")
  150. (org-test-table-target-expect "
  151. | 0 | 0 | 0 | 0 | 0 | 0 |
  152. | <-0x0ab.cf | >-36#0vw.yz | nan | uinf | -inf | inf |
  153. | ab | ab | ab | ab | ab | ab |
  154. "))
  155. (defconst references/target-normal "
  156. | 0 | 1 | replace | replace | replace | replace | replace | replace |
  157. | z | 1 | replace | replace | replace | replace | replace | replace |
  158. | | 1 | replace | replace | replace | replace | replace | replace |
  159. | | | replace | replace | replace | replace | replace | replace |
  160. "
  161. "Normal numbers and non-numbers for Lisp and Calc formula.")
  162. (defconst references/target-special "
  163. | nan | 1 | replace | replace | replace | replace | replace | replace |
  164. | uinf | 1 | replace | replace | replace | replace | replace | replace |
  165. | -inf | 1 | replace | replace | replace | replace | replace | replace |
  166. | inf | 1 | replace | replace | replace | replace | replace | replace |
  167. "
  168. "Special numbers for Calc formula.")
  169. (ert-deftest test-org-table/references/mode-string-EL ()
  170. "Basic: Assign field reference, sum of field references, sum
  171. and len of simple range reference (no row) and complex range
  172. reference (with row). Mode string EL."
  173. ;; Empty fields are kept during parsing field but lost as list
  174. ;; elements within Lisp formula syntactically when used literally
  175. ;; and not enclosed with " within fields, see last columns with len.
  176. (org-test-table-target-expect
  177. references/target-normal
  178. ;; All the #ERROR show that for Lisp calculations N has to be used.
  179. "
  180. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  181. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  182. | | 1 | | 1 | 1 | 1 | 1 | 1 |
  183. | | | | 0 | 0 | 0 | 0 | 0 |
  184. "
  185. 1 (concat
  186. "#+TBLFM: $3 = '(identity \"$1\"); EL :: $4 = '(+ $1 $2); EL :: "
  187. "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
  188. "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL"))
  189. ;; Empty fields are kept during parsing field _and_ as list elements
  190. ;; within Lisp formula syntactically even when used literally when
  191. ;; enclosed with " within fields, see last columns with len.
  192. (org-test-table-target-expect
  193. "
  194. | \"0\" | \"1\" | repl | repl | repl | repl | repl | repl |
  195. | \"z\" | \"1\" | repl | repl | repl | repl | repl | repl |
  196. | \"\" | \"1\" | repl | repl | repl | repl | repl | repl |
  197. | \"\" | \"\" | repl | repl | repl | repl | repl | repl |
  198. "
  199. "
  200. | \"0\" | \"1\" | \"0\" | 1 | #ERROR | #ERROR | 2 | 2 |
  201. | \"z\" | \"1\" | \"z\" | 1 | #ERROR | #ERROR | 2 | 2 |
  202. | \"\" | \"1\" | \"\" | 1 | #ERROR | #ERROR | 2 | 2 |
  203. | \"\" | \"\" | \"\" | 0 | #ERROR | #ERROR | 2 | 2 |
  204. "
  205. 1 (concat
  206. "#+TBLFM: $3 = '(concat \"\\\"\" $1 \"\\\"\"); EL :: "
  207. "$4 = '(+ (string-to-number $1) (string-to-number $2)); EL :: "
  208. "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
  209. "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL")))
  210. (ert-deftest test-org-table/references/mode-string-E ()
  211. "Basic: Assign field reference, sum of field references, sum
  212. and len of simple range reference (no row) and complex range
  213. reference (with row). Mode string E."
  214. (let ((lisp
  215. (concat
  216. "#+TBLFM: $3 = '(identity $1); E :: $4 = '(+ $1 $2); E :: "
  217. "$5 = '(+ $1..$2); E :: $6 = '(+ @0$1..@0$2); E :: "
  218. "$7 = '(length '($1..$2)); E :: $8 = '(length '(@0$1..@0$2)); E"))
  219. (calc
  220. (concat
  221. "#+TBLFM: $3 = $1; E :: $4 = $1 + $2; E :: "
  222. "$5 = vsum($1..$2); E :: $6 = vsum(@0$1..@0$2); E :: "
  223. "$7 = vlen($1..$2); E :: $8 = vlen(@0$1..@0$2); E")))
  224. (org-test-table-target-expect
  225. references/target-normal
  226. ;; All the #ERROR show that for Lisp calculations N has to be used.
  227. "
  228. | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
  229. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  230. | | 1 | | #ERROR | #ERROR | #ERROR | 2 | 2 |
  231. | | | | #ERROR | #ERROR | #ERROR | 2 | 2 |
  232. "
  233. 1 lisp)
  234. (org-test-table-target-expect
  235. references/target-normal
  236. "
  237. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  238. | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
  239. | | 1 | nan | nan | nan | nan | 2 | 2 |
  240. | | | nan | nan | nan | nan | 2 | 2 |
  241. "
  242. 1 calc)
  243. (org-test-table-target-expect
  244. references/target-special
  245. "
  246. | nan | 1 | nan | nan | nan | nan | 2 | 2 |
  247. | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
  248. | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
  249. | inf | 1 | inf | inf | inf | inf | 2 | 2 |
  250. "
  251. 1 calc)))
  252. (ert-deftest test-org-table/references/mode-string-EN ()
  253. "Basic: Assign field reference, sum of field references, sum
  254. and len of simple range reference (no row) and complex range
  255. reference (with row). Mode string EN."
  256. (let ((lisp (concat
  257. "#+TBLFM: $3 = '(identity $1); EN :: $4 = '(+ $1 $2); EN :: "
  258. "$5 = '(+ $1..$2); EN :: $6 = '(+ @0$1..@0$2); EN :: "
  259. "$7 = '(length '($1..$2)); EN :: "
  260. "$8 = '(length '(@0$1..@0$2)); EN"))
  261. (calc (concat
  262. "#+TBLFM: $3 = $1; EN :: $4 = $1 + $2; EN :: "
  263. "$5 = vsum($1..$2); EN :: $6 = vsum(@0$1..@0$2); EN :: "
  264. "$7 = vlen($1..$2); EN :: $8 = vlen(@0$1..@0$2); EN")))
  265. (org-test-table-target-expect
  266. references/target-normal
  267. "
  268. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  269. | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  270. | | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  271. | | | 0 | 0 | 0 | 0 | 2 | 2 |
  272. "
  273. 1 lisp calc)
  274. (org-test-table-target-expect
  275. references/target-special
  276. "
  277. | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  278. | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  279. | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  280. | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  281. "
  282. 1 calc)))
  283. (ert-deftest test-org-table/references/mode-string-L ()
  284. "Basic: Assign field reference, sum of field references, sum
  285. and len of simple range reference (no row) and complex range
  286. reference (with row). Mode string L."
  287. (org-test-table-target-expect
  288. references/target-normal
  289. ;; All the #ERROR show that for Lisp calculations N has to be used.
  290. "
  291. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  292. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  293. | | 1 | | 1 | 1 | 1 | 1 | 1 |
  294. | | | | 0 | 0 | 0 | 0 | 0 |
  295. "
  296. 1 (concat
  297. "#+TBLFM: $3 = '(identity \"$1\"); L :: $4 = '(+ $1 $2); L :: "
  298. "$5 = '(+ $1..$2); L :: $6 = '(+ @0$1..@0$2); L :: "
  299. "$7 = '(length '($1..$2)); L :: $8 = '(length '(@0$1..@0$2)); L")))
  300. (ert-deftest test-org-table/references/mode-string-none ()
  301. "Basic: Assign field reference, sum of field references, sum
  302. and len of simple range reference (no row) and complex range
  303. reference (with row). No mode string."
  304. (let ((lisp (concat
  305. "#+TBLFM: $3 = '(identity $1) :: $4 = '(+ $1 $2) :: "
  306. "$5 = '(+ $1..$2) :: $6 = '(+ @0$1..@0$2) :: "
  307. "$7 = '(length '($1..$2)) :: $8 = '(length '(@0$1..@0$2))"))
  308. (calc (concat
  309. "#+TBLFM: $3 = $1 :: $4 = $1 + $2 :: "
  310. "$5 = vsum($1..$2) :: $6 = vsum(@0$1..@0$2) :: "
  311. "$7 = vlen($1..$2) :: $8 = vlen(@0$1..@0$2)")))
  312. (org-test-table-target-expect
  313. references/target-normal
  314. ;; All the #ERROR show that for Lisp calculations N has to be used.
  315. "
  316. | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
  317. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  318. | | 1 | | #ERROR | #ERROR | #ERROR | 1 | 1 |
  319. | | | | #ERROR | 0 | 0 | 0 | 0 |
  320. "
  321. 1 lisp)
  322. (org-test-table-target-expect
  323. references/target-normal
  324. "
  325. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  326. | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
  327. | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
  328. | | | 0 | 0 | 0 | 0 | 0 | 0 |
  329. "
  330. 1 calc)
  331. (org-test-table-target-expect
  332. references/target-special
  333. "
  334. | nan | 1 | nan | nan | nan | nan | 2 | 2 |
  335. | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
  336. | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
  337. | inf | 1 | inf | inf | inf | inf | 2 | 2 |
  338. "
  339. 1 calc)))
  340. (ert-deftest test-org-table/references/mode-string-N ()
  341. "Basic: Assign field reference, sum of field references, sum
  342. and len of simple range reference (no row) and complex range
  343. reference (with row). Mode string N."
  344. (let ((lisp
  345. (concat
  346. "#+TBLFM: $3 = '(identity $1); N :: $4 = '(+ $1 $2); N :: "
  347. "$5 = '(+ $1..$2); N :: $6 = '(+ @0$1..@0$2); N :: "
  348. "$7 = '(length '($1..$2)); N :: $8 = '(length '(@0$1..@0$2)); N"))
  349. (calc
  350. (concat
  351. "#+TBLFM: $3 = $1; N :: $4 = $1 + $2; N :: "
  352. "$5 = vsum($1..$2); N :: $6 = vsum(@0$1..@0$2); N :: "
  353. "$7 = vlen($1..$2); N :: $8 = vlen(@0$1..@0$2); N")))
  354. (org-test-table-target-expect
  355. references/target-normal
  356. "
  357. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  358. | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  359. | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
  360. | | | 0 | 0 | 0 | 0 | 0 | 0 |
  361. "
  362. 1 lisp calc)
  363. (org-test-table-target-expect
  364. references/target-special
  365. "
  366. | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  367. | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  368. | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  369. | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  370. "
  371. 1 calc)))
  372. (ert-deftest test-org-table/compare ()
  373. "Basic: Compare field references in Calc."
  374. (org-test-table-target-expect
  375. "
  376. | | 0 | z | | nan | uinf | -inf | inf |
  377. |------+------+------+------+------+------+------+------|
  378. | 0 | repl | repl | repl | repl | repl | repl | repl |
  379. | z | repl | repl | repl | repl | repl | repl | repl |
  380. | | repl | repl | repl | repl | repl | repl | repl |
  381. | nan | repl | repl | repl | repl | repl | repl | repl |
  382. | uinf | repl | repl | repl | repl | repl | repl | repl |
  383. | -inf | repl | repl | repl | repl | repl | repl | repl |
  384. | inf | repl | repl | repl | repl | repl | repl | repl |
  385. "
  386. "
  387. | | 0 | z | | nan | uinf | -inf | inf |
  388. |------+---+---+---+-----+------+------+-----|
  389. | 0 | x | | | | | | |
  390. | z | | x | | | | | |
  391. | | | | x | | | | |
  392. | nan | | | | x | | | |
  393. | uinf | | | | | x | | |
  394. | -inf | | | | | | x | |
  395. | inf | | | | | | | x |
  396. "
  397. 1
  398. ;; Compare field reference ($1) with field reference (@1)
  399. "#+TBLFM: @I$<<..@>$> = if(\"$1\" == \"@1\", x, string(\"\")); E"
  400. ;; Compare field reference ($1) with absolute term
  401. (concat "#+TBLFM: "
  402. "$2 = if(\"$1\" == \"(0)\" , x, string(\"\")); E :: "
  403. "$3 = if(\"$1\" == \"(z)\" , x, string(\"\")); E :: "
  404. "$4 = if(\"$1\" == \"nan\" , x, string(\"\")); E :: "
  405. "$5 = if(\"$1\" == \"(nan)\" , x, string(\"\")); E :: "
  406. "$6 = if(\"$1\" == \"(uinf)\", x, string(\"\")); E :: "
  407. "$7 = if(\"$1\" == \"(-inf)\", x, string(\"\")); E :: "
  408. "$8 = if(\"$1\" == \"(inf)\" , x, string(\"\")); E"))
  409. ;; Check field reference converted from an empty field: Despite this
  410. ;; field reference will not end up in a result, Calc evaluates it.
  411. ;; Make sure that also then there is no Calc error.
  412. (org-test-table-target-expect
  413. "
  414. | 0 | replace |
  415. | z | replace |
  416. | | replace |
  417. | nan | replace |
  418. "
  419. "
  420. | 0 | 1 |
  421. | z | z + 1 |
  422. | | |
  423. | nan | nan |
  424. "
  425. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"))
  426. (ert-deftest test-org-table/empty-field ()
  427. "Examples how to deal with empty fields."
  428. ;; Test if one field is empty, else do a calculation
  429. (org-test-table-target-expect
  430. "
  431. | -1 | replace |
  432. | 0 | replace |
  433. | | replace |
  434. "
  435. "
  436. | -1 | 0 |
  437. | 0 | 1 |
  438. | | |
  439. "
  440. 1
  441. ;; Calc formula
  442. "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"
  443. ;; Lisp formula
  444. "#+TBLFM: $2 = '(if (eq \"$1\" \"\") \"\" (1+ $1)); L")
  445. ;; Test if several fields are empty, else do a calculation
  446. (org-test-table-target-expect
  447. "
  448. | 1 | 2 | replace |
  449. | 4 | | replace |
  450. | | 8 | replace |
  451. | | | replace |
  452. "
  453. "
  454. | 1 | 2 | 3 |
  455. | 4 | | |
  456. | | 8 | |
  457. | | | |
  458. "
  459. 1
  460. ;; Calc formula
  461. (concat "#+TBLFM: $3 = if(\"$1\" == \"nan\" || \"$2\" == \"nan\", "
  462. "string(\"\"), $1 + $2); E")
  463. ;; Lisp formula
  464. (concat "#+TBLFM: $3 = '(if (or (eq \"$1\" \"\") (eq \"$2\" \"\")) "
  465. "\"\" (+ $1 $2)); L"))
  466. ;; $2: Use $1 + 0.5 if $1 available, else only reformat $2 if $2 available
  467. (org-test-table-target-expect
  468. "
  469. | 1.5 | 0 |
  470. | 3.5 | |
  471. | | 5 |
  472. | | |
  473. "
  474. "
  475. | 1.5 | 2.0 |
  476. | 3.5 | 4.0 |
  477. | | 5.0 |
  478. | | |
  479. "
  480. 1
  481. ;; Calc formula
  482. (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  483. "if(\"$2\" == \"nan\", string(\"\"), $2 +.0), $1 + 0.5); E f-1")
  484. ;; Lisp formula not implemented yet
  485. )
  486. ;; Empty fields in simple and complex range reference
  487. (org-test-table-target-expect
  488. "
  489. | | | | | repl | repl | repl | repl | repl | repl |
  490. | | | 5 | 7 | repl | repl | repl | repl | repl | repl |
  491. | 1 | 3 | 5 | 7 | repl | repl | repl | repl | repl | repl |
  492. "
  493. "
  494. | | | | | | | | | 0 | 0 |
  495. | | | 5 | 7 | | | 6 | 6 | 3 | 3 |
  496. | 1 | 3 | 5 | 7 | 4 | 4 | 4 | 4 | 4 | 4 |
  497. "
  498. 1
  499. ;; Calc formula
  500. (concat
  501. "#+TBLFM: "
  502. "$5 = if(typeof(vmean($1..$4)) == 12, "
  503. "string(\"\"), vmean($1..$4)); E :: "
  504. "$6 = if(typeof(vmean(@0$1..@0$4)) == 12, "
  505. "string(\"\"), vmean(@0$1..@0$4)); E :: "
  506. "$7 = if(\"$1..$4\" == \"[]\", string(\"\"), vmean($1..$4)) :: "
  507. "$8 = if(\"@0$1..@0$4\" == \"[]\", string(\"\"), vmean(@0$1..@0$4)) :: "
  508. "$9 = vmean($1..$4); EN :: "
  509. "$10 = vmean(@0$1..@0$4); EN")
  510. ;; Lisp formula
  511. (concat
  512. "#+TBLFM: "
  513. "$5 = '(let ((l '($1..$4))) (if (member \"\" l) \"\" "
  514. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  515. "$6 = '(let ((l '(@0$1..@0$4))) (if (member \"\" l) \"\" "
  516. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  517. "$7 = '(let ((l '($1..$4))) "
  518. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  519. "$8 = '(let ((l '(@0$1..@0$4))) "
  520. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  521. "$9 = '(/ (+ $1..$4) (length '($1..$4))); EN :: "
  522. "$10 = '(/ (+ @0$1..@0$4) (length '(@0$1..@0$4))); EN")
  523. ))
  524. (ert-deftest test-org-table/copy-field ()
  525. "Experiments on how to copy one field into another field."
  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. (org-test-table-target-expect
  730. "
  731. #+NAME: table
  732. | | 42 |
  733. | replace | |
  734. "
  735. "
  736. #+NAME: table
  737. | | 42 |
  738. | 42 | |
  739. "
  740. 1 "#+TBLFM: $1 = remote(table, @1$2)"))
  741. (ert-deftest test-org-table/org-at-TBLFM-p ()
  742. (org-test-with-temp-text-in-file
  743. "
  744. | 1 |
  745. | 2 |
  746. #+TBLFM: $2=$1*2
  747. "
  748. (goto-char (point-min))
  749. (forward-line 2)
  750. (should (equal (org-at-TBLFM-p) nil))
  751. (goto-char (point-min))
  752. (forward-line 3)
  753. (should (equal (org-at-TBLFM-p) t))
  754. (goto-char (point-min))
  755. (forward-line 4)
  756. (should (equal (org-at-TBLFM-p) nil))))
  757. (ert-deftest test-org-table/org-table-TBLFM-begin ()
  758. (org-test-with-temp-text-in-file
  759. "
  760. | 1 |
  761. | 2 |
  762. #+TBLFM: $2=$1*2
  763. "
  764. (goto-char (point-min))
  765. (should (equal (org-table-TBLFM-begin)
  766. nil))
  767. (goto-char (point-min))
  768. (forward-line 1)
  769. (should (equal (org-table-TBLFM-begin)
  770. nil))
  771. (goto-char (point-min))
  772. (forward-line 3)
  773. (should (= (org-table-TBLFM-begin)
  774. 14))
  775. (goto-char (point-min))
  776. (forward-line 4)
  777. (should (= (org-table-TBLFM-begin)
  778. 14))
  779. ))
  780. (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
  781. "For multiple #+TBLFM lines."
  782. (org-test-with-temp-text-in-file
  783. "
  784. | 1 |
  785. | 2 |
  786. #+TBLFM: $2=$1*1
  787. #+TBLFM: $2=$1*2
  788. "
  789. (goto-char (point-min))
  790. (should (equal (org-table-TBLFM-begin)
  791. nil))
  792. (goto-char (point-min))
  793. (forward-line 1)
  794. (should (equal (org-table-TBLFM-begin)
  795. nil))
  796. (goto-char (point-min))
  797. (forward-line 3)
  798. (should (= (org-table-TBLFM-begin)
  799. 14))
  800. (goto-char (point-min))
  801. (forward-line 4)
  802. (should (= (org-table-TBLFM-begin)
  803. 14))
  804. (goto-char (point-min))
  805. (forward-line 5)
  806. (should (= (org-table-TBLFM-begin)
  807. 14))
  808. ))
  809. (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
  810. (org-test-with-temp-text-in-file
  811. "
  812. | 1 |
  813. | 2 |
  814. #+TBLFM: $2=$1*1
  815. #+TBLFM: $2=$1*2
  816. | 6 |
  817. | 7 |
  818. #+TBLFM: $2=$1*1
  819. #+TBLFM: $2=$1*2
  820. "
  821. (goto-char (point-min))
  822. (should (equal (org-table-TBLFM-begin)
  823. nil))
  824. (goto-char (point-min))
  825. (forward-line 1)
  826. (should (equal (org-table-TBLFM-begin)
  827. nil))
  828. (goto-char (point-min))
  829. (forward-line 3)
  830. (should (= (org-table-TBLFM-begin)
  831. 14))
  832. (goto-char (point-min))
  833. (forward-line 4)
  834. (should (= (org-table-TBLFM-begin)
  835. 14))
  836. (goto-char (point-min))
  837. (forward-line 5)
  838. (should (= (org-table-TBLFM-begin)
  839. 14))
  840. (goto-char (point-min))
  841. (forward-line 6)
  842. (should (= (org-table-TBLFM-begin)
  843. 14))
  844. (goto-char (point-min))
  845. (forward-line 8)
  846. (should (= (org-table-TBLFM-begin)
  847. 61))
  848. (goto-char (point-min))
  849. (forward-line 9)
  850. (should (= (org-table-TBLFM-begin)
  851. 61))
  852. (goto-char (point-min))
  853. (forward-line 10)
  854. (should (= (org-table-TBLFM-begin)
  855. 61))))
  856. (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
  857. (org-test-with-temp-text-in-file
  858. "
  859. | 1 | |
  860. | 2 | |
  861. #+TBLFM: $2=$1*1
  862. #+TBLFM: $2=$1*2
  863. #+TBLFM: $2=$1*3
  864. "
  865. (let ((got (progn (goto-char (point-min))
  866. (forward-line 3)
  867. (org-table-calc-current-TBLFM)
  868. (buffer-string)))
  869. (expect "
  870. | 1 | 1 |
  871. | 2 | 2 |
  872. #+TBLFM: $2=$1*1
  873. #+TBLFM: $2=$1*2
  874. #+TBLFM: $2=$1*3
  875. "))
  876. (should (string= got
  877. expect)))
  878. (let ((got (progn (goto-char (point-min))
  879. (forward-line 4)
  880. (org-table-calc-current-TBLFM)
  881. (buffer-string)))
  882. (expect "
  883. | 1 | 2 |
  884. | 2 | 4 |
  885. #+TBLFM: $2=$1*1
  886. #+TBLFM: $2=$1*2
  887. #+TBLFM: $2=$1*3
  888. "))
  889. (should (string= got
  890. expect)))))
  891. (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
  892. "org-table-calc-current-TBLFM should preserve the input as it was."
  893. (org-test-with-temp-text-in-file
  894. "
  895. | 1 | 1 |
  896. | 2 | 2 |
  897. #+TBLFM: $2=$1*1
  898. #+TBLFM: $2=$1*2::$2=$1*2
  899. #+TBLFM: $2=$1*3
  900. "
  901. (let ((expect "
  902. | 1 | 1 |
  903. | 2 | 2 |
  904. #+TBLFM: $2=$1*1
  905. #+TBLFM: $2=$1*2::$2=$1*2
  906. #+TBLFM: $2=$1*3
  907. "))
  908. (goto-char (point-min))
  909. (forward-line 4)
  910. (should-error (org-table-calc-current-TBLFM))
  911. (setq got (buffer-string))
  912. (message "%s" got)
  913. (should (string= got
  914. expect)))))
  915. (provide 'test-org-table)
  916. ;;; test-org-table.el ends here