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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
| | ;;; pcase-tests.el --- Test suite for pcase macro. -*- lexical-binding:t -*-
;; Copyright (C) 2012-2023 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'ert)
(require 'cl-lib)
(ert-deftest pcase-tests-base ()
"Test pcase code."
(should (equal (pcase '(1 . 2) ((app car '2) 6) ((app car '1) 5)) 5)))
(ert-deftest pcase-tests-bugs ()
(should (equal (pcase '(2 . 3) ;bug#18554
(`(,hd . ,(and (pred atom) tl)) (list hd tl))
((pred consp) nil))
'(2 3)))
(should (equal (pcase '(2 . 3)
(`(,hd . ,(and (pred (not consp)) tl)) (list hd tl))
((pred consp) nil))
'(2 3))))
(pcase-defmacro pcase-tests-plus (pat n)
`(app (lambda (v) (- v ,n)) ,pat))
(ert-deftest pcase-tests-macro ()
(should (equal (pcase 5 ((pcase-tests-plus x 3) x)) 2)))
(defun pcase-tests-grep (fname exp)
(when (consp exp)
(or (eq fname (car exp))
(cl-some (lambda (exp) (pcase-tests-grep fname exp)) (cdr exp)))))
(ert-deftest pcase-tests-tests ()
(should (pcase-tests-grep 'memq '(or (+ 2 3) (memq x y))))
(should-not (pcase-tests-grep 'memq '(or (+ 2 3) (- x y)))))
(ert-deftest pcase-tests-member ()
(should (pcase-tests-grep
'memq (macroexpand-all '(pcase x ((or 'a 'b 'c) body)))))
(should (pcase-tests-grep
'memql (macroexpand-all '(pcase x ((or 1 2 3 'a) body)))))
(should (pcase-tests-grep
'member (macroexpand-all '(pcase x ((or "a" 2 3 'a) body)))))
(should-not (pcase-tests-grep
'memq (macroexpand-all '(pcase x ((or "a" 2 3) body)))))
(should-not (pcase-tests-grep
'memql (macroexpand-all '(pcase x ((or "a" 2 3) body)))))
(let ((exp (macroexpand-all
'(pcase x
("a" body1)
(2 body2)
((or "a" 2 3) body)))))
(should-not (pcase-tests-grep 'memq exp))
(should-not (pcase-tests-grep 'member exp))))
(ert-deftest pcase-tests-vectors ()
(should (equal (pcase [1 2] (`[,x] 1) (`[,x ,y] (+ x y))) 3)))
(ert-deftest pcase-tests-bug14773 ()
(let ((f (lambda (x)
(pcase 'dummy
((and (let var x) (guard var)) 'left)
((and (let var (not x)) (guard var)) 'right)))))
(should (equal (funcall f t) 'left))
(should (equal (funcall f nil) 'right))))
(ert-deftest pcase-tests-bug46786 ()
(let ((self 'outer))
(ignore self)
(should (equal (cl-macrolet ((show-self () `(list 'self self)))
(pcase-let ((`(,self ,_self2) '(inner "2")))
(show-self)))
'(self inner)))))
(ert-deftest pcase-tests-or-vars ()
(let ((f (lambda (v)
(pcase v
((or (and 'b1 (let x1 4) (let x2 5))
(and 'b2 (let y1 8) (let y2 9)))
(list x1 x2 y1 y2))))))
(should (equal (funcall f 'b1) '(4 5 nil nil)))
(should (equal (funcall f 'b2) '(nil nil 8 9)))))
(ert-deftest pcase-tests-cl-type ()
(should (equal (pcase 1
((cl-type integer) 'integer))
'integer))
(should (equal (pcase 1
((cl-type (integer 0 2)) 'integer-0<=n<=2))
'integer-0<=n<=2))
(should-error
;; Avoid error at compile time due to compiler macro.
(eval '(pcase 1
((cl-type notatype) 'integer))
t)))
(ert-deftest pcase-tests-setq ()
(should (equal (let (a b)
(pcase-setq `((,a) (,b)) '((1) (2)))
(list a b))
(list 1 2)))
(should (equal (list nil nil)
(let ((a 'unset)
(b 'unset))
(pcase-setq `(head ,a ,b) nil)
(list a b))))
(should (equal (let (a b)
(pcase-setq `[,a ,b] [1 2])
(list a b))
'(1 2)))
(should-error (let (a b)
(pcase-setq `[,a ,b] nil)
(list a b)))
(should (equal (let (a b)
(pcase-setq a 1 b 2)
(list a b))
'(1 2)))
(should (= (let (a)
(pcase-setq a 1 `(,a) '(2))
a)
2))
(should (equal (let (array list-item array-copy)
(pcase-setq (or `(,list-item) array) [1 2 3]
array-copy array
;; This re-sets `array' to nil.
(or `(,list-item) array) '(4))
(list array array-copy list-item))
'(nil [1 2 3] 4)))
(let ((a nil))
(should-error (pcase-setq a 1 b)
:type '(wrong-number-of-arguments))
(should (eq a nil)))
(should-error (pcase-setq a)
:type '(wrong-number-of-arguments)))
;;;; Tests for the `cl-arglist' `pcase' pattern.
(ert-deftest pcase-tests-cl-arglist-&whole-should-error ()
"`&whole' must come first if given, and must be followed by a patter."
(should-error (pcase (list 1 2 3)
((cl-arglist (&whole))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (a b &whole c))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&rest a &whole c))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&key a &whole c))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&aux (a 1) &whole c))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&optional (a 1) &whole c))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&whole whole1 &whole whole2))
(list whole1 whole2)))
:type 'cl--pcase-cl-arglist-invalid-arg-list))
(ert-deftest pcase-tests-cl-arglist-&whole ()
"`&whole' can be a `pcase' pattern."
(should (equal (list (list 1 2 3) 1 2 3)
(pcase (list 1 2 3)
((cl-arglist (&whole whole a b c))
(list whole a b c)))))
(should (equal (list 1 2 3 1 2 3)
(pcase (list 1 2 3)
((cl-arglist (&whole `(,a0 ,b0 ,c0) a b c))
(list a0 b0 c0 a b c))))))
(ert-deftest pcase-tests-cl-arglist-pos ()
"Positional variables must match the length of EXPVAL."
(should (equal (list 1 2 3)
(pcase (list 1 2 3)
((cl-arglist (a b c))
(list a b c)))))
(should (equal nil
(pcase (list (list 1))
((cl-arglist (a b))
(list a b)))))
(should (equal nil
(pcase (list (list 1 2 3))
((cl-arglist (a b))
(list a b))))))
(ert-deftest pcase-tests-cl-arglist-pos-sub-patterns ()
(should (equal (list 1 2 3 4)
(pcase (list 1 2 (list 3 4))
((cl-arglist (a b (cl-arglist (c d))))
(list a b c d)))))
(should (equal (list 1 2)
(pcase (list (list 1 2))
((cl-arglist (`(,a ,b)))
(list a b))))))
(ert-deftest pcase-tests-cl-arglist-&optional-should-error ()
"`&optional' cannot be used after `&optional', `&rest', `&key', and `&aux'."
(should-error (equal (list 1 2 3)
(pcase (list 1 2 3)
((cl-arglist (&rest a &optional b c))
(list a b c))))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&body a &optional b c))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&key a &optional b c))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&allow-other-keys &optional b c))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&aux (a 1) &optional b c))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&optional a &optional b c))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list))
(ert-deftest pcase-tests-cl-arglist-&optional ()
(should (equal (list 1 2 3)
(pcase (list 1 2 3)
((cl-arglist (a b &optional c))
(list a b c)))))
(should (equal (list 1 2 nil)
(pcase (list 1 2)
((cl-arglist (a b &optional c))
(list a b c)))))
(should (equal (list 1 2 13)
(pcase (list 1 2)
((cl-arglist (a b &optional (c 13)))
(list a b c)))))
(should (equal (list 1 2 13 nil)
(pcase (list 1 2)
((cl-arglist (a b &optional (c 13 c-supplied)))
(list a b c c-supplied)))))
(should (equal (list 1 2 3 t)
(pcase (list 1 2 3)
((cl-arglist (a b &optional (c 13 c-supplied)))
(list a b c c-supplied))))))
(ert-deftest pcase-tests-cl-arglist-&optional-sub-patterns ()
"Test using sub-patterns in `cl-arglist' pattern.
Sub-patterns must be contained within a sub-list, since a sub-list
also provides a default value."
(should-error (pcase (list 1 2 (list 3 4))
((cl-arglist (a b &optional `(,c ,d)))
(list a b c d))))
(should (equal (list 1 2 33)
(pcase (list 1 2)
((cl-arglist (a b &optional ((and opt1
(guard (numberp opt1)))
33)))
(list a b opt1)))))
(should (equal nil
(pcase (list 1 2 'not-num)
((cl-arglist (a b &optional ((and opt1
(guard (numberp opt1)))
33)))
(list a b opt1)))))
(should (equal nil
(pcase (list 1 2 nil)
((cl-arglist (a b &optional ((and opt1
(guard (numberp opt1)))
'not-num)))
(list a b opt1)))))
(should (equal (list 1 2 3 4)
(pcase (list 1 2 (list 3 4))
((cl-arglist (a b &optional (`(,c ,d))))
(list a b c d)))))
(should (equal (list 1 2 nil nil)
(pcase (list 1 2)
((cl-arglist (a b &optional (`(,c ,d))))
(list a b c d)))))
(should (equal (list 1 2 13 14)
(pcase (list 1 2)
((cl-arglist (a b &optional (`(,c ,d) (list 13 14))))
(list a b c d)))))
(should (equal (list 1 2 13 14)
(pcase (list 1 2)
((cl-arglist ( a b
&optional ((cl-arglist (c &optional (d 14)))
(list 13))))
(list a b c d)))))
(should (equal (list 1 2 13 14 nil)
(pcase (list 1 2)
((cl-arglist (a b &optional (`(,c ,d) (list 13 14) cd-supplied)))
(list a b c d cd-supplied)))))
(should (equal (list 1 2 13 14 nil t nil)
(pcase (list 1 2)
((cl-arglist ( a b
&optional
((cl-arglist (&optional (c 27 c-sub-sup)
(d 14 d-sub-sup)))
(list 13)
cd-supplied)))
(list a b c d cd-supplied c-sub-sup d-sub-sup))))))
(ert-deftest pcase-tests-cl-arglist-&rest-should-error ()
"`&rest' (`&body', `.') cannot be used after `&rest', `&body', `&key',and `&aux'."
(should-error (equal (list 1 2 3)
(pcase (list 1 2 3)
((cl-arglist (&rest a &rest b))
(list a b c))))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (equal (list 1 2 3)
(pcase (list 1 2 3)
((cl-arglist (&body a &body b))
(list a b c))))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (equal (list 1 2 3)
(pcase (list 1 2 3)
((cl-arglist (&body a . b))
(list a b c))))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&body a &rest b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&rest a &body b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&key a &rest b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&key a &body b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&key a . b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&allow-other-keys &rest b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&allow-other-keys &body b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&allow-other-keys . b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&aux (a 1) &rest b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&aux (a 1) &body b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list 1 2 3)
((cl-arglist (&aux (a 1) . b))
(list a b c)))
:type 'cl--pcase-cl-arglist-invalid-arg-list))
(ert-deftest pcase-tests-cl-arglist-&rest-nonlist-cdr ()
(should (equal (list 1 2)
(pcase (cons 1 2)
((cl-arglist (a &rest b))
(list a b)))))
(should (equal (list 1 2)
(pcase (cons 1 2)
((cl-arglist (a &body b))
(list a b)))))
(should (equal (list 1 2)
(pcase (cons 1 2)
((cl-arglist (a . b))
(list a b))))))
(ert-deftest pcase-tests-cl-arglist-&rest-with-&whole ()
(should (equal (list (cons 1 2) 1 2)
(pcase (cons 1 2)
((cl-arglist (&whole whole a &rest b))
(list whole a b)))))
(should (equal (list (cons 1 2) 1 2)
(pcase (cons 1 2)
((cl-arglist (&whole whole a &body b))
(list whole a b)))))
(should (equal (list (cons 1 2) 1 2)
(pcase (cons 1 2)
((cl-arglist (&whole whole a . b))
(list whole a b))))))
(ert-deftest pcase-tests-cl-arglist-&rest-only ()
"Using only `&rest' should work like `&whole'."
(should (equal (list (list 1 2))
(pcase (list 1 2)
((cl-arglist (&rest a))
(list a)))))
(should (equal (list (cons 1 2))
(pcase (cons 1 2)
((cl-arglist (&body a))
(list a))))))
(ert-deftest pcase-tests-cl-arglist-&rest-after-&optional ()
(should (equal (list 1 2 3 (list 4 5))
(pcase (list 1 2 3 4 5)
((cl-arglist (&optional a b c &rest d))
(list a b c d)))))
(should (equal (list 1 2 3 (list 4 5))
(pcase (list 1 2 3 4 5)
((cl-arglist (&optional a b c &body d))
(list a b c d)))))
(should (equal (list 1 2 3 (list 4 5))
(pcase (list 1 2 3 4 5)
((cl-arglist (&optional a b c . d))
(list a b c d))))))
(ert-deftest pcase-tests-cl-arglist-&rest-sub-patterns ()
;; We can't do (a . `(,b . ,c)), so we don't test that.
(should (equal (list 1 2 3)
(pcase (list 1 2 3)
((cl-arglist (a &rest (cl-arglist (b c))))
(list a b c)))))
(should (equal (list 1 2 3)
(pcase (list 1 2 3)
((cl-arglist (a &body `(,b ,c)))
(list a b c))))))
(ert-deftest pcase-tests-cl-arglist-&key-should-error ()
"`&key' cannot be used after `&key', `&allow-other-keys', and `&aux'."
(should-error (pcase (list :a 1 :b 2)
((cl-arglist (&key a &key b))
(list a b)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list :a 1 :b 2)
((cl-arglist (&aux (a 1) &key b))
(list a b)))
:type 'cl--pcase-cl-arglist-invalid-arg-list)
(should-error (pcase (list :a 1 :b 2)
((cl-arglist (&allow-other-keys &key b))
(list a b)))
:type 'cl--pcase-cl-arglist-invalid-arg-list))
(ert-deftest pcase-tests-cl-arglist-&key-exact ()
"`&key' doesn't match unspecified keys unless `&allow-other-keys' or `:allow-other-keys' is given."
(should (equal (list 1 2)
(pcase (list :a 1 :b 2)
((cl-arglist (&key a b))
(list a b)))))
(should (equal nil
(pcase (list :a 1 :b 2 :c 3)
((cl-arglist (&key a b))
(list a b)))))
(should (equal (list 1 2 nil)
(pcase (list :a 1 :b 2)
((cl-arglist (&key a b c))
(list a b c))))))
(ert-deftest pcase-tests-cl-arglist-&key-permissive ()
"`&key' doesn't match unspecified keys unless `&allow-other-keys' or `:allow-other-keys' is given."
(should (equal (list 1 2)
(pcase (list :a 1 :b 2 :c 3)
((cl-arglist (&key a b &allow-other-keys))
(list a b)))))
(should (equal (list 1 2)
(pcase (list :a 1 :b 2 :c 3 :allow-other-keys t)
((cl-arglist (&key a b))
(list a b))))))
(ert-deftest pcase-tests-cl-arglist-&key-not-first ()
"The plist should be after positional values and equal to `&rest'."
(should (equal (list 1 2 3 11 22)
(pcase (list 1 2 3 :k1 11 :k2 22)
((cl-arglist (a b c &key k1 k2))
(list a b c k1 k2)))))
(should (equal (list 1 2 3 (list :k1 11 :k2 22) 11 22)
(pcase (list 1 2 3 :k1 11 :k2 22)
((cl-arglist (a b c &rest r1 &key k1 k2))
(list a b c r1 k1 k2))))))
(ert-deftest pcase-tests-cl-arglist-&key-full-form ()
(should (equal (list 1 2)
(pcase (list :a 1 :b 2)
((cl-arglist (&key a (b 13)))
(list a b)))))
(should (equal (list 1 13)
(pcase (list :a 1)
((cl-arglist (&key a (b 13)))
(list a b)))))
(should (equal (list 1 13 nil)
(pcase (list :a 1)
((cl-arglist (&key a (b 13 b-supplied)))
(list a b b-supplied)))))
(should (equal (list 1 2 t)
(pcase (list :a 1 :b 2)
((cl-arglist (&key a (b 13 b-supplied)))
(list a b b-supplied)))))
(should (equal (list 1 2 t)
(pcase (list :a 1 :bat 2)
((cl-arglist (&key a ((:bat b) 13 b-supplied)))
(list a b b-supplied)))))
(should (equal (list 1 2 t)
(let ((key :bat))
(pcase (list :a 1 :bat 2)
((cl-arglist (&key a ((key b) 13 b-supplied)))
(list a b b-supplied)))))))
(ert-deftest pcase-tests-cl-arglist-&key-sub-patterns ()
(should (equal '(1 2 (:c 77 :e should-ignore) nil 77 t 99 nil)
(pcase '(:ab (1 2))
((cl-arglist (&key
((:ab `(,a ,b)))
((:cd (cl-arglist ( &whole cd
&key
(c 88 c-supp)
((:d d) 99 d-supp)
&allow-other-keys)))
(list :c 77 :e 'should-ignore)
cd-supp)))
(list a b cd cd-supp c c-supp d d-supp)))))
(should (equal '( 1 2 (:c 77 :e should-ignore :allow-other-keys t) nil
77 t 99 nil)
(pcase '(:ab (1 2))
((cl-arglist (&key
((:ab `(,a ,b)))
((:cd (cl-arglist ( &whole cd
&key
(c 88 c-supp)
((:d d) 99 d-supp))))
(list :c 77 :e 'should-ignore
:allow-other-keys t)
cd-supp)))
(list a b cd cd-supp c c-supp d d-supp)))))
(should (equal nil
(pcase '(:ab (1 2))
((cl-arglist (&key
((:ab `(,a ,b)))
((:cd (cl-arglist ( &whole cd
&key
(c 88 c-supp)
((:d d) 99 d-supp))))
(list :c 77 :e 'should-fail)
cd-supp)))
(list a b cd cd-supp c c-supp d d-supp))))))
(ert-deftest pcase-tests-cl-arglist-&aux-should-error ()
"`&aux' cannot be used after `&aux'."
(should-error (pcase nil
((cl-arglist (&aux a &aux b))
(list a b)))
:type 'cl--pcase-cl-arglist-invalid-arg-list))
(ert-deftest pcase-tests-cl-arglist-&aux ()
(should (equal (list 1 2 nil nil)
(pcase nil
((cl-arglist (&aux (a 1) (b 2) (c) d))
(list a b c d)))))
(should (equal (list 0 1 2 nil nil)
(pcase (list 0)
((cl-arglist (z0 &aux (a 1) (b 2) (c) d))
(list z0 a b c d))))))
(ert-deftest pcase-tests-cl-arglist-&aux-sub-patterns ()
(should (equal (list 1 2)
(pcase nil
((cl-arglist (&aux (`(,a ,b) (list 1 2))))
(list a b ))))))
(ert-deftest pcase-tests-cl-arglist-all ()
(should (equal '(1 2 3 4 5 (:k1 111 :k2 222) 111 222 333 444)
(pcase (list 1 2 3 4 5 :k1 111 :k2 222)
((cl-arglist ( a b c
&optional d e
&rest r
&key k1 k2
&aux (x1 333) (x2 444)))
(list a b c d e r k1 k2 x1 x2))))))
;;; pcase-tests.el ends here.
|