test-org-table.el 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311
  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 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 | header |
  97. | 4 | header |
  98. | 8 | header |
  99. |---------+---------|
  100. | replace | replace |
  101. "
  102. "
  103. | 2 | header |
  104. | 4 | header |
  105. | 8 | header |
  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. (ert-deftest test-org-table/align-buffer-tables ()
  155. "Align all tables when updating buffer."
  156. (let ((before "
  157. | a b |
  158. | c d |
  159. ")
  160. (after "
  161. | a b |
  162. | c d |
  163. "))
  164. (should (equal (org-test-with-temp-text before
  165. (org-table-recalculate-buffer-tables)
  166. (buffer-string))
  167. after))
  168. (should (equal (org-test-with-temp-text before
  169. (org-table-iterate-buffer-tables)
  170. (buffer-string))
  171. after))))
  172. (defconst references/target-normal "
  173. | 0 | 1 | replace | replace | replace | replace | replace | replace |
  174. | z | 1 | replace | replace | replace | replace | replace | replace |
  175. | | 1 | replace | replace | replace | replace | replace | replace |
  176. | | | replace | replace | replace | replace | replace | replace |
  177. "
  178. "Normal numbers and non-numbers for Lisp and Calc formula.")
  179. (defconst references/target-special "
  180. | nan | 1 | replace | replace | replace | replace | replace | replace |
  181. | uinf | 1 | replace | replace | replace | replace | replace | replace |
  182. | -inf | 1 | replace | replace | replace | replace | replace | replace |
  183. | inf | 1 | replace | replace | replace | replace | replace | replace |
  184. "
  185. "Special numbers for Calc formula.")
  186. (ert-deftest test-org-table/references/mode-string-EL ()
  187. "Basic: Assign field reference, sum of field references, sum
  188. and len of simple range reference (no row) and complex range
  189. reference (with row). Mode string EL."
  190. ;; Empty fields are kept during parsing field but lost as list
  191. ;; elements within Lisp formula syntactically when used literally
  192. ;; and not enclosed with " within fields, see last columns with len.
  193. (org-test-table-target-expect
  194. references/target-normal
  195. ;; All the #ERROR show that for Lisp calculations N has to be used.
  196. "
  197. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  198. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  199. | | 1 | | 1 | 1 | 1 | 1 | 1 |
  200. | | | | 0 | 0 | 0 | 0 | 0 |
  201. "
  202. 1 (concat
  203. "#+TBLFM: $3 = '(identity \"$1\"); EL :: $4 = '(+ $1 $2); EL :: "
  204. "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
  205. "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL"))
  206. ;; Empty fields are kept during parsing field _and_ as list elements
  207. ;; within Lisp formula syntactically even when used literally when
  208. ;; enclosed with " within fields, see last columns with len.
  209. (org-test-table-target-expect
  210. "
  211. | \"0\" | \"1\" | repl | repl | repl | repl | repl | repl |
  212. | \"z\" | \"1\" | repl | repl | repl | repl | repl | repl |
  213. | \"\" | \"1\" | repl | repl | repl | repl | repl | repl |
  214. | \"\" | \"\" | repl | repl | repl | repl | repl | repl |
  215. "
  216. "
  217. | \"0\" | \"1\" | \"0\" | 1 | #ERROR | #ERROR | 2 | 2 |
  218. | \"z\" | \"1\" | \"z\" | 1 | #ERROR | #ERROR | 2 | 2 |
  219. | \"\" | \"1\" | \"\" | 1 | #ERROR | #ERROR | 2 | 2 |
  220. | \"\" | \"\" | \"\" | 0 | #ERROR | #ERROR | 2 | 2 |
  221. "
  222. 1 (concat
  223. "#+TBLFM: $3 = '(concat \"\\\"\" $1 \"\\\"\"); EL :: "
  224. "$4 = '(+ (string-to-number $1) (string-to-number $2)); EL :: "
  225. "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
  226. "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL")))
  227. (ert-deftest test-org-table/references/mode-string-E ()
  228. "Basic: Assign field reference, sum of field references, sum
  229. and len of simple range reference (no row) and complex range
  230. reference (with row). Mode string E."
  231. (let ((lisp
  232. (concat
  233. "#+TBLFM: $3 = '(identity $1); E :: $4 = '(+ $1 $2); E :: "
  234. "$5 = '(+ $1..$2); E :: $6 = '(+ @0$1..@0$2); E :: "
  235. "$7 = '(length '($1..$2)); E :: $8 = '(length '(@0$1..@0$2)); E"))
  236. (calc
  237. (concat
  238. "#+TBLFM: $3 = $1; E :: $4 = $1 + $2; E :: "
  239. "$5 = vsum($1..$2); E :: $6 = vsum(@0$1..@0$2); E :: "
  240. "$7 = vlen($1..$2); E :: $8 = vlen(@0$1..@0$2); E")))
  241. (org-test-table-target-expect
  242. references/target-normal
  243. ;; All the #ERROR show that for Lisp calculations N has to be used.
  244. "
  245. | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
  246. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  247. | | 1 | | #ERROR | #ERROR | #ERROR | 2 | 2 |
  248. | | | | #ERROR | #ERROR | #ERROR | 2 | 2 |
  249. "
  250. 1 lisp)
  251. (org-test-table-target-expect
  252. references/target-normal
  253. "
  254. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  255. | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
  256. | | 1 | nan | nan | nan | nan | 2 | 2 |
  257. | | | nan | nan | nan | nan | 2 | 2 |
  258. "
  259. 1 calc)
  260. (org-test-table-target-expect
  261. references/target-special
  262. "
  263. | nan | 1 | nan | nan | nan | nan | 2 | 2 |
  264. | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
  265. | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
  266. | inf | 1 | inf | inf | inf | inf | 2 | 2 |
  267. "
  268. 1 calc)))
  269. (ert-deftest test-org-table/references/mode-string-EN ()
  270. "Basic: Assign field reference, sum of field references, sum
  271. and len of simple range reference (no row) and complex range
  272. reference (with row). Mode string EN."
  273. (let ((lisp (concat
  274. "#+TBLFM: $3 = '(identity $1); EN :: $4 = '(+ $1 $2); EN :: "
  275. "$5 = '(+ $1..$2); EN :: $6 = '(+ @0$1..@0$2); EN :: "
  276. "$7 = '(length '($1..$2)); EN :: "
  277. "$8 = '(length '(@0$1..@0$2)); EN"))
  278. (calc (concat
  279. "#+TBLFM: $3 = $1; EN :: $4 = $1 + $2; EN :: "
  280. "$5 = vsum($1..$2); EN :: $6 = vsum(@0$1..@0$2); EN :: "
  281. "$7 = vlen($1..$2); EN :: $8 = vlen(@0$1..@0$2); EN")))
  282. (org-test-table-target-expect
  283. references/target-normal
  284. "
  285. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  286. | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  287. | | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  288. | | | 0 | 0 | 0 | 0 | 2 | 2 |
  289. "
  290. 1 lisp calc)
  291. (org-test-table-target-expect
  292. references/target-special
  293. "
  294. | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  295. | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  296. | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  297. | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  298. "
  299. 1 calc)))
  300. (ert-deftest test-org-table/references/mode-string-L ()
  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). Mode string L."
  304. (org-test-table-target-expect
  305. references/target-normal
  306. ;; All the #ERROR show that for Lisp calculations N has to be used.
  307. "
  308. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  309. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  310. | | 1 | | 1 | 1 | 1 | 1 | 1 |
  311. | | | | 0 | 0 | 0 | 0 | 0 |
  312. "
  313. 1 (concat
  314. "#+TBLFM: $3 = '(identity \"$1\"); L :: $4 = '(+ $1 $2); L :: "
  315. "$5 = '(+ $1..$2); L :: $6 = '(+ @0$1..@0$2); L :: "
  316. "$7 = '(length '($1..$2)); L :: $8 = '(length '(@0$1..@0$2)); L")))
  317. (ert-deftest test-org-table/references/mode-string-none ()
  318. "Basic: Assign field reference, sum of field references, sum
  319. and len of simple range reference (no row) and complex range
  320. reference (with row). No mode string."
  321. (let ((lisp (concat
  322. "#+TBLFM: $3 = '(identity $1) :: $4 = '(+ $1 $2) :: "
  323. "$5 = '(+ $1..$2) :: $6 = '(+ @0$1..@0$2) :: "
  324. "$7 = '(length '($1..$2)) :: $8 = '(length '(@0$1..@0$2))"))
  325. (calc (concat
  326. "#+TBLFM: $3 = $1 :: $4 = $1 + $2 :: "
  327. "$5 = vsum($1..$2) :: $6 = vsum(@0$1..@0$2) :: "
  328. "$7 = vlen($1..$2) :: $8 = vlen(@0$1..@0$2)")))
  329. (org-test-table-target-expect
  330. references/target-normal
  331. ;; All the #ERROR show that for Lisp calculations N has to be used.
  332. "
  333. | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
  334. | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
  335. | | 1 | | #ERROR | #ERROR | #ERROR | 1 | 1 |
  336. | | | | #ERROR | 0 | 0 | 0 | 0 |
  337. "
  338. 1 lisp)
  339. (org-test-table-target-expect
  340. references/target-normal
  341. "
  342. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  343. | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
  344. | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
  345. | | | 0 | 0 | 0 | 0 | 0 | 0 |
  346. "
  347. 1 calc)
  348. (org-test-table-target-expect
  349. references/target-special
  350. "
  351. | nan | 1 | nan | nan | nan | nan | 2 | 2 |
  352. | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
  353. | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
  354. | inf | 1 | inf | inf | inf | inf | 2 | 2 |
  355. "
  356. 1 calc)))
  357. (ert-deftest test-org-table/references/mode-string-N ()
  358. "Basic: Assign field reference, sum of field references, sum
  359. and len of simple range reference (no row) and complex range
  360. reference (with row). Mode string N."
  361. (let ((lisp
  362. (concat
  363. "#+TBLFM: $3 = '(identity $1); N :: $4 = '(+ $1 $2); N :: "
  364. "$5 = '(+ $1..$2); N :: $6 = '(+ @0$1..@0$2); N :: "
  365. "$7 = '(length '($1..$2)); N :: $8 = '(length '(@0$1..@0$2)); N"))
  366. (calc
  367. (concat
  368. "#+TBLFM: $3 = $1; N :: $4 = $1 + $2; N :: "
  369. "$5 = vsum($1..$2); N :: $6 = vsum(@0$1..@0$2); N :: "
  370. "$7 = vlen($1..$2); N :: $8 = vlen(@0$1..@0$2); N")))
  371. (org-test-table-target-expect
  372. references/target-normal
  373. "
  374. | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  375. | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  376. | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
  377. | | | 0 | 0 | 0 | 0 | 0 | 0 |
  378. "
  379. 1 lisp calc)
  380. (org-test-table-target-expect
  381. references/target-special
  382. "
  383. | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  384. | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  385. | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  386. | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
  387. "
  388. 1 calc)))
  389. (ert-deftest test-org-table/lisp-return-value ()
  390. "Basic: Return value of Lisp formulas."
  391. (org-test-table-target-expect
  392. "
  393. | | nil | (list) | '() |
  394. |-------------------------+-------------+--------+-----|
  395. | type-of, no L | replace (r) | r | r |
  396. | type-of identity, no L | r | r | r |
  397. | identity, no L | r | r | r |
  398. |-------------------------+-------------+--------+-----|
  399. | type-of \"@1\" | r | r | r |
  400. | type-of (identity \"@1\") | r | r | r |
  401. | identity \"@1\" | r | r | r |
  402. |-------------------------+-------------+--------+-----|
  403. | type-of @1 | r | r | r |
  404. | type-of (identity @1) | r | r | r |
  405. | identity @1 | r | r | r |
  406. "
  407. "
  408. | | nil | (list) | '() |
  409. |-------------------------+--------+--------+--------|
  410. | type-of, no L | string | string | string |
  411. | type-of identity, no L | string | string | string |
  412. | identity, no L | nil | (list) | '() |
  413. |-------------------------+--------+--------+--------|
  414. | type-of \"@1\" | string | string | string |
  415. | type-of (identity \"@1\") | string | string | string |
  416. | identity \"@1\" | nil | (list) | '() |
  417. |-------------------------+--------+--------+--------|
  418. | type-of @1 | symbol | symbol | symbol |
  419. | type-of (identity @1) | symbol | symbol | symbol |
  420. | identity @1 | nil | nil | nil |
  421. "
  422. 1 (concat "#+TBLFM: @2$<<..@2$> = '(type-of @1) :: "
  423. "@3$<<..@3$> = '(type-of (identity @1)) :: "
  424. "@4$<<..@4$> = '(identity @1) :: @5$<<..@>$> = '(@0$1); L")))
  425. (ert-deftest test-org-table/compare ()
  426. "Basic: Compare field references in Calc."
  427. (org-test-table-target-expect
  428. "
  429. | | 0 | z | | nan | uinf | -inf | inf |
  430. |------+------+------+------+------+------+------+------|
  431. | 0 | repl | repl | repl | repl | repl | repl | repl |
  432. | z | repl | repl | repl | repl | repl | repl | repl |
  433. | | repl | repl | repl | repl | repl | repl | repl |
  434. | nan | repl | repl | repl | repl | repl | repl | repl |
  435. | uinf | repl | repl | repl | repl | repl | repl | repl |
  436. | -inf | repl | repl | repl | repl | repl | repl | repl |
  437. | inf | repl | repl | repl | repl | repl | repl | repl |
  438. "
  439. "
  440. | | 0 | z | | nan | uinf | -inf | inf |
  441. |------+---+---+---+-----+------+------+-----|
  442. | 0 | x | | | | | | |
  443. | z | | x | | | | | |
  444. | | | | x | | | | |
  445. | nan | | | | x | | | |
  446. | uinf | | | | | x | | |
  447. | -inf | | | | | | x | |
  448. | inf | | | | | | | x |
  449. "
  450. 1
  451. ;; Compare field reference ($1) with field reference (@1)
  452. "#+TBLFM: @<<$<<..@>$> = if(\"$1\" == \"@1\", x, string(\"\")); E"
  453. ;; Compare field reference ($1) with absolute term
  454. (concat "#+TBLFM: "
  455. "$2 = if(\"$1\" == \"(0)\" , x, string(\"\")); E :: "
  456. "$3 = if(\"$1\" == \"(z)\" , x, string(\"\")); E :: "
  457. "$4 = if(\"$1\" == \"nan\" , x, string(\"\")); E :: "
  458. "$5 = if(\"$1\" == \"(nan)\" , x, string(\"\")); E :: "
  459. "$6 = if(\"$1\" == \"(uinf)\", x, string(\"\")); E :: "
  460. "$7 = if(\"$1\" == \"(-inf)\", x, string(\"\")); E :: "
  461. "$8 = if(\"$1\" == \"(inf)\" , x, string(\"\")); E"))
  462. ;; Check field reference converted from an empty field: Despite this
  463. ;; field reference will not end up in a result, Calc evaluates it.
  464. ;; Make sure that also then there is no Calc error.
  465. (org-test-table-target-expect
  466. "
  467. | 0 | replace |
  468. | z | replace |
  469. | | replace |
  470. | nan | replace |
  471. "
  472. "
  473. | 0 | 1 |
  474. | z | z + 1 |
  475. | | |
  476. | nan | nan |
  477. "
  478. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"))
  479. (ert-deftest test-org-table/empty-field ()
  480. "Examples how to deal with empty fields."
  481. ;; Test if one field is empty, else do a calculation
  482. (org-test-table-target-expect
  483. "
  484. | -1 | replace |
  485. | 0 | replace |
  486. | | replace |
  487. "
  488. "
  489. | -1 | 0 |
  490. | 0 | 1 |
  491. | | |
  492. "
  493. 1
  494. ;; Calc formula
  495. "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"
  496. ;; Lisp formula
  497. "#+TBLFM: $2 = '(if (eq \"$1\" \"\") \"\" (1+ $1)); L")
  498. ;; Test if several fields are empty, else do a calculation
  499. (org-test-table-target-expect
  500. "
  501. | 1 | 2 | replace |
  502. | 4 | | replace |
  503. | | 8 | replace |
  504. | | | replace |
  505. "
  506. "
  507. | 1 | 2 | 3 |
  508. | 4 | | |
  509. | | 8 | |
  510. | | | |
  511. "
  512. 1
  513. ;; Calc formula
  514. (concat "#+TBLFM: $3 = if(\"$1\" == \"nan\" || \"$2\" == \"nan\", "
  515. "string(\"\"), $1 + $2); E")
  516. ;; Lisp formula
  517. (concat "#+TBLFM: $3 = '(if (or (eq \"$1\" \"\") (eq \"$2\" \"\")) "
  518. "\"\" (+ $1 $2)); L"))
  519. ;; $2: Use $1 + 0.5 if $1 available, else only reformat $2 if $2 available
  520. (org-test-table-target-expect
  521. "
  522. | 1.5 | 0 |
  523. | 3.5 | |
  524. | | 5 |
  525. | | |
  526. "
  527. "
  528. | 1.5 | 2.0 |
  529. | 3.5 | 4.0 |
  530. | | 5.0 |
  531. | | |
  532. "
  533. 1
  534. ;; Calc formula
  535. (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  536. "if(\"$2\" == \"nan\", string(\"\"), $2 +.0), $1 + 0.5); E f-1")
  537. ;; Lisp formula not implemented yet
  538. )
  539. ;; Empty fields in simple and complex range reference
  540. (org-test-table-target-expect
  541. "
  542. | | | | | repl | repl | repl | repl | repl | repl |
  543. | | | 5 | 7 | repl | repl | repl | repl | repl | repl |
  544. | 1 | 3 | 5 | 7 | repl | repl | repl | repl | repl | repl |
  545. "
  546. "
  547. | | | | | | | | | 0 | 0 |
  548. | | | 5 | 7 | | | 6 | 6 | 3 | 3 |
  549. | 1 | 3 | 5 | 7 | 4 | 4 | 4 | 4 | 4 | 4 |
  550. "
  551. 1
  552. ;; Calc formula
  553. (concat
  554. "#+TBLFM: "
  555. "$5 = if(typeof(vmean($1..$4)) == 12, "
  556. "string(\"\"), vmean($1..$4)); E :: "
  557. "$6 = if(typeof(vmean(@0$1..@0$4)) == 12, "
  558. "string(\"\"), vmean(@0$1..@0$4)); E :: "
  559. "$7 = if(\"$1..$4\" == \"[]\", string(\"\"), vmean($1..$4)) :: "
  560. "$8 = if(\"@0$1..@0$4\" == \"[]\", string(\"\"), vmean(@0$1..@0$4)) :: "
  561. "$9 = vmean($1..$4); EN :: "
  562. "$10 = vmean(@0$1..@0$4); EN")
  563. ;; Lisp formula
  564. (concat
  565. "#+TBLFM: "
  566. "$5 = '(let ((l '($1..$4))) (if (member \"\" l) \"\" "
  567. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  568. "$6 = '(let ((l '(@0$1..@0$4))) (if (member \"\" l) \"\" "
  569. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  570. "$7 = '(let ((l '($1..$4))) "
  571. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  572. "$8 = '(let ((l '(@0$1..@0$4))) "
  573. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  574. "$9 = '(/ (+ $1..$4) (length '($1..$4))); EN :: "
  575. "$10 = '(/ (+ @0$1..@0$4) (length '(@0$1..@0$4))); EN")
  576. ))
  577. (ert-deftest test-org-table/copy-field ()
  578. "Experiments on how to copy one field into another field.
  579. See also `test-org-table/remote-reference-access'."
  580. (let ((target "
  581. | 0 | replace |
  582. | a b | replace |
  583. | c d | replace |
  584. | | replace |
  585. | 2012-12 | replace |
  586. | [2012-12-31 Mon] | replace |
  587. "))
  588. ;; Lisp formula to copy literally
  589. (org-test-table-target-expect
  590. target
  591. "
  592. | 0 | 0 |
  593. | a b | a b |
  594. | c d | c d |
  595. | | |
  596. | 2012-12 | 2012-12 |
  597. | [2012-12-31 Mon] | [2012-12-31 Mon] |
  598. "
  599. 1 "#+TBLFM: $2 = '(identity $1)")
  600. ;; Calc formula to copy quite literally
  601. (org-test-table-target-expect
  602. target
  603. "
  604. | 0 | 0 |
  605. | a b | a b |
  606. | c d | c d |
  607. | | |
  608. | 2012-12 | 2012-12 |
  609. | [2012-12-31 Mon] | [2012-12-31 Mon] |
  610. "
  611. 1 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  612. "string(\"\"), string(subvec(\"$1\", 2, vlen(\"$1\")))); E"))
  613. ;; Calc formula simple
  614. (org-test-table-target-expect
  615. target
  616. "
  617. | 0 | 0 |
  618. | a b | a b |
  619. | c d | c d |
  620. | | |
  621. | 2012-12 | 2000 |
  622. | [2012-12-31 Mon] | [2012-12-31 Mon] |
  623. "
  624. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1); E")))
  625. (ert-deftest test-org-table/sub-total ()
  626. "Grouped rows with sub-total.
  627. Begin range with \"@II\" to handle multiline header. Convert
  628. integer to float with \"+.0\" for sub-total of items c1 and c2.
  629. Sum empty fields as value zero but without ignoring them for
  630. \"vlen\" with format specifier \"EN\". Format possibly empty
  631. results with the Calc formatter \"f-1\" instead of the printf
  632. formatter \"%.1f\"."
  633. (org-test-table-target-expect
  634. "
  635. |-------+---------+---------|
  636. | Item | Item | Sub- |
  637. | name | value | total |
  638. |-------+---------+---------|
  639. | a1 | 4.1 | replace |
  640. | a2 | 8.2 | replace |
  641. | a3 | | replace |
  642. |-------+---------+---------|
  643. | b1 | 16.0 | replace |
  644. |-------+---------+---------|
  645. | c1 | 32 | replace |
  646. | c2 | 64 | replace |
  647. |-------+---------+---------|
  648. | Total | replace | replace |
  649. |-------+---------+---------|
  650. "
  651. "
  652. |-------+-------+-------|
  653. | Item | Item | Sub- |
  654. | name | value | total |
  655. |-------+-------+-------|
  656. | a1 | 4.1 | |
  657. | a2 | 8.2 | |
  658. | a3 | | 12.3 |
  659. |-------+-------+-------|
  660. | b1 | 16.0 | 16.0 |
  661. |-------+-------+-------|
  662. | c1 | 32 | |
  663. | c2 | 64 | 96.0 |
  664. |-------+-------+-------|
  665. | Total | 124.3 | |
  666. |-------+-------+-------|
  667. "
  668. 1 (concat "#+TBLFM: @>$2 = vsum(@II..@>>) ::"
  669. "$3 = if(vlen(@0..@+I) == 1, "
  670. "vsum(@-I$2..@+I$2) +.0, string(\"\")); EN f-1 :: "
  671. "@>$3 = string(\"\")")))
  672. (ert-deftest test-org-table/org-lookup-all ()
  673. "Use `org-lookup-all' for several GROUP BY as in SQL and for ranking.
  674. See also http://orgmode.org/worg/org-tutorials/org-lookups.html ."
  675. (let ((data "
  676. #+NAME: data
  677. | Purchase | Product | Shop | Rating |
  678. |----------+---------+------+--------|
  679. | a | p1 | s1 | 1 |
  680. | b | p1 | s2 | 4 |
  681. | c | p2 | s1 | 2 |
  682. | d | p3 | s2 | 8 |
  683. "))
  684. ;; Product rating and ranking by average purchase from "#+NAME: data"
  685. (org-test-table-target-expect
  686. (concat data "
  687. | Product | Rating | Ranking |
  688. |---------+---------+---------|
  689. | p1 | replace | replace |
  690. | p2 | replace | replace |
  691. | p3 | replace | replace |
  692. ")
  693. (concat data "
  694. | Product | Rating | Ranking |
  695. |---------+--------+---------|
  696. | p1 | 2.5 | 2 |
  697. | p2 | 2.0 | 3 |
  698. | p3 | 8.0 | 1 |
  699. ")
  700. 2 (concat
  701. "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
  702. "'(remote(data, @I$2..@>$2)) '(remote(data, @I$4..@>$4))))) "
  703. "(/ (apply '+ all) (length all) 1.0)); L :: "
  704. "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))
  705. ;; Shop rating and ranking by average purchase from "#+NAME: data"
  706. (org-test-table-target-expect
  707. (concat data "
  708. | Shop | Rating | Ranking |
  709. |------+---------+---------|
  710. | s1 | replace | replace |
  711. | s2 | replace | replace |
  712. ")
  713. (concat data "
  714. | Shop | Rating | Ranking |
  715. |------+--------+---------|
  716. | s1 | 1.5 | 2 |
  717. | s2 | 6.0 | 1 |
  718. ")
  719. 2 (concat
  720. "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
  721. "'(remote(data, @I$3..@>$3)) '(remote(data, @I$4..@>$4))))) "
  722. "(/ (apply '+ all) (length all) 1.0)); L :: "
  723. "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))))
  724. (ert-deftest test-org-table/org-table-make-reference/mode-string-EL ()
  725. (fset 'f 'org-table-make-reference)
  726. ;; For Lisp formula only
  727. (should (equal "0" (f "0" t nil 'literal)))
  728. (should (equal "z" (f "z" t nil 'literal)))
  729. (should (equal "" (f "" t nil 'literal)))
  730. (should (equal "0 1" (f '("0" "1") t nil 'literal)))
  731. (should (equal "z 1" (f '("z" "1") t nil 'literal)))
  732. (should (equal " 1" (f '("" "1") t nil 'literal)))
  733. (should (equal " " (f '("" "" ) t nil 'literal))))
  734. (ert-deftest test-org-table/org-table-make-reference/mode-string-E ()
  735. (fset 'f 'org-table-make-reference)
  736. ;; For Lisp formula
  737. (should (equal "\"0\"" (f "0" t nil t)))
  738. (should (equal "\"z\"" (f "z" t nil t)))
  739. (should (equal "\"\"" (f "" t nil t)))
  740. (should (equal "\"0\" \"1\"" (f '("0" "1") t nil t)))
  741. (should (equal "\"z\" \"1\"" (f '("z" "1") t nil t)))
  742. (should (equal "\"\" \"1\"" (f '("" "1") t nil t)))
  743. (should (equal "\"\" \"\"" (f '("" "" ) t nil t)))
  744. ;; For Calc formula
  745. (should (equal "(0)" (f "0" t nil nil)))
  746. (should (equal "(z)" (f "z" t nil nil)))
  747. (should (equal "nan" (f "" t nil nil)))
  748. (should (equal "[0,1]" (f '("0" "1") t nil nil)))
  749. (should (equal "[z,1]" (f '("z" "1") t nil nil)))
  750. (should (equal "[nan,1]" (f '("" "1") t nil nil)))
  751. (should (equal "[nan,nan]" (f '("" "" ) t nil nil)))
  752. ;; For Calc formula, special numbers
  753. (should (equal "(nan)" (f "nan" t nil nil)))
  754. (should (equal "(uinf)" (f "uinf" t nil nil)))
  755. (should (equal "(-inf)" (f "-inf" t nil nil)))
  756. (should (equal "(inf)" (f "inf" t nil nil)))
  757. (should (equal "[nan,1]" (f '( "nan" "1") t nil nil)))
  758. (should (equal "[uinf,1]" (f '("uinf" "1") t nil nil)))
  759. (should (equal "[-inf,1]" (f '("-inf" "1") t nil nil)))
  760. (should (equal "[inf,1]" (f '( "inf" "1") t nil nil))))
  761. (ert-deftest test-org-table/org-table-make-reference/mode-string-EN ()
  762. (fset 'f 'org-table-make-reference)
  763. ;; For Lisp formula
  764. (should (equal "0" (f "0" t t t)))
  765. (should (equal "0" (f "z" t t t)))
  766. (should (equal "0" (f "" t t t)))
  767. (should (equal "0 1" (f '("0" "1") t t t)))
  768. (should (equal "0 1" (f '("z" "1") t t t)))
  769. (should (equal "0 1" (f '("" "1") t t t)))
  770. (should (equal "0 0" (f '("" "" ) t t t)))
  771. ;; For Calc formula
  772. (should (equal "(0)" (f "0" t t nil)))
  773. (should (equal "(0)" (f "z" t t nil)))
  774. (should (equal "(0)" (f "" t t nil)))
  775. (should (equal "[0,1]" (f '("0" "1") t t nil)))
  776. (should (equal "[0,1]" (f '("z" "1") t t nil)))
  777. (should (equal "[0,1]" (f '("" "1") t t nil)))
  778. (should (equal "[0,0]" (f '("" "" ) t t nil)))
  779. ;; For Calc formula, special numbers
  780. (should (equal "(0)" (f "nan" t t nil)))
  781. (should (equal "(0)" (f "uinf" t t nil)))
  782. (should (equal "(0)" (f "-inf" t t nil)))
  783. (should (equal "(0)" (f "inf" t t nil)))
  784. (should (equal "[0,1]" (f '( "nan" "1") t t nil)))
  785. (should (equal "[0,1]" (f '("uinf" "1") t t nil)))
  786. (should (equal "[0,1]" (f '("-inf" "1") t t nil)))
  787. (should (equal "[0,1]" (f '( "inf" "1") t t nil))))
  788. (ert-deftest test-org-table/org-table-make-reference/mode-string-L ()
  789. (fset 'f 'org-table-make-reference)
  790. ;; For Lisp formula only
  791. (should (equal "0" (f "0" nil nil 'literal)))
  792. (should (equal "z" (f "z" nil nil 'literal)))
  793. (should (equal "" (f "" nil nil 'literal)))
  794. (should (equal "0 1" (f '("0" "1") nil nil 'literal)))
  795. (should (equal "z 1" (f '("z" "1") nil nil 'literal)))
  796. (should (equal "1" (f '("" "1") nil nil 'literal)))
  797. (should (equal "" (f '("" "" ) nil nil 'literal))))
  798. (ert-deftest test-org-table/org-table-make-reference/mode-string-none ()
  799. (fset 'f 'org-table-make-reference)
  800. ;; For Lisp formula
  801. (should (equal "\"0\"" (f "0" nil nil t)))
  802. (should (equal "\"z\"" (f "z" nil nil t)))
  803. (should (equal "" (f "" nil nil t)))
  804. (should (equal "\"0\" \"1\"" (f '("0" "1") nil nil t)))
  805. (should (equal "\"z\" \"1\"" (f '("z" "1") nil nil t)))
  806. (should (equal "\"1\"" (f '("" "1") nil nil t)))
  807. (should (equal "" (f '("" "" ) nil nil t)))
  808. ;; For Calc formula
  809. (should (equal "(0)" (f "0" nil nil nil)))
  810. (should (equal "(z)" (f "z" nil nil nil)))
  811. (should (equal "(0)" (f "" nil nil nil)))
  812. (should (equal "[0,1]" (f '("0" "1") nil nil nil)))
  813. (should (equal "[z,1]" (f '("z" "1") nil nil nil)))
  814. (should (equal "[1]" (f '("" "1") nil nil nil)))
  815. (should (equal "[]" (f '("" "" ) nil nil nil)))
  816. ;; For Calc formula, special numbers
  817. (should (equal "(nan)" (f "nan" nil nil nil)))
  818. (should (equal "(uinf)" (f "uinf" nil nil nil)))
  819. (should (equal "(-inf)" (f "-inf" nil nil nil)))
  820. (should (equal "(inf)" (f "inf" nil nil nil)))
  821. (should (equal "[nan,1]" (f '( "nan" "1") nil nil nil)))
  822. (should (equal "[uinf,1]" (f '("uinf" "1") nil nil nil)))
  823. (should (equal "[-inf,1]" (f '("-inf" "1") nil nil nil)))
  824. (should (equal "[inf,1]" (f '( "inf" "1") nil nil nil))))
  825. (ert-deftest test-org-table/org-table-make-reference/mode-string-N ()
  826. (fset 'f 'org-table-make-reference)
  827. ;; For Lisp formula
  828. (should (equal "0" (f "0" nil t t)))
  829. (should (equal "0" (f "z" nil t t)))
  830. (should (equal "" (f "" nil t t)))
  831. (should (equal "0 1" (f '("0" "1") nil t t)))
  832. (should (equal "0 1" (f '("z" "1") nil t t)))
  833. (should (equal "1" (f '("" "1") nil t t)))
  834. (should (equal "" (f '("" "" ) nil t t)))
  835. ;; For Calc formula
  836. (should (equal "(0)" (f "0" nil t nil)))
  837. (should (equal "(0)" (f "z" nil t nil)))
  838. (should (equal "(0)" (f "" nil t nil)))
  839. (should (equal "[0,1]" (f '("0" "1") nil t nil)))
  840. (should (equal "[0,1]" (f '("z" "1") nil t nil)))
  841. (should (equal "[1]" (f '("" "1") nil t nil)))
  842. (should (equal "[]" (f '("" "" ) nil t nil)))
  843. ;; For Calc formula, special numbers
  844. (should (equal "(0)" (f "nan" nil t nil)))
  845. (should (equal "(0)" (f "uinf" nil t nil)))
  846. (should (equal "(0)" (f "-inf" nil t nil)))
  847. (should (equal "(0)" (f "inf" nil t nil)))
  848. (should (equal "[0,1]" (f '( "nan" "1") nil t nil)))
  849. (should (equal "[0,1]" (f '("uinf" "1") nil t nil)))
  850. (should (equal "[0,1]" (f '("-inf" "1") nil t nil)))
  851. (should (equal "[0,1]" (f '( "inf" "1") nil t nil))))
  852. (ert-deftest test-org-table/org-table-convert-refs-to-an/1 ()
  853. "Simple reference @2$1."
  854. (should
  855. (string= "A2" (org-table-convert-refs-to-an "@2$1"))))
  856. ;; TODO: Test broken
  857. ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
  858. ;; "Self reference @1$1."
  859. ;; (should
  860. ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
  861. (ert-deftest test-org-table/org-table-convert-refs-to-an/3 ()
  862. "Remote reference."
  863. (should
  864. (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
  865. (ert-deftest test-org-table/org-table-convert-refs-to-rc/1 ()
  866. "Simple reference @2$1."
  867. (should
  868. (string= "@2$1" (org-table-convert-refs-to-rc "A2"))))
  869. (ert-deftest test-org-table/org-table-convert-refs-to-rc/2 ()
  870. "Self reference $0."
  871. (should
  872. (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
  873. ;; TODO: Test Broken
  874. ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
  875. ;; "Remote reference."
  876. ;; (should
  877. ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
  878. (ert-deftest test-org-table/remote-reference-access ()
  879. "Access to remote reference.
  880. See also `test-org-table/copy-field'."
  881. (org-test-table-target-expect
  882. "
  883. #+NAME: table
  884. | | x 42 | |
  885. | replace | replace |
  886. "
  887. "
  888. #+NAME: table
  889. | | x 42 | |
  890. | x 42 | 84 x |
  891. "
  892. 1 (concat "#+TBLFM: "
  893. ;; Copy text without calculation: Use Lisp formula
  894. "$1 = '(identity remote(table, @1$2)) :: "
  895. ;; Do a calculation: Use Calc (or Lisp ) formula
  896. "$2 = 2 * remote(table, @1$2)")))
  897. (ert-deftest test-org-table/remote-reference-indirect ()
  898. "Access to remote reference with indirection of name or ID."
  899. (let ((source-tables "
  900. #+NAME: 2012
  901. | amount |
  902. |--------|
  903. | 1 |
  904. | 2 |
  905. |--------|
  906. | 3 |
  907. #+TBLFM: @>$1 = vsum(@I..@II)
  908. #+NAME: 2013
  909. | amount |
  910. |--------|
  911. | 4 |
  912. | 8 |
  913. |--------|
  914. | 12 |
  915. #+TBLFM: @>$1 = vsum(@I..@II)
  916. "))
  917. ;; Read several remote references from same column
  918. (org-test-table-target-expect
  919. (concat source-tables "
  920. #+NAME: summary
  921. | year | amount |
  922. |-------+---------|
  923. | 2012 | replace |
  924. | 2013 | replace |
  925. |-------+---------|
  926. | total | replace |
  927. ")
  928. (concat source-tables "
  929. #+NAME: summary
  930. | year | amount |
  931. |-------+--------|
  932. | 2012 | 3 |
  933. | 2013 | 12 |
  934. |-------+--------|
  935. | total | 15 |
  936. ")
  937. 1
  938. ;; Calc formula
  939. "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
  940. ;; Lisp formula
  941. (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
  942. "@>$2 = '(+ @I..@II); N"))
  943. ;; Read several remote references from same row
  944. (org-test-table-target-expect
  945. (concat source-tables "
  946. #+NAME: summary
  947. | year | 2012 | 2013 | total |
  948. |--------+---------+---------+---------|
  949. | amount | replace | replace | replace |
  950. ")
  951. (concat source-tables "
  952. #+NAME: summary
  953. | year | 2012 | 2013 | total |
  954. |--------+------+------+-------|
  955. | amount | 3 | 12 | 15 |
  956. ")
  957. 1
  958. ;; Calc formula
  959. "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
  960. ;; Lisp formula
  961. (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
  962. "@2$> = '(+ $<<..$>>); N"))))
  963. (ert-deftest test-org-table/org-at-TBLFM-p ()
  964. (org-test-with-temp-text-in-file
  965. "
  966. | 1 |
  967. | 2 |
  968. #+TBLFM: $2=$1*2
  969. "
  970. (goto-char (point-min))
  971. (forward-line 2)
  972. (should (equal (org-at-TBLFM-p) nil))
  973. (goto-char (point-min))
  974. (forward-line 3)
  975. (should (equal (org-at-TBLFM-p) t))
  976. (goto-char (point-min))
  977. (forward-line 4)
  978. (should (equal (org-at-TBLFM-p) nil))))
  979. (ert-deftest test-org-table/org-table-TBLFM-begin ()
  980. (org-test-with-temp-text-in-file
  981. "
  982. | 1 |
  983. | 2 |
  984. #+TBLFM: $2=$1*2
  985. "
  986. (goto-char (point-min))
  987. (should (equal (org-table-TBLFM-begin)
  988. nil))
  989. (goto-char (point-min))
  990. (forward-line 1)
  991. (should (equal (org-table-TBLFM-begin)
  992. nil))
  993. (goto-char (point-min))
  994. (forward-line 3)
  995. (should (= (org-table-TBLFM-begin)
  996. 14))
  997. (goto-char (point-min))
  998. (forward-line 4)
  999. (should (= (org-table-TBLFM-begin)
  1000. 14))
  1001. ))
  1002. (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
  1003. "For multiple #+TBLFM lines."
  1004. (org-test-with-temp-text-in-file
  1005. "
  1006. | 1 |
  1007. | 2 |
  1008. #+TBLFM: $2=$1*1
  1009. #+TBLFM: $2=$1*2
  1010. "
  1011. (goto-char (point-min))
  1012. (should (equal (org-table-TBLFM-begin)
  1013. nil))
  1014. (goto-char (point-min))
  1015. (forward-line 1)
  1016. (should (equal (org-table-TBLFM-begin)
  1017. nil))
  1018. (goto-char (point-min))
  1019. (forward-line 3)
  1020. (should (= (org-table-TBLFM-begin)
  1021. 14))
  1022. (goto-char (point-min))
  1023. (forward-line 4)
  1024. (should (= (org-table-TBLFM-begin)
  1025. 14))
  1026. (goto-char (point-min))
  1027. (forward-line 5)
  1028. (should (= (org-table-TBLFM-begin)
  1029. 14))
  1030. ))
  1031. (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
  1032. (org-test-with-temp-text-in-file
  1033. "
  1034. | 1 |
  1035. | 2 |
  1036. #+TBLFM: $2=$1*1
  1037. #+TBLFM: $2=$1*2
  1038. | 6 |
  1039. | 7 |
  1040. #+TBLFM: $2=$1*1
  1041. #+TBLFM: $2=$1*2
  1042. "
  1043. (goto-char (point-min))
  1044. (should (equal (org-table-TBLFM-begin)
  1045. nil))
  1046. (goto-char (point-min))
  1047. (forward-line 1)
  1048. (should (equal (org-table-TBLFM-begin)
  1049. nil))
  1050. (goto-char (point-min))
  1051. (forward-line 3)
  1052. (should (= (org-table-TBLFM-begin)
  1053. 14))
  1054. (goto-char (point-min))
  1055. (forward-line 4)
  1056. (should (= (org-table-TBLFM-begin)
  1057. 14))
  1058. (goto-char (point-min))
  1059. (forward-line 5)
  1060. (should (= (org-table-TBLFM-begin)
  1061. 14))
  1062. (goto-char (point-min))
  1063. (forward-line 6)
  1064. (should (= (org-table-TBLFM-begin)
  1065. 14))
  1066. (goto-char (point-min))
  1067. (forward-line 8)
  1068. (should (= (org-table-TBLFM-begin)
  1069. 61))
  1070. (goto-char (point-min))
  1071. (forward-line 9)
  1072. (should (= (org-table-TBLFM-begin)
  1073. 61))
  1074. (goto-char (point-min))
  1075. (forward-line 10)
  1076. (should (= (org-table-TBLFM-begin)
  1077. 61))))
  1078. (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
  1079. (org-test-with-temp-text-in-file
  1080. "
  1081. | 1 | |
  1082. | 2 | |
  1083. #+TBLFM: $2=$1*1
  1084. #+TBLFM: $2=$1*2
  1085. #+TBLFM: $2=$1*3
  1086. "
  1087. (let ((got (progn (goto-char (point-min))
  1088. (forward-line 3)
  1089. (org-table-calc-current-TBLFM)
  1090. (buffer-string)))
  1091. (expect "
  1092. | 1 | 1 |
  1093. | 2 | 2 |
  1094. #+TBLFM: $2=$1*1
  1095. #+TBLFM: $2=$1*2
  1096. #+TBLFM: $2=$1*3
  1097. "))
  1098. (should (string= got
  1099. expect)))
  1100. (let ((got (progn (goto-char (point-min))
  1101. (forward-line 4)
  1102. (org-table-calc-current-TBLFM)
  1103. (buffer-string)))
  1104. (expect "
  1105. | 1 | 2 |
  1106. | 2 | 4 |
  1107. #+TBLFM: $2=$1*1
  1108. #+TBLFM: $2=$1*2
  1109. #+TBLFM: $2=$1*3
  1110. "))
  1111. (should (string= got
  1112. expect)))))
  1113. (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
  1114. "org-table-calc-current-TBLFM should preserve the input as it was."
  1115. (org-test-with-temp-text-in-file
  1116. "
  1117. | 1 | 1 |
  1118. | 2 | 2 |
  1119. #+TBLFM: $2=$1*1
  1120. #+TBLFM: $2=$1*2::$2=$1*2
  1121. #+TBLFM: $2=$1*3
  1122. "
  1123. (let ((expect "
  1124. | 1 | 1 |
  1125. | 2 | 2 |
  1126. #+TBLFM: $2=$1*1
  1127. #+TBLFM: $2=$1*2::$2=$1*2
  1128. #+TBLFM: $2=$1*3
  1129. "))
  1130. (goto-char (point-min))
  1131. (forward-line 4)
  1132. (should-error (org-table-calc-current-TBLFM))
  1133. (setq got (buffer-string))
  1134. (message "%s" got)
  1135. (should (string= got
  1136. expect)))))
  1137. ;;; Radio Tables
  1138. (ert-deftest test-org-table/to-generic ()
  1139. "Test `orgtbl-to-generic' specifications."
  1140. ;; Test :hline parameter.
  1141. (should
  1142. (equal "a\nb"
  1143. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1144. '(:hline nil))))
  1145. (should
  1146. (equal "a\n~\nb"
  1147. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1148. '(:hline "~"))))
  1149. ;; Test :sep parameter.
  1150. (should
  1151. (equal "a!b\nc!d"
  1152. (orgtbl-to-generic
  1153. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1154. '(:sep "!"))))
  1155. ;; Test :hsep parameter.
  1156. (should
  1157. (equal "a!b\nc?d"
  1158. (orgtbl-to-generic
  1159. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1160. '(:sep "?" :hsep "!"))))
  1161. ;; Test :tstart parameter.
  1162. (should
  1163. (equal "<begin>\na"
  1164. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tstart "<begin>"))))
  1165. (should
  1166. (equal "<begin>\na"
  1167. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1168. '(:tstart (lambda () "<begin>")))))
  1169. (should
  1170. (equal "a"
  1171. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1172. '(:tstart "<begin>" :splice t))))
  1173. ;; Test :tend parameter.
  1174. (should
  1175. (equal "a\n<end>"
  1176. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tend "<end>"))))
  1177. (should
  1178. (equal "a\n<end>"
  1179. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1180. '(:tend (lambda () "<end>")))))
  1181. (should
  1182. (equal "a"
  1183. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1184. '(:tend "<end>" :splice t))))
  1185. ;; Test :lstart parameter.
  1186. (should
  1187. (equal "> a"
  1188. (orgtbl-to-generic
  1189. (org-table-to-lisp "| a |") '(:lstart "> "))))
  1190. (should
  1191. (equal "> a"
  1192. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1193. '(:lstart (lambda () "> ")))))
  1194. ;; Test :llstart parameter.
  1195. (should
  1196. (equal "> a\n>> b"
  1197. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1198. '(:lstart "> " :llstart ">> "))))
  1199. ;; Test :hlstart parameter.
  1200. (should
  1201. (equal "!> a\n> b"
  1202. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1203. '(:lstart "> " :hlstart "!> "))))
  1204. ;; Test :hllstart parameter.
  1205. (should
  1206. (equal "!> a\n!!> b\n> c"
  1207. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1208. '(:lstart "> " :hlstart "!> " :hllstart "!!> "))))
  1209. ;; Test :lend parameter.
  1210. (should
  1211. (equal "a <"
  1212. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:lend " <"))))
  1213. ;; Test :llend parameter.
  1214. (should
  1215. (equal "a <\nb <<"
  1216. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1217. '(:lend " <" :llend " <<"))))
  1218. ;; Test :hlend parameter.
  1219. (should
  1220. (equal "a <!\nb <"
  1221. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1222. '(:lend " <" :hlend " <!"))))
  1223. ;; Test :hllend parameter.
  1224. (should
  1225. (equal "a <!\nb <!!\nc <"
  1226. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1227. '(:lend " <" :hlend " <!" :hllend " <!!"))))
  1228. ;; Test :lfmt parameter.
  1229. (should
  1230. (equal "a!b"
  1231. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1232. '(:lfmt "%s!%s"))))
  1233. (should
  1234. (equal "a+b"
  1235. (orgtbl-to-generic
  1236. (org-table-to-lisp "| a | b |")
  1237. '(:lfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1238. (should
  1239. (equal "a!b"
  1240. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1241. '(:lfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1242. ;; Test :llfmt parameter.
  1243. (should
  1244. (equal "a!b"
  1245. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1246. '(:llfmt "%s!%s"))))
  1247. (should
  1248. (equal "a!b\nc+d"
  1249. (orgtbl-to-generic
  1250. (org-table-to-lisp "| a | b |\n| c | d |")
  1251. '(:lfmt "%s!%s" :llfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1252. (should
  1253. (equal "a!b"
  1254. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1255. '(:llfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1256. ;; Test :hlfmt parameter.
  1257. (should
  1258. (equal "a!b\ncd"
  1259. (orgtbl-to-generic
  1260. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1261. '(:hlfmt "%s!%s"))))
  1262. (should
  1263. (equal "a+b\ncd"
  1264. (orgtbl-to-generic
  1265. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1266. '(:hlfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1267. (should
  1268. (equal "a!b\n>c d<"
  1269. (orgtbl-to-generic
  1270. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1271. '(:hlfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1272. ;; Test :hllfmt parameter.
  1273. (should
  1274. (equal "a!b\ncd"
  1275. (orgtbl-to-generic
  1276. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1277. '(:hllfmt "%s!%s"))))
  1278. (should
  1279. (equal "a+b\ncd"
  1280. (orgtbl-to-generic
  1281. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1282. '(:hllfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1283. (should
  1284. (equal "a!b\n>c d<"
  1285. (orgtbl-to-generic
  1286. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1287. '(:hllfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1288. ;; Test :fmt parameter.
  1289. (should
  1290. (equal ">a<\n>b<"
  1291. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1292. '(:fmt ">%s<"))))
  1293. (should
  1294. (equal ">a<b"
  1295. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1296. '(:fmt (1 ">%s<" 2 (lambda (c) c))))))
  1297. (should
  1298. (equal "a b"
  1299. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1300. '(:fmt (2 " %s")))))
  1301. (should
  1302. (equal ">a<"
  1303. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1304. '(:fmt (lambda (c) (format ">%s<" c))))))
  1305. ;; Test :hfmt parameter.
  1306. (should
  1307. (equal ">a<\nb"
  1308. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1309. '(:hfmt ">%s<"))))
  1310. (should
  1311. (equal ">a<b\ncd"
  1312. (orgtbl-to-generic
  1313. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1314. '(:hfmt (1 ">%s<" 2 identity)))))
  1315. (should
  1316. (equal "a b\ncd"
  1317. (orgtbl-to-generic
  1318. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1319. '(:hfmt (2 " %s")))))
  1320. (should
  1321. (equal ">a<\nb"
  1322. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1323. '(:hfmt (lambda (c) (format ">%s<" c))))))
  1324. ;; Test :efmt parameter.
  1325. (should
  1326. (equal "2x10^3"
  1327. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1328. '(:efmt "%sx10^%s"))))
  1329. (should
  1330. (equal "2x10^3"
  1331. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1332. '(:efmt (lambda (m e) (concat m "x10^" e))))))
  1333. (should
  1334. (equal "2x10^3"
  1335. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1336. '(:efmt (1 "%sx10^%s")))))
  1337. (should
  1338. (equal "2x10^3"
  1339. (orgtbl-to-generic
  1340. (org-table-to-lisp "| 2e3 |")
  1341. '(:efmt (1 (lambda (m e) (format "%sx10^%s" m e)))))))
  1342. (should
  1343. (equal "2e3"
  1344. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |") '(:efmt nil))))
  1345. ;; Test :skip parameter.
  1346. (should
  1347. (equal "cd"
  1348. (orgtbl-to-generic
  1349. (org-table-to-lisp "| \ | <c> |\n| a | b |\n|---+---|\n| c | d |")
  1350. '(:skip 2))))
  1351. ;; Test :skipcols parameter.
  1352. (should
  1353. (equal "a\nc"
  1354. (orgtbl-to-generic
  1355. (org-table-to-lisp "| a | b |\n| c | d |") '(:skipcols (2)))))
  1356. (should
  1357. (equal "a\nc"
  1358. (orgtbl-to-generic
  1359. (org-table-to-lisp
  1360. "| / | <c> | <c> |\n| # | a | b |\n|---+---+---|\n| | c | d |")
  1361. '(:skipcols (2)))))
  1362. ;; Test :raw parameter.
  1363. (when (featurep 'ox-latex)
  1364. (should
  1365. (string-match-p
  1366. "/a/"
  1367. (orgtbl-to-generic (org-table-to-lisp "| /a/ | b |")
  1368. '(:backend latex :raw t)))))
  1369. ;; Hooks are ignored.
  1370. (should
  1371. (equal
  1372. "a\nb"
  1373. (let* ((fun-list (list (lambda (backend) (search-forward "a") (insert "hook"))))
  1374. (org-export-before-parsing-hook fun-list)
  1375. (org-export-before-processing-hook fun-list))
  1376. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1377. '(:hline nil)))))
  1378. ;; User-defined export filters are ignored.
  1379. (should
  1380. (equal
  1381. "a\nb"
  1382. (let ((org-export-filter-table-cell-functions (list (lambda (c b i) "filter"))))
  1383. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1384. '(:hline nil)))))
  1385. ;; Macros, even if unknown, are returned as-is.
  1386. (should
  1387. (equal "{{{macro}}}"
  1388. (orgtbl-to-generic (org-table-to-lisp "| {{{macro}}} |") nil))))
  1389. (ert-deftest test-org-table/to-latex ()
  1390. "Test `orgtbl-to-latex' specifications."
  1391. (should
  1392. (equal "\\begin{tabular}{l}\na\\\\\n\\end{tabular}"
  1393. (orgtbl-to-latex (org-table-to-lisp "| a |") nil)))
  1394. ;; Test :environment parameter.
  1395. (should
  1396. (equal "\\begin{tabularx}{l}\na\\\\\n\\end{tabularx}"
  1397. (orgtbl-to-latex (org-table-to-lisp "| a |")
  1398. '(:environment "tabularx"))))
  1399. ;; Test :booktabs parameter.
  1400. (should
  1401. (string-match-p
  1402. "\\toprule" (orgtbl-to-latex (org-table-to-lisp "| a |") '(:booktabs t))))
  1403. ;; Handle LaTeX snippets.
  1404. (should
  1405. (equal "\\begin{tabular}{l}\n\\(x\\)\\\\\n\\end{tabular}"
  1406. (orgtbl-to-latex (org-table-to-lisp "| $x$ |") nil)))
  1407. ;; Test pseudo objects and :raw parameter.
  1408. (should
  1409. (string-match-p
  1410. "\\$x\\$" (orgtbl-to-latex (org-table-to-lisp "| $x$ |") '(:raw t)))))
  1411. (ert-deftest test-org-table/to-html ()
  1412. "Test `orgtbl-to-html' specifications."
  1413. (should
  1414. (equal (orgtbl-to-html (org-table-to-lisp "| a |") nil)
  1415. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">
  1416. <colgroup>
  1417. <col class=\"org-left\" />
  1418. </colgroup>
  1419. <tbody>
  1420. <tr>
  1421. <td class=\"org-left\">a</td>
  1422. </tr>
  1423. </tbody>
  1424. </table>"))
  1425. ;; Test :attributes parameter.
  1426. (should
  1427. (string-match-p
  1428. "<table>"
  1429. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes nil))))
  1430. (should
  1431. (string-match-p
  1432. "<table border=\"2\">"
  1433. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes (:border "2"))))))
  1434. (ert-deftest test-org-table/to-texinfo ()
  1435. "Test `orgtbl-to-texinfo' specifications."
  1436. (should
  1437. (equal "@multitable {a}\n@item a\n@end multitable"
  1438. (orgtbl-to-texinfo (org-table-to-lisp "| a |") nil)))
  1439. ;; Test :columns parameter.
  1440. (should
  1441. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1442. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1443. '(:columns ".4 .6"))))
  1444. (should
  1445. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1446. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1447. '(:columns "@columnfractions .4 .6"))))
  1448. (should
  1449. (equal "@multitable {xxx} {xx}\n@item a\n@tab b\n@end multitable"
  1450. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1451. '(:columns "{xxx} {xx}")))))
  1452. (ert-deftest test-org-table/to-orgtbl ()
  1453. "Test `orgtbl-to-orgtbl' specifications."
  1454. (should
  1455. (equal "| a | b |\n|---+---|\n| c | d |"
  1456. (orgtbl-to-orgtbl
  1457. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |") nil))))
  1458. (ert-deftest test-org-table/to-unicode ()
  1459. "Test `orgtbl-to-unicode' specifications."
  1460. (should
  1461. (equal "━━━\n a \n━━━"
  1462. (orgtbl-to-unicode (org-table-to-lisp "| a |") nil)))
  1463. ;; Test :narrow parameter.
  1464. (should
  1465. (equal "━━━━\n => \n━━━━"
  1466. (orgtbl-to-unicode (org-table-to-lisp "| <2> |\n| xxx |")
  1467. '(:narrow t)))))
  1468. (ert-deftest test-org-table/send-region ()
  1469. "Test `orgtbl-send-table' specifications."
  1470. ;; Error when not at a table.
  1471. (should-error
  1472. (org-test-with-temp-text "Paragraph"
  1473. (orgtbl-send-table)))
  1474. ;; Error when destination is missing.
  1475. (should-error
  1476. (org-test-with-temp-text "#+ORGTBL: SEND\n<point>| a |"
  1477. (orgtbl-send-table)))
  1478. ;; Error when transformation function is not specified.
  1479. (should-error
  1480. (org-test-with-temp-text "
  1481. # BEGIN RECEIVE ORGTBL table
  1482. # END RECEIVE ORGTBL table
  1483. #+ORGTBL: SEND table
  1484. <point>| a |"
  1485. (orgtbl-send-table)))
  1486. ;; Standard test.
  1487. (should
  1488. (equal "| a |\n|---|\n| b |\n"
  1489. (org-test-with-temp-text "
  1490. # BEGIN RECEIVE ORGTBL table
  1491. # END RECEIVE ORGTBL table
  1492. #+ORGTBL: SEND table orgtbl-to-orgtbl :hlines nil
  1493. <point>| a |\n|---|\n| b |"
  1494. (orgtbl-send-table)
  1495. (goto-char (point-min))
  1496. (buffer-substring-no-properties
  1497. (search-forward "# BEGIN RECEIVE ORGTBL table\n")
  1498. (progn (search-forward "# END RECEIVE ORGTBL table")
  1499. (match-beginning 0))))))
  1500. ;; Allow multiple receiver locations.
  1501. (should
  1502. (org-test-with-temp-text "
  1503. # BEGIN RECEIVE ORGTBL table
  1504. # END RECEIVE ORGTBL table
  1505. #+ORGTBL: SEND table orgtbl-to-orgtbl :hlines nil
  1506. <point>| a |
  1507. # BEGIN RECEIVE ORGTBL table
  1508. # END RECEIVE ORGTBL table"
  1509. (orgtbl-send-table)
  1510. (goto-char (point-min))
  1511. (search-forward "| a |" nil t 3))))
  1512. ;;; Sorting
  1513. (ert-deftest test-org-table/sort-lines ()
  1514. "Test `org-table-sort-lines' specifications."
  1515. ;; Sort numerically.
  1516. (should
  1517. (equal "| 1 | 2 |\n| 2 | 4 |\n| 5 | 3 |\n"
  1518. (org-test-with-temp-text "| <point>1 | 2 |\n| 5 | 3 |\n| 2 | 4 |\n"
  1519. (org-table-sort-lines nil ?n)
  1520. (buffer-string))))
  1521. (should
  1522. (equal "| 5 | 3 |\n| 2 | 4 |\n| 1 | 2 |\n"
  1523. (org-test-with-temp-text "| <point>1 | 2 |\n| 5 | 3 |\n| 2 | 4 |\n"
  1524. (org-table-sort-lines nil ?N)
  1525. (buffer-string))))
  1526. ;; Sort alphabetically.
  1527. (should
  1528. (equal "| a | x |\n| b | 4 |\n| c | 3 |\n"
  1529. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| b | 4 |\n"
  1530. (org-table-sort-lines nil ?a)
  1531. (buffer-string))))
  1532. (should
  1533. (equal "| c | 3 |\n| b | 4 |\n| a | x |\n"
  1534. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| b | 4 |\n"
  1535. (org-table-sort-lines nil ?A)
  1536. (buffer-string))))
  1537. ;; Sort alphabetically with case.
  1538. (should
  1539. (equal "| C |\n| a |\n| b |\n"
  1540. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1541. (org-table-sort-lines t ?a)
  1542. (buffer-string))))
  1543. (should
  1544. (equal "| b |\n| a |\n| C |\n"
  1545. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1546. (org-table-sort-lines nil ?A)
  1547. (buffer-string))))
  1548. ;; Sort by time (timestamps)
  1549. (should
  1550. (equal
  1551. "| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n| <2014-03-04 tue.> |\n"
  1552. (org-test-with-temp-text
  1553. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1554. (org-table-sort-lines nil ?t)
  1555. (buffer-string))))
  1556. (should
  1557. (equal
  1558. "| <2014-03-04 tue.> |\n| <2012-03-29 thu.> |\n| <2008-08-08 sat.> |\n"
  1559. (org-test-with-temp-text
  1560. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1561. (org-table-sort-lines nil ?T)
  1562. (buffer-string))))
  1563. ;; Sort by time (HH:MM values)
  1564. (should
  1565. (equal "| 1:00 |\n| 17:00 |\n| 114:00 |\n"
  1566. (org-test-with-temp-text "| 114:00 |\n| 17:00 |\n| 1:00 |\n"
  1567. (org-table-sort-lines nil ?t)
  1568. (buffer-string))))
  1569. (should
  1570. (equal "| 114:00 |\n| 17:00 |\n| 1:00 |\n"
  1571. (org-test-with-temp-text "| 114:00 |\n| 17:00 |\n| 1:00 |\n"
  1572. (org-table-sort-lines nil ?T)
  1573. (buffer-string))))
  1574. ;; Sort by time (durations)
  1575. (should
  1576. (equal "| 1d 3:00 |\n| 28:00 |\n"
  1577. (org-test-with-temp-text "| 28:00 |\n| 1d 3:00 |\n"
  1578. (org-table-sort-lines nil ?t)
  1579. (buffer-string))))
  1580. ;; Sort with custom functions.
  1581. (should
  1582. (equal "| 22 |\n| 15 |\n| 18 |\n"
  1583. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1584. (org-table-sort-lines nil ?f
  1585. (lambda (s) (% (string-to-number s) 10))
  1586. #'<)
  1587. (buffer-string))))
  1588. (should
  1589. (equal "| 18 |\n| 15 |\n| 22 |\n"
  1590. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1591. (org-table-sort-lines nil ?F
  1592. (lambda (s) (% (string-to-number s) 10))
  1593. #'<)
  1594. (buffer-string))))
  1595. ;; Sort according to current column.
  1596. (should
  1597. (equal "| 1 | 2 |\n| 7 | 3 |\n| 5 | 4 |\n"
  1598. (org-test-with-temp-text "| 1 | <point>2 |\n| 5 | 4 |\n| 7 | 3 |\n"
  1599. (org-table-sort-lines nil ?n)
  1600. (buffer-string))))
  1601. ;; Sort between horizontal separators if possible.
  1602. (should
  1603. (equal
  1604. "| 9 | 8 |\n|---+---|\n| 5 | 3 |\n| 7 | 4 |\n|---+---|\n| 1 | 2 |\n"
  1605. (org-test-with-temp-text
  1606. "| 9 | 8 |\n|---+---|\n| <point>7 | 4 |\n| 5 | 3 |\n|---+---|\n| 1 | 2 |\n"
  1607. (org-table-sort-lines nil ?n)
  1608. (buffer-string)))))
  1609. ;;; Formulas
  1610. (ert-deftest test-org-table/eval-formula ()
  1611. "Test `org-table-eval-formula' specifications."
  1612. ;; Error when not on a table field.
  1613. (should-error
  1614. (org-test-with-temp-text "Text"
  1615. (org-table-eval-formula)))
  1616. (should-error
  1617. (org-test-with-temp-text "| a |\n|---|<point>"
  1618. (org-table-eval-formula)))
  1619. (should-error
  1620. (org-test-with-temp-text "| a |\n#+TBLFM:<point>"
  1621. (org-table-eval-formula)))
  1622. ;; Handle @<, @>, $< and $>.
  1623. (should
  1624. (equal "| 1 |\n| 1 |"
  1625. (org-test-with-temp-text "| <point> |\n| 1 |"
  1626. (org-table-eval-formula nil "@>" nil nil t)
  1627. (buffer-string))))
  1628. (should
  1629. (equal "| 1 |\n| 1 |"
  1630. (org-test-with-temp-text "| 1 |\n| <point> |"
  1631. (org-table-eval-formula nil "@<" nil nil t)
  1632. (buffer-string))))
  1633. (should
  1634. (equal "| 1 | 1 |"
  1635. (org-test-with-temp-text "| <point> | 1 |"
  1636. (org-table-eval-formula nil "$>" nil nil t)
  1637. (buffer-string))))
  1638. (should
  1639. (equal "| 1 | 1 |"
  1640. (org-test-with-temp-text "| 1 | <point> |"
  1641. (org-table-eval-formula nil "$<" nil nil t)
  1642. (buffer-string)))))
  1643. (ert-deftest test-org-table/field-formula-outside-table ()
  1644. "Test `org-table-formula-create-columns' variable."
  1645. ;; Refuse to create column if variable is nil.
  1646. (should-error
  1647. (org-test-with-temp-text "
  1648. | 2 |
  1649. | 4 |
  1650. | 8 |
  1651. <point>#+TBLFM: @1$2=5"
  1652. (let ((org-table-formula-create-columns nil))
  1653. (org-table-calc-current-TBLFM))
  1654. (buffer-string))
  1655. :type (list 'error 'user-error))
  1656. ;; If the variable is non-nil, field formulas and columns formulas
  1657. ;; can create tables.
  1658. (should
  1659. (equal
  1660. "
  1661. | 2 | 5 |
  1662. | 4 | |
  1663. | 8 | |
  1664. #+TBLFM: @1$2=5"
  1665. (org-test-with-temp-text "
  1666. | 2 |
  1667. | 4 |
  1668. | 8 |
  1669. <point>#+TBLFM: @1$2=5"
  1670. (let ((org-table-formula-create-columns t))
  1671. (org-table-calc-current-TBLFM))
  1672. (buffer-string))))
  1673. (should
  1674. (equal
  1675. "
  1676. | 2 | | 15 |
  1677. | 4 | | 15 |
  1678. | 8 | | 15 |
  1679. #+TBLFM: $3=15"
  1680. (org-test-with-temp-text "
  1681. | 2 |
  1682. | 4 |
  1683. | 8 |
  1684. <point>#+TBLFM: $3=15"
  1685. (let ((org-table-formula-create-columns t))
  1686. (org-table-calc-current-TBLFM))
  1687. (buffer-string)))))
  1688. (ert-deftest test-org-table/duration ()
  1689. "Test durations in table formulas."
  1690. ;; Durations in cells.
  1691. (should
  1692. (string-match "| 2:12 | 1:47 | 03:59:00 |"
  1693. (org-test-with-temp-text "
  1694. | 2:12 | 1:47 | |
  1695. <point>#+TBLFM: @1$3=$1+$2;T"
  1696. (org-table-calc-current-TBLFM)
  1697. (buffer-string))))
  1698. (should
  1699. (string-match "| 2:12 | 1:47 | 03:59 |"
  1700. (org-test-with-temp-text "
  1701. | 2:12 | 1:47 | |
  1702. <point>#+TBLFM: @1$3=$1+$2;U"
  1703. (org-table-calc-current-TBLFM)
  1704. (buffer-string))))
  1705. (should
  1706. (string-match "| 3:02:20 | -2:07:00 | 0.92 |"
  1707. (org-test-with-temp-text "
  1708. | 3:02:20 | -2:07:00 | |
  1709. <point>#+TBLFM: @1$3=$1+$2;t"
  1710. (org-table-calc-current-TBLFM)
  1711. (buffer-string))))
  1712. ;; Durations set through properties.
  1713. (should
  1714. (string-match "| 16:00:00 |"
  1715. (org-test-with-temp-text "* H
  1716. :PROPERTIES:
  1717. :time_constant: 08:00:00
  1718. :END:
  1719. | |
  1720. <point>#+TBLFM: $1=2*$PROP_time_constant;T"
  1721. (org-table-calc-current-TBLFM)
  1722. (buffer-string))))
  1723. (should
  1724. (string-match "| 16.00 |"
  1725. (org-test-with-temp-text "* H
  1726. :PROPERTIES:
  1727. :time_constant: 08:00:00
  1728. :END:
  1729. | |
  1730. <point>#+TBLFM: $1=2*$PROP_time_constant;t"
  1731. (org-table-calc-current-TBLFM)
  1732. (buffer-string)))))
  1733. (ert-deftest test-org-table/end-on-hline ()
  1734. "Test with a table ending on a hline."
  1735. (should
  1736. (equal
  1737. (org-test-with-temp-text
  1738. "
  1739. | 1 | 2 | 3 |
  1740. | 4 | 5 | 6 |
  1741. | | | |
  1742. |---+---+---|
  1743. <point>#+TBLFM: @3$2..@3$>=vsum(@1..@2)"
  1744. (org-table-calc-current-TBLFM)
  1745. (buffer-string))
  1746. "
  1747. | 1 | 2 | 3 |
  1748. | 4 | 5 | 6 |
  1749. | | 7 | 9 |
  1750. |---+---+---|
  1751. #+TBLFM: @3$2..@3$>=vsum(@1..@2)")))
  1752. (ert-deftest test-org-table/named-field ()
  1753. "Test formula with a named field."
  1754. (should
  1755. (string-match-p
  1756. "| +| +1 +|"
  1757. (org-test-with-temp-text "
  1758. | | |
  1759. | ^ | name |
  1760. <point>#+TBLFM: $name=1"
  1761. (org-table-calc-current-TBLFM)
  1762. (buffer-string))))
  1763. (should
  1764. (string-match-p
  1765. "| +| +1 +|"
  1766. (org-test-with-temp-text "
  1767. | _ | name |
  1768. | | |
  1769. <point>#+TBLFM: $name=1"
  1770. (org-table-calc-current-TBLFM)
  1771. (buffer-string)))))
  1772. (ert-deftest test-org-table/named-column ()
  1773. "Test formula with a named field."
  1774. (should
  1775. (string-match-p
  1776. "| +| +1 +| +1 +|"
  1777. (org-test-with-temp-text "
  1778. | ! | name | |
  1779. | | 1 | |
  1780. <point>#+TBLFM: @2$3=$name"
  1781. (org-table-calc-current-TBLFM)
  1782. (buffer-string)))))
  1783. (ert-deftest test-org-table/formula-priority ()
  1784. "Test field formula priority over column formula."
  1785. ;; Field formulas bind stronger than column formulas.
  1786. (should
  1787. (equal
  1788. "| 1 | 3 |\n| 2 | 99 |\n"
  1789. (org-test-with-temp-text
  1790. "| 1 | |\n| 2 | |\n<point>#+tblfm: $2=3*$1::@2$2=99"
  1791. (org-table-calc-current-TBLFM)
  1792. (buffer-substring-no-properties (point-min) (point)))))
  1793. ;; When field formula is removed, table formulas is applied again.
  1794. (should
  1795. (equal
  1796. "| 1 | 3 |\n| 2 | 6 |\n"
  1797. (org-test-with-temp-text
  1798. "| 1 | |\n| 2 | |\n#+tblfm: $2=3*$1<point>::@2$2=99"
  1799. (org-table-calc-current-TBLFM)
  1800. (delete-region (point) (line-end-position))
  1801. (org-table-calc-current-TBLFM)
  1802. (buffer-substring-no-properties (point-min) (line-beginning-position))))))
  1803. (ert-deftest test-org-table/tab-indent ()
  1804. "Test named fields with tab indentation."
  1805. (should
  1806. (string-match-p
  1807. "| # | 111 |"
  1808. (org-test-with-temp-text
  1809. "
  1810. | ! | sum | | a | b | c |
  1811. |---+------+------+---+----+-----|
  1812. | # | 1011 | 1000 | 1 | 10 | 100 |
  1813. <point>#+TBLFM: $2=$a+$b+$c
  1814. "
  1815. (org-table-calc-current-TBLFM)
  1816. (buffer-string)))))
  1817. (ert-deftest test-org-table/first-rc ()
  1818. "Test \"$<\" and \"@<\" constructs in formulas."
  1819. (should
  1820. (string-match-p
  1821. "| 1 | 2 |"
  1822. (org-test-with-temp-text
  1823. "| | 2 |
  1824. <point>#+TBLFM: $<=1"
  1825. (org-table-calc-current-TBLFM)
  1826. (buffer-string))))
  1827. (should
  1828. (string-match-p
  1829. "| 2 |\n| 2 |"
  1830. (org-test-with-temp-text
  1831. "| 2 |\n| |
  1832. <point>#+TBLFM: @2$1=@<"
  1833. (org-table-calc-current-TBLFM)
  1834. (buffer-string)))))
  1835. (ert-deftest test-org-table/last-rc ()
  1836. "Test \"$>\" and \"@>\" constructs in formulas."
  1837. (should
  1838. (string-match-p
  1839. "| 2 | 1 |"
  1840. (org-test-with-temp-text
  1841. "| 2 | |\n<point>#+TBLFM: $>=1"
  1842. (org-table-calc-current-TBLFM)
  1843. (buffer-string))))
  1844. (should
  1845. (string-match-p
  1846. "| 2 |\n| 2 |"
  1847. (org-test-with-temp-text
  1848. "| 2 |\n| |\n<point>#+TBLFM: @>$1=@<"
  1849. (org-table-calc-current-TBLFM)
  1850. (buffer-string)))))
  1851. (ert-deftest test-org-table/time-stamps ()
  1852. "Test time-stamps handling."
  1853. ;; Standard test.
  1854. (should
  1855. (string-match-p
  1856. "| 1 |"
  1857. (org-test-with-temp-text
  1858. "| <2016-07-07 Sun> | <2016-07-08 Fri> | |\n<point>#+TBLFM: $3=$2-$1"
  1859. (org-table-calc-current-TBLFM)
  1860. (buffer-string))))
  1861. ;; Handle locale specific time-stamps.
  1862. (should
  1863. (string-match-p
  1864. "| 1 |"
  1865. (org-test-with-temp-text
  1866. "| <2016-07-07 Do> | <2016-07-08 Fr> | |\n<point>#+TBLFM: $3=$2-$1"
  1867. (org-table-calc-current-TBLFM)
  1868. (buffer-string)))))
  1869. (ert-deftest test-org-table/orgtbl-ascii-draw ()
  1870. "Test `orgtbl-ascii-draw'."
  1871. ;; First value: Make sure that an integer input value is converted to a
  1872. ;; float before division. Further values: Show some float input value
  1873. ;; ranges corresponding to the same bar width.
  1874. (should
  1875. (equal
  1876. (org-test-with-temp-text
  1877. "
  1878. | Value | <l> |
  1879. |----------+---------|
  1880. | 19 | replace |
  1881. |----------+---------|
  1882. | -0.50001 | replace |
  1883. | -0.49999 | replace |
  1884. | 0.49999 | replace |
  1885. | 0.50001 | replace |
  1886. | 1.49999 | replace |
  1887. | 22.50001 | replace |
  1888. | 23.49999 | replace |
  1889. | 23.50001 | replace |
  1890. | 24.49999 | replace |
  1891. | 24.50001 | replace |
  1892. <point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"
  1893. (org-table-calc-current-TBLFM)
  1894. (buffer-string))
  1895. "
  1896. | Value | <l> |
  1897. |----------+-----------|
  1898. | 19 | 883 |
  1899. |----------+-----------|
  1900. | -0.50001 | too small |
  1901. | -0.49999 | |
  1902. | 0.49999 | |
  1903. | 0.50001 | 1 |
  1904. | 1.49999 | 1 |
  1905. | 22.50001 | 887 |
  1906. | 23.49999 | 887 |
  1907. | 23.50001 | 888 |
  1908. | 24.49999 | 888 |
  1909. | 24.50001 | too large |
  1910. #+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"))
  1911. ;; Draw bars with a bullet. The bullet does not count in the parameter
  1912. ;; WIDTH of `orgtbl-ascii-draw'.
  1913. (should
  1914. (equal
  1915. (org-test-with-temp-text
  1916. "
  1917. | -1 | replace |
  1918. | 0 | replace |
  1919. | 1 | replace |
  1920. | 2 | replace |
  1921. | 3 | replace |
  1922. | 4 | replace |
  1923. <point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 3 3 \"$-\")"
  1924. (org-table-calc-current-TBLFM)
  1925. (buffer-string))
  1926. "
  1927. | -1 | too small |
  1928. | 0 | $ |
  1929. | 1 | -$ |
  1930. | 2 | --$ |
  1931. | 3 | ---$ |
  1932. | 4 | too large |
  1933. #+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 3 3 \"$-\")")))
  1934. (ert-deftest test-org-table/single-rowgroup ()
  1935. "Test column formula in a table with a single rowgroup."
  1936. (should
  1937. (equal
  1938. "
  1939. |---+---|
  1940. | 1 | 0 |
  1941. |---+---|
  1942. #+TBLFM: $2=$1-1"
  1943. (org-test-with-temp-text "
  1944. |---+---|
  1945. | 1 | |
  1946. |---+---|
  1947. <point>#+TBLFM: $2=$1-1"
  1948. (org-table-calc-current-TBLFM)
  1949. (buffer-string))))
  1950. (should
  1951. (equal
  1952. "
  1953. | 1 | 0 |
  1954. #+TBLFM: $2=$1-1"
  1955. (org-test-with-temp-text "
  1956. | 1 | |
  1957. <point>#+TBLFM: $2=$1-1"
  1958. (org-table-calc-current-TBLFM)
  1959. (buffer-string)))))
  1960. ;;; Navigation
  1961. (ert-deftest test-org-table/next-field ()
  1962. "Test `org-table-next-field' specifications."
  1963. ;; Regular test.
  1964. (should
  1965. (equal
  1966. "b"
  1967. (org-test-with-temp-text "| a<point> | b |"
  1968. (org-table-next-field)
  1969. (org-trim (org-table-get-field)))))
  1970. ;; Create new rows as needed.
  1971. (should
  1972. (equal
  1973. "| a |\n| |\n"
  1974. (org-test-with-temp-text "| a<point> |"
  1975. (org-table-next-field)
  1976. (buffer-string))))
  1977. ;; Jump over hlines, if `org-table-tab-jumps-over-hlines' is
  1978. ;; non-nil.
  1979. (should
  1980. (equal
  1981. "b"
  1982. (org-test-with-temp-text "| a<point> |\n|---|\n| b |"
  1983. (let ((org-table-tab-jumps-over-hlines t)) (org-table-next-field))
  1984. (org-trim (org-table-get-field)))))
  1985. ;; If `org-table-tab-jumps-over-hlines' is nil, however, create
  1986. ;; a new row before the rule.
  1987. (should
  1988. (equal
  1989. "| a |\n| |\n|---|\n| b |"
  1990. (org-test-with-temp-text "| a<point> |\n|---|\n| b |"
  1991. (let ((org-table-tab-jumps-over-hlines nil)) (org-table-next-field))
  1992. (buffer-string)))))
  1993. (ert-deftest test-org-table/previous-field ()
  1994. "Test `org-table-previous-field' specifications."
  1995. ;; Regular tests.
  1996. (should
  1997. (eq ?a
  1998. (org-test-with-temp-text "| a | <point>b |"
  1999. (org-table-previous-field)
  2000. (char-after))))
  2001. (should
  2002. (eq ?a
  2003. (org-test-with-temp-text "| a |\n| <point>b |"
  2004. (org-table-previous-field)
  2005. (char-after))))
  2006. ;; Find previous field across horizontal rules.
  2007. (should
  2008. (eq ?a
  2009. (org-test-with-temp-text "| a |\n|---|\n| <point>b |"
  2010. (org-table-previous-field)
  2011. (char-after))))
  2012. ;; When called on a horizontal rule, find previous data field.
  2013. (should
  2014. (eq ?b
  2015. (org-test-with-temp-text "| a | b |\n|---+-<point>--|"
  2016. (org-table-previous-field)
  2017. (char-after))))
  2018. ;; Error when at first field. Make sure to preserve original
  2019. ;; position.
  2020. (should-error
  2021. (org-test-with-temp-text "| <point> a|"
  2022. (org-table-previous-field)))
  2023. (should-error
  2024. (org-test-with-temp-text "|---|\n| <point>a |"
  2025. (org-table-previous-field)))
  2026. (should
  2027. (eq ?a
  2028. (org-test-with-temp-text "|---|\n| <point>a |"
  2029. (ignore-errors (org-table-previous-field))
  2030. (char-after)))))
  2031. ;;; Moving rows, moving columns
  2032. (ert-deftest test-org-table/move-row-down ()
  2033. "Test `org-table-move-row-down' specifications."
  2034. ;; Error out when row cannot be moved, e.g., it is the last row in
  2035. ;; the table.
  2036. (should-error
  2037. (org-test-with-temp-text "| a |"
  2038. (org-table-move-row-down)))
  2039. (should-error
  2040. (org-test-with-temp-text "| a |\n"
  2041. (org-table-move-row-down)))
  2042. (should-error
  2043. (org-test-with-temp-text "| a |\n| <point>b |"
  2044. (org-table-move-row-down)))
  2045. ;; Move data lines.
  2046. (should
  2047. (equal "| b |\n| a |\n"
  2048. (org-test-with-temp-text "| a |\n| b |\n"
  2049. (org-table-move-row-down)
  2050. (buffer-string))))
  2051. (should
  2052. (equal "|---|\n| a |\n"
  2053. (org-test-with-temp-text "| a |\n|---|\n"
  2054. (org-table-move-row-down)
  2055. (buffer-string))))
  2056. ;; Move hlines.
  2057. (should
  2058. (equal "| b |\n|---|\n"
  2059. (org-test-with-temp-text "|---|\n| b |\n"
  2060. (org-table-move-row-down)
  2061. (buffer-string))))
  2062. (should
  2063. (equal "|---|\n|---|\n"
  2064. (org-test-with-temp-text "|---|\n|---|\n"
  2065. (org-table-move-row-down)
  2066. (buffer-string))))
  2067. ;; Move rows even without a final newline.
  2068. (should
  2069. (equal "| b |\n| a |\n"
  2070. (org-test-with-temp-text "| a |\n| b |"
  2071. (org-table-move-row-down)
  2072. (buffer-string)))))
  2073. (ert-deftest test-org-table/move-row-up ()
  2074. "Test `org-table-move-row-up' specifications."
  2075. ;; Error out when row cannot be moved, e.g., it is the first row in
  2076. ;; the table.
  2077. (should-error
  2078. (org-test-with-temp-text "| a |"
  2079. (org-table-move-row-up)))
  2080. (should-error
  2081. (org-test-with-temp-text "| a |\n"
  2082. (org-table-move-row-up)))
  2083. ;; Move data lines.
  2084. (should
  2085. (equal "| b |\n| a |\n"
  2086. (org-test-with-temp-text "| a |\n| <point>b |\n"
  2087. (org-table-move-row-up)
  2088. (buffer-string))))
  2089. (should
  2090. (equal "| b |\n|---|\n"
  2091. (org-test-with-temp-text "|---|\n| <point>b |\n"
  2092. (org-table-move-row-up)
  2093. (buffer-string))))
  2094. ;; Move hlines.
  2095. (should
  2096. (equal "|---|\n| a |\n"
  2097. (org-test-with-temp-text "| a |\n|<point>---|\n"
  2098. (org-table-move-row-up)
  2099. (buffer-string))))
  2100. (should
  2101. (equal "|---|\n|---|\n"
  2102. (org-test-with-temp-text "|---|\n|<point>---|\n"
  2103. (org-table-move-row-up)
  2104. (buffer-string))))
  2105. ;; Move rows even without a final newline.
  2106. (should
  2107. (equal "| b |\n| a |\n"
  2108. (org-test-with-temp-text "| a |\n| <point>b |"
  2109. (org-table-move-row-up)
  2110. (buffer-string)))))
  2111. ;;; Miscellaneous
  2112. (ert-deftest test-org-table/get-field ()
  2113. "Test `org-table-get-field' specifications."
  2114. ;; Regular test.
  2115. (should
  2116. (equal " a "
  2117. (org-test-with-temp-text "| <point>a |" (org-table-get-field))))
  2118. ;; Get field in open last column.
  2119. (should
  2120. (equal " a "
  2121. (org-test-with-temp-text "| <point>a " (org-table-get-field))))
  2122. ;; Get empty field.
  2123. (should
  2124. (equal ""
  2125. (org-test-with-temp-text "|<point>|" (org-table-get-field))))
  2126. (should
  2127. (equal " "
  2128. (org-test-with-temp-text "| <point>|" (org-table-get-field))))
  2129. ;; Outside the table, return the empty string.
  2130. (should
  2131. (equal ""
  2132. (org-test-with-temp-text "<point>| a |" (org-table-get-field))))
  2133. (should
  2134. (equal ""
  2135. (org-test-with-temp-text "| a |<point>" (org-table-get-field))))
  2136. ;; With optional N argument, select a particular column in current
  2137. ;; row.
  2138. (should
  2139. (equal " 3 "
  2140. (org-test-with-temp-text "| 1 | 2 | 3 |" (org-table-get-field 3))))
  2141. (should
  2142. (equal " 4 "
  2143. (org-test-with-temp-text "| 1 | 2 |\n<point>| 3 | 4 |"
  2144. (org-table-get-field 2))))
  2145. ;; REPLACE optional argument is used to replace selected field.
  2146. (should
  2147. (equal "| foo |"
  2148. (org-test-with-temp-text "| <point>1 |"
  2149. (org-table-get-field nil " foo ")
  2150. (buffer-string))))
  2151. (should
  2152. (equal "| 1 | 2 | foo |"
  2153. (org-test-with-temp-text "| 1 | 2 | 3 |"
  2154. (org-table-get-field 3 " foo ")
  2155. (buffer-string))))
  2156. (should
  2157. (equal " 4 "
  2158. (org-test-with-temp-text "| 1 | 2 |\n<point>| 3 | 4 |"
  2159. (org-table-get-field 2))))
  2160. ;; An empty REPLACE string clears the field.
  2161. (should
  2162. (equal "| |"
  2163. (org-test-with-temp-text "| <point>1 |"
  2164. (org-table-get-field nil "")
  2165. (buffer-string))))
  2166. ;; When using REPLACE still return old value.
  2167. (should
  2168. (equal " 1 "
  2169. (org-test-with-temp-text "| <point>1 |"
  2170. (org-table-get-field nil " foo ")))))
  2171. (provide 'test-org-table)
  2172. ;;; test-org-table.el ends here