view src/clojure/test/tap.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 ; Copyright (c) Rich Hickey. All rights reserved.
2 ; The use and distribution terms for this software are covered by the
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 ; which can be found in the file epl-v10.html at the root of this distribution.
5 ; By using this software in any fashion, you are agreeing to be bound by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
9 ;;; test_is/tap.clj: Extension to test for TAP output
11 ;; by Stuart Sierra
12 ;; March 31, 2009
14 ;; Inspired by ClojureCheck by Meikel Brandmeyer:
15 ;; http://kotka.de/projects/clojure/clojurecheck.html
18 ;; DOCUMENTATION
19 ;;
23 (ns ^{:doc "clojure.test extensions for the Test Anything Protocol (TAP)
25 TAP is a simple text-based syntax for reporting test results. TAP
26 was originally develped for Perl, and now has implementations in
27 several languages. For more information on TAP, see
28 http://testanything.org/ and
29 http://search.cpan.org/~petdance/TAP-1.0.0/TAP.pm
31 To use this library, wrap any calls to
32 clojure.test/run-tests in the with-tap-output macro,
33 like this:
35 (use 'clojure.test)
36 (use 'clojure.test.tap)
38 (with-tap-output
39 (run-tests 'my.cool.library))"
40 :author "Stuart Sierra"}
41 clojure.test.tap
42 (:require [clojure.test :as t]
43 [clojure.stacktrace :as stack]))
45 (defn print-tap-plan
46 "Prints a TAP plan line like '1..n'. n is the number of tests"
47 {:added "1.1"}
48 [n]
49 (println (str "1.." n)))
51 (defn print-tap-diagnostic
52 "Prints a TAP diagnostic line. data is a (possibly multi-line)
53 string."
54 {:added "1.1"}
55 [data]
56 (doseq [line (.split ^String data "\n")]
57 (println "#" line)))
59 (defn print-tap-pass
60 "Prints a TAP 'ok' line. msg is a string, with no line breaks"
61 {:added "1.1"}
62 [msg]
63 (println "ok" msg))
65 (defn print-tap-fail
66 "Prints a TAP 'not ok' line. msg is a string, with no line breaks"
67 {:added "1.1"}
68 [msg]
69 (println "not ok" msg))
71 ;; This multimethod will override test/report
72 (defmulti tap-report (fn [data] (:type data)))
74 (defmethod tap-report :default [data]
75 (t/with-test-out
76 (print-tap-diagnostic (pr-str data))))
78 (defmethod tap-report :pass [data]
79 (t/with-test-out
80 (t/inc-report-counter :pass)
81 (print-tap-pass (t/testing-vars-str))
82 (when (seq t/*testing-contexts*)
83 (print-tap-diagnostic (t/testing-contexts-str)))
84 (when (:message data)
85 (print-tap-diagnostic (:message data)))
86 (print-tap-diagnostic (str "expected:" (pr-str (:expected data))))
87 (print-tap-diagnostic (str " actual:" (pr-str (:actual data))))))
89 (defmethod tap-report :error [data]
90 (t/with-test-out
91 (t/inc-report-counter :error)
92 (print-tap-fail (t/testing-vars-str))
93 (when (seq t/*testing-contexts*)
94 (print-tap-diagnostic (t/testing-contexts-str)))
95 (when (:message data)
96 (print-tap-diagnostic (:message data)))
97 (print-tap-diagnostic "expected:" (pr-str (:expected data)))
98 (print-tap-diagnostic " actual: ")
99 (print-tap-diagnostic
100 (with-out-str
101 (if (instance? Throwable (:actual data))
102 (stack/print-cause-trace (:actual data) t/*stack-trace-depth*)
103 (prn (:actual data)))))))
105 (defmethod tap-report :summary [data]
106 (t/with-test-out
107 (print-tap-plan (+ (:pass data) (:fail data) (:error data)))))
110 (defmacro with-tap-output
111 "Execute body with modified test reporting functions that produce
112 TAP output"
113 {:added "1.1"}
114 [& body]
115 `(binding [t/report tap-report]
116 ~@body))