test-org-table.el 82 KB

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