test-org-table.el 62 KB

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