test-org-table.el 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  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/lisp-return-value ()
  372. "Basic: Return value of Lisp formulas."
  373. (org-test-table-target-expect
  374. "
  375. | | nil | (list) | '() |
  376. |-------------------------+-------------+--------+-----|
  377. | type-of, no L | replace (r) | r | r |
  378. | type-of identity, no L | r | r | r |
  379. | identity, no L | r | r | r |
  380. |-------------------------+-------------+--------+-----|
  381. | type-of \"@1\" | r | r | r |
  382. | type-of (identity \"@1\") | r | r | r |
  383. | identity \"@1\" | r | r | r |
  384. |-------------------------+-------------+--------+-----|
  385. | type-of @1 | r | r | r |
  386. | type-of (identity @1) | r | r | r |
  387. | identity @1 | r | r | r |
  388. "
  389. "
  390. | | nil | (list) | '() |
  391. |-------------------------+--------+--------+--------|
  392. | type-of, no L | string | string | string |
  393. | type-of identity, no L | string | string | string |
  394. | identity, no L | nil | (list) | '() |
  395. |-------------------------+--------+--------+--------|
  396. | type-of \"@1\" | string | string | string |
  397. | type-of (identity \"@1\") | string | string | string |
  398. | identity \"@1\" | nil | (list) | '() |
  399. |-------------------------+--------+--------+--------|
  400. | type-of @1 | symbol | symbol | symbol |
  401. | type-of (identity @1) | symbol | symbol | symbol |
  402. | identity @1 | nil | nil | nil |
  403. "
  404. 1 (concat "#+TBLFM: @2$<<..@2$> = '(type-of @1) :: "
  405. "@3$<<..@3$> = '(type-of (identity @1)) :: "
  406. "@4$<<..@4$> = '(identity @1) :: @5$<<..@>$> = '(@0$1); L")))
  407. (ert-deftest test-org-table/compare ()
  408. "Basic: Compare field references in Calc."
  409. (org-test-table-target-expect
  410. "
  411. | | 0 | z | | nan | uinf | -inf | inf |
  412. |------+------+------+------+------+------+------+------|
  413. | 0 | repl | repl | repl | repl | repl | repl | repl |
  414. | z | repl | repl | repl | repl | repl | repl | repl |
  415. | | repl | repl | repl | repl | repl | repl | repl |
  416. | nan | repl | repl | repl | repl | repl | repl | repl |
  417. | uinf | repl | repl | repl | repl | repl | repl | repl |
  418. | -inf | repl | repl | repl | repl | repl | repl | repl |
  419. | inf | repl | repl | repl | repl | repl | repl | repl |
  420. "
  421. "
  422. | | 0 | z | | nan | uinf | -inf | inf |
  423. |------+---+---+---+-----+------+------+-----|
  424. | 0 | x | | | | | | |
  425. | z | | x | | | | | |
  426. | | | | x | | | | |
  427. | nan | | | | x | | | |
  428. | uinf | | | | | x | | |
  429. | -inf | | | | | | x | |
  430. | inf | | | | | | | x |
  431. "
  432. 1
  433. ;; Compare field reference ($1) with field reference (@1)
  434. "#+TBLFM: @<<$<<..@>$> = if(\"$1\" == \"@1\", x, string(\"\")); E"
  435. ;; Compare field reference ($1) with absolute term
  436. (concat "#+TBLFM: "
  437. "$2 = if(\"$1\" == \"(0)\" , x, string(\"\")); E :: "
  438. "$3 = if(\"$1\" == \"(z)\" , x, string(\"\")); E :: "
  439. "$4 = if(\"$1\" == \"nan\" , x, string(\"\")); E :: "
  440. "$5 = if(\"$1\" == \"(nan)\" , x, string(\"\")); E :: "
  441. "$6 = if(\"$1\" == \"(uinf)\", x, string(\"\")); E :: "
  442. "$7 = if(\"$1\" == \"(-inf)\", x, string(\"\")); E :: "
  443. "$8 = if(\"$1\" == \"(inf)\" , x, string(\"\")); E"))
  444. ;; Check field reference converted from an empty field: Despite this
  445. ;; field reference will not end up in a result, Calc evaluates it.
  446. ;; Make sure that also then there is no Calc error.
  447. (org-test-table-target-expect
  448. "
  449. | 0 | replace |
  450. | z | replace |
  451. | | replace |
  452. | nan | replace |
  453. "
  454. "
  455. | 0 | 1 |
  456. | z | z + 1 |
  457. | | |
  458. | nan | nan |
  459. "
  460. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"))
  461. (ert-deftest test-org-table/empty-field ()
  462. "Examples how to deal with empty fields."
  463. ;; Test if one field is empty, else do a calculation
  464. (org-test-table-target-expect
  465. "
  466. | -1 | replace |
  467. | 0 | replace |
  468. | | replace |
  469. "
  470. "
  471. | -1 | 0 |
  472. | 0 | 1 |
  473. | | |
  474. "
  475. 1
  476. ;; Calc formula
  477. "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"
  478. ;; Lisp formula
  479. "#+TBLFM: $2 = '(if (eq \"$1\" \"\") \"\" (1+ $1)); L")
  480. ;; Test if several fields are empty, else do a calculation
  481. (org-test-table-target-expect
  482. "
  483. | 1 | 2 | replace |
  484. | 4 | | replace |
  485. | | 8 | replace |
  486. | | | replace |
  487. "
  488. "
  489. | 1 | 2 | 3 |
  490. | 4 | | |
  491. | | 8 | |
  492. | | | |
  493. "
  494. 1
  495. ;; Calc formula
  496. (concat "#+TBLFM: $3 = if(\"$1\" == \"nan\" || \"$2\" == \"nan\", "
  497. "string(\"\"), $1 + $2); E")
  498. ;; Lisp formula
  499. (concat "#+TBLFM: $3 = '(if (or (eq \"$1\" \"\") (eq \"$2\" \"\")) "
  500. "\"\" (+ $1 $2)); L"))
  501. ;; $2: Use $1 + 0.5 if $1 available, else only reformat $2 if $2 available
  502. (org-test-table-target-expect
  503. "
  504. | 1.5 | 0 |
  505. | 3.5 | |
  506. | | 5 |
  507. | | |
  508. "
  509. "
  510. | 1.5 | 2.0 |
  511. | 3.5 | 4.0 |
  512. | | 5.0 |
  513. | | |
  514. "
  515. 1
  516. ;; Calc formula
  517. (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  518. "if(\"$2\" == \"nan\", string(\"\"), $2 +.0), $1 + 0.5); E f-1")
  519. ;; Lisp formula not implemented yet
  520. )
  521. ;; Empty fields in simple and complex range reference
  522. (org-test-table-target-expect
  523. "
  524. | | | | | repl | repl | repl | repl | repl | repl |
  525. | | | 5 | 7 | repl | repl | repl | repl | repl | repl |
  526. | 1 | 3 | 5 | 7 | repl | repl | repl | repl | repl | repl |
  527. "
  528. "
  529. | | | | | | | | | 0 | 0 |
  530. | | | 5 | 7 | | | 6 | 6 | 3 | 3 |
  531. | 1 | 3 | 5 | 7 | 4 | 4 | 4 | 4 | 4 | 4 |
  532. "
  533. 1
  534. ;; Calc formula
  535. (concat
  536. "#+TBLFM: "
  537. "$5 = if(typeof(vmean($1..$4)) == 12, "
  538. "string(\"\"), vmean($1..$4)); E :: "
  539. "$6 = if(typeof(vmean(@0$1..@0$4)) == 12, "
  540. "string(\"\"), vmean(@0$1..@0$4)); E :: "
  541. "$7 = if(\"$1..$4\" == \"[]\", string(\"\"), vmean($1..$4)) :: "
  542. "$8 = if(\"@0$1..@0$4\" == \"[]\", string(\"\"), vmean(@0$1..@0$4)) :: "
  543. "$9 = vmean($1..$4); EN :: "
  544. "$10 = vmean(@0$1..@0$4); EN")
  545. ;; Lisp formula
  546. (concat
  547. "#+TBLFM: "
  548. "$5 = '(let ((l '($1..$4))) (if (member \"\" l) \"\" "
  549. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  550. "$6 = '(let ((l '(@0$1..@0$4))) (if (member \"\" l) \"\" "
  551. "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
  552. "$7 = '(let ((l '($1..$4))) "
  553. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  554. "$8 = '(let ((l '(@0$1..@0$4))) "
  555. "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
  556. "$9 = '(/ (+ $1..$4) (length '($1..$4))); EN :: "
  557. "$10 = '(/ (+ @0$1..@0$4) (length '(@0$1..@0$4))); EN")
  558. ))
  559. (ert-deftest test-org-table/copy-field ()
  560. "Experiments on how to copy one field into another field.
  561. See also `test-org-table/remote-reference-access'."
  562. (let ((target "
  563. | 0 | replace |
  564. | a b | replace |
  565. | c d | replace |
  566. | | replace |
  567. | 2012-12 | replace |
  568. | [2012-12-31 Mon] | replace |
  569. "))
  570. ;; Lisp formula to copy literally
  571. (org-test-table-target-expect
  572. target
  573. "
  574. | 0 | 0 |
  575. | a b | a b |
  576. | c d | c d |
  577. | | |
  578. | 2012-12 | 2012-12 |
  579. | [2012-12-31 Mon] | [2012-12-31 Mon] |
  580. "
  581. 1 "#+TBLFM: $2 = '(identity $1)")
  582. ;; Calc formula to copy quite literally
  583. (org-test-table-target-expect
  584. target
  585. "
  586. | 0 | 0 |
  587. | a b | a b |
  588. | c d | c d |
  589. | | |
  590. | 2012-12 | 2012-12 |
  591. | [2012-12-31 Mon] | <2012-12-31 Mon> |
  592. "
  593. 1 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
  594. "string(\"\"), string(subvec(\"$1\", 2, vlen(\"$1\")))); E"))
  595. ;; Calc formula simple
  596. (org-test-table-target-expect
  597. target
  598. "
  599. | 0 | 0 |
  600. | a b | a b |
  601. | c d | c d |
  602. | | |
  603. | 2012-12 | 2000 |
  604. | [2012-12-31 Mon] | <2012-12-31 Mon> |
  605. "
  606. 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1); E")))
  607. (ert-deftest test-org-table/sub-total ()
  608. "Grouped rows with sub-total.
  609. Begin range with \"@II\" to handle multiline header. Convert
  610. integer to float with \"+.0\" for sub-total of items c1 and c2.
  611. Sum empty fields as value zero but without ignoring them for
  612. \"vlen\" with format specifier \"EN\". Format possibly empty
  613. results with the Calc formatter \"f-1\" instead of the printf
  614. formatter \"%.1f\"."
  615. (org-test-table-target-expect
  616. "
  617. |-------+---------+---------|
  618. | Item | Item | Sub- |
  619. | name | value | total |
  620. |-------+---------+---------|
  621. | a1 | 4.1 | replace |
  622. | a2 | 8.2 | replace |
  623. | a3 | | replace |
  624. |-------+---------+---------|
  625. | b1 | 16.0 | replace |
  626. |-------+---------+---------|
  627. | c1 | 32 | replace |
  628. | c2 | 64 | replace |
  629. |-------+---------+---------|
  630. | Total | replace | replace |
  631. |-------+---------+---------|
  632. "
  633. "
  634. |-------+-------+-------|
  635. | Item | Item | Sub- |
  636. | name | value | total |
  637. |-------+-------+-------|
  638. | a1 | 4.1 | |
  639. | a2 | 8.2 | |
  640. | a3 | | 12.3 |
  641. |-------+-------+-------|
  642. | b1 | 16.0 | 16.0 |
  643. |-------+-------+-------|
  644. | c1 | 32 | |
  645. | c2 | 64 | 96.0 |
  646. |-------+-------+-------|
  647. | Total | 124.3 | |
  648. |-------+-------+-------|
  649. "
  650. 1 (concat "#+TBLFM: @>$2 = vsum(@II..@>>) ::"
  651. "$3 = if(vlen(@0..@+I) == 1, "
  652. "vsum(@-I$2..@+I$2) +.0, string(\"\")); EN f-1 :: "
  653. "@>$3 = string(\"\")")))
  654. (ert-deftest test-org-table/org-lookup-all ()
  655. "Use `org-lookup-all' for several GROUP BY as in SQL and for ranking.
  656. See also http://orgmode.org/worg/org-tutorials/org-lookups.html ."
  657. (let ((data "
  658. #+NAME: data
  659. | Purchase | Product | Shop | Rating |
  660. |----------+---------+------+--------|
  661. | a | p1 | s1 | 1 |
  662. | b | p1 | s2 | 4 |
  663. | c | p2 | s1 | 2 |
  664. | d | p3 | s2 | 8 |
  665. "))
  666. ;; Product rating and ranking by average purchase from "#+NAME: data"
  667. (org-test-table-target-expect
  668. (concat data "
  669. | Product | Rating | Ranking |
  670. |---------+---------+---------|
  671. | p1 | replace | replace |
  672. | p2 | replace | replace |
  673. | p3 | replace | replace |
  674. ")
  675. (concat data "
  676. | Product | Rating | Ranking |
  677. |---------+--------+---------|
  678. | p1 | 2.5 | 2 |
  679. | p2 | 2.0 | 3 |
  680. | p3 | 8.0 | 1 |
  681. ")
  682. 2 (concat
  683. "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
  684. "'(remote(data, @I$2..@>$2)) '(remote(data, @I$4..@>$4))))) "
  685. "(/ (apply '+ all) (length all) 1.0)); L :: "
  686. "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))
  687. ;; Shop rating and ranking by average purchase from "#+NAME: data"
  688. (org-test-table-target-expect
  689. (concat data "
  690. | Shop | Rating | Ranking |
  691. |------+---------+---------|
  692. | s1 | replace | replace |
  693. | s2 | replace | replace |
  694. ")
  695. (concat data "
  696. | Shop | Rating | Ranking |
  697. |------+--------+---------|
  698. | s1 | 1.5 | 2 |
  699. | s2 | 6.0 | 1 |
  700. ")
  701. 2 (concat
  702. "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
  703. "'(remote(data, @I$3..@>$3)) '(remote(data, @I$4..@>$4))))) "
  704. "(/ (apply '+ all) (length all) 1.0)); L :: "
  705. "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))))
  706. (ert-deftest test-org-table/org-table-make-reference/mode-string-EL ()
  707. (fset 'f 'org-table-make-reference)
  708. ;; For Lisp formula only
  709. (should (equal "0" (f "0" t nil 'literal)))
  710. (should (equal "z" (f "z" t nil 'literal)))
  711. (should (equal "" (f "" t nil 'literal)))
  712. (should (equal "0 1" (f '("0" "1") t nil 'literal)))
  713. (should (equal "z 1" (f '("z" "1") t nil 'literal)))
  714. (should (equal " 1" (f '("" "1") t nil 'literal)))
  715. (should (equal " " (f '("" "" ) t nil 'literal))))
  716. (ert-deftest test-org-table/org-table-make-reference/mode-string-E ()
  717. (fset 'f 'org-table-make-reference)
  718. ;; For Lisp formula
  719. (should (equal "\"0\"" (f "0" t nil t)))
  720. (should (equal "\"z\"" (f "z" t nil t)))
  721. (should (equal "\"\"" (f "" t nil t)))
  722. (should (equal "\"0\" \"1\"" (f '("0" "1") t nil t)))
  723. (should (equal "\"z\" \"1\"" (f '("z" "1") t nil t)))
  724. (should (equal "\"\" \"1\"" (f '("" "1") t nil t)))
  725. (should (equal "\"\" \"\"" (f '("" "" ) t nil t)))
  726. ;; For Calc formula
  727. (should (equal "(0)" (f "0" t nil nil)))
  728. (should (equal "(z)" (f "z" t nil nil)))
  729. (should (equal "nan" (f "" t nil nil)))
  730. (should (equal "[0,1]" (f '("0" "1") t nil nil)))
  731. (should (equal "[z,1]" (f '("z" "1") t nil nil)))
  732. (should (equal "[nan,1]" (f '("" "1") t nil nil)))
  733. (should (equal "[nan,nan]" (f '("" "" ) t nil nil)))
  734. ;; For Calc formula, special numbers
  735. (should (equal "(nan)" (f "nan" t nil nil)))
  736. (should (equal "(uinf)" (f "uinf" t nil nil)))
  737. (should (equal "(-inf)" (f "-inf" t nil nil)))
  738. (should (equal "(inf)" (f "inf" t nil nil)))
  739. (should (equal "[nan,1]" (f '( "nan" "1") t nil nil)))
  740. (should (equal "[uinf,1]" (f '("uinf" "1") t nil nil)))
  741. (should (equal "[-inf,1]" (f '("-inf" "1") t nil nil)))
  742. (should (equal "[inf,1]" (f '( "inf" "1") t nil nil))))
  743. (ert-deftest test-org-table/org-table-make-reference/mode-string-EN ()
  744. (fset 'f 'org-table-make-reference)
  745. ;; For Lisp formula
  746. (should (equal "0" (f "0" t t t)))
  747. (should (equal "0" (f "z" t t t)))
  748. (should (equal "0" (f "" t t t)))
  749. (should (equal "0 1" (f '("0" "1") t t t)))
  750. (should (equal "0 1" (f '("z" "1") t t t)))
  751. (should (equal "0 1" (f '("" "1") t t t)))
  752. (should (equal "0 0" (f '("" "" ) t t t)))
  753. ;; For Calc formula
  754. (should (equal "(0)" (f "0" t t nil)))
  755. (should (equal "(0)" (f "z" t t nil)))
  756. (should (equal "(0)" (f "" t t nil)))
  757. (should (equal "[0,1]" (f '("0" "1") t t nil)))
  758. (should (equal "[0,1]" (f '("z" "1") t t nil)))
  759. (should (equal "[0,1]" (f '("" "1") t t nil)))
  760. (should (equal "[0,0]" (f '("" "" ) t t nil)))
  761. ;; For Calc formula, special numbers
  762. (should (equal "(0)" (f "nan" t t nil)))
  763. (should (equal "(0)" (f "uinf" t t nil)))
  764. (should (equal "(0)" (f "-inf" t t nil)))
  765. (should (equal "(0)" (f "inf" t t nil)))
  766. (should (equal "[0,1]" (f '( "nan" "1") t t nil)))
  767. (should (equal "[0,1]" (f '("uinf" "1") t t nil)))
  768. (should (equal "[0,1]" (f '("-inf" "1") t t nil)))
  769. (should (equal "[0,1]" (f '( "inf" "1") t t nil))))
  770. (ert-deftest test-org-table/org-table-make-reference/mode-string-L ()
  771. (fset 'f 'org-table-make-reference)
  772. ;; For Lisp formula only
  773. (should (equal "0" (f "0" nil nil 'literal)))
  774. (should (equal "z" (f "z" nil nil 'literal)))
  775. (should (equal "" (f "" nil nil 'literal)))
  776. (should (equal "0 1" (f '("0" "1") nil nil 'literal)))
  777. (should (equal "z 1" (f '("z" "1") nil nil 'literal)))
  778. (should (equal "1" (f '("" "1") nil nil 'literal)))
  779. (should (equal "" (f '("" "" ) nil nil 'literal))))
  780. (ert-deftest test-org-table/org-table-make-reference/mode-string-none ()
  781. (fset 'f 'org-table-make-reference)
  782. ;; For Lisp formula
  783. (should (equal "\"0\"" (f "0" nil nil t)))
  784. (should (equal "\"z\"" (f "z" nil nil t)))
  785. (should (equal "" (f "" nil nil t)))
  786. (should (equal "\"0\" \"1\"" (f '("0" "1") nil nil t)))
  787. (should (equal "\"z\" \"1\"" (f '("z" "1") nil nil t)))
  788. (should (equal "\"1\"" (f '("" "1") nil nil t)))
  789. (should (equal "" (f '("" "" ) nil nil t)))
  790. ;; For Calc formula
  791. (should (equal "(0)" (f "0" nil nil nil)))
  792. (should (equal "(z)" (f "z" nil nil nil)))
  793. (should (equal "(0)" (f "" nil nil nil)))
  794. (should (equal "[0,1]" (f '("0" "1") nil nil nil)))
  795. (should (equal "[z,1]" (f '("z" "1") nil nil nil)))
  796. (should (equal "[1]" (f '("" "1") nil nil nil)))
  797. (should (equal "[]" (f '("" "" ) nil nil nil)))
  798. ;; For Calc formula, special numbers
  799. (should (equal "(nan)" (f "nan" nil nil nil)))
  800. (should (equal "(uinf)" (f "uinf" nil nil nil)))
  801. (should (equal "(-inf)" (f "-inf" nil nil nil)))
  802. (should (equal "(inf)" (f "inf" nil nil nil)))
  803. (should (equal "[nan,1]" (f '( "nan" "1") nil nil nil)))
  804. (should (equal "[uinf,1]" (f '("uinf" "1") nil nil nil)))
  805. (should (equal "[-inf,1]" (f '("-inf" "1") nil nil nil)))
  806. (should (equal "[inf,1]" (f '( "inf" "1") nil nil nil))))
  807. (ert-deftest test-org-table/org-table-make-reference/mode-string-N ()
  808. (fset 'f 'org-table-make-reference)
  809. ;; For Lisp formula
  810. (should (equal "0" (f "0" nil t t)))
  811. (should (equal "0" (f "z" nil t t)))
  812. (should (equal "" (f "" nil t t)))
  813. (should (equal "0 1" (f '("0" "1") nil t t)))
  814. (should (equal "0 1" (f '("z" "1") nil t t)))
  815. (should (equal "1" (f '("" "1") nil t t)))
  816. (should (equal "" (f '("" "" ) nil t t)))
  817. ;; For Calc formula
  818. (should (equal "(0)" (f "0" nil t nil)))
  819. (should (equal "(0)" (f "z" nil t nil)))
  820. (should (equal "(0)" (f "" nil t nil)))
  821. (should (equal "[0,1]" (f '("0" "1") nil t nil)))
  822. (should (equal "[0,1]" (f '("z" "1") nil t nil)))
  823. (should (equal "[1]" (f '("" "1") nil t nil)))
  824. (should (equal "[]" (f '("" "" ) nil t nil)))
  825. ;; For Calc formula, special numbers
  826. (should (equal "(0)" (f "nan" nil t nil)))
  827. (should (equal "(0)" (f "uinf" nil t nil)))
  828. (should (equal "(0)" (f "-inf" nil t nil)))
  829. (should (equal "(0)" (f "inf" nil t nil)))
  830. (should (equal "[0,1]" (f '( "nan" "1") nil t nil)))
  831. (should (equal "[0,1]" (f '("uinf" "1") nil t nil)))
  832. (should (equal "[0,1]" (f '("-inf" "1") nil t nil)))
  833. (should (equal "[0,1]" (f '( "inf" "1") nil t nil))))
  834. (ert-deftest test-org-table/org-table-convert-refs-to-an/1 ()
  835. "Simple reference @1$1."
  836. (should
  837. (string= "A1" (org-table-convert-refs-to-an "@1$1"))))
  838. ;; TODO: Test broken
  839. ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
  840. ;; "Self reference @1$1."
  841. ;; (should
  842. ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
  843. (ert-deftest test-org-table/org-table-convert-refs-to-an/3 ()
  844. "Remote reference."
  845. (should
  846. (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
  847. (ert-deftest test-org-table/org-table-convert-refs-to-rc/1 ()
  848. "Simple reference @1$1."
  849. (should
  850. (string= "@1$1" (org-table-convert-refs-to-rc "A1"))))
  851. (ert-deftest test-org-table/org-table-convert-refs-to-rc/2 ()
  852. "Self reference $0."
  853. (should
  854. (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
  855. ;; TODO: Test Broken
  856. ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
  857. ;; "Remote reference."
  858. ;; (should
  859. ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
  860. (ert-deftest test-org-table/remote-reference-access ()
  861. "Access to remote reference.
  862. See also `test-org-table/copy-field'."
  863. (org-test-table-target-expect
  864. "
  865. #+NAME: table
  866. | | x 42 | |
  867. | replace | replace |
  868. "
  869. "
  870. #+NAME: table
  871. | | x 42 | |
  872. | x 42 | 84 x |
  873. "
  874. 1 (concat "#+TBLFM: "
  875. ;; Copy text without calculation: Use Lisp formula
  876. "$1 = '(identity remote(table, @1$2)) :: "
  877. ;; Do a calculation: Use Calc (or Lisp ) formula
  878. "$2 = 2 * remote(table, @1$2)")))
  879. (ert-deftest test-org-table/remote-reference-indirect ()
  880. "Access to remote reference with indirection of name or ID."
  881. (let ((source-tables "
  882. #+NAME: 2012
  883. | amount |
  884. |--------|
  885. | 1 |
  886. | 2 |
  887. |--------|
  888. | 3 |
  889. #+TBLFM: @>$1 = vsum(@I..@II)
  890. #+NAME: 2013
  891. | amount |
  892. |--------|
  893. | 4 |
  894. | 8 |
  895. |--------|
  896. | 12 |
  897. #+TBLFM: @>$1 = vsum(@I..@II)
  898. "))
  899. ;; Read several remote references from same column
  900. (org-test-table-target-expect
  901. (concat source-tables "
  902. #+NAME: summary
  903. | year | amount |
  904. |-------+---------|
  905. | 2012 | replace |
  906. | 2013 | replace |
  907. |-------+---------|
  908. | total | replace |
  909. ")
  910. (concat source-tables "
  911. #+NAME: summary
  912. | year | amount |
  913. |-------+--------|
  914. | 2012 | 3 |
  915. | 2013 | 12 |
  916. |-------+--------|
  917. | total | 15 |
  918. ")
  919. 1
  920. ;; Calc formula
  921. "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
  922. ;; Lisp formula
  923. (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
  924. "@>$2 = '(+ @I..@II); N"))
  925. ;; Read several remote references from same row
  926. (org-test-table-target-expect
  927. (concat source-tables "
  928. #+NAME: summary
  929. | year | 2012 | 2013 | total |
  930. |--------+---------+---------+---------|
  931. | amount | replace | replace | replace |
  932. ")
  933. (concat source-tables "
  934. #+NAME: summary
  935. | year | 2012 | 2013 | total |
  936. |--------+------+------+-------|
  937. | amount | 3 | 12 | 15 |
  938. ")
  939. 1
  940. ;; Calc formula
  941. "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
  942. ;; Lisp formula
  943. (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
  944. "@2$> = '(+ $<<..$>>); N"))))
  945. (ert-deftest test-org-table/org-at-TBLFM-p ()
  946. (org-test-with-temp-text-in-file
  947. "
  948. | 1 |
  949. | 2 |
  950. #+TBLFM: $2=$1*2
  951. "
  952. (goto-char (point-min))
  953. (forward-line 2)
  954. (should (equal (org-at-TBLFM-p) nil))
  955. (goto-char (point-min))
  956. (forward-line 3)
  957. (should (equal (org-at-TBLFM-p) t))
  958. (goto-char (point-min))
  959. (forward-line 4)
  960. (should (equal (org-at-TBLFM-p) nil))))
  961. (ert-deftest test-org-table/org-table-TBLFM-begin ()
  962. (org-test-with-temp-text-in-file
  963. "
  964. | 1 |
  965. | 2 |
  966. #+TBLFM: $2=$1*2
  967. "
  968. (goto-char (point-min))
  969. (should (equal (org-table-TBLFM-begin)
  970. nil))
  971. (goto-char (point-min))
  972. (forward-line 1)
  973. (should (equal (org-table-TBLFM-begin)
  974. nil))
  975. (goto-char (point-min))
  976. (forward-line 3)
  977. (should (= (org-table-TBLFM-begin)
  978. 14))
  979. (goto-char (point-min))
  980. (forward-line 4)
  981. (should (= (org-table-TBLFM-begin)
  982. 14))
  983. ))
  984. (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
  985. "For multiple #+TBLFM lines."
  986. (org-test-with-temp-text-in-file
  987. "
  988. | 1 |
  989. | 2 |
  990. #+TBLFM: $2=$1*1
  991. #+TBLFM: $2=$1*2
  992. "
  993. (goto-char (point-min))
  994. (should (equal (org-table-TBLFM-begin)
  995. nil))
  996. (goto-char (point-min))
  997. (forward-line 1)
  998. (should (equal (org-table-TBLFM-begin)
  999. nil))
  1000. (goto-char (point-min))
  1001. (forward-line 3)
  1002. (should (= (org-table-TBLFM-begin)
  1003. 14))
  1004. (goto-char (point-min))
  1005. (forward-line 4)
  1006. (should (= (org-table-TBLFM-begin)
  1007. 14))
  1008. (goto-char (point-min))
  1009. (forward-line 5)
  1010. (should (= (org-table-TBLFM-begin)
  1011. 14))
  1012. ))
  1013. (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
  1014. (org-test-with-temp-text-in-file
  1015. "
  1016. | 1 |
  1017. | 2 |
  1018. #+TBLFM: $2=$1*1
  1019. #+TBLFM: $2=$1*2
  1020. | 6 |
  1021. | 7 |
  1022. #+TBLFM: $2=$1*1
  1023. #+TBLFM: $2=$1*2
  1024. "
  1025. (goto-char (point-min))
  1026. (should (equal (org-table-TBLFM-begin)
  1027. nil))
  1028. (goto-char (point-min))
  1029. (forward-line 1)
  1030. (should (equal (org-table-TBLFM-begin)
  1031. nil))
  1032. (goto-char (point-min))
  1033. (forward-line 3)
  1034. (should (= (org-table-TBLFM-begin)
  1035. 14))
  1036. (goto-char (point-min))
  1037. (forward-line 4)
  1038. (should (= (org-table-TBLFM-begin)
  1039. 14))
  1040. (goto-char (point-min))
  1041. (forward-line 5)
  1042. (should (= (org-table-TBLFM-begin)
  1043. 14))
  1044. (goto-char (point-min))
  1045. (forward-line 6)
  1046. (should (= (org-table-TBLFM-begin)
  1047. 14))
  1048. (goto-char (point-min))
  1049. (forward-line 8)
  1050. (should (= (org-table-TBLFM-begin)
  1051. 61))
  1052. (goto-char (point-min))
  1053. (forward-line 9)
  1054. (should (= (org-table-TBLFM-begin)
  1055. 61))
  1056. (goto-char (point-min))
  1057. (forward-line 10)
  1058. (should (= (org-table-TBLFM-begin)
  1059. 61))))
  1060. (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
  1061. (org-test-with-temp-text-in-file
  1062. "
  1063. | 1 | |
  1064. | 2 | |
  1065. #+TBLFM: $2=$1*1
  1066. #+TBLFM: $2=$1*2
  1067. #+TBLFM: $2=$1*3
  1068. "
  1069. (let ((got (progn (goto-char (point-min))
  1070. (forward-line 3)
  1071. (org-table-calc-current-TBLFM)
  1072. (buffer-string)))
  1073. (expect "
  1074. | 1 | 1 |
  1075. | 2 | 2 |
  1076. #+TBLFM: $2=$1*1
  1077. #+TBLFM: $2=$1*2
  1078. #+TBLFM: $2=$1*3
  1079. "))
  1080. (should (string= got
  1081. expect)))
  1082. (let ((got (progn (goto-char (point-min))
  1083. (forward-line 4)
  1084. (org-table-calc-current-TBLFM)
  1085. (buffer-string)))
  1086. (expect "
  1087. | 1 | 2 |
  1088. | 2 | 4 |
  1089. #+TBLFM: $2=$1*1
  1090. #+TBLFM: $2=$1*2
  1091. #+TBLFM: $2=$1*3
  1092. "))
  1093. (should (string= got
  1094. expect)))))
  1095. (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
  1096. "org-table-calc-current-TBLFM should preserve the input as it was."
  1097. (org-test-with-temp-text-in-file
  1098. "
  1099. | 1 | 1 |
  1100. | 2 | 2 |
  1101. #+TBLFM: $2=$1*1
  1102. #+TBLFM: $2=$1*2::$2=$1*2
  1103. #+TBLFM: $2=$1*3
  1104. "
  1105. (let ((expect "
  1106. | 1 | 1 |
  1107. | 2 | 2 |
  1108. #+TBLFM: $2=$1*1
  1109. #+TBLFM: $2=$1*2::$2=$1*2
  1110. #+TBLFM: $2=$1*3
  1111. "))
  1112. (goto-char (point-min))
  1113. (forward-line 4)
  1114. (should-error (org-table-calc-current-TBLFM))
  1115. (setq got (buffer-string))
  1116. (message "%s" got)
  1117. (should (string= got
  1118. expect)))))
  1119. ;;; Radio Tables
  1120. (ert-deftest test-org-table/to-generic ()
  1121. "Test `orgtbl-to-generic' specifications."
  1122. ;; Test :hline parameter.
  1123. (should
  1124. (equal "a\nb"
  1125. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1126. '(:hline nil))))
  1127. (should
  1128. (equal "a\n~\nb"
  1129. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1130. '(:hline "~"))))
  1131. ;; Test :sep parameter.
  1132. (should
  1133. (equal "a!b\nc!d"
  1134. (orgtbl-to-generic
  1135. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1136. '(:sep "!"))))
  1137. ;; Test :hsep parameter.
  1138. (should
  1139. (equal "a!b\nc?d"
  1140. (orgtbl-to-generic
  1141. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1142. '(:sep "?" :hsep "!"))))
  1143. ;; Test :tstart parameter.
  1144. (should
  1145. (equal "<begin>\na"
  1146. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tstart "<begin>"))))
  1147. (should
  1148. (equal "<begin>\na"
  1149. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1150. '(:tstart (lambda () "<begin>")))))
  1151. (should
  1152. (equal "a"
  1153. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1154. '(:tstart "<begin>" :splice t))))
  1155. ;; Test :tend parameter.
  1156. (should
  1157. (equal "a\n<end>"
  1158. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:tend "<end>"))))
  1159. (should
  1160. (equal "a\n<end>"
  1161. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1162. '(:tend (lambda () "<end>")))))
  1163. (should
  1164. (equal "a"
  1165. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1166. '(:tend "<end>" :splice t))))
  1167. ;; Test :lstart parameter.
  1168. (should
  1169. (equal "> a"
  1170. (orgtbl-to-generic
  1171. (org-table-to-lisp "| a |") '(:lstart "> "))))
  1172. (should
  1173. (equal "> a"
  1174. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1175. '(:lstart (lambda () "> ")))))
  1176. ;; Test :llstart parameter.
  1177. (should
  1178. (equal "> a\n>> b"
  1179. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1180. '(:lstart "> " :llstart ">> "))))
  1181. ;; Test :hlstart parameter.
  1182. (should
  1183. (equal "!> a\n> b"
  1184. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1185. '(:lstart "> " :hlstart "!> "))))
  1186. ;; Test :hllstart parameter.
  1187. (should
  1188. (equal "!> a\n!!> b\n> c"
  1189. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1190. '(:lstart "> " :hlstart "!> " :hllstart "!!> "))))
  1191. ;; Test :lend parameter.
  1192. (should
  1193. (equal "a <"
  1194. (orgtbl-to-generic (org-table-to-lisp "| a |") '(:lend " <"))))
  1195. ;; Test :llend parameter.
  1196. (should
  1197. (equal "a <\nb <<"
  1198. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1199. '(:lend " <" :llend " <<"))))
  1200. ;; Test :hlend parameter.
  1201. (should
  1202. (equal "a <!\nb <"
  1203. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1204. '(:lend " <" :hlend " <!"))))
  1205. ;; Test :hllend parameter.
  1206. (should
  1207. (equal "a <!\nb <!!\nc <"
  1208. (orgtbl-to-generic (org-table-to-lisp "| a |\n| b |\n|---|\n| c |")
  1209. '(:lend " <" :hlend " <!" :hllend " <!!"))))
  1210. ;; Test :lfmt parameter.
  1211. (should
  1212. (equal "a!b"
  1213. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1214. '(:lfmt "%s!%s"))))
  1215. (should
  1216. (equal "a+b"
  1217. (orgtbl-to-generic
  1218. (org-table-to-lisp "| a | b |")
  1219. '(:lfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1220. (should
  1221. (equal "a!b"
  1222. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1223. '(:lfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1224. ;; Test :llfmt parameter.
  1225. (should
  1226. (equal "a!b"
  1227. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1228. '(:llfmt "%s!%s"))))
  1229. (should
  1230. (equal "a!b\nc+d"
  1231. (orgtbl-to-generic
  1232. (org-table-to-lisp "| a | b |\n| c | d |")
  1233. '(:lfmt "%s!%s" :llfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1234. (should
  1235. (equal "a!b"
  1236. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1237. '(:llfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1238. ;; Test :hlfmt parameter.
  1239. (should
  1240. (equal "a!b\ncd"
  1241. (orgtbl-to-generic
  1242. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1243. '(:hlfmt "%s!%s"))))
  1244. (should
  1245. (equal "a+b\ncd"
  1246. (orgtbl-to-generic
  1247. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1248. '(:hlfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1249. (should
  1250. (equal "a!b\n>c d<"
  1251. (orgtbl-to-generic
  1252. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1253. '(:hlfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1254. ;; Test :hllfmt parameter.
  1255. (should
  1256. (equal "a!b\ncd"
  1257. (orgtbl-to-generic
  1258. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1259. '(:hllfmt "%s!%s"))))
  1260. (should
  1261. (equal "a+b\ncd"
  1262. (orgtbl-to-generic
  1263. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1264. '(:hllfmt (lambda (c) (concat (car c) "+" (cadr c)))))))
  1265. (should
  1266. (equal "a!b\n>c d<"
  1267. (orgtbl-to-generic
  1268. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1269. '(:hllfmt "%s!%s" :lstart ">" :lend "<" :sep " "))))
  1270. ;; Test :fmt parameter.
  1271. (should
  1272. (equal ">a<\n>b<"
  1273. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1274. '(:fmt ">%s<"))))
  1275. (should
  1276. (equal ">a<b"
  1277. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1278. '(:fmt (1 ">%s<" 2 (lambda (c) c))))))
  1279. (should
  1280. (equal "a b"
  1281. (orgtbl-to-generic (org-table-to-lisp "| a | b |")
  1282. '(:fmt (2 " %s")))))
  1283. (should
  1284. (equal ">a<"
  1285. (orgtbl-to-generic (org-table-to-lisp "| a |")
  1286. '(:fmt (lambda (c) (format ">%s<" c))))))
  1287. ;; Test :hfmt parameter.
  1288. (should
  1289. (equal ">a<\nb"
  1290. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1291. '(:hfmt ">%s<"))))
  1292. (should
  1293. (equal ">a<b\ncd"
  1294. (orgtbl-to-generic
  1295. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1296. '(:hfmt (1 ">%s<" 2 identity)))))
  1297. (should
  1298. (equal "a b\ncd"
  1299. (orgtbl-to-generic
  1300. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |")
  1301. '(:hfmt (2 " %s")))))
  1302. (should
  1303. (equal ">a<\nb"
  1304. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1305. '(:hfmt (lambda (c) (format ">%s<" c))))))
  1306. ;; Test :efmt parameter.
  1307. (should
  1308. (equal "2x10^3"
  1309. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1310. '(:efmt "%sx10^%s"))))
  1311. (should
  1312. (equal "2x10^3"
  1313. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1314. '(:efmt (lambda (m e) (concat m "x10^" e))))))
  1315. (should
  1316. (equal "2x10^3"
  1317. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |")
  1318. '(:efmt (1 "%sx10^%s")))))
  1319. (should
  1320. (equal "2x10^3"
  1321. (orgtbl-to-generic
  1322. (org-table-to-lisp "| 2e3 |")
  1323. '(:efmt (1 (lambda (m e) (format "%sx10^%s" m e)))))))
  1324. (should
  1325. (equal "2e3"
  1326. (orgtbl-to-generic (org-table-to-lisp "| 2e3 |") '(:efmt nil))))
  1327. ;; Test :skip parameter.
  1328. (should
  1329. (equal "cd"
  1330. (orgtbl-to-generic
  1331. (org-table-to-lisp "| \ | <c> |\n| a | b |\n|---+---|\n| c | d |")
  1332. '(:skip 2))))
  1333. ;; Test :skipcols parameter.
  1334. (should
  1335. (equal "a\nc"
  1336. (orgtbl-to-generic
  1337. (org-table-to-lisp "| a | b |\n| c | d |") '(:skipcols (2)))))
  1338. (should
  1339. (equal "a\nc"
  1340. (orgtbl-to-generic
  1341. (org-table-to-lisp
  1342. "| / | <c> | <c> |\n| # | a | b |\n|---+---+---|\n| | c | d |")
  1343. '(:skipcols (2)))))
  1344. ;; Test :raw parameter.
  1345. (when (featurep 'ox-latex)
  1346. (should
  1347. (org-string-match-p
  1348. "/a/"
  1349. (orgtbl-to-generic (org-table-to-lisp "| /a/ | b |")
  1350. '(:backend latex :raw t)))))
  1351. ;; Hooks are ignored.
  1352. (should
  1353. (equal
  1354. "a\nb"
  1355. (let* ((fun-list (list (lambda (backend) (search-forward "a") (insert "hook"))))
  1356. (org-export-before-parsing-hook fun-list)
  1357. (org-export-before-processing-hook fun-list))
  1358. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1359. '(:hline nil)))))
  1360. ;; User-defined export filters are ignored.
  1361. (should
  1362. (equal
  1363. "a\nb"
  1364. (let ((org-export-filter-table-cell-functions (list (lambda (c b i) "filter"))))
  1365. (orgtbl-to-generic (org-table-to-lisp "| a |\n|---|\n| b |")
  1366. '(:hline nil)))))
  1367. ;; Macros, even if unknown, are returned as-is.
  1368. (should
  1369. (equal "{{{macro}}}"
  1370. (orgtbl-to-generic (org-table-to-lisp "| {{{macro}}} |") nil))))
  1371. (ert-deftest test-org-table/to-latex ()
  1372. "Test `orgtbl-to-latex' specifications."
  1373. (should
  1374. (equal "\\begin{tabular}{l}\na\\\\\n\\end{tabular}"
  1375. (orgtbl-to-latex (org-table-to-lisp "| a |") nil)))
  1376. ;; Test :environment parameter.
  1377. (should
  1378. (equal "\\begin{tabularx}{l}\na\\\\\n\\end{tabularx}"
  1379. (orgtbl-to-latex (org-table-to-lisp "| a |")
  1380. '(:environment "tabularx"))))
  1381. ;; Test :booktabs parameter.
  1382. (should
  1383. (org-string-match-p
  1384. "\\toprule" (orgtbl-to-latex (org-table-to-lisp "| a |") '(:booktabs t))))
  1385. ;; Handle LaTeX snippets.
  1386. (should
  1387. (equal "\\begin{tabular}{l}\n\\(x\\)\\\\\n\\end{tabular}"
  1388. (orgtbl-to-latex (org-table-to-lisp "| $x$ |") nil)))
  1389. ;; Test pseudo objects and :raw parameter.
  1390. (should
  1391. (org-string-match-p
  1392. "\\$x\\$" (orgtbl-to-latex (org-table-to-lisp "| $x$ |") '(:raw t)))))
  1393. (ert-deftest test-org-table/to-html ()
  1394. "Test `orgtbl-to-html' specifications."
  1395. (should
  1396. (equal (orgtbl-to-html (org-table-to-lisp "| a |") nil)
  1397. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">
  1398. <colgroup>
  1399. <col class=\"org-left\" />
  1400. </colgroup>
  1401. <tbody>
  1402. <tr>
  1403. <td class=\"org-left\">a</td>
  1404. </tr>
  1405. </tbody>
  1406. </table>"))
  1407. ;; Test :attributes parameter.
  1408. (should
  1409. (org-string-match-p
  1410. "<table>"
  1411. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes nil))))
  1412. (should
  1413. (org-string-match-p
  1414. "<table border=\"2\">"
  1415. (orgtbl-to-html (org-table-to-lisp "| a |") '(:attributes (:border "2"))))))
  1416. (ert-deftest test-org-table/to-texinfo ()
  1417. "Test `orgtbl-to-texinfo' specifications."
  1418. (should
  1419. (equal "@multitable {a}\n@item a\n@end multitable"
  1420. (orgtbl-to-texinfo (org-table-to-lisp "| a |") nil)))
  1421. ;; Test :columns parameter.
  1422. (should
  1423. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1424. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1425. '(:columns ".4 .6"))))
  1426. (should
  1427. (equal "@multitable @columnfractions .4 .6\n@item a\n@tab b\n@end multitable"
  1428. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1429. '(:columns "@columnfractions .4 .6"))))
  1430. (should
  1431. (equal "@multitable {xxx} {xx}\n@item a\n@tab b\n@end multitable"
  1432. (orgtbl-to-texinfo (org-table-to-lisp "| a | b |")
  1433. '(:columns "{xxx} {xx}")))))
  1434. (ert-deftest test-org-table/to-orgtbl ()
  1435. "Test `orgtbl-to-orgtbl' specifications."
  1436. (should
  1437. (equal "| a | b |\n|---+---|\n| c | d |"
  1438. (orgtbl-to-orgtbl
  1439. (org-table-to-lisp "| a | b |\n|---+---|\n| c | d |") nil))))
  1440. (ert-deftest test-org-table/to-unicode ()
  1441. "Test `orgtbl-to-unicode' specifications."
  1442. (should
  1443. (equal "━━━\n a \n━━━"
  1444. (orgtbl-to-unicode (org-table-to-lisp "| a |") nil)))
  1445. ;; Test :narrow parameter.
  1446. (should
  1447. (equal "━━━━\n => \n━━━━"
  1448. (orgtbl-to-unicode (org-table-to-lisp "| <2> |\n| xxx |")
  1449. '(:narrow t)))))
  1450. (ert-deftest test-org-table/send-region ()
  1451. "Test `orgtbl-send-table' specifications."
  1452. ;; Error when not at a table.
  1453. (should-error
  1454. (org-test-with-temp-text "Paragraph"
  1455. (orgtbl-send-table)))
  1456. ;; Error when destination is missing.
  1457. (should-error
  1458. (org-test-with-temp-text "#+ORGTBL: SEND\n<point>| a |"
  1459. (orgtbl-send-table)))
  1460. ;; Error when transformation function is not specified.
  1461. (should-error
  1462. (org-test-with-temp-text "
  1463. # BEGIN RECEIVE ORGTBL table
  1464. # END RECEIVE ORGTBL table
  1465. #+ORGTBL: SEND table
  1466. <point>| a |"
  1467. (orgtbl-send-table)))
  1468. ;; Standard test.
  1469. (should
  1470. (equal "| a |\n|---|\n| b |\n"
  1471. (org-test-with-temp-text "
  1472. # BEGIN RECEIVE ORGTBL table
  1473. # END RECEIVE ORGTBL table
  1474. #+ORGTBL: SEND table orgtbl-to-orgtbl :hlines nil
  1475. <point>| a |\n|---|\n| b |"
  1476. (orgtbl-send-table)
  1477. (goto-char (point-min))
  1478. (buffer-substring-no-properties
  1479. (search-forward "# BEGIN RECEIVE ORGTBL table\n")
  1480. (progn (search-forward "# END RECEIVE ORGTBL table")
  1481. (match-beginning 0)))))))
  1482. ;;; Sorting
  1483. (ert-deftest test-org-table/sort-lines ()
  1484. "Test `org-table-sort-lines' specifications."
  1485. ;; Sort numerically.
  1486. (should
  1487. (equal "| 1 | 2 |\n| 2 | 4 |\n| 5 | 3 |\n"
  1488. (org-test-with-temp-text "| <point>1 | 2 |\n| 5 | 3 |\n| 2 | 4 |\n"
  1489. (org-table-sort-lines nil ?n)
  1490. (buffer-string))))
  1491. (should
  1492. (equal "| 5 | 3 |\n| 2 | 4 |\n| 1 | 2 |\n"
  1493. (org-test-with-temp-text "| <point>1 | 2 |\n| 5 | 3 |\n| 2 | 4 |\n"
  1494. (org-table-sort-lines nil ?N)
  1495. (buffer-string))))
  1496. ;; Sort alphabetically.
  1497. (should
  1498. (equal "| a | x |\n| b | 4 |\n| c | 3 |\n"
  1499. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| b | 4 |\n"
  1500. (org-table-sort-lines nil ?a)
  1501. (buffer-string))))
  1502. (should
  1503. (equal "| c | 3 |\n| b | 4 |\n| a | x |\n"
  1504. (org-test-with-temp-text "| <point>a | x |\n| c | 3 |\n| b | 4 |\n"
  1505. (org-table-sort-lines nil ?A)
  1506. (buffer-string))))
  1507. ;; Sort alphabetically with case.
  1508. (should
  1509. (equal "| C |\n| a |\n| b |\n"
  1510. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1511. (org-table-sort-lines t ?a)
  1512. (buffer-string))))
  1513. (should
  1514. (equal "| b |\n| a |\n| C |\n"
  1515. (org-test-with-temp-text "| <point>a |\n| C |\n| b |\n"
  1516. (org-table-sort-lines nil ?A)
  1517. (buffer-string))))
  1518. ;; Sort by time (timestamps)
  1519. (should
  1520. (equal
  1521. "| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n| <2014-03-04 tue.> |\n"
  1522. (org-test-with-temp-text
  1523. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1524. (org-table-sort-lines nil ?t)
  1525. (buffer-string))))
  1526. (should
  1527. (equal
  1528. "| <2014-03-04 tue.> |\n| <2012-03-29 thu.> |\n| <2008-08-08 sat.> |\n"
  1529. (org-test-with-temp-text
  1530. "| <2014-03-04 tue.> |\n| <2008-08-08 sat.> |\n| <2012-03-29 thu.> |\n"
  1531. (org-table-sort-lines nil ?T)
  1532. (buffer-string))))
  1533. ;; Sort by time (HH:MM values)
  1534. (should
  1535. (equal "| 1:00 |\n| 14:00 |\n| 17:00 |\n"
  1536. (org-test-with-temp-text "| 14:00 |\n| 17:00 |\n| 1:00 |\n"
  1537. (org-table-sort-lines nil ?t)
  1538. (buffer-string))))
  1539. (should
  1540. (equal "| 17:00 |\n| 14:00 |\n| 1:00 |\n"
  1541. (org-test-with-temp-text "| 14:00 |\n| 17:00 |\n| 1:00 |\n"
  1542. (org-table-sort-lines nil ?T)
  1543. (buffer-string))))
  1544. ;; Sort with custom functions.
  1545. (should
  1546. (equal "| 22 |\n| 15 |\n| 18 |\n"
  1547. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1548. (org-table-sort-lines nil ?f
  1549. (lambda (s) (% (string-to-number s) 10))
  1550. #'<)
  1551. (buffer-string))))
  1552. (should
  1553. (equal "| 18 |\n| 15 |\n| 22 |\n"
  1554. (org-test-with-temp-text "| 15 |\n| 22 |\n| 18 |\n"
  1555. (org-table-sort-lines nil ?F
  1556. (lambda (s) (% (string-to-number s) 10))
  1557. #'<)
  1558. (buffer-string))))
  1559. ;; Sort according to current column.
  1560. (should
  1561. (equal "| 1 | 2 |\n| 7 | 3 |\n| 5 | 4 |\n"
  1562. (org-test-with-temp-text "| 1 | <point>2 |\n| 5 | 4 |\n| 7 | 3 |\n"
  1563. (org-table-sort-lines nil ?n)
  1564. (buffer-string))))
  1565. ;; Sort between horizontal separators if possible.
  1566. (should
  1567. (equal
  1568. "| 9 | 8 |\n|---+---|\n| 5 | 3 |\n| 7 | 4 |\n|---+---|\n| 1 | 2 |\n"
  1569. (org-test-with-temp-text
  1570. "| 9 | 8 |\n|---+---|\n| <point>7 | 4 |\n| 5 | 3 |\n|---+---|\n| 1 | 2 |\n"
  1571. (org-table-sort-lines nil ?n)
  1572. (buffer-string)))))
  1573. ;;; Formulas
  1574. (ert-deftest test-org-table/eval-formula ()
  1575. "Test `org-table-eval-formula' specifications."
  1576. ;; Error when not on a table field.
  1577. (should-error
  1578. (org-test-with-temp-text "Text"
  1579. (org-table-eval-formula)))
  1580. (should-error
  1581. (org-test-with-temp-text "| a |\n|---|<point>"
  1582. (org-table-eval-formula)))
  1583. (should-error
  1584. (org-test-with-temp-text "| a |\n#+TBLFM:<point>"
  1585. (org-table-eval-formula)))
  1586. ;; Handle @<, @>, $< and $>.
  1587. (should
  1588. (equal "| 1 |\n| 1 |"
  1589. (org-test-with-temp-text "| <point> |\n| 1 |"
  1590. (org-table-eval-formula nil "@>" nil nil t)
  1591. (buffer-string))))
  1592. (should
  1593. (equal "| 1 |\n| 1 |"
  1594. (org-test-with-temp-text "| 1 |\n| <point> |"
  1595. (org-table-eval-formula nil "@<" nil nil t)
  1596. (buffer-string))))
  1597. (should
  1598. (equal "| 1 | 1 |"
  1599. (org-test-with-temp-text "| <point> | 1 |"
  1600. (org-table-eval-formula nil "$>" nil nil t)
  1601. (buffer-string))))
  1602. (should
  1603. (equal "| 1 | 1 |"
  1604. (org-test-with-temp-text "| 1 | <point> |"
  1605. (org-table-eval-formula nil "$<" nil nil t)
  1606. (buffer-string)))))
  1607. (ert-deftest test-org-table/field-formula-outside-table ()
  1608. "If `org-table-formula-create-columns' is nil, then a formula
  1609. that references an out-of-bounds column should do nothing. If it
  1610. is t, then new columns should be added as needed"
  1611. (let ((org-table-formula-create-columns nil))
  1612. (should-error
  1613. (org-test-table-target-expect
  1614. "
  1615. | 2 |
  1616. | 4 |
  1617. | 8 |
  1618. "
  1619. "
  1620. | 2 |
  1621. | 4 |
  1622. | 8 |
  1623. "
  1624. 1
  1625. "#+TBLFM: @1$2=5")
  1626. :type (list 'error 'user-error)))
  1627. (let ((org-table-formula-create-columns t))
  1628. ;; make sure field formulas work
  1629. (org-test-table-target-expect
  1630. "
  1631. | 2 |
  1632. | 4 |
  1633. | 8 |
  1634. "
  1635. "
  1636. | 2 | 5 |
  1637. | 4 | |
  1638. | 8 | |
  1639. "
  1640. 1
  1641. "#+TBLFM: @1$2=5")
  1642. ;; and make sure column formulas work too
  1643. (org-test-table-target-expect
  1644. "
  1645. | 2 |
  1646. | 4 |
  1647. | 8 |
  1648. "
  1649. "
  1650. | 2 | | 15 |
  1651. | 4 | | 15 |
  1652. | 8 | | 15 |
  1653. "
  1654. 1
  1655. "#+TBLFM: $3=15")))
  1656. (ert-deftest test-org-table/duration ()
  1657. "Test durations in table formulas."
  1658. ;; Durations in cells.
  1659. (should
  1660. (string-match "| 2:12 | 1:47 | 03:59:00 |"
  1661. (org-test-with-temp-text "
  1662. | 2:12 | 1:47 | |
  1663. <point>#+TBLFM: @1$3=$1+$2;T"
  1664. (org-table-calc-current-TBLFM)
  1665. (buffer-string))))
  1666. (should
  1667. (string-match "| 3:02:20 | -2:07:00 | 0.92 |"
  1668. (org-test-with-temp-text "
  1669. | 3:02:20 | -2:07:00 | |
  1670. <point>#+TBLFM: @1$3=$1+$2;t"
  1671. (org-table-calc-current-TBLFM)
  1672. (buffer-string))))
  1673. ;; Durations set through properties.
  1674. (should
  1675. (string-match "| 16:00:00 |"
  1676. (org-test-with-temp-text "* H
  1677. :PROPERTIES:
  1678. :time_constant: 08:00:00
  1679. :END:
  1680. | |
  1681. <point>#+TBLFM: $1=2*$PROP_time_constant;T"
  1682. (org-table-calc-current-TBLFM)
  1683. (buffer-string))))
  1684. (should
  1685. (string-match "| 16.00 |"
  1686. (org-test-with-temp-text "* H
  1687. :PROPERTIES:
  1688. :time_constant: 08:00:00
  1689. :END:
  1690. | |
  1691. <point>#+TBLFM: $1=2*$PROP_time_constant;t"
  1692. (org-table-calc-current-TBLFM)
  1693. (buffer-string)))))
  1694. (ert-deftest test-org-table/end-on-hline ()
  1695. "Test with a table ending on a hline."
  1696. (should
  1697. (equal
  1698. (org-test-with-temp-text
  1699. "
  1700. | 1 | 2 | 3 |
  1701. | 4 | 5 | 6 |
  1702. | | | |
  1703. |---+---+---|
  1704. <point>#+TBLFM: @3$2..@3$>=vsum(@1..@2)"
  1705. (org-table-calc-current-TBLFM)
  1706. (buffer-string))
  1707. "
  1708. | 1 | 2 | 3 |
  1709. | 4 | 5 | 6 |
  1710. | | 7 | 9 |
  1711. |---+---+---|
  1712. #+TBLFM: @3$2..@3$>=vsum(@1..@2)")))
  1713. (ert-deftest test-org-table/named-field ()
  1714. "Test formula with a named field."
  1715. (should
  1716. (org-string-match-p
  1717. "| +| +1 +|"
  1718. (org-test-with-temp-text "
  1719. | | |
  1720. | ^ | name |
  1721. <point>#+TBLFM: $name=1"
  1722. (org-table-calc-current-TBLFM)
  1723. (buffer-string))))
  1724. (should
  1725. (org-string-match-p
  1726. "| +| +1 +|"
  1727. (org-test-with-temp-text "
  1728. | _ | name |
  1729. | | |
  1730. <point>#+TBLFM: $name=1"
  1731. (org-table-calc-current-TBLFM)
  1732. (buffer-string)))))
  1733. (ert-deftest test-org-table/named-column ()
  1734. "Test formula with a named field."
  1735. (should
  1736. (org-string-match-p
  1737. "| +| +1 +| +1 +|"
  1738. (org-test-with-temp-text "
  1739. | ! | name | |
  1740. | | 1 | |
  1741. <point>#+TBLFM: @2$3=$name"
  1742. (org-table-calc-current-TBLFM)
  1743. (buffer-string)))))
  1744. (ert-deftest test-org-table/tab-indent ()
  1745. "Test named fields with tab indentation."
  1746. (should
  1747. (org-string-match-p
  1748. "| # | 111 |"
  1749. (org-test-with-temp-text
  1750. "
  1751. | ! | sum | | a | b | c |
  1752. |---+------+------+---+----+-----|
  1753. | # | 1011 | 1000 | 1 | 10 | 100 |
  1754. <point>#+TBLFM: $2=$a+$b+$c
  1755. "
  1756. (org-table-calc-current-TBLFM)
  1757. (buffer-string)))))
  1758. (ert-deftest test-org-table/first-rc ()
  1759. "Test \"$<\" and \"@<\" constructs in formulas."
  1760. (should
  1761. (org-string-match-p
  1762. "| 1 | 2 |"
  1763. (org-test-with-temp-text
  1764. "| | 2 |
  1765. <point>#+TBLFM: $<=1"
  1766. (org-table-calc-current-TBLFM)
  1767. (buffer-string))))
  1768. (should
  1769. (org-string-match-p
  1770. "| 2 |\n| 2 |"
  1771. (org-test-with-temp-text
  1772. "| 2 |\n| |
  1773. <point>#+TBLFM: @2$1=@<"
  1774. (org-table-calc-current-TBLFM)
  1775. (buffer-string)))))
  1776. (ert-deftest test-org-table/last-rc ()
  1777. "Test \"$>\" and \"@>\" constructs in formulas."
  1778. (should
  1779. (org-string-match-p
  1780. "| 2 | 1 |"
  1781. (org-test-with-temp-text
  1782. "| 2 | |\n<point>#+TBLFM: $>=1"
  1783. (org-table-calc-current-TBLFM)
  1784. (buffer-string))))
  1785. (should
  1786. (org-string-match-p
  1787. "| 2 |\n| 2 |"
  1788. (org-test-with-temp-text
  1789. "| 2 |\n| |\n<point>#+TBLFM: @>$1=@<"
  1790. (org-table-calc-current-TBLFM)
  1791. (buffer-string)))))
  1792. (ert-deftest test-org-table/orgtbl-ascii-draw ()
  1793. "Test `orgtbl-ascii-draw'."
  1794. ;; First value: Make sure that an integer input value is converted to a
  1795. ;; float before division. Further values: Show some float input value
  1796. ;; ranges corresponding to the same bar width.
  1797. (should
  1798. (equal
  1799. (org-test-with-temp-text
  1800. "
  1801. | Value | <l> |
  1802. |----------+---------|
  1803. | 19 | replace |
  1804. |----------+---------|
  1805. | -0.50001 | replace |
  1806. | -0.49999 | replace |
  1807. | 0.49999 | replace |
  1808. | 0.50001 | replace |
  1809. | 1.49999 | replace |
  1810. | 22.50001 | replace |
  1811. | 23.49999 | replace |
  1812. | 23.50001 | replace |
  1813. | 24.49999 | replace |
  1814. | 24.50001 | replace |
  1815. <point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"
  1816. (org-table-calc-current-TBLFM)
  1817. (buffer-string))
  1818. "
  1819. | Value | <l> |
  1820. |----------+-----------|
  1821. | 19 | 883 |
  1822. |----------+-----------|
  1823. | -0.50001 | too small |
  1824. | -0.49999 | |
  1825. | 0.49999 | |
  1826. | 0.50001 | 1 |
  1827. | 1.49999 | 1 |
  1828. | 22.50001 | 887 |
  1829. | 23.49999 | 887 |
  1830. | 23.50001 | 888 |
  1831. | 24.49999 | 888 |
  1832. | 24.50001 | too large |
  1833. #+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"))
  1834. ;; Draw bars with a bullet. The bullet does not count in the parameter
  1835. ;; WIDTH of `orgtbl-ascii-draw'.
  1836. (should
  1837. (equal
  1838. (org-test-with-temp-text
  1839. "
  1840. | -1 | replace |
  1841. | 0 | replace |
  1842. | 1 | replace |
  1843. | 2 | replace |
  1844. | 3 | replace |
  1845. | 4 | replace |
  1846. <point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 3 3 \"$-\")"
  1847. (org-table-calc-current-TBLFM)
  1848. (buffer-string))
  1849. "
  1850. | -1 | too small |
  1851. | 0 | $ |
  1852. | 1 | -$ |
  1853. | 2 | --$ |
  1854. | 3 | ---$ |
  1855. | 4 | too large |
  1856. #+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 3 3 \"$-\")")))
  1857. (provide 'test-org-table)
  1858. ;;; test-org-table.el ends here