test-org-table.el 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. (ert-deftest test-org-table/sub-total ()
  573. "Grouped rows with sub-total.
  574. Begin range with \"@II\" to handle multiline header. Convert
  575. integer to float with \"+.0\" for sub-total of items c1 and c2.
  576. Sum empty fields as value zero but without ignoring them for
  577. \"vlen\" with format specifier \"EN\". Format possibly empty
  578. results with the Calc formatter \"f-1\" instead of the printf
  579. formatter \"%.1f\"."
  580. (org-test-table-target-expect
  581. "
  582. |-------+---------+---------|
  583. | Item | Item | Sub- |
  584. | name | value | total |
  585. |-------+---------+---------|
  586. | a1 | 4.1 | replace |
  587. | a2 | 8.2 | replace |
  588. | a3 | | replace |
  589. |-------+---------+---------|
  590. | b1 | 16.0 | replace |
  591. |-------+---------+---------|
  592. | c1 | 32 | replace |
  593. | c2 | 64 | replace |
  594. |-------+---------+---------|
  595. | Total | replace | replace |
  596. |-------+---------+---------|
  597. "
  598. "
  599. |-------+-------+-------|
  600. | Item | Item | Sub- |
  601. | name | value | total |
  602. |-------+-------+-------|
  603. | a1 | 4.1 | |
  604. | a2 | 8.2 | |
  605. | a3 | | 12.3 |
  606. |-------+-------+-------|
  607. | b1 | 16.0 | 16.0 |
  608. |-------+-------+-------|
  609. | c1 | 32 | |
  610. | c2 | 64 | 96.0 |
  611. |-------+-------+-------|
  612. | Total | 124.3 | |
  613. |-------+-------+-------|
  614. "
  615. 1 (concat "#+TBLFM: @>$2 = vsum(@II..@>>) ::"
  616. "$3 = if(vlen(@-I$2..@0$2) == vlen(@-I$2..@+I$2), "
  617. "vsum(@-I$2..@+I$2) +.0, string(\"\")); EN f-1 :: "
  618. "@>$3 = string(\"\")")))
  619. (ert-deftest test-org-table/org-table-make-reference/mode-string-EL ()
  620. (fset 'f 'org-table-make-reference)
  621. ;; For Lisp formula only
  622. (should (equal "0" (f "0" t nil 'literal)))
  623. (should (equal "z" (f "z" t nil 'literal)))
  624. (should (equal "" (f "" t nil 'literal)))
  625. (should (equal "0 1" (f '("0" "1") t nil 'literal)))
  626. (should (equal "z 1" (f '("z" "1") t nil 'literal)))
  627. (should (equal " 1" (f '("" "1") t nil 'literal)))
  628. (should (equal " " (f '("" "" ) t nil 'literal))))
  629. (ert-deftest test-org-table/org-table-make-reference/mode-string-E ()
  630. (fset 'f 'org-table-make-reference)
  631. ;; For Lisp formula
  632. (should (equal "\"0\"" (f "0" t nil t)))
  633. (should (equal "\"z\"" (f "z" t nil t)))
  634. (should (equal "\"\"" (f "" t nil t)))
  635. (should (equal "\"0\" \"1\"" (f '("0" "1") t nil t)))
  636. (should (equal "\"z\" \"1\"" (f '("z" "1") t nil t)))
  637. (should (equal "\"\" \"1\"" (f '("" "1") t nil t)))
  638. (should (equal "\"\" \"\"" (f '("" "" ) t nil t)))
  639. ;; For Calc formula
  640. (should (equal "(0)" (f "0" t nil nil)))
  641. (should (equal "(z)" (f "z" t nil nil)))
  642. (should (equal "nan" (f "" t nil nil)))
  643. (should (equal "[0,1]" (f '("0" "1") t nil nil)))
  644. (should (equal "[z,1]" (f '("z" "1") t nil nil)))
  645. (should (equal "[nan,1]" (f '("" "1") t nil nil)))
  646. (should (equal "[nan,nan]" (f '("" "" ) t nil nil)))
  647. ;; For Calc formula, special numbers
  648. (should (equal "(nan)" (f "nan" t nil nil)))
  649. (should (equal "(uinf)" (f "uinf" t nil nil)))
  650. (should (equal "(-inf)" (f "-inf" t nil nil)))
  651. (should (equal "(inf)" (f "inf" t nil nil)))
  652. (should (equal "[nan,1]" (f '( "nan" "1") t nil nil)))
  653. (should (equal "[uinf,1]" (f '("uinf" "1") t nil nil)))
  654. (should (equal "[-inf,1]" (f '("-inf" "1") t nil nil)))
  655. (should (equal "[inf,1]" (f '( "inf" "1") t nil nil))))
  656. (ert-deftest test-org-table/org-table-make-reference/mode-string-EN ()
  657. (fset 'f 'org-table-make-reference)
  658. ;; For Lisp formula
  659. (should (equal "0" (f "0" t t t)))
  660. (should (equal "0" (f "z" t t t)))
  661. (should (equal "0" (f "" t t t)))
  662. (should (equal "0 1" (f '("0" "1") t t t)))
  663. (should (equal "0 1" (f '("z" "1") t t t)))
  664. (should (equal "0 1" (f '("" "1") t t t)))
  665. (should (equal "0 0" (f '("" "" ) t t t)))
  666. ;; For Calc formula
  667. (should (equal "(0)" (f "0" t t nil)))
  668. (should (equal "(0)" (f "z" t t nil)))
  669. (should (equal "(0)" (f "" t t nil)))
  670. (should (equal "[0,1]" (f '("0" "1") t t nil)))
  671. (should (equal "[0,1]" (f '("z" "1") t t nil)))
  672. (should (equal "[0,1]" (f '("" "1") t t nil)))
  673. (should (equal "[0,0]" (f '("" "" ) t t nil)))
  674. ;; For Calc formula, special numbers
  675. (should (equal "(0)" (f "nan" t t nil)))
  676. (should (equal "(0)" (f "uinf" t t nil)))
  677. (should (equal "(0)" (f "-inf" t t nil)))
  678. (should (equal "(0)" (f "inf" t t nil)))
  679. (should (equal "[0,1]" (f '( "nan" "1") t t nil)))
  680. (should (equal "[0,1]" (f '("uinf" "1") t t nil)))
  681. (should (equal "[0,1]" (f '("-inf" "1") t t nil)))
  682. (should (equal "[0,1]" (f '( "inf" "1") t t nil))))
  683. (ert-deftest test-org-table/org-table-make-reference/mode-string-L ()
  684. (fset 'f 'org-table-make-reference)
  685. ;; For Lisp formula only
  686. (should (equal "0" (f "0" nil nil 'literal)))
  687. (should (equal "z" (f "z" nil nil 'literal)))
  688. (should (equal "" (f "" nil nil 'literal)))
  689. (should (equal "0 1" (f '("0" "1") nil nil 'literal)))
  690. (should (equal "z 1" (f '("z" "1") nil nil 'literal)))
  691. (should (equal "1" (f '("" "1") nil nil 'literal)))
  692. (should (equal "" (f '("" "" ) nil nil 'literal))))
  693. (ert-deftest test-org-table/org-table-make-reference/mode-string-none ()
  694. (fset 'f 'org-table-make-reference)
  695. ;; For Lisp formula
  696. (should (equal "\"0\"" (f "0" nil nil t)))
  697. (should (equal "\"z\"" (f "z" nil nil t)))
  698. (should (equal "" (f "" nil nil t)))
  699. (should (equal "\"0\" \"1\"" (f '("0" "1") nil nil t)))
  700. (should (equal "\"z\" \"1\"" (f '("z" "1") nil nil t)))
  701. (should (equal "\"1\"" (f '("" "1") nil nil t)))
  702. (should (equal "" (f '("" "" ) nil nil t)))
  703. ;; For Calc formula
  704. (should (equal "(0)" (f "0" nil nil nil)))
  705. (should (equal "(z)" (f "z" nil nil nil)))
  706. (should (equal "(0)" (f "" nil nil nil)))
  707. (should (equal "[0,1]" (f '("0" "1") nil nil nil)))
  708. (should (equal "[z,1]" (f '("z" "1") nil nil nil)))
  709. (should (equal "[1]" (f '("" "1") nil nil nil)))
  710. (should (equal "[]" (f '("" "" ) nil nil nil)))
  711. ;; For Calc formula, special numbers
  712. (should (equal "(nan)" (f "nan" nil nil nil)))
  713. (should (equal "(uinf)" (f "uinf" nil nil nil)))
  714. (should (equal "(-inf)" (f "-inf" nil nil nil)))
  715. (should (equal "(inf)" (f "inf" nil nil nil)))
  716. (should (equal "[nan,1]" (f '( "nan" "1") nil nil nil)))
  717. (should (equal "[uinf,1]" (f '("uinf" "1") nil nil nil)))
  718. (should (equal "[-inf,1]" (f '("-inf" "1") nil nil nil)))
  719. (should (equal "[inf,1]" (f '( "inf" "1") nil nil nil))))
  720. (ert-deftest test-org-table/org-table-make-reference/mode-string-N ()
  721. (fset 'f 'org-table-make-reference)
  722. ;; For Lisp formula
  723. (should (equal "0" (f "0" nil t t)))
  724. (should (equal "0" (f "z" nil t t)))
  725. (should (equal "" (f "" nil t t)))
  726. (should (equal "0 1" (f '("0" "1") nil t t)))
  727. (should (equal "0 1" (f '("z" "1") nil t t)))
  728. (should (equal "1" (f '("" "1") nil t t)))
  729. (should (equal "" (f '("" "" ) nil t t)))
  730. ;; For Calc formula
  731. (should (equal "(0)" (f "0" nil t nil)))
  732. (should (equal "(0)" (f "z" nil t nil)))
  733. (should (equal "(0)" (f "" nil t nil)))
  734. (should (equal "[0,1]" (f '("0" "1") nil t nil)))
  735. (should (equal "[0,1]" (f '("z" "1") nil t nil)))
  736. (should (equal "[1]" (f '("" "1") nil t nil)))
  737. (should (equal "[]" (f '("" "" ) nil t nil)))
  738. ;; For Calc formula, special numbers
  739. (should (equal "(0)" (f "nan" nil t nil)))
  740. (should (equal "(0)" (f "uinf" nil t nil)))
  741. (should (equal "(0)" (f "-inf" nil t nil)))
  742. (should (equal "(0)" (f "inf" nil t nil)))
  743. (should (equal "[0,1]" (f '( "nan" "1") nil t nil)))
  744. (should (equal "[0,1]" (f '("uinf" "1") nil t nil)))
  745. (should (equal "[0,1]" (f '("-inf" "1") nil t nil)))
  746. (should (equal "[0,1]" (f '( "inf" "1") nil t nil))))
  747. (ert-deftest test-org-table/org-table-convert-refs-to-an/1 ()
  748. "Simple reference @1$1."
  749. (should
  750. (string= "A1" (org-table-convert-refs-to-an "@1$1"))))
  751. ;; TODO: Test broken
  752. ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
  753. ;; "Self reference @1$1."
  754. ;; (should
  755. ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
  756. (ert-deftest test-org-table/org-table-convert-refs-to-an/3 ()
  757. "Remote reference."
  758. (should
  759. (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
  760. (ert-deftest test-org-table/org-table-convert-refs-to-rc/1 ()
  761. "Simple reference @1$1."
  762. (should
  763. (string= "@1$1" (org-table-convert-refs-to-rc "A1"))))
  764. (ert-deftest test-org-table/org-table-convert-refs-to-rc/2 ()
  765. "Self reference $0."
  766. (should
  767. (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
  768. ;; TODO: Test Broken
  769. ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
  770. ;; "Remote reference."
  771. ;; (should
  772. ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
  773. (ert-deftest test-org-table/remote-reference-access ()
  774. "Access to remote reference.
  775. See also `test-org-table/copy-field'."
  776. (org-test-table-target-expect
  777. "
  778. #+NAME: table
  779. | | x 42 | |
  780. | replace | replace |
  781. "
  782. "
  783. #+NAME: table
  784. | | x 42 | |
  785. | x 42 | 84 x |
  786. "
  787. 1 (concat "#+TBLFM: "
  788. ;; Copy text without calculation: Use Lisp formula
  789. "$1 = '(identity remote(table, @1$2)) :: "
  790. ;; Do a calculation: Use Calc (or Lisp ) formula
  791. "$2 = 2 * remote(table, @1$2)")))
  792. (ert-deftest test-org-table/remote-reference-indirect ()
  793. "Access to remote reference with indirection of name or ID."
  794. (let ((source-tables "
  795. #+NAME: 2012
  796. | amount |
  797. |--------|
  798. | 1 |
  799. | 2 |
  800. |--------|
  801. | 3 |
  802. #+TBLFM: @>$1 = vsum(@I..@II)
  803. #+NAME: 2013
  804. | amount |
  805. |--------|
  806. | 4 |
  807. | 8 |
  808. |--------|
  809. | 12 |
  810. #+TBLFM: @>$1 = vsum(@I..@II)
  811. "))
  812. ;; Read several remote references from same column
  813. (org-test-table-target-expect
  814. (concat source-tables "
  815. #+NAME: summary
  816. | year | amount |
  817. |-------+---------|
  818. | 2012 | replace |
  819. | 2013 | replace |
  820. |-------+---------|
  821. | total | replace |
  822. ")
  823. (concat source-tables "
  824. #+NAME: summary
  825. | year | amount |
  826. |-------+--------|
  827. | 2012 | 3 |
  828. | 2013 | 12 |
  829. |-------+--------|
  830. | total | 15 |
  831. ")
  832. 1
  833. ;; Calc formula
  834. "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
  835. ;; Lisp formula
  836. (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
  837. "@>$2 = '(+ @I..@II); N"))
  838. ;; Read several remote references from same row
  839. (org-test-table-target-expect
  840. (concat source-tables "
  841. #+NAME: summary
  842. | year | 2012 | 2013 | total |
  843. |--------+---------+---------+---------|
  844. | amount | replace | replace | replace |
  845. ")
  846. (concat source-tables "
  847. #+NAME: summary
  848. | year | 2012 | 2013 | total |
  849. |--------+------+------+-------|
  850. | amount | 3 | 12 | 15 |
  851. ")
  852. 1
  853. ;; Calc formula
  854. "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
  855. ;; Lisp formula
  856. (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
  857. "@2$> = '(+ $<<..$>>); N"))))
  858. (ert-deftest test-org-table/org-at-TBLFM-p ()
  859. (org-test-with-temp-text-in-file
  860. "
  861. | 1 |
  862. | 2 |
  863. #+TBLFM: $2=$1*2
  864. "
  865. (goto-char (point-min))
  866. (forward-line 2)
  867. (should (equal (org-at-TBLFM-p) nil))
  868. (goto-char (point-min))
  869. (forward-line 3)
  870. (should (equal (org-at-TBLFM-p) t))
  871. (goto-char (point-min))
  872. (forward-line 4)
  873. (should (equal (org-at-TBLFM-p) nil))))
  874. (ert-deftest test-org-table/org-table-TBLFM-begin ()
  875. (org-test-with-temp-text-in-file
  876. "
  877. | 1 |
  878. | 2 |
  879. #+TBLFM: $2=$1*2
  880. "
  881. (goto-char (point-min))
  882. (should (equal (org-table-TBLFM-begin)
  883. nil))
  884. (goto-char (point-min))
  885. (forward-line 1)
  886. (should (equal (org-table-TBLFM-begin)
  887. nil))
  888. (goto-char (point-min))
  889. (forward-line 3)
  890. (should (= (org-table-TBLFM-begin)
  891. 14))
  892. (goto-char (point-min))
  893. (forward-line 4)
  894. (should (= (org-table-TBLFM-begin)
  895. 14))
  896. ))
  897. (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
  898. "For multiple #+TBLFM lines."
  899. (org-test-with-temp-text-in-file
  900. "
  901. | 1 |
  902. | 2 |
  903. #+TBLFM: $2=$1*1
  904. #+TBLFM: $2=$1*2
  905. "
  906. (goto-char (point-min))
  907. (should (equal (org-table-TBLFM-begin)
  908. nil))
  909. (goto-char (point-min))
  910. (forward-line 1)
  911. (should (equal (org-table-TBLFM-begin)
  912. nil))
  913. (goto-char (point-min))
  914. (forward-line 3)
  915. (should (= (org-table-TBLFM-begin)
  916. 14))
  917. (goto-char (point-min))
  918. (forward-line 4)
  919. (should (= (org-table-TBLFM-begin)
  920. 14))
  921. (goto-char (point-min))
  922. (forward-line 5)
  923. (should (= (org-table-TBLFM-begin)
  924. 14))
  925. ))
  926. (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
  927. (org-test-with-temp-text-in-file
  928. "
  929. | 1 |
  930. | 2 |
  931. #+TBLFM: $2=$1*1
  932. #+TBLFM: $2=$1*2
  933. | 6 |
  934. | 7 |
  935. #+TBLFM: $2=$1*1
  936. #+TBLFM: $2=$1*2
  937. "
  938. (goto-char (point-min))
  939. (should (equal (org-table-TBLFM-begin)
  940. nil))
  941. (goto-char (point-min))
  942. (forward-line 1)
  943. (should (equal (org-table-TBLFM-begin)
  944. nil))
  945. (goto-char (point-min))
  946. (forward-line 3)
  947. (should (= (org-table-TBLFM-begin)
  948. 14))
  949. (goto-char (point-min))
  950. (forward-line 4)
  951. (should (= (org-table-TBLFM-begin)
  952. 14))
  953. (goto-char (point-min))
  954. (forward-line 5)
  955. (should (= (org-table-TBLFM-begin)
  956. 14))
  957. (goto-char (point-min))
  958. (forward-line 6)
  959. (should (= (org-table-TBLFM-begin)
  960. 14))
  961. (goto-char (point-min))
  962. (forward-line 8)
  963. (should (= (org-table-TBLFM-begin)
  964. 61))
  965. (goto-char (point-min))
  966. (forward-line 9)
  967. (should (= (org-table-TBLFM-begin)
  968. 61))
  969. (goto-char (point-min))
  970. (forward-line 10)
  971. (should (= (org-table-TBLFM-begin)
  972. 61))))
  973. (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
  974. (org-test-with-temp-text-in-file
  975. "
  976. | 1 | |
  977. | 2 | |
  978. #+TBLFM: $2=$1*1
  979. #+TBLFM: $2=$1*2
  980. #+TBLFM: $2=$1*3
  981. "
  982. (let ((got (progn (goto-char (point-min))
  983. (forward-line 3)
  984. (org-table-calc-current-TBLFM)
  985. (buffer-string)))
  986. (expect "
  987. | 1 | 1 |
  988. | 2 | 2 |
  989. #+TBLFM: $2=$1*1
  990. #+TBLFM: $2=$1*2
  991. #+TBLFM: $2=$1*3
  992. "))
  993. (should (string= got
  994. expect)))
  995. (let ((got (progn (goto-char (point-min))
  996. (forward-line 4)
  997. (org-table-calc-current-TBLFM)
  998. (buffer-string)))
  999. (expect "
  1000. | 1 | 2 |
  1001. | 2 | 4 |
  1002. #+TBLFM: $2=$1*1
  1003. #+TBLFM: $2=$1*2
  1004. #+TBLFM: $2=$1*3
  1005. "))
  1006. (should (string= got
  1007. expect)))))
  1008. (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
  1009. "org-table-calc-current-TBLFM should preserve the input as it was."
  1010. (org-test-with-temp-text-in-file
  1011. "
  1012. | 1 | 1 |
  1013. | 2 | 2 |
  1014. #+TBLFM: $2=$1*1
  1015. #+TBLFM: $2=$1*2::$2=$1*2
  1016. #+TBLFM: $2=$1*3
  1017. "
  1018. (let ((expect "
  1019. | 1 | 1 |
  1020. | 2 | 2 |
  1021. #+TBLFM: $2=$1*1
  1022. #+TBLFM: $2=$1*2::$2=$1*2
  1023. #+TBLFM: $2=$1*3
  1024. "))
  1025. (goto-char (point-min))
  1026. (forward-line 4)
  1027. (should-error (org-table-calc-current-TBLFM))
  1028. (setq got (buffer-string))
  1029. (message "%s" got)
  1030. (should (string= got
  1031. expect)))))
  1032. (provide 'test-org-table)
  1033. ;;; test-org-table.el ends here