fcbe753d985525007b1c4bad512fadb79edff968
[lisp-unit.git] / extensions / test-anything-protocol.lisp
blobfcbe753d985525007b1c4bad512fadb79edff968
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
2 #|
4 Test Anything Protocol (TAP) support for LISP-UNIT
6 Copyright (c) 2009-2013, Ryan Davis <ryan@acceleration.net>
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the "Software"),
10 to deal in the Software without restriction, including without limitation
11 the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 and/or sell copies of the Software, and to permit persons to whom the
13 Software is furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 OTHER DEALINGS IN THE SOFTWARE.
26 References
27 [TAP]: http://testanything.org/wiki/index.php/Main_Page
31 (in-package :lisp-unit)
33 ;;; Symbols exported from the TAP extension
35 (export '(write-tap write-tap-to-file))
37 (defun %write-tap-test-result (name test-result i stream)
38 "output a single test, taking care to ensure the indentation level is the
39 same before and after invocation."
40 (pprint-logical-block (stream nil)
41 (format stream
42 "~:[ok~;not ok~] ~d - ~s"
43 (or (fail test-result)
44 (exerr test-result))
45 i name)
47 (when (or (fail test-result)
48 (exerr test-result))
49 ;; indent only takes affect after a newline, so force one
50 (format stream "~2I~:@_---~@:_")
51 (when (exerr test-result)
52 (format stream "message: |~4I~_~s~2I~@:_" (exerr test-result)))
53 (when (fail test-result)
54 (format stream "message: ~d failed assertions~@:_"
55 (length (fail test-result))))
56 (format stream "..."))
57 ;; always reset to zero and force a newline
58 (format stream "~0I~@:_")))
60 (defun write-tap (test-results &optional (stream *standard-output*))
61 "write the test results to `stream` in TAP format. Returns the test
62 results."
63 (check-type test-results test-results-db)
64 (let ((i 0))
65 (format stream "TAP version 13~%1..~d~%"
66 (hash-table-count (database test-results)))
67 (maphash
68 #'(lambda (name test-result)
69 (%write-tap-test-result name test-result (incf i) stream))
70 (database test-results)))
71 test-results)
73 (defun write-tap-to-file (test-results path)
74 "write the test results to `path` in TAP format, overwriting `path`. Returns
75 pathname to the output file"
76 (check-type path (or string pathname))
77 (ensure-directories-exist path)
78 (with-open-file (s path :direction :output :if-exists :supersede)
79 (write-tap test-results s))
80 (truename path))