eval-when.lsp 3.87 KB
Newer Older
pfdietz's avatar
pfdietz committed
1 2 3 4 5 6 7 8 9 10 11 12 13
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Apr  6 17:00:30 2003
;;;; Contains: Tests for EVAL-WHEN

;;; The following test was suggested by Sam Steingold,
;;; so I've created this file to hold it.

(in-package :cl-test)

(defvar *eval-when.1-collector*)

(deftest eval-when.1
sds's avatar
sds committed
14

pfdietz's avatar
pfdietz committed
15 16 17
  (let ((forms nil) all (ff "generated-eval-when-test-file.lisp"))
    (dolist (c '(nil (:compile-toplevel)))
      (dolist (l '(nil (:load-toplevel)))
sds's avatar
sds committed
18 19 20 21
        (dolist (x '(nil (:execute)))
          (push `(eval-when (,@c ,@l ,@x)
                   (push '(,@c ,@l ,@x) *eval-when.1-collector*))
                forms))))
pfdietz's avatar
pfdietz committed
22 23
    (dolist (c '(nil (:compile-toplevel)))
      (dolist (l '(nil (:load-toplevel)))
sds's avatar
sds committed
24 25 26 27
        (dolist (x '(nil (:execute)))
          (push `(let () (eval-when (,@c ,@l ,@x)
                           (push '(let ,@c ,@l ,@x) *eval-when.1-collector*)))
                forms))))
28
    (with-open-file (o ff :direction :output :if-exists :supersede)
sds's avatar
sds committed
29 30 31
                    (dolist (f forms)
                      (prin1 f o)
                      (terpri o)))
pfdietz's avatar
pfdietz committed
32 33 34 35 36 37 38 39 40 41 42 43 44
    (let ((*eval-when.1-collector* nil))
      (load ff)
      (push (cons "load source" *eval-when.1-collector*) all))
    (let ((*eval-when.1-collector* nil))
      (compile-file ff)
      (push (cons "compile source" *eval-when.1-collector*) all))
    (let ((*eval-when.1-collector* nil))
      (load (compile-file-pathname ff))
      (push (cons "load compiled" *eval-when.1-collector*) all))
    (delete-file ff)
    (delete-file (compile-file-pathname ff))
    #+clisp (delete-file (make-pathname :type "lib" :defaults ff))
    (nreverse all))
sds's avatar
sds committed
45

pfdietz's avatar
pfdietz committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  (("load source"
    (:execute) (:load-toplevel :execute) (:compile-toplevel :execute)
    (:compile-toplevel :load-toplevel :execute)
    (let :execute) (let :load-toplevel :execute)
    (let :compile-toplevel :execute)
    (let :compile-toplevel :load-toplevel :execute))
   ("compile source"
    (:compile-toplevel) (:compile-toplevel :execute)
    (:compile-toplevel :load-toplevel)
    (:compile-toplevel :load-toplevel :execute))
   ("load compiled"
    (:load-toplevel) (:load-toplevel :execute)
    (:compile-toplevel :load-toplevel)
    (:compile-toplevel :load-toplevel :execute)
    (let :execute) (let :load-toplevel :execute)
    (let :compile-toplevel :execute)
    (let :compile-toplevel :load-toplevel :execute))))

;;; More EVAL-WHEN tests to go here

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
(deftest eval-when.2
  (eval-when () :bad)
  nil)

(deftest eval-when.3
  (eval-when (:execute))
  nil)

(deftest eval-when.4
  (eval-when (:execute) :good)
  :good)

(deftest eval-when.5
  (eval-when (:compile-toplevel) :bad)
  nil)

(deftest eval-when.6
  (eval-when (:load-toplevel) :bad)
  nil)

(deftest eval-when.7
  (eval-when (:compile-toplevel :execute) :good)
  :good)

(deftest eval-when.8
  (eval-when (:load-toplevel :execute) :good)
  :good)

(deftest eval-when.9
  (eval-when (:load-toplevel :compile-toplevel) :bad)
  nil)

(deftest eval-when.10
  (eval-when (:load-toplevel :compile-toplevel :execute) :good)
  :good)

(deftest eval-when.11
  (eval-when (:execute) (values 'a 'b 'c 'd))
  a b c d)

pfdietz's avatar
pfdietz committed
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
(deftest eval-when.12
  (let ((x :good))
    (values (eval-when (:load-toplevel) (setq x :bad)) x))
  nil :good)

(deftest eval-when.13
  (let ((x :good))
    (values (eval-when (:compile-toplevel) (setq x :bad)) x))
  nil :good)

(deftest eval-when.14
  (let ((x :bad))
    (values (eval-when (:execute) (setq x :good)) x))
  :good :good)

(deftest eval-when.15
  (let ((x :good))
    (values (eval-when (load) (setq x :bad)) x))
  nil :good)

(deftest eval-when.16
  (let ((x :good))
    (values (eval-when (compile) (setq x :bad)) x))
  nil :good)

(deftest eval-when.17
  (let ((x :bad))
    (values (eval-when (eval) (setq x :good)) x))
  :good :good)
135

136 137 138 139
;;; Macros are expanded in the appropriate environment

(deftest eval-when.18
  (macrolet ((%m (z) z))
sds's avatar
sds committed
140
            (eval-when (:execute) (expand-in-current-env (%m :good))))
141
  :good)