test-org-table.el 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. (provide 'test-org-table)
  1084. ;;; test-org-table.el ends here