X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fsuite.lisp;h=345a0905934e4460e6840f54dd4fced0e6f602e1;hb=71548856f46f8a0c5b171d3ee37c1e2d7b070686;hp=eb2691da4d38a1eb87a7b1928b13e10e4c58c60a;hpb=55740edc3e2b3444e7e17978f68df8eced2b19e7;p=fiveam.git diff --git a/src/suite.lisp b/src/suite.lisp index eb2691d..345a090 100644 --- a/src/suite.lisp +++ b/src/suite.lisp @@ -1,4 +1,4 @@ -;; -*- lisp -*- +;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*- (in-package :it.bese.fiveam) @@ -16,17 +16,34 @@ ;;;; ** Creating Suits -(defmacro def-suite (name &key description in) +(defvar *suites* (make-hash-table :test 'eql)) + +(defmacro def-suite (name &key description (in nil in-p) (fixture nil fixture-p)) "Define a new test-suite named NAME. +NAME:: + The symbol naming the test. + +DESCRIPTION:: + A string describing the contents/purpose of this suite. + IN (a symbol), if provided, causes this suite te be nested in the -suite named by IN. NB: This macro is built on top of make-suite, -as such it, like make-suite, will overrwrite any existing suite -named NAME." +suite named by `IN`. If `IN` is `NIL`, as opposed to not being passed +at all, the new suite will not be a part of any existing suite. + +\[NOTE] +This macro is built on top of `make-suite` as such it, like `make-suite`, +will overrwrite any existing suite named `NAME`. + +DESCRIPTION is just a string. + +FIXTURE is the fixture argument (exactly like the `:fixture` argument to +def-test) to pass to tests in this suite." `(eval-when (:compile-toplevel :load-toplevel :execute) (make-suite ',name ,@(when description `(:description ,description)) - ,@(when in `(:in ',in))) + ,@(when in-p `(:in ',in)) + ,@(when fixture-p `(:fixture ',fixture))) ',name)) (defmacro def-suite* (name &rest def-suite-args) @@ -34,14 +51,25 @@ named NAME." (def-suite ,name ,@def-suite-args) (in-suite ,name))) -(defun make-suite (name &key description in) +(defun remove-from-suites (test-name) + (when (get-test test-name) + ;; if this suite already exists, and its :IN some other suite, remove it. + (dolist (s (list-all-suites)) + (when (gethash test-name (tests s)) + (remhash test-name (tests s)))))) + +(declaim (special *suite*)) + +(defun make-suite (name &key description ((:in parent-suite) *suite*) fixture) "Create a new test suite object. -Overides any existing suite named NAME." - (let ((suite (make-instance 'test-suite :name name))) +Overrides any existing suite named NAME." + (remove-from-suites name) + (let ((suite (make-instance 'test-suite :name name :fixture fixture))) (when description (setf (description suite) description)) - (loop for i in (ensure-list in) + (setf (gethash name *suites*) suite) + (loop for i in (ensure-list parent-suite) for in-suite = (get-test i) do (progn (when (null in-suite) @@ -52,37 +80,46 @@ Overides any existing suite named NAME." (setf (get-test name) suite) suite)) +(defun list-all-suites () + (loop for suite being the hash-value in *suites* + collect suite)) + ;;;; ** Managing the Current Suite -(defvar *suite* (setf (get-test 'NIL) - (make-suite 'NIL :description "Global Suite")) +(defvar *suite* (setf (get-test 'T) (make-suite 'T :description "Default global suite" :in nil)) "The current test suite object") (defmacro in-suite (suite-name) - "Set the *suite* special variable so that all tests defined + "Set the `*suite*` special variable so that all tests defined after the execution of this form are, unless specified otherwise, -in the test-suite named SUITE-NAME. +in the test-suite named `SUITE-NAME`. -See also: DEF-SUITE *SUITE*" +See also: `DEF-SUITE` and `*SUITE*`. " `(eval-when (:compile-toplevel :load-toplevel :execute) (%in-suite ,suite-name))) -(defmacro in-suite* (suite-name &key in) - "Just like in-suite, but silently creates missing suites." - `(%in-suite ,suite-name :in ,in :fail-on-error nil)) - -(defmacro %in-suite (suite-name &key (fail-on-error t) in) - (with-unique-names (suite) - `(progn - (if-bind ,suite (get-test ',suite-name) - (setf *suite* ,suite) - (progn - (when ,fail-on-error - (cerror "Create a new suite named ~A." - "Unkown suite ~A." ',suite-name)) - (setf (get-test ',suite-name) (make-suite ',suite-name :in ',in) - *suite* (get-test ',suite-name)))) - ',suite-name))) +(defmacro in-suite* (suite-name &rest def-suite-args) + "Same effect as `IN-SUITE`, but if `SUITE-NAME` does not exist it +will be created (as per DEF-SUITE)" + `(%in-suite ,suite-name + :fail-on-error nil + ,@def-suite-args)) + +(defmacro %in-suite (suite-name &rest def-suite-args &key fail-on-error &allow-other-keys) + (declare (ignore fail-on-error)) + (with-gensyms (suite) + (let ((fail-on-error (getf def-suite-args :fail-on-error t))) + (remf def-suite-args :fail-on-error) + `(progn + (if-let (,suite (get-test ',suite-name)) + (setf *suite* ,suite) + (progn + (when ,fail-on-error + (cerror "Create a new suite named ~A." + "Unknown suite ~A." ',suite-name)) + (setf (get-test ',suite-name) (make-suite ',suite-name ,@def-suite-args) + *suite* (get-test ',suite-name)))) + ',suite-name)))) ;; Copyright (c) 2002-2003, Edward Marco Baringer ;; All rights reserved.