-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.lisp
455 lines (372 loc) · 15.4 KB
/
example.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
;;;; example.lisp — Example for Confidence
;;;; Confidence (https://github.com/melusina-org/cl-confidence)
;;;; This file is part of Confidence.
;;;;
;;;; Copyright © 2019–2024 Michaël Le Barbier
;;;; All rights reserved.
;;;; This file must be used under the terms of the MIT License.
;;;; This source file is licensed as described in the file LICENSE, which
;;;; you should have received as part of this distribution. The terms
;;;; are also available at https://opensource.org/licenses/MIT
;;;; This example file displays a few features of Confidence. To use it,
;;;; evaluate the toplevel forms one by one and read the comments.
;;;;
;;;; Package Declarations
;;;;
#+quicklisp
(unless (find-package "ORG.MELUSINA.CONFIDENCE")
(ql:quickload "ORG.MELUSINA.CONFIDENCE"))
;; We create a package to host tests we write. We import required symbols so that
;; we avoid to just :use Confidence.
(defpackage #:org.melusina.confidence/example
(:use #:cl)
(:import-from #:org.melusina.confidence
#:define-testcase
#:assert-nil
#:assert-string=
#:assert-t)
(:export
;; A testcase function defined with DEFINE-TESTCASE is always added
;; to the export list.
))
(in-package #:org.melusina.confidence/example)
;;;;
;;;; A Succesful Testcase
;;;;
;; We define a testcase VALIDATE-COMMON-LISP-STRING-DESIGNATOR-A which exercises
;; some aspects of string designator equality for symbols in Common Lisp.
(define-testcase validate-common-lisp-string-designator-a ()
(assert-t (string-equal "a" :A))
(assert-t (string-equal "A" :A))
(assert-nil (string= "a" :A))
(assert-t (string= "A" :A)))
;; We run the VALIDATE-COMMON-LISP-STRING-DESIGNATOR-A TEST, note that
;; the testcase is represented by a function in the export list of
;; the package.
(org.melusina.confidence/example:validate-common-lisp-string-designator-a)
;; When working in SLIME/Emacs, the “Eval defun” (C-M x) command prints
;; the following description message in the slime REPL window
#|
Name: VALIDATE-COMMON-LISP-STRING-DESIGNATOR-A
Total: 4
Success: 4/4 (100%)
Failure: 0/4 (0%)
Condition: 0/4 (0%)
Outcome: Success
|#
;; An object of type ORG.MELUSINA.CONFIDENCE:TESTCASE-RESULT is returned and
;; displayed below the modeline in Emacs.
;;
;; Just issuing the call at the REPL shows the description message and the
;; returned value, displayed below with line breaking for easier reading.
#|
#<ORG.MELUSINA.CONFIDENCE:TESTCASE-RESULT
:NAME ORG.MELUSINA.CONFIDENCE/EXAMPLE:VALIDATE-COMMON-LISP-STRING-DESIGNATOR-A
:ORG.MELUSINA.CONFIDENCE::TOTAL 4
:ORG.MELUSINA.CONFIDENCE::SUCCESS 4
:ORG.MELUSINA.CONFIDENCE::FAILURE 0
:CONDITION 0 {7000000000}
>
|#
;; The description message above is the actual description provided by DESCRIBE for
;; the TESTCASE-RESULT value returned by Confidence.
;;;;
;;;; A Failing Testcase
;;;;
;; We define a testcase DEMONSTRATE-ASSERTION-FAILURE which displays the behaviour
;; of Confidence when an assertion fails.
(define-testcase demonstrate-assertion-failure ()
(assert-string= "AABA"
(concatenate 'string "A" "A" "A" "A")))
(org.melusina.confidence/example:demonstrate-assertion-failure)
;; The following test summary is displayed:
#|
Name: ORG.MELUSINA.CONFIDENCE/EXAMPLE:DEMONSTRATE-ASSERTION-FAILURE
Total: 1
Success: 0/1 (0%)
Failure: 1/1 (100%)
Condition: 0/1 (0%)
Outcome: Failure
================================================================================
#<ASSERTION-FAILURE {7000000000}> is an assertion result of type ASSERTION-FAILURE.
Type: :FUNCTION
Name: ORG.MELUSINA.CONFIDENCE:ASSERT-STRING=
Path:
ORG.MELUSINA.CONFIDENCE/EXAMPLE:DEMONSTRATE-ASSERTION-FAILURE
Arguments:
Argument #1: "AABA"
Argument #2: "AAAA"
Form: (ORG.MELUSINA.CONFIDENCE:ASSERT-STRING= "AABA"
(CONCATENATE 'STRING "A"
"A" "A" "A"))
Outcome: Failure
Description: Assert that STRING1 and STRING2 satisfy the STRING= predicate.
This assertion supports the same keyword parameters as STRING=.
Every character of STRING1 is equal to the character of STRING2 at
the same index upto index 2. However this condition does not hold for characters
at position 2, which are #\B and #\A.
In this call, forms in argument position evaluate as:
STRING1: "AABA"
STRING2: (CONCATENATE 'STRING "A" "A" "A" "A") => "AAAA"
|#
;; The output feature an overall summary before it dives into the details of the failed
;; assertions, after the separation line made of several EQUAL-signs.
;;
;; - The Type of the failed assertion is almost always :FUNCTION,
;; but ASSERT-CONDITION is a macro.
;; - The Name is the symbol name of the failed assertion.
;; - The Path locates the assertion form in the codebase and in the test hierarchy.
;; - The Arguments is the vector of arguments of the assertions.
;; - The Form is the assertion form as it appears in the program.
;; - The Outcome displays a Failure.
;; - The Description is an explanatory text providing context to the assertion failure,
;; especially the result of evaluating forms in argument position in the expression
;; supplied by Form.
;;
;; The Description is a very important field and must be concise while providing
;; enough details so that failure analysis can start without touching a debugger
;; or altering the code.
;;
;; This description message is generated by the assertion and Confidence users who
;; define new assertions with DEFINE-ASSERTION have the chance to define how
;; the description message looks like.
;; The corresponding testcase result values is displayed below, adding linebreaks for
;; readability.
#|
#<ORG.MELUSINA.CONFIDENCE:TESTCASE-RESULT
:NAME ORG.MELUSINA.CONFIDENCE/EXAMPLE:DEMONSTRATE-ASSERTION-FAILURE
:ORG.MELUSINA.CONFIDENCE::TOTAL 1
:ORG.MELUSINA.CONFIDENCE::SUCCESS 0
:ORG.MELUSINA.CONFIDENCE::FAILURE 1
:CONDITION 0 {7000000000}
>
|#
;;;;
;;;; A Testcase where assertion argument evaluation triggers a condition
;;;;
;; We define a testcase DEMONSTRATE-ARGUMENT-FAILURE which displays the behaviour
;; of Confidence when one of the arguments of an assertion yields a condition.
(define-testcase demonstrate-argument-failure ()
(assert-string= "AABA"
(error "AAAA")))
;; We can run the testcase we just defined.
(org.melusina.confidence/example:demonstrate-argument-failure)
;; The resulting testcase result value has its description reproduced below,
;; it is very similar to the description of a failed assertion but also
;; provides details about the condition signalled by the form in argument
;; position.
#|
Name: ORG.MELUSINA.CONFIDENCE/EXAMPLE:DEMONSTRATE-ARGUMENT-FAILURE
Total: 1
Success: 0/1 (0%)
Failure: 0/1 (0%)
Condition: 1/1 (100%)
Outcome: Failure
================================================================================
#<ASSERTION-CONDITION {7000000000}> is an assertion result of type ASSERTION-CONDITION.
Type: :FUNCTION
Name: ORG.MELUSINA.CONFIDENCE:ASSERT-STRING=
Path:
ORG.MELUSINA.CONFIDENCE/EXAMPLE:DEMONSTRATE-ARGUMENT-FAILURE
Arguments:
Argument #1: "AABA"
Argument #2: #<SIMPLE-ERROR "AAAA" {7000000000}>
Form: (ORG.MELUSINA.CONFIDENCE:ASSERT-STRING= "AABA" (ERROR "AAAA"))
Outcome: Condition
Description:
In this call, forms in argument position evaluate as:
STRING1: "AABA"
STRING2: (ERROR "AAAA") => CONDITION
The evaluation of this form yielded a condition
#<SIMPLE-ERROR "AAAA" {7000000000}>
[condition]
Slots with :INSTANCE allocation:
FORMAT-CONTROL = "AAAA"
FORMAT-ARGUMENTS = NIL
|#
;; The corresponding testcase result values is displayed below, adding
;; linebreaks for readability.
#|
#<ORG.MELUSINA.CONFIDENCE:TESTCASE-RESULT
:NAME ORG.MELUSINA.CONFIDENCE/EXAMPLE:DEMONSTRATE-ARGUMENT-FAILURE
:ORG.MELUSINA.CONFIDENCE::TOTAL 1
:ORG.MELUSINA.CONFIDENCE::SUCCESS 0
:ORG.MELUSINA.CONFIDENCE::FAILURE 0
:CONDITION 1 {7000000000}
>
|#
;;;;
;;;; Creating test hierarchies
;;;;
;; Test hierarchies are just test cases calling other test cases in
;; Confidence. Remember that a test case defined by DEFINE-TESTCASE is a
;; function. Hence we can define a test suite called RUN-ALL-TESTS and
;; decorated with a documentation string as follows:
(define-testcase run-all-tests ()
"Run all tests for the Confidence introduction example."
(validate-common-lisp-string-designator-a)
(demonstrate-assertion-failure)
(demonstrate-argument-failure))
;; We can now run all tests and see the different testcase results produced by
;; VALIDATE-COMMON-LISP-STRING-DESIGNATOR-A, DEMONSTRATE-ASSERTION-FAILURE
;; and DEMONSTRATE-ARGUMENT-FAILURE aggregated together in a testcase result
;; whose description is reproduced below. The main difference to the individual
;; description we saw above is the Path attribute in the reports, which locates
;; the failed assertions form in the codebase and in the test hierarchy.
(org.melusina.confidence/example:run-all-tests)
#|
Name: ORG.MELUSINA.CONFIDENCE/EXAMPLE:RUN-ALL-TESTS
Total: 6
Success: 4/6 (67%)
Failure: 1/6 (17%)
Condition: 1/6 (17%)
Outcome: Failure
================================================================================
Name: ORG.MELUSINA.CONFIDENCE/EXAMPLE:DEMONSTRATE-ASSERTION-FAILURE
Total: 1
Success: 0/1 (0%)
Failure: 1/1 (100%)
Condition: 0/1 (0%)
Outcome: Failure
================================================================================
#<ASSERTION-FAILURE {7000000000}> is an assertion result of type ASSERTION-FAILURE.
Type: :FUNCTION
Name: ORG.MELUSINA.CONFIDENCE:ASSERT-STRING=
Path:
ORG.MELUSINA.CONFIDENCE/EXAMPLE:RUN-ALL-TESTS
ORG.MELUSINA.CONFIDENCE/EXAMPLE:DEMONSTRATE-ASSERTION-FAILURE
Arguments:
Argument #1: "AABA"
Argument #2: "AAAA"
Form: (ORG.MELUSINA.CONFIDENCE:ASSERT-STRING= "AABA"
(CONCATENATE 'STRING "A"
"A" "A" "A"))
Outcome: Failure
Description: Assert that STRING1 and STRING2 satisfy the STRING= predicate.
This assertion supports the same keyword parameters as STRING=.
Every character of STRING1 is equal to the character of STRING2 at
the same index upto index 2. However this condition does not hold for characters
at position 2, which are #\B and #\A.
In this call, forms in argument position evaluate as:
STRING1: "AABA"
STRING2: (CONCATENATE 'STRING "A" "A" "A" "A") => "AAAA"
================================================================================
Name: ORG.MELUSINA.CONFIDENCE/EXAMPLE:DEMONSTRATE-ARGUMENT-FAILURE
Total: 1
Success: 0/1 (0%)
Failure: 0/1 (0%)
Condition: 1/1 (100%)
Outcome: Failure
================================================================================
#<ASSERTION-CONDITION {7000000000}> is an assertion result of type ASSERTION-CONDITION.
Type: :FUNCTION
Name: ORG.MELUSINA.CONFIDENCE:ASSERT-STRING=
Path:
ORG.MELUSINA.CONFIDENCE/EXAMPLE:RUN-ALL-TESTS
ORG.MELUSINA.CONFIDENCE/EXAMPLE:DEMONSTRATE-ARGUMENT-FAILURE
Arguments:
Argument #1: "AABA"
Argument #2: #<SIMPLE-ERROR "AAAA" {7000000000}>
Form: (ORG.MELUSINA.CONFIDENCE:ASSERT-STRING= "AABA" (ERROR "AAAA"))
Outcome: Condition
Description:
In this call, forms in argument position evaluate as:
STRING1: "AABA"
STRING2: (ERROR "AAAA") => CONDITION
The evaluation of this form yielded a condition
#<SIMPLE-ERROR "AAAA" {7000000000}>
[condition]
Slots with :INSTANCE allocation:
FORMAT-CONTROL = "AAAA"
FORMAT-ARGUMENTS = NIL
|#
;; The corresponding testcase result values is displayed below, adding linebreaks for
;; readability.
#|
#<ORG.MELUSINA.CONFIDENCE:TESTCASE-RESULT
:NAME ORG.MELUSINA.CONFIDENCE/EXAMPLE:RUN-ALL-TESTS
:ORG.MELUSINA.CONFIDENCE::TOTAL 6
:ORG.MELUSINA.CONFIDENCE::SUCCESS 4
:ORG.MELUSINA.CONFIDENCE::FAILURE 1
:CONDITION 1 {7000000000}
>
|#
;;;;
;;;; Parametrised Tests
;;;;
;; A testcase is a function and can be parametrised, for instance, the testcase
;; VALIDATE-COMMON-LISP-STRING-DESIGNATOR-A could be generalised as
(define-testcase validate-common-lisp-string-designator (keyword uppercase-name lowercase-name)
"Exercise string predicates on KEYWORD and its UPPERCASE-NAME and LOWERCASE-NAME.
This testcase encodes several basic expectations about the relationships between a
keyword name (the name of the symbol) and its string representations."
(assert-t (string-equal lowercase-name keyword))
(assert-t (string-equal uppercase-name keyword))
(assert-nil (string= lowercase-name keyword))
(assert-t (string= uppercase-name keyword)))
;; This allows us to redefine VALIDATE-COMMON-LISP-STRING-DESIGNATOR-A as a specific case:
(define-testcase validate-common-lisp-string-designator-a ()
(validate-common-lisp-string-designator :a "A" "a"))
;; We can then quickly add more tests at an eloquent level of abstraction:
(define-testcase validate-common-lisp-string-designator-batch ()
(validate-common-lisp-string-designator :a "A" "a")
(validate-common-lisp-string-designator :b "B" "b")
(validate-common-lisp-string-designator :c "C" "c"))
;; The latter can also be rewritten using various looping techniques, as displayed by
;; the following examples:
(define-testcase validate-common-lisp-string-designator/dolist ()
(dolist (spec '((:a "A" "a")
(:b "B" "b")
(:c "C" "c")))
(destructuring-bind (keyword uppercase-name lowercase-name) spec
(validate-common-lisp-string-designator keyword uppercase-name lowercase-name))))
(define-testcase validate-common-lisp-string-designator/loop ()
(loop :for (keyword uppercase-name lowercase-name)
:in '((:a "A" "a")
(:b "B" "b")
(:c "C" "c"))
:do (validate-common-lisp-string-designator keyword uppercase-name lowercase-name)))
;;;;
;;;; Running tests from the command line
;;;;
;; When running a testcase from the command line, Confidence prints the test summary
;; and terminates the process with the appropriate exit code. This makes it easy
;; to run testcases from the command line and interpret their results, as in the
;; following example:
#|
sbcl --eval '(ql:quickload "org.melusina.confidence/testsuite" :silent t)"\
--eval "(org.melusina.confidence/testsuite:run-all-tests)"
|#
;; The script `development/testsuite' give a more realistic example how to make
;; running the testsuite or any of its part easy to developers as well as
;; to the continuous delivery pipeline.
;; The strategy that Confidence uses to decide wether to exit after a testcase completed
;; is actually controlled by the special variable *TESTCASE-INTERACTIVE-P*
;; which is initialised appropriately by looking at the :SWANK feature. This heuristic
;; is good enough to tell command line use from interactive use in many cases but can be
;; overridden explicitly by setting the value of *TESTCASE-INTERACTIVE-P*
;; after loading Confidence, as in:
#|
sbcl --eval '(ql:quickload "org.melusina.confidence/testsuite" :silent t)"\
--eval "(setf org.melusina.confidence/testsuite:*testcase-interactive-p*)"\
--eval "(org.melusina.confidence/testsuite:run-all-tests)"
|#
;;;;
;;;; Reference Manual
;;;;
;; There is a reference manual which can be built locally using TexInfo and
;; the “development/makedoc” script or downloaded from GitHub actions:
;;
;; https://github.com/melusina-org/cl-confidence/actions
;;
;; Look for the artefacts on a recent workflow run, three files are available:
;; PDF, HTML and INFO.
;;;;
;;;; Confidence in Public Repositories
;;;;
;; Here is a list of public repositories using Confidence:
;;
;; - https://github.com/melusina-org/cl-confidence
;; - https://github.com/melusina-org/cl-rashell
;; - https://github.com/melusina-org/cl-atelier
;;;; End of file `example.lisp'