1 ;;; subr-x-tests.el --- Testing the extended lisp routines
3 ;; Copyright (C) 2014-2018 Free Software Foundation, Inc.
5 ;; Author: Fabián E. Gallina <fgallina@gnu.org>
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 <https://www.gnu.org/licenses/>.
33 (ert-deftest subr-x-test-if-let
*-single-binding-expansion
()
34 "Test single bindings are expanded properly."
40 '(let* ((a (and t
1)))
49 '(let* ((a (and t a
)))
54 (ert-deftest subr-x-test-if-let
*-single-symbol-expansion
()
55 "Test single symbol bindings are expanded properly."
61 '(let* ((a (and t a
)))
88 (ert-deftest subr-x-test-if-let
*-nil-related-expansion
()
89 "Test nil is processed properly."
95 '(let* ((nil (and t nil
)))
101 '(if-let* ((a 1) nil
(b 2))
104 '(let* ((a (and t
1))
111 (ert-deftest subr-x-test-if-let
*-malformed-binding
()
112 "Test malformed bindings trigger errors."
113 (should-error (macroexpand
114 '(if-let* (_ (a 1 1) (b 2) (c 3) d
)
118 (should-error (macroexpand
119 '(if-let* (_ (a 1) (b 2 2) (c 3) d
)
123 (should-error (macroexpand
124 '(if-let* (_ (a 1) (b 2) (c 3 3) d
)
128 (should-error (macroexpand
134 (ert-deftest subr-x-test-if-let
*-true
()
135 "Test `if-let' with truthy bindings."
142 (if-let* ((a 1) (b 2) (c 3))
147 (ert-deftest subr-x-test-if-let
*-false
()
148 "Test `if-let' with falsie bindings."
155 (if-let* ((a nil
) (b 2) (c 3))
160 (if-let* ((a 1) (b nil
) (c 3))
165 (if-let* ((a 1) (b 2) (c nil
))
171 (if-let* (z (a 1) (b 2) (c 3))
177 (if-let* ((a 1) (b 2) (c 3) d
)
182 (ert-deftest subr-x-test-if-let
*-bound-references
()
183 "Test `if-let' bindings can refer to already bound symbols."
185 (if-let* ((a (1+ 0)) (b (1+ a
)) (c (1+ b
)))
190 (ert-deftest subr-x-test-if-let
*-and-laziness-is-preserved
()
191 "Test `if-let' respects `and' laziness."
192 (let (a-called b-called c-called
)
195 (b (setq b-called t
))
196 (c (setq c-called t
)))
198 (list a-called b-called c-called
))
199 (list nil nil nil
))))
200 (let (a-called b-called c-called
)
202 (if-let* ((a (setq a-called t
))
204 (c (setq c-called t
)))
206 (list a-called b-called c-called
))
208 (let (a-called b-called c-called
)
210 (if-let* ((a (setq a-called t
))
211 (b (setq b-called t
))
213 (d (setq c-called t
)))
215 (list a-called b-called c-called
))
221 (ert-deftest subr-x-test-when-let
*-body-expansion
()
222 "Test body allows for multiple sexps wrapping with progn."
228 '(let* ((a (and t
1)))
234 (ert-deftest subr-x-test-when-let
*-single-symbol-expansion
()
235 "Test single symbol bindings are expanded properly."
240 '(let* ((a (and t a
)))
247 '(let* ((a (and t a
))
254 '(when-let* (a (b 2) c
)
256 '(let* ((a (and t a
))
262 (ert-deftest subr-x-test-when-let
*-nil-related-expansion
()
263 "Test nil is processed properly."
268 '(let* ((nil (and t nil
)))
273 '(when-let* ((a 1) nil
(b 2))
275 '(let* ((a (and t
1))
281 (ert-deftest subr-x-test-when-let
*-malformed-binding
()
282 "Test malformed bindings trigger errors."
283 (should-error (macroexpand
284 '(when-let* (_ (a 1 1) (b 2) (c 3) d
)
287 (should-error (macroexpand
288 '(when-let* (_ (a 1) (b 2 2) (c 3) d
)
291 (should-error (macroexpand
292 '(when-let* (_ (a 1) (b 2) (c 3 3) d
)
295 (should-error (macroexpand
296 '(when-let* ((a 1 1))
300 (ert-deftest subr-x-test-when-let
*-true
()
301 "Test `when-let' with truthy bindings."
307 (when-let* ((a 1) (b 2) (c 3))
311 (ert-deftest subr-x-test-when-let
*-false
()
312 "Test `when-let' with falsie bindings."
318 (when-let* ((a nil
) (b 2) (c 3))
322 (when-let* ((a 1) (b nil
) (c 3))
326 (when-let* ((a 1) (b 2) (c nil
))
331 (when-let* (z (a 1) (b 2) (c 3))
336 (when-let* ((a 1) (b 2) (c 3) d
)
340 (ert-deftest subr-x-test-when-let
*-bound-references
()
341 "Test `when-let' bindings can refer to already bound symbols."
343 (when-let* ((a (1+ 0)) (b (1+ a
)) (c (1+ b
)))
347 (ert-deftest subr-x-test-when-let
*-and-laziness-is-preserved
()
348 "Test `when-let' respects `and' laziness."
349 (let (a-called b-called c-called
)
353 (b (setq b-called t
))
354 (c (setq c-called t
)))
356 (list a-called b-called c-called
))
357 (list nil nil nil
))))
358 (let (a-called b-called c-called
)
361 (when-let* ((a (setq a-called t
))
363 (c (setq c-called t
)))
365 (list a-called b-called c-called
))
367 (let (a-called b-called c-called
)
370 (when-let* ((a (setq a-called t
))
371 (b (setq b-called t
))
373 (d (setq c-called t
)))
375 (list a-called b-called c-called
))
381 ;; Adapted from the Guile tests
382 ;; https://git.savannah.gnu.org/cgit/guile.git/tree/test-suite/tests/srfi-2.test
384 (ert-deftest subr-x-and-let
*-test-empty-varlist
()
385 (should (equal 1 (and-let* () 1)))
386 (should (equal 2 (and-let* () 1 2)))
387 (should (equal t
(and-let* ()))))
389 (ert-deftest subr-x-and-let
*-test-group-1
()
390 (should (equal nil
(let ((x nil
)) (and-let* (x)))))
391 (should (equal 1 (let ((x 1)) (and-let* (x)))))
392 (should (equal nil
(and-let* ((x nil
)))))
393 (should (equal 1 (and-let* ((x 1)))))
394 ;; The error doesn't trigger when compiled: the compiler will give
395 ;; a warning and then drop the erroneous code. Therefore, use
396 ;; `eval' to avoid compilation.
397 (should-error (eval '(and-let* (nil (x 1))) lexical-binding
)
398 :type
'setting-constant
)
399 (should (equal nil
(and-let* ((nil) (x 1)))))
400 (should-error (eval '(and-let* (2 (x 1))) lexical-binding
)
401 :type
'wrong-type-argument
)
402 (should (equal 1 (and-let* ((2) (x 1)))))
403 (should (equal 2 (and-let* ((x 1) (2)))))
404 (should (equal nil
(let ((x nil
)) (and-let* (x) x
))))
405 (should (equal "" (let ((x "")) (and-let* (x) x
))))
406 (should (equal "" (let ((x "")) (and-let* (x)))))
407 (should (equal 2 (let ((x 1)) (and-let* (x) (+ x
1)))))
408 (should (equal nil
(let ((x nil
)) (and-let* (x) (+ x
1)))))
409 (should (equal 2 (let ((x 1)) (and-let* (((> x
0))) (+ x
1)))))
410 (should (equal t
(let ((x 1)) (and-let* (((> x
0)))))))
411 (should (equal nil
(let ((x 0)) (and-let* (((> x
0))) (+ x
1)))))
413 (let ((x 1)) (and-let* (((> x
0)) (x (+ x
1))) (+ x
1))))))
415 (ert-deftest subr-x-and-let
*-test-rebind
()
419 (and-let* (((> x
0)) (x (+ x
1)) (x (+ x
1))) (+ x
1))))))
421 (ert-deftest subr-x-and-let
*-test-group-2
()
423 (equal 2 (let ((x 1)) (and-let* (x ((> x
0))) (+ x
1)))))
425 (equal 2 (let ((x 1)) (and-let* (((progn x
)) ((> x
0))) (+ x
1)))))
426 (should (equal nil
(let ((x 0)) (and-let* (x ((> x
0))) (+ x
1)))))
427 (should (equal nil
(let ((x nil
)) (and-let* (x ((> x
0))) (+ x
1)))))
429 (equal nil
(let ((x nil
)) (and-let* (((progn x
)) ((> x
0))) (+ x
1))))))
431 (ert-deftest subr-x-and-let
*-test-group-3
()
433 (equal nil
(let ((x 1)) (and-let* (x (y (- x
1)) ((> y
0))) (/ x y
)))))
435 (equal nil
(let ((x 0)) (and-let* (x (y (- x
1)) ((> y
0))) (/ x y
)))))
438 (let ((x nil
)) (and-let* (x (y (- x
1)) ((> y
0))) (/ x y
)))))
441 (let ((x 3.0)) (and-let* (x (y (- x
1)) ((> y
0))) (/ x y
))))))
445 ;; Thread first tests
447 (ert-deftest subr-x-test-thread-first-no-forms
()
448 "Test `thread-first' with no forms expands to the first form."
449 (should (equal (macroexpand '(thread-first 5)) 5))
450 (should (equal (macroexpand '(thread-first (+ 1 2))) '(+ 1 2))))
452 (ert-deftest subr-x-test-thread-first-function-names-are-threaded
()
453 "Test `thread-first' wraps single function names."
454 (should (equal (macroexpand
458 (should (equal (macroexpand
459 '(thread-first (+ 1 2)
463 (ert-deftest subr-x-test-thread-first-expansion
()
464 "Test `thread-first' expands correctly."
466 (macroexpand '(thread-first
472 '(+ (- (/ (+ 5 20) 25)) 40))))
474 (ert-deftest subr-x-test-thread-first-examples
()
475 "Test several `thread-first' examples."
476 (should (equal (thread-first (+ 40 2)) 42))
477 (should (equal (thread-first
483 (should (equal (thread-first
487 (append (list "good")))
488 (list "this" "is" "good"))))
492 (ert-deftest subr-x-test-thread-last-no-forms
()
493 "Test `thread-last' with no forms expands to the first form."
494 (should (equal (macroexpand '(thread-last 5)) 5))
495 (should (equal (macroexpand '(thread-last (+ 1 2))) '(+ 1 2))))
497 (ert-deftest subr-x-test-thread-last-function-names-are-threaded
()
498 "Test `thread-last' wraps single function names."
499 (should (equal (macroexpand
503 (should (equal (macroexpand
504 '(thread-last (+ 1 2)
508 (ert-deftest subr-x-test-thread-last-expansion
()
509 "Test `thread-last' expands correctly."
511 (macroexpand '(thread-last
517 '(+ 40 (- (/ 25 (+ 20 5)))))))
519 (ert-deftest subr-x-test-thread-last-examples
()
520 "Test several `thread-last' examples."
521 (should (equal (thread-last (+ 40 2)) 42))
522 (should (equal (thread-last
528 (should (equal (thread-last
532 (format "abs sum is: %s"))
536 (provide 'subr-x-tests
)
537 ;;; subr-x-tests.el ends here