;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; This file is part of "NOVA": NOVA = search + COCOMO tools ; Copyright, 2008, Tim Menzies tim@menzies.us ; ; NOVA is free software: you can redistribute it and/or modify ; it under the terms of the GNU General Public License as published by ; the Free Software Foundation, either version 3 of the License, or ; (at your option) any later version. ; ; NOVA is distributed in the hope that it will be useful, ; but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU General Public License for more details. ; You should have received a copy of the GNU General Public License ; a long with NOVA. If not, see . ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; define tests with (egs key (eg ...) (eg ...)) ;;;; run tests with (demo key) (defparameter *all-tests* nil) (defstruct example in of out failp) (defun eg (in &key (of "example") (out 'any)) "Defines a new test." (make-example :in in :of of :out out)) (defun egs (key &rest tests) "Defines a new test suite." (if (cdr (assoc key *all-tests*)) (setf (cdr (assoc key *all-tests*)) tests) (push (cons key tests) *all-tests*)) key) (defun eg0 (key &optional (all-tests *all-tests*)) "Returns the tests in a test suite." (cdr (assoc key all-tests))) (defun demo (key &key (str t) (all-tests *all-tests*)) "Executes tests and returns the number of failed tests." (let ((n 0) (failed 0) (tests (eg0 key all-tests))) (dolist (test tests failed) (incf n) (format str "; ~a) ~a~%" n (example-of test)) (format str "; IN: ~a~%" (example-in test)) (let* ((want (example-out test)) (got (eval (example-in test))) (happy (or (eql want 'any) (equalp got want)))) (if (example-failp test) (setf happy nil)) (format str "; WANT: ~a~%" want) (format str "; GOT: ~a~%" got) (when (not happy) (format str "; !!! failure !!!~%") (incf failed)))))) (defun demof (key file &key (all-tests *all-tests*)) (with-open-file (*standard-output* file :direction :output :if-exists :supersede) (let ((failures (demo key :all-tests all-tests))) (format t "; failures = ~a~%" failures)))) ;; sample test suite (egs :sample (eg '(+ 1 2) :of "a successful example" :out 3) (eg '(+ 1 2) :of "a failing example" :out 4) (eg '(+ 1 2) :of "an example with any output"))