Help save net neutrality! Learn more.
Close

[e4c1b1]: / tests / packages.impure.lisp  Maximize  Restore  History

Download this file

733 lines (673 with data), 30.3 kB

  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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
;;;; miscellaneous tests of package-related stuff
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;;
;;;; This software is in the public domain and is provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for
;;;; more information.
(make-package "FOO")
(defvar *foo* (find-package (coerce "FOO" 'base-string)))
(rename-package "FOO" (make-array 0 :element-type nil))
(assert (eq *foo* (find-package "")))
(assert (delete-package ""))
(make-package "BAR")
(defvar *baz* (rename-package "BAR" "BAZ"))
(assert (eq *baz* (find-package "BAZ")))
(assert (delete-package *baz*))
(handler-case
(export :foo)
(package-error (c) (princ c))
(:no-error (&rest args) (error "(EXPORT :FOO) returned ~S" args)))
(make-package "FOO")
(assert (shadow #\a :foo))
(defpackage :PACKAGE-DESIGNATOR-1 (:use #.(find-package :cl)))
(defpackage :PACKAGE-DESIGNATOR-2
(:import-from #.(find-package :cl) "+"))
(defpackage "EXAMPLE-INDIRECT"
(:import-from "CL" "+"))
(defpackage "EXAMPLE-PACKAGE"
(:shadow "CAR")
(:shadowing-import-from "CL" "CAAR")
(:use)
(:import-from "CL" "CDR")
(:import-from "EXAMPLE-INDIRECT" "+")
(:export "CAR" "CDR" "EXAMPLE"))
(flet ((check-symbol (name expected-status expected-home-name)
(multiple-value-bind (symbol status)
(find-symbol name "EXAMPLE-PACKAGE")
(let ((home (symbol-package symbol))
(expected-home (find-package expected-home-name)))
(assert (eql home expected-home))
(assert (eql status expected-status))))))
(check-symbol "CAR" :external "EXAMPLE-PACKAGE")
(check-symbol "CDR" :external "CL")
(check-symbol "EXAMPLE" :external "EXAMPLE-PACKAGE")
(check-symbol "CAAR" :internal "CL")
(check-symbol "+" :internal "CL")
(check-symbol "CDDR" nil "CL"))
(defpackage "TEST-ORIGINAL" (:nicknames "A-NICKNAME"))
(assert-error (defpackage "A-NICKNAME"))
(assert (eql (find-package "A-NICKNAME")
(find-package "TEST-ORIGINAL")))
;;;; Utilities
(defun sym (package name)
(let ((package (or (find-package package) package)))
(multiple-value-bind (symbol status)
(find-symbol name package)
(assert status
(package name symbol status)
"No symbol with name ~A in ~S." name package symbol status)
(values symbol status))))
(defmacro with-name-conflict-resolution ((symbol &key restarted)
form &body body)
"Resolves potential name conflict condition arising from FORM.
The conflict is resolved in favour of SYMBOL, a form which must
evaluate to a symbol.
If RESTARTED is a symbol, it is bound for the BODY forms and set to T
if a restart was invoked."
(check-type restarted symbol "a binding name")
(let ((%symbol (copy-symbol 'symbol)))
`(let (,@(when restarted `((,restarted)))
(,%symbol ,symbol))
(handler-bind
((sb-ext:name-conflict
(lambda (condition)
,@(when restarted `((setf ,restarted t)))
(assert (member ,%symbol (sb-ext:name-conflict-symbols condition)))
(invoke-restart 'sb-ext:resolve-conflict ,%symbol))))
,form)
,@body)))
(defmacro with-packages (specs &body forms)
(let ((names (mapcar #'car specs)))
`(unwind-protect
(progn
(delete-packages ',names)
,@(mapcar (lambda (spec)
`(defpackage ,@spec))
specs)
,@forms)
(delete-packages ',names))))
(defun delete-packages (names)
(dolist (p names)
(ignore-errors (delete-package p))))
;;;; Tests
;;; USE-PACKAGE
(with-test (:name :use-package.1)
(with-packages (("FOO" (:export "SYM"))
("BAR" (:export "SYM"))
("BAZ" (:use)))
(with-name-conflict-resolution ((sym "BAR" "SYM") :restarted restartedp)
(use-package '("FOO" "BAR") "BAZ")
(is restartedp)
(is (eq (sym "BAR" "SYM")
(sym "BAZ" "SYM"))))))
(with-test (:name :use-package.2)
(with-packages (("FOO" (:export "SYM"))
("BAZ" (:use) (:intern "SYM")))
(with-name-conflict-resolution ((sym "FOO" "SYM") :restarted restartedp)
(use-package "FOO" "BAZ")
(is restartedp)
(is (eq (sym "FOO" "SYM")
(sym "BAZ" "SYM"))))))
(with-test (:name :use-package.2a)
(with-packages (("FOO" (:export "SYM"))
("BAZ" (:use) (:intern "SYM")))
(with-name-conflict-resolution ((sym "BAZ" "SYM") :restarted restartedp)
(use-package "FOO" "BAZ")
(is restartedp)
(is (equal (list (sym "BAZ" "SYM") :internal)
(multiple-value-list (sym "BAZ" "SYM")))))))
(with-test (:name :use-package-conflict-set :fails-on :sbcl)
(with-packages (("FOO" (:export "SYM"))
("QUX" (:export "SYM"))
("BAR" (:intern "SYM"))
("BAZ" (:use) (:import-from "BAR" "SYM")))
(let ((conflict-set))
(block nil
(handler-bind
((sb-ext:name-conflict
(lambda (condition)
(setf conflict-set (copy-list
(sb-ext:name-conflict-symbols condition)))
(return))))
(use-package '("FOO" "QUX") "BAZ")))
(setf conflict-set
(sort conflict-set #'string<
:key (lambda (symbol)
(package-name (symbol-package symbol)))))
(is (equal (list (sym "BAR" "SYM") (sym "FOO" "SYM") (sym "QUX" "SYM"))
conflict-set)))))
;;; EXPORT
(with-test (:name :export.1)
(with-packages (("FOO" (:intern "SYM"))
("BAR" (:export "SYM"))
("BAZ" (:use "FOO" "BAR")))
(with-name-conflict-resolution ((sym "FOO" "SYM") :restarted restartedp)
(export (sym "FOO" "SYM") "FOO")
(is restartedp)
(is (eq (sym "FOO" "SYM")
(sym "BAZ" "SYM"))))))
(with-test (:name :export.1a)
(with-packages (("FOO" (:intern "SYM"))
("BAR" (:export "SYM"))
("BAZ" (:use "FOO" "BAR")))
(with-name-conflict-resolution ((sym "BAR" "SYM") :restarted restartedp)
(export (sym "FOO" "SYM") "FOO")
(is restartedp)
(is (eq (sym "BAR" "SYM")
(sym "BAZ" "SYM"))))))
(with-test (:name :export.ensure-exported)
(with-packages (("FOO" (:intern "SYM"))
("BAR" (:export "SYM"))
("BAZ" (:use "FOO" "BAR") (:IMPORT-FROM "BAR" "SYM")))
(with-name-conflict-resolution ((sym "FOO" "SYM") :restarted restartedp)
(export (sym "FOO" "SYM") "FOO")
(is restartedp)
(is (equal (list (sym "FOO" "SYM") :external)
(multiple-value-list (sym "FOO" "SYM"))))
(is (eq (sym "FOO" "SYM")
(sym "BAZ" "SYM"))))))
(with-test (:name :export.3.intern)
(with-packages (("FOO" (:intern "SYM"))
("BAZ" (:use "FOO") (:intern "SYM")))
(with-name-conflict-resolution ((sym "FOO" "SYM") :restarted restartedp)
(export (sym "FOO" "SYM") "FOO")
(is restartedp)
(is (eq (sym "FOO" "SYM")
(sym "BAZ" "SYM"))))))
(with-test (:name :export.3a.intern)
(with-packages (("FOO" (:intern "SYM"))
("BAZ" (:use "FOO") (:intern "SYM")))
(with-name-conflict-resolution ((sym "BAZ" "SYM") :restarted restartedp)
(export (sym "FOO" "SYM") "FOO")
(is restartedp)
(is (equal (list (sym "BAZ" "SYM") :internal)
(multiple-value-list (sym "BAZ" "SYM")))))))
;;; IMPORT
(with-test (:name :import-nil.1)
(with-packages (("FOO" (:use) (:intern "NIL"))
("BAZ" (:use) (:intern "NIL")))
(with-name-conflict-resolution ((sym "FOO" "NIL") :restarted restartedp)
(import (list (sym "FOO" "NIL")) "BAZ")
(is restartedp)
(is (eq (sym "FOO" "NIL")
(sym "BAZ" "NIL"))))))
(with-test (:name :import-nil.2)
(with-packages (("BAZ" (:use) (:intern "NIL")))
(with-name-conflict-resolution ('CL:NIL :restarted restartedp)
(import '(CL:NIL) "BAZ")
(is restartedp)
(is (eq 'CL:NIL
(sym "BAZ" "NIL"))))))
(with-test (:name :import-single-conflict :fails-on :sbcl)
(with-packages (("FOO" (:export "NIL"))
("BAR" (:export "NIL"))
("BAZ" (:use)))
(let ((conflict-sets '()))
(handler-bind
((sb-ext:name-conflict
(lambda (condition)
(push (copy-list (sb-ext:name-conflict-symbols condition))
conflict-sets)
(invoke-restart 'sb-ext:resolve-conflict 'CL:NIL))))
(import (list 'CL:NIL (sym "FOO" "NIL") (sym "BAR" "NIL")) "BAZ"))
(is (eql 1 (length conflict-sets)))
(is (eql 3 (length (first conflict-sets)))))))
;;; Make sure that resolving a name-conflict in IMPORT doesn't leave
;;; multiple symbols of the same name in the package (this particular
;;; scenario found in 1.0.38.9, but clearly a longstanding issue).
(with-test (:name :import-conflict-resolution)
(with-packages (("FOO" (:export "NIL"))
("BAR" (:use)))
(with-name-conflict-resolution ((sym "FOO" "NIL"))
(import (list 'CL:NIL (sym "FOO" "NIL")) "BAR"))
(do-symbols (sym "BAR")
(assert (eq sym (sym "FOO" "NIL"))))))
;;; UNINTERN
(with-test (:name :unintern.1)
(with-packages (("FOO" (:export "SYM"))
("BAR" (:export "SYM"))
("BAZ" (:use "FOO" "BAR") (:shadow "SYM")))
(with-name-conflict-resolution ((sym "FOO" "SYM") :restarted restartedp)
(unintern (sym "BAZ" "SYM") "BAZ")
(is restartedp)
(is (eq (sym "FOO" "SYM")
(sym "BAZ" "SYM"))))))
(with-test (:name :unintern.2)
(with-packages (("FOO" (:intern "SYM")))
(unintern :sym "FOO")
(assert (find-symbol "SYM" "FOO"))))
;;; WITH-PACKAGE-ITERATOR error signalling had problems
(with-test (:name :with-package-iterator.error)
(assert (eq :good
(handler-case
(progn
(eval '(with-package-iterator (sym :cl-user :foo)
(sym)))
:bad)
((and simple-condition program-error) (c)
(assert (equal (list :foo) (simple-condition-format-arguments c)))
:good)))))
;; X3J13 writeup HASH-TABLE-PACKAGE-GENERATORS says
;; "An argument of NIL is treated as an empty list of packages."
;; This used to fail with "NIL does not name a package"
(with-test (:name :with-package-iterator-nil-list)
(with-package-iterator (iter '() :internal)
(print (nth-value 1 (iter)))))
;;; MAKE-PACKAGE error in another thread blocking FIND-PACKAGE & FIND-SYMBOL
(with-test (:name :bug-511072 :skipped-on '(not :sb-thread))
(let* ((p (make-package :bug-511072))
(sem1 (sb-thread:make-semaphore))
(sem2 (sb-thread:make-semaphore))
(t2 (make-join-thread (lambda ()
(handler-bind ((error (lambda (c)
(sb-thread:signal-semaphore sem1)
(sb-thread:wait-on-semaphore sem2)
(abort c))))
(make-package :bug-511072))))))
(sb-thread:wait-on-semaphore sem1)
(with-timeout 10
(assert (eq 'cons (read-from-string "CL:CONS"))))
(sb-thread:signal-semaphore sem2)))
(with-test (:name :quick-name-conflict-resolution-import)
(let (p1 p2)
(unwind-protect
(progn
(setf p1 (make-package "QUICK-NAME-CONFLICT-RESOLUTION-IMPORT.1")
p2 (make-package "QUICK-NAME-CONFLICT-RESOLUTION-IMPORT.2"))
(intern "FOO" p1)
(handler-bind ((name-conflict (lambda (c)
(invoke-restart 'sb-impl::dont-import-it))))
(import (intern "FOO" p2) p1))
(assert (not (eq (intern "FOO" p1) (intern "FOO" p2))))
(handler-bind ((name-conflict (lambda (c)
(invoke-restart 'sb-impl::shadowing-import-it))))
(import (intern "FOO" p2) p1))
(assert (eq (intern "FOO" p1) (intern "FOO" p2))))
(when p1 (delete-package p1))
(when p2 (delete-package p2)))))
(with-test (:name :quick-name-conflict-resolution-export.1)
(let (p1 p2)
(unwind-protect
(progn
(setf p1 (make-package "QUICK-NAME-CONFLICT-RESOLUTION-EXPORT.1a")
p2 (make-package "QUICK-NAME-CONFLICT-RESOLUTION-EXPORT.2a"))
(intern "FOO" p1)
(use-package p2 p1)
(handler-bind ((name-conflict (lambda (c)
(invoke-restart 'sb-impl::keep-old))))
(export (intern "FOO" p2) p2))
(assert (not (eq (intern "FOO" p1) (intern "FOO" p2)))))
(when p1 (delete-package p1))
(when p2 (delete-package p2)))))
(with-test (:name :quick-name-conflict-resolution-export.2)
(let (p1 p2)
(unwind-protect
(progn
(setf p1 (make-package "QUICK-NAME-CONFLICT-RESOLUTION-EXPORT.1b")
p2 (make-package "QUICK-NAME-CONFLICT-RESOLUTION-EXPORT.2b"))
(intern "FOO" p1)
(use-package p2 p1)
(handler-bind ((name-conflict (lambda (c)
(invoke-restart 'sb-impl::take-new))))
(export (intern "FOO" p2) p2))
(assert (eq (intern "FOO" p1) (intern "FOO" p2))))
(when p1 (delete-package p1))
(when p2 (delete-package p2)))))
(with-test (:name :quick-name-conflict-resolution-use-package.1)
(let (p1 p2)
(unwind-protect
(progn
(setf p1 (make-package "QUICK-NAME-CONFLICT-RESOLUTION-USE-PACKAGE.1a")
p2 (make-package "QUICK-NAME-CONFLICT-RESOLUTION-USE-PACKAGE.2a"))
(intern "FOO" p1)
(intern "BAR" p1)
(export (intern "FOO" p2) p2)
(export (intern "BAR" p2) p2)
(handler-bind ((name-conflict (lambda (c)
(invoke-restart 'sb-impl::keep-old))))
(use-package p2 p1))
(assert (not (eq (intern "FOO" p1) (intern "FOO" p2))))
(assert (not (eq (intern "BAR" p1) (intern "BAR" p2)))))
(when p1 (delete-package p1))
(when p2 (delete-package p2)))))
(with-test (:name :quick-name-conflict-resolution-use-package.2)
(let (p1 p2)
(unwind-protect
(progn
(setf p1 (make-package "QUICK-NAME-CONFLICT-RESOLUTION-USE-PACKAGE.1b")
p2 (make-package "QUICK-NAME-CONFLICT-RESOLUTION-USE-PACKAGE.2b"))
(intern "FOO" p1)
(intern "BAR" p1)
(export (intern "FOO" p2) p2)
(export (intern "BAR" p2) p2)
(handler-bind ((name-conflict (lambda (c)
(invoke-restart 'sb-impl::take-new))))
(use-package p2 p1))
(assert (eq (intern "FOO" p1) (intern "FOO" p2)))
(assert (eq (intern "BAR" p1) (intern "BAR" p2))))
(when p1 (delete-package p1))
(when p2 (delete-package p2)))))
(with-test (:name (:package-at-variance-restarts :shadow))
(let ((p nil)
(*on-package-variance* '(:error t)))
(unwind-protect
(progn
(setf p (eval `(defpackage :package-at-variance-restarts.1
(:use :cl)
(:shadow "CONS"))))
(handler-bind ((sb-kernel::package-at-variance-error
(lambda (c)
(invoke-restart 'sb-impl::keep-them))))
(eval `(defpackage :package-at-variance-restarts.1
(:use :cl))))
(assert (not (eq 'cl:cons (intern "CONS" p))))
(handler-bind ((sb-kernel::package-at-variance-error
(lambda (c)
(invoke-restart 'sb-impl::drop-them))))
(eval `(defpackage :package-at-variance-restarts.1
(:use :cl))))
(assert (eq 'cl:cons (intern "CONS" p))))
(when p (delete-package p)))))
(with-test (:name (:package-at-variance-restarts :use))
(let ((p nil)
(*on-package-variance* '(:error t)))
(unwind-protect
(progn
(setf p (eval `(defpackage :package-at-variance-restarts.2
(:use :cl))))
(handler-bind ((sb-kernel::package-at-variance-error
(lambda (c)
(invoke-restart 'sb-impl::keep-them))))
(eval `(defpackage :package-at-variance-restarts.2
(:use))))
(assert (eq 'cl:cons (intern "CONS" p)))
(handler-bind ((sb-kernel::package-at-variance-error
(lambda (c)
(invoke-restart 'sb-impl::drop-them))))
(eval `(defpackage :package-at-variance-restarts.2
(:use))))
(assert (not (eq 'cl:cons (intern "CONS" p)))))
(when p (delete-package p)))))
(with-test (:name (:package-at-variance-restarts :export))
(let ((p nil)
(*on-package-variance* '(:error t)))
(unwind-protect
(progn
(setf p (eval `(defpackage :package-at-variance-restarts.4
(:export "FOO"))))
(handler-bind ((sb-kernel::package-at-variance-error
(lambda (c)
(invoke-restart 'sb-impl::keep-them))))
(eval `(defpackage :package-at-variance-restarts.4)))
(assert (eq :external (nth-value 1 (find-symbol "FOO" p))))
(handler-bind ((sb-kernel::package-at-variance-error
(lambda (c)
(invoke-restart 'sb-impl::drop-them))))
(eval `(defpackage :package-at-variance-restarts.4)))
(assert (eq :internal (nth-value 1 (find-symbol "FOO" p)))))
(when p (delete-package p)))))
(with-test (:name (:package-at-variance-restarts :implement))
(let ((p nil)
(*on-package-variance* '(:error t)))
(unwind-protect
(progn
(setf p (eval `(defpackage :package-at-variance-restarts.5
(:implement :sb-int))))
(handler-bind ((sb-kernel::package-at-variance-error
(lambda (c)
(invoke-restart 'sb-impl::keep-them))))
(eval `(defpackage :package-at-variance-restarts.5)))
(assert (member p (package-implemented-by-list :sb-int)))
(handler-bind ((sb-kernel::package-at-variance-error
(lambda (c)
(invoke-restart 'sb-impl::drop-them))))
(eval `(defpackage :package-at-variance-restarts.5)))
(assert (not (member p (package-implemented-by-list :sb-int)))))
(when p (delete-package p)))))
(with-test (:name (:delete-package :implementation-package))
(let (p1 p2)
(unwind-protect
(progn
(setf p1 (make-package "DELETE-PACKAGE/IMPLEMENTATION-PACKAGE.1")
p2 (make-package "DELETE-PACKAGE/IMPLEMENTATION-PACKAGE.2"))
(add-implementation-package p2 p1)
(assert (= 1 (length (package-implemented-by-list p1))))
(delete-package p2)
(assert (= 0 (length (package-implemented-by-list p1)))))
(when p1 (delete-package p1))
(when p2 (delete-package p2)))))
(with-test (:name (:delete-package :implementated-package))
(let (p1 p2)
(unwind-protect
(progn
(setf p1 (make-package "DELETE-PACKAGE/IMPLEMENTED-PACKAGE.1")
p2 (make-package "DELETE-PACKAGE/IMPLEMENTED-PACKAGE.2"))
(add-implementation-package p2 p1)
(assert (= 1 (length (package-implements-list p2))))
(delete-package p1)
(assert (= 0 (length (package-implements-list p2)))))
(when p1 (delete-package p1))
(when p2 (delete-package p2)))))
(with-test (:name :package-local-nicknames)
;; Clear slate
(without-package-locks
(when (find-package :package-local-nicknames-test-1)
(delete-package :package-local-nicknames-test-1))
(when (find-package :package-local-nicknames-test-2)
(delete-package :package-local-nicknames-test-2)))
(eval `(defpackage :package-local-nicknames-test-1
(:local-nicknames (:l :cl) (:sb :sb-ext))))
(eval `(defpackage :package-local-nicknames-test-2
(:export "CONS")))
;; Introspection
(let ((alist (package-local-nicknames :package-local-nicknames-test-1)))
(assert (equal (cons "L" (find-package "CL")) (assoc "L" alist :test 'string=)))
(assert (equal (cons "SB" (find-package "SB-EXT")) (assoc "SB" alist :test 'string=)))
(assert (eql 2 (length alist))))
;; Usage
(let ((*package* (find-package :package-local-nicknames-test-1)))
(let ((cons0 (read-from-string "L:CONS"))
(exit0 (read-from-string "SB:EXIT"))
(cons1 (find-symbol "CONS" :l))
(exit1 (find-symbol "EXIT" :sb))
(cl (find-package :l))
(sb (find-package :sb)))
(assert (eq 'cons cons0))
(assert (eq 'cons cons1))
(assert (equal "L:CONS" (prin1-to-string cons0)))
(assert (eq 'sb-ext:exit exit0))
(assert (eq 'sb-ext:exit exit1))
(assert (equal "SB:EXIT" (prin1-to-string exit0)))
(assert (eq cl (find-package :common-lisp)))
(assert (eq sb (find-package :sb-ext)))))
;; Can't add same name twice for different global names.
(assert (eq :oopsie
(handler-case
(add-package-local-nickname :l :package-local-nicknames-test-2
:package-local-nicknames-test-1)
(error ()
:oopsie))))
;; But same name twice is OK.
(add-package-local-nickname :l :cl :package-local-nicknames-test-1)
;; Removal.
(assert (remove-package-local-nickname :l :package-local-nicknames-test-1))
(let ((*package* (find-package :package-local-nicknames-test-1)))
(let ((exit0 (read-from-string "SB:EXIT"))
(exit1 (find-symbol "EXIT" :sb))
(sb (find-package :sb)))
(assert (eq 'sb-ext:exit exit0))
(assert (eq 'sb-ext:exit exit1))
(assert (equal "SB:EXIT" (prin1-to-string exit0)))
(assert (eq sb (find-package :sb-ext)))
(assert (not (find-package :l)))))
;; Adding back as another package.
(assert (eq (find-package :package-local-nicknames-test-1)
(add-package-local-nickname :l :package-local-nicknames-test-2
:package-local-nicknames-test-1)))
(let ((*package* (find-package :package-local-nicknames-test-1)))
(let ((cons0 (read-from-string "L:CONS"))
(exit0 (read-from-string "SB:EXIT"))
(cons1 (find-symbol "CONS" :l))
(exit1 (find-symbol "EXIT" :sb))
(cl (find-package :l))
(sb (find-package :sb)))
(assert (eq cons0 cons1))
(assert (not (eq 'cons cons0)))
(assert (eq (find-symbol "CONS" :package-local-nicknames-test-2)
cons0))
(assert (equal "L:CONS" (prin1-to-string cons0)))
(assert (eq 'sb-ext:exit exit0))
(assert (eq 'sb-ext:exit exit1))
(assert (equal "SB:EXIT" (prin1-to-string exit0)))
(assert (eq cl (find-package :package-local-nicknames-test-2)))
(assert (eq sb (find-package :sb-ext)))))
;; Interaction with package locks.
(lock-package :package-local-nicknames-test-1)
(assert (eq :package-oopsie
(handler-case
(add-package-local-nickname :c :sb-c :package-local-nicknames-test-1)
(package-lock-violation ()
:package-oopsie))))
(assert (eq :package-oopsie
(handler-case
(remove-package-local-nickname :l :package-local-nicknames-test-1)
(package-lock-violation ()
:package-oopsie))))
(unlock-package :package-local-nicknames-test-1)
(add-package-local-nickname :c :sb-c :package-local-nicknames-test-1)
(remove-package-local-nickname :l :package-local-nicknames-test-1))
(defmacro with-tmp-packages (bindings &body body)
`(let ,(mapcar #'car bindings)
(unwind-protect
(progn
(setf ,@(apply #'append bindings))
,@body)
,@(mapcar (lambda (p)
`(when ,p (delete-package ,p)))
(mapcar #'car bindings)))))
(with-test (:name (:delete-package :locally-nicknames-others))
(with-tmp-packages ((p1 (make-package "LOCALLY-NICKNAMES-OTHERS"))
(p2 (make-package "LOCALLY-NICKNAMED-BY-OTHERS")))
(add-package-local-nickname :foo p2 p1)
(assert (equal (list p1) (package-locally-nicknamed-by-list p2)))
(delete-package p1)
(assert (not (package-locally-nicknamed-by-list p2)))))
(with-test (:name (:delete-package :locally-nicknamed-by-others))
(with-tmp-packages ((p1 (make-package "LOCALLY-NICKNAMES-OTHERS"))
(p2 (make-package "LOCALLY-NICKNAMED-BY-OTHERS")))
(add-package-local-nickname :foo p2 p1)
(assert (package-local-nicknames p1))
(delete-package p2)
(assert (not (package-local-nicknames p1)))))
(with-test (:name :own-name-as-local-nickname)
(with-tmp-packages ((p1 (make-package "OWN-NAME-AS-NICKNAME1"))
(p2 (make-package "OWN-NAME-AS-NICKNAME2")))
(assert (eq :oops
(handler-case
(add-package-local-nickname :own-name-as-nickname1 p2 p1)
(error ()
:oops))))
(handler-bind ((error #'continue))
(add-package-local-nickname :own-name-as-nickname1 p2 p1))
(assert (eq (intern "FOO" p2)
(let ((*package* p1))
(intern "FOO" :own-name-as-nickname1))))))
(with-test (:name :own-nickname-as-local-nickname)
(with-tmp-packages ((p1 (make-package "OWN-NICKNAME-AS-NICKNAME1"
:nicknames '("OWN-NICKNAME")))
(p2 (make-package "OWN-NICKNAME-AS-NICKNAME2")))
(assert (eq :oops
(handler-case
(add-package-local-nickname :own-nickname p2 p1)
(error ()
:oops))))
(handler-bind ((error #'continue))
(add-package-local-nickname :own-nickname p2 p1))
(assert (eq (intern "FOO" p2)
(let ((*package* p1))
(intern "FOO" :own-nickname))))))
(with-test (:name :delete-package-restart)
(let* (ok
(result
(handler-bind
((sb-kernel:simple-package-error
(lambda (c)
(setf ok t)
(continue c))))
(delete-package (gensym)))))
(assert ok)
(assert (not result))))
;; WITH-PACKAGE-ITERATOR isn't well-exercised by tests (though LOOP uses it)
;; so here's a basic correctness test with some complications involving
;; shadowing symbols.
(make-package "P1" :use '("SB-FORMAT"))
(make-package "P2")
(export 'p1::foo 'p1)
(shadow "FORMAT-ERROR" 'p1)
(make-package "A" :use '("SB-FORMAT" "P1" "P2"))
(shadow '("PROG2" "FOO") 'a)
(intern "BLAH" "P2")
(export 'p2::(foo bar baz) 'p2)
(export 'a::goodfun 'a)
(with-test (:name :with-package-iterator)
(let ((tests '((:internal) (:external) (:inherited)
(:internal :inherited)
(:internal :external)
(:external :inherited)
(:internal :external :inherited)))
(maximum-answer
'(;; symbols visible in A
(a::prog2 :internal "A")
(a::foo :internal "A")
(a:goodfun :external "A")
(p2:bar :inherited "A")
(p2:baz :inherited "A")
(sb-format:%compiler-walk-format-string :inherited "A")
(sb-format:format-error :inherited "A")
;; ... P1
(p1:foo :external "P1")
(p1::format-error :internal "P1")
(sb-format:%compiler-walk-format-string :inherited "P1")
;; ... P2
(p2::blah :internal "P2")
(p2:foo :external "P2")
(p2:bar :external "P2")
(p2:baz :external "P2"))))
;; Compile a new function to test each combination of
;; accessibility-kind since the macro doesn't eval them.
(dolist (access tests)
; (print `(testing ,access))
(let ((f (compile
nil
`(lambda ()
(with-package-iterator (iter '(p1 a p2) ,@access)
(let (res)
(loop
(multiple-value-bind (foundp sym access pkg) (iter)
(if foundp
(push (list sym access (package-name pkg)) res)
(return))))
res))))))
(let ((answer (funcall f))
(expect (remove-if-not (lambda (x) (member (second x) access))
maximum-answer)))
;; exactly as many results as expected
(assert (equal (length answer) (length expect)))
;; each result is right
(assert (equal (length (intersection answer expect :test #'equal))
(length expect))))))))
(with-test (:name :export-inaccessible-lookalike)
(make-package "E1")
(make-package "E2")
(export (intern "A" "E2") 'e2)
(multiple-value-bind (answer condition)
(ignore-errors (export (intern "A" "E1") 'e2))
(assert (and (not answer)
(and (typep condition 'sb-kernel:simple-package-error)
(search "not accessible"
(simple-condition-format-control condition)))))))