1 ;;; lunit-report.el --- output test report in XML compatible with JUnitTask
3 ;; Copyright (C) 2004 Daiki Ueno.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; This file is part of Riece.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
29 (luna-define-class lunit-test-reporter (lunit-test-listener)
33 (luna-define-internal-accessors 'lunit-test-reporter))
35 ;; stolen (and renamed) from time-date.el.
36 (defun lunit-time-since (time)
37 "Return the time elapsed since TIME."
38 (let* ((current (current-time))
39 (rest (when (< (nth 1 current) (nth 1 time))
41 (list (- (+ (car current) (if rest -1 0)) (car time))
42 (- (+ (or rest 0) (nth 1 current)) (nth 1 time))
43 (- (nth 2 current) (nth 2 time)))))
45 (defun lunit-escape-quote (string)
47 (while (string-match "\"" string index)
48 (setq string (replace-match """ nil t string)
52 (luna-define-method lunit-test-listener-error ((reporter lunit-test-reporter)
55 (set-buffer (lunit-test-reporter-buffer-internal reporter))
57 <error message=\"%s\" type=\"error\"/>
59 (lunit-escape-quote (pp-to-string error))))))
61 (luna-define-method lunit-test-listener-failure ((reporter lunit-test-reporter)
64 (set-buffer (lunit-test-reporter-buffer-internal reporter))
66 <failure message=\"%s\" type=\"failure\"/>
68 (lunit-escape-quote (pp-to-string failure))))))
70 (luna-define-method lunit-test-listener-start ((reporter lunit-test-reporter)
73 (set-buffer (lunit-test-reporter-buffer-internal reporter))
74 (goto-char (point-max))
75 (narrow-to-region (point) (point))
77 <testcase name=\"%s\" classname=\"%s\">
79 (lunit-test-name-internal case)
80 (luna-class-name case)))
81 (lunit-test-reporter-set-start-time-internal reporter (current-time))))
83 (luna-define-method lunit-test-listener-end ((reporter lunit-test-reporter)
87 (lunit-test-reporter-start-time-internal reporter))))
89 (set-buffer (lunit-test-reporter-buffer-internal reporter))
94 (goto-char (point-min))
95 (looking-at " *<testcase\\>")
96 (goto-char (match-end 0))
97 (insert (format " time=\"%.03f\" "
99 (/ (nth 2 elapsed) 1000000.0))))
102 (defun lunit-report (test file)
103 "Run TEST and output result as XML."
105 (luna-make-entity 'lunit-test-printer))
107 (lunit-make-test-result printer))
108 (buffer (find-file-noselect file))
113 (lunit-test-result-add-listener
115 (luna-make-entity 'lunit-test-reporter :buffer buffer))
116 (setq start-time (current-time))
117 (lunit-test-run test result)
119 (lunit-test-result-assert-count-internal result))
121 (lunit-test-result-failures-internal result))
123 (lunit-test-result-errors-internal result))
125 (elapsed (lunit-time-since start-time)))
126 (princ (format "%d runs, %d assertions, %d failures, %d errors\n"
127 (lunit-test-number-of-tests test)
133 (goto-char (point-min))
135 <?xml version=\"1.0\" encoding=\"UTF-8\"?>
137 <testsuite name=\"\" tests=\"%d\" failures=\"%d\" \
138 errors=\"%d\" time =\"%.03f\">
140 <property name=\"emacs-version\" value=\"%s\"/>
143 (lunit-test-number-of-tests test)
147 (/ (nth 2 elapsed) 1000000.0))
148 (lunit-escape-quote (emacs-version))))
149 (goto-char (point-max))
155 (provide 'lunit-report)