all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob c477a63a29fabbf9d25ca00641d1ee8be9c5b1fe 18698 bytes (raw)
name: test/lisp/emacs-lisp/subr-x-tests.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
 
;;; subr-x-tests.el --- Testing the extended lisp routines    -*- lexical-binding: t -*-

;; Copyright (C) 2014-2017 Free Software Foundation, Inc.

;; Author: Fabián E. Gallina <fgallina@gnu.org>
;; Keywords:

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(require 'ert)
(require 'subr-x)

\f
;; `if-let*' tests

(ert-deftest subr-x-test-if-let*-single-binding-expansion ()
  "Test single bindings are expanded properly."
  (should (equal
           (macroexpand
            '(if-let* ((a 1))
                 (- a)
               "no"))
           '(let* ((a (and t 1)))
              (if a
                  (- a)
                "no"))))
  (should (equal
           (macroexpand
            '(if-let* (a)
                 (- a)
               "no"))
           '(let* ((a (and t a)))
              (if a
                  (- a)
                "no")))))

(ert-deftest subr-x-test-if-let*-single-symbol-expansion ()
  "Test single symbol bindings are expanded properly."
  (should (equal
           (macroexpand
            '(if-let* (a)
                 (- a)
               "no"))
           '(let* ((a (and t a)))
              (if a
                  (- a)
                "no"))))
  (should (equal
           (macroexpand
            '(if-let* (a b c)
                 (- a)
               "no"))
           '(let* ((a (and t a))
                   (b (and a b))
                   (c (and b c)))
              (if c
                  (- a)
                "no"))))
  (should (equal
           (macroexpand
            '(if-let* (a (b 2) c)
                 (- a)
               "no"))
           '(let* ((a (and t a))
                   (b (and a 2))
                   (c (and b c)))
              (if c
                  (- a)
                "no")))))

(ert-deftest subr-x-test-if-let*-nil-related-expansion ()
  "Test nil is processed properly."
  (should (equal
           (macroexpand
            '(if-let* (nil)
                 (- a)
               "no"))
           '(let* ((nil (and t nil)))
              (if nil
                  (- a)
                "no"))))
  (should (equal
           (macroexpand
            '(if-let* ((a 1) nil (b 2))
                 (- a)
               "no"))
           '(let* ((a (and t 1))
                   (nil (and a nil))
                   (b (and nil 2)))
              (if b
                  (- a)
                "no")))))

(ert-deftest subr-x-test-if-let*-malformed-binding ()
  "Test malformed bindings trigger errors."
  (should-error (macroexpand
                 '(if-let* (_ (a 1 1) (b 2) (c 3) d)
                      (- a)
                    "no"))
                :type 'error)
  (should-error (macroexpand
                 '(if-let* (_ (a 1) (b 2 2) (c 3) d)
                      (- a)
                    "no"))
                :type 'error)
  (should-error (macroexpand
                 '(if-let* (_ (a 1) (b 2) (c 3 3) d)
                      (- a)
                    "no"))
                :type 'error)
  (should-error (macroexpand
                 '(if-let* ((a 1 1))
                      (- a)
                    "no"))
                :type 'error))

(ert-deftest subr-x-test-if-let*-true ()
  "Test `if-let' with truthy bindings."
  (should (equal
           (if-let* ((a 1))
               a
             "no")
           1))
  (should (equal
           (if-let* ((a 1) (b 2) (c 3))
               (list a b c)
             "no")
           (list 1 2 3))))

(ert-deftest subr-x-test-if-let*-false ()
  "Test `if-let' with falsie bindings."
  (should (equal
           (if-let* ((a nil))
               (list a b c)
             "no")
           "no"))
  (should (equal
           (if-let* ((a nil) (b 2) (c 3))
               (list a b c)
             "no")
           "no"))
  (should (equal
           (if-let* ((a 1) (b nil) (c 3))
               (list a b c)
             "no")
           "no"))
  (should (equal
           (if-let* ((a 1) (b 2) (c nil))
               (list a b c)
             "no")
           "no"))
  (should (equal
           (let (z)
             (if-let* (z (a 1) (b 2) (c 3))
                 (list a b c)
               "no"))
           "no"))
  (should (equal
           (let (d)
             (if-let* ((a 1) (b 2) (c 3) d)
                 (list a b c)
               "no"))
           "no")))

(ert-deftest subr-x-test-if-let*-bound-references ()
  "Test `if-let' bindings can refer to already bound symbols."
  (should (equal
           (if-let* ((a (1+ 0)) (b (1+ a)) (c (1+ b)))
               (list a b c)
             "no")
           (list 1 2 3))))

(ert-deftest subr-x-test-if-let*-and-laziness-is-preserved ()
  "Test `if-let' respects `and' laziness."
  (let (a-called b-called c-called)
    (should (equal
             (if-let* ((a nil)
                       (b (setq b-called t))
                       (c (setq c-called t)))
                 "yes"
               (list a-called b-called c-called))
             (list nil nil nil))))
  (let (a-called b-called c-called)
    (should (equal
             (if-let* ((a (setq a-called t))
                       (b nil)
                       (c (setq c-called t)))
                 "yes"
               (list a-called b-called c-called))
             (list t nil nil))))
  (let (a-called b-called c-called)
    (should (equal
             (if-let* ((a (setq a-called t))
                      (b (setq b-called t))
                      (c nil)
                      (d (setq c-called t)))
                 "yes"
               (list a-called b-called c-called))
             (list t t nil)))))

\f
;; `when-let*' tests

(ert-deftest subr-x-test-when-let*-body-expansion ()
  "Test body allows for multiple sexps wrapping with progn."
  (should (equal
           (macroexpand
            '(when-let* ((a 1))
               (message "opposite")
               (- a)))
           '(let* ((a (and t 1)))
              (if a
                  (progn
                    (message "opposite")
                    (- a)))))))

(ert-deftest subr-x-test-when-let*-single-symbol-expansion ()
  "Test single symbol bindings are expanded properly."
  (should (equal
           (macroexpand
            '(when-let* (a)
               (- a)))
           '(let* ((a (and t a)))
              (if a
                  (- a)))))
  (should (equal
           (macroexpand
            '(when-let* (a b c)
               (- a)))
           '(let* ((a (and t a))
                   (b (and a b))
                   (c (and b c)))
              (if c
                  (- a)))))
  (should (equal
           (macroexpand
            '(when-let* (a (b 2) c)
               (- a)))
           '(let* ((a (and t a))
                   (b (and a 2))
                   (c (and b c)))
              (if c
                  (- a))))))

(ert-deftest subr-x-test-when-let*-nil-related-expansion ()
  "Test nil is processed properly."
  (should (equal
           (macroexpand
            '(when-let* (nil)
               (- a)))
           '(let* ((nil (and t nil)))
              (if nil
                  (- a)))))
  (should (equal
           (macroexpand
            '(when-let* ((a 1) nil (b 2))
               (- a)))
           '(let* ((a (and t 1))
                   (nil (and a nil))
                   (b (and nil 2)))
              (if b
                  (- a))))))

(ert-deftest subr-x-test-when-let*-malformed-binding ()
  "Test malformed bindings trigger errors."
  (should-error (macroexpand
                 '(when-let* (_ (a 1 1) (b 2) (c 3) d)
                    (- a)))
                :type 'error)
  (should-error (macroexpand
                 '(when-let* (_ (a 1) (b 2 2) (c 3) d)
                    (- a)))
                :type 'error)
  (should-error (macroexpand
                 '(when-let* (_ (a 1) (b 2) (c 3 3) d)
                    (- a)))
                :type 'error)
  (should-error (macroexpand
                 '(when-let* ((a 1 1))
                    (- a)))
                :type 'error))

(ert-deftest subr-x-test-when-let*-true ()
  "Test `when-let' with truthy bindings."
  (should (equal
           (when-let* ((a 1))
             a)
           1))
  (should (equal
           (when-let* ((a 1) (b 2) (c 3))
             (list a b c))
           (list 1 2 3))))

(ert-deftest subr-x-test-when-let*-false ()
  "Test `when-let' with falsie bindings."
  (should (equal
           (when-let* ((a nil))
             (list a b c)
             "no")
           nil))
  (should (equal
           (when-let* ((a nil) (b 2) (c 3))
             (list a b c)
             "no")
           nil))
  (should (equal
           (when-let* ((a 1) (b nil) (c 3))
             (list a b c)
             "no")
           nil))
  (should (equal
           (when-let* ((a 1) (b 2) (c nil))
             (list a b c)
             "no")
           nil))
  (should (equal
           (let (z)
             (when-let* (z (a 1) (b 2) (c 3))
               (list a b c)
               "no"))
           nil))
  (should (equal
           (let (d)
             (when-let* ((a 1) (b 2) (c 3) d)
               (list a b c)
               "no"))
           nil)))

(ert-deftest subr-x-test-when-let*-bound-references ()
  "Test `when-let' bindings can refer to already bound symbols."
  (should (equal
           (when-let* ((a (1+ 0)) (b (1+ a)) (c (1+ b)))
             (list a b c))
           (list 1 2 3))))

(ert-deftest subr-x-test-when-let*-and-laziness-is-preserved ()
  "Test `when-let' respects `and' laziness."
  (let (a-called b-called c-called)
    (should (equal
             (progn
               (when-let* ((a nil)
                           (b (setq b-called t))
                           (c (setq c-called t)))
                 "yes")
               (list a-called b-called c-called))
             (list nil nil nil))))
  (let (a-called b-called c-called)
    (should (equal
             (progn
               (when-let* ((a (setq a-called t))
                           (b nil)
                           (c (setq c-called t)))
                 "yes")
               (list a-called b-called c-called))
             (list t nil nil))))
  (let (a-called b-called c-called)
    (should (equal
             (progn
               (when-let* ((a (setq a-called t))
                           (b (setq b-called t))
                           (c nil)
                           (d (setq c-called t)))
                 "yes")
               (list a-called b-called c-called))
             (list t t nil)))))

\f
;; `and-let*' tests

;; Adapted from the Guile tests
;; https://git.savannah.gnu.org/cgit/guile.git/tree/test-suite/tests/srfi-2.test

(ert-deftest subr-x-and-let*-test-empty-varlist ()
  (should (equal 1 (and-let* () 1)))
  (should (equal 2 (and-let* () 1 2)))
  (should (equal t (and-let* ()))))

(ert-deftest subr-x-and-let*-test-group-1 ()
   (should (equal nil (let ((x nil)) (and-let* (x)))))
   (should (equal 1 (let ((x 1)) (and-let* (x)))))
   (should (equal nil (and-let* ((x nil)))))
   (should (equal 1 (and-let* ((x 1)))))
   ;; The error doesn't trigger when compiled: the compiler will give
   ;; a warning and then drop the erroneous code.  Therefore, use
   ;; `eval' to avoid compilation.
   (should-error (eval '(and-let* (nil (x 1))) lexical-binding)
                 :type 'setting-constant)
   (should (equal nil (and-let* ((nil) (x 1)))))
   (should-error (eval (and-let* (2 (x 1))) lexical-binding)
                 :type 'wrong-type-argument)
   (should (equal 1 (and-let* ((2) (x 1)))))
   (should (equal 2 (and-let* ((x 1) (2)))))
   (should (equal nil (let ((x nil)) (and-let* (x) x))))
   (should (equal "" (let ((x "")) (and-let* (x) x))))
   (should (equal "" (let ((x "")) (and-let* (x)))))
   (should (equal 2 (let ((x 1)) (and-let* (x) (+ x 1)))))
   (should (equal nil (let ((x nil)) (and-let* (x) (+ x 1)))))
   (should (equal 2 (let ((x 1)) (and-let* (((> x 0))) (+ x 1)))))
   (should (equal t (let ((x 1)) (and-let* (((> x 0)))))))
   (should (equal nil (let ((x 0)) (and-let* (((> x 0))) (+ x 1)))))
   (should (equal 3
                  (let ((x 1)) (and-let* (((> x 0)) (x (+ x 1))) (+ x 1))))))

(ert-deftest subr-x-and-let*-test-rebind ()
   (should
    (equal 4
           (let ((x 1))
             (and-let* (((> x 0)) (x (+ x 1)) (x (+ x 1))) (+ x 1))))))

(ert-deftest subr-x-and-let*-test-group-2 ()
   (should
    (equal 2 (let ((x 1)) (and-let* (x ((> x 0))) (+ x 1)))))
   (should
    (equal 2 (let ((x 1)) (and-let* (((progn x)) ((> x 0))) (+ x 1)))))
   (should (equal nil (let ((x 0)) (and-let* (x ((> x 0))) (+ x 1)))))
   (should (equal nil (let ((x nil)) (and-let* (x ((> x 0))) (+ x 1)))))
   (should
    (equal nil (let ((x nil)) (and-let* (((progn x)) ((> x 0))) (+ x 1))))))

(ert-deftest subr-x-and-let*-test-group-3 ()
   (should
    (equal nil (let ((x 1)) (and-let* (x (y (- x 1)) ((> y 0))) (/ x y)))))
   (should
    (equal nil (let ((x 0)) (and-let* (x (y (- x 1)) ((> y 0))) (/ x y)))))
   (should
    (equal nil
           (let ((x nil)) (and-let* (x (y (- x 1)) ((> y 0))) (/ x y)))))
   (should
    (equal (/ 3.0 2)
           (let ((x 3.0)) (and-let* (x (y (- x 1)) ((> y 0))) (/ x y))))))


\f
;; Thread first tests

(ert-deftest subr-x-test-thread-first-no-forms ()
  "Test `thread-first' with no forms expands to the first form."
  (should (equal (macroexpand '(thread-first 5)) 5))
  (should (equal (macroexpand '(thread-first (+ 1 2))) '(+ 1 2))))

(ert-deftest subr-x-test-thread-first-function-names-are-threaded ()
  "Test `thread-first' wraps single function names."
  (should (equal (macroexpand
                  '(thread-first 5
                     -))
                 '(- 5)))
  (should (equal (macroexpand
                  '(thread-first (+ 1 2)
                     -))
                 '(- (+ 1 2)))))

(ert-deftest subr-x-test-thread-first-expansion ()
  "Test `thread-first' expands correctly."
  (should (equal
           (macroexpand '(thread-first
                             5
                           (+ 20)
                           (/ 25)
                           -
                           (+ 40)))
           '(+ (- (/ (+ 5 20) 25)) 40))))

(ert-deftest subr-x-test-thread-first-examples ()
  "Test several `thread-first' examples."
  (should (equal (thread-first (+ 40 2)) 42))
  (should (equal (thread-first
                     5
                   (+ 20)
                   (/ 25)
                   -
                   (+ 40)) 39))
  (should (equal (thread-first
                     "this-is-a-string"
                   (split-string "-")
                   (nbutlast 2)
                   (append (list "good")))
                 (list "this" "is" "good"))))
\f
;; Thread last tests

(ert-deftest subr-x-test-thread-last-no-forms ()
  "Test `thread-last' with no forms expands to the first form."
  (should (equal (macroexpand '(thread-last 5)) 5))
  (should (equal (macroexpand '(thread-last (+ 1 2))) '(+ 1 2))))

(ert-deftest subr-x-test-thread-last-function-names-are-threaded ()
  "Test `thread-last' wraps single function names."
  (should (equal (macroexpand
                  '(thread-last 5
                     -))
                 '(- 5)))
  (should (equal (macroexpand
                  '(thread-last (+ 1 2)
                     -))
                 '(- (+ 1 2)))))

(ert-deftest subr-x-test-thread-last-expansion ()
  "Test `thread-last' expands correctly."
  (should (equal
           (macroexpand '(thread-last
                             5
                           (+ 20)
                           (/ 25)
                           -
                           (+ 40)))
           '(+ 40 (- (/ 25 (+ 20 5)))))))

(ert-deftest subr-x-test-thread-last-examples ()
  "Test several `thread-last' examples."
  (should (equal (thread-last (+ 40 2)) 42))
  (should (equal (thread-last
                     5
                   (+ 20)
                   (/ 25)
                   -
                   (+ 40)) 39))
  (should (equal (thread-last
                     (list 1 -2 3 -4 5)
                   (mapcar #'abs)
                   (cl-reduce #'+)
                   (format "abs sum is: %s"))
                 "abs sum is: 15")))

\f
;; lazy-let tests

(ert-deftest subr-x-lazy-let-basic-test ()
  "Test whether bindings are established."
  (should (equal (lazy-let ((x 1) (y 2)) (+ x y)) 3)))

(ert-deftest subr-x-lazy-let*-basic-test ()
  "Test whether bindings are established."
  (should (equal (lazy-let* ((x 1) (y (+ 1 x))) (+ x y)) 3)))

(ert-deftest subr-x-lazy-let-bound-vars-cant-be-bound-test ()
  "Test whether setting or binding a `lazy-let' bound variable fails."
  (should-error (eval '(lazy-let ((x 1)) (let ((y 7)) (setq x (+ x y)) (* 10 x))) t))
  (should-error (eval '(lazy-let ((x 1)) (let ((x 2)) x)) t)))

(ert-deftest subr-x-lazy-let-lazyness-test ()
  "Test for lazyness."
  (should
   (equal (let ((x-evalled nil)
                (y-evalled nil))
            (lazy-let ((x (progn (setq x-evalled t) (+ 1 2)))
                       (y (progn (setq y-evalled t) (+ 3 4))))
              (let ((evalled-y y))
                (list x-evalled y-evalled evalled-y))))
          (list nil t 7))))

(ert-deftest subr-x-lazy-let*-lazyness-test ()
  "Test lazyness of `lazy-let*'."
  (should
   (equal (let ((x-evalled nil)
                (y-evalled nil)
                (z-evalled nil)
                (a-evalled nil))
            (lazy-let* ((x (progn (setq x-evalled t) (+ 1 1)))
                        (y (progn (setq y-evalled t) (+ x 1)))
                        (z (progn (setq z-evalled t) (+ y 1)))
                        (a (progn (setq a-evalled t) (+ z 1))))
              (let ((evalled-z z))
                (list x-evalled y-evalled z-evalled a-evalled evalled-z))))
          (list t t t nil 4))))

(ert-deftest subr-x-lazy-let-bad-binding-test ()
  "Test whether a bad binding causes a compiler error."
  (should-error (byte-compile (lazy-let ((x 1 1)) x)))
  (should-error (byte-compile (lazy-let (27) x)))
  (should-error (byte-compile (lazy-let x x))))


(provide 'subr-x-tests)
;;; subr-x-tests.el ends here

debug log:

solving c477a63a29 ...
found c477a63a29 in https://yhetil.org/emacs/87o9ocd6s4.fsf@web.de/ ||
	https://yhetil.org/emacs/87wp2zcwm2.fsf@web.de/ ||
	https://yhetil.org/emacs/87mv3vwb2c.fsf_-_@web.de/
found 0e8871d9a9 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 0e8871d9a9c073398d5108f5cc61aa8cf5da6bcf	test/lisp/emacs-lisp/subr-x-tests.el

applying [1/1] https://yhetil.org/emacs/87o9ocd6s4.fsf@web.de/
diff --git a/test/lisp/emacs-lisp/subr-x-tests.el b/test/lisp/emacs-lisp/subr-x-tests.el
index 0e8871d9a9..c477a63a29 100644

Checking patch test/lisp/emacs-lisp/subr-x-tests.el...
Applied patch test/lisp/emacs-lisp/subr-x-tests.el cleanly.

skipping https://yhetil.org/emacs/87o9ocd6s4.fsf@web.de/ for c477a63a29
skipping https://yhetil.org/emacs/87wp2zcwm2.fsf@web.de/ for c477a63a29
skipping https://yhetil.org/emacs/87wp2zcwm2.fsf@web.de/ for c477a63a29
skipping https://yhetil.org/emacs/87mv3vwb2c.fsf_-_@web.de/ for c477a63a29
index at:
100644 c477a63a29fabbf9d25ca00641d1ee8be9c5b1fe	test/lisp/emacs-lisp/subr-x-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.