r5451: *** empty log message ***
[xlunit.git] / assert.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; ID:       $Id: assert.lisp,v 1.3 2003/08/04 12:28:46 kevin Exp $
6 ;;;; Purpose:  Assert functions for XLUnit
7 ;;;;
8 ;;;; *************************************************************************
9
10 (in-package #:xlunit)
11
12
13 ;;; Assertions
14
15 (define-condition test-failure-condition (simple-condition) 
16   ((msg :initform nil :initarg :msg :accessor msg))
17   (:documentation "Base class for all test failures."))
18
19
20 (defun failure-msg (msg &optional format-str &rest args)
21   "Signal a test failure and exit the test."
22   (signal 'test-failure-condition
23           :msg msg
24           :format-control format-str
25           :format-arguments args))
26
27 (defun failure (format-str &rest args)
28   "Signal a test failure and exit the test."
29   (apply #'failure-msg nil format-str args))
30
31 (defmacro test-assert (test &optional msg)
32   `(unless ,test
33     (failure-msg ,msg "Test assertion: ~s" ',test)))
34
35 (defun assert-equal (v1 v2 &optional msg)
36   (unless (equal v1 v2)
37     (failure-msg msg "Test equal: ~S ~S" v1 v2)))
38
39 (defmacro assert-true (v &optional msg)
40   `(unless ,v
41     (failure-msg msg "Not true: ~S" ',v)))
42
43 (defmacro assert-false (v &optional msg)
44   `(when ,v
45      (failure-msg msg "Not false: ~S" ',v)))