Initial import of FiveAM code. This is exactly equal to to bese-2004@common-lisp...
[fiveam.git] / t / example.lisp
1 ;;;; -*- lisp -*-
2
3 (asdf:oos 'asdf:load-op :FiveAM)
4
5 (defpackage :it.bese.FiveAM.example
6   (:use :common-lisp
7         :it.bese.FiveAM))
8
9 (in-package :it.bese.FiveAM.example)
10
11 ;;; First we need some functions to test.
12
13 (defun add-2 (n)
14   (+ n 2))
15
16 (defun add-4 (n) 
17   (+ n 4))
18
19 ;;; Now we need to create a test which makes sure that add-2 and add-4
20 ;;; work as specified.
21
22 ;; we create a test named ADD-2 and supply a short description.
23 (test add-2
24  "Test the ADD-2 function" ;; a short description
25  ;; the checks
26  (is (= 2 (add-2 0)))
27  (is (= 0 (add-2 -2))))
28
29 ;; we can already run add-2. This will return the list of test
30 ;; results, it should be a list of two test-passed objects.
31
32 (run 'add-2) 
33
34 ;; since we'd like to have some kind of readbale output we'll explain
35 ;; the results
36
37 (explain *)
38
39 ;; or we could do both at once:
40
41 (run! 'add-2)
42
43 ;;; So now we've defined and run a single test. Since we plan on
44 ;;; having more than one test and we'd like to run them together let's
45 ;;; create a simple test suite.
46
47 (def-suite example-suite :description "The example test suite.")
48
49 ;; we could explictly specify that every test we create is in the the
50 ;; example-suite suite, but it's easier to just change the default
51 ;; suite:
52
53 (in-suite example-suite)
54
55 ;; now we'll create a new test for the add-4 function.
56
57 (test add-4
58   (is (= 0 (add-4 -4))))
59
60 ;; now let's run the test
61
62 (run! 'add-4)
63
64 ;; we can get the same effect by running the suite:
65
66 (run! 'example-suite)
67
68 ;; since we'd like both add-2 and add-4 to be in the same suite, let's
69 ;; redefine add-2 to be in this suite:
70
71 (test add-2 "Test the ADD-2 function"
72  (is (= 2 (add-2 0)))
73  (is (= 0 (add-2 -2))))
74
75 ;; now we can run the suite and we'll see that both add-2 and add-4
76 ;; have been run (we know this since we no get 4 checks as opposed to
77 ;; 2 as before.
78
79 (run! 'example-suite)
80
81 ;; Just for fun let's see what happens when a test fails. Again we'll
82 ;; redefine add-2, but add in a third, failing, check:
83
84 (test add-2 "Test the ADD-2 function"
85  (is (= 2 (add-2 0)))
86  (is (= 0 (add-2 -2)))
87  (is (= 0 (add-2 0))))