Update domain name to kpe.io
[xlunit.git] / example.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; ID:      $Id$
6 ;;;; Purpose: Example file for XLUnit
7 ;;;;
8 ;;;; *************************************************************************
9
10 (defpackage #:xlunit-example
11   (:use #:cl #:xlunit)
12   (:export #:math-test-suite))
13
14 (in-package #:xlunit-example)
15
16 ;;; First we define some basic test-cases that we are going to need to
17 ;;; perform our tests.  A test-case is a place to hold data we need
18 ;;; during testing.  Often there are many test cases that use the same
19 ;;; data.  Each of these test cases is an instance of a test-case.
20
21 (defclass math-test-case (test-case)
22   ((numbera :accessor numbera)
23    (numberb :accessor numberb))
24   (:documentation "Test test-case for math testing"))
25
26 ;;; Then we define a set-up method for the test-case.  This method is run
27 ;;; prior to perfoming any test with an instance of this test-case.  It
28 ;;; should perform all initialization needed, and assume that it is starting
29 ;;; with a pristine environment, well to a point, use your head here.
30
31 (defmethod set-up ((tcase math-test-case))
32   (setf (numbera tcase) 2)
33   (setf (numberb tcase) 3))
34
35
36 (def-test-method test-addition ((test math-test-case) :run nil)
37   (let ((result (+ (numbera test) (numberb test))))
38     (assert-true (= result 5))))
39
40 (def-test-method test-subtraction ((test math-test-case) :run nil)
41   (let ((result (- (numberb test) (numbera test))))
42     (assert-equal result 1)))
43
44 ;;; This method is meant to signal a failure
45 (def-test-method test-subtraction-2 ((test math-test-case) :run nil)
46   (let ((result (- (numbera test) (numberb test))))
47     (assert-equal result 1 "This is meant to failure")))
48
49 ;;;; Finally we can run our test suite and see how it performs.
50 (textui-test-run (get-suite math-test-case))
51