org-gnus.el: silent compiler
[org-mode/org-kjn.git] / testing / lisp / test-org-table.el
blob40101bed5bf4fd5fc01643c0f55c13492282fc8b
1 ;;; test-org-table.el --- tests for org-table.el
3 ;; Copyright (c) David Maus
4 ;; Authors: David Maus, Michael Brand
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;;; Comments:
23 ;; Template test file for Org-mode tests. Many tests are also a howto
24 ;; example collection as a user documentation, more or less all those
25 ;; using `org-test-table-target-expect'. See also the doc string of
26 ;; `org-test-table-target-expect'.
28 ;;; Code:
30 (require 'org-table) ; `org-table-make-reference'
32 (ert-deftest test-org-table/simple-formula/no-grouping/no-title-row ()
33 "Simple sum without grouping rows, without title row."
34 (org-test-table-target-expect
36 | 2 |
37 | 4 |
38 | 8 |
39 | replace |
42 | 2 |
43 | 4 |
44 | 8 |
45 | 14 |
48 ;; Calc formula
49 "#+TBLFM: @>$1 = vsum(@<..@>>)"
50 ;; Lisp formula
51 "#+TBLFM: @>$1 = '(+ @<..@>>); N"))
53 (ert-deftest test-org-table/simple-formula/no-grouping/with-title-row ()
54 "Simple sum without grouping rows, with title row."
55 (org-test-table-target-expect
57 | foo |
58 |---------|
59 | 2 |
60 | 4 |
61 | 8 |
62 | replace |
65 | foo |
66 |-----|
67 | 2 |
68 | 4 |
69 | 8 |
70 | 14 |
73 ;; Calc formula
74 "#+TBLFM: @>$1 = vsum(@I..@>>)"
75 ;; Lisp formula
76 "#+TBLFM: @>$1 = '(+ @I..@>>); N"))
78 (ert-deftest test-org-table/simple-formula/with-grouping/no-title-row ()
79 "Simple sum with grouping rows, how not to do."
80 ;; The first example has a problem, see the second example in this
81 ;; ert-deftest.
82 (org-test-table-target-expect
84 | 2 |
85 | 4 |
86 | 8 |
87 |---------|
88 | replace |
91 | 2 |
92 | 4 |
93 | 8 |
94 |----|
95 | 14 |
98 ;; Calc formula
99 "#+TBLFM: $1 = vsum(@<..@>>)"
100 ;; Lisp formula
101 "#+TBLFM: $1 = '(+ @<..@>>); N")
103 ;; The problem is that the first three rows with the summands are
104 ;; considered the header and therefore column formulas are not
105 ;; applied on them as shown below. Also export behaves unexpected.
106 ;; See next ert-deftest how to group rows right.
107 (org-test-table-target-expect
109 | 2 | replace |
110 | 4 | replace |
111 | 8 | replace |
112 |---------+---------|
113 | replace | replace |
116 | 2 | replace |
117 | 4 | replace |
118 | 8 | replace |
119 |----+---------|
120 | 14 | 28 |
123 ;; Calc formula
124 "#+TBLFM: @>$1 = vsum(@<..@>>) :: $2 = 2 * $1"
125 ;; Lisp formula
126 "#+TBLFM: @>$1 = '(+ @<..@>>); N :: $2 = '(* 2 $1); N"))
128 (ert-deftest test-org-table/simple-formula/with-grouping/with-title-row ()
129 "Simple sum with grouping rows, how to do it right."
130 ;; Always add a top row with the column names separated by hline to
131 ;; get the desired header when you want to group rows.
132 (org-test-table-target-expect
134 | foo | bar |
135 |---------+---------|
136 | 2 | replace |
137 | 4 | replace |
138 | 8 | replace |
139 |---------+---------|
140 | replace | replace |
143 | foo | bar |
144 |-----+-----|
145 | 2 | 4 |
146 | 4 | 8 |
147 | 8 | 16 |
148 |-----+-----|
149 | 14 | 28 |
152 ;; Calc formula
153 "#+TBLFM: @>$1 = vsum(@I..@>>) :: $2 = 2 * $1"
154 ;; Lisp formula
155 "#+TBLFM: @>$1 = '(+ @I..@>>); N :: $2 = '(* 2 $1); N"))
157 (ert-deftest test-org-table/align ()
158 "Align columns within Org buffer, depends on `org-table-number-regexp'."
159 (org-test-table-target-expect "
160 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
161 | ab | 12 | 12.2 | 2.4e-08 | 2x10^12 | 4.034+-0.02 | 2.7(10) | >3.5 |
162 | ab | ab | ab | ab | ab | ab | ab | ab |
164 (org-test-table-target-expect "
165 | 0 | 0 | 0 | 0 | 0 | 0 |
166 | <-0x0ab.cf | >-36#0vw.yz | nan | uinf | -inf | inf |
167 | ab | ab | ab | ab | ab | ab |
170 (defconst references/target-normal "
171 | 0 | 1 | replace | replace | replace | replace | replace | replace |
172 | z | 1 | replace | replace | replace | replace | replace | replace |
173 | | 1 | replace | replace | replace | replace | replace | replace |
174 | | | replace | replace | replace | replace | replace | replace |
176 "Normal numbers and non-numbers for Lisp and Calc formula.")
178 (defconst references/target-special "
179 | nan | 1 | replace | replace | replace | replace | replace | replace |
180 | uinf | 1 | replace | replace | replace | replace | replace | replace |
181 | -inf | 1 | replace | replace | replace | replace | replace | replace |
182 | inf | 1 | replace | replace | replace | replace | replace | replace |
184 "Special numbers for Calc formula.")
186 (ert-deftest test-org-table/references/mode-string-EL ()
187 "Basic: Assign field reference, sum of field references, sum
188 and len of simple range reference (no row) and complex range
189 reference (with row). Mode string EL."
190 ;; Empty fields are kept during parsing field but lost as list
191 ;; elements within Lisp formula syntactically when used literally
192 ;; and not enclosed with " within fields, see last columns with len.
193 (org-test-table-target-expect
194 references/target-normal
195 ;; All the #ERROR show that for Lisp calculations N has to be used.
197 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
198 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
199 | | 1 | | 1 | 1 | 1 | 1 | 1 |
200 | | | | 0 | 0 | 0 | 0 | 0 |
202 1 (concat
203 "#+TBLFM: $3 = '(identity \"$1\"); EL :: $4 = '(+ $1 $2); EL :: "
204 "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
205 "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL"))
207 ;; Empty fields are kept during parsing field _and_ as list elements
208 ;; within Lisp formula syntactically even when used literally when
209 ;; enclosed with " within fields, see last columns with len.
210 (org-test-table-target-expect
212 | \"0\" | \"1\" | repl | repl | repl | repl | repl | repl |
213 | \"z\" | \"1\" | repl | repl | repl | repl | repl | repl |
214 | \"\" | \"1\" | repl | repl | repl | repl | repl | repl |
215 | \"\" | \"\" | repl | repl | repl | repl | repl | repl |
218 | \"0\" | \"1\" | \"0\" | 1 | #ERROR | #ERROR | 2 | 2 |
219 | \"z\" | \"1\" | \"z\" | 1 | #ERROR | #ERROR | 2 | 2 |
220 | \"\" | \"1\" | \"\" | 1 | #ERROR | #ERROR | 2 | 2 |
221 | \"\" | \"\" | \"\" | 0 | #ERROR | #ERROR | 2 | 2 |
223 1 (concat
224 "#+TBLFM: $3 = '(concat \"\\\"\" $1 \"\\\"\"); EL :: "
225 "$4 = '(+ (string-to-number $1) (string-to-number $2)); EL :: "
226 "$5 = '(+ $1..$2); EL :: $6 = '(+ @0$1..@0$2); EL :: "
227 "$7 = '(length '($1..$2)); EL :: $8 = '(length '(@0$1..@0$2)); EL")))
229 (ert-deftest test-org-table/references/mode-string-E ()
230 "Basic: Assign field reference, sum of field references, sum
231 and len of simple range reference (no row) and complex range
232 reference (with row). Mode string E."
233 (let ((lisp
234 (concat
235 "#+TBLFM: $3 = '(identity $1); E :: $4 = '(+ $1 $2); E :: "
236 "$5 = '(+ $1..$2); E :: $6 = '(+ @0$1..@0$2); E :: "
237 "$7 = '(length '($1..$2)); E :: $8 = '(length '(@0$1..@0$2)); E"))
238 (calc
239 (concat
240 "#+TBLFM: $3 = $1; E :: $4 = $1 + $2; E :: "
241 "$5 = vsum($1..$2); E :: $6 = vsum(@0$1..@0$2); E :: "
242 "$7 = vlen($1..$2); E :: $8 = vlen(@0$1..@0$2); E")))
243 (org-test-table-target-expect
244 references/target-normal
245 ;; All the #ERROR show that for Lisp calculations N has to be used.
247 | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
248 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
249 | | 1 | | #ERROR | #ERROR | #ERROR | 2 | 2 |
250 | | | | #ERROR | #ERROR | #ERROR | 2 | 2 |
252 1 lisp)
253 (org-test-table-target-expect
254 references/target-normal
256 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
257 | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
258 | | 1 | nan | nan | nan | nan | 2 | 2 |
259 | | | nan | nan | nan | nan | 2 | 2 |
261 1 calc)
262 (org-test-table-target-expect
263 references/target-special
265 | nan | 1 | nan | nan | nan | nan | 2 | 2 |
266 | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
267 | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
268 | inf | 1 | inf | inf | inf | inf | 2 | 2 |
270 1 calc)))
272 (ert-deftest test-org-table/references/mode-string-EN ()
273 "Basic: Assign field reference, sum of field references, sum
274 and len of simple range reference (no row) and complex range
275 reference (with row). Mode string EN."
276 (let ((lisp (concat
277 "#+TBLFM: $3 = '(identity $1); EN :: $4 = '(+ $1 $2); EN :: "
278 "$5 = '(+ $1..$2); EN :: $6 = '(+ @0$1..@0$2); EN :: "
279 "$7 = '(length '($1..$2)); EN :: "
280 "$8 = '(length '(@0$1..@0$2)); EN"))
281 (calc (concat
282 "#+TBLFM: $3 = $1; EN :: $4 = $1 + $2; EN :: "
283 "$5 = vsum($1..$2); EN :: $6 = vsum(@0$1..@0$2); EN :: "
284 "$7 = vlen($1..$2); EN :: $8 = vlen(@0$1..@0$2); EN")))
285 (org-test-table-target-expect
286 references/target-normal
288 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
289 | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
290 | | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
291 | | | 0 | 0 | 0 | 0 | 2 | 2 |
293 1 lisp calc)
294 (org-test-table-target-expect
295 references/target-special
297 | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
298 | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
299 | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
300 | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
302 1 calc)))
304 (ert-deftest test-org-table/references/mode-string-L ()
305 "Basic: Assign field reference, sum of field references, sum
306 and len of simple range reference (no row) and complex range
307 reference (with row). Mode string L."
308 (org-test-table-target-expect
309 references/target-normal
310 ;; All the #ERROR show that for Lisp calculations N has to be used.
312 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
313 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
314 | | 1 | | 1 | 1 | 1 | 1 | 1 |
315 | | | | 0 | 0 | 0 | 0 | 0 |
317 1 (concat
318 "#+TBLFM: $3 = '(identity \"$1\"); L :: $4 = '(+ $1 $2); L :: "
319 "$5 = '(+ $1..$2); L :: $6 = '(+ @0$1..@0$2); L :: "
320 "$7 = '(length '($1..$2)); L :: $8 = '(length '(@0$1..@0$2)); L")))
322 (ert-deftest test-org-table/references/mode-string-none ()
323 "Basic: Assign field reference, sum of field references, sum
324 and len of simple range reference (no row) and complex range
325 reference (with row). No mode string."
326 (let ((lisp (concat
327 "#+TBLFM: $3 = '(identity $1) :: $4 = '(+ $1 $2) :: "
328 "$5 = '(+ $1..$2) :: $6 = '(+ @0$1..@0$2) :: "
329 "$7 = '(length '($1..$2)) :: $8 = '(length '(@0$1..@0$2))"))
330 (calc (concat
331 "#+TBLFM: $3 = $1 :: $4 = $1 + $2 :: "
332 "$5 = vsum($1..$2) :: $6 = vsum(@0$1..@0$2) :: "
333 "$7 = vlen($1..$2) :: $8 = vlen(@0$1..@0$2)")))
334 (org-test-table-target-expect
335 references/target-normal
336 ;; All the #ERROR show that for Lisp calculations N has to be used.
338 | 0 | 1 | 0 | #ERROR | #ERROR | #ERROR | 2 | 2 |
339 | z | 1 | z | #ERROR | #ERROR | #ERROR | 2 | 2 |
340 | | 1 | | #ERROR | #ERROR | #ERROR | 1 | 1 |
341 | | | | #ERROR | 0 | 0 | 0 | 0 |
343 1 lisp)
344 (org-test-table-target-expect
345 references/target-normal
347 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
348 | z | 1 | z | z + 1 | z + 1 | z + 1 | 2 | 2 |
349 | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
350 | | | 0 | 0 | 0 | 0 | 0 | 0 |
352 1 calc)
353 (org-test-table-target-expect
354 references/target-special
356 | nan | 1 | nan | nan | nan | nan | 2 | 2 |
357 | uinf | 1 | uinf | uinf | uinf | uinf | 2 | 2 |
358 | -inf | 1 | -inf | -inf | -inf | -inf | 2 | 2 |
359 | inf | 1 | inf | inf | inf | inf | 2 | 2 |
361 1 calc)))
363 (ert-deftest test-org-table/references/mode-string-N ()
364 "Basic: Assign field reference, sum of field references, sum
365 and len of simple range reference (no row) and complex range
366 reference (with row). Mode string N."
367 (let ((lisp
368 (concat
369 "#+TBLFM: $3 = '(identity $1); N :: $4 = '(+ $1 $2); N :: "
370 "$5 = '(+ $1..$2); N :: $6 = '(+ @0$1..@0$2); N :: "
371 "$7 = '(length '($1..$2)); N :: $8 = '(length '(@0$1..@0$2)); N"))
372 (calc
373 (concat
374 "#+TBLFM: $3 = $1; N :: $4 = $1 + $2; N :: "
375 "$5 = vsum($1..$2); N :: $6 = vsum(@0$1..@0$2); N :: "
376 "$7 = vlen($1..$2); N :: $8 = vlen(@0$1..@0$2); N")))
377 (org-test-table-target-expect
378 references/target-normal
380 | 0 | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
381 | z | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
382 | | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
383 | | | 0 | 0 | 0 | 0 | 0 | 0 |
385 1 lisp calc)
386 (org-test-table-target-expect
387 references/target-special
389 | nan | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
390 | uinf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
391 | -inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
392 | inf | 1 | 0 | 1 | 1 | 1 | 2 | 2 |
394 1 calc)))
396 (ert-deftest test-org-table/compare ()
397 "Basic: Compare field references in Calc."
398 (org-test-table-target-expect
400 | | 0 | z | | nan | uinf | -inf | inf |
401 |------+------+------+------+------+------+------+------|
402 | 0 | repl | repl | repl | repl | repl | repl | repl |
403 | z | repl | repl | repl | repl | repl | repl | repl |
404 | | repl | repl | repl | repl | repl | repl | repl |
405 | nan | repl | repl | repl | repl | repl | repl | repl |
406 | uinf | repl | repl | repl | repl | repl | repl | repl |
407 | -inf | repl | repl | repl | repl | repl | repl | repl |
408 | inf | repl | repl | repl | repl | repl | repl | repl |
411 | | 0 | z | | nan | uinf | -inf | inf |
412 |------+---+---+---+-----+------+------+-----|
413 | 0 | x | | | | | | |
414 | z | | x | | | | | |
415 | | | | x | | | | |
416 | nan | | | | x | | | |
417 | uinf | | | | | x | | |
418 | -inf | | | | | | x | |
419 | inf | | | | | | | x |
422 ;; Compare field reference ($1) with field reference (@1)
423 "#+TBLFM: @<<$<<..@>$> = if(\"$1\" == \"@1\", x, string(\"\")); E"
424 ;; Compare field reference ($1) with absolute term
425 (concat "#+TBLFM: "
426 "$2 = if(\"$1\" == \"(0)\" , x, string(\"\")); E :: "
427 "$3 = if(\"$1\" == \"(z)\" , x, string(\"\")); E :: "
428 "$4 = if(\"$1\" == \"nan\" , x, string(\"\")); E :: "
429 "$5 = if(\"$1\" == \"(nan)\" , x, string(\"\")); E :: "
430 "$6 = if(\"$1\" == \"(uinf)\", x, string(\"\")); E :: "
431 "$7 = if(\"$1\" == \"(-inf)\", x, string(\"\")); E :: "
432 "$8 = if(\"$1\" == \"(inf)\" , x, string(\"\")); E"))
434 ;; Check field reference converted from an empty field: Despite this
435 ;; field reference will not end up in a result, Calc evaluates it.
436 ;; Make sure that also then there is no Calc error.
437 (org-test-table-target-expect
439 | 0 | replace |
440 | z | replace |
441 | | replace |
442 | nan | replace |
445 | 0 | 1 |
446 | z | z + 1 |
447 | | |
448 | nan | nan |
450 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"))
452 (ert-deftest test-org-table/empty-field ()
453 "Examples how to deal with empty fields."
454 ;; Test if one field is empty, else do a calculation
455 (org-test-table-target-expect
457 | -1 | replace |
458 | 0 | replace |
459 | | replace |
462 | -1 | 0 |
463 | 0 | 1 |
464 | | |
467 ;; Calc formula
468 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1 + 1); E"
469 ;; Lisp formula
470 "#+TBLFM: $2 = '(if (eq \"$1\" \"\") \"\" (1+ $1)); L")
472 ;; Test if several fields are empty, else do a calculation
473 (org-test-table-target-expect
475 | 1 | 2 | replace |
476 | 4 | | replace |
477 | | 8 | replace |
478 | | | replace |
481 | 1 | 2 | 3 |
482 | 4 | | |
483 | | 8 | |
484 | | | |
487 ;; Calc formula
488 (concat "#+TBLFM: $3 = if(\"$1\" == \"nan\" || \"$2\" == \"nan\", "
489 "string(\"\"), $1 + $2); E")
490 ;; Lisp formula
491 (concat "#+TBLFM: $3 = '(if (or (eq \"$1\" \"\") (eq \"$2\" \"\")) "
492 "\"\" (+ $1 $2)); L"))
494 ;; $2: Use $1 + 0.5 if $1 available, else only reformat $2 if $2 available
495 (org-test-table-target-expect
497 | 1.5 | 0 |
498 | 3.5 | |
499 | | 5 |
500 | | |
503 | 1.5 | 2.0 |
504 | 3.5 | 4.0 |
505 | | 5.0 |
506 | | |
509 ;; Calc formula
510 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
511 "if(\"$2\" == \"nan\", string(\"\"), $2 +.0), $1 + 0.5); E f-1")
512 ;; Lisp formula not implemented yet
515 ;; Empty fields in simple and complex range reference
516 (org-test-table-target-expect
518 | | | | | repl | repl | repl | repl | repl | repl |
519 | | | 5 | 7 | repl | repl | repl | repl | repl | repl |
520 | 1 | 3 | 5 | 7 | repl | repl | repl | repl | repl | repl |
523 | | | | | | | | | 0 | 0 |
524 | | | 5 | 7 | | | 6 | 6 | 3 | 3 |
525 | 1 | 3 | 5 | 7 | 4 | 4 | 4 | 4 | 4 | 4 |
528 ;; Calc formula
529 (concat
530 "#+TBLFM: "
531 "$5 = if(typeof(vmean($1..$4)) == 12, "
532 "string(\"\"), vmean($1..$4)); E :: "
533 "$6 = if(typeof(vmean(@0$1..@0$4)) == 12, "
534 "string(\"\"), vmean(@0$1..@0$4)); E :: "
535 "$7 = if(\"$1..$4\" == \"[]\", string(\"\"), vmean($1..$4)) :: "
536 "$8 = if(\"@0$1..@0$4\" == \"[]\", string(\"\"), vmean(@0$1..@0$4)) :: "
537 "$9 = vmean($1..$4); EN :: "
538 "$10 = vmean(@0$1..@0$4); EN")
539 ;; Lisp formula
540 (concat
541 "#+TBLFM: "
542 "$5 = '(let ((l '($1..$4))) (if (member \"\" l) \"\" "
543 "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
544 "$6 = '(let ((l '(@0$1..@0$4))) (if (member \"\" l) \"\" "
545 "(/ (apply '+ (mapcar 'string-to-number l)) (length l)))); E :: "
546 "$7 = '(let ((l '($1..$4))) "
547 "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
548 "$8 = '(let ((l '(@0$1..@0$4))) "
549 "(if l (/ (apply '+ l) (length l)) \"\")); N :: "
550 "$9 = '(/ (+ $1..$4) (length '($1..$4))); EN :: "
551 "$10 = '(/ (+ @0$1..@0$4) (length '(@0$1..@0$4))); EN")
554 (ert-deftest test-org-table/copy-field ()
555 "Experiments on how to copy one field into another field.
556 See also `test-org-table/remote-reference-access'."
557 (let ((target "
558 | 0 | replace |
559 | a b | replace |
560 | c d | replace |
561 | | replace |
562 | 2012-12 | replace |
563 | [2012-12-31 Mon] | replace |
565 ;; Lisp formula to copy literally
566 (org-test-table-target-expect
567 target
569 | 0 | 0 |
570 | a b | a b |
571 | c d | c d |
572 | | |
573 | 2012-12 | 2012-12 |
574 | [2012-12-31 Mon] | [2012-12-31 Mon] |
576 1 "#+TBLFM: $2 = '(identity $1)")
578 ;; Calc formula to copy quite literally
579 (org-test-table-target-expect
580 target
582 | 0 | 0 |
583 | a b | a b |
584 | c d | c d |
585 | | |
586 | 2012-12 | 2012-12 |
587 | [2012-12-31 Mon] | <2012-12-31 Mon> |
589 1 (concat "#+TBLFM: $2 = if(\"$1\" == \"nan\", "
590 "string(\"\"), string(subvec(\"$1\", 2, vlen(\"$1\")))); E"))
592 ;; Calc formula simple
593 (org-test-table-target-expect
594 target
596 | 0 | 0 |
597 | a b | a b |
598 | c d | c d |
599 | | |
600 | 2012-12 | 2000 |
601 | [2012-12-31 Mon] | <2012-12-31 Mon> |
603 1 "#+TBLFM: $2 = if(\"$1\" == \"nan\", string(\"\"), $1); E")))
605 (ert-deftest test-org-table/sub-total ()
606 "Grouped rows with sub-total.
607 Begin range with \"@II\" to handle multiline header. Convert
608 integer to float with \"+.0\" for sub-total of items c1 and c2.
609 Sum empty fields as value zero but without ignoring them for
610 \"vlen\" with format specifier \"EN\". Format possibly empty
611 results with the Calc formatter \"f-1\" instead of the printf
612 formatter \"%.1f\"."
613 (org-test-table-target-expect
615 |-------+---------+---------|
616 | Item | Item | Sub- |
617 | name | value | total |
618 |-------+---------+---------|
619 | a1 | 4.1 | replace |
620 | a2 | 8.2 | replace |
621 | a3 | | replace |
622 |-------+---------+---------|
623 | b1 | 16.0 | replace |
624 |-------+---------+---------|
625 | c1 | 32 | replace |
626 | c2 | 64 | replace |
627 |-------+---------+---------|
628 | Total | replace | replace |
629 |-------+---------+---------|
632 |-------+-------+-------|
633 | Item | Item | Sub- |
634 | name | value | total |
635 |-------+-------+-------|
636 | a1 | 4.1 | |
637 | a2 | 8.2 | |
638 | a3 | | 12.3 |
639 |-------+-------+-------|
640 | b1 | 16.0 | 16.0 |
641 |-------+-------+-------|
642 | c1 | 32 | |
643 | c2 | 64 | 96.0 |
644 |-------+-------+-------|
645 | Total | 124.3 | |
646 |-------+-------+-------|
648 1 (concat "#+TBLFM: @>$2 = vsum(@II..@>>) ::"
649 "$3 = if(vlen(@0..@+I) == 1, "
650 "vsum(@-I$2..@+I$2) +.0, string(\"\")); EN f-1 :: "
651 "@>$3 = string(\"\")")))
653 (ert-deftest test-org-table/org-lookup-all ()
654 "Use `org-lookup-all' for several GROUP BY as in SQL and for ranking.
655 See also http://orgmode.org/worg/org-tutorials/org-lookups.html ."
656 (let ((data "
657 #+NAME: data
658 | Purchase | Product | Shop | Rating |
659 |----------+---------+------+--------|
660 | a | p1 | s1 | 1 |
661 | b | p1 | s2 | 4 |
662 | c | p2 | s1 | 2 |
663 | d | p3 | s2 | 8 |
666 ;; Product rating and ranking by average purchase from "#+NAME: data"
667 (org-test-table-target-expect
668 (concat data "
669 | Product | Rating | Ranking |
670 |---------+---------+---------|
671 | p1 | replace | replace |
672 | p2 | replace | replace |
673 | p3 | replace | replace |
675 (concat data "
676 | Product | Rating | Ranking |
677 |---------+--------+---------|
678 | p1 | 2.5 | 2 |
679 | p2 | 2.0 | 3 |
680 | p3 | 8.0 | 1 |
682 2 (concat
683 "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
684 "'(remote(data, @I$2..@>$2)) '(remote(data, @I$4..@>$4))))) "
685 "(/ (apply '+ all) (length all) 1.0)); L :: "
686 "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))
688 ;; Shop rating and ranking by average purchase from "#+NAME: data"
689 (org-test-table-target-expect
690 (concat data "
691 | Shop | Rating | Ranking |
692 |------+---------+---------|
693 | s1 | replace | replace |
694 | s2 | replace | replace |
696 (concat data "
697 | Shop | Rating | Ranking |
698 |------+--------+---------|
699 | s1 | 1.5 | 2 |
700 | s2 | 6.0 | 1 |
702 2 (concat
703 "#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 "
704 "'(remote(data, @I$3..@>$3)) '(remote(data, @I$4..@>$4))))) "
705 "(/ (apply '+ all) (length all) 1.0)); L :: "
706 "$3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2) nil '<))); N"))))
708 (ert-deftest test-org-table/org-table-make-reference/mode-string-EL ()
709 (fset 'f 'org-table-make-reference)
710 ;; For Lisp formula only
711 (should (equal "0" (f "0" t nil 'literal)))
712 (should (equal "z" (f "z" t nil 'literal)))
713 (should (equal "" (f "" t nil 'literal)))
714 (should (equal "0 1" (f '("0" "1") t nil 'literal)))
715 (should (equal "z 1" (f '("z" "1") t nil 'literal)))
716 (should (equal " 1" (f '("" "1") t nil 'literal)))
717 (should (equal " " (f '("" "" ) t nil 'literal))))
719 (ert-deftest test-org-table/org-table-make-reference/mode-string-E ()
720 (fset 'f 'org-table-make-reference)
721 ;; For Lisp formula
722 (should (equal "\"0\"" (f "0" t nil t)))
723 (should (equal "\"z\"" (f "z" t nil t)))
724 (should (equal "\"\"" (f "" t nil t)))
725 (should (equal "\"0\" \"1\"" (f '("0" "1") t nil t)))
726 (should (equal "\"z\" \"1\"" (f '("z" "1") t nil t)))
727 (should (equal "\"\" \"1\"" (f '("" "1") t nil t)))
728 (should (equal "\"\" \"\"" (f '("" "" ) t nil t)))
729 ;; For Calc formula
730 (should (equal "(0)" (f "0" t nil nil)))
731 (should (equal "(z)" (f "z" t nil nil)))
732 (should (equal "nan" (f "" t nil nil)))
733 (should (equal "[0,1]" (f '("0" "1") t nil nil)))
734 (should (equal "[z,1]" (f '("z" "1") t nil nil)))
735 (should (equal "[nan,1]" (f '("" "1") t nil nil)))
736 (should (equal "[nan,nan]" (f '("" "" ) t nil nil)))
737 ;; For Calc formula, special numbers
738 (should (equal "(nan)" (f "nan" t nil nil)))
739 (should (equal "(uinf)" (f "uinf" t nil nil)))
740 (should (equal "(-inf)" (f "-inf" t nil nil)))
741 (should (equal "(inf)" (f "inf" t nil nil)))
742 (should (equal "[nan,1]" (f '( "nan" "1") t nil nil)))
743 (should (equal "[uinf,1]" (f '("uinf" "1") t nil nil)))
744 (should (equal "[-inf,1]" (f '("-inf" "1") t nil nil)))
745 (should (equal "[inf,1]" (f '( "inf" "1") t nil nil))))
747 (ert-deftest test-org-table/org-table-make-reference/mode-string-EN ()
748 (fset 'f 'org-table-make-reference)
749 ;; For Lisp formula
750 (should (equal "0" (f "0" t t t)))
751 (should (equal "0" (f "z" t t t)))
752 (should (equal "0" (f "" t t t)))
753 (should (equal "0 1" (f '("0" "1") t t t)))
754 (should (equal "0 1" (f '("z" "1") t t t)))
755 (should (equal "0 1" (f '("" "1") t t t)))
756 (should (equal "0 0" (f '("" "" ) t t t)))
757 ;; For Calc formula
758 (should (equal "(0)" (f "0" t t nil)))
759 (should (equal "(0)" (f "z" t t nil)))
760 (should (equal "(0)" (f "" t t nil)))
761 (should (equal "[0,1]" (f '("0" "1") t t nil)))
762 (should (equal "[0,1]" (f '("z" "1") t t nil)))
763 (should (equal "[0,1]" (f '("" "1") t t nil)))
764 (should (equal "[0,0]" (f '("" "" ) t t nil)))
765 ;; For Calc formula, special numbers
766 (should (equal "(0)" (f "nan" t t nil)))
767 (should (equal "(0)" (f "uinf" t t nil)))
768 (should (equal "(0)" (f "-inf" t t nil)))
769 (should (equal "(0)" (f "inf" t t nil)))
770 (should (equal "[0,1]" (f '( "nan" "1") t t nil)))
771 (should (equal "[0,1]" (f '("uinf" "1") t t nil)))
772 (should (equal "[0,1]" (f '("-inf" "1") t t nil)))
773 (should (equal "[0,1]" (f '( "inf" "1") t t nil))))
775 (ert-deftest test-org-table/org-table-make-reference/mode-string-L ()
776 (fset 'f 'org-table-make-reference)
777 ;; For Lisp formula only
778 (should (equal "0" (f "0" nil nil 'literal)))
779 (should (equal "z" (f "z" nil nil 'literal)))
780 (should (equal "" (f "" nil nil 'literal)))
781 (should (equal "0 1" (f '("0" "1") nil nil 'literal)))
782 (should (equal "z 1" (f '("z" "1") nil nil 'literal)))
783 (should (equal "1" (f '("" "1") nil nil 'literal)))
784 (should (equal "" (f '("" "" ) nil nil 'literal))))
786 (ert-deftest test-org-table/org-table-make-reference/mode-string-none ()
787 (fset 'f 'org-table-make-reference)
788 ;; For Lisp formula
789 (should (equal "\"0\"" (f "0" nil nil t)))
790 (should (equal "\"z\"" (f "z" nil nil t)))
791 (should (equal "" (f "" nil nil t)))
792 (should (equal "\"0\" \"1\"" (f '("0" "1") nil nil t)))
793 (should (equal "\"z\" \"1\"" (f '("z" "1") nil nil t)))
794 (should (equal "\"1\"" (f '("" "1") nil nil t)))
795 (should (equal "" (f '("" "" ) nil nil t)))
796 ;; For Calc formula
797 (should (equal "(0)" (f "0" nil nil nil)))
798 (should (equal "(z)" (f "z" nil nil nil)))
799 (should (equal "(0)" (f "" nil nil nil)))
800 (should (equal "[0,1]" (f '("0" "1") nil nil nil)))
801 (should (equal "[z,1]" (f '("z" "1") nil nil nil)))
802 (should (equal "[1]" (f '("" "1") nil nil nil)))
803 (should (equal "[]" (f '("" "" ) nil nil nil)))
804 ;; For Calc formula, special numbers
805 (should (equal "(nan)" (f "nan" nil nil nil)))
806 (should (equal "(uinf)" (f "uinf" nil nil nil)))
807 (should (equal "(-inf)" (f "-inf" nil nil nil)))
808 (should (equal "(inf)" (f "inf" nil nil nil)))
809 (should (equal "[nan,1]" (f '( "nan" "1") nil nil nil)))
810 (should (equal "[uinf,1]" (f '("uinf" "1") nil nil nil)))
811 (should (equal "[-inf,1]" (f '("-inf" "1") nil nil nil)))
812 (should (equal "[inf,1]" (f '( "inf" "1") nil nil nil))))
814 (ert-deftest test-org-table/org-table-make-reference/mode-string-N ()
815 (fset 'f 'org-table-make-reference)
816 ;; For Lisp formula
817 (should (equal "0" (f "0" nil t t)))
818 (should (equal "0" (f "z" nil t t)))
819 (should (equal "" (f "" nil t t)))
820 (should (equal "0 1" (f '("0" "1") nil t t)))
821 (should (equal "0 1" (f '("z" "1") nil t t)))
822 (should (equal "1" (f '("" "1") nil t t)))
823 (should (equal "" (f '("" "" ) nil t t)))
824 ;; For Calc formula
825 (should (equal "(0)" (f "0" nil t nil)))
826 (should (equal "(0)" (f "z" nil t nil)))
827 (should (equal "(0)" (f "" nil t nil)))
828 (should (equal "[0,1]" (f '("0" "1") nil t nil)))
829 (should (equal "[0,1]" (f '("z" "1") nil t nil)))
830 (should (equal "[1]" (f '("" "1") nil t nil)))
831 (should (equal "[]" (f '("" "" ) nil t nil)))
832 ;; For Calc formula, special numbers
833 (should (equal "(0)" (f "nan" nil t nil)))
834 (should (equal "(0)" (f "uinf" nil t nil)))
835 (should (equal "(0)" (f "-inf" nil t nil)))
836 (should (equal "(0)" (f "inf" nil t nil)))
837 (should (equal "[0,1]" (f '( "nan" "1") nil t nil)))
838 (should (equal "[0,1]" (f '("uinf" "1") nil t nil)))
839 (should (equal "[0,1]" (f '("-inf" "1") nil t nil)))
840 (should (equal "[0,1]" (f '( "inf" "1") nil t nil))))
842 (ert-deftest test-org-table/org-table-convert-refs-to-an/1 ()
843 "Simple reference @1$1."
844 (should
845 (string= "A1" (org-table-convert-refs-to-an "@1$1"))))
847 ;; TODO: Test broken
848 ;; (ert-deftest test-org-table/org-table-convert-refs-to-an/2 ()
849 ;; "Self reference @1$1."
850 ;; (should
851 ;; (string= "A1 = $0" (org-table-convert-refs-to-an "@1$1 = $0"))))
853 (ert-deftest test-org-table/org-table-convert-refs-to-an/3 ()
854 "Remote reference."
855 (should
856 (string= "C& = remote(FOO, @@#B&)" (org-table-convert-refs-to-an "$3 = remote(FOO, @@#$2)"))))
858 (ert-deftest test-org-table/org-table-convert-refs-to-rc/1 ()
859 "Simple reference @1$1."
860 (should
861 (string= "@1$1" (org-table-convert-refs-to-rc "A1"))))
863 (ert-deftest test-org-table/org-table-convert-refs-to-rc/2 ()
864 "Self reference $0."
865 (should
866 (string= "@1$1 = $0" (org-table-convert-refs-to-rc "A1 = $0"))))
868 ;; TODO: Test Broken
869 ;; (ert-deftest test-org-table/org-table-convert-refs-to-rc/3 ()
870 ;; "Remote reference."
871 ;; (should
872 ;; (string= "$3 = remote(FOO, @@#$2)" (org-table-convert-refs-to-rc "C& = remote(FOO, @@#B&)"))))
874 (ert-deftest test-org-table/remote-reference-access ()
875 "Access to remote reference.
876 See also `test-org-table/copy-field'."
877 (org-test-table-target-expect
879 #+NAME: table
880 | | x 42 | |
882 | replace | replace |
885 #+NAME: table
886 | | x 42 | |
888 | x 42 | 84 x |
890 1 (concat "#+TBLFM: "
891 ;; Copy text without calculation: Use Lisp formula
892 "$1 = '(identity remote(table, @1$2)) :: "
893 ;; Do a calculation: Use Calc (or Lisp ) formula
894 "$2 = 2 * remote(table, @1$2)")))
896 (ert-deftest test-org-table/remote-reference-indirect ()
897 "Access to remote reference with indirection of name or ID."
898 (let ((source-tables "
899 #+NAME: 2012
900 | amount |
901 |--------|
902 | 1 |
903 | 2 |
904 |--------|
905 | 3 |
906 #+TBLFM: @>$1 = vsum(@I..@II)
908 #+NAME: 2013
909 | amount |
910 |--------|
911 | 4 |
912 | 8 |
913 |--------|
914 | 12 |
915 #+TBLFM: @>$1 = vsum(@I..@II)
918 ;; Read several remote references from same column
919 (org-test-table-target-expect
920 (concat source-tables "
921 #+NAME: summary
922 | year | amount |
923 |-------+---------|
924 | 2012 | replace |
925 | 2013 | replace |
926 |-------+---------|
927 | total | replace |
929 (concat source-tables "
930 #+NAME: summary
931 | year | amount |
932 |-------+--------|
933 | 2012 | 3 |
934 | 2013 | 12 |
935 |-------+--------|
936 | total | 15 |
939 ;; Calc formula
940 "#+TBLFM: @<<$2..@>>$2 = remote($<, @>$1) :: @>$2 = vsum(@I..@II)"
941 ;; Lisp formula
942 (concat "#+TBLFM: @<<$2..@>>$2 = '(identity remote($<, @>$1)); N :: "
943 "@>$2 = '(+ @I..@II); N"))
945 ;; Read several remote references from same row
946 (org-test-table-target-expect
947 (concat source-tables "
948 #+NAME: summary
949 | year | 2012 | 2013 | total |
950 |--------+---------+---------+---------|
951 | amount | replace | replace | replace |
953 (concat source-tables "
954 #+NAME: summary
955 | year | 2012 | 2013 | total |
956 |--------+------+------+-------|
957 | amount | 3 | 12 | 15 |
960 ;; Calc formula
961 "#+TBLFM: @2$<<..@2$>> = remote(@<, @>$1) :: @2$> = vsum($<<..$>>)"
962 ;; Lisp formula
963 (concat "#+TBLFM: @2$<<..@2$>> = '(identity remote(@<, @>$1)); N :: "
964 "@2$> = '(+ $<<..$>>); N"))))
966 (ert-deftest test-org-table/org-at-TBLFM-p ()
967 (org-test-with-temp-text-in-file
969 | 1 |
970 | 2 |
971 #+TBLFM: $2=$1*2
974 (goto-char (point-min))
975 (forward-line 2)
976 (should (equal (org-at-TBLFM-p) nil))
978 (goto-char (point-min))
979 (forward-line 3)
980 (should (equal (org-at-TBLFM-p) t))
982 (goto-char (point-min))
983 (forward-line 4)
984 (should (equal (org-at-TBLFM-p) nil))))
986 (ert-deftest test-org-table/org-table-TBLFM-begin ()
987 (org-test-with-temp-text-in-file
989 | 1 |
990 | 2 |
991 #+TBLFM: $2=$1*2
994 (goto-char (point-min))
995 (should (equal (org-table-TBLFM-begin)
996 nil))
998 (goto-char (point-min))
999 (forward-line 1)
1000 (should (equal (org-table-TBLFM-begin)
1001 nil))
1003 (goto-char (point-min))
1004 (forward-line 3)
1005 (should (= (org-table-TBLFM-begin)
1006 14))
1008 (goto-char (point-min))
1009 (forward-line 4)
1010 (should (= (org-table-TBLFM-begin)
1011 14))
1015 (ert-deftest test-org-table/org-table-TBLFM-begin-for-multiple-TBLFM-lines ()
1016 "For multiple #+TBLFM lines."
1017 (org-test-with-temp-text-in-file
1019 | 1 |
1020 | 2 |
1021 #+TBLFM: $2=$1*1
1022 #+TBLFM: $2=$1*2
1025 (goto-char (point-min))
1026 (should (equal (org-table-TBLFM-begin)
1027 nil))
1029 (goto-char (point-min))
1030 (forward-line 1)
1031 (should (equal (org-table-TBLFM-begin)
1032 nil))
1034 (goto-char (point-min))
1035 (forward-line 3)
1036 (should (= (org-table-TBLFM-begin)
1037 14))
1039 (goto-char (point-min))
1040 (forward-line 4)
1041 (should (= (org-table-TBLFM-begin)
1042 14))
1044 (goto-char (point-min))
1045 (forward-line 5)
1046 (should (= (org-table-TBLFM-begin)
1047 14))
1051 (ert-deftest test-org-table/org-table-TBLFM-begin-for-pultiple-TBLFM-lines-blocks ()
1052 (org-test-with-temp-text-in-file
1054 | 1 |
1055 | 2 |
1056 #+TBLFM: $2=$1*1
1057 #+TBLFM: $2=$1*2
1059 | 6 |
1060 | 7 |
1061 #+TBLFM: $2=$1*1
1062 #+TBLFM: $2=$1*2
1065 (goto-char (point-min))
1066 (should (equal (org-table-TBLFM-begin)
1067 nil))
1069 (goto-char (point-min))
1070 (forward-line 1)
1071 (should (equal (org-table-TBLFM-begin)
1072 nil))
1074 (goto-char (point-min))
1075 (forward-line 3)
1076 (should (= (org-table-TBLFM-begin)
1077 14))
1079 (goto-char (point-min))
1080 (forward-line 4)
1081 (should (= (org-table-TBLFM-begin)
1082 14))
1084 (goto-char (point-min))
1085 (forward-line 5)
1086 (should (= (org-table-TBLFM-begin)
1087 14))
1089 (goto-char (point-min))
1090 (forward-line 6)
1091 (should (= (org-table-TBLFM-begin)
1092 14))
1094 (goto-char (point-min))
1095 (forward-line 8)
1096 (should (= (org-table-TBLFM-begin)
1097 61))
1099 (goto-char (point-min))
1100 (forward-line 9)
1101 (should (= (org-table-TBLFM-begin)
1102 61))
1104 (goto-char (point-min))
1105 (forward-line 10)
1106 (should (= (org-table-TBLFM-begin)
1107 61))))
1109 (ert-deftest test-org-table/org-table-calc-current-TBLFM ()
1110 (org-test-with-temp-text-in-file
1112 | 1 | |
1113 | 2 | |
1114 #+TBLFM: $2=$1*1
1115 #+TBLFM: $2=$1*2
1116 #+TBLFM: $2=$1*3
1118 (let ((got (progn (goto-char (point-min))
1119 (forward-line 3)
1120 (org-table-calc-current-TBLFM)
1121 (buffer-string)))
1122 (expect "
1123 | 1 | 1 |
1124 | 2 | 2 |
1125 #+TBLFM: $2=$1*1
1126 #+TBLFM: $2=$1*2
1127 #+TBLFM: $2=$1*3
1129 (should (string= got
1130 expect)))
1132 (let ((got (progn (goto-char (point-min))
1133 (forward-line 4)
1134 (org-table-calc-current-TBLFM)
1135 (buffer-string)))
1136 (expect "
1137 | 1 | 2 |
1138 | 2 | 4 |
1139 #+TBLFM: $2=$1*1
1140 #+TBLFM: $2=$1*2
1141 #+TBLFM: $2=$1*3
1143 (should (string= got
1144 expect)))))
1146 (ert-deftest test-org-table/org-table-calc-current-TBLFM-when-stop-because-of-error ()
1147 "org-table-calc-current-TBLFM should preserve the input as it was."
1148 (org-test-with-temp-text-in-file
1150 | 1 | 1 |
1151 | 2 | 2 |
1152 #+TBLFM: $2=$1*1
1153 #+TBLFM: $2=$1*2::$2=$1*2
1154 #+TBLFM: $2=$1*3
1156 (let ((expect "
1157 | 1 | 1 |
1158 | 2 | 2 |
1159 #+TBLFM: $2=$1*1
1160 #+TBLFM: $2=$1*2::$2=$1*2
1161 #+TBLFM: $2=$1*3
1163 (goto-char (point-min))
1164 (forward-line 4)
1165 (should-error (org-table-calc-current-TBLFM))
1166 (setq got (buffer-string))
1167 (message "%s" got)
1168 (should (string= got
1169 expect)))))
1171 (provide 'test-org-table)
1173 ;;; test-org-table.el ends here