load.lsp 6.73 KB
Newer Older
pfdietz's avatar
pfdietz committed
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Tue Apr 12 21:51:49 2005
;;;; Contains: Tests of LOAD

6 7
(in-package :cl-test)

pfdietz's avatar
pfdietz committed
8
(defun load-file-test (file funname &rest args &key
sds's avatar
sds committed
9 10 11 12 13 14
                            if-does-not-exist
                            (print nil print-p)
                            (verbose nil verbose-p)
                            (*load-print* nil)
                            (*load-verbose* nil)
                            external-format)
pfdietz's avatar
pfdietz committed
15
  (declare (ignorable external-format if-does-not-exist
sds's avatar
sds committed
16
                      print print-p verbose verbose-p))
pfdietz's avatar
pfdietz committed
17 18
  (fmakunbound funname)
  (let* ((str (make-array '(0) :element-type 'character :adjustable t
sds's avatar
sds committed
19 20 21 22 23 24 25
                          :fill-pointer 0))
         (vals (multiple-value-list
                (with-output-to-string
                  (*standard-output* str)
                  (apply #'load file :allow-other-keys t args))))
         (print? (if print-p print *load-print*))
         (verbose? (if verbose-p verbose *load-verbose*)))
pfdietz's avatar
pfdietz committed
26 27
      (values
       (let ((v1 (car vals))
sds's avatar
sds committed
28 29 30 31 32 33 34 35 36 37
             (v2 (or (and verbose-p (not verbose))
                     (and (not verbose-p) (not *load-verbose*))
                     (position #\; str)))
             (v3 (or (and print-p (not print))
                     (and (not print-p) (not *load-print*))
                     (> (length str) 0)))
             (v4 (if (or print? verbose?)
                     (> (length str) 0)
                   t)))
         (if (and (= (length vals) 1) v1 v2 v3 v4) t (list vals v2 v3 v4 str)))
pfdietz's avatar
pfdietz committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
       (funcall funname))))

(deftest load.1
  (load-file-test "compile-file-test-file.lsp" 'compile-file-test-fun.1)
  t nil)

(deftest load.2
  (load-file-test #p"compile-file-test-file.lsp" 'compile-file-test-fun.1)
  t nil)

(deftest load.3
  (with-input-from-string
   (s "(in-package :cl-test) (defun load-file-test-fun.2 () 'good)")
   (load-file-test s 'load-file-test-fun.2))
  t good)

(deftest load.4
  (load-file-test "compile-file-test-file.lsp" 'compile-file-test-fun.1
sds's avatar
sds committed
56
                  :external-format :default)
pfdietz's avatar
pfdietz committed
57 58 59 60
  t nil)

(deftest load.5
  (load-file-test "compile-file-test-file.lsp" 'compile-file-test-fun.1
sds's avatar
sds committed
61
                  :verbose t)
pfdietz's avatar
pfdietz committed
62 63 64 65
  t nil)

(deftest load.6
  (load-file-test "compile-file-test-file.lsp" 'compile-file-test-fun.1
sds's avatar
sds committed
66
                  :*load-verbose* t)
pfdietz's avatar
pfdietz committed
67 68 69 70
  t nil)

(deftest load.7
  (load-file-test "compile-file-test-file.lsp" 'compile-file-test-fun.1
sds's avatar
sds committed
71
                  :*load-verbose* t :verbose nil)
pfdietz's avatar
pfdietz committed
72 73 74 75 76 77 78 79 80 81
  t nil)

(deftest load.8
  (with-input-from-string
   (s "(in-package :cl-test) (defun load-file-test-fun.2 () 'good)")
   (load-file-test s 'load-file-test-fun.2 :verbose t))
  t good)

(deftest load.9
  (load-file-test "compile-file-test-file.lsp" 'compile-file-test-fun.1
sds's avatar
sds committed
82
                  :print t)
pfdietz's avatar
pfdietz committed
83 84 85 86
  t nil)

(deftest load.10
  (load-file-test "compile-file-test-file.lsp" 'compile-file-test-fun.1
sds's avatar
sds committed
87
                  :*load-print* t)
pfdietz's avatar
pfdietz committed
88 89 90 91
  t nil)

(deftest load.11
  (load-file-test "compile-file-test-file.lsp" 'compile-file-test-fun.1
sds's avatar
sds committed
92
                  :*load-print* t :print nil)
pfdietz's avatar
pfdietz committed
93 94 95 96
  t nil)

(deftest load.12
  (load-file-test "compile-file-test-file.lsp" 'compile-file-test-fun.1
sds's avatar
sds committed
97
                  :*load-print* nil :print t)
pfdietz's avatar
pfdietz committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  t nil)

(deftest load.13
  (with-input-from-string
   (s "(in-package :cl-test) (defun load-file-test-fun.2 () 'good)")
   (load-file-test s 'load-file-test-fun.2 :print t))
  t good)

(deftest load.14
  (load "nonexistent-file.lsp" :if-does-not-exist nil)
  nil)

(defpackage LOAD-TEST-PACKAGE (:use "COMMON-LISP"))

(deftest load.15
  (let ((*package* (find-package "LOAD-TEST-PACKAGE")))
    (with-input-from-string
     (s "(defun f () 'good)")
     (load-file-test s 'load-test-package::f)))
  t load-test-package::good)

119 120 121 122 123 124 125 126 127 128
(deftest load.15a
  (let ((*package* (find-package "CL-TEST")))
    (values
     (with-input-from-string
      (s "(eval-when (:load-toplevel :execute) (setq *package* (find-package \"LOAD-TEST-PACKAGE\")))
          (defun f () 'good)")
      (multiple-value-list (load-file-test s 'load-test-package::f)))
     (read-from-string "GOOD")))
  (t load-test-package::good) good)

pfdietz's avatar
pfdietz committed
129 130 131 132 133 134 135 136
(deftest load.16
  (let ((*readtable* (copy-readtable nil)))
    (set-macro-character #\! (get-macro-character #\'))
    (with-input-from-string
     (s "(in-package :cl-test) (defun load-file-test-fun.3 () !good)")
     (load-file-test s 'load-file-test-fun.3)))
  t good)

137 138
(deftest load.16a
  (let ((*readtable* *readtable*)
sds's avatar
sds committed
139
        (*package* (find-package "CL-TEST")))
140 141 142 143 144 145 146 147 148 149 150 151
    (values
     (with-input-from-string
      (s "(in-package :cl-test)
         (eval-when (:load-toplevel :execute)
            (setq *readtable* (copy-readtable nil))
            (set-macro-character #\\! (get-macro-character #\\')))
         (defun load-file-test-fun.3 () !good)")
      (multiple-value-list
       (load-file-test s 'load-file-test-fun.3)))
     (read-from-string "!FOO")))
  (t good) !FOO)

pfdietz's avatar
pfdietz committed
152 153
(deftest load.17
  (let ((file #p"load-test-file.lsp"))
pfdietz's avatar
pfdietz committed
154 155
    (fmakunbound 'load-file-test-fun.1)
    (fmakunbound 'load-file-test-fun.2)
pfdietz's avatar
pfdietz committed
156 157 158
    (values
     (notnot (load file))
     (let ((p1 (pathname (merge-pathnames file)))
sds's avatar
sds committed
159
           (p2 (funcall 'load-file-test-fun.1)))
160
       (equalpt-or-report p1 p2))
pfdietz's avatar
pfdietz committed
161
     (let ((p1 (truename file))
sds's avatar
sds committed
162
           (p2 (funcall 'load-file-test-fun.2)))
163
       (equalpt-or-report p1 p2))))
pfdietz's avatar
pfdietz committed
164 165 166 167 168 169 170
  t t t)

;;; Test that the load pathname/truename variables are bound
;;; properly when loading compiled files

(deftest load.18
  (let* ((file "load-test-file-2.lsp")
sds's avatar
sds committed
171
         (target (enough-namestring (compile-file-pathname file))))
pfdietz's avatar
pfdietz committed
172 173 174 175 176 177 178
    (declare (special *load-test-var.1* *load-test-var.2*))
    (compile-file file)
    (makunbound '*load-test-var.1*)
    (makunbound '*load-test-var.2*)
    (load target)
    (values
     (let ((p1 (pathname (merge-pathnames target)))
sds's avatar
sds committed
179
           (p2 *load-test-var.1*))
180
       (equalpt-or-report p1 p2))
pfdietz's avatar
pfdietz committed
181
     (let ((p1 (truename target))
sds's avatar
sds committed
182
           (p2 *load-test-var.2*))
183
       (equalpt-or-report p1 p2))))
pfdietz's avatar
pfdietz committed
184 185
  t t)

pfdietz's avatar
pfdietz committed
186 187
(deftest load.19
  (let ((file (logical-pathname "CLTEST:LDTEST.LSP"))
sds's avatar
sds committed
188 189
        (fn 'load-test-fun-3)
        (*package* (find-package "CL-TEST")))
pfdietz's avatar
pfdietz committed
190 191
    (with-open-file
     (s file :direction :output :if-exists :supersede
sds's avatar
sds committed
192
        :if-does-not-exist :create)
pfdietz's avatar
pfdietz committed
193 194 195 196 197 198
     (format s "(in-package :cl-test) (defun ~a () :foo)" fn))
    (fmakunbound fn)
    (values
     (notnot (load file))
     (funcall fn)))
  t :foo)
pfdietz's avatar
pfdietz committed
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213
;;; Defaults of the load variables

(deftest load-pathname.1
  *load-pathname*
  nil)

(deftest load-truename.1
  *load-truename*
  nil)

(deftest load-print.1
  *load-print*
  nil)

pfdietz's avatar
pfdietz committed
214 215 216 217 218 219 220 221 222
;;; Error tests

(deftest load.error.1
  (signals-error (load "nonexistent-file.lsp") file-error)
  t)

(deftest load.error.2
  (signals-error (load) program-error)
  t)
223

pfdietz's avatar
pfdietz committed
224 225
(deftest load.error.3
  (signals-error (load "compile-file-test-file.lsp" :bad-key-arg t)
sds's avatar
sds committed
226
                 program-error)
pfdietz's avatar
pfdietz committed
227
  t)