Fork of https://github.com/google/lisp-koans so that I could go through them. THIS CONTAINS ANSWERS.

lisp-unit.lsp 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. ;;;-*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
  2. #|
  3. This version of lisp-unit.lsp has been extended to support the lisp koans.
  4. Specifically, it is an unnamed branch from
  5. https://github.com/OdonataResearchLLC/lisp-unit/
  6. with hash 93d07b2fa6e32364916225f6218e9e7313027c1f
  7. Modifications were made to:
  8. 1) Support *incomplete* tests in addition to *passing* and *failing* ones
  9. 2) End test execution at the first non-passing test.
  10. |#
  11. #|
  12. Copyright (c) 2004-2005 Christopher K. Riesbeck
  13. Permission is hereby granted, free of charge, to any person obtaining
  14. a copy of this software and associated documentation files (the "Software"),
  15. to deal in the Software without restriction, including without limitation
  16. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. and/or sell copies of the Software, and to permit persons to whom the
  18. Software is furnished to do so, subject to the following conditions:
  19. The above copyright notice and this permission notice shall be included
  20. in all copies or substantial portions of the Software.
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  25. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  26. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. OTHER DEALINGS IN THE SOFTWARE.
  28. How to use
  29. ----------
  30. 1. Read the documentation at:
  31. https://github.com/OdonataResearchLLC/lisp-unit/wiki
  32. 2. Make a file of DEFINE-TEST's. See exercise-tests.lisp for many
  33. examples. If you want, start your test file with (REMOVE-TESTS :ALL)
  34. to clear any previously defined tests.
  35. 3. Load this file.
  36. 4. (use-package :lisp-unit)
  37. 5. Load your code file and your file of tests.
  38. 6. Test your code with (RUN-TESTS '(test-name1 test-name2 ...)) or
  39. simply (RUN-TESTS :ALL) to run all defined tests.
  40. A summary of how many tests passed and failed will be printed.
  41. NOTE: Nothing is compiled until RUN-TESTS is expanded. Redefining
  42. functions or even macros does not require reloading any tests.
  43. |#
  44. #|
  45. Copyright 2013 Google Inc.
  46. Licensed under the Apache License, Version 2.0 (the "License");
  47. you may not use this file except in compliance with the License.
  48. You may obtain a copy of the License at
  49. http://www.apache.org/licenses/LICENSE-2.0
  50. Unless required by applicable law or agreed to in writing, software
  51. distributed under the License is distributed on an "AS IS" BASIS,
  52. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  53. See the License for the specific language governing permissions and
  54. limitations under the License.
  55. |#
  56. ;;; Packages
  57. (in-package :cl-user)
  58. (defpackage :lisp-unit
  59. (:use :common-lisp)
  60. ;; Print parameters
  61. (:export :*print-summary*
  62. :*print-failures*
  63. :*print-errors*
  64. :*proceed-after-failure*)
  65. ;; Forms for assertions
  66. (:export :assert-eq
  67. :assert-eql
  68. :assert-equal
  69. :true-or-false?
  70. :assert-equalp
  71. :assert-equality
  72. :assert-prints
  73. :assert-expands
  74. :assert-true
  75. :assert-false
  76. :assert-error)
  77. ;; Functions for managing tests
  78. (:export :define-test
  79. :list-tests
  80. :test-code
  81. :test-documentation
  82. :remove-tests
  83. :run-tests
  84. :run-koans
  85. :use-debugger)
  86. ;; Functions for managing tags
  87. (:export :list-tags
  88. :tagged-tests
  89. :remove-tags
  90. :run-tags)
  91. ;; Constants for blanks in koans
  92. (:export __
  93. ___
  94. ____
  95. +blanks+)
  96. ;; Functions for reporting test results
  97. (:export :test-names
  98. :failed-tests
  99. :incomplete-tests
  100. :error-tests
  101. :missing-tests
  102. :summarize-results
  103. :any-non-pass-p)
  104. ;; Utility predicates
  105. (:export :logically-equal :set-equal))
  106. (in-package :lisp-unit)
  107. ;; blank constants allow the incomplete tests to compile without errors
  108. (defconstant __ :blank-value)
  109. (defconstant ___ :blank-value)
  110. (defconstant ____ :blank-value)
  111. (defconstant +blanks+ '(__ ___ ____))
  112. (defconstant +blank-value+ 'BLANK-VALUE)
  113. ;;; Global counters
  114. (defparameter *pass* 0
  115. "The number of passed assertions.")
  116. (defparameter *fail* 0
  117. "The number of failed assertions.")
  118. (defparameter *incomplete* 0
  119. "The number of incomplete assertions.")
  120. (defparameter *koan-assert-list* nil
  121. "The record of a single koan.")
  122. ;;; Global options
  123. (defparameter *proceed-after-failure* nil
  124. "set to nil for normal operation. t will eval every koan")
  125. (defparameter *print-summary* nil
  126. "Print a summary of the pass, fail, and error count if non-nil.")
  127. (defparameter *print-failures* nil
  128. "Print failure messages if non-NIL.")
  129. (defparameter *print-errors* nil
  130. "Print error messages if non-NIL.")
  131. (defparameter *use-debugger* nil
  132. "If not NIL, enter the debugger when an error is encountered in an
  133. assertion.")
  134. (defun use-debugger-p (condition)
  135. "Debug or ignore errors."
  136. (cond
  137. ((eq :ask *use-debugger*)
  138. (y-or-n-p "~A -- debug?" condition))
  139. (*use-debugger*)))
  140. (defun use-debugger (&optional (flag t))
  141. "Use the debugger when testing, or not."
  142. (setq *use-debugger* flag))
  143. ;;; Failure control strings
  144. (defgeneric print-failure (type form expected actual extras)
  145. (:documentation
  146. "Report the details of the failure assertion."))
  147. (defmethod print-failure :around (type form expected actual extras)
  148. "Failure header and footer output."
  149. (declare (ignore expected actual))
  150. (format t "~& | Failed Form: ~S" form)
  151. (call-next-method)
  152. (when extras
  153. (format t "~{~& | ~S => ~S~}~%" (funcall extras)))
  154. (format t "~& |~%")
  155. type)
  156. (defmethod print-failure (type form expected actual extras)
  157. (declare (ignore type form extras))
  158. (format t "~& | Expected ~{~S~^; ~} " expected)
  159. (format t "~<~% | ~:;but saw ~{~S~^; ~}~>" actual))
  160. (defmethod print-failure ((type (eql :error))
  161. form expected actual extras)
  162. (declare (ignore form extras))
  163. (format t "~& | ~@[Should have signalled ~{~S~^; ~} but saw~]"
  164. expected)
  165. (format t " ~{~S~^; ~}" actual))
  166. (defmethod print-failure ((type (eql :macro))
  167. form expected actual extras)
  168. (declare (ignore form extras))
  169. (format t "~& | Should have expanded to ~{~S~^; ~} " expected)
  170. (format t "~<~%~:;but saw ~{~S~^; ~}~>" actual))
  171. (defmethod print-failure ((type (eql :output))
  172. form expected actual extras)
  173. (declare (ignore form extras))
  174. (format t "~& | Should have printed ~{~S~^; ~} " expected)
  175. (format t "~<~%~:;but saw ~{~S~^; ~}~>" actual))
  176. (defun print-error (condition)
  177. "Print the error condition."
  178. (let ((*print-escape* nil))
  179. (format t "~& | Execution error:~% | ~W" condition)
  180. (format t "~& |~%")))
  181. (defun print-summary (name pass fail incomplete &optional exerr)
  182. "Print a summary of the test results."
  183. (format t "~&~A: ~S assertions passed, ~S failed, ~S incomplete"
  184. name pass fail incomplete)
  185. (format t "~@[, ~S execution errors~].~2%" exerr))
  186. ;;; Global unit test database
  187. (defparameter *test-db* (make-hash-table :test #'eq)
  188. "The unit test database is simply a hash table.")
  189. (defun package-table (package &optional create)
  190. (cond
  191. ((gethash (find-package package) *test-db*))
  192. (create
  193. (setf (gethash package *test-db*) (make-hash-table)))
  194. (t (warn "No tests defined for package: ~S" package))))
  195. ;;; Global tags database
  196. (defparameter *tag-db* (make-hash-table :test #'eq)
  197. "The tag database is simply a hash table.")
  198. (defun package-tags (package &optional create)
  199. "Return the tags DB for the package."
  200. (cond
  201. ((gethash (find-package package) *tag-db*))
  202. (create
  203. (setf (gethash package *tag-db*) (make-hash-table)))
  204. (t (warn "No tags defined for package: ~S" package))))
  205. (defclass unit-test ()
  206. ((doc
  207. :type string
  208. :initarg :doc
  209. :reader doc)
  210. (code
  211. :type list
  212. :initarg :code
  213. :reader code))
  214. (:default-initargs :doc "" :code ())
  215. (:documentation
  216. "Organize the unit test documentation and code."))
  217. ;;; NOTE: Shamelessly taken from PG's analyze-body
  218. (defun parse-body (body &optional doc tag)
  219. "Separate the components of the body."
  220. (let ((item (first body)))
  221. (cond
  222. ((and (listp item) (eq :tag (first item)))
  223. (parse-body (rest body) doc (nconc (rest item) tag)))
  224. ((and (stringp item) (not doc) (rest body))
  225. (if tag
  226. (values doc tag (rest body))
  227. (parse-body (rest body) doc tag)))
  228. (t (values doc tag body)))))
  229. (defmacro define-test (name &body body)
  230. "Store the test in the test database."
  231. (multiple-value-bind (doc tag code) (parse-body body)
  232. `(let ((doc (or ,doc (string ',name))))
  233. (setf
  234. ;; Unit test
  235. (gethash ',name (package-table *package* t))
  236. (make-instance 'unit-test :doc doc :code ',code))
  237. ;; Tags
  238. (loop for tag in ',tag do
  239. (pushnew
  240. ',name (gethash tag (package-tags *package* t))))
  241. ;; Return the name of the test
  242. ',name)))
  243. ;;; Manage tests
  244. (defun list-tests (&optional (package *package*))
  245. "Return a list of the tests in package."
  246. (let ((table (package-table package)))
  247. (when table
  248. (loop for test-name being each hash-key in table
  249. collect test-name))))
  250. (defun test-documentation (name &optional (package *package*))
  251. "Return the documentation for the test."
  252. (let ((unit-test (gethash name (package-table package))))
  253. (if (null unit-test)
  254. (warn "No code defined for test ~A in package ~S."
  255. name package)
  256. (doc unit-test))))
  257. (defun test-code (name &optional (package *package*))
  258. "Returns the code stored for the test name."
  259. (let ((unit-test (gethash name (package-table package))))
  260. (if (null unit-test)
  261. (warn "No code defined for test ~A in package ~S."
  262. name package)
  263. (code unit-test))))
  264. (defun remove-tests (names &optional (package *package*))
  265. "Remove individual tests or entire sets."
  266. (if (eq :all names)
  267. (if (null package)
  268. (clrhash *test-db*)
  269. (progn
  270. (remhash (find-package package) *test-db*)
  271. (remhash (find-package package) *tag-db*)))
  272. (let ((table (package-table package)))
  273. (unless (null table)
  274. ;; Remove tests
  275. (loop for name in names
  276. always (remhash name table)
  277. collect name into removed
  278. finally (return removed))
  279. ;; Remove tests from tags
  280. (loop with tags = (package-tags package)
  281. for tag being each hash-key in tags
  282. using (hash-value tagged-tests)
  283. do
  284. (setf
  285. (gethash tag tags)
  286. (set-difference tagged-tests names)))))))
  287. ;;; Manage tags
  288. (defun %tests-from-all-tags (&optional (package *package*))
  289. "Return all of the tests that have been tagged."
  290. (loop for tests being each hash-value in (package-tags package)
  291. nconc (copy-list tests) into all-tests
  292. finally (return (delete-duplicates all-tests))))
  293. (defun %tests-from-tags (tags &optional (package *package*))
  294. "Return the tests associated with the tags."
  295. (loop with table = (package-tags package)
  296. for tag in tags
  297. as tests = (gethash tag table)
  298. nconc (copy-list tests) into all-tests
  299. finally (return (delete-duplicates all-tests))))
  300. (defun list-tags (&optional (package *package*))
  301. "Return a list of the tags in package."
  302. (let ((tags (package-tags package)))
  303. (when tags
  304. (loop for tag being each hash-key in tags collect tag))))
  305. (defun tagged-tests (tags &optional (package *package*))
  306. "Run the tests associated with the specified tags in package."
  307. (if (eq :all tags)
  308. (%tests-from-all-tags package)
  309. (%tests-from-tags tags package)))
  310. (defun remove-tags (tags &optional (package *package*))
  311. "Remove individual tags or entire sets."
  312. (if (eq :all tags)
  313. (if (null package)
  314. (clrhash *tag-db*)
  315. (remhash (find-package package) *tag-db*))
  316. (let ((table (package-tags package)))
  317. (unless (null table)
  318. (loop for tag in tags
  319. always (remhash tag table)
  320. collect tag into removed
  321. finally (return removed))))))
  322. ;;; Assert macros
  323. (defmacro assert-eq (expected form &rest extras)
  324. "Assert whether expected and form are EQ."
  325. `(expand-assert :equal ,form ,form ,expected ,extras :test #'eq))
  326. (defmacro assert-eql (expected form &rest extras)
  327. "Assert whether expected and form are EQL."
  328. `(expand-assert :equal ,form ,form ,expected ,extras :test #'eql))
  329. (defmacro assert-equal (expected form &rest extras)
  330. "Assert whether expected and form are EQUAL."
  331. `(expand-assert :equal ,form ,form ,expected ,extras :test #'equal))
  332. (defmacro assert-equalp (expected form &rest extras)
  333. "Assert whether expected and form are EQUALP."
  334. `(expand-assert :equal ,form ,form ,expected ,extras :test #'equalp))
  335. (defmacro true-or-false? (expected form &rest extras)
  336. "Assert whether expected and form are EQUAL."
  337. `(expand-assert :equal ,form (not (not ,form)) ,expected ,extras :test #'equal))
  338. (defmacro assert-error (condition form &rest extras)
  339. "Assert whether form signals condition."
  340. `(expand-assert :error ,form (expand-error-form ,form)
  341. ,condition ,extras))
  342. (defmacro assert-expands (expansion form &rest extras)
  343. "Assert whether form expands to expansion."
  344. `(expand-assert :macro ,form
  345. (expand-macro-form ,form nil)
  346. ,expansion ,extras))
  347. (defmacro assert-false (form &rest extras)
  348. "Assert whether the form is false."
  349. `(expand-assert :result ,form ,form nil ,extras))
  350. (defmacro assert-equality (test expected form &rest extras)
  351. "Assert whether expected and form are equal according to test."
  352. `(expand-assert :equal ,form ,form ,expected ,extras :test ,test))
  353. (defmacro assert-prints (output form &rest extras)
  354. "Assert whether printing the form generates the output."
  355. `(expand-assert :output ,form (expand-output-form ,form)
  356. ,output ,extras))
  357. (defmacro assert-true (form &rest extras)
  358. "Assert whether the form is true."
  359. `(expand-assert :result ,form ,form t ,extras))
  360. (defmacro expand-assert (type form body expected extras &key (test '#'eql))
  361. "Expand the assertion to the internal format."
  362. `(internal-assert ,type ',form
  363. (lambda () ,body)
  364. (lambda () ,expected)
  365. (expand-extras ,extras)
  366. ,test))
  367. (defmacro expand-error-form (form)
  368. "Wrap the error assertion in HANDLER-CASE."
  369. `(handler-case ,form
  370. (condition (error) error)))
  371. (defmacro expand-output-form (form)
  372. "Capture the output of the form in a string."
  373. (let ((out (gensym)))
  374. `(let* ((,out (make-string-output-stream))
  375. (*standard-output*
  376. (make-broadcast-stream *standard-output* ,out)))
  377. ,form
  378. (get-output-stream-string ,out))))
  379. (defmacro expand-macro-form (form env)
  380. "Expand the macro form once."
  381. `(macroexpand-1 ',form ,env))
  382. (defmacro expand-extras (extras)
  383. "Expand extra forms."
  384. `(lambda ()
  385. (list ,@(mapcan (lambda (form) (list `',form form)) extras))))
  386. ;;; Test passed predicate.
  387. (defgeneric test-passed-p (type expected actual test)
  388. (:documentation
  389. "Return the result of the test."))
  390. (defmethod test-passed-p ((type (eql :error)) expected actual test)
  391. "Return the result of the error assertion."
  392. (declare (ignore test))
  393. (or
  394. (eql (car actual) (car expected))
  395. (typep (car actual) (car expected))))
  396. (defmethod test-passed-p ((type (eql :equal)) expected actual test)
  397. "Return the result of the equality assertion."
  398. (and
  399. (<= (length expected) (length actual))
  400. (every test expected actual)))
  401. (defmethod test-passed-p ((type (eql :macro)) expected actual test)
  402. "Return the result of the macro expansion."
  403. (declare (ignore test))
  404. (equal (car actual) (car expected)))
  405. (defmethod test-passed-p ((type (eql :output)) expected actual test)
  406. "Return the result of the printed output."
  407. (declare (ignore test))
  408. (string=
  409. (string-trim '(#\newline #\return #\space) (car actual))
  410. (car expected)))
  411. ;;; (LOGICALLY-EQUAL x y) => true or false
  412. ;;; Return true if x and y both false or both true
  413. (defun logically-equal (x y)
  414. (eql (not x) (not y)))
  415. (defmethod test-passed-p ((type (eql :result)) expected actual test)
  416. "Return the result of the assertion."
  417. (declare (ignore test))
  418. (logically-equal (car actual) (car expected)))
  419. (defun form-contains-one-of-p (form symbol-list)
  420. ;; returns nil if form contains (recursively) no element of the symbol-list
  421. ;; otherwise it returns the first element of symbol-list that it finds
  422. ;; in form.
  423. (cond
  424. ((symbolp form) (find form symbol-list))
  425. ((listp form) (or (form-contains-one-of-p (car form) symbol-list)
  426. (form-contains-one-of-p (cdr form) symbol-list)))
  427. (t nil)))
  428. (defun internal-assert
  429. (type form code-thunk expected-thunk extras test)
  430. "Perform the assertion and record the results."
  431. (let* ((expected (multiple-value-list (funcall expected-thunk)))
  432. (actual (multiple-value-list (funcall code-thunk)))
  433. (passed (test-passed-p type expected actual test))
  434. (incomplete (or (form-contains-one-of-p form +blanks+)
  435. (form-contains-one-of-p expected '(:blank-value)))))
  436. (cond
  437. (incomplete (progn
  438. (incf *incomplete*)
  439. (push :incomplete *koan-assert-list*)))
  440. (passed (progn
  441. (incf *pass*)
  442. (push :pass *koan-assert-list*)))
  443. (t (progn
  444. (incf *fail*)
  445. (push :fail *koan-assert-list*))))
  446. ;; Report the assertion
  447. (when (and (not passed) *print-failures*)
  448. (print-failure type form expected actual extras))
  449. ;; Return the result
  450. passed))
  451. ;;; results
  452. (defclass test-results ()
  453. ((test-names
  454. :type list
  455. :initarg :test-names
  456. :accessor test-names)
  457. (pass
  458. :type fixnum
  459. :initform 0
  460. :accessor pass)
  461. (fail
  462. :type fixnum
  463. :initform 0
  464. :accessor fail)
  465. (incomplete
  466. :type fixnum
  467. :initform 0
  468. :accessor incomplete)
  469. (exerr
  470. :type fixnum
  471. :initform 0
  472. :accessor exerr)
  473. (failed-tests
  474. :type list
  475. :initform ()
  476. :accessor failed-tests)
  477. (incomplete-tests
  478. :type list
  479. :initform ()
  480. :accessor incomplete-tests)
  481. (error-tests
  482. :type list
  483. :initform ()
  484. :accessor error-tests)
  485. (missing-tests
  486. :type list
  487. :initform ()
  488. :accessor missing-tests))
  489. (:default-initargs :test-names ())
  490. (:documentation
  491. "Store the results of the tests for further evaluation."))
  492. (defmethod print-object ((object test-results) stream)
  493. "Print the summary counts with the object."
  494. (format stream "#<~A Total(~D) Passed(~D) Failed(~D) Incomplete(~D) Errors(~D)>~%"
  495. (class-name (class-of object))
  496. (+ (pass object) (fail object) (incomplete object))
  497. (pass object) (fail object) (incomplete object) (exerr object)))
  498. (defun summarize-results (results)
  499. "Print a summary of all results."
  500. (let ((pass (pass results))
  501. (fail (fail results))
  502. (incomplete (incomplete results)))
  503. (format t "~&Unit Test Summary~%")
  504. (format t " | ~D assertions total~%" (+ pass fail incomplete))
  505. (format t " | ~D passed~%" pass)
  506. (format t " | ~D failed~%" fail)
  507. (format t " | ~D incomplete~%" incomplete)
  508. (format t " | ~D execution errors~%" (exerr results))
  509. (format t " | ~D missing tests~2%"
  510. (length (missing-tests results)))))
  511. ;;; Run the tests
  512. (defun run-code (code)
  513. "Run the code to test the assertions."
  514. (funcall (coerce `(lambda () ,@code) 'function)))
  515. (defun run-test-thunk (code)
  516. (let ((*pass* 0)
  517. (*fail* 0)
  518. (*incomplete* 0))
  519. (handler-bind
  520. ((error (lambda (condition)
  521. (when *print-errors*
  522. (print-error condition))
  523. (if (use-debugger-p condition)
  524. condition
  525. (return-from run-test-thunk
  526. (values *pass* *fail* *incomplete* condition))))))
  527. (run-code code))
  528. ;; Return the result count
  529. (values *pass* *fail* *incomplete* nil)))
  530. (defun run-koan-thunk (code)
  531. (let ((*koan-assert-list* nil))
  532. (handler-bind
  533. ((error (lambda (condition)
  534. (push :error *koan-assert-list*)
  535. (when *print-errors*
  536. (print-error condition))
  537. (if (use-debugger-p condition)
  538. condition
  539. (return-from run-koan-thunk
  540. (values *koan-assert-list* condition))))))
  541. (run-code code))
  542. ;; Return the result count
  543. (values *koan-assert-list* nil)))
  544. (defun koan-result (code)
  545. "Run the code. Return a list of assertion result elements.
  546. An assertion result element is one of :pass, :fail, :error, :incomplete"
  547. (run-koan-thunk code))
  548. (defun record-result (test-name code results)
  549. "Run the test code and record the result."
  550. (multiple-value-bind (pass fail incomplete exerr)
  551. (run-test-thunk code)
  552. (push test-name (test-names results))
  553. ;; Count passed tests
  554. (when (plusp pass)
  555. (incf (pass results) pass))
  556. ;; Count failed tests and record name
  557. (when (plusp fail)
  558. (incf (fail results) fail)
  559. (push test-name (failed-tests results)))
  560. ;; Count incomplete tests and record name
  561. (when (plusp incomplete)
  562. (incf (incomplete results) incomplete)
  563. (push test-name (incomplete-tests results)))
  564. ;; Count errors and record name
  565. (when exerr
  566. (incf (exerr results))
  567. (push test-name (error-tests results)))
  568. ;; Print a summary of the results
  569. (when (or *print-summary* *print-failures* *print-errors*)
  570. (print-summary
  571. test-name pass fail incomplete (when exerr 1)))))
  572. (defun %run-all-thunks (&optional (package *package*))
  573. "Run all of the test thunks in the package."
  574. (loop
  575. with results = (make-instance 'test-results)
  576. for test-name being each hash-key in (package-table package)
  577. using (hash-value unit-test)
  578. if unit-test do
  579. (record-result test-name (code unit-test) results)
  580. else do
  581. (push test-name (missing-tests results))
  582. ;; Summarize and return the test results
  583. finally
  584. (summarize-results results)
  585. (return results)))
  586. (defun %run-thunks (test-names &optional (package *package*))
  587. "Run the list of test thunks in the package."
  588. (loop
  589. with table = (package-table package)
  590. and results = (make-instance 'test-results)
  591. for test-name in test-names
  592. as unit-test = (gethash test-name table)
  593. if unit-test do
  594. (record-result test-name (code unit-test) results)
  595. else do
  596. (push test-name (missing-tests results))
  597. finally
  598. (summarize-results results)
  599. (return results)))
  600. (defun run-koans (package)
  601. "Run the list of test thunks in the package. Stopping
  602. at a failure or incomplete, with more helpful messaging"
  603. (loop
  604. with koan-results = nil
  605. for test-name being each hash-key in (package-table package)
  606. using (hash-value unit-test)
  607. if unit-test do
  608. (push (list test-name (koan-result (code unit-test))) koan-results)
  609. else do
  610. (push (list test-name :missing) koan-results)
  611. until (and (not *proceed-after-failure*) (any-non-pass-p koan-results))
  612. finally (return koan-results)))
  613. (defun any-non-pass-p (koan-results)
  614. (dolist (one-koan koan-results)
  615. (dolist (assert-result (second one-koan))
  616. (if (not (equal :pass assert-result))
  617. (return-from any-non-pass-p t))))
  618. nil)
  619. (defun run-tests (test-names &optional (package *package*))
  620. "Run the specified tests in package."
  621. (if (eq :all test-names)
  622. (%run-all-thunks package)
  623. (%run-thunks test-names package)))
  624. (defun run-tags (tags &optional (package *package*))
  625. "Run the tests associated with the specified tags in package."
  626. (%run-thunks (tagged-tests tags package) package))
  627. ;;; (SET-EQUAL l1 l2 :test) => true or false
  628. ;;; Return true if every element of l1 is an element of l2
  629. ;;; and vice versa.
  630. (defun set-equal (l1 l2 &key (test #'equal))
  631. (and (listp l1)
  632. (listp l2)
  633. (subsetp l1 l2 :test test)
  634. (subsetp l2 l1 :test test)))
  635. (pushnew :lisp-unit common-lisp:*features*)